blob: bf4100d1d925619cffc3a3551a17a1d30c3053ca [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,
39 NFA_SKIP_CHAR, /* matches a 0-length char */
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 Moolenaard75799ab72013-06-05 11:05:17 +020084 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020085
86 /* The following are used only in the postfix form, not in the NFA */
87 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
88 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
89 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
90 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
91 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
92
Bram Moolenaar5714b802013-05-28 22:03:20 +020093 NFA_BACKREF1, /* \1 */
94 NFA_BACKREF2, /* \2 */
95 NFA_BACKREF3, /* \3 */
96 NFA_BACKREF4, /* \4 */
97 NFA_BACKREF5, /* \5 */
98 NFA_BACKREF6, /* \6 */
99 NFA_BACKREF7, /* \7 */
100 NFA_BACKREF8, /* \8 */
101 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200102#ifdef FEAT_SYN_HL
103 NFA_ZREF1, /* \z1 */
104 NFA_ZREF2, /* \z2 */
105 NFA_ZREF3, /* \z3 */
106 NFA_ZREF4, /* \z4 */
107 NFA_ZREF5, /* \z5 */
108 NFA_ZREF6, /* \z6 */
109 NFA_ZREF7, /* \z7 */
110 NFA_ZREF8, /* \z8 */
111 NFA_ZREF9, /* \z9 */
112#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200113 NFA_SKIP, /* Skip characters */
114
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200115 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200116 NFA_MOPEN1,
117 NFA_MOPEN2,
118 NFA_MOPEN3,
119 NFA_MOPEN4,
120 NFA_MOPEN5,
121 NFA_MOPEN6,
122 NFA_MOPEN7,
123 NFA_MOPEN8,
124 NFA_MOPEN9,
125
126 NFA_MCLOSE,
127 NFA_MCLOSE1,
128 NFA_MCLOSE2,
129 NFA_MCLOSE3,
130 NFA_MCLOSE4,
131 NFA_MCLOSE5,
132 NFA_MCLOSE6,
133 NFA_MCLOSE7,
134 NFA_MCLOSE8,
135 NFA_MCLOSE9,
136
137#ifdef FEAT_SYN_HL
138 NFA_ZOPEN,
139 NFA_ZOPEN1,
140 NFA_ZOPEN2,
141 NFA_ZOPEN3,
142 NFA_ZOPEN4,
143 NFA_ZOPEN5,
144 NFA_ZOPEN6,
145 NFA_ZOPEN7,
146 NFA_ZOPEN8,
147 NFA_ZOPEN9,
148
149 NFA_ZCLOSE,
150 NFA_ZCLOSE1,
151 NFA_ZCLOSE2,
152 NFA_ZCLOSE3,
153 NFA_ZCLOSE4,
154 NFA_ZCLOSE5,
155 NFA_ZCLOSE6,
156 NFA_ZCLOSE7,
157 NFA_ZCLOSE8,
158 NFA_ZCLOSE9,
159#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200160
161 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200162 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200163 NFA_IDENT, /* Match identifier char */
164 NFA_SIDENT, /* Match identifier char but no digit */
165 NFA_KWORD, /* Match keyword char */
166 NFA_SKWORD, /* Match word char but no digit */
167 NFA_FNAME, /* Match file name char */
168 NFA_SFNAME, /* Match file name char but no digit */
169 NFA_PRINT, /* Match printable char */
170 NFA_SPRINT, /* Match printable char but no digit */
171 NFA_WHITE, /* Match whitespace char */
172 NFA_NWHITE, /* Match non-whitespace char */
173 NFA_DIGIT, /* Match digit char */
174 NFA_NDIGIT, /* Match non-digit char */
175 NFA_HEX, /* Match hex char */
176 NFA_NHEX, /* Match non-hex char */
177 NFA_OCTAL, /* Match octal char */
178 NFA_NOCTAL, /* Match non-octal char */
179 NFA_WORD, /* Match word char */
180 NFA_NWORD, /* Match non-word char */
181 NFA_HEAD, /* Match head char */
182 NFA_NHEAD, /* Match non-head char */
183 NFA_ALPHA, /* Match alpha char */
184 NFA_NALPHA, /* Match non-alpha char */
185 NFA_LOWER, /* Match lowercase char */
186 NFA_NLOWER, /* Match non-lowercase char */
187 NFA_UPPER, /* Match uppercase char */
188 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200189 NFA_LOWER_IC, /* Match [a-z] */
190 NFA_NLOWER_IC, /* Match [^a-z] */
191 NFA_UPPER_IC, /* Match [A-Z] */
192 NFA_NUPPER_IC, /* Match [^A-Z] */
193
194 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
195 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200196
197 NFA_CURSOR, /* Match cursor pos */
198 NFA_LNUM, /* Match line number */
199 NFA_LNUM_GT, /* Match > line number */
200 NFA_LNUM_LT, /* Match < line number */
201 NFA_COL, /* Match cursor column */
202 NFA_COL_GT, /* Match > cursor column */
203 NFA_COL_LT, /* Match < cursor column */
204 NFA_VCOL, /* Match cursor virtual column */
205 NFA_VCOL_GT, /* Match > cursor virtual column */
206 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200207 NFA_MARK, /* Match mark */
208 NFA_MARK_GT, /* Match > mark */
209 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200210 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200212 /* Character classes [:alnum:] etc */
213 NFA_CLASS_ALNUM,
214 NFA_CLASS_ALPHA,
215 NFA_CLASS_BLANK,
216 NFA_CLASS_CNTRL,
217 NFA_CLASS_DIGIT,
218 NFA_CLASS_GRAPH,
219 NFA_CLASS_LOWER,
220 NFA_CLASS_PRINT,
221 NFA_CLASS_PUNCT,
222 NFA_CLASS_SPACE,
223 NFA_CLASS_UPPER,
224 NFA_CLASS_XDIGIT,
225 NFA_CLASS_TAB,
226 NFA_CLASS_RETURN,
227 NFA_CLASS_BACKSPACE,
228 NFA_CLASS_ESCAPE
229};
230
231/* Keep in sync with classchars. */
232static int nfa_classcodes[] = {
233 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
234 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
235 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
236 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
237 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
238 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
239 NFA_UPPER, NFA_NUPPER
240};
241
242static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200245static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaar428e9872013-05-30 17:05:39 +0200247/* NFA regexp \1 .. \9 encountered. */
248static int nfa_has_backref;
249
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200250#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200251/* NFA regexp has \z( ), set zsubexpr. */
252static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200253#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200254
Bram Moolenaar963fee22013-05-26 21:47:28 +0200255/* Number of sub expressions actually being used during execution. 1 if only
256 * the whole match (subexpr 0) is used. */
257static int nfa_nsubexpr;
258
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259static int *post_start; /* holds the postfix form of r.e. */
260static int *post_end;
261static int *post_ptr;
262
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200263static int nstate; /* Number of states in the NFA. Also used when
264 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200265static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266
Bram Moolenaar307aa162013-06-02 16:34:21 +0200267/* If not NULL match must end at this position */
268static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200269
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200270/* listid is global, so that it increases on recursive calls to
271 * nfa_regmatch(), which means we don't have to clear the lastlist field of
272 * all the states. */
273static int nfa_listid;
274static int nfa_alt_listid;
275
276/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
277static int nfa_ll_index = 0;
278
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200279static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200280static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
281static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200282static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200283static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200284static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200285static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286static int nfa_regatom __ARGS((void));
287static int nfa_regpiece __ARGS((void));
288static int nfa_regconcat __ARGS((void));
289static int nfa_regbranch __ARGS((void));
290static int nfa_reg __ARGS((int paren));
291#ifdef DEBUG
292static void nfa_set_code __ARGS((int c));
293static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200294static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
295static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200296static void nfa_dump __ARGS((nfa_regprog_T *prog));
297#endif
298static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200299static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +0200300static void st_error __ARGS((int *postfix, int *end, int *p));
301static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200303static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200304static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200305static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
306static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200307static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200308static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200309static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
310static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200311static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
313static 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 +0200314static int match_follows __ARGS((nfa_state_T *startstate, int depth));
315static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316
317/* helper functions used when doing re2post() ... regatom() parsing */
318#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200319 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200320 return FAIL; \
321 *post_ptr++ = c; \
322 } while (0)
323
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324/*
325 * Initialize internal variables before NFA compilation.
326 * Return OK on success, FAIL otherwise.
327 */
328 static int
329nfa_regcomp_start(expr, re_flags)
330 char_u *expr;
331 int re_flags; /* see vim_regcomp() */
332{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200333 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200334 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200335
336 nstate = 0;
337 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200338 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200339 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200340
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200341 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200342 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200343 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200344
345 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200346 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200348 post_start = (int *)lalloc(postfix_size, TRUE);
349 if (post_start == NULL)
350 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200351 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200352 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200353 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200354 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200355
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200356 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 regcomp_start(expr, re_flags);
358
359 return OK;
360}
361
362/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200363 * Figure out if the NFA state list starts with an anchor, must match at start
364 * of the line.
365 */
366 static int
367nfa_get_reganch(start, depth)
368 nfa_state_T *start;
369 int depth;
370{
371 nfa_state_T *p = start;
372
373 if (depth > 4)
374 return 0;
375
376 while (p != NULL)
377 {
378 switch (p->c)
379 {
380 case NFA_BOL:
381 case NFA_BOF:
382 return 1; /* yes! */
383
384 case NFA_ZSTART:
385 case NFA_ZEND:
386 case NFA_CURSOR:
387 case NFA_VISUAL:
388
389 case NFA_MOPEN:
390 case NFA_MOPEN1:
391 case NFA_MOPEN2:
392 case NFA_MOPEN3:
393 case NFA_MOPEN4:
394 case NFA_MOPEN5:
395 case NFA_MOPEN6:
396 case NFA_MOPEN7:
397 case NFA_MOPEN8:
398 case NFA_MOPEN9:
399 case NFA_NOPEN:
400#ifdef FEAT_SYN_HL
401 case NFA_ZOPEN:
402 case NFA_ZOPEN1:
403 case NFA_ZOPEN2:
404 case NFA_ZOPEN3:
405 case NFA_ZOPEN4:
406 case NFA_ZOPEN5:
407 case NFA_ZOPEN6:
408 case NFA_ZOPEN7:
409 case NFA_ZOPEN8:
410 case NFA_ZOPEN9:
411#endif
412 p = p->out;
413 break;
414
415 case NFA_SPLIT:
416 return nfa_get_reganch(p->out, depth + 1)
417 && nfa_get_reganch(p->out1, depth + 1);
418
419 default:
420 return 0; /* noooo */
421 }
422 }
423 return 0;
424}
425
426/*
427 * Figure out if the NFA state list starts with a character which must match
428 * at start of the match.
429 */
430 static int
431nfa_get_regstart(start, depth)
432 nfa_state_T *start;
433 int depth;
434{
435 nfa_state_T *p = start;
436
437 if (depth > 4)
438 return 0;
439
440 while (p != NULL)
441 {
442 switch (p->c)
443 {
444 /* all kinds of zero-width matches */
445 case NFA_BOL:
446 case NFA_BOF:
447 case NFA_BOW:
448 case NFA_EOW:
449 case NFA_ZSTART:
450 case NFA_ZEND:
451 case NFA_CURSOR:
452 case NFA_VISUAL:
453 case NFA_LNUM:
454 case NFA_LNUM_GT:
455 case NFA_LNUM_LT:
456 case NFA_COL:
457 case NFA_COL_GT:
458 case NFA_COL_LT:
459 case NFA_VCOL:
460 case NFA_VCOL_GT:
461 case NFA_VCOL_LT:
462 case NFA_MARK:
463 case NFA_MARK_GT:
464 case NFA_MARK_LT:
465
466 case NFA_MOPEN:
467 case NFA_MOPEN1:
468 case NFA_MOPEN2:
469 case NFA_MOPEN3:
470 case NFA_MOPEN4:
471 case NFA_MOPEN5:
472 case NFA_MOPEN6:
473 case NFA_MOPEN7:
474 case NFA_MOPEN8:
475 case NFA_MOPEN9:
476 case NFA_NOPEN:
477#ifdef FEAT_SYN_HL
478 case NFA_ZOPEN:
479 case NFA_ZOPEN1:
480 case NFA_ZOPEN2:
481 case NFA_ZOPEN3:
482 case NFA_ZOPEN4:
483 case NFA_ZOPEN5:
484 case NFA_ZOPEN6:
485 case NFA_ZOPEN7:
486 case NFA_ZOPEN8:
487 case NFA_ZOPEN9:
488#endif
489 p = p->out;
490 break;
491
492 case NFA_SPLIT:
493 {
494 int c1 = nfa_get_regstart(p->out, depth + 1);
495 int c2 = nfa_get_regstart(p->out1, depth + 1);
496
497 if (c1 == c2)
498 return c1; /* yes! */
499 return 0;
500 }
501
502 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200503 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200504 return p->c; /* yes! */
505 return 0;
506 }
507 }
508 return 0;
509}
510
511/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200512 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200513 * else. If so return a string in allocated memory with what must match after
514 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200515 */
516 static char_u *
517nfa_get_match_text(start)
518 nfa_state_T *start;
519{
520 nfa_state_T *p = start;
521 int len = 0;
522 char_u *ret;
523 char_u *s;
524
525 if (p->c != NFA_MOPEN)
526 return NULL; /* just in case */
527 p = p->out;
528 while (p->c > 0)
529 {
530 len += MB_CHAR2LEN(p->c);
531 p = p->out;
532 }
533 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
534 return NULL;
535
536 ret = alloc(len);
537 if (ret != NULL)
538 {
539 len = 0;
540 p = start->out->out; /* skip first char, it goes into regstart */
541 s = ret;
542 while (p->c > 0)
543 {
544#ifdef FEAT_MBYTE
545 if (has_mbyte)
546 s += (*mb_char2bytes)(p->c, s);
547 else
548#endif
549 *s++ = p->c;
550 p = p->out;
551 }
552 *s = NUL;
553 }
554 return ret;
555}
556
557/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 * Allocate more space for post_start. Called when
559 * running above the estimated number of states.
560 */
561 static int
562realloc_post_list()
563{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200564 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200565 int new_max = nstate_max + 1000;
566 int *new_start;
567 int *old_start;
568
569 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
570 if (new_start == NULL)
571 return FAIL;
572 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200573 old_start = post_start;
574 post_start = new_start;
575 post_ptr = new_start + (post_ptr - old_start);
576 post_end = post_start + new_max;
577 vim_free(old_start);
578 return OK;
579}
580
581/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 * Search between "start" and "end" and try to recognize a
583 * character class in expanded form. For example [0-9].
584 * On success, return the id the character class to be emitted.
585 * On failure, return 0 (=FAIL)
586 * Start points to the first char of the range, while end should point
587 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200588 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
589 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200590 */
591 static int
592nfa_recognize_char_class(start, end, extra_newl)
593 char_u *start;
594 char_u *end;
595 int extra_newl;
596{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200597# define CLASS_not 0x80
598# define CLASS_af 0x40
599# define CLASS_AF 0x20
600# define CLASS_az 0x10
601# define CLASS_AZ 0x08
602# define CLASS_o7 0x04
603# define CLASS_o9 0x02
604# define CLASS_underscore 0x01
605
606 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200608 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609
610 if (extra_newl == TRUE)
611 newl = TRUE;
612
613 if (*end != ']')
614 return FAIL;
615 p = start;
616 if (*p == '^')
617 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200618 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200619 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200620 }
621
622 while (p < end)
623 {
624 if (p + 2 < end && *(p + 1) == '-')
625 {
626 switch (*p)
627 {
628 case '0':
629 if (*(p + 2) == '9')
630 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200631 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200632 break;
633 }
634 else
635 if (*(p + 2) == '7')
636 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200637 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 break;
639 }
640 case 'a':
641 if (*(p + 2) == 'z')
642 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200643 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 break;
645 }
646 else
647 if (*(p + 2) == 'f')
648 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200649 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200650 break;
651 }
652 case 'A':
653 if (*(p + 2) == 'Z')
654 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200655 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200656 break;
657 }
658 else
659 if (*(p + 2) == 'F')
660 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200661 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200662 break;
663 }
664 /* FALLTHROUGH */
665 default:
666 return FAIL;
667 }
668 p += 3;
669 }
670 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
671 {
672 newl = TRUE;
673 p += 2;
674 }
675 else if (*p == '_')
676 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200677 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200678 p ++;
679 }
680 else if (*p == '\n')
681 {
682 newl = TRUE;
683 p ++;
684 }
685 else
686 return FAIL;
687 } /* while (p < end) */
688
689 if (p != end)
690 return FAIL;
691
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200693 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200694
695 switch (config)
696 {
697 case CLASS_o9:
698 return extra_newl + NFA_DIGIT;
699 case CLASS_not | CLASS_o9:
700 return extra_newl + NFA_NDIGIT;
701 case CLASS_af | CLASS_AF | CLASS_o9:
702 return extra_newl + NFA_HEX;
703 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
704 return extra_newl + NFA_NHEX;
705 case CLASS_o7:
706 return extra_newl + NFA_OCTAL;
707 case CLASS_not | CLASS_o7:
708 return extra_newl + NFA_NOCTAL;
709 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
710 return extra_newl + NFA_WORD;
711 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
712 return extra_newl + NFA_NWORD;
713 case CLASS_az | CLASS_AZ | CLASS_underscore:
714 return extra_newl + NFA_HEAD;
715 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
716 return extra_newl + NFA_NHEAD;
717 case CLASS_az | CLASS_AZ:
718 return extra_newl + NFA_ALPHA;
719 case CLASS_not | CLASS_az | CLASS_AZ:
720 return extra_newl + NFA_NALPHA;
721 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200722 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200723 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200724 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200725 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200726 return extra_newl + NFA_UPPER_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_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200729 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200731}
732
733/*
734 * Produce the bytes for equivalence class "c".
735 * Currently only handles latin1, latin9 and utf-8.
736 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
737 * equivalent to 'a OR b OR c'
738 *
739 * NOTE! When changing this function, also update reg_equi_class()
740 */
741 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200742nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200743 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200744{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200745#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
746#ifdef FEAT_MBYTE
747# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
748#else
749# define EMITMBC(c)
750#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200751
752#ifdef FEAT_MBYTE
753 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
754 || STRCMP(p_enc, "iso-8859-15") == 0)
755#endif
756 {
757 switch (c)
758 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200759 case 'A': case 0300: case 0301: case 0302:
760 case 0303: case 0304: case 0305:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200761 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
762 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
763 EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302);
764 EMIT2(0303); EMIT2(0304); EMIT2(0305);
765 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
766 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
767 EMITMBC(0x1ea2)
768 return OK;
769
770 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
771 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200772 return OK;
773
Bram Moolenaar417bad22013-06-07 14:08:30 +0200774 case 'C': case 0307:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200775 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
776 EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108)
777 EMITMBC(0x10a) EMITMBC(0x10c)
778 return OK;
779
780 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
781 CASEMBC(0x1e0e) CASEMBC(0x1e10)
782 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
783 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200784 return OK;
785
Bram Moolenaar417bad22013-06-07 14:08:30 +0200786 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200787 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
788 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
789 EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312);
790 EMIT2(0313);
791 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
792 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
793 EMITMBC(0x1ebc)
794 return OK;
795
796 case 'F': CASEMBC(0x1e1e)
797 EMIT2('F'); EMITMBC(0x1e1e)
798 return OK;
799
800 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
801 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
802 CASEMBC(0x1e20)
803 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
804 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
805 EMITMBC(0x1f4) EMITMBC(0x1e20)
806 return OK;
807
808 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
809 CASEMBC(0x1e26) CASEMBC(0x1e28)
810 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
811 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200812 return OK;
813
Bram Moolenaar417bad22013-06-07 14:08:30 +0200814 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200815 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
816 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
817 EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316);
818 EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a)
819 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
820 EMITMBC(0x1cf) EMITMBC(0x1ec8)
821 return OK;
822
823 case 'J': CASEMBC(0x134)
824 EMIT2('J'); EMITMBC(0x134)
825 return OK;
826
827 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
828 CASEMBC(0x1e34)
829 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
830 EMITMBC(0x1e34)
831 return OK;
832
833 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
834 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
835 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
836 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
837 return OK;
838
839 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
840 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200841 return OK;
842
Bram Moolenaar417bad22013-06-07 14:08:30 +0200843 case 'N': case 0321:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200844 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
845 CASEMBC(0x1e48)
846 EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145)
847 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200848 return OK;
849
Bram Moolenaar417bad22013-06-07 14:08:30 +0200850 case 'O': case 0322: case 0323: case 0324: case 0325:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200851 case 0326: case 0330:
852 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
853 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
854 EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324);
855 EMIT2(0325); EMIT2(0326); EMIT2(0330);
856 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
857 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
858 EMITMBC(0x1ec) EMITMBC(0x1ece)
859 return OK;
860
861 case 'P': case 0x1e54: case 0x1e56:
862 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
863 return OK;
864
865 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
866 CASEMBC(0x1e58) CASEMBC(0x1e5e)
867 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
868 EMITMBC(0x1e58) EMITMBC(0x1e5e)
869 return OK;
870
871 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
872 CASEMBC(0x160) CASEMBC(0x1e60)
873 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
874 EMITMBC(0x160) EMITMBC(0x1e60)
875 return OK;
876
877 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
878 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
879 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
880 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200881 return OK;
882
Bram Moolenaar417bad22013-06-07 14:08:30 +0200883 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200884 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
885 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
886 CASEMBC(0x1ee6)
887 EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333);
888 EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a)
889 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
890 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
891 EMITMBC(0x1ee6)
892 return OK;
893
894 case 'V': CASEMBC(0x1e7c)
895 EMIT2('V'); EMITMBC(0x1e7c)
896 return OK;
897
898 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
899 CASEMBC(0x1e84) CASEMBC(0x1e86)
900 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
901 EMITMBC(0x1e84) EMITMBC(0x1e86)
902 return OK;
903
904 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
905 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200906 return OK;
907
Bram Moolenaar417bad22013-06-07 14:08:30 +0200908 case 'Y': case 0335:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200909 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
910 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
911 EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178)
912 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
913 EMITMBC(0x1ef8)
914 return OK;
915
916 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
917 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
918 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
919 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200920 return OK;
921
Bram Moolenaar417bad22013-06-07 14:08:30 +0200922 case 'a': case 0340: case 0341: case 0342:
923 case 0343: case 0344: case 0345:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200924 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
925 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
926 EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342);
927 EMIT2(0343); EMIT2(0344); EMIT2(0345);
928 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
929 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
930 EMITMBC(0x1ea3)
931 return OK;
932
933 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
934 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200935 return OK;
936
Bram Moolenaar417bad22013-06-07 14:08:30 +0200937 case 'c': case 0347:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200938 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
939 EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109)
940 EMITMBC(0x10b) EMITMBC(0x10d)
941 return OK;
942
943 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
944 CASEMBC(0x1e11)
945 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) EMITMBC(0x1e0b)
946 EMITMBC(0x01e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200947 return OK;
948
Bram Moolenaar417bad22013-06-07 14:08:30 +0200949 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200950 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
951 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
952 EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352);
953 EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115)
954 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
955 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
956 return OK;
957
958 case 'f': CASEMBC(0x1e1f)
959 EMIT2('f'); EMITMBC(0x1e1f)
960 return OK;
961
962 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
963 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
964 CASEMBC(0x1e21)
965 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
966 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
967 EMITMBC(0x1f5) EMITMBC(0x1e21)
968 return OK;
969
970 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
971 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
972 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
973 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200974 return OK;
975
Bram Moolenaar417bad22013-06-07 14:08:30 +0200976 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200977 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
978 CASEMBC(0x1d0) CASEMBC(0x1ec9)
979 EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356);
980 EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b)
981 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
982 EMITMBC(0x1ec9)
983 return OK;
984
985 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
986 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
987 return OK;
988
989 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
990 CASEMBC(0x1e35)
991 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
992 EMITMBC(0x1e35)
993 return OK;
994
995 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
996 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
997 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
998 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
999 return OK;
1000
1001 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1002 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001003 return OK;
1004
Bram Moolenaar417bad22013-06-07 14:08:30 +02001005 case 'n': case 0361:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001006 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1007 CASEMBC(0x1e45) CASEMBC(0x1e49)
1008 EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146)
1009 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1010 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001011 return OK;
1012
Bram Moolenaar417bad22013-06-07 14:08:30 +02001013 case 'o': case 0362: case 0363: case 0364: case 0365:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001014 case 0366: case 0370:
1015 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1016 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
1017 EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364);
1018 EMIT2(0365); EMIT2(0366); EMIT2(0370);
1019 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1020 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1021 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1022 return OK;
1023
1024 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1025 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1026 return OK;
1027
1028 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1029 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1030 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1031 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1032 return OK;
1033
1034 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1035 CASEMBC(0x161) CASEMBC(0x1e61)
1036 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1037 EMITMBC(0x161) EMITMBC(0x1e61)
1038 return OK;
1039
1040 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1041 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1042 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1043 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001044 return OK;
1045
Bram Moolenaar417bad22013-06-07 14:08:30 +02001046 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001047 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1048 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1049 CASEMBC(0x1ee7)
1050 EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373);
1051 EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b)
1052 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1053 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1054 EMITMBC(0x1ee7)
1055 return OK;
1056
1057 case 'v': CASEMBC(0x1e7d)
1058 EMIT2('v'); EMITMBC(0x1e7d)
1059 return OK;
1060
1061 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1062 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1063 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1064 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1065 return OK;
1066
1067 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1068 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001069 return OK;
1070
Bram Moolenaar417bad22013-06-07 14:08:30 +02001071 case 'y': case 0375: case 0377:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001072 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1073 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1074 EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177)
1075 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1076 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001077 return OK;
1078
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001079 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1080 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1081 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1082 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1083 return OK;
1084
1085 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 }
1087 }
1088
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001089 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001090 return OK;
1091#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001093}
1094
1095/*
1096 * Code to parse regular expression.
1097 *
1098 * We try to reuse parsing functions in regexp.c to
1099 * minimize surprise and keep the syntax consistent.
1100 */
1101
1102/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001103 * Parse the lowest level.
1104 *
1105 * An atom can be one of a long list of items. Many atoms match one character
1106 * in the text. It is often an ordinary character or a character class.
1107 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1108 * is only for syntax highlighting.
1109 *
1110 * atom ::= ordinary-atom
1111 * or \( pattern \)
1112 * or \%( pattern \)
1113 * or \z( pattern \)
1114 */
1115 static int
1116nfa_regatom()
1117{
1118 int c;
1119 int charclass;
1120 int equiclass;
1121 int collclass;
1122 int got_coll_char;
1123 char_u *p;
1124 char_u *endp;
1125#ifdef FEAT_MBYTE
1126 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001127#endif
1128 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001129 int emit_range;
1130 int negated;
1131 int result;
1132 int startc = -1;
1133 int endc = -1;
1134 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001135
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001136 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001137 switch (c)
1138 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001139 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +02001140 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
1141
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001142 case Magic('^'):
1143 EMIT(NFA_BOL);
1144 break;
1145
1146 case Magic('$'):
1147 EMIT(NFA_EOL);
1148#if defined(FEAT_SYN_HL) || defined(PROTO)
1149 had_eol = TRUE;
1150#endif
1151 break;
1152
1153 case Magic('<'):
1154 EMIT(NFA_BOW);
1155 break;
1156
1157 case Magic('>'):
1158 EMIT(NFA_EOW);
1159 break;
1160
1161 case Magic('_'):
1162 c = no_Magic(getchr());
1163 if (c == '^') /* "\_^" is start-of-line */
1164 {
1165 EMIT(NFA_BOL);
1166 break;
1167 }
1168 if (c == '$') /* "\_$" is end-of-line */
1169 {
1170 EMIT(NFA_EOL);
1171#if defined(FEAT_SYN_HL) || defined(PROTO)
1172 had_eol = TRUE;
1173#endif
1174 break;
1175 }
1176
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001177 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001178
1179 /* "\_[" is collection plus newline */
1180 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001181 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001182
1183 /* "\_x" is character class plus newline */
1184 /*FALLTHROUGH*/
1185
1186 /*
1187 * Character classes.
1188 */
1189 case Magic('.'):
1190 case Magic('i'):
1191 case Magic('I'):
1192 case Magic('k'):
1193 case Magic('K'):
1194 case Magic('f'):
1195 case Magic('F'):
1196 case Magic('p'):
1197 case Magic('P'):
1198 case Magic('s'):
1199 case Magic('S'):
1200 case Magic('d'):
1201 case Magic('D'):
1202 case Magic('x'):
1203 case Magic('X'):
1204 case Magic('o'):
1205 case Magic('O'):
1206 case Magic('w'):
1207 case Magic('W'):
1208 case Magic('h'):
1209 case Magic('H'):
1210 case Magic('a'):
1211 case Magic('A'):
1212 case Magic('l'):
1213 case Magic('L'):
1214 case Magic('u'):
1215 case Magic('U'):
1216 p = vim_strchr(classchars, no_Magic(c));
1217 if (p == NULL)
1218 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02001219 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1220 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001221 }
1222#ifdef FEAT_MBYTE
1223 /* When '.' is followed by a composing char ignore the dot, so that
1224 * the composing char is matched here. */
1225 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1226 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001227 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001228 c = getchr();
1229 goto nfa_do_multibyte;
1230 }
1231#endif
1232 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001233 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 {
1235 EMIT(NFA_NEWL);
1236 EMIT(NFA_OR);
1237 regflags |= RF_HASNL;
1238 }
1239 break;
1240
1241 case Magic('n'):
1242 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001243 /* In a string "\n" matches a newline character. */
1244 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245 else
1246 {
1247 /* In buffer text "\n" matches the end of a line. */
1248 EMIT(NFA_NEWL);
1249 regflags |= RF_HASNL;
1250 }
1251 break;
1252
1253 case Magic('('):
1254 if (nfa_reg(REG_PAREN) == FAIL)
1255 return FAIL; /* cascaded error */
1256 break;
1257
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001258 case Magic('|'):
1259 case Magic('&'):
1260 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001261 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262 return FAIL;
1263
1264 case Magic('='):
1265 case Magic('?'):
1266 case Magic('+'):
1267 case Magic('@'):
1268 case Magic('*'):
1269 case Magic('{'):
1270 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001271 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001272 return FAIL;
1273
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001274 case Magic('~'):
1275 {
1276 char_u *lp;
1277
1278 /* Previous substitute pattern.
1279 * Generated as "\%(pattern\)". */
1280 if (reg_prev_sub == NULL)
1281 {
1282 EMSG(_(e_nopresub));
1283 return FAIL;
1284 }
1285 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1286 {
1287 EMIT(PTR2CHAR(lp));
1288 if (lp != reg_prev_sub)
1289 EMIT(NFA_CONCAT);
1290 }
1291 EMIT(NFA_NOPEN);
1292 break;
1293 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001294
Bram Moolenaar428e9872013-05-30 17:05:39 +02001295 case Magic('1'):
1296 case Magic('2'):
1297 case Magic('3'):
1298 case Magic('4'):
1299 case Magic('5'):
1300 case Magic('6'):
1301 case Magic('7'):
1302 case Magic('8'):
1303 case Magic('9'):
1304 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1305 nfa_has_backref = TRUE;
1306 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001307
1308 case Magic('z'):
1309 c = no_Magic(getchr());
1310 switch (c)
1311 {
1312 case 's':
1313 EMIT(NFA_ZSTART);
1314 break;
1315 case 'e':
1316 EMIT(NFA_ZEND);
1317 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001318 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001319#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001320 case '1':
1321 case '2':
1322 case '3':
1323 case '4':
1324 case '5':
1325 case '6':
1326 case '7':
1327 case '8':
1328 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001329 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001330 if (reg_do_extmatch != REX_USE)
1331 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001332 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1333 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001334 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001335 re_has_z = REX_USE;
1336 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001337 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001338 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001339 if (reg_do_extmatch != REX_SET)
1340 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001341 if (nfa_reg(REG_ZPAREN) == FAIL)
1342 return FAIL; /* cascaded error */
1343 re_has_z = REX_SET;
1344 break;
1345#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001346 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001347 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 no_Magic(c));
1349 return FAIL;
1350 }
1351 break;
1352
1353 case Magic('%'):
1354 c = no_Magic(getchr());
1355 switch (c)
1356 {
1357 /* () without a back reference */
1358 case '(':
1359 if (nfa_reg(REG_NPAREN) == FAIL)
1360 return FAIL;
1361 EMIT(NFA_NOPEN);
1362 break;
1363
1364 case 'd': /* %d123 decimal */
1365 case 'o': /* %o123 octal */
1366 case 'x': /* %xab hex 2 */
1367 case 'u': /* %uabcd hex 4 */
1368 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001369 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001370 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001371
Bram Moolenaar47196582013-05-25 22:04:23 +02001372 switch (c)
1373 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001374 case 'd': nr = getdecchrs(); break;
1375 case 'o': nr = getoctchrs(); break;
1376 case 'x': nr = gethexchrs(2); break;
1377 case 'u': nr = gethexchrs(4); break;
1378 case 'U': nr = gethexchrs(8); break;
1379 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001380 }
1381
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001382 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001383 EMSG2_RET_FAIL(
1384 _("E678: Invalid character after %s%%[dxouU]"),
1385 reg_magic == MAGIC_ALL);
1386 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001387 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001388 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001389 break;
1390
1391 /* Catch \%^ and \%$ regardless of where they appear in the
1392 * pattern -- regardless of whether or not it makes sense. */
1393 case '^':
1394 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001395 break;
1396
1397 case '$':
1398 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001399 break;
1400
1401 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001402 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001403 break;
1404
1405 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001406 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 break;
1408
1409 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001410 {
1411 int n;
1412
1413 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001414 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001415 {
1416 if (c == NUL)
1417 EMSG2_RET_FAIL(_(e_missing_sb),
1418 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001419 /* recursive call! */
1420 if (nfa_regatom() == FAIL)
1421 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001422 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001423 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001424 if (n == 0)
1425 EMSG2_RET_FAIL(_(e_empty_sb),
1426 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001427 EMIT(NFA_OPT_CHARS);
1428 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001429
1430 /* Emit as "\%(\%[abc]\)" to be able to handle
1431 * "\%[abc]*" which would cause the empty string to be
1432 * matched an unlimited number of times. NFA_NOPEN is
1433 * added only once at a position, while NFA_SPLIT is
1434 * added multiple times. This is more efficient than
1435 * not allowsing NFA_SPLIT multiple times, it is used
1436 * a lot. */
1437 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001438 break;
1439 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001440
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001441 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001442 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001443 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001444 int cmp = c;
1445
1446 if (c == '<' || c == '>')
1447 c = getchr();
1448 while (VIM_ISDIGIT(c))
1449 {
1450 n = n * 10 + (c - '0');
1451 c = getchr();
1452 }
1453 if (c == 'l' || c == 'c' || c == 'v')
1454 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001455 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001456 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001457 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001458 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001459 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001460 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001461 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001462 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001463 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001464 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001465 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001466 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001467 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001468 break;
1469 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001470 else if (c == '\'' && n == 0)
1471 {
1472 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001473 EMIT(cmp == '<' ? NFA_MARK_LT :
1474 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001475 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001476 break;
1477 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001478 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001479 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1480 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001481 return FAIL;
1482 }
1483 break;
1484
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001485 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001486collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001487 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001488 * [abc] uses NFA_START_COLL - NFA_END_COLL
1489 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1490 * Each character is produced as a regular state, using
1491 * NFA_CONCAT to bind them together.
1492 * Besides normal characters there can be:
1493 * - character classes NFA_CLASS_*
1494 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001495 */
1496
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 p = regparse;
1498 endp = skip_anyof(p);
1499 if (*endp == ']')
1500 {
1501 /*
1502 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001503 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 * and perform the necessary substitutions in the NFA.
1505 */
1506 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001507 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508 if (result != FAIL)
1509 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001510 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001511 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001512 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001513 EMIT(NFA_NEWL);
1514 EMIT(NFA_OR);
1515 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001516 else
1517 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001519 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520 return OK;
1521 }
1522 /*
1523 * Failed to recognize a character class. Use the simple
1524 * version that turns [abc] into 'a' OR 'b' OR 'c'
1525 */
1526 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001527 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001528 if (*regparse == '^') /* negated range */
1529 {
1530 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001531 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001532 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001533 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001534 else
1535 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001536 if (*regparse == '-')
1537 {
1538 startc = '-';
1539 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001540 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001541 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 }
1543 /* Emit the OR branches for each character in the [] */
1544 emit_range = FALSE;
1545 while (regparse < endp)
1546 {
1547 oldstartc = startc;
1548 startc = -1;
1549 got_coll_char = FALSE;
1550 if (*regparse == '[')
1551 {
1552 /* Check for [: :], [= =], [. .] */
1553 equiclass = collclass = 0;
1554 charclass = get_char_class(&regparse);
1555 if (charclass == CLASS_NONE)
1556 {
1557 equiclass = get_equi_class(&regparse);
1558 if (equiclass == 0)
1559 collclass = get_coll_element(&regparse);
1560 }
1561
1562 /* Character class like [:alpha:] */
1563 if (charclass != CLASS_NONE)
1564 {
1565 switch (charclass)
1566 {
1567 case CLASS_ALNUM:
1568 EMIT(NFA_CLASS_ALNUM);
1569 break;
1570 case CLASS_ALPHA:
1571 EMIT(NFA_CLASS_ALPHA);
1572 break;
1573 case CLASS_BLANK:
1574 EMIT(NFA_CLASS_BLANK);
1575 break;
1576 case CLASS_CNTRL:
1577 EMIT(NFA_CLASS_CNTRL);
1578 break;
1579 case CLASS_DIGIT:
1580 EMIT(NFA_CLASS_DIGIT);
1581 break;
1582 case CLASS_GRAPH:
1583 EMIT(NFA_CLASS_GRAPH);
1584 break;
1585 case CLASS_LOWER:
1586 EMIT(NFA_CLASS_LOWER);
1587 break;
1588 case CLASS_PRINT:
1589 EMIT(NFA_CLASS_PRINT);
1590 break;
1591 case CLASS_PUNCT:
1592 EMIT(NFA_CLASS_PUNCT);
1593 break;
1594 case CLASS_SPACE:
1595 EMIT(NFA_CLASS_SPACE);
1596 break;
1597 case CLASS_UPPER:
1598 EMIT(NFA_CLASS_UPPER);
1599 break;
1600 case CLASS_XDIGIT:
1601 EMIT(NFA_CLASS_XDIGIT);
1602 break;
1603 case CLASS_TAB:
1604 EMIT(NFA_CLASS_TAB);
1605 break;
1606 case CLASS_RETURN:
1607 EMIT(NFA_CLASS_RETURN);
1608 break;
1609 case CLASS_BACKSPACE:
1610 EMIT(NFA_CLASS_BACKSPACE);
1611 break;
1612 case CLASS_ESCAPE:
1613 EMIT(NFA_CLASS_ESCAPE);
1614 break;
1615 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001616 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001617 continue;
1618 }
1619 /* Try equivalence class [=a=] and the like */
1620 if (equiclass != 0)
1621 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001622 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001623 if (result == FAIL)
1624 {
1625 /* should never happen */
1626 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1627 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 continue;
1629 }
1630 /* Try collating class like [. .] */
1631 if (collclass != 0)
1632 {
1633 startc = collclass; /* allow [.a.]-x as a range */
1634 /* Will emit the proper atom at the end of the
1635 * while loop. */
1636 }
1637 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001638 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1639 * start character. */
1640 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001641 {
1642 emit_range = TRUE;
1643 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001644 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 continue; /* reading the end of the range */
1646 }
1647
1648 /* Now handle simple and escaped characters.
1649 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1650 * accepts "\t", "\e", etc., but only when the 'l' flag in
1651 * 'cpoptions' is not included.
1652 * Posix doesn't recognize backslash at all.
1653 */
1654 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001655 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001656 && regparse + 1 <= endp
1657 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001658 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001659 && vim_strchr(REGEXP_ABBR, regparse[1])
1660 != NULL)
1661 )
1662 )
1663 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001664 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001665
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001666 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001667 startc = reg_string ? NL : NFA_NEWL;
1668 else
1669 if (*regparse == 'd'
1670 || *regparse == 'o'
1671 || *regparse == 'x'
1672 || *regparse == 'u'
1673 || *regparse == 'U'
1674 )
1675 {
1676 /* TODO(RE) This needs more testing */
1677 startc = coll_get_char();
1678 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001679 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001680 }
1681 else
1682 {
1683 /* \r,\t,\e,\b */
1684 startc = backslash_trans(*regparse);
1685 }
1686 }
1687
1688 /* Normal printable char */
1689 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001690 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001691
1692 /* Previous char was '-', so this char is end of range. */
1693 if (emit_range)
1694 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001695 endc = startc;
1696 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001697 if (startc > endc)
1698 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001699
1700 if (endc > startc + 2)
1701 {
1702 /* Emit a range instead of the sequence of
1703 * individual characters. */
1704 if (startc == 0)
1705 /* \x00 is translated to \x0a, start at \x01. */
1706 EMIT(1);
1707 else
1708 --post_ptr; /* remove NFA_CONCAT */
1709 EMIT(endc);
1710 EMIT(NFA_RANGE);
1711 EMIT(NFA_CONCAT);
1712 }
1713 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001714#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001715 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001716 || (*mb_char2len)(endc) > 1))
1717 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001718 /* Emit the characters in the range.
1719 * "startc" was already emitted, so skip it.
1720 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001721 for (c = startc + 1; c <= endc; c++)
1722 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001723 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001724 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001725 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001726 }
1727 else
1728#endif
1729 {
1730#ifdef EBCDIC
1731 int alpha_only = FALSE;
1732
1733 /* for alphabetical range skip the gaps
1734 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1735 if (isalpha(startc) && isalpha(endc))
1736 alpha_only = TRUE;
1737#endif
1738 /* Emit the range. "startc" was already emitted, so
1739 * skip it. */
1740 for (c = startc + 1; c <= endc; c++)
1741#ifdef EBCDIC
1742 if (!alpha_only || isalpha(startc))
1743#endif
1744 {
1745 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 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001749 emit_range = FALSE;
1750 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001751 }
1752 else
1753 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001754 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001755 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001756 * Normally, simply emit startc. But if we get char
1757 * code=0 from a collating char, then replace it with
1758 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001759 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001760 * the backtracking engine. */
1761 if (startc == NFA_NEWL)
1762 {
1763 /* Line break can't be matched as part of the
1764 * collection, add an OR below. But not for negated
1765 * range. */
1766 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001767 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001768 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001770 {
1771 if (got_coll_char == TRUE && startc == 0)
1772 EMIT(0x0a);
1773 else
1774 EMIT(startc);
1775 EMIT(NFA_CONCAT);
1776 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 }
1778
Bram Moolenaar51a29832013-05-28 22:30:35 +02001779 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780 } /* while (p < endp) */
1781
Bram Moolenaar51a29832013-05-28 22:30:35 +02001782 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001783 if (*regparse == '-') /* if last, '-' is just a char */
1784 {
1785 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001786 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001787 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789 /* skip the trailing ] */
1790 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001791 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001792
1793 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001795 EMIT(NFA_END_NEG_COLL);
1796 else
1797 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001798
1799 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001800 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001801 {
1802 EMIT(reg_string ? NL : NFA_NEWL);
1803 EMIT(NFA_OR);
1804 }
1805
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001806 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001807 } /* if exists closing ] */
1808
1809 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001810 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001811 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001812
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001813 default:
1814 {
1815#ifdef FEAT_MBYTE
1816 int plen;
1817
1818nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001819 /* plen is length of current char with composing chars */
1820 if (enc_utf8 && ((*mb_char2len)(c)
1821 != (plen = (*mb_ptr2len)(old_regparse))
1822 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001824 int i = 0;
1825
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001826 /* A base character plus composing characters, or just one
1827 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001828 * This requires creating a separate atom as if enclosing
1829 * the characters in (), where NFA_COMPOSING is the ( and
1830 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831 * building the postfix form, not the NFA itself;
1832 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001833 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001834 for (;;)
1835 {
1836 EMIT(c);
1837 if (i > 0)
1838 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001839 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001840 break;
1841 c = utf_ptr2char(old_regparse + i);
1842 }
1843 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 regparse = old_regparse + plen;
1845 }
1846 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001847#endif
1848 {
1849 c = no_Magic(c);
1850 EMIT(c);
1851 }
1852 return OK;
1853 }
1854 }
1855
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856 return OK;
1857}
1858
1859/*
1860 * Parse something followed by possible [*+=].
1861 *
1862 * A piece is an atom, possibly followed by a multi, an indication of how many
1863 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1864 * characters: "", "a", "aa", etc.
1865 *
1866 * piece ::= atom
1867 * or atom multi
1868 */
1869 static int
1870nfa_regpiece()
1871{
1872 int i;
1873 int op;
1874 int ret;
1875 long minval, maxval;
1876 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001877 parse_state_T old_state;
1878 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001879 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001880 int old_post_pos;
1881 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001882 int quest;
1883
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001884 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1885 * next. */
1886 save_parse_state(&old_state);
1887
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001888 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001889 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001890
1891 ret = nfa_regatom();
1892 if (ret == FAIL)
1893 return FAIL; /* cascaded error */
1894
1895 op = peekchr();
1896 if (re_multi_type(op) == NOT_MULTI)
1897 return OK;
1898
1899 skipchr();
1900 switch (op)
1901 {
1902 case Magic('*'):
1903 EMIT(NFA_STAR);
1904 break;
1905
1906 case Magic('+'):
1907 /*
1908 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1909 * first and only submatch would be "aaa". But the backtracking
1910 * engine interprets the plus as "try matching one more time", and
1911 * a* matches a second time at the end of the input, the empty
1912 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001913 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001914 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001915 * In order to be consistent with the old engine, we replace
1916 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001917 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001918 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 curchr = -1;
1920 if (nfa_regatom() == FAIL)
1921 return FAIL;
1922 EMIT(NFA_STAR);
1923 EMIT(NFA_CONCAT);
1924 skipchr(); /* skip the \+ */
1925 break;
1926
1927 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001928 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001930 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 switch(op)
1932 {
1933 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001934 /* \@= */
1935 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001937 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001938 /* \@! */
1939 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001940 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001942 op = no_Magic(getchr());
1943 if (op == '=')
1944 /* \@<= */
1945 i = NFA_PREV_ATOM_JUST_BEFORE;
1946 else if (op == '!')
1947 /* \@<! */
1948 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1949 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001951 /* \@> */
1952 i = NFA_PREV_ATOM_LIKE_PATTERN;
1953 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001954 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001955 if (i == 0)
1956 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001957 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1958 return FAIL;
1959 }
1960 EMIT(i);
1961 if (i == NFA_PREV_ATOM_JUST_BEFORE
1962 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1963 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001964 break;
1965
1966 case Magic('?'):
1967 case Magic('='):
1968 EMIT(NFA_QUEST);
1969 break;
1970
1971 case Magic('{'):
1972 /* a{2,5} will expand to 'aaa?a?a?'
1973 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1974 * version of '?'
1975 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1976 * parenthesis have the same id
1977 */
1978
1979 greedy = TRUE;
1980 c2 = peekchr();
1981 if (c2 == '-' || c2 == Magic('-'))
1982 {
1983 skipchr();
1984 greedy = FALSE;
1985 }
1986 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001987 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001988
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1990 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001991 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001992 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001993 if (greedy)
1994 /* \{}, \{0,} */
1995 EMIT(NFA_STAR);
1996 else
1997 /* \{-}, \{-0,} */
1998 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999 break;
2000 }
2001
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002002 /* Special case: x{0} or x{-0} */
2003 if (maxval == 0)
2004 {
2005 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002006 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007 /* NFA_SKIP_CHAR has 0-length and works everywhere */
2008 EMIT(NFA_SKIP_CHAR);
2009 return OK;
2010 }
2011
2012 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002013 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002014 /* Save parse state after the repeated atom and the \{} */
2015 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002016
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002017 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2018 for (i = 0; i < maxval; i++)
2019 {
2020 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002021 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002022 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 if (nfa_regatom() == FAIL)
2024 return FAIL;
2025 /* after "minval" times, atoms are optional */
2026 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002027 {
2028 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002029 {
2030 if (greedy)
2031 EMIT(NFA_STAR);
2032 else
2033 EMIT(NFA_STAR_NONGREEDY);
2034 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002035 else
2036 EMIT(quest);
2037 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002038 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002039 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002040 if (i + 1 > minval && maxval == MAX_LIMIT)
2041 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002042 }
2043
2044 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002045 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002046 curchr = -1;
2047
2048 break;
2049
2050
2051 default:
2052 break;
2053 } /* end switch */
2054
2055 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002057 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058
2059 return OK;
2060}
2061
2062/*
2063 * Parse one or more pieces, concatenated. It matches a match for the
2064 * first piece, followed by a match for the second piece, etc. Example:
2065 * "f[0-9]b", first matches "f", then a digit and then "b".
2066 *
2067 * concat ::= piece
2068 * or piece piece
2069 * or piece piece piece
2070 * etc.
2071 */
2072 static int
2073nfa_regconcat()
2074{
2075 int cont = TRUE;
2076 int first = TRUE;
2077
2078 while (cont)
2079 {
2080 switch (peekchr())
2081 {
2082 case NUL:
2083 case Magic('|'):
2084 case Magic('&'):
2085 case Magic(')'):
2086 cont = FALSE;
2087 break;
2088
2089 case Magic('Z'):
2090#ifdef FEAT_MBYTE
2091 regflags |= RF_ICOMBINE;
2092#endif
2093 skipchr_keepstart();
2094 break;
2095 case Magic('c'):
2096 regflags |= RF_ICASE;
2097 skipchr_keepstart();
2098 break;
2099 case Magic('C'):
2100 regflags |= RF_NOICASE;
2101 skipchr_keepstart();
2102 break;
2103 case Magic('v'):
2104 reg_magic = MAGIC_ALL;
2105 skipchr_keepstart();
2106 curchr = -1;
2107 break;
2108 case Magic('m'):
2109 reg_magic = MAGIC_ON;
2110 skipchr_keepstart();
2111 curchr = -1;
2112 break;
2113 case Magic('M'):
2114 reg_magic = MAGIC_OFF;
2115 skipchr_keepstart();
2116 curchr = -1;
2117 break;
2118 case Magic('V'):
2119 reg_magic = MAGIC_NONE;
2120 skipchr_keepstart();
2121 curchr = -1;
2122 break;
2123
2124 default:
2125 if (nfa_regpiece() == FAIL)
2126 return FAIL;
2127 if (first == FALSE)
2128 EMIT(NFA_CONCAT);
2129 else
2130 first = FALSE;
2131 break;
2132 }
2133 }
2134
2135 return OK;
2136}
2137
2138/*
2139 * Parse a branch, one or more concats, separated by "\&". It matches the
2140 * last concat, but only if all the preceding concats also match at the same
2141 * position. Examples:
2142 * "foobeep\&..." matches "foo" in "foobeep".
2143 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2144 *
2145 * branch ::= concat
2146 * or concat \& concat
2147 * or concat \& concat \& concat
2148 * etc.
2149 */
2150 static int
2151nfa_regbranch()
2152{
2153 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002154 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002155
Bram Moolenaar16299b52013-05-30 18:45:23 +02002156 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002157
2158 /* First branch, possibly the only one */
2159 if (nfa_regconcat() == FAIL)
2160 return FAIL;
2161
2162 ch = peekchr();
2163 /* Try next concats */
2164 while (ch == Magic('&'))
2165 {
2166 skipchr();
2167 EMIT(NFA_NOPEN);
2168 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002169 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002170 if (nfa_regconcat() == FAIL)
2171 return FAIL;
2172 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002173 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002174 EMIT(NFA_SKIP_CHAR);
2175 EMIT(NFA_CONCAT);
2176 ch = peekchr();
2177 }
2178
2179 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002180 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002181 EMIT(NFA_SKIP_CHAR);
2182
2183 return OK;
2184}
2185
2186/*
2187 * Parse a pattern, one or more branches, separated by "\|". It matches
2188 * anything that matches one of the branches. Example: "foo\|beep" matches
2189 * "foo" and matches "beep". If more than one branch matches, the first one
2190 * is used.
2191 *
2192 * pattern ::= branch
2193 * or branch \| branch
2194 * or branch \| branch \| branch
2195 * etc.
2196 */
2197 static int
2198nfa_reg(paren)
2199 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
2200{
2201 int parno = 0;
2202
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002203 if (paren == REG_PAREN)
2204 {
2205 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002206 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002207 parno = regnpar++;
2208 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002209#ifdef FEAT_SYN_HL
2210 else if (paren == REG_ZPAREN)
2211 {
2212 /* Make a ZOPEN node. */
2213 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002214 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002215 parno = regnzpar++;
2216 }
2217#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218
2219 if (nfa_regbranch() == FAIL)
2220 return FAIL; /* cascaded error */
2221
2222 while (peekchr() == Magic('|'))
2223 {
2224 skipchr();
2225 if (nfa_regbranch() == FAIL)
2226 return FAIL; /* cascaded error */
2227 EMIT(NFA_OR);
2228 }
2229
2230 /* Check for proper termination. */
2231 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2232 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233 if (paren == REG_NPAREN)
2234 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2235 else
2236 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2237 }
2238 else if (paren == REG_NOPAREN && peekchr() != NUL)
2239 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002240 if (peekchr() == Magic(')'))
2241 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2242 else
2243 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2244 }
2245 /*
2246 * Here we set the flag allowing back references to this set of
2247 * parentheses.
2248 */
2249 if (paren == REG_PAREN)
2250 {
2251 had_endbrace[parno] = TRUE; /* have seen the close paren */
2252 EMIT(NFA_MOPEN + parno);
2253 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002254#ifdef FEAT_SYN_HL
2255 else if (paren == REG_ZPAREN)
2256 EMIT(NFA_ZOPEN + parno);
2257#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002258
2259 return OK;
2260}
2261
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002262#ifdef DEBUG
2263static char_u code[50];
2264
2265 static void
2266nfa_set_code(c)
2267 int c;
2268{
2269 int addnl = FALSE;
2270
2271 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2272 {
2273 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002274 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002275 }
2276
2277 STRCPY(code, "");
2278 switch (c)
2279 {
2280 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2281 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2282 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2283 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2284 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2285 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2286
Bram Moolenaar5714b802013-05-28 22:03:20 +02002287 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2288 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2289 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2290 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2291 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2292 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2293 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2294 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2295 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002296#ifdef FEAT_SYN_HL
2297 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2298 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2299 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2300 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2301 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2302 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2303 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2304 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2305 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2306#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002307 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2308
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002309 case NFA_PREV_ATOM_NO_WIDTH:
2310 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002311 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2312 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002313 case NFA_PREV_ATOM_JUST_BEFORE:
2314 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2315 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2316 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002317 case NFA_PREV_ATOM_LIKE_PATTERN:
2318 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2319
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002320 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2321 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002322 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002323 case NFA_START_INVISIBLE_FIRST:
2324 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002325 case NFA_START_INVISIBLE_NEG:
2326 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002327 case NFA_START_INVISIBLE_NEG_FIRST:
2328 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002329 case NFA_START_INVISIBLE_BEFORE:
2330 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002331 case NFA_START_INVISIBLE_BEFORE_FIRST:
2332 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002333 case NFA_START_INVISIBLE_BEFORE_NEG:
2334 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002335 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2336 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002337 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002338 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002339 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002340 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002341
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002342 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2343 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002344 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002345
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002346 case NFA_MOPEN:
2347 case NFA_MOPEN1:
2348 case NFA_MOPEN2:
2349 case NFA_MOPEN3:
2350 case NFA_MOPEN4:
2351 case NFA_MOPEN5:
2352 case NFA_MOPEN6:
2353 case NFA_MOPEN7:
2354 case NFA_MOPEN8:
2355 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002356 STRCPY(code, "NFA_MOPEN(x)");
2357 code[10] = c - NFA_MOPEN + '0';
2358 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002359 case NFA_MCLOSE:
2360 case NFA_MCLOSE1:
2361 case NFA_MCLOSE2:
2362 case NFA_MCLOSE3:
2363 case NFA_MCLOSE4:
2364 case NFA_MCLOSE5:
2365 case NFA_MCLOSE6:
2366 case NFA_MCLOSE7:
2367 case NFA_MCLOSE8:
2368 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002369 STRCPY(code, "NFA_MCLOSE(x)");
2370 code[11] = c - NFA_MCLOSE + '0';
2371 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002372#ifdef FEAT_SYN_HL
2373 case NFA_ZOPEN:
2374 case NFA_ZOPEN1:
2375 case NFA_ZOPEN2:
2376 case NFA_ZOPEN3:
2377 case NFA_ZOPEN4:
2378 case NFA_ZOPEN5:
2379 case NFA_ZOPEN6:
2380 case NFA_ZOPEN7:
2381 case NFA_ZOPEN8:
2382 case NFA_ZOPEN9:
2383 STRCPY(code, "NFA_ZOPEN(x)");
2384 code[10] = c - NFA_ZOPEN + '0';
2385 break;
2386 case NFA_ZCLOSE:
2387 case NFA_ZCLOSE1:
2388 case NFA_ZCLOSE2:
2389 case NFA_ZCLOSE3:
2390 case NFA_ZCLOSE4:
2391 case NFA_ZCLOSE5:
2392 case NFA_ZCLOSE6:
2393 case NFA_ZCLOSE7:
2394 case NFA_ZCLOSE8:
2395 case NFA_ZCLOSE9:
2396 STRCPY(code, "NFA_ZCLOSE(x)");
2397 code[11] = c - NFA_ZCLOSE + '0';
2398 break;
2399#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002400 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2401 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2402 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2403 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002404 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2405 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002406 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2407 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2408 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2409 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2410 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2411 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2412 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2413 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2414 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2415 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2416 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2417 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2418 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2419 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2420
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002422 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2423 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2424 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002425 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2426 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002427
2428 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2429 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2430 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2431 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2432 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2433 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2434 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2435
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002436 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2437 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2438 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2439 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2440 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2441 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2442 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2443 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2444 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2445 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2446 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2447 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2448 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2449 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2450 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2451 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2452
2453 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2454 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2455 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2456 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2457 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2458 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2459 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2460 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2461 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2462 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2463 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2464 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2465 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2466 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2467 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2468 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2469 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2470 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2471 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2472 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2473 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2474 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2475 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2476 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2477 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2478 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2479 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002480 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2481 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2482 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2483 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002484
2485 default:
2486 STRCPY(code, "CHAR(x)");
2487 code[5] = c;
2488 }
2489
2490 if (addnl == TRUE)
2491 STRCAT(code, " + NEWLINE ");
2492
2493}
2494
2495#ifdef ENABLE_LOG
2496static FILE *log_fd;
2497
2498/*
2499 * Print the postfix notation of the current regexp.
2500 */
2501 static void
2502nfa_postfix_dump(expr, retval)
2503 char_u *expr;
2504 int retval;
2505{
2506 int *p;
2507 FILE *f;
2508
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002509 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002510 if (f != NULL)
2511 {
2512 fprintf(f, "\n-------------------------\n");
2513 if (retval == FAIL)
2514 fprintf(f, ">>> NFA engine failed ... \n");
2515 else if (retval == OK)
2516 fprintf(f, ">>> NFA engine succeeded !\n");
2517 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002518 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002519 {
2520 nfa_set_code(*p);
2521 fprintf(f, "%s, ", code);
2522 }
2523 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002524 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002525 fprintf(f, "%d ", *p);
2526 fprintf(f, "\n\n");
2527 fclose(f);
2528 }
2529}
2530
2531/*
2532 * Print the NFA starting with a root node "state".
2533 */
2534 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002535nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002536 FILE *debugf;
2537 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002538{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002539 garray_T indent;
2540
2541 ga_init2(&indent, 1, 64);
2542 ga_append(&indent, '\0');
2543 nfa_print_state2(debugf, state, &indent);
2544 ga_clear(&indent);
2545}
2546
2547 static void
2548nfa_print_state2(debugf, state, indent)
2549 FILE *debugf;
2550 nfa_state_T *state;
2551 garray_T *indent;
2552{
2553 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002554
2555 if (state == NULL)
2556 return;
2557
2558 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002559
2560 /* Output indent */
2561 p = (char_u *)indent->ga_data;
2562 if (indent->ga_len >= 3)
2563 {
2564 int last = indent->ga_len - 3;
2565 char_u save[2];
2566
2567 STRNCPY(save, &p[last], 2);
2568 STRNCPY(&p[last], "+-", 2);
2569 fprintf(debugf, " %s", p);
2570 STRNCPY(&p[last], save, 2);
2571 }
2572 else
2573 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002574
2575 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002576 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002577 code,
2578 state->c,
2579 abs(state->id),
2580 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002581 if (state->id < 0)
2582 return;
2583
2584 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002585
2586 /* grow indent for state->out */
2587 indent->ga_len -= 1;
2588 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002589 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002590 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002591 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002592 ga_append(indent, '\0');
2593
2594 nfa_print_state2(debugf, state->out, indent);
2595
2596 /* replace last part of indent for state->out1 */
2597 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002598 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002599 ga_append(indent, '\0');
2600
2601 nfa_print_state2(debugf, state->out1, indent);
2602
2603 /* shrink indent */
2604 indent->ga_len -= 3;
2605 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002606}
2607
2608/*
2609 * Print the NFA state machine.
2610 */
2611 static void
2612nfa_dump(prog)
2613 nfa_regprog_T *prog;
2614{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002615 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002616
2617 if (debugf != NULL)
2618 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002619 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002620
Bram Moolenaar473de612013-06-08 18:19:48 +02002621 if (prog->reganch)
2622 fprintf(debugf, "reganch: %d\n", prog->reganch);
2623 if (prog->regstart != NUL)
2624 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2625 prog->regstart, prog->regstart);
2626 if (prog->match_text != NULL)
2627 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002628
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002629 fclose(debugf);
2630 }
2631}
2632#endif /* ENABLE_LOG */
2633#endif /* DEBUG */
2634
2635/*
2636 * Parse r.e. @expr and convert it into postfix form.
2637 * Return the postfix string on success, NULL otherwise.
2638 */
2639 static int *
2640re2post()
2641{
2642 if (nfa_reg(REG_NOPAREN) == FAIL)
2643 return NULL;
2644 EMIT(NFA_MOPEN);
2645 return post_start;
2646}
2647
2648/* NB. Some of the code below is inspired by Russ's. */
2649
2650/*
2651 * Represents an NFA state plus zero or one or two arrows exiting.
2652 * if c == MATCH, no arrows out; matching state.
2653 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2654 * If c < 256, labeled arrow with character c to out.
2655 */
2656
2657static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2658
2659/*
2660 * Allocate and initialize nfa_state_T.
2661 */
2662 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002663alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002664 int c;
2665 nfa_state_T *out;
2666 nfa_state_T *out1;
2667{
2668 nfa_state_T *s;
2669
2670 if (istate >= nstate)
2671 return NULL;
2672
2673 s = &state_ptr[istate++];
2674
2675 s->c = c;
2676 s->out = out;
2677 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002678 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002679
2680 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002681 s->lastlist[0] = 0;
2682 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002683
2684 return s;
2685}
2686
2687/*
2688 * A partially built NFA without the matching state filled in.
2689 * Frag_T.start points at the start state.
2690 * Frag_T.out is a list of places that need to be set to the
2691 * next state for this fragment.
2692 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002693
2694/* Since the out pointers in the list are always
2695 * uninitialized, we use the pointers themselves
2696 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002698union Ptrlist
2699{
2700 Ptrlist *next;
2701 nfa_state_T *s;
2702};
2703
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002704struct Frag
2705{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002706 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002707 Ptrlist *out;
2708};
2709typedef struct Frag Frag_T;
2710
2711static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2712static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2713static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2714static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2715static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2716static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2717
2718/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002719 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002720 */
2721 static Frag_T
2722frag(start, out)
2723 nfa_state_T *start;
2724 Ptrlist *out;
2725{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002726 Frag_T n;
2727
2728 n.start = start;
2729 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002730 return n;
2731}
2732
2733/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002734 * Create singleton list containing just outp.
2735 */
2736 static Ptrlist *
2737list1(outp)
2738 nfa_state_T **outp;
2739{
2740 Ptrlist *l;
2741
2742 l = (Ptrlist *)outp;
2743 l->next = NULL;
2744 return l;
2745}
2746
2747/*
2748 * Patch the list of states at out to point to start.
2749 */
2750 static void
2751patch(l, s)
2752 Ptrlist *l;
2753 nfa_state_T *s;
2754{
2755 Ptrlist *next;
2756
2757 for (; l; l = next)
2758 {
2759 next = l->next;
2760 l->s = s;
2761 }
2762}
2763
2764
2765/*
2766 * Join the two lists l1 and l2, returning the combination.
2767 */
2768 static Ptrlist *
2769append(l1, l2)
2770 Ptrlist *l1;
2771 Ptrlist *l2;
2772{
2773 Ptrlist *oldl1;
2774
2775 oldl1 = l1;
2776 while (l1->next)
2777 l1 = l1->next;
2778 l1->next = l2;
2779 return oldl1;
2780}
2781
2782/*
2783 * Stack used for transforming postfix form into NFA.
2784 */
2785static Frag_T empty;
2786
2787 static void
2788st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002789 int *postfix UNUSED;
2790 int *end UNUSED;
2791 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002792{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002793#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002794 FILE *df;
2795 int *p2;
2796
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002797 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002798 if (df)
2799 {
2800 fprintf(df, "Error popping the stack!\n");
2801#ifdef DEBUG
2802 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2803#endif
2804 fprintf(df, "Postfix form is: ");
2805#ifdef DEBUG
2806 for (p2 = postfix; p2 < end; p2++)
2807 {
2808 nfa_set_code(*p2);
2809 fprintf(df, "%s, ", code);
2810 }
2811 nfa_set_code(*p);
2812 fprintf(df, "\nCurrent position is: ");
2813 for (p2 = postfix; p2 <= p; p2 ++)
2814 {
2815 nfa_set_code(*p2);
2816 fprintf(df, "%s, ", code);
2817 }
2818#else
2819 for (p2 = postfix; p2 < end; p2++)
2820 {
2821 fprintf(df, "%d, ", *p2);
2822 }
2823 fprintf(df, "\nCurrent position is: ");
2824 for (p2 = postfix; p2 <= p; p2 ++)
2825 {
2826 fprintf(df, "%d, ", *p2);
2827 }
2828#endif
2829 fprintf(df, "\n--------------------------\n");
2830 fclose(df);
2831 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002832#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002833 EMSG(_("E874: (NFA) Could not pop the stack !"));
2834}
2835
2836/*
2837 * Push an item onto the stack.
2838 */
2839 static void
2840st_push(s, p, stack_end)
2841 Frag_T s;
2842 Frag_T **p;
2843 Frag_T *stack_end;
2844{
2845 Frag_T *stackp = *p;
2846
2847 if (stackp >= stack_end)
2848 return;
2849 *stackp = s;
2850 *p = *p + 1;
2851}
2852
2853/*
2854 * Pop an item from the stack.
2855 */
2856 static Frag_T
2857st_pop(p, stack)
2858 Frag_T **p;
2859 Frag_T *stack;
2860{
2861 Frag_T *stackp;
2862
2863 *p = *p - 1;
2864 stackp = *p;
2865 if (stackp < stack)
2866 return empty;
2867 return **p;
2868}
2869
2870/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002871 * Estimate the maximum byte length of anything matching "state".
2872 * When unknown or unlimited return -1.
2873 */
2874 static int
2875nfa_max_width(startstate, depth)
2876 nfa_state_T *startstate;
2877 int depth;
2878{
2879 int l, r;
2880 nfa_state_T *state = startstate;
2881 int len = 0;
2882
2883 /* detect looping in a NFA_SPLIT */
2884 if (depth > 4)
2885 return -1;
2886
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002887 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002888 {
2889 switch (state->c)
2890 {
2891 case NFA_END_INVISIBLE:
2892 case NFA_END_INVISIBLE_NEG:
2893 /* the end, return what we have */
2894 return len;
2895
2896 case NFA_SPLIT:
2897 /* two alternatives, use the maximum */
2898 l = nfa_max_width(state->out, depth + 1);
2899 r = nfa_max_width(state->out1, depth + 1);
2900 if (l < 0 || r < 0)
2901 return -1;
2902 return len + (l > r ? l : r);
2903
2904 case NFA_ANY:
2905 case NFA_START_COLL:
2906 case NFA_START_NEG_COLL:
2907 /* matches some character, including composing chars */
2908#ifdef FEAT_MBYTE
2909 if (enc_utf8)
2910 len += MB_MAXBYTES;
2911 else if (has_mbyte)
2912 len += 2;
2913 else
2914#endif
2915 ++len;
2916 if (state->c != NFA_ANY)
2917 {
2918 /* skip over the characters */
2919 state = state->out1->out;
2920 continue;
2921 }
2922 break;
2923
2924 case NFA_DIGIT:
2925 case NFA_WHITE:
2926 case NFA_HEX:
2927 case NFA_OCTAL:
2928 /* ascii */
2929 ++len;
2930 break;
2931
2932 case NFA_IDENT:
2933 case NFA_SIDENT:
2934 case NFA_KWORD:
2935 case NFA_SKWORD:
2936 case NFA_FNAME:
2937 case NFA_SFNAME:
2938 case NFA_PRINT:
2939 case NFA_SPRINT:
2940 case NFA_NWHITE:
2941 case NFA_NDIGIT:
2942 case NFA_NHEX:
2943 case NFA_NOCTAL:
2944 case NFA_WORD:
2945 case NFA_NWORD:
2946 case NFA_HEAD:
2947 case NFA_NHEAD:
2948 case NFA_ALPHA:
2949 case NFA_NALPHA:
2950 case NFA_LOWER:
2951 case NFA_NLOWER:
2952 case NFA_UPPER:
2953 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002954 case NFA_LOWER_IC:
2955 case NFA_NLOWER_IC:
2956 case NFA_UPPER_IC:
2957 case NFA_NUPPER_IC:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002958 /* possibly non-ascii */
2959#ifdef FEAT_MBYTE
2960 if (has_mbyte)
2961 len += 3;
2962 else
2963#endif
2964 ++len;
2965 break;
2966
2967 case NFA_START_INVISIBLE:
2968 case NFA_START_INVISIBLE_NEG:
2969 case NFA_START_INVISIBLE_BEFORE:
2970 case NFA_START_INVISIBLE_BEFORE_NEG:
2971 /* zero-width, out1 points to the END state */
2972 state = state->out1->out;
2973 continue;
2974
2975 case NFA_BACKREF1:
2976 case NFA_BACKREF2:
2977 case NFA_BACKREF3:
2978 case NFA_BACKREF4:
2979 case NFA_BACKREF5:
2980 case NFA_BACKREF6:
2981 case NFA_BACKREF7:
2982 case NFA_BACKREF8:
2983 case NFA_BACKREF9:
2984#ifdef FEAT_SYN_HL
2985 case NFA_ZREF1:
2986 case NFA_ZREF2:
2987 case NFA_ZREF3:
2988 case NFA_ZREF4:
2989 case NFA_ZREF5:
2990 case NFA_ZREF6:
2991 case NFA_ZREF7:
2992 case NFA_ZREF8:
2993 case NFA_ZREF9:
2994#endif
2995 case NFA_NEWL:
2996 case NFA_SKIP:
2997 /* unknown width */
2998 return -1;
2999
3000 case NFA_BOL:
3001 case NFA_EOL:
3002 case NFA_BOF:
3003 case NFA_EOF:
3004 case NFA_BOW:
3005 case NFA_EOW:
3006 case NFA_MOPEN:
3007 case NFA_MOPEN1:
3008 case NFA_MOPEN2:
3009 case NFA_MOPEN3:
3010 case NFA_MOPEN4:
3011 case NFA_MOPEN5:
3012 case NFA_MOPEN6:
3013 case NFA_MOPEN7:
3014 case NFA_MOPEN8:
3015 case NFA_MOPEN9:
3016#ifdef FEAT_SYN_HL
3017 case NFA_ZOPEN:
3018 case NFA_ZOPEN1:
3019 case NFA_ZOPEN2:
3020 case NFA_ZOPEN3:
3021 case NFA_ZOPEN4:
3022 case NFA_ZOPEN5:
3023 case NFA_ZOPEN6:
3024 case NFA_ZOPEN7:
3025 case NFA_ZOPEN8:
3026 case NFA_ZOPEN9:
3027 case NFA_ZCLOSE:
3028 case NFA_ZCLOSE1:
3029 case NFA_ZCLOSE2:
3030 case NFA_ZCLOSE3:
3031 case NFA_ZCLOSE4:
3032 case NFA_ZCLOSE5:
3033 case NFA_ZCLOSE6:
3034 case NFA_ZCLOSE7:
3035 case NFA_ZCLOSE8:
3036 case NFA_ZCLOSE9:
3037#endif
3038 case NFA_MCLOSE:
3039 case NFA_MCLOSE1:
3040 case NFA_MCLOSE2:
3041 case NFA_MCLOSE3:
3042 case NFA_MCLOSE4:
3043 case NFA_MCLOSE5:
3044 case NFA_MCLOSE6:
3045 case NFA_MCLOSE7:
3046 case NFA_MCLOSE8:
3047 case NFA_MCLOSE9:
3048 case NFA_NOPEN:
3049 case NFA_NCLOSE:
3050
3051 case NFA_LNUM_GT:
3052 case NFA_LNUM_LT:
3053 case NFA_COL_GT:
3054 case NFA_COL_LT:
3055 case NFA_VCOL_GT:
3056 case NFA_VCOL_LT:
3057 case NFA_MARK_GT:
3058 case NFA_MARK_LT:
3059 case NFA_VISUAL:
3060 case NFA_LNUM:
3061 case NFA_CURSOR:
3062 case NFA_COL:
3063 case NFA_VCOL:
3064 case NFA_MARK:
3065
3066 case NFA_ZSTART:
3067 case NFA_ZEND:
3068 case NFA_OPT_CHARS:
3069 case NFA_SKIP_CHAR:
3070 case NFA_START_PATTERN:
3071 case NFA_END_PATTERN:
3072 case NFA_COMPOSING:
3073 case NFA_END_COMPOSING:
3074 /* zero-width */
3075 break;
3076
3077 default:
3078 if (state->c < 0)
3079 /* don't know what this is */
3080 return -1;
3081 /* normal character */
3082 len += MB_CHAR2LEN(state->c);
3083 break;
3084 }
3085
3086 /* normal way to continue */
3087 state = state->out;
3088 }
3089
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003090 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003091 return -1;
3092}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003093
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003094/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003095 * Convert a postfix form into its equivalent NFA.
3096 * Return the NFA start state on success, NULL otherwise.
3097 */
3098 static nfa_state_T *
3099post2nfa(postfix, end, nfa_calc_size)
3100 int *postfix;
3101 int *end;
3102 int nfa_calc_size;
3103{
3104 int *p;
3105 int mopen;
3106 int mclose;
3107 Frag_T *stack = NULL;
3108 Frag_T *stackp = NULL;
3109 Frag_T *stack_end = NULL;
3110 Frag_T e1;
3111 Frag_T e2;
3112 Frag_T e;
3113 nfa_state_T *s;
3114 nfa_state_T *s1;
3115 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003116 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003117
3118 if (postfix == NULL)
3119 return NULL;
3120
Bram Moolenaar053bb602013-05-20 13:55:21 +02003121#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003122#define POP() st_pop(&stackp, stack); \
3123 if (stackp < stack) \
3124 { \
3125 st_error(postfix, end, p); \
3126 return NULL; \
3127 }
3128
3129 if (nfa_calc_size == FALSE)
3130 {
3131 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003132 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003133 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003134 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003135 }
3136
3137 for (p = postfix; p < end; ++p)
3138 {
3139 switch (*p)
3140 {
3141 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003142 /* Concatenation.
3143 * Pay attention: this operator does not exist in the r.e. itself
3144 * (it is implicit, really). It is added when r.e. is translated
3145 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003146 if (nfa_calc_size == TRUE)
3147 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003148 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003149 break;
3150 }
3151 e2 = POP();
3152 e1 = POP();
3153 patch(e1.out, e2.start);
3154 PUSH(frag(e1.start, e2.out));
3155 break;
3156
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003157 case NFA_OR:
3158 /* Alternation */
3159 if (nfa_calc_size == TRUE)
3160 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003161 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003162 break;
3163 }
3164 e2 = POP();
3165 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003166 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003167 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003168 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003169 PUSH(frag(s, append(e1.out, e2.out)));
3170 break;
3171
3172 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003173 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003174 if (nfa_calc_size == TRUE)
3175 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003176 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003177 break;
3178 }
3179 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003180 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003181 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003182 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003183 patch(e.out, s);
3184 PUSH(frag(s, list1(&s->out1)));
3185 break;
3186
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003187 case NFA_STAR_NONGREEDY:
3188 /* Zero or more, prefer zero */
3189 if (nfa_calc_size == TRUE)
3190 {
3191 nstate++;
3192 break;
3193 }
3194 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003195 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003196 if (s == NULL)
3197 goto theend;
3198 patch(e.out, s);
3199 PUSH(frag(s, list1(&s->out)));
3200 break;
3201
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202 case NFA_QUEST:
3203 /* one or zero atoms=> greedy match */
3204 if (nfa_calc_size == TRUE)
3205 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003206 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003207 break;
3208 }
3209 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003210 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003211 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003212 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003213 PUSH(frag(s, append(e.out, list1(&s->out1))));
3214 break;
3215
3216 case NFA_QUEST_NONGREEDY:
3217 /* zero or one atoms => non-greedy match */
3218 if (nfa_calc_size == TRUE)
3219 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003220 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003221 break;
3222 }
3223 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003224 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003225 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003226 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003227 PUSH(frag(s, append(e.out, list1(&s->out))));
3228 break;
3229
Bram Moolenaar417bad22013-06-07 14:08:30 +02003230 case NFA_END_COLL:
3231 case NFA_END_NEG_COLL:
3232 /* On the stack is the sequence starting with NFA_START_COLL or
3233 * NFA_START_NEG_COLL and all possible characters. Patch it to
3234 * add the output to the start. */
3235 if (nfa_calc_size == TRUE)
3236 {
3237 nstate++;
3238 break;
3239 }
3240 e = POP();
3241 s = alloc_state(NFA_END_COLL, NULL, NULL);
3242 if (s == NULL)
3243 goto theend;
3244 patch(e.out, s);
3245 e.start->out1 = s;
3246 PUSH(frag(e.start, list1(&s->out)));
3247 break;
3248
3249 case NFA_RANGE:
3250 /* Before this are two characters, the low and high end of a
3251 * range. Turn them into two states with MIN and MAX. */
3252 if (nfa_calc_size == TRUE)
3253 {
3254 /* nstate += 0; */
3255 break;
3256 }
3257 e2 = POP();
3258 e1 = POP();
3259 e2.start->val = e2.start->c;
3260 e2.start->c = NFA_RANGE_MAX;
3261 e1.start->val = e1.start->c;
3262 e1.start->c = NFA_RANGE_MIN;
3263 patch(e1.out, e2.start);
3264 PUSH(frag(e1.start, e2.out));
3265 break;
3266
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003267 case NFA_SKIP_CHAR:
3268 /* Symbol of 0-length, Used in a repetition
3269 * with max/min count of 0 */
3270 if (nfa_calc_size == TRUE)
3271 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003272 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003273 break;
3274 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003275 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003276 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003277 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003278 PUSH(frag(s, list1(&s->out)));
3279 break;
3280
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003281 case NFA_OPT_CHARS:
3282 {
3283 int n;
3284
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003285 /* \%[abc] implemented as:
3286 * NFA_SPLIT
3287 * +-CHAR(a)
3288 * | +-NFA_SPLIT
3289 * | +-CHAR(b)
3290 * | | +-NFA_SPLIT
3291 * | | +-CHAR(c)
3292 * | | | +-next
3293 * | | +- next
3294 * | +- next
3295 * +- next
3296 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003297 n = *++p; /* get number of characters */
3298 if (nfa_calc_size == TRUE)
3299 {
3300 nstate += n;
3301 break;
3302 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003303 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003304 e1.out = NULL; /* stores list with out1's */
3305 s1 = NULL; /* previous NFA_SPLIT to connect to */
3306 while (n-- > 0)
3307 {
3308 e = POP(); /* get character */
3309 s = alloc_state(NFA_SPLIT, e.start, NULL);
3310 if (s == NULL)
3311 goto theend;
3312 if (e1.out == NULL)
3313 e1 = e;
3314 patch(e.out, s1);
3315 append(e1.out, list1(&s->out1));
3316 s1 = s;
3317 }
3318 PUSH(frag(s, e1.out));
3319 break;
3320 }
3321
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003322 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003323 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003324 case NFA_PREV_ATOM_JUST_BEFORE:
3325 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003326 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003327 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003328 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3329 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003330 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003331 int start_state;
3332 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003333 int n = 0;
3334 nfa_state_T *zend;
3335 nfa_state_T *skip;
3336
Bram Moolenaardecd9542013-06-07 16:31:50 +02003337 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003338 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003339 case NFA_PREV_ATOM_NO_WIDTH:
3340 start_state = NFA_START_INVISIBLE;
3341 end_state = NFA_END_INVISIBLE;
3342 break;
3343 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3344 start_state = NFA_START_INVISIBLE_NEG;
3345 end_state = NFA_END_INVISIBLE_NEG;
3346 break;
3347 case NFA_PREV_ATOM_JUST_BEFORE:
3348 start_state = NFA_START_INVISIBLE_BEFORE;
3349 end_state = NFA_END_INVISIBLE;
3350 break;
3351 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3352 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3353 end_state = NFA_END_INVISIBLE_NEG;
3354 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003355 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003356 start_state = NFA_START_PATTERN;
3357 end_state = NFA_END_PATTERN;
3358 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003359 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003360
3361 if (before)
3362 n = *++p; /* get the count */
3363
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003364 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003365 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003366 * The \@<= operator: match for the preceding atom.
3367 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003369 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003370
3371 if (nfa_calc_size == TRUE)
3372 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003373 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003374 break;
3375 }
3376 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003377 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003378 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003379 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003380
Bram Moolenaar87953742013-06-05 18:52:40 +02003381 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003382 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003383 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003384 if (pattern)
3385 {
3386 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3387 skip = alloc_state(NFA_SKIP, NULL, NULL);
3388 zend = alloc_state(NFA_ZEND, s1, NULL);
3389 s1->out= skip;
3390 patch(e.out, zend);
3391 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003392 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003393 else
3394 {
3395 patch(e.out, s1);
3396 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003397 if (before)
3398 {
3399 if (n <= 0)
3400 /* See if we can guess the maximum width, it avoids a
3401 * lot of pointless tries. */
3402 n = nfa_max_width(e.start, 0);
3403 s->val = n; /* store the count */
3404 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003405 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003406 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003407 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003408
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003409#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003410 case NFA_COMPOSING: /* char with composing char */
3411#if 0
3412 /* TODO */
3413 if (regflags & RF_ICOMBINE)
3414 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003415 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003416 }
3417#endif
3418 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003419#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003420
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003421 case NFA_MOPEN: /* \( \) Submatch */
3422 case NFA_MOPEN1:
3423 case NFA_MOPEN2:
3424 case NFA_MOPEN3:
3425 case NFA_MOPEN4:
3426 case NFA_MOPEN5:
3427 case NFA_MOPEN6:
3428 case NFA_MOPEN7:
3429 case NFA_MOPEN8:
3430 case NFA_MOPEN9:
3431#ifdef FEAT_SYN_HL
3432 case NFA_ZOPEN: /* \z( \) Submatch */
3433 case NFA_ZOPEN1:
3434 case NFA_ZOPEN2:
3435 case NFA_ZOPEN3:
3436 case NFA_ZOPEN4:
3437 case NFA_ZOPEN5:
3438 case NFA_ZOPEN6:
3439 case NFA_ZOPEN7:
3440 case NFA_ZOPEN8:
3441 case NFA_ZOPEN9:
3442#endif
3443 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003444 if (nfa_calc_size == TRUE)
3445 {
3446 nstate += 2;
3447 break;
3448 }
3449
3450 mopen = *p;
3451 switch (*p)
3452 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003453 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3454#ifdef FEAT_SYN_HL
3455 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3456 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3457 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3458 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3459 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3460 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3461 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3462 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3463 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3464 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3465#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003466#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003467 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003468#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003469 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003470 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003471 mclose = *p + NSUBEXP;
3472 break;
3473 }
3474
3475 /* Allow "NFA_MOPEN" as a valid postfix representation for
3476 * the empty regexp "". In this case, the NFA will be
3477 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3478 * empty groups of parenthesis, and empty mbyte chars */
3479 if (stackp == stack)
3480 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003481 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003483 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003484 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003485 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003486 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 patch(list1(&s->out), s1);
3488 PUSH(frag(s, list1(&s1->out)));
3489 break;
3490 }
3491
3492 /* At least one node was emitted before NFA_MOPEN, so
3493 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3494 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003495 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003496 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003497 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498
Bram Moolenaar525666f2013-06-02 16:40:55 +02003499 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003500 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003501 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003502 patch(e.out, s1);
3503
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003504#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003505 if (mopen == NFA_COMPOSING)
3506 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003507 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003508#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509
3510 PUSH(frag(s, list1(&s1->out)));
3511 break;
3512
Bram Moolenaar5714b802013-05-28 22:03:20 +02003513 case NFA_BACKREF1:
3514 case NFA_BACKREF2:
3515 case NFA_BACKREF3:
3516 case NFA_BACKREF4:
3517 case NFA_BACKREF5:
3518 case NFA_BACKREF6:
3519 case NFA_BACKREF7:
3520 case NFA_BACKREF8:
3521 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003522#ifdef FEAT_SYN_HL
3523 case NFA_ZREF1:
3524 case NFA_ZREF2:
3525 case NFA_ZREF3:
3526 case NFA_ZREF4:
3527 case NFA_ZREF5:
3528 case NFA_ZREF6:
3529 case NFA_ZREF7:
3530 case NFA_ZREF8:
3531 case NFA_ZREF9:
3532#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003533 if (nfa_calc_size == TRUE)
3534 {
3535 nstate += 2;
3536 break;
3537 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003538 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003539 if (s == NULL)
3540 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003541 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003542 if (s1 == NULL)
3543 goto theend;
3544 patch(list1(&s->out), s1);
3545 PUSH(frag(s, list1(&s1->out)));
3546 break;
3547
Bram Moolenaar423532e2013-05-29 21:14:42 +02003548 case NFA_LNUM:
3549 case NFA_LNUM_GT:
3550 case NFA_LNUM_LT:
3551 case NFA_VCOL:
3552 case NFA_VCOL_GT:
3553 case NFA_VCOL_LT:
3554 case NFA_COL:
3555 case NFA_COL_GT:
3556 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003557 case NFA_MARK:
3558 case NFA_MARK_GT:
3559 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003560 {
3561 int n = *++p; /* lnum, col or mark name */
3562
Bram Moolenaar423532e2013-05-29 21:14:42 +02003563 if (nfa_calc_size == TRUE)
3564 {
3565 nstate += 1;
3566 break;
3567 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003568 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003569 if (s == NULL)
3570 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003571 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003572 PUSH(frag(s, list1(&s->out)));
3573 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003574 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003575
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003576 case NFA_ZSTART:
3577 case NFA_ZEND:
3578 default:
3579 /* Operands */
3580 if (nfa_calc_size == TRUE)
3581 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003582 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003583 break;
3584 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003585 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003586 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003587 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003588 PUSH(frag(s, list1(&s->out)));
3589 break;
3590
3591 } /* switch(*p) */
3592
3593 } /* for(p = postfix; *p; ++p) */
3594
3595 if (nfa_calc_size == TRUE)
3596 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003597 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003598 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 }
3600
3601 e = POP();
3602 if (stackp != stack)
3603 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3604
3605 if (istate >= nstate)
3606 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3607
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003608 matchstate = &state_ptr[istate++]; /* the match state */
3609 matchstate->c = NFA_MATCH;
3610 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003611 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003612
3613 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003614 ret = e.start;
3615
3616theend:
3617 vim_free(stack);
3618 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619
3620#undef POP1
3621#undef PUSH1
3622#undef POP2
3623#undef PUSH2
3624#undef POP
3625#undef PUSH
3626}
3627
Bram Moolenaara2947e22013-06-11 22:44:09 +02003628/*
3629 * After building the NFA program, inspect it to add optimization hints.
3630 */
3631 static void
3632nfa_postprocess(prog)
3633 nfa_regprog_T *prog;
3634{
3635 int i;
3636 int c;
3637
3638 for (i = 0; i < prog->nstate; ++i)
3639 {
3640 c = prog->state[i].c;
3641 if (c == NFA_START_INVISIBLE
3642 || c == NFA_START_INVISIBLE_NEG
3643 || c == NFA_START_INVISIBLE_BEFORE
3644 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3645 {
3646 int directly;
3647
3648 /* Do it directly when what follows is possibly the end of the
3649 * match. */
3650 if (match_follows(prog->state[i].out1->out, 0))
3651 directly = TRUE;
3652 else
3653 {
3654 int ch_invisible = failure_chance(prog->state[i].out, 0);
3655 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3656
3657 /* Postpone when the invisible match is expensive or has a
3658 * lower chance of failing. */
3659 if (c == NFA_START_INVISIBLE_BEFORE
3660 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3661 {
3662 /* "before" matches are very expensive when
3663 * unbounded, always prefer what follows then,
3664 * unless what follows will always match.
3665 * Otherwise strongly prefer what follows. */
3666 if (prog->state[i].val <= 0 && ch_follows > 0)
3667 directly = FALSE;
3668 else
3669 directly = ch_follows * 10 < ch_invisible;
3670 }
3671 else
3672 {
3673 /* normal invisible, first do the one with the
3674 * highest failure chance */
3675 directly = ch_follows < ch_invisible;
3676 }
3677 }
3678 if (directly)
3679 /* switch to the _FIRST state */
3680 ++prog->state[i].c;
3681 }
3682 }
3683}
3684
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003685/****************************************************************
3686 * NFA execution code.
3687 ****************************************************************/
3688
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003689typedef struct
3690{
3691 int in_use; /* number of subexpr with useful info */
3692
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003693 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003694 union
3695 {
3696 struct multipos
3697 {
3698 lpos_T start;
3699 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003700 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003701 struct linepos
3702 {
3703 char_u *start;
3704 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003705 } line[NSUBEXP];
3706 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003707} regsub_T;
3708
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003709typedef struct
3710{
3711 regsub_T norm; /* \( .. \) matches */
3712#ifdef FEAT_SYN_HL
3713 regsub_T synt; /* \z( .. \) matches */
3714#endif
3715} regsubs_T;
3716
Bram Moolenaara2d95102013-06-04 14:23:05 +02003717/* nfa_pim_T stores a Postponed Invisible Match. */
3718typedef struct nfa_pim_S nfa_pim_T;
3719struct nfa_pim_S
3720{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003721 int result; /* NFA_PIM_*, see below */
3722 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003723 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003724 union
3725 {
3726 lpos_T pos;
3727 char_u *ptr;
3728 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003729};
3730
3731/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003732#define NFA_PIM_UNUSED 0 /* pim not used */
3733#define NFA_PIM_TODO 1 /* pim not done yet */
3734#define NFA_PIM_MATCH 2 /* pim executed, matches */
3735#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003736
3737
Bram Moolenaar963fee22013-05-26 21:47:28 +02003738/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003739typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003740{
3741 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003742 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003743 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3744 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003745 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003746} nfa_thread_T;
3747
Bram Moolenaar963fee22013-05-26 21:47:28 +02003748/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749typedef struct
3750{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003751 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003752 int n; /* nr of states currently in "t" */
3753 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003754 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003755 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003756} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003757
Bram Moolenaar5714b802013-05-28 22:03:20 +02003758#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003759static void log_subsexpr __ARGS((regsubs_T *subs));
3760static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003761static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003762
3763 static void
3764log_subsexpr(subs)
3765 regsubs_T *subs;
3766{
3767 log_subexpr(&subs->norm);
3768# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003769 if (nfa_has_zsubexpr)
3770 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003771# endif
3772}
3773
Bram Moolenaar5714b802013-05-28 22:03:20 +02003774 static void
3775log_subexpr(sub)
3776 regsub_T *sub;
3777{
3778 int j;
3779
3780 for (j = 0; j < sub->in_use; j++)
3781 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003782 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003783 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003784 sub->list.multi[j].start.col,
3785 (int)sub->list.multi[j].start.lnum,
3786 sub->list.multi[j].end.col,
3787 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003788 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003789 {
3790 char *s = (char *)sub->list.line[j].start;
3791 char *e = (char *)sub->list.line[j].end;
3792
Bram Moolenaar87953742013-06-05 18:52:40 +02003793 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003794 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003795 s == NULL ? "NULL" : s,
3796 e == NULL ? "NULL" : e);
3797 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003798}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003799
3800 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003801pim_info(pim)
3802 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003803{
3804 static char buf[30];
3805
3806 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3807 buf[0] = NUL;
3808 else
3809 {
3810 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3811 : (int)(pim->end.ptr - reginput));
3812 }
3813 return buf;
3814}
3815
Bram Moolenaar5714b802013-05-28 22:03:20 +02003816#endif
3817
Bram Moolenaar963fee22013-05-26 21:47:28 +02003818/* Used during execution: whether a match has been found. */
3819static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003820
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003821static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003822static void clear_sub __ARGS((regsub_T *sub));
3823static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3824static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003825static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003826static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003827static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3828static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003829static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003830static 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 +02003831static 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 +02003832
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003833/*
3834 * Copy postponed invisible match info from "from" to "to".
3835 */
3836 static void
3837copy_pim(to, from)
3838 nfa_pim_T *to;
3839 nfa_pim_T *from;
3840{
3841 to->result = from->result;
3842 to->state = from->state;
3843 copy_sub(&to->subs.norm, &from->subs.norm);
3844#ifdef FEAT_SYN_HL
3845 if (nfa_has_zsubexpr)
3846 copy_sub(&to->subs.synt, &from->subs.synt);
3847#endif
3848 to->end = from->end;
3849}
3850
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003851 static void
3852clear_sub(sub)
3853 regsub_T *sub;
3854{
3855 if (REG_MULTI)
3856 /* Use 0xff to set lnum to -1 */
3857 vim_memset(sub->list.multi, 0xff,
3858 sizeof(struct multipos) * nfa_nsubexpr);
3859 else
3860 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3861 sub->in_use = 0;
3862}
3863
3864/*
3865 * Copy the submatches from "from" to "to".
3866 */
3867 static void
3868copy_sub(to, from)
3869 regsub_T *to;
3870 regsub_T *from;
3871{
3872 to->in_use = from->in_use;
3873 if (from->in_use > 0)
3874 {
3875 /* Copy the match start and end positions. */
3876 if (REG_MULTI)
3877 mch_memmove(&to->list.multi[0],
3878 &from->list.multi[0],
3879 sizeof(struct multipos) * from->in_use);
3880 else
3881 mch_memmove(&to->list.line[0],
3882 &from->list.line[0],
3883 sizeof(struct linepos) * from->in_use);
3884 }
3885}
3886
3887/*
3888 * Like copy_sub() but exclude the main match.
3889 */
3890 static void
3891copy_sub_off(to, from)
3892 regsub_T *to;
3893 regsub_T *from;
3894{
3895 if (to->in_use < from->in_use)
3896 to->in_use = from->in_use;
3897 if (from->in_use > 1)
3898 {
3899 /* Copy the match start and end positions. */
3900 if (REG_MULTI)
3901 mch_memmove(&to->list.multi[1],
3902 &from->list.multi[1],
3903 sizeof(struct multipos) * (from->in_use - 1));
3904 else
3905 mch_memmove(&to->list.line[1],
3906 &from->list.line[1],
3907 sizeof(struct linepos) * (from->in_use - 1));
3908 }
3909}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003910
Bram Moolenaar428e9872013-05-30 17:05:39 +02003911/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003912 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003913 */
3914 static int
3915sub_equal(sub1, sub2)
3916 regsub_T *sub1;
3917 regsub_T *sub2;
3918{
3919 int i;
3920 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003921 linenr_T s1;
3922 linenr_T s2;
3923 char_u *sp1;
3924 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003925
3926 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3927 if (REG_MULTI)
3928 {
3929 for (i = 0; i < todo; ++i)
3930 {
3931 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003932 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003933 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003934 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003935 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003936 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003937 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003938 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003939 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003940 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02003941 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02003942 != sub2->list.multi[i].start.col)
3943 return FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003944 }
3945 }
3946 else
3947 {
3948 for (i = 0; i < todo; ++i)
3949 {
3950 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003951 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003952 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003953 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003954 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003955 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003956 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003957 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003958 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003959 return FALSE;
3960 }
3961 }
3962
3963 return TRUE;
3964}
3965
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003966#ifdef ENABLE_LOG
3967 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003968report_state(char *action,
3969 regsub_T *sub,
3970 nfa_state_T *state,
3971 int lid,
3972 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003973{
3974 int col;
3975
3976 if (sub->in_use <= 0)
3977 col = -1;
3978 else if (REG_MULTI)
3979 col = sub->list.multi[0].start.col;
3980 else
3981 col = (int)(sub->list.line[0].start - regline);
3982 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003983 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3984 action, abs(state->id), lid, state->c, code, col,
3985 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003986}
3987#endif
3988
Bram Moolenaar43e02982013-06-07 17:31:29 +02003989/*
3990 * Return TRUE if the same state is already in list "l" with the same
3991 * positions as "subs".
3992 */
3993 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02003994has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02003995 nfa_list_T *l; /* runtime state list */
3996 nfa_state_T *state; /* state to update */
3997 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02003998 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003999{
4000 nfa_thread_T *thread;
4001 int i;
4002
4003 for (i = 0; i < l->n; ++i)
4004 {
4005 thread = &l->t[i];
4006 if (thread->state->id == state->id
4007 && sub_equal(&thread->subs.norm, &subs->norm)
4008#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004009 && (!nfa_has_zsubexpr
4010 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004011#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004012 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004013 return TRUE;
4014 }
4015 return FALSE;
4016}
4017
4018/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004019 * Return TRUE if "one" and "two" are equal. That includes when both are not
4020 * set.
4021 */
4022 static int
4023pim_equal(one, two)
4024 nfa_pim_T *one;
4025 nfa_pim_T *two;
4026{
4027 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4028 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4029
4030 if (one_unused)
4031 /* one is unused: equal when two is also unused */
4032 return two_unused;
4033 if (two_unused)
4034 /* one is used and two is not: not equal */
4035 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004036 /* compare the state id */
4037 if (one->state->id != two->state->id)
4038 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004039 /* compare the position */
4040 if (REG_MULTI)
4041 return one->end.pos.lnum == two->end.pos.lnum
4042 && one->end.pos.col == two->end.pos.col;
4043 return one->end.ptr == two->end.ptr;
4044}
4045
4046/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004047 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4048 */
4049 static int
4050match_follows(startstate, depth)
4051 nfa_state_T *startstate;
4052 int depth;
4053{
4054 nfa_state_T *state = startstate;
4055
4056 /* avoid too much recursion */
4057 if (depth > 10)
4058 return FALSE;
4059
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004060 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004061 {
4062 switch (state->c)
4063 {
4064 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004065 case NFA_MCLOSE:
4066 case NFA_END_INVISIBLE:
4067 case NFA_END_INVISIBLE_NEG:
4068 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004069 return TRUE;
4070
4071 case NFA_SPLIT:
4072 return match_follows(state->out, depth + 1)
4073 || match_follows(state->out1, depth + 1);
4074
4075 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004076 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004077 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004078 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004079 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004080 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004081 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004082 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004083 case NFA_COMPOSING:
4084 /* skip ahead to next state */
4085 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004086 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004087
4088 case NFA_ANY:
4089 case NFA_IDENT:
4090 case NFA_SIDENT:
4091 case NFA_KWORD:
4092 case NFA_SKWORD:
4093 case NFA_FNAME:
4094 case NFA_SFNAME:
4095 case NFA_PRINT:
4096 case NFA_SPRINT:
4097 case NFA_WHITE:
4098 case NFA_NWHITE:
4099 case NFA_DIGIT:
4100 case NFA_NDIGIT:
4101 case NFA_HEX:
4102 case NFA_NHEX:
4103 case NFA_OCTAL:
4104 case NFA_NOCTAL:
4105 case NFA_WORD:
4106 case NFA_NWORD:
4107 case NFA_HEAD:
4108 case NFA_NHEAD:
4109 case NFA_ALPHA:
4110 case NFA_NALPHA:
4111 case NFA_LOWER:
4112 case NFA_NLOWER:
4113 case NFA_UPPER:
4114 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004115 case NFA_LOWER_IC:
4116 case NFA_NLOWER_IC:
4117 case NFA_UPPER_IC:
4118 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004119 case NFA_START_COLL:
4120 case NFA_START_NEG_COLL:
4121 case NFA_NEWL:
4122 /* state will advance input */
4123 return FALSE;
4124
4125 default:
4126 if (state->c > 0)
4127 /* state will advance input */
4128 return FALSE;
4129
4130 /* Others: zero-width or possibly zero-width, might still find
4131 * a match at the same position, keep looking. */
4132 break;
4133 }
4134 state = state->out;
4135 }
4136 return FALSE;
4137}
4138
4139
4140/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004141 * Return TRUE if "state" is already in list "l".
4142 */
4143 static int
4144state_in_list(l, state, subs)
4145 nfa_list_T *l; /* runtime state list */
4146 nfa_state_T *state; /* state to update */
4147 regsubs_T *subs; /* pointers to subexpressions */
4148{
4149 if (state->lastlist[nfa_ll_index] == l->id)
4150 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004151 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004152 return TRUE;
4153 }
4154 return FALSE;
4155}
4156
Bram Moolenaard05bf562013-06-30 23:24:08 +02004157/*
4158 * Add "state" and possibly what follows to state list ".".
4159 * Returns "subs_arg", possibly copied into temp_subs.
4160 */
4161
4162 static regsubs_T *
4163addstate(l, state, subs_arg, pim, off)
4164 nfa_list_T *l; /* runtime state list */
4165 nfa_state_T *state; /* state to update */
4166 regsubs_T *subs_arg; /* pointers to subexpressions */
4167 nfa_pim_T *pim; /* postponed look-behind match */
4168 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004169{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004170 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004171 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004172 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004173 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004174 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004175 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004176 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004177 regsubs_T *subs = subs_arg;
4178 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004179#ifdef ENABLE_LOG
4180 int did_print = FALSE;
4181#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004182
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004183 switch (state->c)
4184 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004185 case NFA_NCLOSE:
4186 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004187 case NFA_MCLOSE1:
4188 case NFA_MCLOSE2:
4189 case NFA_MCLOSE3:
4190 case NFA_MCLOSE4:
4191 case NFA_MCLOSE5:
4192 case NFA_MCLOSE6:
4193 case NFA_MCLOSE7:
4194 case NFA_MCLOSE8:
4195 case NFA_MCLOSE9:
4196#ifdef FEAT_SYN_HL
4197 case NFA_ZCLOSE:
4198 case NFA_ZCLOSE1:
4199 case NFA_ZCLOSE2:
4200 case NFA_ZCLOSE3:
4201 case NFA_ZCLOSE4:
4202 case NFA_ZCLOSE5:
4203 case NFA_ZCLOSE6:
4204 case NFA_ZCLOSE7:
4205 case NFA_ZCLOSE8:
4206 case NFA_ZCLOSE9:
4207#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004208 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004209 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004210 case NFA_SPLIT:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004211 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004212 /* These nodes are not added themselves but their "out" and/or
4213 * "out1" may be added below. */
4214 break;
4215
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004216 case NFA_BOL:
4217 case NFA_BOF:
4218 /* "^" won't match past end-of-line, don't bother trying.
4219 * Except when at the end of the line, or when we are going to the
4220 * next line for a look-behind match. */
4221 if (reginput > regline
4222 && *reginput != NUL
4223 && (nfa_endp == NULL
4224 || !REG_MULTI
4225 || reglnum == nfa_endp->se_u.pos.lnum))
4226 goto skip_add;
4227 /* FALLTHROUGH */
4228
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004229 case NFA_MOPEN1:
4230 case NFA_MOPEN2:
4231 case NFA_MOPEN3:
4232 case NFA_MOPEN4:
4233 case NFA_MOPEN5:
4234 case NFA_MOPEN6:
4235 case NFA_MOPEN7:
4236 case NFA_MOPEN8:
4237 case NFA_MOPEN9:
4238#ifdef FEAT_SYN_HL
4239 case NFA_ZOPEN:
4240 case NFA_ZOPEN1:
4241 case NFA_ZOPEN2:
4242 case NFA_ZOPEN3:
4243 case NFA_ZOPEN4:
4244 case NFA_ZOPEN5:
4245 case NFA_ZOPEN6:
4246 case NFA_ZOPEN7:
4247 case NFA_ZOPEN8:
4248 case NFA_ZOPEN9:
4249#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004250 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004251 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004252 /* These nodes need to be added so that we can bail out when it
4253 * was added to this list before at the same position to avoid an
4254 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004255
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004257 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004258 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004259 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004260 * unless it is an MOPEN that is used for a backreference or
4261 * when there is a PIM. */
Bram Moolenaar196ed142013-07-21 18:59:24 +02004262 if (!nfa_has_backref && pim == NULL && !l->has_pim)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004263 {
4264skip_add:
4265#ifdef ENABLE_LOG
4266 nfa_set_code(state->c);
4267 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4268 abs(state->id), l->id, state->c, code);
4269#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004270 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004271 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004272
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004273 /* Do not add the state again when it exists with the same
4274 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004275 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004276 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004277 }
4278
Bram Moolenaara0169122013-06-26 18:16:58 +02004279 /* When there are backreferences or PIMs the number of states may
4280 * be (a lot) bigger than anticipated. */
4281 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004282 {
4283 int newlen = l->len * 3 / 2 + 50;
4284
Bram Moolenaard05bf562013-06-30 23:24:08 +02004285 if (subs != &temp_subs)
4286 {
4287 /* "subs" may point into the current array, need to make a
4288 * copy before it becomes invalid. */
4289 copy_sub(&temp_subs.norm, &subs->norm);
4290#ifdef FEAT_SYN_HL
4291 if (nfa_has_zsubexpr)
4292 copy_sub(&temp_subs.synt, &subs->synt);
4293#endif
4294 subs = &temp_subs;
4295 }
4296
Bram Moolenaar428e9872013-05-30 17:05:39 +02004297 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4298 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004299 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004300
4301 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004302 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004303 thread = &l->t[l->n++];
4304 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004305 if (pim == NULL)
4306 thread->pim.result = NFA_PIM_UNUSED;
4307 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004308 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004309 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004310 l->has_pim = TRUE;
4311 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004312 copy_sub(&thread->subs.norm, &subs->norm);
4313#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004314 if (nfa_has_zsubexpr)
4315 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004316#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004317#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004318 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004319 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004320#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004321 }
4322
4323#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004324 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004325 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004326#endif
4327 switch (state->c)
4328 {
4329 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004330 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004331 break;
4332
4333 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004334 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004335 subs = addstate(l, state->out, subs, pim, off);
4336 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004337 break;
4338
Bram Moolenaar5714b802013-05-28 22:03:20 +02004339 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004340 case NFA_NOPEN:
4341 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004342 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004343 break;
4344
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004345 case NFA_MOPEN:
4346 case NFA_MOPEN1:
4347 case NFA_MOPEN2:
4348 case NFA_MOPEN3:
4349 case NFA_MOPEN4:
4350 case NFA_MOPEN5:
4351 case NFA_MOPEN6:
4352 case NFA_MOPEN7:
4353 case NFA_MOPEN8:
4354 case NFA_MOPEN9:
4355#ifdef FEAT_SYN_HL
4356 case NFA_ZOPEN:
4357 case NFA_ZOPEN1:
4358 case NFA_ZOPEN2:
4359 case NFA_ZOPEN3:
4360 case NFA_ZOPEN4:
4361 case NFA_ZOPEN5:
4362 case NFA_ZOPEN6:
4363 case NFA_ZOPEN7:
4364 case NFA_ZOPEN8:
4365 case NFA_ZOPEN9:
4366#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004368 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004369 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004370 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004371 sub = &subs->norm;
4372 }
4373#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004374 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004375 {
4376 subidx = state->c - NFA_ZOPEN;
4377 sub = &subs->synt;
4378 }
4379#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004380 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004381 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004382 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004383 sub = &subs->norm;
4384 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004385
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004386 /* avoid compiler warnings */
4387 save_ptr = NULL;
4388 save_lpos.lnum = 0;
4389 save_lpos.col = 0;
4390
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004391 /* Set the position (with "off" added) in the subexpression. Save
4392 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004393 if (REG_MULTI)
4394 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004395 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004396 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004397 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004398 save_in_use = -1;
4399 }
4400 else
4401 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004402 save_in_use = sub->in_use;
4403 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004404 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004405 sub->list.multi[i].start.lnum = -1;
4406 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004407 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004408 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004409 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004410 if (off == -1)
4411 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004412 sub->list.multi[subidx].start.lnum = reglnum + 1;
4413 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004414 }
4415 else
4416 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004417 sub->list.multi[subidx].start.lnum = reglnum;
4418 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004419 (colnr_T)(reginput - regline + off);
4420 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004421 }
4422 else
4423 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004424 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004425 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004426 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004427 save_in_use = -1;
4428 }
4429 else
4430 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004431 save_in_use = sub->in_use;
4432 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004433 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004434 sub->list.line[i].start = NULL;
4435 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004436 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004437 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004438 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004439 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004440 }
4441
Bram Moolenaard05bf562013-06-30 23:24:08 +02004442 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004443 /* "subs" may have changed, need to set "sub" again */
4444#ifdef FEAT_SYN_HL
4445 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4446 sub = &subs->synt;
4447 else
4448#endif
4449 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004450
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004451 if (save_in_use == -1)
4452 {
4453 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004454 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004455 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004456 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004457 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004458 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004459 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004460 break;
4461
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004462 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004463 if (nfa_has_zend && (REG_MULTI
4464 ? subs->norm.list.multi[0].end.lnum >= 0
4465 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004466 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004467 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004468 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 break;
4470 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004471 case NFA_MCLOSE1:
4472 case NFA_MCLOSE2:
4473 case NFA_MCLOSE3:
4474 case NFA_MCLOSE4:
4475 case NFA_MCLOSE5:
4476 case NFA_MCLOSE6:
4477 case NFA_MCLOSE7:
4478 case NFA_MCLOSE8:
4479 case NFA_MCLOSE9:
4480#ifdef FEAT_SYN_HL
4481 case NFA_ZCLOSE:
4482 case NFA_ZCLOSE1:
4483 case NFA_ZCLOSE2:
4484 case NFA_ZCLOSE3:
4485 case NFA_ZCLOSE4:
4486 case NFA_ZCLOSE5:
4487 case NFA_ZCLOSE6:
4488 case NFA_ZCLOSE7:
4489 case NFA_ZCLOSE8:
4490 case NFA_ZCLOSE9:
4491#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004492 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004493 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004494 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004495 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004496 sub = &subs->norm;
4497 }
4498#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004499 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004500 {
4501 subidx = state->c - NFA_ZCLOSE;
4502 sub = &subs->synt;
4503 }
4504#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004505 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004506 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004507 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004508 sub = &subs->norm;
4509 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004510
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004511 /* We don't fill in gaps here, there must have been an MOPEN that
4512 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004513 save_in_use = sub->in_use;
4514 if (sub->in_use <= subidx)
4515 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004516 if (REG_MULTI)
4517 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004518 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004519 if (off == -1)
4520 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004521 sub->list.multi[subidx].end.lnum = reglnum + 1;
4522 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004523 }
4524 else
4525 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004526 sub->list.multi[subidx].end.lnum = reglnum;
4527 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004528 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004529 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004530 /* avoid compiler warnings */
4531 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004532 }
4533 else
4534 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004535 save_ptr = sub->list.line[subidx].end;
4536 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004537 /* avoid compiler warnings */
4538 save_lpos.lnum = 0;
4539 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004540 }
4541
Bram Moolenaard05bf562013-06-30 23:24:08 +02004542 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004543 /* "subs" may have changed, need to set "sub" again */
4544#ifdef FEAT_SYN_HL
4545 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4546 sub = &subs->synt;
4547 else
4548#endif
4549 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004550
4551 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004552 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004554 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004555 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004556 break;
4557 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004558 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004559}
4560
4561/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004562 * Like addstate(), but the new state(s) are put at position "*ip".
4563 * Used for zero-width matches, next state to use is the added one.
4564 * This makes sure the order of states to be tried does not change, which
4565 * matters for alternatives.
4566 */
4567 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004568addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004569 nfa_list_T *l; /* runtime state list */
4570 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004571 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004572 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004573 int *ip;
4574{
4575 int tlen = l->n;
4576 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004577 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004578
4579 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004580 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004581
Bram Moolenaar4b417062013-05-25 20:19:50 +02004582 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004583 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004584 return;
4585
4586 /* re-order to put the new state at the current position */
4587 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004588 if (count == 0)
4589 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004590 if (count == 1)
4591 {
4592 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004593 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004594 }
4595 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004596 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004597 if (l->n + count - 1 >= l->len)
4598 {
4599 /* not enough space to move the new states, reallocate the list
4600 * and move the states to the right position */
4601 nfa_thread_T *newl;
4602
4603 l->len = l->len * 3 / 2 + 50;
4604 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4605 if (newl == NULL)
4606 return;
4607 mch_memmove(&(newl[0]),
4608 &(l->t[0]),
4609 sizeof(nfa_thread_T) * listidx);
4610 mch_memmove(&(newl[listidx]),
4611 &(l->t[l->n - count]),
4612 sizeof(nfa_thread_T) * count);
4613 mch_memmove(&(newl[listidx + count]),
4614 &(l->t[listidx + 1]),
4615 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4616 vim_free(l->t);
4617 l->t = newl;
4618 }
4619 else
4620 {
4621 /* make space for new states, then move them from the
4622 * end to the current position */
4623 mch_memmove(&(l->t[listidx + count]),
4624 &(l->t[listidx + 1]),
4625 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4626 mch_memmove(&(l->t[listidx]),
4627 &(l->t[l->n - 1]),
4628 sizeof(nfa_thread_T) * count);
4629 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004630 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004631 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004632 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004633}
4634
4635/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004636 * Check character class "class" against current character c.
4637 */
4638 static int
4639check_char_class(class, c)
4640 int class;
4641 int c;
4642{
4643 switch (class)
4644 {
4645 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004646 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004647 return OK;
4648 break;
4649 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004650 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004651 return OK;
4652 break;
4653 case NFA_CLASS_BLANK:
4654 if (c == ' ' || c == '\t')
4655 return OK;
4656 break;
4657 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004658 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004659 return OK;
4660 break;
4661 case NFA_CLASS_DIGIT:
4662 if (VIM_ISDIGIT(c))
4663 return OK;
4664 break;
4665 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004666 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004667 return OK;
4668 break;
4669 case NFA_CLASS_LOWER:
4670 if (MB_ISLOWER(c))
4671 return OK;
4672 break;
4673 case NFA_CLASS_PRINT:
4674 if (vim_isprintc(c))
4675 return OK;
4676 break;
4677 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004678 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004679 return OK;
4680 break;
4681 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004682 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004683 return OK;
4684 break;
4685 case NFA_CLASS_UPPER:
4686 if (MB_ISUPPER(c))
4687 return OK;
4688 break;
4689 case NFA_CLASS_XDIGIT:
4690 if (vim_isxdigit(c))
4691 return OK;
4692 break;
4693 case NFA_CLASS_TAB:
4694 if (c == '\t')
4695 return OK;
4696 break;
4697 case NFA_CLASS_RETURN:
4698 if (c == '\r')
4699 return OK;
4700 break;
4701 case NFA_CLASS_BACKSPACE:
4702 if (c == '\b')
4703 return OK;
4704 break;
4705 case NFA_CLASS_ESCAPE:
4706 if (c == '\033')
4707 return OK;
4708 break;
4709
4710 default:
4711 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004712 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4713 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004714 }
4715 return FAIL;
4716}
4717
Bram Moolenaar5714b802013-05-28 22:03:20 +02004718/*
4719 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004720 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004721 */
4722 static int
4723match_backref(sub, subidx, bytelen)
4724 regsub_T *sub; /* pointers to subexpressions */
4725 int subidx;
4726 int *bytelen; /* out: length of match in bytes */
4727{
4728 int len;
4729
4730 if (sub->in_use <= subidx)
4731 {
4732retempty:
4733 /* backref was not set, match an empty string */
4734 *bytelen = 0;
4735 return TRUE;
4736 }
4737
4738 if (REG_MULTI)
4739 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004740 if (sub->list.multi[subidx].start.lnum < 0
4741 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004742 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004743 if (sub->list.multi[subidx].start.lnum == reglnum
4744 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004745 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004746 len = sub->list.multi[subidx].end.col
4747 - sub->list.multi[subidx].start.col;
4748 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4749 reginput, &len) == 0)
4750 {
4751 *bytelen = len;
4752 return TRUE;
4753 }
4754 }
4755 else
4756 {
4757 if (match_with_backref(
4758 sub->list.multi[subidx].start.lnum,
4759 sub->list.multi[subidx].start.col,
4760 sub->list.multi[subidx].end.lnum,
4761 sub->list.multi[subidx].end.col,
4762 bytelen) == RA_MATCH)
4763 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004764 }
4765 }
4766 else
4767 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004768 if (sub->list.line[subidx].start == NULL
4769 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004770 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004771 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4772 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004773 {
4774 *bytelen = len;
4775 return TRUE;
4776 }
4777 }
4778 return FALSE;
4779}
4780
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004781#ifdef FEAT_SYN_HL
4782
4783static int match_zref __ARGS((int subidx, int *bytelen));
4784
4785/*
4786 * Check for a match with \z subexpression "subidx".
4787 * Return TRUE if it matches.
4788 */
4789 static int
4790match_zref(subidx, bytelen)
4791 int subidx;
4792 int *bytelen; /* out: length of match in bytes */
4793{
4794 int len;
4795
4796 cleanup_zsubexpr();
4797 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4798 {
4799 /* backref was not set, match an empty string */
4800 *bytelen = 0;
4801 return TRUE;
4802 }
4803
4804 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4805 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4806 {
4807 *bytelen = len;
4808 return TRUE;
4809 }
4810 return FALSE;
4811}
4812#endif
4813
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004814/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004815 * Save list IDs for all NFA states of "prog" into "list".
4816 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004817 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004818 */
4819 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004820nfa_save_listids(prog, list)
4821 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004822 int *list;
4823{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004824 int i;
4825 nfa_state_T *p;
4826
4827 /* Order in the list is reverse, it's a bit faster that way. */
4828 p = &prog->state[0];
4829 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004830 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004831 list[i] = p->lastlist[1];
4832 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004833 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004834 }
4835}
4836
4837/*
4838 * Restore list IDs from "list" to all NFA states.
4839 */
4840 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004841nfa_restore_listids(prog, list)
4842 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004843 int *list;
4844{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004845 int i;
4846 nfa_state_T *p;
4847
4848 p = &prog->state[0];
4849 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004850 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004851 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004852 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004853 }
4854}
4855
Bram Moolenaar423532e2013-05-29 21:14:42 +02004856 static int
4857nfa_re_num_cmp(val, op, pos)
4858 long_u val;
4859 int op;
4860 long_u pos;
4861{
4862 if (op == 1) return pos > val;
4863 if (op == 2) return pos < val;
4864 return val == pos;
4865}
4866
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004867static 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 +02004868static 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 +02004869
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004870/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004871 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004872 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4873 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004874 */
4875 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004876recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004877 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004878 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004879 nfa_regprog_T *prog;
4880 regsubs_T *submatch;
4881 regsubs_T *m;
4882 int **listids;
4883{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004884 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004885 int save_reglnum = reglnum;
4886 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004887 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004888 save_se_T *save_nfa_endp = nfa_endp;
4889 save_se_T endpos;
4890 save_se_T *endposp = NULL;
4891 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004892 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004893
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004894 if (pim != NULL)
4895 {
4896 /* start at the position where the postponed match was */
4897 if (REG_MULTI)
4898 reginput = regline + pim->end.pos.col;
4899 else
4900 reginput = pim->end.ptr;
4901 }
4902
Bram Moolenaardecd9542013-06-07 16:31:50 +02004903 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004904 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4905 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4906 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004907 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004908 /* The recursive match must end at the current position. When "pim" is
4909 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004910 endposp = &endpos;
4911 if (REG_MULTI)
4912 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004913 if (pim == NULL)
4914 {
4915 endpos.se_u.pos.col = (int)(reginput - regline);
4916 endpos.se_u.pos.lnum = reglnum;
4917 }
4918 else
4919 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004920 }
4921 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004922 {
4923 if (pim == NULL)
4924 endpos.se_u.ptr = reginput;
4925 else
4926 endpos.se_u.ptr = pim->end.ptr;
4927 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004928
4929 /* Go back the specified number of bytes, or as far as the
4930 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02004931 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004932 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004933 if (state->val <= 0)
4934 {
4935 if (REG_MULTI)
4936 {
4937 regline = reg_getline(--reglnum);
4938 if (regline == NULL)
4939 /* can't go before the first line */
4940 regline = reg_getline(++reglnum);
4941 }
4942 reginput = regline;
4943 }
4944 else
4945 {
4946 if (REG_MULTI && (int)(reginput - regline) < state->val)
4947 {
4948 /* Not enough bytes in this line, go to end of
4949 * previous line. */
4950 regline = reg_getline(--reglnum);
4951 if (regline == NULL)
4952 {
4953 /* can't go before the first line */
4954 regline = reg_getline(++reglnum);
4955 reginput = regline;
4956 }
4957 else
4958 reginput = regline + STRLEN(regline);
4959 }
4960 if ((int)(reginput - regline) >= state->val)
4961 {
4962 reginput -= state->val;
4963#ifdef FEAT_MBYTE
4964 if (has_mbyte)
4965 reginput -= mb_head_off(regline, reginput);
4966#endif
4967 }
4968 else
4969 reginput = regline;
4970 }
4971 }
4972
Bram Moolenaarf46da702013-06-02 22:37:42 +02004973#ifdef ENABLE_LOG
4974 if (log_fd != stderr)
4975 fclose(log_fd);
4976 log_fd = NULL;
4977#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004978 /* Have to clear the lastlist field of the NFA nodes, so that
4979 * nfa_regmatch() and addstate() can run properly after recursion. */
4980 if (nfa_ll_index == 1)
4981 {
4982 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4983 * values and clear them. */
4984 if (*listids == NULL)
4985 {
4986 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4987 if (*listids == NULL)
4988 {
4989 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4990 return 0;
4991 }
4992 }
4993 nfa_save_listids(prog, *listids);
4994 need_restore = TRUE;
4995 /* any value of nfa_listid will do */
4996 }
4997 else
4998 {
4999 /* First recursive nfa_regmatch() call, switch to the second lastlist
5000 * entry. Make sure nfa_listid is different from a previous recursive
5001 * call, because some states may still have this ID. */
5002 ++nfa_ll_index;
5003 if (nfa_listid <= nfa_alt_listid)
5004 nfa_listid = nfa_alt_listid;
5005 }
5006
5007 /* Call nfa_regmatch() to check if the current concat matches at this
5008 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005009 nfa_endp = endposp;
5010 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005011
5012 if (need_restore)
5013 nfa_restore_listids(prog, *listids);
5014 else
5015 {
5016 --nfa_ll_index;
5017 nfa_alt_listid = nfa_listid;
5018 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005019
5020 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005021 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005022 if (REG_MULTI)
5023 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005024 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005025 nfa_match = save_nfa_match;
5026 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005027 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005028
5029#ifdef ENABLE_LOG
5030 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5031 if (log_fd != NULL)
5032 {
5033 fprintf(log_fd, "****************************\n");
5034 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5035 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5036 fprintf(log_fd, "****************************\n");
5037 }
5038 else
5039 {
5040 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5041 log_fd = stderr;
5042 }
5043#endif
5044
5045 return result;
5046}
5047
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005048static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005049static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005050
5051/*
5052 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005053 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005054 * NFA_ANY: 1
5055 * specific character: 99
5056 */
5057 static int
5058failure_chance(state, depth)
5059 nfa_state_T *state;
5060 int depth;
5061{
5062 int c = state->c;
5063 int l, r;
5064
5065 /* detect looping */
5066 if (depth > 4)
5067 return 1;
5068
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005069 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005070 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005071 case NFA_SPLIT:
5072 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5073 /* avoid recursive stuff */
5074 return 1;
5075 /* two alternatives, use the lowest failure chance */
5076 l = failure_chance(state->out, depth + 1);
5077 r = failure_chance(state->out1, depth + 1);
5078 return l < r ? l : r;
5079
5080 case NFA_ANY:
5081 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005082 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005083
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005084 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005085 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005086 /* empty match works always */
5087 return 0;
5088
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005089 case NFA_START_INVISIBLE:
5090 case NFA_START_INVISIBLE_FIRST:
5091 case NFA_START_INVISIBLE_NEG:
5092 case NFA_START_INVISIBLE_NEG_FIRST:
5093 case NFA_START_INVISIBLE_BEFORE:
5094 case NFA_START_INVISIBLE_BEFORE_FIRST:
5095 case NFA_START_INVISIBLE_BEFORE_NEG:
5096 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5097 case NFA_START_PATTERN:
5098 /* recursive regmatch is expensive, use low failure chance */
5099 return 5;
5100
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005101 case NFA_BOL:
5102 case NFA_EOL:
5103 case NFA_BOF:
5104 case NFA_EOF:
5105 case NFA_NEWL:
5106 return 99;
5107
5108 case NFA_BOW:
5109 case NFA_EOW:
5110 return 90;
5111
5112 case NFA_MOPEN:
5113 case NFA_MOPEN1:
5114 case NFA_MOPEN2:
5115 case NFA_MOPEN3:
5116 case NFA_MOPEN4:
5117 case NFA_MOPEN5:
5118 case NFA_MOPEN6:
5119 case NFA_MOPEN7:
5120 case NFA_MOPEN8:
5121 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005122#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005123 case NFA_ZOPEN:
5124 case NFA_ZOPEN1:
5125 case NFA_ZOPEN2:
5126 case NFA_ZOPEN3:
5127 case NFA_ZOPEN4:
5128 case NFA_ZOPEN5:
5129 case NFA_ZOPEN6:
5130 case NFA_ZOPEN7:
5131 case NFA_ZOPEN8:
5132 case NFA_ZOPEN9:
5133 case NFA_ZCLOSE:
5134 case NFA_ZCLOSE1:
5135 case NFA_ZCLOSE2:
5136 case NFA_ZCLOSE3:
5137 case NFA_ZCLOSE4:
5138 case NFA_ZCLOSE5:
5139 case NFA_ZCLOSE6:
5140 case NFA_ZCLOSE7:
5141 case NFA_ZCLOSE8:
5142 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005143#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005144 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005145 case NFA_MCLOSE1:
5146 case NFA_MCLOSE2:
5147 case NFA_MCLOSE3:
5148 case NFA_MCLOSE4:
5149 case NFA_MCLOSE5:
5150 case NFA_MCLOSE6:
5151 case NFA_MCLOSE7:
5152 case NFA_MCLOSE8:
5153 case NFA_MCLOSE9:
5154 case NFA_NCLOSE:
5155 return failure_chance(state->out, depth + 1);
5156
5157 case NFA_BACKREF1:
5158 case NFA_BACKREF2:
5159 case NFA_BACKREF3:
5160 case NFA_BACKREF4:
5161 case NFA_BACKREF5:
5162 case NFA_BACKREF6:
5163 case NFA_BACKREF7:
5164 case NFA_BACKREF8:
5165 case NFA_BACKREF9:
5166#ifdef FEAT_SYN_HL
5167 case NFA_ZREF1:
5168 case NFA_ZREF2:
5169 case NFA_ZREF3:
5170 case NFA_ZREF4:
5171 case NFA_ZREF5:
5172 case NFA_ZREF6:
5173 case NFA_ZREF7:
5174 case NFA_ZREF8:
5175 case NFA_ZREF9:
5176#endif
5177 /* backreferences don't match in many places */
5178 return 94;
5179
5180 case NFA_LNUM_GT:
5181 case NFA_LNUM_LT:
5182 case NFA_COL_GT:
5183 case NFA_COL_LT:
5184 case NFA_VCOL_GT:
5185 case NFA_VCOL_LT:
5186 case NFA_MARK_GT:
5187 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005188 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005189 /* before/after positions don't match very often */
5190 return 85;
5191
5192 case NFA_LNUM:
5193 return 90;
5194
5195 case NFA_CURSOR:
5196 case NFA_COL:
5197 case NFA_VCOL:
5198 case NFA_MARK:
5199 /* specific positions rarely match */
5200 return 98;
5201
5202 case NFA_COMPOSING:
5203 return 95;
5204
5205 default:
5206 if (c > 0)
5207 /* character match fails often */
5208 return 95;
5209 }
5210
5211 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005212 return 50;
5213}
5214
Bram Moolenaarf46da702013-06-02 22:37:42 +02005215/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005216 * Skip until the char "c" we know a match must start with.
5217 */
5218 static int
5219skip_to_start(c, colp)
5220 int c;
5221 colnr_T *colp;
5222{
5223 char_u *s;
5224
5225 /* Used often, do some work to avoid call overhead. */
5226 if (!ireg_ic
5227#ifdef FEAT_MBYTE
5228 && !has_mbyte
5229#endif
5230 )
5231 s = vim_strbyte(regline + *colp, c);
5232 else
5233 s = cstrchr(regline + *colp, c);
5234 if (s == NULL)
5235 return FAIL;
5236 *colp = (int)(s - regline);
5237 return OK;
5238}
5239
5240/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005241 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005242 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005243 * Returns zero for no match, 1 for a match.
5244 */
5245 static long
5246find_match_text(startcol, regstart, match_text)
5247 colnr_T startcol;
5248 int regstart;
5249 char_u *match_text;
5250{
5251 colnr_T col = startcol;
5252 int c1, c2;
5253 int len1, len2;
5254 int match;
5255
5256 for (;;)
5257 {
5258 match = TRUE;
5259 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5260 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5261 {
5262 c1 = PTR2CHAR(match_text + len1);
5263 c2 = PTR2CHAR(regline + col + len2);
5264 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5265 {
5266 match = FALSE;
5267 break;
5268 }
5269 len2 += MB_CHAR2LEN(c2);
5270 }
5271 if (match
5272#ifdef FEAT_MBYTE
5273 /* check that no composing char follows */
5274 && !(enc_utf8
5275 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5276#endif
5277 )
5278 {
5279 cleanup_subexpr();
5280 if (REG_MULTI)
5281 {
5282 reg_startpos[0].lnum = reglnum;
5283 reg_startpos[0].col = col;
5284 reg_endpos[0].lnum = reglnum;
5285 reg_endpos[0].col = col + len2;
5286 }
5287 else
5288 {
5289 reg_startp[0] = regline + col;
5290 reg_endp[0] = regline + col + len2;
5291 }
5292 return 1L;
5293 }
5294
5295 /* Try finding regstart after the current match. */
5296 col += MB_CHAR2LEN(regstart); /* skip regstart */
5297 if (skip_to_start(regstart, &col) == FAIL)
5298 break;
5299 }
5300 return 0L;
5301}
5302
5303/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005304 * Main matching routine.
5305 *
5306 * Run NFA to determine whether it matches reginput.
5307 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005308 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005309 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005310 * Return TRUE if there is a match, FALSE otherwise.
5311 * Note: Caller must ensure that: start != NULL.
5312 */
5313 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005314nfa_regmatch(prog, start, submatch, m)
5315 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005316 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005317 regsubs_T *submatch;
5318 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005319{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005320 int result;
5321 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005322 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005323 int go_to_nextline = FALSE;
5324 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005325 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005326 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005327 nfa_list_T *thislist;
5328 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005329 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005330 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005331 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005332 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005333 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005334 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005335#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005336 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005337
5338 if (debug == NULL)
5339 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005340 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005341 return FALSE;
5342 }
5343#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005344 /* Some patterns may take a long time to match, especially when using
5345 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5346 fast_breakcheck();
5347 if (got_int)
5348 return FALSE;
5349
Bram Moolenaar963fee22013-05-26 21:47:28 +02005350 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005351
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005352 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005353 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005354 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005355 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005356 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005357 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005358 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005359 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005360
5361#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005362 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005363 if (log_fd != NULL)
5364 {
5365 fprintf(log_fd, "**********************************\n");
5366 nfa_set_code(start->c);
5367 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5368 abs(start->id), code);
5369 fprintf(log_fd, "**********************************\n");
5370 }
5371 else
5372 {
5373 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5374 log_fd = stderr;
5375 }
5376#endif
5377
5378 thislist = &list[0];
5379 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005380 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005381 nextlist = &list[1];
5382 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005383 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005384#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005385 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005386#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005387 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005388
5389 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5390 * it's the first MOPEN. */
5391 if (toplevel)
5392 {
5393 if (REG_MULTI)
5394 {
5395 m->norm.list.multi[0].start.lnum = reglnum;
5396 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5397 }
5398 else
5399 m->norm.list.line[0].start = reginput;
5400 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005401 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005402 }
5403 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005404 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005405
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005406#define ADD_STATE_IF_MATCH(state) \
5407 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005408 add_state = state->out; \
5409 add_off = clen; \
5410 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005411
5412 /*
5413 * Run for each character.
5414 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005415 for (;;)
5416 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005417 int curc;
5418 int clen;
5419
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005420#ifdef FEAT_MBYTE
5421 if (has_mbyte)
5422 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005423 curc = (*mb_ptr2char)(reginput);
5424 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005425 }
5426 else
5427#endif
5428 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005429 curc = *reginput;
5430 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005431 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005432 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005433 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005434 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005435 go_to_nextline = FALSE;
5436 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005437
5438 /* swap lists */
5439 thislist = &list[flag];
5440 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005441 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005442 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005443 ++nfa_listid;
5444 thislist->id = nfa_listid;
5445 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005446
5447#ifdef ENABLE_LOG
5448 fprintf(log_fd, "------------------------------------------\n");
5449 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005450 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005451 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005452 {
5453 int i;
5454
5455 for (i = 0; i < thislist->n; i++)
5456 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5457 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005458 fprintf(log_fd, "\n");
5459#endif
5460
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005461#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005462 fprintf(debug, "\n-------------------\n");
5463#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005464 /*
5465 * If the state lists are empty we can stop.
5466 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005467 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005468 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005469
5470 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005471 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005472 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005473 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005474
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005475#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005476 nfa_set_code(t->state->c);
5477 fprintf(debug, "%s, ", code);
5478#endif
5479#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005480 {
5481 int col;
5482
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005483 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005484 col = -1;
5485 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005486 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005487 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005488 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005489 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005490 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5491 abs(t->state->id), (int)t->state->c, code, col,
5492 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005493 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005494#endif
5495
5496 /*
5497 * Handle the possible codes of the current state.
5498 * The most important is NFA_MATCH.
5499 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005500 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005501 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005502 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503 switch (t->state->c)
5504 {
5505 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005506 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005507 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005508 copy_sub(&submatch->norm, &t->subs.norm);
5509#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005510 if (nfa_has_zsubexpr)
5511 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005512#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005513#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005514 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005516 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005517 * states at this position. When the list of states is going
5518 * to be empty quit without advancing, so that "reginput" is
5519 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005520 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005521 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005522 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005523 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005524
5525 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005526 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005527 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005528 /*
5529 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005530 * NFA_START_INVISIBLE_BEFORE node.
5531 * They surround a zero-width group, used with "\@=", "\&",
5532 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005533 * If we got here, it means that the current "invisible" group
5534 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005535 * nfa_regmatch(). For a look-behind match only when it ends
5536 * in the position in "nfa_endp".
5537 * Submatches are stored in *m, and used in the parent call.
5538 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005539#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005540 if (nfa_endp != NULL)
5541 {
5542 if (REG_MULTI)
5543 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5544 (int)reglnum,
5545 (int)nfa_endp->se_u.pos.lnum,
5546 (int)(reginput - regline),
5547 nfa_endp->se_u.pos.col);
5548 else
5549 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5550 (int)(reginput - regline),
5551 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005553#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005554 /* If "nfa_endp" is set it's only a match if it ends at
5555 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005556 if (nfa_endp != NULL && (REG_MULTI
5557 ? (reglnum != nfa_endp->se_u.pos.lnum
5558 || (int)(reginput - regline)
5559 != nfa_endp->se_u.pos.col)
5560 : reginput != nfa_endp->se_u.ptr))
5561 break;
5562
5563 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005564 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005565 {
5566 copy_sub(&m->norm, &t->subs.norm);
5567#ifdef FEAT_SYN_HL
5568 if (nfa_has_zsubexpr)
5569 copy_sub(&m->synt, &t->subs.synt);
5570#endif
5571 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005572#ifdef ENABLE_LOG
5573 fprintf(log_fd, "Match found:\n");
5574 log_subsexpr(m);
5575#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005576 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005577 /* See comment above at "goto nextchar". */
5578 if (nextlist->n == 0)
5579 clen = 0;
5580 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005581
5582 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005583 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005584 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005585 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005586 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005587 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005588 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005589 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005590 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005591#ifdef ENABLE_LOG
5592 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5593 failure_chance(t->state->out, 0),
5594 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005595#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005596 /* Do it directly if there already is a PIM or when
5597 * nfa_postprocess() detected it will work better. */
5598 if (t->pim.result != NFA_PIM_UNUSED
5599 || t->state->c == NFA_START_INVISIBLE_FIRST
5600 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5601 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5602 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005603 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005604 int in_use = m->norm.in_use;
5605
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005606 /* Copy submatch info for the recursive call, so that
5607 * \1 can be matched. */
5608 copy_sub_off(&m->norm, &t->subs.norm);
5609
Bram Moolenaara2d95102013-06-04 14:23:05 +02005610 /*
5611 * First try matching the invisible match, then what
5612 * follows.
5613 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005614 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005615 submatch, m, &listids);
5616
Bram Moolenaardecd9542013-06-07 16:31:50 +02005617 /* for \@! and \@<! it is a match when the result is
5618 * FALSE */
5619 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005620 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5621 || t->state->c
5622 == NFA_START_INVISIBLE_BEFORE_NEG
5623 || t->state->c
5624 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005625 {
5626 /* Copy submatch info from the recursive call */
5627 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005628#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005629 if (nfa_has_zsubexpr)
5630 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005631#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005632
Bram Moolenaara2d95102013-06-04 14:23:05 +02005633 /* t->state->out1 is the corresponding
5634 * END_INVISIBLE node; Add its out to the current
5635 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005636 add_here = TRUE;
5637 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005638 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005639 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005640 }
5641 else
5642 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005643 nfa_pim_T pim;
5644
Bram Moolenaara2d95102013-06-04 14:23:05 +02005645 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005646 * First try matching what follows. Only if a match
5647 * is found verify the invisible match matches. Add a
5648 * nfa_pim_T to the following states, it contains info
5649 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005650 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005651 pim.state = t->state;
5652 pim.result = NFA_PIM_TODO;
5653 pim.subs.norm.in_use = 0;
5654#ifdef FEAT_SYN_HL
5655 pim.subs.synt.in_use = 0;
5656#endif
5657 if (REG_MULTI)
5658 {
5659 pim.end.pos.col = (int)(reginput - regline);
5660 pim.end.pos.lnum = reglnum;
5661 }
5662 else
5663 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005664
5665 /* t->state->out1 is the corresponding END_INVISIBLE
5666 * node; Add its out to the current list (zero-width
5667 * match). */
5668 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005669 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005670 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005671 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005672 break;
5673
Bram Moolenaar87953742013-06-05 18:52:40 +02005674 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005675 {
5676 nfa_state_T *skip = NULL;
5677#ifdef ENABLE_LOG
5678 int skip_lid = 0;
5679#endif
5680
5681 /* There is no point in trying to match the pattern if the
5682 * output state is not going to be added to the list. */
5683 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5684 {
5685 skip = t->state->out1->out;
5686#ifdef ENABLE_LOG
5687 skip_lid = nextlist->id;
5688#endif
5689 }
5690 else if (state_in_list(nextlist,
5691 t->state->out1->out->out, &t->subs))
5692 {
5693 skip = t->state->out1->out->out;
5694#ifdef ENABLE_LOG
5695 skip_lid = nextlist->id;
5696#endif
5697 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005698 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005699 t->state->out1->out->out, &t->subs))
5700 {
5701 skip = t->state->out1->out->out;
5702#ifdef ENABLE_LOG
5703 skip_lid = thislist->id;
5704#endif
5705 }
5706 if (skip != NULL)
5707 {
5708#ifdef ENABLE_LOG
5709 nfa_set_code(skip->c);
5710 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5711 abs(skip->id), skip_lid, skip->c, code);
5712#endif
5713 break;
5714 }
5715
Bram Moolenaar87953742013-06-05 18:52:40 +02005716 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005717 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005718 submatch, m, &listids);
5719 if (result)
5720 {
5721 int bytelen;
5722
5723#ifdef ENABLE_LOG
5724 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5725 log_subsexpr(m);
5726#endif
5727 /* Copy submatch info from the recursive call */
5728 copy_sub_off(&t->subs.norm, &m->norm);
5729#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005730 if (nfa_has_zsubexpr)
5731 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005732#endif
5733 /* Now we need to skip over the matched text and then
5734 * continue with what follows. */
5735 if (REG_MULTI)
5736 /* TODO: multi-line match */
5737 bytelen = m->norm.list.multi[0].end.col
5738 - (int)(reginput - regline);
5739 else
5740 bytelen = (int)(m->norm.list.line[0].end - reginput);
5741
5742#ifdef ENABLE_LOG
5743 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5744#endif
5745 if (bytelen == 0)
5746 {
5747 /* empty match, output of corresponding
5748 * NFA_END_PATTERN/NFA_SKIP to be used at current
5749 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005750 add_here = TRUE;
5751 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005752 }
5753 else if (bytelen <= clen)
5754 {
5755 /* match current character, output of corresponding
5756 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005757 add_state = t->state->out1->out->out;
5758 add_off = clen;
5759 }
5760 else
5761 {
5762 /* skip over the matched characters, set character
5763 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005764 add_state = t->state->out1->out;
5765 add_off = bytelen;
5766 add_count = bytelen - clen;
5767 }
5768 }
5769 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005770 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005771
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005772 case NFA_BOL:
5773 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005774 {
5775 add_here = TRUE;
5776 add_state = t->state->out;
5777 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005778 break;
5779
5780 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005781 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005782 {
5783 add_here = TRUE;
5784 add_state = t->state->out;
5785 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005786 break;
5787
5788 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005789 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005790
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005791 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005792 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005793#ifdef FEAT_MBYTE
5794 else if (has_mbyte)
5795 {
5796 int this_class;
5797
5798 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005799 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005800 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005801 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005802 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005803 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005804 }
5805#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005806 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005807 || (reginput > regline
5808 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005809 result = FALSE;
5810 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005811 {
5812 add_here = TRUE;
5813 add_state = t->state->out;
5814 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005815 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005816
5817 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005818 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005819 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005820 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005821#ifdef FEAT_MBYTE
5822 else if (has_mbyte)
5823 {
5824 int this_class, prev_class;
5825
5826 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005827 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005828 prev_class = reg_prev_class();
5829 if (this_class == prev_class
5830 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005831 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005832 }
5833#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005834 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005835 || (reginput[0] != NUL
5836 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005837 result = FALSE;
5838 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005839 {
5840 add_here = TRUE;
5841 add_state = t->state->out;
5842 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005843 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005844
Bram Moolenaar4b780632013-05-31 22:14:52 +02005845 case NFA_BOF:
5846 if (reglnum == 0 && reginput == regline
5847 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005848 {
5849 add_here = TRUE;
5850 add_state = t->state->out;
5851 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005852 break;
5853
5854 case NFA_EOF:
5855 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005856 {
5857 add_here = TRUE;
5858 add_state = t->state->out;
5859 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005860 break;
5861
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005862#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005863 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005864 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005865 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005866 int len = 0;
5867 nfa_state_T *end;
5868 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005869 int cchars[MAX_MCO];
5870 int ccount = 0;
5871 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005872
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005873 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005874 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005875 if (utf_iscomposing(sta->c))
5876 {
5877 /* Only match composing character(s), ignore base
5878 * character. Used for ".{composing}" and "{composing}"
5879 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005880 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005881 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005882 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005883 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005884 /* If \Z was present, then ignore composing characters.
5885 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005886 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005887 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005888 else
5889 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005890 while (sta->c != NFA_END_COMPOSING)
5891 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005892 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005893
5894 /* Check base character matches first, unless ignored. */
5895 else if (len > 0 || mc == sta->c)
5896 {
5897 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005898 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005899 len += mb_char2len(mc);
5900 sta = sta->out;
5901 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005902
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005903 /* We don't care about the order of composing characters.
5904 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005905 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005906 {
5907 mc = mb_ptr2char(reginput + len);
5908 cchars[ccount++] = mc;
5909 len += mb_char2len(mc);
5910 if (ccount == MAX_MCO)
5911 break;
5912 }
5913
5914 /* Check that each composing char in the pattern matches a
5915 * composing char in the text. We do not check if all
5916 * composing chars are matched. */
5917 result = OK;
5918 while (sta->c != NFA_END_COMPOSING)
5919 {
5920 for (j = 0; j < ccount; ++j)
5921 if (cchars[j] == sta->c)
5922 break;
5923 if (j == ccount)
5924 {
5925 result = FAIL;
5926 break;
5927 }
5928 sta = sta->out;
5929 }
5930 }
5931 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005932 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005933
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005934 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005935 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005936 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005937 }
5938#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005939
5940 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005941 if (curc == NUL && !reg_line_lbr && REG_MULTI
5942 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005943 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005944 go_to_nextline = TRUE;
5945 /* Pass -1 for the offset, which means taking the position
5946 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005947 add_state = t->state->out;
5948 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005949 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005950 else if (curc == '\n' && reg_line_lbr)
5951 {
5952 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005953 add_state = t->state->out;
5954 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005955 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005956 break;
5957
Bram Moolenaar417bad22013-06-07 14:08:30 +02005958 case NFA_START_COLL:
5959 case NFA_START_NEG_COLL:
5960 {
5961 /* What follows is a list of characters, until NFA_END_COLL.
5962 * One of them must match or none of them must match. */
5963 nfa_state_T *state;
5964 int result_if_matched;
5965 int c1, c2;
5966
5967 /* Never match EOL. If it's part of the collection it is added
5968 * as a separate state with an OR. */
5969 if (curc == NUL)
5970 break;
5971
5972 state = t->state->out;
5973 result_if_matched = (t->state->c == NFA_START_COLL);
5974 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005975 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005976 if (state->c == NFA_END_COLL)
5977 {
5978 result = !result_if_matched;
5979 break;
5980 }
5981 if (state->c == NFA_RANGE_MIN)
5982 {
5983 c1 = state->val;
5984 state = state->out; /* advance to NFA_RANGE_MAX */
5985 c2 = state->val;
5986#ifdef ENABLE_LOG
5987 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5988 curc, c1, c2);
5989#endif
5990 if (curc >= c1 && curc <= c2)
5991 {
5992 result = result_if_matched;
5993 break;
5994 }
5995 if (ireg_ic)
5996 {
5997 int curc_low = MB_TOLOWER(curc);
5998 int done = FALSE;
5999
6000 for ( ; c1 <= c2; ++c1)
6001 if (MB_TOLOWER(c1) == curc_low)
6002 {
6003 result = result_if_matched;
6004 done = TRUE;
6005 break;
6006 }
6007 if (done)
6008 break;
6009 }
6010 }
6011 else if (state->c < 0 ? check_char_class(state->c, curc)
6012 : (curc == state->c
6013 || (ireg_ic && MB_TOLOWER(curc)
6014 == MB_TOLOWER(state->c))))
6015 {
6016 result = result_if_matched;
6017 break;
6018 }
6019 state = state->out;
6020 }
6021 if (result)
6022 {
6023 /* next state is in out of the NFA_END_COLL, out1 of
6024 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006025 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006026 add_off = clen;
6027 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006028 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006029 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006030
6031 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006032 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006033 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006034 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006035 add_state = t->state->out;
6036 add_off = clen;
6037 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006038 break;
6039
6040 /*
6041 * Character classes like \a for alpha, \d for digit etc.
6042 */
6043 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006044 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006045 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006046 break;
6047
6048 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006049 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006050 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006051 break;
6052
6053 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006054 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006055 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006056 break;
6057
6058 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006059 result = !VIM_ISDIGIT(curc)
6060 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006061 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006062 break;
6063
6064 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006065 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006066 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067 break;
6068
6069 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006070 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006071 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006072 break;
6073
6074 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006075 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006076 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006077 break;
6078
6079 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006080 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006081 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006082 break;
6083
6084 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006085 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006086 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006087 break;
6088
6089 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006090 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006091 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006092 break;
6093
6094 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006095 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006096 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006097 break;
6098
6099 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006100 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006101 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006102 break;
6103
6104 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006105 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006106 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006107 break;
6108
6109 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006110 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006111 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006112 break;
6113
6114 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006115 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006116 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006117 break;
6118
6119 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006120 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006121 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006122 break;
6123
6124 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006125 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006126 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006127 break;
6128
6129 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006130 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006131 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006132 break;
6133
6134 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006135 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006136 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006137 break;
6138
6139 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006140 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006141 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006142 break;
6143
6144 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006145 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006146 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006147 break;
6148
6149 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006150 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006151 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006152 break;
6153
6154 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006155 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006156 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006157 break;
6158
6159 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006160 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006161 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006162 break;
6163
6164 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006165 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006166 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006167 break;
6168
6169 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006170 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006171 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006172 break;
6173
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006174 case NFA_LOWER_IC: /* [a-z] */
6175 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6176 ADD_STATE_IF_MATCH(t->state);
6177 break;
6178
6179 case NFA_NLOWER_IC: /* [^a-z] */
6180 result = curc != NUL
6181 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6182 ADD_STATE_IF_MATCH(t->state);
6183 break;
6184
6185 case NFA_UPPER_IC: /* [A-Z] */
6186 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6187 ADD_STATE_IF_MATCH(t->state);
6188 break;
6189
6190 case NFA_NUPPER_IC: /* ^[A-Z] */
6191 result = curc != NUL
6192 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6193 ADD_STATE_IF_MATCH(t->state);
6194 break;
6195
Bram Moolenaar5714b802013-05-28 22:03:20 +02006196 case NFA_BACKREF1:
6197 case NFA_BACKREF2:
6198 case NFA_BACKREF3:
6199 case NFA_BACKREF4:
6200 case NFA_BACKREF5:
6201 case NFA_BACKREF6:
6202 case NFA_BACKREF7:
6203 case NFA_BACKREF8:
6204 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006205#ifdef FEAT_SYN_HL
6206 case NFA_ZREF1:
6207 case NFA_ZREF2:
6208 case NFA_ZREF3:
6209 case NFA_ZREF4:
6210 case NFA_ZREF5:
6211 case NFA_ZREF6:
6212 case NFA_ZREF7:
6213 case NFA_ZREF8:
6214 case NFA_ZREF9:
6215#endif
6216 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006217 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006218 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006219 int bytelen;
6220
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006221 if (t->state->c <= NFA_BACKREF9)
6222 {
6223 subidx = t->state->c - NFA_BACKREF1 + 1;
6224 result = match_backref(&t->subs.norm, subidx, &bytelen);
6225 }
6226#ifdef FEAT_SYN_HL
6227 else
6228 {
6229 subidx = t->state->c - NFA_ZREF1 + 1;
6230 result = match_zref(subidx, &bytelen);
6231 }
6232#endif
6233
Bram Moolenaar5714b802013-05-28 22:03:20 +02006234 if (result)
6235 {
6236 if (bytelen == 0)
6237 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006238 /* empty match always works, output of NFA_SKIP to be
6239 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006240 add_here = TRUE;
6241 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006242 }
6243 else if (bytelen <= clen)
6244 {
6245 /* match current character, jump ahead to out of
6246 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006247 add_state = t->state->out->out;
6248 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006249 }
6250 else
6251 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006252 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006253 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006254 add_state = t->state->out;
6255 add_off = bytelen;
6256 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006257 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006258 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006259 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006260 }
6261 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006262 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006263 if (t->count - clen <= 0)
6264 {
6265 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006266 add_state = t->state->out;
6267 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006268 }
6269 else
6270 {
6271 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006272 add_state = t->state;
6273 add_off = 0;
6274 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006275 }
6276 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006277
Bram Moolenaar423532e2013-05-29 21:14:42 +02006278 case NFA_LNUM:
6279 case NFA_LNUM_GT:
6280 case NFA_LNUM_LT:
6281 result = (REG_MULTI &&
6282 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6283 (long_u)(reglnum + reg_firstlnum)));
6284 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006285 {
6286 add_here = TRUE;
6287 add_state = t->state->out;
6288 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006289 break;
6290
6291 case NFA_COL:
6292 case NFA_COL_GT:
6293 case NFA_COL_LT:
6294 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6295 (long_u)(reginput - regline) + 1);
6296 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006297 {
6298 add_here = TRUE;
6299 add_state = t->state->out;
6300 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006301 break;
6302
6303 case NFA_VCOL:
6304 case NFA_VCOL_GT:
6305 case NFA_VCOL_LT:
6306 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
6307 (long_u)win_linetabsize(
6308 reg_win == NULL ? curwin : reg_win,
6309 regline, (colnr_T)(reginput - regline)) + 1);
6310 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006311 {
6312 add_here = TRUE;
6313 add_state = t->state->out;
6314 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006315 break;
6316
Bram Moolenaar044aa292013-06-04 21:27:38 +02006317 case NFA_MARK:
6318 case NFA_MARK_GT:
6319 case NFA_MARK_LT:
6320 {
6321 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6322
6323 /* Compare the mark position to the match position. */
6324 result = (pos != NULL /* mark doesn't exist */
6325 && pos->lnum > 0 /* mark isn't set in reg_buf */
6326 && (pos->lnum == reglnum + reg_firstlnum
6327 ? (pos->col == (colnr_T)(reginput - regline)
6328 ? t->state->c == NFA_MARK
6329 : (pos->col < (colnr_T)(reginput - regline)
6330 ? t->state->c == NFA_MARK_GT
6331 : t->state->c == NFA_MARK_LT))
6332 : (pos->lnum < reglnum + reg_firstlnum
6333 ? t->state->c == NFA_MARK_GT
6334 : t->state->c == NFA_MARK_LT)));
6335 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006336 {
6337 add_here = TRUE;
6338 add_state = t->state->out;
6339 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006340 break;
6341 }
6342
Bram Moolenaar423532e2013-05-29 21:14:42 +02006343 case NFA_CURSOR:
6344 result = (reg_win != NULL
6345 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6346 && ((colnr_T)(reginput - regline)
6347 == reg_win->w_cursor.col));
6348 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006349 {
6350 add_here = TRUE;
6351 add_state = t->state->out;
6352 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006353 break;
6354
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006355 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02006356#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006357 result = reg_match_visual();
6358 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006359 {
6360 add_here = TRUE;
6361 add_state = t->state->out;
6362 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02006363#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02006364 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006365
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006366 case NFA_MOPEN1:
6367 case NFA_MOPEN2:
6368 case NFA_MOPEN3:
6369 case NFA_MOPEN4:
6370 case NFA_MOPEN5:
6371 case NFA_MOPEN6:
6372 case NFA_MOPEN7:
6373 case NFA_MOPEN8:
6374 case NFA_MOPEN9:
6375#ifdef FEAT_SYN_HL
6376 case NFA_ZOPEN:
6377 case NFA_ZOPEN1:
6378 case NFA_ZOPEN2:
6379 case NFA_ZOPEN3:
6380 case NFA_ZOPEN4:
6381 case NFA_ZOPEN5:
6382 case NFA_ZOPEN6:
6383 case NFA_ZOPEN7:
6384 case NFA_ZOPEN8:
6385 case NFA_ZOPEN9:
6386#endif
6387 case NFA_NOPEN:
6388 case NFA_ZSTART:
6389 /* These states are only added to be able to bail out when
6390 * they are added again, nothing is to be done. */
6391 break;
6392
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006393 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006394 {
6395 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006396
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006397#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006398 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006399 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006400#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006401 result = (c == curc);
6402
6403 if (!result && ireg_ic)
6404 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006405#ifdef FEAT_MBYTE
6406 /* If there is a composing character which is not being
6407 * ignored there can be no match. Match with composing
6408 * character uses NFA_COMPOSING above. */
6409 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006410 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006411 result = FALSE;
6412#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006413 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006415 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006416
6417 } /* switch (t->state->c) */
6418
6419 if (add_state != NULL)
6420 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006421 nfa_pim_T *pim;
6422
6423 if (t->pim.result == NFA_PIM_UNUSED)
6424 pim = NULL;
6425 else
6426 pim = &t->pim;
6427
6428 /* Handle the postponed invisible match if the match might end
6429 * without advancing and before the end of the line. */
6430 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006431 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006432 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006433 {
6434#ifdef ENABLE_LOG
6435 fprintf(log_fd, "\n");
6436 fprintf(log_fd, "==================================\n");
6437 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6438 fprintf(log_fd, "\n");
6439#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006440 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006441 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006442 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006443 /* for \@! and \@<! it is a match when the result is
6444 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006445 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006446 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6447 || pim->state->c
6448 == NFA_START_INVISIBLE_BEFORE_NEG
6449 || pim->state->c
6450 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006451 {
6452 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006453 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006454#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006455 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006456 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006457#endif
6458 }
6459 }
6460 else
6461 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006462 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006463#ifdef ENABLE_LOG
6464 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006465 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006466 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6467 fprintf(log_fd, "\n");
6468#endif
6469 }
6470
Bram Moolenaardecd9542013-06-07 16:31:50 +02006471 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006472 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006473 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6474 || pim->state->c
6475 == NFA_START_INVISIBLE_BEFORE_NEG
6476 || pim->state->c
6477 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006478 {
6479 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006480 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006481#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006482 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006483 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006484#endif
6485 }
6486 else
6487 /* look-behind match failed, don't add the state */
6488 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006489
6490 /* Postponed invisible match was handled, don't add it to
6491 * following states. */
6492 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006493 }
6494
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006495 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006496 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006497 else
6498 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006499 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006500 if (add_count > 0)
6501 nextlist->t[nextlist->n - 1].count = add_count;
6502 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006503 }
6504
6505 } /* for (thislist = thislist; thislist->state; thislist++) */
6506
Bram Moolenaare23febd2013-05-26 18:40:14 +02006507 /* Look for the start of a match in the current position by adding the
6508 * start state to the list of states.
6509 * The first found match is the leftmost one, thus the order of states
6510 * matters!
6511 * Do not add the start state in recursive calls of nfa_regmatch(),
6512 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006513 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006514 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006515 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006516 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006517 && reglnum == 0
6518 && clen != 0
6519 && (ireg_maxcol == 0
6520 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006521 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006522 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006523 ? (reglnum < nfa_endp->se_u.pos.lnum
6524 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006525 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006526 < nfa_endp->se_u.pos.col))
6527 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006528 {
6529#ifdef ENABLE_LOG
6530 fprintf(log_fd, "(---) STARTSTATE\n");
6531#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006532 /* Inline optimized code for addstate() if we know the state is
6533 * the first MOPEN. */
6534 if (toplevel)
6535 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006536 int add = TRUE;
6537 int c;
6538
6539 if (prog->regstart != NUL && clen != 0)
6540 {
6541 if (nextlist->n == 0)
6542 {
6543 colnr_T col = (colnr_T)(reginput - regline) + clen;
6544
6545 /* Nextlist is empty, we can skip ahead to the
6546 * character that must appear at the start. */
6547 if (skip_to_start(prog->regstart, &col) == FAIL)
6548 break;
6549#ifdef ENABLE_LOG
6550 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6551 col - ((colnr_T)(reginput - regline) + clen));
6552#endif
6553 reginput = regline + col - clen;
6554 }
6555 else
6556 {
6557 /* Checking if the required start character matches is
6558 * cheaper than adding a state that won't match. */
6559 c = PTR2CHAR(reginput + clen);
6560 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6561 != MB_TOLOWER(prog->regstart)))
6562 {
6563#ifdef ENABLE_LOG
6564 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6565#endif
6566 add = FALSE;
6567 }
6568 }
6569 }
6570
6571 if (add)
6572 {
6573 if (REG_MULTI)
6574 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006575 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006576 else
6577 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006578 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006579 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006580 }
6581 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006582 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006583 }
6584
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006585#ifdef ENABLE_LOG
6586 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006587 {
6588 int i;
6589
6590 for (i = 0; i < thislist->n; i++)
6591 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6592 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006593 fprintf(log_fd, "\n");
6594#endif
6595
6596nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006597 /* Advance to the next character, or advance to the next line, or
6598 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006599 if (clen != 0)
6600 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006601 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6602 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006603 reg_nextline();
6604 else
6605 break;
6606 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006607
6608#ifdef ENABLE_LOG
6609 if (log_fd != stderr)
6610 fclose(log_fd);
6611 log_fd = NULL;
6612#endif
6613
6614theend:
6615 /* Free memory */
6616 vim_free(list[0].t);
6617 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006618 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006619#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006620#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006621 fclose(debug);
6622#endif
6623
Bram Moolenaar963fee22013-05-26 21:47:28 +02006624 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006625}
6626
6627/*
6628 * Try match of "prog" with at regline["col"].
6629 * Returns 0 for failure, number of lines contained in the match otherwise.
6630 */
6631 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006632nfa_regtry(prog, col)
6633 nfa_regprog_T *prog;
6634 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006635{
6636 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006637 regsubs_T subs, m;
6638 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006639#ifdef ENABLE_LOG
6640 FILE *f;
6641#endif
6642
6643 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006644
6645#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006646 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006647 if (f != NULL)
6648 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006649 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006650#ifdef DEBUG
6651 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6652#endif
6653 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006654 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006655 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006656 fprintf(f, "\n\n");
6657 fclose(f);
6658 }
6659 else
6660 EMSG(_("Could not open temporary log file for writing "));
6661#endif
6662
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006663 clear_sub(&subs.norm);
6664 clear_sub(&m.norm);
6665#ifdef FEAT_SYN_HL
6666 clear_sub(&subs.synt);
6667 clear_sub(&m.synt);
6668#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006669
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006670 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006671 return 0;
6672
6673 cleanup_subexpr();
6674 if (REG_MULTI)
6675 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006676 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006677 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006678 reg_startpos[i] = subs.norm.list.multi[i].start;
6679 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006680 }
6681
6682 if (reg_startpos[0].lnum < 0)
6683 {
6684 reg_startpos[0].lnum = 0;
6685 reg_startpos[0].col = col;
6686 }
6687 if (reg_endpos[0].lnum < 0)
6688 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006689 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006690 reg_endpos[0].lnum = reglnum;
6691 reg_endpos[0].col = (int)(reginput - regline);
6692 }
6693 else
6694 /* Use line number of "\ze". */
6695 reglnum = reg_endpos[0].lnum;
6696 }
6697 else
6698 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006699 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006700 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006701 reg_startp[i] = subs.norm.list.line[i].start;
6702 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006703 }
6704
6705 if (reg_startp[0] == NULL)
6706 reg_startp[0] = regline + col;
6707 if (reg_endp[0] == NULL)
6708 reg_endp[0] = reginput;
6709 }
6710
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006711#ifdef FEAT_SYN_HL
6712 /* Package any found \z(...\) matches for export. Default is none. */
6713 unref_extmatch(re_extmatch_out);
6714 re_extmatch_out = NULL;
6715
6716 if (prog->reghasz == REX_SET)
6717 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006718 cleanup_zsubexpr();
6719 re_extmatch_out = make_extmatch();
6720 for (i = 0; i < subs.synt.in_use; i++)
6721 {
6722 if (REG_MULTI)
6723 {
6724 struct multipos *mpos = &subs.synt.list.multi[i];
6725
6726 /* Only accept single line matches. */
6727 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6728 re_extmatch_out->matches[i] =
6729 vim_strnsave(reg_getline(mpos->start.lnum)
6730 + mpos->start.col,
6731 mpos->end.col - mpos->start.col);
6732 }
6733 else
6734 {
6735 struct linepos *lpos = &subs.synt.list.line[i];
6736
6737 if (lpos->start != NULL && lpos->end != NULL)
6738 re_extmatch_out->matches[i] =
6739 vim_strnsave(lpos->start,
6740 (int)(lpos->end - lpos->start));
6741 }
6742 }
6743 }
6744#endif
6745
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006746 return 1 + reglnum;
6747}
6748
6749/*
6750 * Match a regexp against a string ("line" points to the string) or multiple
6751 * lines ("line" is NULL, use reg_getline()).
6752 *
6753 * Returns 0 for failure, number of lines contained in the match otherwise.
6754 */
6755 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006756nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006757 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006758 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006759{
6760 nfa_regprog_T *prog;
6761 long retval = 0L;
6762 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006763 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006764
6765 if (REG_MULTI)
6766 {
6767 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6768 line = reg_getline((linenr_T)0); /* relative to the cursor */
6769 reg_startpos = reg_mmatch->startpos;
6770 reg_endpos = reg_mmatch->endpos;
6771 }
6772 else
6773 {
6774 prog = (nfa_regprog_T *)reg_match->regprog;
6775 reg_startp = reg_match->startp;
6776 reg_endp = reg_match->endp;
6777 }
6778
6779 /* Be paranoid... */
6780 if (prog == NULL || line == NULL)
6781 {
6782 EMSG(_(e_null));
6783 goto theend;
6784 }
6785
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006786 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6787 if (prog->regflags & RF_ICASE)
6788 ireg_ic = TRUE;
6789 else if (prog->regflags & RF_NOICASE)
6790 ireg_ic = FALSE;
6791
6792#ifdef FEAT_MBYTE
6793 /* If pattern contains "\Z" overrule value of ireg_icombine */
6794 if (prog->regflags & RF_ICOMBINE)
6795 ireg_icombine = TRUE;
6796#endif
6797
6798 regline = line;
6799 reglnum = 0; /* relative to line */
6800
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006801 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006802 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006803 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006804 nfa_listid = 1;
6805 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006806#ifdef DEBUG
6807 nfa_regengine.expr = prog->pattern;
6808#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006809
Bram Moolenaard89616e2013-06-06 18:46:06 +02006810 if (prog->reganch && col > 0)
6811 return 0L;
6812
Bram Moolenaar473de612013-06-08 18:19:48 +02006813 need_clear_subexpr = TRUE;
6814#ifdef FEAT_SYN_HL
6815 /* Clear the external match subpointers if necessary. */
6816 if (prog->reghasz == REX_SET)
6817 {
6818 nfa_has_zsubexpr = TRUE;
6819 need_clear_zsubexpr = TRUE;
6820 }
6821 else
6822 nfa_has_zsubexpr = FALSE;
6823#endif
6824
Bram Moolenaard89616e2013-06-06 18:46:06 +02006825 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006826 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006827 /* Skip ahead until a character we know the match must start with.
6828 * When there is none there is no match. */
6829 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006830 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006831
Bram Moolenaar473de612013-06-08 18:19:48 +02006832 /* If match_text is set it contains the full text that must match.
6833 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006834 if (prog->match_text != NULL
6835#ifdef FEAT_MBYTE
6836 && !ireg_icombine
6837#endif
6838 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006839 return find_match_text(col, prog->regstart, prog->match_text);
6840 }
6841
Bram Moolenaard89616e2013-06-06 18:46:06 +02006842 /* If the start column is past the maximum column: no need to try. */
6843 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6844 goto theend;
6845
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006846 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006847 for (i = 0; i < nstate; ++i)
6848 {
6849 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006850 prog->state[i].lastlist[0] = 0;
6851 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006852 }
6853
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006854 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006855
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006856#ifdef DEBUG
6857 nfa_regengine.expr = NULL;
6858#endif
6859
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006860theend:
6861 return retval;
6862}
6863
6864/*
6865 * Compile a regular expression into internal code for the NFA matcher.
6866 * Returns the program in allocated space. Returns NULL for an error.
6867 */
6868 static regprog_T *
6869nfa_regcomp(expr, re_flags)
6870 char_u *expr;
6871 int re_flags;
6872{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006873 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006874 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006875 int *postfix;
6876
6877 if (expr == NULL)
6878 return NULL;
6879
6880#ifdef DEBUG
6881 nfa_regengine.expr = expr;
6882#endif
6883
6884 init_class_tab();
6885
6886 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6887 return NULL;
6888
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006889 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006890 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006891 postfix = re2post();
6892 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006893 {
6894 /* TODO: only give this error for debugging? */
6895 if (post_ptr >= post_end)
6896 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006897 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006898 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006899
6900 /*
6901 * In order to build the NFA, we parse the input regexp twice:
6902 * 1. first pass to count size (so we can allocate space)
6903 * 2. second to emit code
6904 */
6905#ifdef ENABLE_LOG
6906 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006907 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006908
6909 if (f != NULL)
6910 {
6911 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6912 fclose(f);
6913 }
6914 }
6915#endif
6916
6917 /*
6918 * PASS 1
6919 * Count number of NFA states in "nstate". Do not build the NFA.
6920 */
6921 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006922
Bram Moolenaar16619a22013-06-11 18:42:36 +02006923 /* allocate the regprog with space for the compiled regexp */
6924 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006925 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6926 if (prog == NULL)
6927 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006928 state_ptr = prog->state;
6929
6930 /*
6931 * PASS 2
6932 * Build the NFA
6933 */
6934 prog->start = post2nfa(postfix, post_ptr, FALSE);
6935 if (prog->start == NULL)
6936 goto fail;
6937
6938 prog->regflags = regflags;
6939 prog->engine = &nfa_regengine;
6940 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006941 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006942 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006943 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006944
Bram Moolenaara2947e22013-06-11 22:44:09 +02006945 nfa_postprocess(prog);
6946
Bram Moolenaard89616e2013-06-06 18:46:06 +02006947 prog->reganch = nfa_get_reganch(prog->start, 0);
6948 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006949 prog->match_text = nfa_get_match_text(prog->start);
6950
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006951#ifdef ENABLE_LOG
6952 nfa_postfix_dump(expr, OK);
6953 nfa_dump(prog);
6954#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006955#ifdef FEAT_SYN_HL
6956 /* Remember whether this pattern has any \z specials in it. */
6957 prog->reghasz = re_has_z;
6958#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006959#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006960 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006961 nfa_regengine.expr = NULL;
6962#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006963
6964out:
6965 vim_free(post_start);
6966 post_start = post_ptr = post_end = NULL;
6967 state_ptr = NULL;
6968 return (regprog_T *)prog;
6969
6970fail:
6971 vim_free(prog);
6972 prog = NULL;
6973#ifdef ENABLE_LOG
6974 nfa_postfix_dump(expr, FAIL);
6975#endif
6976#ifdef DEBUG
6977 nfa_regengine.expr = NULL;
6978#endif
6979 goto out;
6980}
6981
Bram Moolenaar473de612013-06-08 18:19:48 +02006982/*
6983 * Free a compiled regexp program, returned by nfa_regcomp().
6984 */
6985 static void
6986nfa_regfree(prog)
6987 regprog_T *prog;
6988{
6989 if (prog != NULL)
6990 {
6991 vim_free(((nfa_regprog_T *)prog)->match_text);
6992#ifdef DEBUG
6993 vim_free(((nfa_regprog_T *)prog)->pattern);
6994#endif
6995 vim_free(prog);
6996 }
6997}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006998
6999/*
7000 * Match a regexp against a string.
7001 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7002 * Uses curbuf for line count and 'iskeyword'.
7003 *
7004 * Return TRUE if there is a match, FALSE if not.
7005 */
7006 static int
7007nfa_regexec(rmp, line, col)
7008 regmatch_T *rmp;
7009 char_u *line; /* string to match against */
7010 colnr_T col; /* column to start looking for match */
7011{
7012 reg_match = rmp;
7013 reg_mmatch = NULL;
7014 reg_maxline = 0;
7015 reg_line_lbr = FALSE;
7016 reg_buf = curbuf;
7017 reg_win = NULL;
7018 ireg_ic = rmp->rm_ic;
7019#ifdef FEAT_MBYTE
7020 ireg_icombine = FALSE;
7021#endif
7022 ireg_maxcol = 0;
7023 return (nfa_regexec_both(line, col) != 0);
7024}
7025
7026#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
7027 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
7028
7029static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
7030
7031/*
7032 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
7033 */
7034 static int
7035nfa_regexec_nl(rmp, line, col)
7036 regmatch_T *rmp;
7037 char_u *line; /* string to match against */
7038 colnr_T col; /* column to start looking for match */
7039{
7040 reg_match = rmp;
7041 reg_mmatch = NULL;
7042 reg_maxline = 0;
7043 reg_line_lbr = TRUE;
7044 reg_buf = curbuf;
7045 reg_win = NULL;
7046 ireg_ic = rmp->rm_ic;
7047#ifdef FEAT_MBYTE
7048 ireg_icombine = FALSE;
7049#endif
7050 ireg_maxcol = 0;
7051 return (nfa_regexec_both(line, col) != 0);
7052}
7053#endif
7054
7055
7056/*
7057 * Match a regexp against multiple lines.
7058 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7059 * Uses curbuf for line count and 'iskeyword'.
7060 *
7061 * Return zero if there is no match. Return number of lines contained in the
7062 * match otherwise.
7063 *
7064 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7065 *
7066 * ! Also NOTE : match may actually be in another line. e.g.:
7067 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7068 *
7069 * +-------------------------+
7070 * |a |
7071 * |b |
7072 * |c |
7073 * | |
7074 * +-------------------------+
7075 *
7076 * then nfa_regexec_multi() returns 3. while the original
7077 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7078 *
7079 * FIXME if this behavior is not compatible.
7080 */
7081 static long
7082nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7083 regmmatch_T *rmp;
7084 win_T *win; /* window in which to search or NULL */
7085 buf_T *buf; /* buffer in which to search */
7086 linenr_T lnum; /* nr of line to start looking for match */
7087 colnr_T col; /* column to start looking for match */
7088 proftime_T *tm UNUSED; /* timeout limit or NULL */
7089{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007090 reg_match = NULL;
7091 reg_mmatch = rmp;
7092 reg_buf = buf;
7093 reg_win = win;
7094 reg_firstlnum = lnum;
7095 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7096 reg_line_lbr = FALSE;
7097 ireg_ic = rmp->rmm_ic;
7098#ifdef FEAT_MBYTE
7099 ireg_icombine = FALSE;
7100#endif
7101 ireg_maxcol = rmp->rmm_maxcol;
7102
Bram Moolenaarf878bf02013-05-21 21:20:20 +02007103 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007104}
7105
7106#ifdef DEBUG
7107# undef ENABLE_LOG
7108#endif