blob: 3873d9ab23140a413e96dacb314fe08f6e237cc6 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
53 NFA_STAR, /* greedy * (posfix only) */
54 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram 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
Bram Moolenaar174a8482013-11-28 14:20:17 +0100242static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200243static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100244static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200245
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200247static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200248
Bram Moolenaar428e9872013-05-30 17:05:39 +0200249/* NFA regexp \1 .. \9 encountered. */
250static int nfa_has_backref;
251
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200252#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200253/* NFA regexp has \z( ), set zsubexpr. */
254static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200255#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200256
Bram Moolenaar963fee22013-05-26 21:47:28 +0200257/* Number of sub expressions actually being used during execution. 1 if only
258 * the whole match (subexpr 0) is used. */
259static int nfa_nsubexpr;
260
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200261static int *post_start; /* holds the postfix form of r.e. */
262static int *post_end;
263static int *post_ptr;
264
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200265static int nstate; /* Number of states in the NFA. Also used when
266 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200267static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200268
Bram Moolenaar307aa162013-06-02 16:34:21 +0200269/* If not NULL match must end at this position */
270static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200271
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200272/* listid is global, so that it increases on recursive calls to
273 * nfa_regmatch(), which means we don't have to clear the lastlist field of
274 * all the states. */
275static int nfa_listid;
276static int nfa_alt_listid;
277
278/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
279static int nfa_ll_index = 0;
280
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200281static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200282static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
283static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200284static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200285static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200287static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200288static int nfa_regatom __ARGS((void));
289static int nfa_regpiece __ARGS((void));
290static int nfa_regconcat __ARGS((void));
291static int nfa_regbranch __ARGS((void));
292static int nfa_reg __ARGS((int paren));
293#ifdef DEBUG
294static void nfa_set_code __ARGS((int c));
295static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200296static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
297static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200298static void nfa_dump __ARGS((nfa_regprog_T *prog));
299#endif
300static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200301static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +0200302static void st_error __ARGS((int *postfix, int *end, int *p));
303static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200304static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200305static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200306static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200307static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
308static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200309static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200310static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200311static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
312static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200313static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaar2af78a12014-04-23 19:06:37 +0200314static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200315static 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 +0200316static int match_follows __ARGS((nfa_state_T *startstate, int depth));
317static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200318
319/* helper functions used when doing re2post() ... regatom() parsing */
320#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200321 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200322 return FAIL; \
323 *post_ptr++ = c; \
324 } while (0)
325
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200326/*
327 * Initialize internal variables before NFA compilation.
328 * Return OK on success, FAIL otherwise.
329 */
330 static int
331nfa_regcomp_start(expr, re_flags)
332 char_u *expr;
333 int re_flags; /* see vim_regcomp() */
334{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200335 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200336 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200337
338 nstate = 0;
339 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200340 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200341 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200342
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200343 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200344 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200345 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200346
347 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200348 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200349
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200350 post_start = (int *)lalloc(postfix_size, TRUE);
351 if (post_start == NULL)
352 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200353 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200354 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200355 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200356 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200358 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200359 regcomp_start(expr, re_flags);
360
361 return OK;
362}
363
364/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200365 * Figure out if the NFA state list starts with an anchor, must match at start
366 * of the line.
367 */
368 static int
369nfa_get_reganch(start, depth)
370 nfa_state_T *start;
371 int depth;
372{
373 nfa_state_T *p = start;
374
375 if (depth > 4)
376 return 0;
377
378 while (p != NULL)
379 {
380 switch (p->c)
381 {
382 case NFA_BOL:
383 case NFA_BOF:
384 return 1; /* yes! */
385
386 case NFA_ZSTART:
387 case NFA_ZEND:
388 case NFA_CURSOR:
389 case NFA_VISUAL:
390
391 case NFA_MOPEN:
392 case NFA_MOPEN1:
393 case NFA_MOPEN2:
394 case NFA_MOPEN3:
395 case NFA_MOPEN4:
396 case NFA_MOPEN5:
397 case NFA_MOPEN6:
398 case NFA_MOPEN7:
399 case NFA_MOPEN8:
400 case NFA_MOPEN9:
401 case NFA_NOPEN:
402#ifdef FEAT_SYN_HL
403 case NFA_ZOPEN:
404 case NFA_ZOPEN1:
405 case NFA_ZOPEN2:
406 case NFA_ZOPEN3:
407 case NFA_ZOPEN4:
408 case NFA_ZOPEN5:
409 case NFA_ZOPEN6:
410 case NFA_ZOPEN7:
411 case NFA_ZOPEN8:
412 case NFA_ZOPEN9:
413#endif
414 p = p->out;
415 break;
416
417 case NFA_SPLIT:
418 return nfa_get_reganch(p->out, depth + 1)
419 && nfa_get_reganch(p->out1, depth + 1);
420
421 default:
422 return 0; /* noooo */
423 }
424 }
425 return 0;
426}
427
428/*
429 * Figure out if the NFA state list starts with a character which must match
430 * at start of the match.
431 */
432 static int
433nfa_get_regstart(start, depth)
434 nfa_state_T *start;
435 int depth;
436{
437 nfa_state_T *p = start;
438
439 if (depth > 4)
440 return 0;
441
442 while (p != NULL)
443 {
444 switch (p->c)
445 {
446 /* all kinds of zero-width matches */
447 case NFA_BOL:
448 case NFA_BOF:
449 case NFA_BOW:
450 case NFA_EOW:
451 case NFA_ZSTART:
452 case NFA_ZEND:
453 case NFA_CURSOR:
454 case NFA_VISUAL:
455 case NFA_LNUM:
456 case NFA_LNUM_GT:
457 case NFA_LNUM_LT:
458 case NFA_COL:
459 case NFA_COL_GT:
460 case NFA_COL_LT:
461 case NFA_VCOL:
462 case NFA_VCOL_GT:
463 case NFA_VCOL_LT:
464 case NFA_MARK:
465 case NFA_MARK_GT:
466 case NFA_MARK_LT:
467
468 case NFA_MOPEN:
469 case NFA_MOPEN1:
470 case NFA_MOPEN2:
471 case NFA_MOPEN3:
472 case NFA_MOPEN4:
473 case NFA_MOPEN5:
474 case NFA_MOPEN6:
475 case NFA_MOPEN7:
476 case NFA_MOPEN8:
477 case NFA_MOPEN9:
478 case NFA_NOPEN:
479#ifdef FEAT_SYN_HL
480 case NFA_ZOPEN:
481 case NFA_ZOPEN1:
482 case NFA_ZOPEN2:
483 case NFA_ZOPEN3:
484 case NFA_ZOPEN4:
485 case NFA_ZOPEN5:
486 case NFA_ZOPEN6:
487 case NFA_ZOPEN7:
488 case NFA_ZOPEN8:
489 case NFA_ZOPEN9:
490#endif
491 p = p->out;
492 break;
493
494 case NFA_SPLIT:
495 {
496 int c1 = nfa_get_regstart(p->out, depth + 1);
497 int c2 = nfa_get_regstart(p->out1, depth + 1);
498
499 if (c1 == c2)
500 return c1; /* yes! */
501 return 0;
502 }
503
504 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200505 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200506 return p->c; /* yes! */
507 return 0;
508 }
509 }
510 return 0;
511}
512
513/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200514 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200515 * else. If so return a string in allocated memory with what must match after
516 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200517 */
518 static char_u *
519nfa_get_match_text(start)
520 nfa_state_T *start;
521{
522 nfa_state_T *p = start;
523 int len = 0;
524 char_u *ret;
525 char_u *s;
526
527 if (p->c != NFA_MOPEN)
528 return NULL; /* just in case */
529 p = p->out;
530 while (p->c > 0)
531 {
532 len += MB_CHAR2LEN(p->c);
533 p = p->out;
534 }
535 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
536 return NULL;
537
538 ret = alloc(len);
539 if (ret != NULL)
540 {
541 len = 0;
542 p = start->out->out; /* skip first char, it goes into regstart */
543 s = ret;
544 while (p->c > 0)
545 {
546#ifdef FEAT_MBYTE
547 if (has_mbyte)
548 s += (*mb_char2bytes)(p->c, s);
549 else
550#endif
551 *s++ = p->c;
552 p = p->out;
553 }
554 *s = NUL;
555 }
556 return ret;
557}
558
559/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200560 * Allocate more space for post_start. Called when
561 * running above the estimated number of states.
562 */
563 static int
564realloc_post_list()
565{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200566 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200567 int new_max = nstate_max + 1000;
568 int *new_start;
569 int *old_start;
570
571 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
572 if (new_start == NULL)
573 return FAIL;
574 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200575 old_start = post_start;
576 post_start = new_start;
577 post_ptr = new_start + (post_ptr - old_start);
578 post_end = post_start + new_max;
579 vim_free(old_start);
580 return OK;
581}
582
583/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200584 * Search between "start" and "end" and try to recognize a
585 * character class in expanded form. For example [0-9].
586 * On success, return the id the character class to be emitted.
587 * On failure, return 0 (=FAIL)
588 * Start points to the first char of the range, while end should point
589 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200590 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
591 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200592 */
593 static int
594nfa_recognize_char_class(start, end, extra_newl)
595 char_u *start;
596 char_u *end;
597 int extra_newl;
598{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200599# define CLASS_not 0x80
600# define CLASS_af 0x40
601# define CLASS_AF 0x20
602# define CLASS_az 0x10
603# define CLASS_AZ 0x08
604# define CLASS_o7 0x04
605# define CLASS_o9 0x02
606# define CLASS_underscore 0x01
607
608 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200610 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200611
612 if (extra_newl == TRUE)
613 newl = TRUE;
614
615 if (*end != ']')
616 return FAIL;
617 p = start;
618 if (*p == '^')
619 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200620 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200621 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200622 }
623
624 while (p < end)
625 {
626 if (p + 2 < end && *(p + 1) == '-')
627 {
628 switch (*p)
629 {
630 case '0':
631 if (*(p + 2) == '9')
632 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200633 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200634 break;
635 }
636 else
637 if (*(p + 2) == '7')
638 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200639 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200640 break;
641 }
642 case 'a':
643 if (*(p + 2) == 'z')
644 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200645 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200646 break;
647 }
648 else
649 if (*(p + 2) == 'f')
650 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200651 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200652 break;
653 }
654 case 'A':
655 if (*(p + 2) == 'Z')
656 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200657 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200658 break;
659 }
660 else
661 if (*(p + 2) == 'F')
662 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200663 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200664 break;
665 }
666 /* FALLTHROUGH */
667 default:
668 return FAIL;
669 }
670 p += 3;
671 }
672 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
673 {
674 newl = TRUE;
675 p += 2;
676 }
677 else if (*p == '_')
678 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200679 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200680 p ++;
681 }
682 else if (*p == '\n')
683 {
684 newl = TRUE;
685 p ++;
686 }
687 else
688 return FAIL;
689 } /* while (p < end) */
690
691 if (p != end)
692 return FAIL;
693
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200694 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200695 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200696
697 switch (config)
698 {
699 case CLASS_o9:
700 return extra_newl + NFA_DIGIT;
701 case CLASS_not | CLASS_o9:
702 return extra_newl + NFA_NDIGIT;
703 case CLASS_af | CLASS_AF | CLASS_o9:
704 return extra_newl + NFA_HEX;
705 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
706 return extra_newl + NFA_NHEX;
707 case CLASS_o7:
708 return extra_newl + NFA_OCTAL;
709 case CLASS_not | CLASS_o7:
710 return extra_newl + NFA_NOCTAL;
711 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
712 return extra_newl + NFA_WORD;
713 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
714 return extra_newl + NFA_NWORD;
715 case CLASS_az | CLASS_AZ | CLASS_underscore:
716 return extra_newl + NFA_HEAD;
717 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
718 return extra_newl + NFA_NHEAD;
719 case CLASS_az | CLASS_AZ:
720 return extra_newl + NFA_ALPHA;
721 case CLASS_not | CLASS_az | CLASS_AZ:
722 return extra_newl + NFA_NALPHA;
723 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200724 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200725 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200726 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200727 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200728 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200729 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200730 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200731 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200733}
734
735/*
736 * Produce the bytes for equivalence class "c".
737 * Currently only handles latin1, latin9 and utf-8.
738 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
739 * equivalent to 'a OR b OR c'
740 *
741 * NOTE! When changing this function, also update reg_equi_class()
742 */
743 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200744nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200745 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200746{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200747#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
748#ifdef FEAT_MBYTE
749# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
750#else
751# define EMITMBC(c)
752#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200753
754#ifdef FEAT_MBYTE
755 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
756 || STRCMP(p_enc, "iso-8859-15") == 0)
757#endif
758 {
759 switch (c)
760 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200761 case 'A': case 0300: case 0301: case 0302:
762 case 0303: case 0304: case 0305:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200763 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
764 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
765 EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302);
766 EMIT2(0303); EMIT2(0304); EMIT2(0305);
767 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
768 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
769 EMITMBC(0x1ea2)
770 return OK;
771
772 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
773 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200774 return OK;
775
Bram Moolenaar417bad22013-06-07 14:08:30 +0200776 case 'C': case 0307:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200777 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
778 EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108)
779 EMITMBC(0x10a) EMITMBC(0x10c)
780 return OK;
781
782 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
783 CASEMBC(0x1e0e) CASEMBC(0x1e10)
784 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
785 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200786 return OK;
787
Bram Moolenaar417bad22013-06-07 14:08:30 +0200788 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200789 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
790 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
791 EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312);
792 EMIT2(0313);
793 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
794 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
795 EMITMBC(0x1ebc)
796 return OK;
797
798 case 'F': CASEMBC(0x1e1e)
799 EMIT2('F'); EMITMBC(0x1e1e)
800 return OK;
801
802 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
803 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
804 CASEMBC(0x1e20)
805 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
806 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
807 EMITMBC(0x1f4) EMITMBC(0x1e20)
808 return OK;
809
810 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
811 CASEMBC(0x1e26) CASEMBC(0x1e28)
812 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
813 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 return OK;
815
Bram Moolenaar417bad22013-06-07 14:08:30 +0200816 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200817 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
818 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
819 EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316);
820 EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a)
821 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
822 EMITMBC(0x1cf) EMITMBC(0x1ec8)
823 return OK;
824
825 case 'J': CASEMBC(0x134)
826 EMIT2('J'); EMITMBC(0x134)
827 return OK;
828
829 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
830 CASEMBC(0x1e34)
831 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
832 EMITMBC(0x1e34)
833 return OK;
834
835 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
836 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
837 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
838 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
839 return OK;
840
841 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
842 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200843 return OK;
844
Bram Moolenaar417bad22013-06-07 14:08:30 +0200845 case 'N': case 0321:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200846 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
847 CASEMBC(0x1e48)
848 EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145)
849 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200850 return OK;
851
Bram Moolenaar417bad22013-06-07 14:08:30 +0200852 case 'O': case 0322: case 0323: case 0324: case 0325:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200853 case 0326: case 0330:
854 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
855 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
856 EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324);
857 EMIT2(0325); EMIT2(0326); EMIT2(0330);
858 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
859 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
860 EMITMBC(0x1ec) EMITMBC(0x1ece)
861 return OK;
862
863 case 'P': case 0x1e54: case 0x1e56:
864 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
865 return OK;
866
867 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
868 CASEMBC(0x1e58) CASEMBC(0x1e5e)
869 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
870 EMITMBC(0x1e58) EMITMBC(0x1e5e)
871 return OK;
872
873 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
874 CASEMBC(0x160) CASEMBC(0x1e60)
875 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
876 EMITMBC(0x160) EMITMBC(0x1e60)
877 return OK;
878
879 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
880 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
881 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
882 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200883 return OK;
884
Bram Moolenaar417bad22013-06-07 14:08:30 +0200885 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200886 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
887 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
888 CASEMBC(0x1ee6)
889 EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333);
890 EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a)
891 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
892 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
893 EMITMBC(0x1ee6)
894 return OK;
895
896 case 'V': CASEMBC(0x1e7c)
897 EMIT2('V'); EMITMBC(0x1e7c)
898 return OK;
899
900 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
901 CASEMBC(0x1e84) CASEMBC(0x1e86)
902 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
903 EMITMBC(0x1e84) EMITMBC(0x1e86)
904 return OK;
905
906 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
907 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200908 return OK;
909
Bram Moolenaar417bad22013-06-07 14:08:30 +0200910 case 'Y': case 0335:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200911 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
912 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
913 EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178)
914 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
915 EMITMBC(0x1ef8)
916 return OK;
917
918 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
919 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
920 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
921 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200922 return OK;
923
Bram Moolenaar417bad22013-06-07 14:08:30 +0200924 case 'a': case 0340: case 0341: case 0342:
925 case 0343: case 0344: case 0345:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200926 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
927 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
928 EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342);
929 EMIT2(0343); EMIT2(0344); EMIT2(0345);
930 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
931 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
932 EMITMBC(0x1ea3)
933 return OK;
934
935 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
936 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200937 return OK;
938
Bram Moolenaar417bad22013-06-07 14:08:30 +0200939 case 'c': case 0347:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200940 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
941 EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109)
942 EMITMBC(0x10b) EMITMBC(0x10d)
943 return OK;
944
945 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
946 CASEMBC(0x1e11)
947 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) EMITMBC(0x1e0b)
948 EMITMBC(0x01e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200949 return OK;
950
Bram Moolenaar417bad22013-06-07 14:08:30 +0200951 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200952 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
953 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
954 EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352);
955 EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115)
956 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
957 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
958 return OK;
959
960 case 'f': CASEMBC(0x1e1f)
961 EMIT2('f'); EMITMBC(0x1e1f)
962 return OK;
963
964 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
965 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
966 CASEMBC(0x1e21)
967 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
968 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
969 EMITMBC(0x1f5) EMITMBC(0x1e21)
970 return OK;
971
972 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
973 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
974 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
975 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200976 return OK;
977
Bram Moolenaar417bad22013-06-07 14:08:30 +0200978 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200979 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
980 CASEMBC(0x1d0) CASEMBC(0x1ec9)
981 EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356);
982 EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b)
983 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
984 EMITMBC(0x1ec9)
985 return OK;
986
987 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
988 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
989 return OK;
990
991 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
992 CASEMBC(0x1e35)
993 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
994 EMITMBC(0x1e35)
995 return OK;
996
997 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
998 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
999 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1000 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1001 return OK;
1002
1003 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1004 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001005 return OK;
1006
Bram Moolenaar417bad22013-06-07 14:08:30 +02001007 case 'n': case 0361:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001008 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1009 CASEMBC(0x1e45) CASEMBC(0x1e49)
1010 EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146)
1011 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1012 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001013 return OK;
1014
Bram Moolenaar417bad22013-06-07 14:08:30 +02001015 case 'o': case 0362: case 0363: case 0364: case 0365:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001016 case 0366: case 0370:
1017 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1018 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
1019 EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364);
1020 EMIT2(0365); EMIT2(0366); EMIT2(0370);
1021 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1022 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1023 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1024 return OK;
1025
1026 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1027 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1028 return OK;
1029
1030 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1031 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1032 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1033 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1034 return OK;
1035
1036 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1037 CASEMBC(0x161) CASEMBC(0x1e61)
1038 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1039 EMITMBC(0x161) EMITMBC(0x1e61)
1040 return OK;
1041
1042 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1043 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1044 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1045 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001046 return OK;
1047
Bram Moolenaar417bad22013-06-07 14:08:30 +02001048 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001049 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1050 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1051 CASEMBC(0x1ee7)
1052 EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373);
1053 EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b)
1054 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1055 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1056 EMITMBC(0x1ee7)
1057 return OK;
1058
1059 case 'v': CASEMBC(0x1e7d)
1060 EMIT2('v'); EMITMBC(0x1e7d)
1061 return OK;
1062
1063 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1064 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1065 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1066 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1067 return OK;
1068
1069 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1070 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001071 return OK;
1072
Bram Moolenaar417bad22013-06-07 14:08:30 +02001073 case 'y': case 0375: case 0377:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001074 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1075 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1076 EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177)
1077 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1078 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001079 return OK;
1080
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001081 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1082 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1083 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1084 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1085 return OK;
1086
1087 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001088 }
1089 }
1090
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001091 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001092 return OK;
1093#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001094#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001095}
1096
1097/*
1098 * Code to parse regular expression.
1099 *
1100 * We try to reuse parsing functions in regexp.c to
1101 * minimize surprise and keep the syntax consistent.
1102 */
1103
1104/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001105 * Parse the lowest level.
1106 *
1107 * An atom can be one of a long list of items. Many atoms match one character
1108 * in the text. It is often an ordinary character or a character class.
1109 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1110 * is only for syntax highlighting.
1111 *
1112 * atom ::= ordinary-atom
1113 * or \( pattern \)
1114 * or \%( pattern \)
1115 * or \z( pattern \)
1116 */
1117 static int
1118nfa_regatom()
1119{
1120 int c;
1121 int charclass;
1122 int equiclass;
1123 int collclass;
1124 int got_coll_char;
1125 char_u *p;
1126 char_u *endp;
1127#ifdef FEAT_MBYTE
1128 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001129#endif
1130 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001131 int emit_range;
1132 int negated;
1133 int result;
1134 int startc = -1;
1135 int endc = -1;
1136 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001137
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001138 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139 switch (c)
1140 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001141 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001142 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001143
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001144 case Magic('^'):
1145 EMIT(NFA_BOL);
1146 break;
1147
1148 case Magic('$'):
1149 EMIT(NFA_EOL);
1150#if defined(FEAT_SYN_HL) || defined(PROTO)
1151 had_eol = TRUE;
1152#endif
1153 break;
1154
1155 case Magic('<'):
1156 EMIT(NFA_BOW);
1157 break;
1158
1159 case Magic('>'):
1160 EMIT(NFA_EOW);
1161 break;
1162
1163 case Magic('_'):
1164 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001165 if (c == NUL)
1166 EMSG_RET_FAIL(_(e_nul_found));
1167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001168 if (c == '^') /* "\_^" is start-of-line */
1169 {
1170 EMIT(NFA_BOL);
1171 break;
1172 }
1173 if (c == '$') /* "\_$" is end-of-line */
1174 {
1175 EMIT(NFA_EOL);
1176#if defined(FEAT_SYN_HL) || defined(PROTO)
1177 had_eol = TRUE;
1178#endif
1179 break;
1180 }
1181
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001182 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001183
1184 /* "\_[" is collection plus newline */
1185 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001186 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001187
1188 /* "\_x" is character class plus newline */
1189 /*FALLTHROUGH*/
1190
1191 /*
1192 * Character classes.
1193 */
1194 case Magic('.'):
1195 case Magic('i'):
1196 case Magic('I'):
1197 case Magic('k'):
1198 case Magic('K'):
1199 case Magic('f'):
1200 case Magic('F'):
1201 case Magic('p'):
1202 case Magic('P'):
1203 case Magic('s'):
1204 case Magic('S'):
1205 case Magic('d'):
1206 case Magic('D'):
1207 case Magic('x'):
1208 case Magic('X'):
1209 case Magic('o'):
1210 case Magic('O'):
1211 case Magic('w'):
1212 case Magic('W'):
1213 case Magic('h'):
1214 case Magic('H'):
1215 case Magic('a'):
1216 case Magic('A'):
1217 case Magic('l'):
1218 case Magic('L'):
1219 case Magic('u'):
1220 case Magic('U'):
1221 p = vim_strchr(classchars, no_Magic(c));
1222 if (p == NULL)
1223 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001224 if (extra == NFA_ADD_NL)
1225 {
1226 EMSGN(_(e_ill_char_class), c);
1227 rc_did_emsg = TRUE;
1228 return FAIL;
1229 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001230 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1231 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001232 }
1233#ifdef FEAT_MBYTE
1234 /* When '.' is followed by a composing char ignore the dot, so that
1235 * the composing char is matched here. */
1236 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1237 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001238 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001239 c = getchr();
1240 goto nfa_do_multibyte;
1241 }
1242#endif
1243 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001244 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001245 {
1246 EMIT(NFA_NEWL);
1247 EMIT(NFA_OR);
1248 regflags |= RF_HASNL;
1249 }
1250 break;
1251
1252 case Magic('n'):
1253 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001254 /* In a string "\n" matches a newline character. */
1255 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001256 else
1257 {
1258 /* In buffer text "\n" matches the end of a line. */
1259 EMIT(NFA_NEWL);
1260 regflags |= RF_HASNL;
1261 }
1262 break;
1263
1264 case Magic('('):
1265 if (nfa_reg(REG_PAREN) == FAIL)
1266 return FAIL; /* cascaded error */
1267 break;
1268
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269 case Magic('|'):
1270 case Magic('&'):
1271 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001272 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001273 return FAIL;
1274
1275 case Magic('='):
1276 case Magic('?'):
1277 case Magic('+'):
1278 case Magic('@'):
1279 case Magic('*'):
1280 case Magic('{'):
1281 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001282 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001283 return FAIL;
1284
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001285 case Magic('~'):
1286 {
1287 char_u *lp;
1288
1289 /* Previous substitute pattern.
1290 * Generated as "\%(pattern\)". */
1291 if (reg_prev_sub == NULL)
1292 {
1293 EMSG(_(e_nopresub));
1294 return FAIL;
1295 }
1296 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1297 {
1298 EMIT(PTR2CHAR(lp));
1299 if (lp != reg_prev_sub)
1300 EMIT(NFA_CONCAT);
1301 }
1302 EMIT(NFA_NOPEN);
1303 break;
1304 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001305
Bram Moolenaar428e9872013-05-30 17:05:39 +02001306 case Magic('1'):
1307 case Magic('2'):
1308 case Magic('3'):
1309 case Magic('4'):
1310 case Magic('5'):
1311 case Magic('6'):
1312 case Magic('7'):
1313 case Magic('8'):
1314 case Magic('9'):
1315 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1316 nfa_has_backref = TRUE;
1317 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318
1319 case Magic('z'):
1320 c = no_Magic(getchr());
1321 switch (c)
1322 {
1323 case 's':
1324 EMIT(NFA_ZSTART);
1325 break;
1326 case 'e':
1327 EMIT(NFA_ZEND);
1328 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001329 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001330#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001331 case '1':
1332 case '2':
1333 case '3':
1334 case '4':
1335 case '5':
1336 case '6':
1337 case '7':
1338 case '8':
1339 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001340 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001341 if (reg_do_extmatch != REX_USE)
1342 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001343 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1344 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001345 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001346 re_has_z = REX_USE;
1347 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001348 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001349 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001350 if (reg_do_extmatch != REX_SET)
1351 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001352 if (nfa_reg(REG_ZPAREN) == FAIL)
1353 return FAIL; /* cascaded error */
1354 re_has_z = REX_SET;
1355 break;
1356#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001357 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001358 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001359 no_Magic(c));
1360 return FAIL;
1361 }
1362 break;
1363
1364 case Magic('%'):
1365 c = no_Magic(getchr());
1366 switch (c)
1367 {
1368 /* () without a back reference */
1369 case '(':
1370 if (nfa_reg(REG_NPAREN) == FAIL)
1371 return FAIL;
1372 EMIT(NFA_NOPEN);
1373 break;
1374
1375 case 'd': /* %d123 decimal */
1376 case 'o': /* %o123 octal */
1377 case 'x': /* %xab hex 2 */
1378 case 'u': /* %uabcd hex 4 */
1379 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001380 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001381 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001382
Bram Moolenaar47196582013-05-25 22:04:23 +02001383 switch (c)
1384 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001385 case 'd': nr = getdecchrs(); break;
1386 case 'o': nr = getoctchrs(); break;
1387 case 'x': nr = gethexchrs(2); break;
1388 case 'u': nr = gethexchrs(4); break;
1389 case 'U': nr = gethexchrs(8); break;
1390 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001391 }
1392
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001393 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001394 EMSG2_RET_FAIL(
1395 _("E678: Invalid character after %s%%[dxouU]"),
1396 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001397 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001398 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001399 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001400 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001401 break;
1402
1403 /* Catch \%^ and \%$ regardless of where they appear in the
1404 * pattern -- regardless of whether or not it makes sense. */
1405 case '^':
1406 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001407 break;
1408
1409 case '$':
1410 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001411 break;
1412
1413 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001414 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001415 break;
1416
1417 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001418 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001419 break;
1420
1421 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001422 {
1423 int n;
1424
1425 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001426 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001427 {
1428 if (c == NUL)
1429 EMSG2_RET_FAIL(_(e_missing_sb),
1430 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001431 /* recursive call! */
1432 if (nfa_regatom() == FAIL)
1433 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001434 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001435 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001436 if (n == 0)
1437 EMSG2_RET_FAIL(_(e_empty_sb),
1438 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001439 EMIT(NFA_OPT_CHARS);
1440 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001441
1442 /* Emit as "\%(\%[abc]\)" to be able to handle
1443 * "\%[abc]*" which would cause the empty string to be
1444 * matched an unlimited number of times. NFA_NOPEN is
1445 * added only once at a position, while NFA_SPLIT is
1446 * added multiple times. This is more efficient than
1447 * not allowsing NFA_SPLIT multiple times, it is used
1448 * a lot. */
1449 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001450 break;
1451 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001452
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001453 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001454 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001455 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001456 int cmp = c;
1457
1458 if (c == '<' || c == '>')
1459 c = getchr();
1460 while (VIM_ISDIGIT(c))
1461 {
1462 n = n * 10 + (c - '0');
1463 c = getchr();
1464 }
1465 if (c == 'l' || c == 'c' || c == 'v')
1466 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001467 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001468 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001469 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001470 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001471 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001472 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001473 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001474 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001475 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001476 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001477 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001478 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001479 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001480 break;
1481 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001482 else if (c == '\'' && n == 0)
1483 {
1484 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001485 EMIT(cmp == '<' ? NFA_MARK_LT :
1486 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001487 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001488 break;
1489 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001490 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001491 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1492 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001493 return FAIL;
1494 }
1495 break;
1496
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001498collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001500 * [abc] uses NFA_START_COLL - NFA_END_COLL
1501 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1502 * Each character is produced as a regular state, using
1503 * NFA_CONCAT to bind them together.
1504 * Besides normal characters there can be:
1505 * - character classes NFA_CLASS_*
1506 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001507 */
1508
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001509 p = regparse;
1510 endp = skip_anyof(p);
1511 if (*endp == ']')
1512 {
1513 /*
1514 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001515 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516 * and perform the necessary substitutions in the NFA.
1517 */
1518 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001519 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001520 if (result != FAIL)
1521 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001522 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001523 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001524 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001525 EMIT(NFA_NEWL);
1526 EMIT(NFA_OR);
1527 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001528 else
1529 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001531 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 return OK;
1533 }
1534 /*
1535 * Failed to recognize a character class. Use the simple
1536 * version that turns [abc] into 'a' OR 'b' OR 'c'
1537 */
1538 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001540 if (*regparse == '^') /* negated range */
1541 {
1542 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001543 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001544 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001546 else
1547 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001548 if (*regparse == '-')
1549 {
1550 startc = '-';
1551 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001552 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001553 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001554 }
1555 /* Emit the OR branches for each character in the [] */
1556 emit_range = FALSE;
1557 while (regparse < endp)
1558 {
1559 oldstartc = startc;
1560 startc = -1;
1561 got_coll_char = FALSE;
1562 if (*regparse == '[')
1563 {
1564 /* Check for [: :], [= =], [. .] */
1565 equiclass = collclass = 0;
1566 charclass = get_char_class(&regparse);
1567 if (charclass == CLASS_NONE)
1568 {
1569 equiclass = get_equi_class(&regparse);
1570 if (equiclass == 0)
1571 collclass = get_coll_element(&regparse);
1572 }
1573
1574 /* Character class like [:alpha:] */
1575 if (charclass != CLASS_NONE)
1576 {
1577 switch (charclass)
1578 {
1579 case CLASS_ALNUM:
1580 EMIT(NFA_CLASS_ALNUM);
1581 break;
1582 case CLASS_ALPHA:
1583 EMIT(NFA_CLASS_ALPHA);
1584 break;
1585 case CLASS_BLANK:
1586 EMIT(NFA_CLASS_BLANK);
1587 break;
1588 case CLASS_CNTRL:
1589 EMIT(NFA_CLASS_CNTRL);
1590 break;
1591 case CLASS_DIGIT:
1592 EMIT(NFA_CLASS_DIGIT);
1593 break;
1594 case CLASS_GRAPH:
1595 EMIT(NFA_CLASS_GRAPH);
1596 break;
1597 case CLASS_LOWER:
1598 EMIT(NFA_CLASS_LOWER);
1599 break;
1600 case CLASS_PRINT:
1601 EMIT(NFA_CLASS_PRINT);
1602 break;
1603 case CLASS_PUNCT:
1604 EMIT(NFA_CLASS_PUNCT);
1605 break;
1606 case CLASS_SPACE:
1607 EMIT(NFA_CLASS_SPACE);
1608 break;
1609 case CLASS_UPPER:
1610 EMIT(NFA_CLASS_UPPER);
1611 break;
1612 case CLASS_XDIGIT:
1613 EMIT(NFA_CLASS_XDIGIT);
1614 break;
1615 case CLASS_TAB:
1616 EMIT(NFA_CLASS_TAB);
1617 break;
1618 case CLASS_RETURN:
1619 EMIT(NFA_CLASS_RETURN);
1620 break;
1621 case CLASS_BACKSPACE:
1622 EMIT(NFA_CLASS_BACKSPACE);
1623 break;
1624 case CLASS_ESCAPE:
1625 EMIT(NFA_CLASS_ESCAPE);
1626 break;
1627 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001628 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001629 continue;
1630 }
1631 /* Try equivalence class [=a=] and the like */
1632 if (equiclass != 0)
1633 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001634 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001635 if (result == FAIL)
1636 {
1637 /* should never happen */
1638 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1639 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001640 continue;
1641 }
1642 /* Try collating class like [. .] */
1643 if (collclass != 0)
1644 {
1645 startc = collclass; /* allow [.a.]-x as a range */
1646 /* Will emit the proper atom at the end of the
1647 * while loop. */
1648 }
1649 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001650 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1651 * start character. */
1652 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001653 {
1654 emit_range = TRUE;
1655 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001656 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001657 continue; /* reading the end of the range */
1658 }
1659
1660 /* Now handle simple and escaped characters.
1661 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1662 * accepts "\t", "\e", etc., but only when the 'l' flag in
1663 * 'cpoptions' is not included.
1664 * Posix doesn't recognize backslash at all.
1665 */
1666 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001667 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 && regparse + 1 <= endp
1669 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001670 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001671 && vim_strchr(REGEXP_ABBR, regparse[1])
1672 != NULL)
1673 )
1674 )
1675 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001676 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001678 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001679 startc = reg_string ? NL : NFA_NEWL;
1680 else
1681 if (*regparse == 'd'
1682 || *regparse == 'o'
1683 || *regparse == 'x'
1684 || *regparse == 'u'
1685 || *regparse == 'U'
1686 )
1687 {
1688 /* TODO(RE) This needs more testing */
1689 startc = coll_get_char();
1690 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001691 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001692 }
1693 else
1694 {
1695 /* \r,\t,\e,\b */
1696 startc = backslash_trans(*regparse);
1697 }
1698 }
1699
1700 /* Normal printable char */
1701 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001702 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001703
1704 /* Previous char was '-', so this char is end of range. */
1705 if (emit_range)
1706 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001707 endc = startc;
1708 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001709 if (startc > endc)
1710 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001711
1712 if (endc > startc + 2)
1713 {
1714 /* Emit a range instead of the sequence of
1715 * individual characters. */
1716 if (startc == 0)
1717 /* \x00 is translated to \x0a, start at \x01. */
1718 EMIT(1);
1719 else
1720 --post_ptr; /* remove NFA_CONCAT */
1721 EMIT(endc);
1722 EMIT(NFA_RANGE);
1723 EMIT(NFA_CONCAT);
1724 }
1725 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001726#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001727 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001728 || (*mb_char2len)(endc) > 1))
1729 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001730 /* Emit the characters in the range.
1731 * "startc" was already emitted, so skip it.
1732 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001733 for (c = startc + 1; c <= endc; c++)
1734 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001735 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001736 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001737 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 }
1739 else
1740#endif
1741 {
1742#ifdef EBCDIC
1743 int alpha_only = FALSE;
1744
1745 /* for alphabetical range skip the gaps
1746 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1747 if (isalpha(startc) && isalpha(endc))
1748 alpha_only = TRUE;
1749#endif
1750 /* Emit the range. "startc" was already emitted, so
1751 * skip it. */
1752 for (c = startc + 1; c <= endc; c++)
1753#ifdef EBCDIC
1754 if (!alpha_only || isalpha(startc))
1755#endif
1756 {
1757 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001758 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001759 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001760 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001761 emit_range = FALSE;
1762 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001763 }
1764 else
1765 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001766 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001767 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001768 * Normally, simply emit startc. But if we get char
1769 * code=0 from a collating char, then replace it with
1770 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001771 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001772 * the backtracking engine. */
1773 if (startc == NFA_NEWL)
1774 {
1775 /* Line break can't be matched as part of the
1776 * collection, add an OR below. But not for negated
1777 * range. */
1778 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001779 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001780 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001781 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001782 {
1783 if (got_coll_char == TRUE && startc == 0)
1784 EMIT(0x0a);
1785 else
1786 EMIT(startc);
1787 EMIT(NFA_CONCAT);
1788 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001789 }
1790
Bram Moolenaar51a29832013-05-28 22:30:35 +02001791 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001792 } /* while (p < endp) */
1793
Bram Moolenaar51a29832013-05-28 22:30:35 +02001794 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001795 if (*regparse == '-') /* if last, '-' is just a char */
1796 {
1797 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001798 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001799 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 /* skip the trailing ] */
1802 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001803 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001804
1805 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001806 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001807 EMIT(NFA_END_NEG_COLL);
1808 else
1809 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001810
1811 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001812 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001813 {
1814 EMIT(reg_string ? NL : NFA_NEWL);
1815 EMIT(NFA_OR);
1816 }
1817
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001818 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001819 } /* if exists closing ] */
1820
1821 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001822 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001823 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001824
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001825 default:
1826 {
1827#ifdef FEAT_MBYTE
1828 int plen;
1829
1830nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001831 /* plen is length of current char with composing chars */
1832 if (enc_utf8 && ((*mb_char2len)(c)
1833 != (plen = (*mb_ptr2len)(old_regparse))
1834 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001835 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001836 int i = 0;
1837
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001838 /* A base character plus composing characters, or just one
1839 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001840 * This requires creating a separate atom as if enclosing
1841 * the characters in (), where NFA_COMPOSING is the ( and
1842 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001843 * building the postfix form, not the NFA itself;
1844 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001845 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001846 for (;;)
1847 {
1848 EMIT(c);
1849 if (i > 0)
1850 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001851 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001852 break;
1853 c = utf_ptr2char(old_regparse + i);
1854 }
1855 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001856 regparse = old_regparse + plen;
1857 }
1858 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001859#endif
1860 {
1861 c = no_Magic(c);
1862 EMIT(c);
1863 }
1864 return OK;
1865 }
1866 }
1867
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 return OK;
1869}
1870
1871/*
1872 * Parse something followed by possible [*+=].
1873 *
1874 * A piece is an atom, possibly followed by a multi, an indication of how many
1875 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1876 * characters: "", "a", "aa", etc.
1877 *
1878 * piece ::= atom
1879 * or atom multi
1880 */
1881 static int
1882nfa_regpiece()
1883{
1884 int i;
1885 int op;
1886 int ret;
1887 long minval, maxval;
1888 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001889 parse_state_T old_state;
1890 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001891 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001892 int old_post_pos;
1893 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001894 int quest;
1895
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001896 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1897 * next. */
1898 save_parse_state(&old_state);
1899
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001901 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902
1903 ret = nfa_regatom();
1904 if (ret == FAIL)
1905 return FAIL; /* cascaded error */
1906
1907 op = peekchr();
1908 if (re_multi_type(op) == NOT_MULTI)
1909 return OK;
1910
1911 skipchr();
1912 switch (op)
1913 {
1914 case Magic('*'):
1915 EMIT(NFA_STAR);
1916 break;
1917
1918 case Magic('+'):
1919 /*
1920 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1921 * first and only submatch would be "aaa". But the backtracking
1922 * engine interprets the plus as "try matching one more time", and
1923 * a* matches a second time at the end of the input, the empty
1924 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001925 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001926 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001927 * In order to be consistent with the old engine, we replace
1928 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001929 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001930 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 curchr = -1;
1932 if (nfa_regatom() == FAIL)
1933 return FAIL;
1934 EMIT(NFA_STAR);
1935 EMIT(NFA_CONCAT);
1936 skipchr(); /* skip the \+ */
1937 break;
1938
1939 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001940 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001941 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001942 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001943 switch(op)
1944 {
1945 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001946 /* \@= */
1947 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001949 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001950 /* \@! */
1951 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001952 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001954 op = no_Magic(getchr());
1955 if (op == '=')
1956 /* \@<= */
1957 i = NFA_PREV_ATOM_JUST_BEFORE;
1958 else if (op == '!')
1959 /* \@<! */
1960 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1961 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001963 /* \@> */
1964 i = NFA_PREV_ATOM_LIKE_PATTERN;
1965 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001966 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001967 if (i == 0)
1968 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001969 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1970 return FAIL;
1971 }
1972 EMIT(i);
1973 if (i == NFA_PREV_ATOM_JUST_BEFORE
1974 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1975 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001976 break;
1977
1978 case Magic('?'):
1979 case Magic('='):
1980 EMIT(NFA_QUEST);
1981 break;
1982
1983 case Magic('{'):
1984 /* a{2,5} will expand to 'aaa?a?a?'
1985 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1986 * version of '?'
1987 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1988 * parenthesis have the same id
1989 */
1990
1991 greedy = TRUE;
1992 c2 = peekchr();
1993 if (c2 == '-' || c2 == Magic('-'))
1994 {
1995 skipchr();
1996 greedy = FALSE;
1997 }
1998 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001999 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002000
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002001 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2002 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002003 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002004 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002005 if (greedy)
2006 /* \{}, \{0,} */
2007 EMIT(NFA_STAR);
2008 else
2009 /* \{-}, \{-0,} */
2010 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011 break;
2012 }
2013
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002014 /* Special case: x{0} or x{-0} */
2015 if (maxval == 0)
2016 {
2017 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002018 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002019 /* NFA_EMPTY is 0-length and works everywhere */
2020 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002021 return OK;
2022 }
2023
2024 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002025 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002026 /* Save parse state after the repeated atom and the \{} */
2027 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002028
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002029 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2030 for (i = 0; i < maxval; i++)
2031 {
2032 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002033 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002034 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002035 if (nfa_regatom() == FAIL)
2036 return FAIL;
2037 /* after "minval" times, atoms are optional */
2038 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002039 {
2040 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002041 {
2042 if (greedy)
2043 EMIT(NFA_STAR);
2044 else
2045 EMIT(NFA_STAR_NONGREEDY);
2046 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002047 else
2048 EMIT(quest);
2049 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002050 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002051 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002052 if (i + 1 > minval && maxval == MAX_LIMIT)
2053 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002054 }
2055
2056 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002057 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 curchr = -1;
2059
2060 break;
2061
2062
2063 default:
2064 break;
2065 } /* end switch */
2066
2067 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002068 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002069 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002070
2071 return OK;
2072}
2073
2074/*
2075 * Parse one or more pieces, concatenated. It matches a match for the
2076 * first piece, followed by a match for the second piece, etc. Example:
2077 * "f[0-9]b", first matches "f", then a digit and then "b".
2078 *
2079 * concat ::= piece
2080 * or piece piece
2081 * or piece piece piece
2082 * etc.
2083 */
2084 static int
2085nfa_regconcat()
2086{
2087 int cont = TRUE;
2088 int first = TRUE;
2089
2090 while (cont)
2091 {
2092 switch (peekchr())
2093 {
2094 case NUL:
2095 case Magic('|'):
2096 case Magic('&'):
2097 case Magic(')'):
2098 cont = FALSE;
2099 break;
2100
2101 case Magic('Z'):
2102#ifdef FEAT_MBYTE
2103 regflags |= RF_ICOMBINE;
2104#endif
2105 skipchr_keepstart();
2106 break;
2107 case Magic('c'):
2108 regflags |= RF_ICASE;
2109 skipchr_keepstart();
2110 break;
2111 case Magic('C'):
2112 regflags |= RF_NOICASE;
2113 skipchr_keepstart();
2114 break;
2115 case Magic('v'):
2116 reg_magic = MAGIC_ALL;
2117 skipchr_keepstart();
2118 curchr = -1;
2119 break;
2120 case Magic('m'):
2121 reg_magic = MAGIC_ON;
2122 skipchr_keepstart();
2123 curchr = -1;
2124 break;
2125 case Magic('M'):
2126 reg_magic = MAGIC_OFF;
2127 skipchr_keepstart();
2128 curchr = -1;
2129 break;
2130 case Magic('V'):
2131 reg_magic = MAGIC_NONE;
2132 skipchr_keepstart();
2133 curchr = -1;
2134 break;
2135
2136 default:
2137 if (nfa_regpiece() == FAIL)
2138 return FAIL;
2139 if (first == FALSE)
2140 EMIT(NFA_CONCAT);
2141 else
2142 first = FALSE;
2143 break;
2144 }
2145 }
2146
2147 return OK;
2148}
2149
2150/*
2151 * Parse a branch, one or more concats, separated by "\&". It matches the
2152 * last concat, but only if all the preceding concats also match at the same
2153 * position. Examples:
2154 * "foobeep\&..." matches "foo" in "foobeep".
2155 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2156 *
2157 * branch ::= concat
2158 * or concat \& concat
2159 * or concat \& concat \& concat
2160 * etc.
2161 */
2162 static int
2163nfa_regbranch()
2164{
2165 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002166 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002167
Bram Moolenaar16299b52013-05-30 18:45:23 +02002168 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002169
2170 /* First branch, possibly the only one */
2171 if (nfa_regconcat() == FAIL)
2172 return FAIL;
2173
2174 ch = peekchr();
2175 /* Try next concats */
2176 while (ch == Magic('&'))
2177 {
2178 skipchr();
2179 EMIT(NFA_NOPEN);
2180 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002181 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002182 if (nfa_regconcat() == FAIL)
2183 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002184 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002185 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002186 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187 EMIT(NFA_CONCAT);
2188 ch = peekchr();
2189 }
2190
Bram Moolenaar699c1202013-09-25 16:41:54 +02002191 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002192 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002193 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002194
2195 return OK;
2196}
2197
2198/*
2199 * Parse a pattern, one or more branches, separated by "\|". It matches
2200 * anything that matches one of the branches. Example: "foo\|beep" matches
2201 * "foo" and matches "beep". If more than one branch matches, the first one
2202 * is used.
2203 *
2204 * pattern ::= branch
2205 * or branch \| branch
2206 * or branch \| branch \| branch
2207 * etc.
2208 */
2209 static int
2210nfa_reg(paren)
2211 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
2212{
2213 int parno = 0;
2214
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002215 if (paren == REG_PAREN)
2216 {
2217 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002218 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002219 parno = regnpar++;
2220 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002221#ifdef FEAT_SYN_HL
2222 else if (paren == REG_ZPAREN)
2223 {
2224 /* Make a ZOPEN node. */
2225 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002226 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002227 parno = regnzpar++;
2228 }
2229#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002230
2231 if (nfa_regbranch() == FAIL)
2232 return FAIL; /* cascaded error */
2233
2234 while (peekchr() == Magic('|'))
2235 {
2236 skipchr();
2237 if (nfa_regbranch() == FAIL)
2238 return FAIL; /* cascaded error */
2239 EMIT(NFA_OR);
2240 }
2241
2242 /* Check for proper termination. */
2243 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2244 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002245 if (paren == REG_NPAREN)
2246 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2247 else
2248 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2249 }
2250 else if (paren == REG_NOPAREN && peekchr() != NUL)
2251 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002252 if (peekchr() == Magic(')'))
2253 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2254 else
2255 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2256 }
2257 /*
2258 * Here we set the flag allowing back references to this set of
2259 * parentheses.
2260 */
2261 if (paren == REG_PAREN)
2262 {
2263 had_endbrace[parno] = TRUE; /* have seen the close paren */
2264 EMIT(NFA_MOPEN + parno);
2265 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002266#ifdef FEAT_SYN_HL
2267 else if (paren == REG_ZPAREN)
2268 EMIT(NFA_ZOPEN + parno);
2269#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002270
2271 return OK;
2272}
2273
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274#ifdef DEBUG
2275static char_u code[50];
2276
2277 static void
2278nfa_set_code(c)
2279 int c;
2280{
2281 int addnl = FALSE;
2282
2283 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2284 {
2285 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002286 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287 }
2288
2289 STRCPY(code, "");
2290 switch (c)
2291 {
2292 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2293 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2294 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2295 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2296 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2297 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2298
Bram Moolenaar5714b802013-05-28 22:03:20 +02002299 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2300 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2301 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2302 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2303 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2304 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2305 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2306 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2307 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002308#ifdef FEAT_SYN_HL
2309 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2310 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2311 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2312 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2313 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2314 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2315 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2316 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2317 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2318#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002319 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2320
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002321 case NFA_PREV_ATOM_NO_WIDTH:
2322 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002323 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2324 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002325 case NFA_PREV_ATOM_JUST_BEFORE:
2326 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2327 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2328 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002329 case NFA_PREV_ATOM_LIKE_PATTERN:
2330 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2331
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002332 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2333 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002334 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002335 case NFA_START_INVISIBLE_FIRST:
2336 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002337 case NFA_START_INVISIBLE_NEG:
2338 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002339 case NFA_START_INVISIBLE_NEG_FIRST:
2340 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002341 case NFA_START_INVISIBLE_BEFORE:
2342 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002343 case NFA_START_INVISIBLE_BEFORE_FIRST:
2344 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002345 case NFA_START_INVISIBLE_BEFORE_NEG:
2346 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002347 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2348 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002349 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002350 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002351 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002352 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002354 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2355 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002356 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002357
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002358 case NFA_MOPEN:
2359 case NFA_MOPEN1:
2360 case NFA_MOPEN2:
2361 case NFA_MOPEN3:
2362 case NFA_MOPEN4:
2363 case NFA_MOPEN5:
2364 case NFA_MOPEN6:
2365 case NFA_MOPEN7:
2366 case NFA_MOPEN8:
2367 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002368 STRCPY(code, "NFA_MOPEN(x)");
2369 code[10] = c - NFA_MOPEN + '0';
2370 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002371 case NFA_MCLOSE:
2372 case NFA_MCLOSE1:
2373 case NFA_MCLOSE2:
2374 case NFA_MCLOSE3:
2375 case NFA_MCLOSE4:
2376 case NFA_MCLOSE5:
2377 case NFA_MCLOSE6:
2378 case NFA_MCLOSE7:
2379 case NFA_MCLOSE8:
2380 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002381 STRCPY(code, "NFA_MCLOSE(x)");
2382 code[11] = c - NFA_MCLOSE + '0';
2383 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002384#ifdef FEAT_SYN_HL
2385 case NFA_ZOPEN:
2386 case NFA_ZOPEN1:
2387 case NFA_ZOPEN2:
2388 case NFA_ZOPEN3:
2389 case NFA_ZOPEN4:
2390 case NFA_ZOPEN5:
2391 case NFA_ZOPEN6:
2392 case NFA_ZOPEN7:
2393 case NFA_ZOPEN8:
2394 case NFA_ZOPEN9:
2395 STRCPY(code, "NFA_ZOPEN(x)");
2396 code[10] = c - NFA_ZOPEN + '0';
2397 break;
2398 case NFA_ZCLOSE:
2399 case NFA_ZCLOSE1:
2400 case NFA_ZCLOSE2:
2401 case NFA_ZCLOSE3:
2402 case NFA_ZCLOSE4:
2403 case NFA_ZCLOSE5:
2404 case NFA_ZCLOSE6:
2405 case NFA_ZCLOSE7:
2406 case NFA_ZCLOSE8:
2407 case NFA_ZCLOSE9:
2408 STRCPY(code, "NFA_ZCLOSE(x)");
2409 code[11] = c - NFA_ZCLOSE + '0';
2410 break;
2411#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002412 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2413 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2414 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2415 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002416 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2417 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002418 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2419 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2420 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2421 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2422 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2423 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2424 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2425 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2426 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2427 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2428 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2429 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2430 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2431 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2432
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002433 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002434 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2435 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2436 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002437 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002438 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002439
2440 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2441 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2442 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2443 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2444 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2445 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2446 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2447
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2449 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2450 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2451 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2452 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2453 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2454 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2455 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2456 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2457 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2458 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2459 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2460 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2461 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2462 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2463 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2464
2465 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2466 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2467 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2468 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2469 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2470 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2471 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2472 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2473 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2474 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2475 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2476 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2477 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2478 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2479 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2480 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2481 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2482 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2483 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2484 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2485 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2486 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2487 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2488 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2489 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2490 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2491 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002492 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2493 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2494 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2495 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002496
2497 default:
2498 STRCPY(code, "CHAR(x)");
2499 code[5] = c;
2500 }
2501
2502 if (addnl == TRUE)
2503 STRCAT(code, " + NEWLINE ");
2504
2505}
2506
2507#ifdef ENABLE_LOG
2508static FILE *log_fd;
2509
2510/*
2511 * Print the postfix notation of the current regexp.
2512 */
2513 static void
2514nfa_postfix_dump(expr, retval)
2515 char_u *expr;
2516 int retval;
2517{
2518 int *p;
2519 FILE *f;
2520
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002521 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002522 if (f != NULL)
2523 {
2524 fprintf(f, "\n-------------------------\n");
2525 if (retval == FAIL)
2526 fprintf(f, ">>> NFA engine failed ... \n");
2527 else if (retval == OK)
2528 fprintf(f, ">>> NFA engine succeeded !\n");
2529 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002530 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002531 {
2532 nfa_set_code(*p);
2533 fprintf(f, "%s, ", code);
2534 }
2535 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002536 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002537 fprintf(f, "%d ", *p);
2538 fprintf(f, "\n\n");
2539 fclose(f);
2540 }
2541}
2542
2543/*
2544 * Print the NFA starting with a root node "state".
2545 */
2546 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002547nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002548 FILE *debugf;
2549 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002550{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002551 garray_T indent;
2552
2553 ga_init2(&indent, 1, 64);
2554 ga_append(&indent, '\0');
2555 nfa_print_state2(debugf, state, &indent);
2556 ga_clear(&indent);
2557}
2558
2559 static void
2560nfa_print_state2(debugf, state, indent)
2561 FILE *debugf;
2562 nfa_state_T *state;
2563 garray_T *indent;
2564{
2565 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002566
2567 if (state == NULL)
2568 return;
2569
2570 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002571
2572 /* Output indent */
2573 p = (char_u *)indent->ga_data;
2574 if (indent->ga_len >= 3)
2575 {
2576 int last = indent->ga_len - 3;
2577 char_u save[2];
2578
2579 STRNCPY(save, &p[last], 2);
2580 STRNCPY(&p[last], "+-", 2);
2581 fprintf(debugf, " %s", p);
2582 STRNCPY(&p[last], save, 2);
2583 }
2584 else
2585 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002586
2587 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002588 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002589 code,
2590 state->c,
2591 abs(state->id),
2592 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002593 if (state->id < 0)
2594 return;
2595
2596 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002597
2598 /* grow indent for state->out */
2599 indent->ga_len -= 1;
2600 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002601 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002602 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002603 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002604 ga_append(indent, '\0');
2605
2606 nfa_print_state2(debugf, state->out, indent);
2607
2608 /* replace last part of indent for state->out1 */
2609 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002610 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002611 ga_append(indent, '\0');
2612
2613 nfa_print_state2(debugf, state->out1, indent);
2614
2615 /* shrink indent */
2616 indent->ga_len -= 3;
2617 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002618}
2619
2620/*
2621 * Print the NFA state machine.
2622 */
2623 static void
2624nfa_dump(prog)
2625 nfa_regprog_T *prog;
2626{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002627 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002628
2629 if (debugf != NULL)
2630 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002631 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002632
Bram Moolenaar473de612013-06-08 18:19:48 +02002633 if (prog->reganch)
2634 fprintf(debugf, "reganch: %d\n", prog->reganch);
2635 if (prog->regstart != NUL)
2636 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2637 prog->regstart, prog->regstart);
2638 if (prog->match_text != NULL)
2639 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002640
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002641 fclose(debugf);
2642 }
2643}
2644#endif /* ENABLE_LOG */
2645#endif /* DEBUG */
2646
2647/*
2648 * Parse r.e. @expr and convert it into postfix form.
2649 * Return the postfix string on success, NULL otherwise.
2650 */
2651 static int *
2652re2post()
2653{
2654 if (nfa_reg(REG_NOPAREN) == FAIL)
2655 return NULL;
2656 EMIT(NFA_MOPEN);
2657 return post_start;
2658}
2659
2660/* NB. Some of the code below is inspired by Russ's. */
2661
2662/*
2663 * Represents an NFA state plus zero or one or two arrows exiting.
2664 * if c == MATCH, no arrows out; matching state.
2665 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2666 * If c < 256, labeled arrow with character c to out.
2667 */
2668
2669static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2670
2671/*
2672 * Allocate and initialize nfa_state_T.
2673 */
2674 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002675alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002676 int c;
2677 nfa_state_T *out;
2678 nfa_state_T *out1;
2679{
2680 nfa_state_T *s;
2681
2682 if (istate >= nstate)
2683 return NULL;
2684
2685 s = &state_ptr[istate++];
2686
2687 s->c = c;
2688 s->out = out;
2689 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002690 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002691
2692 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002693 s->lastlist[0] = 0;
2694 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002695
2696 return s;
2697}
2698
2699/*
2700 * A partially built NFA without the matching state filled in.
2701 * Frag_T.start points at the start state.
2702 * Frag_T.out is a list of places that need to be set to the
2703 * next state for this fragment.
2704 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002705
2706/* Since the out pointers in the list are always
2707 * uninitialized, we use the pointers themselves
2708 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002709typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002710union Ptrlist
2711{
2712 Ptrlist *next;
2713 nfa_state_T *s;
2714};
2715
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002716struct Frag
2717{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002718 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002719 Ptrlist *out;
2720};
2721typedef struct Frag Frag_T;
2722
2723static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2724static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2725static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2726static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2727static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2728static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2729
2730/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002731 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002732 */
2733 static Frag_T
2734frag(start, out)
2735 nfa_state_T *start;
2736 Ptrlist *out;
2737{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002738 Frag_T n;
2739
2740 n.start = start;
2741 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 return n;
2743}
2744
2745/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002746 * Create singleton list containing just outp.
2747 */
2748 static Ptrlist *
2749list1(outp)
2750 nfa_state_T **outp;
2751{
2752 Ptrlist *l;
2753
2754 l = (Ptrlist *)outp;
2755 l->next = NULL;
2756 return l;
2757}
2758
2759/*
2760 * Patch the list of states at out to point to start.
2761 */
2762 static void
2763patch(l, s)
2764 Ptrlist *l;
2765 nfa_state_T *s;
2766{
2767 Ptrlist *next;
2768
2769 for (; l; l = next)
2770 {
2771 next = l->next;
2772 l->s = s;
2773 }
2774}
2775
2776
2777/*
2778 * Join the two lists l1 and l2, returning the combination.
2779 */
2780 static Ptrlist *
2781append(l1, l2)
2782 Ptrlist *l1;
2783 Ptrlist *l2;
2784{
2785 Ptrlist *oldl1;
2786
2787 oldl1 = l1;
2788 while (l1->next)
2789 l1 = l1->next;
2790 l1->next = l2;
2791 return oldl1;
2792}
2793
2794/*
2795 * Stack used for transforming postfix form into NFA.
2796 */
2797static Frag_T empty;
2798
2799 static void
2800st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002801 int *postfix UNUSED;
2802 int *end UNUSED;
2803 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002804{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002805#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002806 FILE *df;
2807 int *p2;
2808
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002809 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002810 if (df)
2811 {
2812 fprintf(df, "Error popping the stack!\n");
2813#ifdef DEBUG
2814 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2815#endif
2816 fprintf(df, "Postfix form is: ");
2817#ifdef DEBUG
2818 for (p2 = postfix; p2 < end; p2++)
2819 {
2820 nfa_set_code(*p2);
2821 fprintf(df, "%s, ", code);
2822 }
2823 nfa_set_code(*p);
2824 fprintf(df, "\nCurrent position is: ");
2825 for (p2 = postfix; p2 <= p; p2 ++)
2826 {
2827 nfa_set_code(*p2);
2828 fprintf(df, "%s, ", code);
2829 }
2830#else
2831 for (p2 = postfix; p2 < end; p2++)
2832 {
2833 fprintf(df, "%d, ", *p2);
2834 }
2835 fprintf(df, "\nCurrent position is: ");
2836 for (p2 = postfix; p2 <= p; p2 ++)
2837 {
2838 fprintf(df, "%d, ", *p2);
2839 }
2840#endif
2841 fprintf(df, "\n--------------------------\n");
2842 fclose(df);
2843 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002844#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002845 EMSG(_("E874: (NFA) Could not pop the stack !"));
2846}
2847
2848/*
2849 * Push an item onto the stack.
2850 */
2851 static void
2852st_push(s, p, stack_end)
2853 Frag_T s;
2854 Frag_T **p;
2855 Frag_T *stack_end;
2856{
2857 Frag_T *stackp = *p;
2858
2859 if (stackp >= stack_end)
2860 return;
2861 *stackp = s;
2862 *p = *p + 1;
2863}
2864
2865/*
2866 * Pop an item from the stack.
2867 */
2868 static Frag_T
2869st_pop(p, stack)
2870 Frag_T **p;
2871 Frag_T *stack;
2872{
2873 Frag_T *stackp;
2874
2875 *p = *p - 1;
2876 stackp = *p;
2877 if (stackp < stack)
2878 return empty;
2879 return **p;
2880}
2881
2882/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002883 * Estimate the maximum byte length of anything matching "state".
2884 * When unknown or unlimited return -1.
2885 */
2886 static int
2887nfa_max_width(startstate, depth)
2888 nfa_state_T *startstate;
2889 int depth;
2890{
2891 int l, r;
2892 nfa_state_T *state = startstate;
2893 int len = 0;
2894
2895 /* detect looping in a NFA_SPLIT */
2896 if (depth > 4)
2897 return -1;
2898
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002899 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002900 {
2901 switch (state->c)
2902 {
2903 case NFA_END_INVISIBLE:
2904 case NFA_END_INVISIBLE_NEG:
2905 /* the end, return what we have */
2906 return len;
2907
2908 case NFA_SPLIT:
2909 /* two alternatives, use the maximum */
2910 l = nfa_max_width(state->out, depth + 1);
2911 r = nfa_max_width(state->out1, depth + 1);
2912 if (l < 0 || r < 0)
2913 return -1;
2914 return len + (l > r ? l : r);
2915
2916 case NFA_ANY:
2917 case NFA_START_COLL:
2918 case NFA_START_NEG_COLL:
2919 /* matches some character, including composing chars */
2920#ifdef FEAT_MBYTE
2921 if (enc_utf8)
2922 len += MB_MAXBYTES;
2923 else if (has_mbyte)
2924 len += 2;
2925 else
2926#endif
2927 ++len;
2928 if (state->c != NFA_ANY)
2929 {
2930 /* skip over the characters */
2931 state = state->out1->out;
2932 continue;
2933 }
2934 break;
2935
2936 case NFA_DIGIT:
2937 case NFA_WHITE:
2938 case NFA_HEX:
2939 case NFA_OCTAL:
2940 /* ascii */
2941 ++len;
2942 break;
2943
2944 case NFA_IDENT:
2945 case NFA_SIDENT:
2946 case NFA_KWORD:
2947 case NFA_SKWORD:
2948 case NFA_FNAME:
2949 case NFA_SFNAME:
2950 case NFA_PRINT:
2951 case NFA_SPRINT:
2952 case NFA_NWHITE:
2953 case NFA_NDIGIT:
2954 case NFA_NHEX:
2955 case NFA_NOCTAL:
2956 case NFA_WORD:
2957 case NFA_NWORD:
2958 case NFA_HEAD:
2959 case NFA_NHEAD:
2960 case NFA_ALPHA:
2961 case NFA_NALPHA:
2962 case NFA_LOWER:
2963 case NFA_NLOWER:
2964 case NFA_UPPER:
2965 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002966 case NFA_LOWER_IC:
2967 case NFA_NLOWER_IC:
2968 case NFA_UPPER_IC:
2969 case NFA_NUPPER_IC:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002970 /* possibly non-ascii */
2971#ifdef FEAT_MBYTE
2972 if (has_mbyte)
2973 len += 3;
2974 else
2975#endif
2976 ++len;
2977 break;
2978
2979 case NFA_START_INVISIBLE:
2980 case NFA_START_INVISIBLE_NEG:
2981 case NFA_START_INVISIBLE_BEFORE:
2982 case NFA_START_INVISIBLE_BEFORE_NEG:
2983 /* zero-width, out1 points to the END state */
2984 state = state->out1->out;
2985 continue;
2986
2987 case NFA_BACKREF1:
2988 case NFA_BACKREF2:
2989 case NFA_BACKREF3:
2990 case NFA_BACKREF4:
2991 case NFA_BACKREF5:
2992 case NFA_BACKREF6:
2993 case NFA_BACKREF7:
2994 case NFA_BACKREF8:
2995 case NFA_BACKREF9:
2996#ifdef FEAT_SYN_HL
2997 case NFA_ZREF1:
2998 case NFA_ZREF2:
2999 case NFA_ZREF3:
3000 case NFA_ZREF4:
3001 case NFA_ZREF5:
3002 case NFA_ZREF6:
3003 case NFA_ZREF7:
3004 case NFA_ZREF8:
3005 case NFA_ZREF9:
3006#endif
3007 case NFA_NEWL:
3008 case NFA_SKIP:
3009 /* unknown width */
3010 return -1;
3011
3012 case NFA_BOL:
3013 case NFA_EOL:
3014 case NFA_BOF:
3015 case NFA_EOF:
3016 case NFA_BOW:
3017 case NFA_EOW:
3018 case NFA_MOPEN:
3019 case NFA_MOPEN1:
3020 case NFA_MOPEN2:
3021 case NFA_MOPEN3:
3022 case NFA_MOPEN4:
3023 case NFA_MOPEN5:
3024 case NFA_MOPEN6:
3025 case NFA_MOPEN7:
3026 case NFA_MOPEN8:
3027 case NFA_MOPEN9:
3028#ifdef FEAT_SYN_HL
3029 case NFA_ZOPEN:
3030 case NFA_ZOPEN1:
3031 case NFA_ZOPEN2:
3032 case NFA_ZOPEN3:
3033 case NFA_ZOPEN4:
3034 case NFA_ZOPEN5:
3035 case NFA_ZOPEN6:
3036 case NFA_ZOPEN7:
3037 case NFA_ZOPEN8:
3038 case NFA_ZOPEN9:
3039 case NFA_ZCLOSE:
3040 case NFA_ZCLOSE1:
3041 case NFA_ZCLOSE2:
3042 case NFA_ZCLOSE3:
3043 case NFA_ZCLOSE4:
3044 case NFA_ZCLOSE5:
3045 case NFA_ZCLOSE6:
3046 case NFA_ZCLOSE7:
3047 case NFA_ZCLOSE8:
3048 case NFA_ZCLOSE9:
3049#endif
3050 case NFA_MCLOSE:
3051 case NFA_MCLOSE1:
3052 case NFA_MCLOSE2:
3053 case NFA_MCLOSE3:
3054 case NFA_MCLOSE4:
3055 case NFA_MCLOSE5:
3056 case NFA_MCLOSE6:
3057 case NFA_MCLOSE7:
3058 case NFA_MCLOSE8:
3059 case NFA_MCLOSE9:
3060 case NFA_NOPEN:
3061 case NFA_NCLOSE:
3062
3063 case NFA_LNUM_GT:
3064 case NFA_LNUM_LT:
3065 case NFA_COL_GT:
3066 case NFA_COL_LT:
3067 case NFA_VCOL_GT:
3068 case NFA_VCOL_LT:
3069 case NFA_MARK_GT:
3070 case NFA_MARK_LT:
3071 case NFA_VISUAL:
3072 case NFA_LNUM:
3073 case NFA_CURSOR:
3074 case NFA_COL:
3075 case NFA_VCOL:
3076 case NFA_MARK:
3077
3078 case NFA_ZSTART:
3079 case NFA_ZEND:
3080 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003081 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003082 case NFA_START_PATTERN:
3083 case NFA_END_PATTERN:
3084 case NFA_COMPOSING:
3085 case NFA_END_COMPOSING:
3086 /* zero-width */
3087 break;
3088
3089 default:
3090 if (state->c < 0)
3091 /* don't know what this is */
3092 return -1;
3093 /* normal character */
3094 len += MB_CHAR2LEN(state->c);
3095 break;
3096 }
3097
3098 /* normal way to continue */
3099 state = state->out;
3100 }
3101
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003102 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003103 return -1;
3104}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003105
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003106/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003107 * Convert a postfix form into its equivalent NFA.
3108 * Return the NFA start state on success, NULL otherwise.
3109 */
3110 static nfa_state_T *
3111post2nfa(postfix, end, nfa_calc_size)
3112 int *postfix;
3113 int *end;
3114 int nfa_calc_size;
3115{
3116 int *p;
3117 int mopen;
3118 int mclose;
3119 Frag_T *stack = NULL;
3120 Frag_T *stackp = NULL;
3121 Frag_T *stack_end = NULL;
3122 Frag_T e1;
3123 Frag_T e2;
3124 Frag_T e;
3125 nfa_state_T *s;
3126 nfa_state_T *s1;
3127 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003128 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003129
3130 if (postfix == NULL)
3131 return NULL;
3132
Bram Moolenaar053bb602013-05-20 13:55:21 +02003133#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003134#define POP() st_pop(&stackp, stack); \
3135 if (stackp < stack) \
3136 { \
3137 st_error(postfix, end, p); \
3138 return NULL; \
3139 }
3140
3141 if (nfa_calc_size == FALSE)
3142 {
3143 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003144 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003145 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003146 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003147 }
3148
3149 for (p = postfix; p < end; ++p)
3150 {
3151 switch (*p)
3152 {
3153 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003154 /* Concatenation.
3155 * Pay attention: this operator does not exist in the r.e. itself
3156 * (it is implicit, really). It is added when r.e. is translated
3157 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003158 if (nfa_calc_size == TRUE)
3159 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003160 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003161 break;
3162 }
3163 e2 = POP();
3164 e1 = POP();
3165 patch(e1.out, e2.start);
3166 PUSH(frag(e1.start, e2.out));
3167 break;
3168
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003169 case NFA_OR:
3170 /* Alternation */
3171 if (nfa_calc_size == TRUE)
3172 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003173 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003174 break;
3175 }
3176 e2 = POP();
3177 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003178 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003179 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003180 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003181 PUSH(frag(s, append(e1.out, e2.out)));
3182 break;
3183
3184 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003185 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003186 if (nfa_calc_size == TRUE)
3187 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003188 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003189 break;
3190 }
3191 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003192 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003194 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003195 patch(e.out, s);
3196 PUSH(frag(s, list1(&s->out1)));
3197 break;
3198
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003199 case NFA_STAR_NONGREEDY:
3200 /* Zero or more, prefer zero */
3201 if (nfa_calc_size == TRUE)
3202 {
3203 nstate++;
3204 break;
3205 }
3206 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003207 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003208 if (s == NULL)
3209 goto theend;
3210 patch(e.out, s);
3211 PUSH(frag(s, list1(&s->out)));
3212 break;
3213
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003214 case NFA_QUEST:
3215 /* one or zero atoms=> greedy match */
3216 if (nfa_calc_size == TRUE)
3217 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003218 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003219 break;
3220 }
3221 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003222 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003223 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003224 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003225 PUSH(frag(s, append(e.out, list1(&s->out1))));
3226 break;
3227
3228 case NFA_QUEST_NONGREEDY:
3229 /* zero or one atoms => non-greedy match */
3230 if (nfa_calc_size == TRUE)
3231 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003232 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003233 break;
3234 }
3235 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003236 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003237 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003238 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003239 PUSH(frag(s, append(e.out, list1(&s->out))));
3240 break;
3241
Bram Moolenaar417bad22013-06-07 14:08:30 +02003242 case NFA_END_COLL:
3243 case NFA_END_NEG_COLL:
3244 /* On the stack is the sequence starting with NFA_START_COLL or
3245 * NFA_START_NEG_COLL and all possible characters. Patch it to
3246 * add the output to the start. */
3247 if (nfa_calc_size == TRUE)
3248 {
3249 nstate++;
3250 break;
3251 }
3252 e = POP();
3253 s = alloc_state(NFA_END_COLL, NULL, NULL);
3254 if (s == NULL)
3255 goto theend;
3256 patch(e.out, s);
3257 e.start->out1 = s;
3258 PUSH(frag(e.start, list1(&s->out)));
3259 break;
3260
3261 case NFA_RANGE:
3262 /* Before this are two characters, the low and high end of a
3263 * range. Turn them into two states with MIN and MAX. */
3264 if (nfa_calc_size == TRUE)
3265 {
3266 /* nstate += 0; */
3267 break;
3268 }
3269 e2 = POP();
3270 e1 = POP();
3271 e2.start->val = e2.start->c;
3272 e2.start->c = NFA_RANGE_MAX;
3273 e1.start->val = e1.start->c;
3274 e1.start->c = NFA_RANGE_MIN;
3275 patch(e1.out, e2.start);
3276 PUSH(frag(e1.start, e2.out));
3277 break;
3278
Bram Moolenaar699c1202013-09-25 16:41:54 +02003279 case NFA_EMPTY:
3280 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003281 if (nfa_calc_size == TRUE)
3282 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003283 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003284 break;
3285 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003286 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003287 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003288 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003289 PUSH(frag(s, list1(&s->out)));
3290 break;
3291
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003292 case NFA_OPT_CHARS:
3293 {
3294 int n;
3295
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003296 /* \%[abc] implemented as:
3297 * NFA_SPLIT
3298 * +-CHAR(a)
3299 * | +-NFA_SPLIT
3300 * | +-CHAR(b)
3301 * | | +-NFA_SPLIT
3302 * | | +-CHAR(c)
3303 * | | | +-next
3304 * | | +- next
3305 * | +- next
3306 * +- next
3307 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003308 n = *++p; /* get number of characters */
3309 if (nfa_calc_size == TRUE)
3310 {
3311 nstate += n;
3312 break;
3313 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003314 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003315 e1.out = NULL; /* stores list with out1's */
3316 s1 = NULL; /* previous NFA_SPLIT to connect to */
3317 while (n-- > 0)
3318 {
3319 e = POP(); /* get character */
3320 s = alloc_state(NFA_SPLIT, e.start, NULL);
3321 if (s == NULL)
3322 goto theend;
3323 if (e1.out == NULL)
3324 e1 = e;
3325 patch(e.out, s1);
3326 append(e1.out, list1(&s->out1));
3327 s1 = s;
3328 }
3329 PUSH(frag(s, e1.out));
3330 break;
3331 }
3332
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003333 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003334 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003335 case NFA_PREV_ATOM_JUST_BEFORE:
3336 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003337 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003338 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003339 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3340 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003341 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003342 int start_state;
3343 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003344 int n = 0;
3345 nfa_state_T *zend;
3346 nfa_state_T *skip;
3347
Bram Moolenaardecd9542013-06-07 16:31:50 +02003348 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003349 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003350 case NFA_PREV_ATOM_NO_WIDTH:
3351 start_state = NFA_START_INVISIBLE;
3352 end_state = NFA_END_INVISIBLE;
3353 break;
3354 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3355 start_state = NFA_START_INVISIBLE_NEG;
3356 end_state = NFA_END_INVISIBLE_NEG;
3357 break;
3358 case NFA_PREV_ATOM_JUST_BEFORE:
3359 start_state = NFA_START_INVISIBLE_BEFORE;
3360 end_state = NFA_END_INVISIBLE;
3361 break;
3362 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3363 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3364 end_state = NFA_END_INVISIBLE_NEG;
3365 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003366 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003367 start_state = NFA_START_PATTERN;
3368 end_state = NFA_END_PATTERN;
3369 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003370 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003371
3372 if (before)
3373 n = *++p; /* get the count */
3374
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003375 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003376 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003377 * The \@<= operator: match for the preceding atom.
3378 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003379 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003380 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003381
3382 if (nfa_calc_size == TRUE)
3383 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003384 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003385 break;
3386 }
3387 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003388 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003389 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003390 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003391
Bram Moolenaar87953742013-06-05 18:52:40 +02003392 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003393 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003394 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003395 if (pattern)
3396 {
3397 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3398 skip = alloc_state(NFA_SKIP, NULL, NULL);
3399 zend = alloc_state(NFA_ZEND, s1, NULL);
3400 s1->out= skip;
3401 patch(e.out, zend);
3402 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003403 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003404 else
3405 {
3406 patch(e.out, s1);
3407 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003408 if (before)
3409 {
3410 if (n <= 0)
3411 /* See if we can guess the maximum width, it avoids a
3412 * lot of pointless tries. */
3413 n = nfa_max_width(e.start, 0);
3414 s->val = n; /* store the count */
3415 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003416 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003417 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003418 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003419
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003420#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003421 case NFA_COMPOSING: /* char with composing char */
3422#if 0
3423 /* TODO */
3424 if (regflags & RF_ICOMBINE)
3425 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003426 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003427 }
3428#endif
3429 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003430#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003431
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003432 case NFA_MOPEN: /* \( \) Submatch */
3433 case NFA_MOPEN1:
3434 case NFA_MOPEN2:
3435 case NFA_MOPEN3:
3436 case NFA_MOPEN4:
3437 case NFA_MOPEN5:
3438 case NFA_MOPEN6:
3439 case NFA_MOPEN7:
3440 case NFA_MOPEN8:
3441 case NFA_MOPEN9:
3442#ifdef FEAT_SYN_HL
3443 case NFA_ZOPEN: /* \z( \) Submatch */
3444 case NFA_ZOPEN1:
3445 case NFA_ZOPEN2:
3446 case NFA_ZOPEN3:
3447 case NFA_ZOPEN4:
3448 case NFA_ZOPEN5:
3449 case NFA_ZOPEN6:
3450 case NFA_ZOPEN7:
3451 case NFA_ZOPEN8:
3452 case NFA_ZOPEN9:
3453#endif
3454 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003455 if (nfa_calc_size == TRUE)
3456 {
3457 nstate += 2;
3458 break;
3459 }
3460
3461 mopen = *p;
3462 switch (*p)
3463 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003464 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3465#ifdef FEAT_SYN_HL
3466 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3467 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3468 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3469 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3470 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3471 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3472 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3473 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3474 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3475 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3476#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003477#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003478 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003479#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003480 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003481 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003482 mclose = *p + NSUBEXP;
3483 break;
3484 }
3485
3486 /* Allow "NFA_MOPEN" as a valid postfix representation for
3487 * the empty regexp "". In this case, the NFA will be
3488 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3489 * empty groups of parenthesis, and empty mbyte chars */
3490 if (stackp == stack)
3491 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003492 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003494 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003495 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003496 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003497 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498 patch(list1(&s->out), s1);
3499 PUSH(frag(s, list1(&s1->out)));
3500 break;
3501 }
3502
3503 /* At least one node was emitted before NFA_MOPEN, so
3504 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3505 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003506 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003507 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003508 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509
Bram Moolenaar525666f2013-06-02 16:40:55 +02003510 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003511 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003512 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003513 patch(e.out, s1);
3514
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003515#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003516 if (mopen == NFA_COMPOSING)
3517 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003519#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520
3521 PUSH(frag(s, list1(&s1->out)));
3522 break;
3523
Bram Moolenaar5714b802013-05-28 22:03:20 +02003524 case NFA_BACKREF1:
3525 case NFA_BACKREF2:
3526 case NFA_BACKREF3:
3527 case NFA_BACKREF4:
3528 case NFA_BACKREF5:
3529 case NFA_BACKREF6:
3530 case NFA_BACKREF7:
3531 case NFA_BACKREF8:
3532 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003533#ifdef FEAT_SYN_HL
3534 case NFA_ZREF1:
3535 case NFA_ZREF2:
3536 case NFA_ZREF3:
3537 case NFA_ZREF4:
3538 case NFA_ZREF5:
3539 case NFA_ZREF6:
3540 case NFA_ZREF7:
3541 case NFA_ZREF8:
3542 case NFA_ZREF9:
3543#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003544 if (nfa_calc_size == TRUE)
3545 {
3546 nstate += 2;
3547 break;
3548 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003549 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003550 if (s == NULL)
3551 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003552 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003553 if (s1 == NULL)
3554 goto theend;
3555 patch(list1(&s->out), s1);
3556 PUSH(frag(s, list1(&s1->out)));
3557 break;
3558
Bram Moolenaar423532e2013-05-29 21:14:42 +02003559 case NFA_LNUM:
3560 case NFA_LNUM_GT:
3561 case NFA_LNUM_LT:
3562 case NFA_VCOL:
3563 case NFA_VCOL_GT:
3564 case NFA_VCOL_LT:
3565 case NFA_COL:
3566 case NFA_COL_GT:
3567 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003568 case NFA_MARK:
3569 case NFA_MARK_GT:
3570 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003571 {
3572 int n = *++p; /* lnum, col or mark name */
3573
Bram Moolenaar423532e2013-05-29 21:14:42 +02003574 if (nfa_calc_size == TRUE)
3575 {
3576 nstate += 1;
3577 break;
3578 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003579 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003580 if (s == NULL)
3581 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003582 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003583 PUSH(frag(s, list1(&s->out)));
3584 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003585 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003586
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003587 case NFA_ZSTART:
3588 case NFA_ZEND:
3589 default:
3590 /* Operands */
3591 if (nfa_calc_size == TRUE)
3592 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003593 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 break;
3595 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003596 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003597 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003598 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003599 PUSH(frag(s, list1(&s->out)));
3600 break;
3601
3602 } /* switch(*p) */
3603
3604 } /* for(p = postfix; *p; ++p) */
3605
3606 if (nfa_calc_size == TRUE)
3607 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003608 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003609 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 }
3611
3612 e = POP();
3613 if (stackp != stack)
3614 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3615
3616 if (istate >= nstate)
3617 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3618
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003619 matchstate = &state_ptr[istate++]; /* the match state */
3620 matchstate->c = NFA_MATCH;
3621 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003622 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003623
3624 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003625 ret = e.start;
3626
3627theend:
3628 vim_free(stack);
3629 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630
3631#undef POP1
3632#undef PUSH1
3633#undef POP2
3634#undef PUSH2
3635#undef POP
3636#undef PUSH
3637}
3638
Bram Moolenaara2947e22013-06-11 22:44:09 +02003639/*
3640 * After building the NFA program, inspect it to add optimization hints.
3641 */
3642 static void
3643nfa_postprocess(prog)
3644 nfa_regprog_T *prog;
3645{
3646 int i;
3647 int c;
3648
3649 for (i = 0; i < prog->nstate; ++i)
3650 {
3651 c = prog->state[i].c;
3652 if (c == NFA_START_INVISIBLE
3653 || c == NFA_START_INVISIBLE_NEG
3654 || c == NFA_START_INVISIBLE_BEFORE
3655 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3656 {
3657 int directly;
3658
3659 /* Do it directly when what follows is possibly the end of the
3660 * match. */
3661 if (match_follows(prog->state[i].out1->out, 0))
3662 directly = TRUE;
3663 else
3664 {
3665 int ch_invisible = failure_chance(prog->state[i].out, 0);
3666 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3667
3668 /* Postpone when the invisible match is expensive or has a
3669 * lower chance of failing. */
3670 if (c == NFA_START_INVISIBLE_BEFORE
3671 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3672 {
3673 /* "before" matches are very expensive when
3674 * unbounded, always prefer what follows then,
3675 * unless what follows will always match.
3676 * Otherwise strongly prefer what follows. */
3677 if (prog->state[i].val <= 0 && ch_follows > 0)
3678 directly = FALSE;
3679 else
3680 directly = ch_follows * 10 < ch_invisible;
3681 }
3682 else
3683 {
3684 /* normal invisible, first do the one with the
3685 * highest failure chance */
3686 directly = ch_follows < ch_invisible;
3687 }
3688 }
3689 if (directly)
3690 /* switch to the _FIRST state */
3691 ++prog->state[i].c;
3692 }
3693 }
3694}
3695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003696/****************************************************************
3697 * NFA execution code.
3698 ****************************************************************/
3699
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003700typedef struct
3701{
3702 int in_use; /* number of subexpr with useful info */
3703
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003704 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003705 union
3706 {
3707 struct multipos
3708 {
3709 lpos_T start;
3710 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003711 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003712 struct linepos
3713 {
3714 char_u *start;
3715 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003716 } line[NSUBEXP];
3717 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003718} regsub_T;
3719
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003720typedef struct
3721{
3722 regsub_T norm; /* \( .. \) matches */
3723#ifdef FEAT_SYN_HL
3724 regsub_T synt; /* \z( .. \) matches */
3725#endif
3726} regsubs_T;
3727
Bram Moolenaara2d95102013-06-04 14:23:05 +02003728/* nfa_pim_T stores a Postponed Invisible Match. */
3729typedef struct nfa_pim_S nfa_pim_T;
3730struct nfa_pim_S
3731{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003732 int result; /* NFA_PIM_*, see below */
3733 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003734 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003735 union
3736 {
3737 lpos_T pos;
3738 char_u *ptr;
3739 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003740};
3741
3742/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003743#define NFA_PIM_UNUSED 0 /* pim not used */
3744#define NFA_PIM_TODO 1 /* pim not done yet */
3745#define NFA_PIM_MATCH 2 /* pim executed, matches */
3746#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003747
3748
Bram Moolenaar963fee22013-05-26 21:47:28 +02003749/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003750typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003751{
3752 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003753 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003754 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3755 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003756 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003757} nfa_thread_T;
3758
Bram Moolenaar963fee22013-05-26 21:47:28 +02003759/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003760typedef struct
3761{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003762 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003763 int n; /* nr of states currently in "t" */
3764 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003765 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003766 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003767} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003768
Bram Moolenaar5714b802013-05-28 22:03:20 +02003769#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003770static void log_subsexpr __ARGS((regsubs_T *subs));
3771static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003772static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003773
3774 static void
3775log_subsexpr(subs)
3776 regsubs_T *subs;
3777{
3778 log_subexpr(&subs->norm);
3779# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003780 if (nfa_has_zsubexpr)
3781 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003782# endif
3783}
3784
Bram Moolenaar5714b802013-05-28 22:03:20 +02003785 static void
3786log_subexpr(sub)
3787 regsub_T *sub;
3788{
3789 int j;
3790
3791 for (j = 0; j < sub->in_use; j++)
3792 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003793 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003794 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003795 sub->list.multi[j].start.col,
3796 (int)sub->list.multi[j].start.lnum,
3797 sub->list.multi[j].end.col,
3798 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003799 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003800 {
3801 char *s = (char *)sub->list.line[j].start;
3802 char *e = (char *)sub->list.line[j].end;
3803
Bram Moolenaar87953742013-06-05 18:52:40 +02003804 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003805 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003806 s == NULL ? "NULL" : s,
3807 e == NULL ? "NULL" : e);
3808 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003809}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003810
3811 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003812pim_info(pim)
3813 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003814{
3815 static char buf[30];
3816
3817 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3818 buf[0] = NUL;
3819 else
3820 {
3821 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3822 : (int)(pim->end.ptr - reginput));
3823 }
3824 return buf;
3825}
3826
Bram Moolenaar5714b802013-05-28 22:03:20 +02003827#endif
3828
Bram Moolenaar963fee22013-05-26 21:47:28 +02003829/* Used during execution: whether a match has been found. */
3830static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003831
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003832static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003833static void clear_sub __ARGS((regsub_T *sub));
3834static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3835static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaarf2118842013-09-25 18:16:38 +02003836static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003837static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003838static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003839static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3840static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003841static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003842static 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 +02003843static 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 +02003844
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003845/*
3846 * Copy postponed invisible match info from "from" to "to".
3847 */
3848 static void
3849copy_pim(to, from)
3850 nfa_pim_T *to;
3851 nfa_pim_T *from;
3852{
3853 to->result = from->result;
3854 to->state = from->state;
3855 copy_sub(&to->subs.norm, &from->subs.norm);
3856#ifdef FEAT_SYN_HL
3857 if (nfa_has_zsubexpr)
3858 copy_sub(&to->subs.synt, &from->subs.synt);
3859#endif
3860 to->end = from->end;
3861}
3862
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003863 static void
3864clear_sub(sub)
3865 regsub_T *sub;
3866{
3867 if (REG_MULTI)
3868 /* Use 0xff to set lnum to -1 */
3869 vim_memset(sub->list.multi, 0xff,
3870 sizeof(struct multipos) * nfa_nsubexpr);
3871 else
3872 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3873 sub->in_use = 0;
3874}
3875
3876/*
3877 * Copy the submatches from "from" to "to".
3878 */
3879 static void
3880copy_sub(to, from)
3881 regsub_T *to;
3882 regsub_T *from;
3883{
3884 to->in_use = from->in_use;
3885 if (from->in_use > 0)
3886 {
3887 /* Copy the match start and end positions. */
3888 if (REG_MULTI)
3889 mch_memmove(&to->list.multi[0],
3890 &from->list.multi[0],
3891 sizeof(struct multipos) * from->in_use);
3892 else
3893 mch_memmove(&to->list.line[0],
3894 &from->list.line[0],
3895 sizeof(struct linepos) * from->in_use);
3896 }
3897}
3898
3899/*
3900 * Like copy_sub() but exclude the main match.
3901 */
3902 static void
3903copy_sub_off(to, from)
3904 regsub_T *to;
3905 regsub_T *from;
3906{
3907 if (to->in_use < from->in_use)
3908 to->in_use = from->in_use;
3909 if (from->in_use > 1)
3910 {
3911 /* Copy the match start and end positions. */
3912 if (REG_MULTI)
3913 mch_memmove(&to->list.multi[1],
3914 &from->list.multi[1],
3915 sizeof(struct multipos) * (from->in_use - 1));
3916 else
3917 mch_memmove(&to->list.line[1],
3918 &from->list.line[1],
3919 sizeof(struct linepos) * (from->in_use - 1));
3920 }
3921}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003922
Bram Moolenaar428e9872013-05-30 17:05:39 +02003923/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003924 * Like copy_sub() but only do the end of the main match if \ze is present.
3925 */
3926 static void
3927copy_ze_off(to, from)
3928 regsub_T *to;
3929 regsub_T *from;
3930{
3931 if (nfa_has_zend)
3932 {
3933 if (REG_MULTI)
3934 {
3935 if (from->list.multi[0].end.lnum >= 0)
3936 to->list.multi[0].end = from->list.multi[0].end;
3937 }
3938 else
3939 {
3940 if (from->list.line[0].end != NULL)
3941 to->list.line[0].end = from->list.line[0].end;
3942 }
3943 }
3944}
3945
3946/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003947 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003948 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003949 */
3950 static int
3951sub_equal(sub1, sub2)
3952 regsub_T *sub1;
3953 regsub_T *sub2;
3954{
3955 int i;
3956 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003957 linenr_T s1;
3958 linenr_T s2;
3959 char_u *sp1;
3960 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003961
3962 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3963 if (REG_MULTI)
3964 {
3965 for (i = 0; i < todo; ++i)
3966 {
3967 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003968 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003969 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003970 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003971 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003972 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003973 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003974 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003975 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003976 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02003977 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02003978 != sub2->list.multi[i].start.col)
3979 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02003980
3981 if (nfa_has_backref)
3982 {
3983 if (i < sub1->in_use)
3984 s1 = sub1->list.multi[i].end.lnum;
3985 else
3986 s1 = -1;
3987 if (i < sub2->in_use)
3988 s2 = sub2->list.multi[i].end.lnum;
3989 else
3990 s2 = -1;
3991 if (s1 != s2)
3992 return FALSE;
3993 if (s1 != -1 && sub1->list.multi[i].end.col
3994 != sub2->list.multi[i].end.col)
3995 return FALSE;
3996 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02003997 }
3998 }
3999 else
4000 {
4001 for (i = 0; i < todo; ++i)
4002 {
4003 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004004 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004005 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004006 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004007 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004008 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004009 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004010 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004011 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004012 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004013 if (nfa_has_backref)
4014 {
4015 if (i < sub1->in_use)
4016 sp1 = sub1->list.line[i].end;
4017 else
4018 sp1 = NULL;
4019 if (i < sub2->in_use)
4020 sp2 = sub2->list.line[i].end;
4021 else
4022 sp2 = NULL;
4023 if (sp1 != sp2)
4024 return FALSE;
4025 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004026 }
4027 }
4028
4029 return TRUE;
4030}
4031
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004032#ifdef ENABLE_LOG
4033 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004034report_state(char *action,
4035 regsub_T *sub,
4036 nfa_state_T *state,
4037 int lid,
4038 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004039{
4040 int col;
4041
4042 if (sub->in_use <= 0)
4043 col = -1;
4044 else if (REG_MULTI)
4045 col = sub->list.multi[0].start.col;
4046 else
4047 col = (int)(sub->list.line[0].start - regline);
4048 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004049 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4050 action, abs(state->id), lid, state->c, code, col,
4051 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004052}
4053#endif
4054
Bram Moolenaar43e02982013-06-07 17:31:29 +02004055/*
4056 * Return TRUE if the same state is already in list "l" with the same
4057 * positions as "subs".
4058 */
4059 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02004060has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02004061 nfa_list_T *l; /* runtime state list */
4062 nfa_state_T *state; /* state to update */
4063 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004064 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004065{
4066 nfa_thread_T *thread;
4067 int i;
4068
4069 for (i = 0; i < l->n; ++i)
4070 {
4071 thread = &l->t[i];
4072 if (thread->state->id == state->id
4073 && sub_equal(&thread->subs.norm, &subs->norm)
4074#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004075 && (!nfa_has_zsubexpr
4076 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004077#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004078 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004079 return TRUE;
4080 }
4081 return FALSE;
4082}
4083
4084/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004085 * Return TRUE if "one" and "two" are equal. That includes when both are not
4086 * set.
4087 */
4088 static int
4089pim_equal(one, two)
4090 nfa_pim_T *one;
4091 nfa_pim_T *two;
4092{
4093 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4094 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4095
4096 if (one_unused)
4097 /* one is unused: equal when two is also unused */
4098 return two_unused;
4099 if (two_unused)
4100 /* one is used and two is not: not equal */
4101 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004102 /* compare the state id */
4103 if (one->state->id != two->state->id)
4104 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004105 /* compare the position */
4106 if (REG_MULTI)
4107 return one->end.pos.lnum == two->end.pos.lnum
4108 && one->end.pos.col == two->end.pos.col;
4109 return one->end.ptr == two->end.ptr;
4110}
4111
4112/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004113 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4114 */
4115 static int
4116match_follows(startstate, depth)
4117 nfa_state_T *startstate;
4118 int depth;
4119{
4120 nfa_state_T *state = startstate;
4121
4122 /* avoid too much recursion */
4123 if (depth > 10)
4124 return FALSE;
4125
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004126 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004127 {
4128 switch (state->c)
4129 {
4130 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004131 case NFA_MCLOSE:
4132 case NFA_END_INVISIBLE:
4133 case NFA_END_INVISIBLE_NEG:
4134 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004135 return TRUE;
4136
4137 case NFA_SPLIT:
4138 return match_follows(state->out, depth + 1)
4139 || match_follows(state->out1, depth + 1);
4140
4141 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004142 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004143 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004144 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004145 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004146 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004147 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004148 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004149 case NFA_COMPOSING:
4150 /* skip ahead to next state */
4151 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004152 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004153
4154 case NFA_ANY:
4155 case NFA_IDENT:
4156 case NFA_SIDENT:
4157 case NFA_KWORD:
4158 case NFA_SKWORD:
4159 case NFA_FNAME:
4160 case NFA_SFNAME:
4161 case NFA_PRINT:
4162 case NFA_SPRINT:
4163 case NFA_WHITE:
4164 case NFA_NWHITE:
4165 case NFA_DIGIT:
4166 case NFA_NDIGIT:
4167 case NFA_HEX:
4168 case NFA_NHEX:
4169 case NFA_OCTAL:
4170 case NFA_NOCTAL:
4171 case NFA_WORD:
4172 case NFA_NWORD:
4173 case NFA_HEAD:
4174 case NFA_NHEAD:
4175 case NFA_ALPHA:
4176 case NFA_NALPHA:
4177 case NFA_LOWER:
4178 case NFA_NLOWER:
4179 case NFA_UPPER:
4180 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004181 case NFA_LOWER_IC:
4182 case NFA_NLOWER_IC:
4183 case NFA_UPPER_IC:
4184 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004185 case NFA_START_COLL:
4186 case NFA_START_NEG_COLL:
4187 case NFA_NEWL:
4188 /* state will advance input */
4189 return FALSE;
4190
4191 default:
4192 if (state->c > 0)
4193 /* state will advance input */
4194 return FALSE;
4195
4196 /* Others: zero-width or possibly zero-width, might still find
4197 * a match at the same position, keep looking. */
4198 break;
4199 }
4200 state = state->out;
4201 }
4202 return FALSE;
4203}
4204
4205
4206/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004207 * Return TRUE if "state" is already in list "l".
4208 */
4209 static int
4210state_in_list(l, state, subs)
4211 nfa_list_T *l; /* runtime state list */
4212 nfa_state_T *state; /* state to update */
4213 regsubs_T *subs; /* pointers to subexpressions */
4214{
4215 if (state->lastlist[nfa_ll_index] == l->id)
4216 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004217 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004218 return TRUE;
4219 }
4220 return FALSE;
4221}
4222
Bram Moolenaard05bf562013-06-30 23:24:08 +02004223/*
4224 * Add "state" and possibly what follows to state list ".".
4225 * Returns "subs_arg", possibly copied into temp_subs.
4226 */
4227
4228 static regsubs_T *
4229addstate(l, state, subs_arg, pim, off)
4230 nfa_list_T *l; /* runtime state list */
4231 nfa_state_T *state; /* state to update */
4232 regsubs_T *subs_arg; /* pointers to subexpressions */
4233 nfa_pim_T *pim; /* postponed look-behind match */
4234 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004235{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004236 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004237 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004238 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004239 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004240 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004241 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004242 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004243 regsubs_T *subs = subs_arg;
4244 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004245#ifdef ENABLE_LOG
4246 int did_print = FALSE;
4247#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004249 switch (state->c)
4250 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004251 case NFA_NCLOSE:
4252 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004253 case NFA_MCLOSE1:
4254 case NFA_MCLOSE2:
4255 case NFA_MCLOSE3:
4256 case NFA_MCLOSE4:
4257 case NFA_MCLOSE5:
4258 case NFA_MCLOSE6:
4259 case NFA_MCLOSE7:
4260 case NFA_MCLOSE8:
4261 case NFA_MCLOSE9:
4262#ifdef FEAT_SYN_HL
4263 case NFA_ZCLOSE:
4264 case NFA_ZCLOSE1:
4265 case NFA_ZCLOSE2:
4266 case NFA_ZCLOSE3:
4267 case NFA_ZCLOSE4:
4268 case NFA_ZCLOSE5:
4269 case NFA_ZCLOSE6:
4270 case NFA_ZCLOSE7:
4271 case NFA_ZCLOSE8:
4272 case NFA_ZCLOSE9:
4273#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004274 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004275 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004276 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004277 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004278 /* These nodes are not added themselves but their "out" and/or
4279 * "out1" may be added below. */
4280 break;
4281
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004282 case NFA_BOL:
4283 case NFA_BOF:
4284 /* "^" won't match past end-of-line, don't bother trying.
4285 * Except when at the end of the line, or when we are going to the
4286 * next line for a look-behind match. */
4287 if (reginput > regline
4288 && *reginput != NUL
4289 && (nfa_endp == NULL
4290 || !REG_MULTI
4291 || reglnum == nfa_endp->se_u.pos.lnum))
4292 goto skip_add;
4293 /* FALLTHROUGH */
4294
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004295 case NFA_MOPEN1:
4296 case NFA_MOPEN2:
4297 case NFA_MOPEN3:
4298 case NFA_MOPEN4:
4299 case NFA_MOPEN5:
4300 case NFA_MOPEN6:
4301 case NFA_MOPEN7:
4302 case NFA_MOPEN8:
4303 case NFA_MOPEN9:
4304#ifdef FEAT_SYN_HL
4305 case NFA_ZOPEN:
4306 case NFA_ZOPEN1:
4307 case NFA_ZOPEN2:
4308 case NFA_ZOPEN3:
4309 case NFA_ZOPEN4:
4310 case NFA_ZOPEN5:
4311 case NFA_ZOPEN6:
4312 case NFA_ZOPEN7:
4313 case NFA_ZOPEN8:
4314 case NFA_ZOPEN9:
4315#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004316 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004317 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004318 /* These nodes need to be added so that we can bail out when it
4319 * was added to this list before at the same position to avoid an
4320 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004321
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004322 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004323 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004324 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004325 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004326 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004327 * when there is a PIM. For NFA_MATCH check the position,
4328 * lower position is preferred. */
4329 if (!nfa_has_backref && pim == NULL && !l->has_pim
4330 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004331 {
4332skip_add:
4333#ifdef ENABLE_LOG
4334 nfa_set_code(state->c);
4335 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4336 abs(state->id), l->id, state->c, code);
4337#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004338 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004339 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004340
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004341 /* Do not add the state again when it exists with the same
4342 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004343 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004344 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004345 }
4346
Bram Moolenaara0169122013-06-26 18:16:58 +02004347 /* When there are backreferences or PIMs the number of states may
4348 * be (a lot) bigger than anticipated. */
4349 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004350 {
4351 int newlen = l->len * 3 / 2 + 50;
4352
Bram Moolenaard05bf562013-06-30 23:24:08 +02004353 if (subs != &temp_subs)
4354 {
4355 /* "subs" may point into the current array, need to make a
4356 * copy before it becomes invalid. */
4357 copy_sub(&temp_subs.norm, &subs->norm);
4358#ifdef FEAT_SYN_HL
4359 if (nfa_has_zsubexpr)
4360 copy_sub(&temp_subs.synt, &subs->synt);
4361#endif
4362 subs = &temp_subs;
4363 }
4364
Bram Moolenaar428e9872013-05-30 17:05:39 +02004365 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4366 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004368
4369 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004370 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004371 thread = &l->t[l->n++];
4372 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004373 if (pim == NULL)
4374 thread->pim.result = NFA_PIM_UNUSED;
4375 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004376 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004377 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004378 l->has_pim = TRUE;
4379 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004380 copy_sub(&thread->subs.norm, &subs->norm);
4381#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004382 if (nfa_has_zsubexpr)
4383 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004384#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004385#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004386 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004387 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004388#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004389 }
4390
4391#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004392 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004393 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004394#endif
4395 switch (state->c)
4396 {
4397 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004398 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004399 break;
4400
4401 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004402 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004403 subs = addstate(l, state->out, subs, pim, off);
4404 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004405 break;
4406
Bram Moolenaar699c1202013-09-25 16:41:54 +02004407 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004408 case NFA_NOPEN:
4409 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004410 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004411 break;
4412
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004413 case NFA_MOPEN:
4414 case NFA_MOPEN1:
4415 case NFA_MOPEN2:
4416 case NFA_MOPEN3:
4417 case NFA_MOPEN4:
4418 case NFA_MOPEN5:
4419 case NFA_MOPEN6:
4420 case NFA_MOPEN7:
4421 case NFA_MOPEN8:
4422 case NFA_MOPEN9:
4423#ifdef FEAT_SYN_HL
4424 case NFA_ZOPEN:
4425 case NFA_ZOPEN1:
4426 case NFA_ZOPEN2:
4427 case NFA_ZOPEN3:
4428 case NFA_ZOPEN4:
4429 case NFA_ZOPEN5:
4430 case NFA_ZOPEN6:
4431 case NFA_ZOPEN7:
4432 case NFA_ZOPEN8:
4433 case NFA_ZOPEN9:
4434#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004435 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004436 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004437 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004438 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004439 sub = &subs->norm;
4440 }
4441#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004442 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004443 {
4444 subidx = state->c - NFA_ZOPEN;
4445 sub = &subs->synt;
4446 }
4447#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004448 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004449 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004450 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004451 sub = &subs->norm;
4452 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004453
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004454 /* avoid compiler warnings */
4455 save_ptr = NULL;
4456 save_lpos.lnum = 0;
4457 save_lpos.col = 0;
4458
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004459 /* Set the position (with "off" added) in the subexpression. Save
4460 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004461 if (REG_MULTI)
4462 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004463 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004464 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004465 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004466 save_in_use = -1;
4467 }
4468 else
4469 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004470 save_in_use = sub->in_use;
4471 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004472 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004473 sub->list.multi[i].start.lnum = -1;
4474 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004475 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004476 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004477 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004478 if (off == -1)
4479 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004480 sub->list.multi[subidx].start.lnum = reglnum + 1;
4481 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004482 }
4483 else
4484 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004485 sub->list.multi[subidx].start.lnum = reglnum;
4486 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004487 (colnr_T)(reginput - regline + off);
4488 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004489 }
4490 else
4491 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004492 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004493 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004494 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004495 save_in_use = -1;
4496 }
4497 else
4498 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004499 save_in_use = sub->in_use;
4500 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004501 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004502 sub->list.line[i].start = NULL;
4503 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004504 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004505 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004506 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004507 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004508 }
4509
Bram Moolenaard05bf562013-06-30 23:24:08 +02004510 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004511 /* "subs" may have changed, need to set "sub" again */
4512#ifdef FEAT_SYN_HL
4513 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4514 sub = &subs->synt;
4515 else
4516#endif
4517 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004518
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004519 if (save_in_use == -1)
4520 {
4521 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004522 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004523 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004524 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004525 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004526 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004527 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004528 break;
4529
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004530 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004531 if (nfa_has_zend && (REG_MULTI
4532 ? subs->norm.list.multi[0].end.lnum >= 0
4533 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004534 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004535 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004536 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004537 break;
4538 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004539 case NFA_MCLOSE1:
4540 case NFA_MCLOSE2:
4541 case NFA_MCLOSE3:
4542 case NFA_MCLOSE4:
4543 case NFA_MCLOSE5:
4544 case NFA_MCLOSE6:
4545 case NFA_MCLOSE7:
4546 case NFA_MCLOSE8:
4547 case NFA_MCLOSE9:
4548#ifdef FEAT_SYN_HL
4549 case NFA_ZCLOSE:
4550 case NFA_ZCLOSE1:
4551 case NFA_ZCLOSE2:
4552 case NFA_ZCLOSE3:
4553 case NFA_ZCLOSE4:
4554 case NFA_ZCLOSE5:
4555 case NFA_ZCLOSE6:
4556 case NFA_ZCLOSE7:
4557 case NFA_ZCLOSE8:
4558 case NFA_ZCLOSE9:
4559#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004560 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004562 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004563 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004564 sub = &subs->norm;
4565 }
4566#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004567 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004568 {
4569 subidx = state->c - NFA_ZCLOSE;
4570 sub = &subs->synt;
4571 }
4572#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004573 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004574 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004575 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004576 sub = &subs->norm;
4577 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004579 /* We don't fill in gaps here, there must have been an MOPEN that
4580 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004581 save_in_use = sub->in_use;
4582 if (sub->in_use <= subidx)
4583 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004584 if (REG_MULTI)
4585 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004586 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004587 if (off == -1)
4588 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004589 sub->list.multi[subidx].end.lnum = reglnum + 1;
4590 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004591 }
4592 else
4593 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004594 sub->list.multi[subidx].end.lnum = reglnum;
4595 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004596 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004597 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004598 /* avoid compiler warnings */
4599 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004600 }
4601 else
4602 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004603 save_ptr = sub->list.line[subidx].end;
4604 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004605 /* avoid compiler warnings */
4606 save_lpos.lnum = 0;
4607 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004608 }
4609
Bram Moolenaard05bf562013-06-30 23:24:08 +02004610 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004611 /* "subs" may have changed, need to set "sub" again */
4612#ifdef FEAT_SYN_HL
4613 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4614 sub = &subs->synt;
4615 else
4616#endif
4617 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004618
4619 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004620 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004621 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004622 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004623 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004624 break;
4625 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004626 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004627}
4628
4629/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004630 * Like addstate(), but the new state(s) are put at position "*ip".
4631 * Used for zero-width matches, next state to use is the added one.
4632 * This makes sure the order of states to be tried does not change, which
4633 * matters for alternatives.
4634 */
4635 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004636addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004637 nfa_list_T *l; /* runtime state list */
4638 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004639 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004640 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004641 int *ip;
4642{
4643 int tlen = l->n;
4644 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004645 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004646
4647 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004648 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004649
Bram Moolenaar4b417062013-05-25 20:19:50 +02004650 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004651 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004652 return;
4653
4654 /* re-order to put the new state at the current position */
4655 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004656 if (count == 0)
4657 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004658 if (count == 1)
4659 {
4660 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004661 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004662 }
4663 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004664 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004665 if (l->n + count - 1 >= l->len)
4666 {
4667 /* not enough space to move the new states, reallocate the list
4668 * and move the states to the right position */
4669 nfa_thread_T *newl;
4670
4671 l->len = l->len * 3 / 2 + 50;
4672 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4673 if (newl == NULL)
4674 return;
4675 mch_memmove(&(newl[0]),
4676 &(l->t[0]),
4677 sizeof(nfa_thread_T) * listidx);
4678 mch_memmove(&(newl[listidx]),
4679 &(l->t[l->n - count]),
4680 sizeof(nfa_thread_T) * count);
4681 mch_memmove(&(newl[listidx + count]),
4682 &(l->t[listidx + 1]),
4683 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4684 vim_free(l->t);
4685 l->t = newl;
4686 }
4687 else
4688 {
4689 /* make space for new states, then move them from the
4690 * end to the current position */
4691 mch_memmove(&(l->t[listidx + count]),
4692 &(l->t[listidx + 1]),
4693 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4694 mch_memmove(&(l->t[listidx]),
4695 &(l->t[l->n - 1]),
4696 sizeof(nfa_thread_T) * count);
4697 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004698 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004699 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004700 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004701}
4702
4703/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004704 * Check character class "class" against current character c.
4705 */
4706 static int
4707check_char_class(class, c)
4708 int class;
4709 int c;
4710{
4711 switch (class)
4712 {
4713 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004714 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004715 return OK;
4716 break;
4717 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004718 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004719 return OK;
4720 break;
4721 case NFA_CLASS_BLANK:
4722 if (c == ' ' || c == '\t')
4723 return OK;
4724 break;
4725 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004726 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004727 return OK;
4728 break;
4729 case NFA_CLASS_DIGIT:
4730 if (VIM_ISDIGIT(c))
4731 return OK;
4732 break;
4733 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004734 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004735 return OK;
4736 break;
4737 case NFA_CLASS_LOWER:
4738 if (MB_ISLOWER(c))
4739 return OK;
4740 break;
4741 case NFA_CLASS_PRINT:
4742 if (vim_isprintc(c))
4743 return OK;
4744 break;
4745 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004746 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004747 return OK;
4748 break;
4749 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004750 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004751 return OK;
4752 break;
4753 case NFA_CLASS_UPPER:
4754 if (MB_ISUPPER(c))
4755 return OK;
4756 break;
4757 case NFA_CLASS_XDIGIT:
4758 if (vim_isxdigit(c))
4759 return OK;
4760 break;
4761 case NFA_CLASS_TAB:
4762 if (c == '\t')
4763 return OK;
4764 break;
4765 case NFA_CLASS_RETURN:
4766 if (c == '\r')
4767 return OK;
4768 break;
4769 case NFA_CLASS_BACKSPACE:
4770 if (c == '\b')
4771 return OK;
4772 break;
4773 case NFA_CLASS_ESCAPE:
4774 if (c == '\033')
4775 return OK;
4776 break;
4777
4778 default:
4779 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004780 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004781 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004782 }
4783 return FAIL;
4784}
4785
Bram Moolenaar5714b802013-05-28 22:03:20 +02004786/*
4787 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004788 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004789 */
4790 static int
4791match_backref(sub, subidx, bytelen)
4792 regsub_T *sub; /* pointers to subexpressions */
4793 int subidx;
4794 int *bytelen; /* out: length of match in bytes */
4795{
4796 int len;
4797
4798 if (sub->in_use <= subidx)
4799 {
4800retempty:
4801 /* backref was not set, match an empty string */
4802 *bytelen = 0;
4803 return TRUE;
4804 }
4805
4806 if (REG_MULTI)
4807 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004808 if (sub->list.multi[subidx].start.lnum < 0
4809 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004810 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004811 if (sub->list.multi[subidx].start.lnum == reglnum
4812 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004813 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004814 len = sub->list.multi[subidx].end.col
4815 - sub->list.multi[subidx].start.col;
4816 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4817 reginput, &len) == 0)
4818 {
4819 *bytelen = len;
4820 return TRUE;
4821 }
4822 }
4823 else
4824 {
4825 if (match_with_backref(
4826 sub->list.multi[subidx].start.lnum,
4827 sub->list.multi[subidx].start.col,
4828 sub->list.multi[subidx].end.lnum,
4829 sub->list.multi[subidx].end.col,
4830 bytelen) == RA_MATCH)
4831 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004832 }
4833 }
4834 else
4835 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004836 if (sub->list.line[subidx].start == NULL
4837 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004838 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004839 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4840 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004841 {
4842 *bytelen = len;
4843 return TRUE;
4844 }
4845 }
4846 return FALSE;
4847}
4848
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004849#ifdef FEAT_SYN_HL
4850
4851static int match_zref __ARGS((int subidx, int *bytelen));
4852
4853/*
4854 * Check for a match with \z subexpression "subidx".
4855 * Return TRUE if it matches.
4856 */
4857 static int
4858match_zref(subidx, bytelen)
4859 int subidx;
4860 int *bytelen; /* out: length of match in bytes */
4861{
4862 int len;
4863
4864 cleanup_zsubexpr();
4865 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4866 {
4867 /* backref was not set, match an empty string */
4868 *bytelen = 0;
4869 return TRUE;
4870 }
4871
4872 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4873 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4874 {
4875 *bytelen = len;
4876 return TRUE;
4877 }
4878 return FALSE;
4879}
4880#endif
4881
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004882/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004883 * Save list IDs for all NFA states of "prog" into "list".
4884 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004885 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004886 */
4887 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004888nfa_save_listids(prog, list)
4889 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004890 int *list;
4891{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004892 int i;
4893 nfa_state_T *p;
4894
4895 /* Order in the list is reverse, it's a bit faster that way. */
4896 p = &prog->state[0];
4897 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004898 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004899 list[i] = p->lastlist[1];
4900 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004901 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004902 }
4903}
4904
4905/*
4906 * Restore list IDs from "list" to all NFA states.
4907 */
4908 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004909nfa_restore_listids(prog, list)
4910 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004911 int *list;
4912{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004913 int i;
4914 nfa_state_T *p;
4915
4916 p = &prog->state[0];
4917 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004918 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004919 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004920 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004921 }
4922}
4923
Bram Moolenaar423532e2013-05-29 21:14:42 +02004924 static int
4925nfa_re_num_cmp(val, op, pos)
4926 long_u val;
4927 int op;
4928 long_u pos;
4929{
4930 if (op == 1) return pos > val;
4931 if (op == 2) return pos < val;
4932 return val == pos;
4933}
4934
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004935static 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 +02004936static 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 +02004937
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004938/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004939 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004940 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4941 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004942 */
4943 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004944recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004945 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004946 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004947 nfa_regprog_T *prog;
4948 regsubs_T *submatch;
4949 regsubs_T *m;
4950 int **listids;
4951{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004952 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004953 int save_reglnum = reglnum;
4954 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004955 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004956 save_se_T *save_nfa_endp = nfa_endp;
4957 save_se_T endpos;
4958 save_se_T *endposp = NULL;
4959 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004960 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004961
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004962 if (pim != NULL)
4963 {
4964 /* start at the position where the postponed match was */
4965 if (REG_MULTI)
4966 reginput = regline + pim->end.pos.col;
4967 else
4968 reginput = pim->end.ptr;
4969 }
4970
Bram Moolenaardecd9542013-06-07 16:31:50 +02004971 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004972 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4973 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4974 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004975 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004976 /* The recursive match must end at the current position. When "pim" is
4977 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004978 endposp = &endpos;
4979 if (REG_MULTI)
4980 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004981 if (pim == NULL)
4982 {
4983 endpos.se_u.pos.col = (int)(reginput - regline);
4984 endpos.se_u.pos.lnum = reglnum;
4985 }
4986 else
4987 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004988 }
4989 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004990 {
4991 if (pim == NULL)
4992 endpos.se_u.ptr = reginput;
4993 else
4994 endpos.se_u.ptr = pim->end.ptr;
4995 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004996
4997 /* Go back the specified number of bytes, or as far as the
4998 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02004999 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005000 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005001 if (state->val <= 0)
5002 {
5003 if (REG_MULTI)
5004 {
5005 regline = reg_getline(--reglnum);
5006 if (regline == NULL)
5007 /* can't go before the first line */
5008 regline = reg_getline(++reglnum);
5009 }
5010 reginput = regline;
5011 }
5012 else
5013 {
5014 if (REG_MULTI && (int)(reginput - regline) < state->val)
5015 {
5016 /* Not enough bytes in this line, go to end of
5017 * previous line. */
5018 regline = reg_getline(--reglnum);
5019 if (regline == NULL)
5020 {
5021 /* can't go before the first line */
5022 regline = reg_getline(++reglnum);
5023 reginput = regline;
5024 }
5025 else
5026 reginput = regline + STRLEN(regline);
5027 }
5028 if ((int)(reginput - regline) >= state->val)
5029 {
5030 reginput -= state->val;
5031#ifdef FEAT_MBYTE
5032 if (has_mbyte)
5033 reginput -= mb_head_off(regline, reginput);
5034#endif
5035 }
5036 else
5037 reginput = regline;
5038 }
5039 }
5040
Bram Moolenaarf46da702013-06-02 22:37:42 +02005041#ifdef ENABLE_LOG
5042 if (log_fd != stderr)
5043 fclose(log_fd);
5044 log_fd = NULL;
5045#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005046 /* Have to clear the lastlist field of the NFA nodes, so that
5047 * nfa_regmatch() and addstate() can run properly after recursion. */
5048 if (nfa_ll_index == 1)
5049 {
5050 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5051 * values and clear them. */
5052 if (*listids == NULL)
5053 {
5054 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5055 if (*listids == NULL)
5056 {
5057 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5058 return 0;
5059 }
5060 }
5061 nfa_save_listids(prog, *listids);
5062 need_restore = TRUE;
5063 /* any value of nfa_listid will do */
5064 }
5065 else
5066 {
5067 /* First recursive nfa_regmatch() call, switch to the second lastlist
5068 * entry. Make sure nfa_listid is different from a previous recursive
5069 * call, because some states may still have this ID. */
5070 ++nfa_ll_index;
5071 if (nfa_listid <= nfa_alt_listid)
5072 nfa_listid = nfa_alt_listid;
5073 }
5074
5075 /* Call nfa_regmatch() to check if the current concat matches at this
5076 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005077 nfa_endp = endposp;
5078 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005079
5080 if (need_restore)
5081 nfa_restore_listids(prog, *listids);
5082 else
5083 {
5084 --nfa_ll_index;
5085 nfa_alt_listid = nfa_listid;
5086 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005087
5088 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005089 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005090 if (REG_MULTI)
5091 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005092 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005093 nfa_match = save_nfa_match;
5094 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005095 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005096
5097#ifdef ENABLE_LOG
5098 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5099 if (log_fd != NULL)
5100 {
5101 fprintf(log_fd, "****************************\n");
5102 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5103 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5104 fprintf(log_fd, "****************************\n");
5105 }
5106 else
5107 {
5108 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5109 log_fd = stderr;
5110 }
5111#endif
5112
5113 return result;
5114}
5115
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005116static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005117static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005118
5119/*
5120 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005121 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005122 * NFA_ANY: 1
5123 * specific character: 99
5124 */
5125 static int
5126failure_chance(state, depth)
5127 nfa_state_T *state;
5128 int depth;
5129{
5130 int c = state->c;
5131 int l, r;
5132
5133 /* detect looping */
5134 if (depth > 4)
5135 return 1;
5136
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005137 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005138 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005139 case NFA_SPLIT:
5140 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5141 /* avoid recursive stuff */
5142 return 1;
5143 /* two alternatives, use the lowest failure chance */
5144 l = failure_chance(state->out, depth + 1);
5145 r = failure_chance(state->out1, depth + 1);
5146 return l < r ? l : r;
5147
5148 case NFA_ANY:
5149 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005150 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005151
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005152 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005153 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005154 /* empty match works always */
5155 return 0;
5156
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005157 case NFA_START_INVISIBLE:
5158 case NFA_START_INVISIBLE_FIRST:
5159 case NFA_START_INVISIBLE_NEG:
5160 case NFA_START_INVISIBLE_NEG_FIRST:
5161 case NFA_START_INVISIBLE_BEFORE:
5162 case NFA_START_INVISIBLE_BEFORE_FIRST:
5163 case NFA_START_INVISIBLE_BEFORE_NEG:
5164 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5165 case NFA_START_PATTERN:
5166 /* recursive regmatch is expensive, use low failure chance */
5167 return 5;
5168
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005169 case NFA_BOL:
5170 case NFA_EOL:
5171 case NFA_BOF:
5172 case NFA_EOF:
5173 case NFA_NEWL:
5174 return 99;
5175
5176 case NFA_BOW:
5177 case NFA_EOW:
5178 return 90;
5179
5180 case NFA_MOPEN:
5181 case NFA_MOPEN1:
5182 case NFA_MOPEN2:
5183 case NFA_MOPEN3:
5184 case NFA_MOPEN4:
5185 case NFA_MOPEN5:
5186 case NFA_MOPEN6:
5187 case NFA_MOPEN7:
5188 case NFA_MOPEN8:
5189 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005190#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005191 case NFA_ZOPEN:
5192 case NFA_ZOPEN1:
5193 case NFA_ZOPEN2:
5194 case NFA_ZOPEN3:
5195 case NFA_ZOPEN4:
5196 case NFA_ZOPEN5:
5197 case NFA_ZOPEN6:
5198 case NFA_ZOPEN7:
5199 case NFA_ZOPEN8:
5200 case NFA_ZOPEN9:
5201 case NFA_ZCLOSE:
5202 case NFA_ZCLOSE1:
5203 case NFA_ZCLOSE2:
5204 case NFA_ZCLOSE3:
5205 case NFA_ZCLOSE4:
5206 case NFA_ZCLOSE5:
5207 case NFA_ZCLOSE6:
5208 case NFA_ZCLOSE7:
5209 case NFA_ZCLOSE8:
5210 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005211#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005212 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005213 case NFA_MCLOSE1:
5214 case NFA_MCLOSE2:
5215 case NFA_MCLOSE3:
5216 case NFA_MCLOSE4:
5217 case NFA_MCLOSE5:
5218 case NFA_MCLOSE6:
5219 case NFA_MCLOSE7:
5220 case NFA_MCLOSE8:
5221 case NFA_MCLOSE9:
5222 case NFA_NCLOSE:
5223 return failure_chance(state->out, depth + 1);
5224
5225 case NFA_BACKREF1:
5226 case NFA_BACKREF2:
5227 case NFA_BACKREF3:
5228 case NFA_BACKREF4:
5229 case NFA_BACKREF5:
5230 case NFA_BACKREF6:
5231 case NFA_BACKREF7:
5232 case NFA_BACKREF8:
5233 case NFA_BACKREF9:
5234#ifdef FEAT_SYN_HL
5235 case NFA_ZREF1:
5236 case NFA_ZREF2:
5237 case NFA_ZREF3:
5238 case NFA_ZREF4:
5239 case NFA_ZREF5:
5240 case NFA_ZREF6:
5241 case NFA_ZREF7:
5242 case NFA_ZREF8:
5243 case NFA_ZREF9:
5244#endif
5245 /* backreferences don't match in many places */
5246 return 94;
5247
5248 case NFA_LNUM_GT:
5249 case NFA_LNUM_LT:
5250 case NFA_COL_GT:
5251 case NFA_COL_LT:
5252 case NFA_VCOL_GT:
5253 case NFA_VCOL_LT:
5254 case NFA_MARK_GT:
5255 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005256 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005257 /* before/after positions don't match very often */
5258 return 85;
5259
5260 case NFA_LNUM:
5261 return 90;
5262
5263 case NFA_CURSOR:
5264 case NFA_COL:
5265 case NFA_VCOL:
5266 case NFA_MARK:
5267 /* specific positions rarely match */
5268 return 98;
5269
5270 case NFA_COMPOSING:
5271 return 95;
5272
5273 default:
5274 if (c > 0)
5275 /* character match fails often */
5276 return 95;
5277 }
5278
5279 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005280 return 50;
5281}
5282
Bram Moolenaarf46da702013-06-02 22:37:42 +02005283/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005284 * Skip until the char "c" we know a match must start with.
5285 */
5286 static int
5287skip_to_start(c, colp)
5288 int c;
5289 colnr_T *colp;
5290{
5291 char_u *s;
5292
5293 /* Used often, do some work to avoid call overhead. */
5294 if (!ireg_ic
5295#ifdef FEAT_MBYTE
5296 && !has_mbyte
5297#endif
5298 )
5299 s = vim_strbyte(regline + *colp, c);
5300 else
5301 s = cstrchr(regline + *colp, c);
5302 if (s == NULL)
5303 return FAIL;
5304 *colp = (int)(s - regline);
5305 return OK;
5306}
5307
5308/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005309 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005310 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005311 * Returns zero for no match, 1 for a match.
5312 */
5313 static long
5314find_match_text(startcol, regstart, match_text)
5315 colnr_T startcol;
5316 int regstart;
5317 char_u *match_text;
5318{
5319 colnr_T col = startcol;
5320 int c1, c2;
5321 int len1, len2;
5322 int match;
5323
5324 for (;;)
5325 {
5326 match = TRUE;
5327 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5328 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5329 {
5330 c1 = PTR2CHAR(match_text + len1);
5331 c2 = PTR2CHAR(regline + col + len2);
5332 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5333 {
5334 match = FALSE;
5335 break;
5336 }
5337 len2 += MB_CHAR2LEN(c2);
5338 }
5339 if (match
5340#ifdef FEAT_MBYTE
5341 /* check that no composing char follows */
5342 && !(enc_utf8
5343 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5344#endif
5345 )
5346 {
5347 cleanup_subexpr();
5348 if (REG_MULTI)
5349 {
5350 reg_startpos[0].lnum = reglnum;
5351 reg_startpos[0].col = col;
5352 reg_endpos[0].lnum = reglnum;
5353 reg_endpos[0].col = col + len2;
5354 }
5355 else
5356 {
5357 reg_startp[0] = regline + col;
5358 reg_endp[0] = regline + col + len2;
5359 }
5360 return 1L;
5361 }
5362
5363 /* Try finding regstart after the current match. */
5364 col += MB_CHAR2LEN(regstart); /* skip regstart */
5365 if (skip_to_start(regstart, &col) == FAIL)
5366 break;
5367 }
5368 return 0L;
5369}
5370
5371/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005372 * Main matching routine.
5373 *
5374 * Run NFA to determine whether it matches reginput.
5375 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005376 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005377 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005378 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005379 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005380 * Note: Caller must ensure that: start != NULL.
5381 */
5382 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005383nfa_regmatch(prog, start, submatch, m)
5384 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005385 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005386 regsubs_T *submatch;
5387 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005388{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005389 int result;
5390 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005391 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005392 int go_to_nextline = FALSE;
5393 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005394 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005395 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005396 nfa_list_T *thislist;
5397 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005398 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005399 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005400 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005401 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005402 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005403 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005404#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005405 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005406
5407 if (debug == NULL)
5408 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005409 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 return FALSE;
5411 }
5412#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005413 /* Some patterns may take a long time to match, especially when using
5414 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5415 fast_breakcheck();
5416 if (got_int)
5417 return FALSE;
5418
Bram Moolenaar963fee22013-05-26 21:47:28 +02005419 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005420
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005421 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005422 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005423 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005424 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005425 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005426 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005427 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005428 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005429
5430#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005431 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005432 if (log_fd != NULL)
5433 {
5434 fprintf(log_fd, "**********************************\n");
5435 nfa_set_code(start->c);
5436 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5437 abs(start->id), code);
5438 fprintf(log_fd, "**********************************\n");
5439 }
5440 else
5441 {
5442 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5443 log_fd = stderr;
5444 }
5445#endif
5446
5447 thislist = &list[0];
5448 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005449 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005450 nextlist = &list[1];
5451 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005452 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005453#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005454 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005455#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005456 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005457
5458 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5459 * it's the first MOPEN. */
5460 if (toplevel)
5461 {
5462 if (REG_MULTI)
5463 {
5464 m->norm.list.multi[0].start.lnum = reglnum;
5465 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5466 }
5467 else
5468 m->norm.list.line[0].start = reginput;
5469 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005470 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005471 }
5472 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005473 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005474
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005475#define ADD_STATE_IF_MATCH(state) \
5476 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005477 add_state = state->out; \
5478 add_off = clen; \
5479 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005480
5481 /*
5482 * Run for each character.
5483 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005484 for (;;)
5485 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005486 int curc;
5487 int clen;
5488
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005489#ifdef FEAT_MBYTE
5490 if (has_mbyte)
5491 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005492 curc = (*mb_ptr2char)(reginput);
5493 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005494 }
5495 else
5496#endif
5497 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005498 curc = *reginput;
5499 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005500 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005501 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005502 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005503 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005504 go_to_nextline = FALSE;
5505 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005506
5507 /* swap lists */
5508 thislist = &list[flag];
5509 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005510 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005511 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005512 ++nfa_listid;
5513 thislist->id = nfa_listid;
5514 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515
5516#ifdef ENABLE_LOG
5517 fprintf(log_fd, "------------------------------------------\n");
5518 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005519 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005520 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005521 {
5522 int i;
5523
5524 for (i = 0; i < thislist->n; i++)
5525 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5526 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005527 fprintf(log_fd, "\n");
5528#endif
5529
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005530#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531 fprintf(debug, "\n-------------------\n");
5532#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005533 /*
5534 * If the state lists are empty we can stop.
5535 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005536 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005537 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538
5539 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005540 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005541 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005542 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005543
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005544#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005545 nfa_set_code(t->state->c);
5546 fprintf(debug, "%s, ", code);
5547#endif
5548#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005549 {
5550 int col;
5551
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005552 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005553 col = -1;
5554 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005555 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005556 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005557 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005558 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005559 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5560 abs(t->state->id), (int)t->state->c, code, col,
5561 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005562 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005563#endif
5564
5565 /*
5566 * Handle the possible codes of the current state.
5567 * The most important is NFA_MATCH.
5568 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005569 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005570 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005571 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005572 switch (t->state->c)
5573 {
5574 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005575 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005576 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005577 copy_sub(&submatch->norm, &t->subs.norm);
5578#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005579 if (nfa_has_zsubexpr)
5580 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005581#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005583 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005584#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005585 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005586 * states at this position. When the list of states is going
5587 * to be empty quit without advancing, so that "reginput" is
5588 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005589 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005590 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005591 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005592 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005593
5594 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005595 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005596 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005597 /*
5598 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005599 * NFA_START_INVISIBLE_BEFORE node.
5600 * They surround a zero-width group, used with "\@=", "\&",
5601 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005602 * If we got here, it means that the current "invisible" group
5603 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005604 * nfa_regmatch(). For a look-behind match only when it ends
5605 * in the position in "nfa_endp".
5606 * Submatches are stored in *m, and used in the parent call.
5607 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005608#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005609 if (nfa_endp != NULL)
5610 {
5611 if (REG_MULTI)
5612 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5613 (int)reglnum,
5614 (int)nfa_endp->se_u.pos.lnum,
5615 (int)(reginput - regline),
5616 nfa_endp->se_u.pos.col);
5617 else
5618 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5619 (int)(reginput - regline),
5620 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005621 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005622#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005623 /* If "nfa_endp" is set it's only a match if it ends at
5624 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005625 if (nfa_endp != NULL && (REG_MULTI
5626 ? (reglnum != nfa_endp->se_u.pos.lnum
5627 || (int)(reginput - regline)
5628 != nfa_endp->se_u.pos.col)
5629 : reginput != nfa_endp->se_u.ptr))
5630 break;
5631
5632 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005633 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005634 {
5635 copy_sub(&m->norm, &t->subs.norm);
5636#ifdef FEAT_SYN_HL
5637 if (nfa_has_zsubexpr)
5638 copy_sub(&m->synt, &t->subs.synt);
5639#endif
5640 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005641#ifdef ENABLE_LOG
5642 fprintf(log_fd, "Match found:\n");
5643 log_subsexpr(m);
5644#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005645 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005646 /* See comment above at "goto nextchar". */
5647 if (nextlist->n == 0)
5648 clen = 0;
5649 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005650
5651 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005652 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005653 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005654 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005655 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005656 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005657 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005658 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005659 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005660#ifdef ENABLE_LOG
5661 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5662 failure_chance(t->state->out, 0),
5663 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005664#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005665 /* Do it directly if there already is a PIM or when
5666 * nfa_postprocess() detected it will work better. */
5667 if (t->pim.result != NFA_PIM_UNUSED
5668 || t->state->c == NFA_START_INVISIBLE_FIRST
5669 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5670 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5671 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005672 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005673 int in_use = m->norm.in_use;
5674
Bram Moolenaar699c1202013-09-25 16:41:54 +02005675 /* Copy submatch info for the recursive call, opposite
5676 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005677 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005678#ifdef FEAT_SYN_HL
5679 if (nfa_has_zsubexpr)
5680 copy_sub_off(&m->synt, &t->subs.synt);
5681#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005682
Bram Moolenaara2d95102013-06-04 14:23:05 +02005683 /*
5684 * First try matching the invisible match, then what
5685 * follows.
5686 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005687 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005688 submatch, m, &listids);
5689
Bram Moolenaardecd9542013-06-07 16:31:50 +02005690 /* for \@! and \@<! it is a match when the result is
5691 * FALSE */
5692 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005693 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5694 || t->state->c
5695 == NFA_START_INVISIBLE_BEFORE_NEG
5696 || t->state->c
5697 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005698 {
5699 /* Copy submatch info from the recursive call */
5700 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005701#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005702 if (nfa_has_zsubexpr)
5703 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005704#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005705 /* If the pattern has \ze and it matched in the
5706 * sub pattern, use it. */
5707 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005708
Bram Moolenaara2d95102013-06-04 14:23:05 +02005709 /* t->state->out1 is the corresponding
5710 * END_INVISIBLE node; Add its out to the current
5711 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005712 add_here = TRUE;
5713 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005714 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005715 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005716 }
5717 else
5718 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005719 nfa_pim_T pim;
5720
Bram Moolenaara2d95102013-06-04 14:23:05 +02005721 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005722 * First try matching what follows. Only if a match
5723 * is found verify the invisible match matches. Add a
5724 * nfa_pim_T to the following states, it contains info
5725 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005726 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005727 pim.state = t->state;
5728 pim.result = NFA_PIM_TODO;
5729 pim.subs.norm.in_use = 0;
5730#ifdef FEAT_SYN_HL
5731 pim.subs.synt.in_use = 0;
5732#endif
5733 if (REG_MULTI)
5734 {
5735 pim.end.pos.col = (int)(reginput - regline);
5736 pim.end.pos.lnum = reglnum;
5737 }
5738 else
5739 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005740
5741 /* t->state->out1 is the corresponding END_INVISIBLE
5742 * node; Add its out to the current list (zero-width
5743 * match). */
5744 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005745 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005746 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005747 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005748 break;
5749
Bram Moolenaar87953742013-06-05 18:52:40 +02005750 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005751 {
5752 nfa_state_T *skip = NULL;
5753#ifdef ENABLE_LOG
5754 int skip_lid = 0;
5755#endif
5756
5757 /* There is no point in trying to match the pattern if the
5758 * output state is not going to be added to the list. */
5759 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5760 {
5761 skip = t->state->out1->out;
5762#ifdef ENABLE_LOG
5763 skip_lid = nextlist->id;
5764#endif
5765 }
5766 else if (state_in_list(nextlist,
5767 t->state->out1->out->out, &t->subs))
5768 {
5769 skip = t->state->out1->out->out;
5770#ifdef ENABLE_LOG
5771 skip_lid = nextlist->id;
5772#endif
5773 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005774 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005775 t->state->out1->out->out, &t->subs))
5776 {
5777 skip = t->state->out1->out->out;
5778#ifdef ENABLE_LOG
5779 skip_lid = thislist->id;
5780#endif
5781 }
5782 if (skip != NULL)
5783 {
5784#ifdef ENABLE_LOG
5785 nfa_set_code(skip->c);
5786 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5787 abs(skip->id), skip_lid, skip->c, code);
5788#endif
5789 break;
5790 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005791 /* Copy submatch info to the recursive call, opposite of what
5792 * happens afterwards. */
5793 copy_sub_off(&m->norm, &t->subs.norm);
5794#ifdef FEAT_SYN_HL
5795 if (nfa_has_zsubexpr)
5796 copy_sub_off(&m->synt, &t->subs.synt);
5797#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005798
Bram Moolenaar87953742013-06-05 18:52:40 +02005799 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005800 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005801 submatch, m, &listids);
5802 if (result)
5803 {
5804 int bytelen;
5805
5806#ifdef ENABLE_LOG
5807 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5808 log_subsexpr(m);
5809#endif
5810 /* Copy submatch info from the recursive call */
5811 copy_sub_off(&t->subs.norm, &m->norm);
5812#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005813 if (nfa_has_zsubexpr)
5814 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005815#endif
5816 /* Now we need to skip over the matched text and then
5817 * continue with what follows. */
5818 if (REG_MULTI)
5819 /* TODO: multi-line match */
5820 bytelen = m->norm.list.multi[0].end.col
5821 - (int)(reginput - regline);
5822 else
5823 bytelen = (int)(m->norm.list.line[0].end - reginput);
5824
5825#ifdef ENABLE_LOG
5826 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5827#endif
5828 if (bytelen == 0)
5829 {
5830 /* empty match, output of corresponding
5831 * NFA_END_PATTERN/NFA_SKIP to be used at current
5832 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005833 add_here = TRUE;
5834 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005835 }
5836 else if (bytelen <= clen)
5837 {
5838 /* match current character, output of corresponding
5839 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005840 add_state = t->state->out1->out->out;
5841 add_off = clen;
5842 }
5843 else
5844 {
5845 /* skip over the matched characters, set character
5846 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005847 add_state = t->state->out1->out;
5848 add_off = bytelen;
5849 add_count = bytelen - clen;
5850 }
5851 }
5852 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005853 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005854
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005855 case NFA_BOL:
5856 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005857 {
5858 add_here = TRUE;
5859 add_state = t->state->out;
5860 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005861 break;
5862
5863 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005864 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005865 {
5866 add_here = TRUE;
5867 add_state = t->state->out;
5868 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005869 break;
5870
5871 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005872 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005873
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005874 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005875 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005876#ifdef FEAT_MBYTE
5877 else if (has_mbyte)
5878 {
5879 int this_class;
5880
5881 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005882 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005883 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005884 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005885 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005886 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005887 }
5888#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005889 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005890 || (reginput > regline
5891 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005892 result = FALSE;
5893 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005894 {
5895 add_here = TRUE;
5896 add_state = t->state->out;
5897 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005898 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005899
5900 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005901 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005902 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005903 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005904#ifdef FEAT_MBYTE
5905 else if (has_mbyte)
5906 {
5907 int this_class, prev_class;
5908
5909 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005910 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005911 prev_class = reg_prev_class();
5912 if (this_class == prev_class
5913 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005914 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005915 }
5916#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005917 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005918 || (reginput[0] != NUL
5919 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005920 result = FALSE;
5921 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005922 {
5923 add_here = TRUE;
5924 add_state = t->state->out;
5925 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005926 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005927
Bram Moolenaar4b780632013-05-31 22:14:52 +02005928 case NFA_BOF:
5929 if (reglnum == 0 && reginput == regline
5930 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005931 {
5932 add_here = TRUE;
5933 add_state = t->state->out;
5934 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005935 break;
5936
5937 case NFA_EOF:
5938 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005939 {
5940 add_here = TRUE;
5941 add_state = t->state->out;
5942 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005943 break;
5944
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005945#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005946 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005947 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005948 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005949 int len = 0;
5950 nfa_state_T *end;
5951 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005952 int cchars[MAX_MCO];
5953 int ccount = 0;
5954 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005955
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005956 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005957 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005958 if (utf_iscomposing(sta->c))
5959 {
5960 /* Only match composing character(s), ignore base
5961 * character. Used for ".{composing}" and "{composing}"
5962 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005963 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005964 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005965 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005966 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005967 /* If \Z was present, then ignore composing characters.
5968 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005969 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005970 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005971 else
5972 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005973 while (sta->c != NFA_END_COMPOSING)
5974 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005975 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005976
5977 /* Check base character matches first, unless ignored. */
5978 else if (len > 0 || mc == sta->c)
5979 {
5980 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005981 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005982 len += mb_char2len(mc);
5983 sta = sta->out;
5984 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005985
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005986 /* We don't care about the order of composing characters.
5987 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005988 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005989 {
5990 mc = mb_ptr2char(reginput + len);
5991 cchars[ccount++] = mc;
5992 len += mb_char2len(mc);
5993 if (ccount == MAX_MCO)
5994 break;
5995 }
5996
5997 /* Check that each composing char in the pattern matches a
5998 * composing char in the text. We do not check if all
5999 * composing chars are matched. */
6000 result = OK;
6001 while (sta->c != NFA_END_COMPOSING)
6002 {
6003 for (j = 0; j < ccount; ++j)
6004 if (cchars[j] == sta->c)
6005 break;
6006 if (j == ccount)
6007 {
6008 result = FAIL;
6009 break;
6010 }
6011 sta = sta->out;
6012 }
6013 }
6014 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006015 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006016
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006017 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006018 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006019 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006020 }
6021#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006022
6023 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006024 if (curc == NUL && !reg_line_lbr && REG_MULTI
6025 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006026 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006027 go_to_nextline = TRUE;
6028 /* Pass -1 for the offset, which means taking the position
6029 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006030 add_state = t->state->out;
6031 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006032 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006033 else if (curc == '\n' && reg_line_lbr)
6034 {
6035 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006036 add_state = t->state->out;
6037 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006038 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006039 break;
6040
Bram Moolenaar417bad22013-06-07 14:08:30 +02006041 case NFA_START_COLL:
6042 case NFA_START_NEG_COLL:
6043 {
6044 /* What follows is a list of characters, until NFA_END_COLL.
6045 * One of them must match or none of them must match. */
6046 nfa_state_T *state;
6047 int result_if_matched;
6048 int c1, c2;
6049
6050 /* Never match EOL. If it's part of the collection it is added
6051 * as a separate state with an OR. */
6052 if (curc == NUL)
6053 break;
6054
6055 state = t->state->out;
6056 result_if_matched = (t->state->c == NFA_START_COLL);
6057 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006058 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006059 if (state->c == NFA_END_COLL)
6060 {
6061 result = !result_if_matched;
6062 break;
6063 }
6064 if (state->c == NFA_RANGE_MIN)
6065 {
6066 c1 = state->val;
6067 state = state->out; /* advance to NFA_RANGE_MAX */
6068 c2 = state->val;
6069#ifdef ENABLE_LOG
6070 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6071 curc, c1, c2);
6072#endif
6073 if (curc >= c1 && curc <= c2)
6074 {
6075 result = result_if_matched;
6076 break;
6077 }
6078 if (ireg_ic)
6079 {
6080 int curc_low = MB_TOLOWER(curc);
6081 int done = FALSE;
6082
6083 for ( ; c1 <= c2; ++c1)
6084 if (MB_TOLOWER(c1) == curc_low)
6085 {
6086 result = result_if_matched;
6087 done = TRUE;
6088 break;
6089 }
6090 if (done)
6091 break;
6092 }
6093 }
6094 else if (state->c < 0 ? check_char_class(state->c, curc)
6095 : (curc == state->c
6096 || (ireg_ic && MB_TOLOWER(curc)
6097 == MB_TOLOWER(state->c))))
6098 {
6099 result = result_if_matched;
6100 break;
6101 }
6102 state = state->out;
6103 }
6104 if (result)
6105 {
6106 /* next state is in out of the NFA_END_COLL, out1 of
6107 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006108 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006109 add_off = clen;
6110 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006111 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006112 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006113
6114 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006115 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006116 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006117 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006118 add_state = t->state->out;
6119 add_off = clen;
6120 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006121 break;
6122
6123 /*
6124 * Character classes like \a for alpha, \d for digit etc.
6125 */
6126 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006127 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006128 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006129 break;
6130
6131 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006132 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006133 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006134 break;
6135
6136 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006137 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006138 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006139 break;
6140
6141 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006142 result = !VIM_ISDIGIT(curc)
6143 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006144 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006145 break;
6146
6147 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006148 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006149 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006150 break;
6151
6152 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006153 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006154 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006155 break;
6156
6157 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006158 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006159 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006160 break;
6161
6162 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006163 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006164 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006165 break;
6166
6167 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006168 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006169 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006170 break;
6171
6172 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006173 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006174 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006175 break;
6176
6177 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006178 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006179 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006180 break;
6181
6182 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006183 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006184 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006185 break;
6186
6187 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006188 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006189 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006190 break;
6191
6192 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006193 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006194 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006195 break;
6196
6197 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006198 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006199 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006200 break;
6201
6202 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006203 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006204 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006205 break;
6206
6207 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006208 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006209 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006210 break;
6211
6212 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006213 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006214 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006215 break;
6216
6217 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006218 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006219 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006220 break;
6221
6222 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006223 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006224 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006225 break;
6226
6227 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006228 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006229 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006230 break;
6231
6232 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006233 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006234 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006235 break;
6236
6237 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006238 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006239 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006240 break;
6241
6242 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006243 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006244 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006245 break;
6246
6247 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006248 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006249 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006250 break;
6251
6252 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006253 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006254 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006255 break;
6256
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006257 case NFA_LOWER_IC: /* [a-z] */
6258 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6259 ADD_STATE_IF_MATCH(t->state);
6260 break;
6261
6262 case NFA_NLOWER_IC: /* [^a-z] */
6263 result = curc != NUL
6264 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6265 ADD_STATE_IF_MATCH(t->state);
6266 break;
6267
6268 case NFA_UPPER_IC: /* [A-Z] */
6269 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6270 ADD_STATE_IF_MATCH(t->state);
6271 break;
6272
6273 case NFA_NUPPER_IC: /* ^[A-Z] */
6274 result = curc != NUL
6275 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6276 ADD_STATE_IF_MATCH(t->state);
6277 break;
6278
Bram Moolenaar5714b802013-05-28 22:03:20 +02006279 case NFA_BACKREF1:
6280 case NFA_BACKREF2:
6281 case NFA_BACKREF3:
6282 case NFA_BACKREF4:
6283 case NFA_BACKREF5:
6284 case NFA_BACKREF6:
6285 case NFA_BACKREF7:
6286 case NFA_BACKREF8:
6287 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006288#ifdef FEAT_SYN_HL
6289 case NFA_ZREF1:
6290 case NFA_ZREF2:
6291 case NFA_ZREF3:
6292 case NFA_ZREF4:
6293 case NFA_ZREF5:
6294 case NFA_ZREF6:
6295 case NFA_ZREF7:
6296 case NFA_ZREF8:
6297 case NFA_ZREF9:
6298#endif
6299 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006300 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006301 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006302 int bytelen;
6303
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006304 if (t->state->c <= NFA_BACKREF9)
6305 {
6306 subidx = t->state->c - NFA_BACKREF1 + 1;
6307 result = match_backref(&t->subs.norm, subidx, &bytelen);
6308 }
6309#ifdef FEAT_SYN_HL
6310 else
6311 {
6312 subidx = t->state->c - NFA_ZREF1 + 1;
6313 result = match_zref(subidx, &bytelen);
6314 }
6315#endif
6316
Bram Moolenaar5714b802013-05-28 22:03:20 +02006317 if (result)
6318 {
6319 if (bytelen == 0)
6320 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006321 /* empty match always works, output of NFA_SKIP to be
6322 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006323 add_here = TRUE;
6324 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006325 }
6326 else if (bytelen <= clen)
6327 {
6328 /* match current character, jump ahead to out of
6329 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006330 add_state = t->state->out->out;
6331 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006332 }
6333 else
6334 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006335 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006336 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006337 add_state = t->state->out;
6338 add_off = bytelen;
6339 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006340 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006341 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006342 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006343 }
6344 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006345 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006346 if (t->count - clen <= 0)
6347 {
6348 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006349 add_state = t->state->out;
6350 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006351 }
6352 else
6353 {
6354 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006355 add_state = t->state;
6356 add_off = 0;
6357 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006358 }
6359 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006360
Bram Moolenaar423532e2013-05-29 21:14:42 +02006361 case NFA_LNUM:
6362 case NFA_LNUM_GT:
6363 case NFA_LNUM_LT:
6364 result = (REG_MULTI &&
6365 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6366 (long_u)(reglnum + reg_firstlnum)));
6367 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006368 {
6369 add_here = TRUE;
6370 add_state = t->state->out;
6371 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006372 break;
6373
6374 case NFA_COL:
6375 case NFA_COL_GT:
6376 case NFA_COL_LT:
6377 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6378 (long_u)(reginput - regline) + 1);
6379 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006380 {
6381 add_here = TRUE;
6382 add_state = t->state->out;
6383 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006384 break;
6385
6386 case NFA_VCOL:
6387 case NFA_VCOL_GT:
6388 case NFA_VCOL_LT:
6389 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
6390 (long_u)win_linetabsize(
6391 reg_win == NULL ? curwin : reg_win,
6392 regline, (colnr_T)(reginput - regline)) + 1);
6393 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006394 {
6395 add_here = TRUE;
6396 add_state = t->state->out;
6397 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006398 break;
6399
Bram Moolenaar044aa292013-06-04 21:27:38 +02006400 case NFA_MARK:
6401 case NFA_MARK_GT:
6402 case NFA_MARK_LT:
6403 {
6404 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6405
6406 /* Compare the mark position to the match position. */
6407 result = (pos != NULL /* mark doesn't exist */
6408 && pos->lnum > 0 /* mark isn't set in reg_buf */
6409 && (pos->lnum == reglnum + reg_firstlnum
6410 ? (pos->col == (colnr_T)(reginput - regline)
6411 ? t->state->c == NFA_MARK
6412 : (pos->col < (colnr_T)(reginput - regline)
6413 ? t->state->c == NFA_MARK_GT
6414 : t->state->c == NFA_MARK_LT))
6415 : (pos->lnum < reglnum + reg_firstlnum
6416 ? t->state->c == NFA_MARK_GT
6417 : t->state->c == NFA_MARK_LT)));
6418 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006419 {
6420 add_here = TRUE;
6421 add_state = t->state->out;
6422 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006423 break;
6424 }
6425
Bram Moolenaar423532e2013-05-29 21:14:42 +02006426 case NFA_CURSOR:
6427 result = (reg_win != NULL
6428 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6429 && ((colnr_T)(reginput - regline)
6430 == reg_win->w_cursor.col));
6431 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006432 {
6433 add_here = TRUE;
6434 add_state = t->state->out;
6435 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006436 break;
6437
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006438 case NFA_VISUAL:
6439 result = reg_match_visual();
6440 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006441 {
6442 add_here = TRUE;
6443 add_state = t->state->out;
6444 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006445 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006446
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006447 case NFA_MOPEN1:
6448 case NFA_MOPEN2:
6449 case NFA_MOPEN3:
6450 case NFA_MOPEN4:
6451 case NFA_MOPEN5:
6452 case NFA_MOPEN6:
6453 case NFA_MOPEN7:
6454 case NFA_MOPEN8:
6455 case NFA_MOPEN9:
6456#ifdef FEAT_SYN_HL
6457 case NFA_ZOPEN:
6458 case NFA_ZOPEN1:
6459 case NFA_ZOPEN2:
6460 case NFA_ZOPEN3:
6461 case NFA_ZOPEN4:
6462 case NFA_ZOPEN5:
6463 case NFA_ZOPEN6:
6464 case NFA_ZOPEN7:
6465 case NFA_ZOPEN8:
6466 case NFA_ZOPEN9:
6467#endif
6468 case NFA_NOPEN:
6469 case NFA_ZSTART:
6470 /* These states are only added to be able to bail out when
6471 * they are added again, nothing is to be done. */
6472 break;
6473
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006474 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006475 {
6476 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006477
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006478#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006479 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006480 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006481#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006482 result = (c == curc);
6483
6484 if (!result && ireg_ic)
6485 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006486#ifdef FEAT_MBYTE
6487 /* If there is a composing character which is not being
6488 * ignored there can be no match. Match with composing
6489 * character uses NFA_COMPOSING above. */
6490 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006491 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006492 result = FALSE;
6493#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006494 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006495 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006496 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006497
6498 } /* switch (t->state->c) */
6499
6500 if (add_state != NULL)
6501 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006502 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006503 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006504
6505 if (t->pim.result == NFA_PIM_UNUSED)
6506 pim = NULL;
6507 else
6508 pim = &t->pim;
6509
6510 /* Handle the postponed invisible match if the match might end
6511 * without advancing and before the end of the line. */
6512 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006513 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006514 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006515 {
6516#ifdef ENABLE_LOG
6517 fprintf(log_fd, "\n");
6518 fprintf(log_fd, "==================================\n");
6519 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6520 fprintf(log_fd, "\n");
6521#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006522 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006523 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006524 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006525 /* for \@! and \@<! it is a match when the result is
6526 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006527 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006528 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6529 || pim->state->c
6530 == NFA_START_INVISIBLE_BEFORE_NEG
6531 || pim->state->c
6532 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006533 {
6534 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006535 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006536#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006537 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006538 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006539#endif
6540 }
6541 }
6542 else
6543 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006544 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006545#ifdef ENABLE_LOG
6546 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006547 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006548 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6549 fprintf(log_fd, "\n");
6550#endif
6551 }
6552
Bram Moolenaardecd9542013-06-07 16:31:50 +02006553 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006554 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006555 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6556 || pim->state->c
6557 == NFA_START_INVISIBLE_BEFORE_NEG
6558 || pim->state->c
6559 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006560 {
6561 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006562 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006563#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006564 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006565 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006566#endif
6567 }
6568 else
6569 /* look-behind match failed, don't add the state */
6570 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006571
6572 /* Postponed invisible match was handled, don't add it to
6573 * following states. */
6574 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006575 }
6576
Bram Moolenaara951e352013-10-06 15:46:11 +02006577 /* If "pim" points into l->t it will become invalid when
6578 * adding the state causes the list to be reallocated. Make a
6579 * local copy to avoid that. */
6580 if (pim == &t->pim)
6581 {
6582 copy_pim(&pim_copy, pim);
6583 pim = &pim_copy;
6584 }
6585
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006586 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006587 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006588 else
6589 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006590 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006591 if (add_count > 0)
6592 nextlist->t[nextlist->n - 1].count = add_count;
6593 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006594 }
6595
6596 } /* for (thislist = thislist; thislist->state; thislist++) */
6597
Bram Moolenaare23febd2013-05-26 18:40:14 +02006598 /* Look for the start of a match in the current position by adding the
6599 * start state to the list of states.
6600 * The first found match is the leftmost one, thus the order of states
6601 * matters!
6602 * Do not add the start state in recursive calls of nfa_regmatch(),
6603 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006604 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006605 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006606 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006607 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006608 && reglnum == 0
6609 && clen != 0
6610 && (ireg_maxcol == 0
6611 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006612 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006613 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006614 ? (reglnum < nfa_endp->se_u.pos.lnum
6615 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006616 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006617 < nfa_endp->se_u.pos.col))
6618 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006619 {
6620#ifdef ENABLE_LOG
6621 fprintf(log_fd, "(---) STARTSTATE\n");
6622#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006623 /* Inline optimized code for addstate() if we know the state is
6624 * the first MOPEN. */
6625 if (toplevel)
6626 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006627 int add = TRUE;
6628 int c;
6629
6630 if (prog->regstart != NUL && clen != 0)
6631 {
6632 if (nextlist->n == 0)
6633 {
6634 colnr_T col = (colnr_T)(reginput - regline) + clen;
6635
6636 /* Nextlist is empty, we can skip ahead to the
6637 * character that must appear at the start. */
6638 if (skip_to_start(prog->regstart, &col) == FAIL)
6639 break;
6640#ifdef ENABLE_LOG
6641 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6642 col - ((colnr_T)(reginput - regline) + clen));
6643#endif
6644 reginput = regline + col - clen;
6645 }
6646 else
6647 {
6648 /* Checking if the required start character matches is
6649 * cheaper than adding a state that won't match. */
6650 c = PTR2CHAR(reginput + clen);
6651 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6652 != MB_TOLOWER(prog->regstart)))
6653 {
6654#ifdef ENABLE_LOG
6655 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6656#endif
6657 add = FALSE;
6658 }
6659 }
6660 }
6661
6662 if (add)
6663 {
6664 if (REG_MULTI)
6665 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006666 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006667 else
6668 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006669 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006670 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006671 }
6672 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006673 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006674 }
6675
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006676#ifdef ENABLE_LOG
6677 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006678 {
6679 int i;
6680
6681 for (i = 0; i < thislist->n; i++)
6682 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6683 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006684 fprintf(log_fd, "\n");
6685#endif
6686
6687nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006688 /* Advance to the next character, or advance to the next line, or
6689 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006690 if (clen != 0)
6691 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006692 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6693 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006694 reg_nextline();
6695 else
6696 break;
6697 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006698
6699#ifdef ENABLE_LOG
6700 if (log_fd != stderr)
6701 fclose(log_fd);
6702 log_fd = NULL;
6703#endif
6704
6705theend:
6706 /* Free memory */
6707 vim_free(list[0].t);
6708 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006709 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006710#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006711#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006712 fclose(debug);
6713#endif
6714
Bram Moolenaar963fee22013-05-26 21:47:28 +02006715 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006716}
6717
6718/*
6719 * Try match of "prog" with at regline["col"].
6720 * Returns 0 for failure, number of lines contained in the match otherwise.
6721 */
6722 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006723nfa_regtry(prog, col)
6724 nfa_regprog_T *prog;
6725 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006726{
6727 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006728 regsubs_T subs, m;
6729 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006730#ifdef ENABLE_LOG
6731 FILE *f;
6732#endif
6733
6734 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006735
6736#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006737 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006738 if (f != NULL)
6739 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006740 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006741#ifdef DEBUG
6742 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6743#endif
6744 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006745 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006746 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006747 fprintf(f, "\n\n");
6748 fclose(f);
6749 }
6750 else
6751 EMSG(_("Could not open temporary log file for writing "));
6752#endif
6753
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006754 clear_sub(&subs.norm);
6755 clear_sub(&m.norm);
6756#ifdef FEAT_SYN_HL
6757 clear_sub(&subs.synt);
6758 clear_sub(&m.synt);
6759#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006760
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006761 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006762 return 0;
6763
6764 cleanup_subexpr();
6765 if (REG_MULTI)
6766 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006767 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006768 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006769 reg_startpos[i] = subs.norm.list.multi[i].start;
6770 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006771 }
6772
6773 if (reg_startpos[0].lnum < 0)
6774 {
6775 reg_startpos[0].lnum = 0;
6776 reg_startpos[0].col = col;
6777 }
6778 if (reg_endpos[0].lnum < 0)
6779 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006780 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006781 reg_endpos[0].lnum = reglnum;
6782 reg_endpos[0].col = (int)(reginput - regline);
6783 }
6784 else
6785 /* Use line number of "\ze". */
6786 reglnum = reg_endpos[0].lnum;
6787 }
6788 else
6789 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006790 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006791 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006792 reg_startp[i] = subs.norm.list.line[i].start;
6793 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006794 }
6795
6796 if (reg_startp[0] == NULL)
6797 reg_startp[0] = regline + col;
6798 if (reg_endp[0] == NULL)
6799 reg_endp[0] = reginput;
6800 }
6801
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006802#ifdef FEAT_SYN_HL
6803 /* Package any found \z(...\) matches for export. Default is none. */
6804 unref_extmatch(re_extmatch_out);
6805 re_extmatch_out = NULL;
6806
6807 if (prog->reghasz == REX_SET)
6808 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006809 cleanup_zsubexpr();
6810 re_extmatch_out = make_extmatch();
6811 for (i = 0; i < subs.synt.in_use; i++)
6812 {
6813 if (REG_MULTI)
6814 {
6815 struct multipos *mpos = &subs.synt.list.multi[i];
6816
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006817 /* Only accept single line matches that are valid. */
6818 if (mpos->start.lnum >= 0
6819 && mpos->start.lnum == mpos->end.lnum
6820 && mpos->end.col >= mpos->start.col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006821 re_extmatch_out->matches[i] =
6822 vim_strnsave(reg_getline(mpos->start.lnum)
6823 + mpos->start.col,
6824 mpos->end.col - mpos->start.col);
6825 }
6826 else
6827 {
6828 struct linepos *lpos = &subs.synt.list.line[i];
6829
6830 if (lpos->start != NULL && lpos->end != NULL)
6831 re_extmatch_out->matches[i] =
6832 vim_strnsave(lpos->start,
6833 (int)(lpos->end - lpos->start));
6834 }
6835 }
6836 }
6837#endif
6838
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006839 return 1 + reglnum;
6840}
6841
6842/*
6843 * Match a regexp against a string ("line" points to the string) or multiple
6844 * lines ("line" is NULL, use reg_getline()).
6845 *
6846 * Returns 0 for failure, number of lines contained in the match otherwise.
6847 */
6848 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006849nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006850 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006851 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006852{
6853 nfa_regprog_T *prog;
6854 long retval = 0L;
6855 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006856 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006857
6858 if (REG_MULTI)
6859 {
6860 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6861 line = reg_getline((linenr_T)0); /* relative to the cursor */
6862 reg_startpos = reg_mmatch->startpos;
6863 reg_endpos = reg_mmatch->endpos;
6864 }
6865 else
6866 {
6867 prog = (nfa_regprog_T *)reg_match->regprog;
6868 reg_startp = reg_match->startp;
6869 reg_endp = reg_match->endp;
6870 }
6871
6872 /* Be paranoid... */
6873 if (prog == NULL || line == NULL)
6874 {
6875 EMSG(_(e_null));
6876 goto theend;
6877 }
6878
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006879 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6880 if (prog->regflags & RF_ICASE)
6881 ireg_ic = TRUE;
6882 else if (prog->regflags & RF_NOICASE)
6883 ireg_ic = FALSE;
6884
6885#ifdef FEAT_MBYTE
6886 /* If pattern contains "\Z" overrule value of ireg_icombine */
6887 if (prog->regflags & RF_ICOMBINE)
6888 ireg_icombine = TRUE;
6889#endif
6890
6891 regline = line;
6892 reglnum = 0; /* relative to line */
6893
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006894 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006895 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006896 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006897 nfa_listid = 1;
6898 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006899#ifdef DEBUG
6900 nfa_regengine.expr = prog->pattern;
6901#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006902
Bram Moolenaard89616e2013-06-06 18:46:06 +02006903 if (prog->reganch && col > 0)
6904 return 0L;
6905
Bram Moolenaar473de612013-06-08 18:19:48 +02006906 need_clear_subexpr = TRUE;
6907#ifdef FEAT_SYN_HL
6908 /* Clear the external match subpointers if necessary. */
6909 if (prog->reghasz == REX_SET)
6910 {
6911 nfa_has_zsubexpr = TRUE;
6912 need_clear_zsubexpr = TRUE;
6913 }
6914 else
6915 nfa_has_zsubexpr = FALSE;
6916#endif
6917
Bram Moolenaard89616e2013-06-06 18:46:06 +02006918 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006919 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006920 /* Skip ahead until a character we know the match must start with.
6921 * When there is none there is no match. */
6922 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006923 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006924
Bram Moolenaar473de612013-06-08 18:19:48 +02006925 /* If match_text is set it contains the full text that must match.
6926 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006927 if (prog->match_text != NULL
6928#ifdef FEAT_MBYTE
6929 && !ireg_icombine
6930#endif
6931 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006932 return find_match_text(col, prog->regstart, prog->match_text);
6933 }
6934
Bram Moolenaard89616e2013-06-06 18:46:06 +02006935 /* If the start column is past the maximum column: no need to try. */
6936 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6937 goto theend;
6938
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006939 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006940 for (i = 0; i < nstate; ++i)
6941 {
6942 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006943 prog->state[i].lastlist[0] = 0;
6944 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006945 }
6946
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006947 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006948
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006949#ifdef DEBUG
6950 nfa_regengine.expr = NULL;
6951#endif
6952
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006953theend:
6954 return retval;
6955}
6956
6957/*
6958 * Compile a regular expression into internal code for the NFA matcher.
6959 * Returns the program in allocated space. Returns NULL for an error.
6960 */
6961 static regprog_T *
6962nfa_regcomp(expr, re_flags)
6963 char_u *expr;
6964 int re_flags;
6965{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006966 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006967 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006968 int *postfix;
6969
6970 if (expr == NULL)
6971 return NULL;
6972
6973#ifdef DEBUG
6974 nfa_regengine.expr = expr;
6975#endif
6976
6977 init_class_tab();
6978
6979 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6980 return NULL;
6981
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006982 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006983 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006984 postfix = re2post();
6985 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006986 {
6987 /* TODO: only give this error for debugging? */
6988 if (post_ptr >= post_end)
6989 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006990 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006991 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992
6993 /*
6994 * In order to build the NFA, we parse the input regexp twice:
6995 * 1. first pass to count size (so we can allocate space)
6996 * 2. second to emit code
6997 */
6998#ifdef ENABLE_LOG
6999 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007000 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007001
7002 if (f != NULL)
7003 {
7004 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7005 fclose(f);
7006 }
7007 }
7008#endif
7009
7010 /*
7011 * PASS 1
7012 * Count number of NFA states in "nstate". Do not build the NFA.
7013 */
7014 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007015
Bram Moolenaar16619a22013-06-11 18:42:36 +02007016 /* allocate the regprog with space for the compiled regexp */
7017 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007018 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7019 if (prog == NULL)
7020 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007021 state_ptr = prog->state;
7022
7023 /*
7024 * PASS 2
7025 * Build the NFA
7026 */
7027 prog->start = post2nfa(postfix, post_ptr, FALSE);
7028 if (prog->start == NULL)
7029 goto fail;
7030
7031 prog->regflags = regflags;
7032 prog->engine = &nfa_regengine;
7033 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007034 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007035 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007036 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007037
Bram Moolenaara2947e22013-06-11 22:44:09 +02007038 nfa_postprocess(prog);
7039
Bram Moolenaard89616e2013-06-06 18:46:06 +02007040 prog->reganch = nfa_get_reganch(prog->start, 0);
7041 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007042 prog->match_text = nfa_get_match_text(prog->start);
7043
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007044#ifdef ENABLE_LOG
7045 nfa_postfix_dump(expr, OK);
7046 nfa_dump(prog);
7047#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007048#ifdef FEAT_SYN_HL
7049 /* Remember whether this pattern has any \z specials in it. */
7050 prog->reghasz = re_has_z;
7051#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007052#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02007053 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007054 nfa_regengine.expr = NULL;
7055#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007056
7057out:
7058 vim_free(post_start);
7059 post_start = post_ptr = post_end = NULL;
7060 state_ptr = NULL;
7061 return (regprog_T *)prog;
7062
7063fail:
7064 vim_free(prog);
7065 prog = NULL;
7066#ifdef ENABLE_LOG
7067 nfa_postfix_dump(expr, FAIL);
7068#endif
7069#ifdef DEBUG
7070 nfa_regengine.expr = NULL;
7071#endif
7072 goto out;
7073}
7074
Bram Moolenaar473de612013-06-08 18:19:48 +02007075/*
7076 * Free a compiled regexp program, returned by nfa_regcomp().
7077 */
7078 static void
7079nfa_regfree(prog)
7080 regprog_T *prog;
7081{
7082 if (prog != NULL)
7083 {
7084 vim_free(((nfa_regprog_T *)prog)->match_text);
7085#ifdef DEBUG
7086 vim_free(((nfa_regprog_T *)prog)->pattern);
7087#endif
7088 vim_free(prog);
7089 }
7090}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007091
7092/*
7093 * Match a regexp against a string.
7094 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7095 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007096 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007097 *
7098 * Return TRUE if there is a match, FALSE if not.
7099 */
7100 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007101nfa_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007102 regmatch_T *rmp;
7103 char_u *line; /* string to match against */
7104 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007105 int line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007106{
7107 reg_match = rmp;
7108 reg_mmatch = NULL;
7109 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007110 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007111 reg_buf = curbuf;
7112 reg_win = NULL;
7113 ireg_ic = rmp->rm_ic;
7114#ifdef FEAT_MBYTE
7115 ireg_icombine = FALSE;
7116#endif
7117 ireg_maxcol = 0;
7118 return (nfa_regexec_both(line, col) != 0);
7119}
7120
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007121
7122/*
7123 * Match a regexp against multiple lines.
7124 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7125 * Uses curbuf for line count and 'iskeyword'.
7126 *
7127 * Return zero if there is no match. Return number of lines contained in the
7128 * match otherwise.
7129 *
7130 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7131 *
7132 * ! Also NOTE : match may actually be in another line. e.g.:
7133 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7134 *
7135 * +-------------------------+
7136 * |a |
7137 * |b |
7138 * |c |
7139 * | |
7140 * +-------------------------+
7141 *
7142 * then nfa_regexec_multi() returns 3. while the original
7143 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7144 *
7145 * FIXME if this behavior is not compatible.
7146 */
7147 static long
7148nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7149 regmmatch_T *rmp;
7150 win_T *win; /* window in which to search or NULL */
7151 buf_T *buf; /* buffer in which to search */
7152 linenr_T lnum; /* nr of line to start looking for match */
7153 colnr_T col; /* column to start looking for match */
7154 proftime_T *tm UNUSED; /* timeout limit or NULL */
7155{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007156 reg_match = NULL;
7157 reg_mmatch = rmp;
7158 reg_buf = buf;
7159 reg_win = win;
7160 reg_firstlnum = lnum;
7161 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7162 reg_line_lbr = FALSE;
7163 ireg_ic = rmp->rmm_ic;
7164#ifdef FEAT_MBYTE
7165 ireg_icombine = FALSE;
7166#endif
7167 ireg_maxcol = rmp->rmm_maxcol;
7168
Bram Moolenaarf878bf02013-05-21 21:20:20 +02007169 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007170}
7171
7172#ifdef DEBUG
7173# undef ENABLE_LOG
7174#endif