blob: a7fbe7b254f5041f237cac4d7835b28473621c9b [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
Bram Moolenaar699c1202013-09-25 16:41:54 +020039 NFA_EMPTY, /* matches 0-length */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
53 NFA_STAR, /* greedy * (posfix only) */
54 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +020084 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020085 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020086
87 /* The following are used only in the postfix form, not in the NFA */
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
93
Bram Moolenaar5714b802013-05-28 22:03:20 +020094 NFA_BACKREF1, /* \1 */
95 NFA_BACKREF2, /* \2 */
96 NFA_BACKREF3, /* \3 */
97 NFA_BACKREF4, /* \4 */
98 NFA_BACKREF5, /* \5 */
99 NFA_BACKREF6, /* \6 */
100 NFA_BACKREF7, /* \7 */
101 NFA_BACKREF8, /* \8 */
102 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200103#ifdef FEAT_SYN_HL
104 NFA_ZREF1, /* \z1 */
105 NFA_ZREF2, /* \z2 */
106 NFA_ZREF3, /* \z3 */
107 NFA_ZREF4, /* \z4 */
108 NFA_ZREF5, /* \z5 */
109 NFA_ZREF6, /* \z6 */
110 NFA_ZREF7, /* \z7 */
111 NFA_ZREF8, /* \z8 */
112 NFA_ZREF9, /* \z9 */
113#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200114 NFA_SKIP, /* Skip characters */
115
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200116 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200117 NFA_MOPEN1,
118 NFA_MOPEN2,
119 NFA_MOPEN3,
120 NFA_MOPEN4,
121 NFA_MOPEN5,
122 NFA_MOPEN6,
123 NFA_MOPEN7,
124 NFA_MOPEN8,
125 NFA_MOPEN9,
126
127 NFA_MCLOSE,
128 NFA_MCLOSE1,
129 NFA_MCLOSE2,
130 NFA_MCLOSE3,
131 NFA_MCLOSE4,
132 NFA_MCLOSE5,
133 NFA_MCLOSE6,
134 NFA_MCLOSE7,
135 NFA_MCLOSE8,
136 NFA_MCLOSE9,
137
138#ifdef FEAT_SYN_HL
139 NFA_ZOPEN,
140 NFA_ZOPEN1,
141 NFA_ZOPEN2,
142 NFA_ZOPEN3,
143 NFA_ZOPEN4,
144 NFA_ZOPEN5,
145 NFA_ZOPEN6,
146 NFA_ZOPEN7,
147 NFA_ZOPEN8,
148 NFA_ZOPEN9,
149
150 NFA_ZCLOSE,
151 NFA_ZCLOSE1,
152 NFA_ZCLOSE2,
153 NFA_ZCLOSE3,
154 NFA_ZCLOSE4,
155 NFA_ZCLOSE5,
156 NFA_ZCLOSE6,
157 NFA_ZCLOSE7,
158 NFA_ZCLOSE8,
159 NFA_ZCLOSE9,
160#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200161
162 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200163 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200164 NFA_IDENT, /* Match identifier char */
165 NFA_SIDENT, /* Match identifier char but no digit */
166 NFA_KWORD, /* Match keyword char */
167 NFA_SKWORD, /* Match word char but no digit */
168 NFA_FNAME, /* Match file name char */
169 NFA_SFNAME, /* Match file name char but no digit */
170 NFA_PRINT, /* Match printable char */
171 NFA_SPRINT, /* Match printable char but no digit */
172 NFA_WHITE, /* Match whitespace char */
173 NFA_NWHITE, /* Match non-whitespace char */
174 NFA_DIGIT, /* Match digit char */
175 NFA_NDIGIT, /* Match non-digit char */
176 NFA_HEX, /* Match hex char */
177 NFA_NHEX, /* Match non-hex char */
178 NFA_OCTAL, /* Match octal char */
179 NFA_NOCTAL, /* Match non-octal char */
180 NFA_WORD, /* Match word char */
181 NFA_NWORD, /* Match non-word char */
182 NFA_HEAD, /* Match head char */
183 NFA_NHEAD, /* Match non-head char */
184 NFA_ALPHA, /* Match alpha char */
185 NFA_NALPHA, /* Match non-alpha char */
186 NFA_LOWER, /* Match lowercase char */
187 NFA_NLOWER, /* Match non-lowercase char */
188 NFA_UPPER, /* Match uppercase char */
189 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200190 NFA_LOWER_IC, /* Match [a-z] */
191 NFA_NLOWER_IC, /* Match [^a-z] */
192 NFA_UPPER_IC, /* Match [A-Z] */
193 NFA_NUPPER_IC, /* Match [^A-Z] */
194
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200197
198 NFA_CURSOR, /* Match cursor pos */
199 NFA_LNUM, /* Match line number */
200 NFA_LNUM_GT, /* Match > line number */
201 NFA_LNUM_LT, /* Match < line number */
202 NFA_COL, /* Match cursor column */
203 NFA_COL_GT, /* Match > cursor column */
204 NFA_COL_LT, /* Match < cursor column */
205 NFA_VCOL, /* Match cursor virtual column */
206 NFA_VCOL_GT, /* Match > cursor virtual column */
207 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200208 NFA_MARK, /* Match mark */
209 NFA_MARK_GT, /* Match > mark */
210 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200211 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200212
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200213 /* Character classes [:alnum:] etc */
214 NFA_CLASS_ALNUM,
215 NFA_CLASS_ALPHA,
216 NFA_CLASS_BLANK,
217 NFA_CLASS_CNTRL,
218 NFA_CLASS_DIGIT,
219 NFA_CLASS_GRAPH,
220 NFA_CLASS_LOWER,
221 NFA_CLASS_PRINT,
222 NFA_CLASS_PUNCT,
223 NFA_CLASS_SPACE,
224 NFA_CLASS_UPPER,
225 NFA_CLASS_XDIGIT,
226 NFA_CLASS_TAB,
227 NFA_CLASS_RETURN,
228 NFA_CLASS_BACKSPACE,
229 NFA_CLASS_ESCAPE
230};
231
232/* Keep in sync with classchars. */
233static int nfa_classcodes[] = {
234 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
235 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
236 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
237 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
238 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
239 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
240 NFA_UPPER, NFA_NUPPER
241};
242
Bram Moolenaar174a8482013-11-28 14:20:17 +0100243static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
Bram Moolenaar174a8482013-11-28 14:20:17 +0100245static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200247/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200248static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200249
Bram Moolenaar428e9872013-05-30 17:05:39 +0200250/* NFA regexp \1 .. \9 encountered. */
251static int nfa_has_backref;
252
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200253#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200254/* NFA regexp has \z( ), set zsubexpr. */
255static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200256#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200257
Bram Moolenaar963fee22013-05-26 21:47:28 +0200258/* Number of sub expressions actually being used during execution. 1 if only
259 * the whole match (subexpr 0) is used. */
260static int nfa_nsubexpr;
261
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200262static int *post_start; /* holds the postfix form of r.e. */
263static int *post_end;
264static int *post_ptr;
265
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200266static int nstate; /* Number of states in the NFA. Also used when
267 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200268static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200269
Bram Moolenaar307aa162013-06-02 16:34:21 +0200270/* If not NULL match must end at this position */
271static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200272
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200273/* listid is global, so that it increases on recursive calls to
274 * nfa_regmatch(), which means we don't have to clear the lastlist field of
275 * all the states. */
276static int nfa_listid;
277static int nfa_alt_listid;
278
279/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
280static int nfa_ll_index = 0;
281
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200282static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200283static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
284static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200285static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200286static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200287static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200288static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200289static int nfa_regatom __ARGS((void));
290static int nfa_regpiece __ARGS((void));
291static int nfa_regconcat __ARGS((void));
292static int nfa_regbranch __ARGS((void));
293static int nfa_reg __ARGS((int paren));
294#ifdef DEBUG
295static void nfa_set_code __ARGS((int c));
296static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200297static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
298static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200299static void nfa_dump __ARGS((nfa_regprog_T *prog));
300#endif
301static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200302static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +0200303static void st_error __ARGS((int *postfix, int *end, int *p));
304static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200305static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200306static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200307static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200308static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
309static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200310static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200311static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
313static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200314static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaar2af78a12014-04-23 19:06:37 +0200315static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316static 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 +0200317static int match_follows __ARGS((nfa_state_T *startstate, int depth));
318static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200319
320/* helper functions used when doing re2post() ... regatom() parsing */
321#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200322 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200323 return FAIL; \
324 *post_ptr++ = c; \
325 } while (0)
326
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200327/*
328 * Initialize internal variables before NFA compilation.
329 * Return OK on success, FAIL otherwise.
330 */
331 static int
332nfa_regcomp_start(expr, re_flags)
333 char_u *expr;
334 int re_flags; /* see vim_regcomp() */
335{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200336 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200337 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200338
339 nstate = 0;
340 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200341 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200342 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200343
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200344 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200345 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200346 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347
348 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200349 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200350
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200351 post_start = (int *)lalloc(postfix_size, TRUE);
352 if (post_start == NULL)
353 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200355 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200356 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200357 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200358
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200359 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200360 regcomp_start(expr, re_flags);
361
362 return OK;
363}
364
365/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200366 * Figure out if the NFA state list starts with an anchor, must match at start
367 * of the line.
368 */
369 static int
370nfa_get_reganch(start, depth)
371 nfa_state_T *start;
372 int depth;
373{
374 nfa_state_T *p = start;
375
376 if (depth > 4)
377 return 0;
378
379 while (p != NULL)
380 {
381 switch (p->c)
382 {
383 case NFA_BOL:
384 case NFA_BOF:
385 return 1; /* yes! */
386
387 case NFA_ZSTART:
388 case NFA_ZEND:
389 case NFA_CURSOR:
390 case NFA_VISUAL:
391
392 case NFA_MOPEN:
393 case NFA_MOPEN1:
394 case NFA_MOPEN2:
395 case NFA_MOPEN3:
396 case NFA_MOPEN4:
397 case NFA_MOPEN5:
398 case NFA_MOPEN6:
399 case NFA_MOPEN7:
400 case NFA_MOPEN8:
401 case NFA_MOPEN9:
402 case NFA_NOPEN:
403#ifdef FEAT_SYN_HL
404 case NFA_ZOPEN:
405 case NFA_ZOPEN1:
406 case NFA_ZOPEN2:
407 case NFA_ZOPEN3:
408 case NFA_ZOPEN4:
409 case NFA_ZOPEN5:
410 case NFA_ZOPEN6:
411 case NFA_ZOPEN7:
412 case NFA_ZOPEN8:
413 case NFA_ZOPEN9:
414#endif
415 p = p->out;
416 break;
417
418 case NFA_SPLIT:
419 return nfa_get_reganch(p->out, depth + 1)
420 && nfa_get_reganch(p->out1, depth + 1);
421
422 default:
423 return 0; /* noooo */
424 }
425 }
426 return 0;
427}
428
429/*
430 * Figure out if the NFA state list starts with a character which must match
431 * at start of the match.
432 */
433 static int
434nfa_get_regstart(start, depth)
435 nfa_state_T *start;
436 int depth;
437{
438 nfa_state_T *p = start;
439
440 if (depth > 4)
441 return 0;
442
443 while (p != NULL)
444 {
445 switch (p->c)
446 {
447 /* all kinds of zero-width matches */
448 case NFA_BOL:
449 case NFA_BOF:
450 case NFA_BOW:
451 case NFA_EOW:
452 case NFA_ZSTART:
453 case NFA_ZEND:
454 case NFA_CURSOR:
455 case NFA_VISUAL:
456 case NFA_LNUM:
457 case NFA_LNUM_GT:
458 case NFA_LNUM_LT:
459 case NFA_COL:
460 case NFA_COL_GT:
461 case NFA_COL_LT:
462 case NFA_VCOL:
463 case NFA_VCOL_GT:
464 case NFA_VCOL_LT:
465 case NFA_MARK:
466 case NFA_MARK_GT:
467 case NFA_MARK_LT:
468
469 case NFA_MOPEN:
470 case NFA_MOPEN1:
471 case NFA_MOPEN2:
472 case NFA_MOPEN3:
473 case NFA_MOPEN4:
474 case NFA_MOPEN5:
475 case NFA_MOPEN6:
476 case NFA_MOPEN7:
477 case NFA_MOPEN8:
478 case NFA_MOPEN9:
479 case NFA_NOPEN:
480#ifdef FEAT_SYN_HL
481 case NFA_ZOPEN:
482 case NFA_ZOPEN1:
483 case NFA_ZOPEN2:
484 case NFA_ZOPEN3:
485 case NFA_ZOPEN4:
486 case NFA_ZOPEN5:
487 case NFA_ZOPEN6:
488 case NFA_ZOPEN7:
489 case NFA_ZOPEN8:
490 case NFA_ZOPEN9:
491#endif
492 p = p->out;
493 break;
494
495 case NFA_SPLIT:
496 {
497 int c1 = nfa_get_regstart(p->out, depth + 1);
498 int c2 = nfa_get_regstart(p->out1, depth + 1);
499
500 if (c1 == c2)
501 return c1; /* yes! */
502 return 0;
503 }
504
505 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200506 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200507 return p->c; /* yes! */
508 return 0;
509 }
510 }
511 return 0;
512}
513
514/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200515 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200516 * else. If so return a string in allocated memory with what must match after
517 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200518 */
519 static char_u *
520nfa_get_match_text(start)
521 nfa_state_T *start;
522{
523 nfa_state_T *p = start;
524 int len = 0;
525 char_u *ret;
526 char_u *s;
527
528 if (p->c != NFA_MOPEN)
529 return NULL; /* just in case */
530 p = p->out;
531 while (p->c > 0)
532 {
533 len += MB_CHAR2LEN(p->c);
534 p = p->out;
535 }
536 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
537 return NULL;
538
539 ret = alloc(len);
540 if (ret != NULL)
541 {
542 len = 0;
543 p = start->out->out; /* skip first char, it goes into regstart */
544 s = ret;
545 while (p->c > 0)
546 {
547#ifdef FEAT_MBYTE
548 if (has_mbyte)
549 s += (*mb_char2bytes)(p->c, s);
550 else
551#endif
552 *s++ = p->c;
553 p = p->out;
554 }
555 *s = NUL;
556 }
557 return ret;
558}
559
560/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200561 * Allocate more space for post_start. Called when
562 * running above the estimated number of states.
563 */
564 static int
565realloc_post_list()
566{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200567 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200568 int new_max = nstate_max + 1000;
569 int *new_start;
570 int *old_start;
571
572 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
573 if (new_start == NULL)
574 return FAIL;
575 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200576 old_start = post_start;
577 post_start = new_start;
578 post_ptr = new_start + (post_ptr - old_start);
579 post_end = post_start + new_max;
580 vim_free(old_start);
581 return OK;
582}
583
584/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200585 * Search between "start" and "end" and try to recognize a
586 * character class in expanded form. For example [0-9].
587 * On success, return the id the character class to be emitted.
588 * On failure, return 0 (=FAIL)
589 * Start points to the first char of the range, while end should point
590 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200591 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
592 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200593 */
594 static int
595nfa_recognize_char_class(start, end, extra_newl)
596 char_u *start;
597 char_u *end;
598 int extra_newl;
599{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200600# define CLASS_not 0x80
601# define CLASS_af 0x40
602# define CLASS_AF 0x20
603# define CLASS_az 0x10
604# define CLASS_AZ 0x08
605# define CLASS_o7 0x04
606# define CLASS_o9 0x02
607# define CLASS_underscore 0x01
608
609 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200610 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200611 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200612
613 if (extra_newl == TRUE)
614 newl = TRUE;
615
616 if (*end != ']')
617 return FAIL;
618 p = start;
619 if (*p == '^')
620 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200621 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200622 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200623 }
624
625 while (p < end)
626 {
627 if (p + 2 < end && *(p + 1) == '-')
628 {
629 switch (*p)
630 {
631 case '0':
632 if (*(p + 2) == '9')
633 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200634 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200635 break;
636 }
637 else
638 if (*(p + 2) == '7')
639 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200640 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200641 break;
642 }
643 case 'a':
644 if (*(p + 2) == 'z')
645 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200646 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200647 break;
648 }
649 else
650 if (*(p + 2) == 'f')
651 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200652 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200653 break;
654 }
655 case 'A':
656 if (*(p + 2) == 'Z')
657 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200658 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200659 break;
660 }
661 else
662 if (*(p + 2) == 'F')
663 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200664 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200665 break;
666 }
667 /* FALLTHROUGH */
668 default:
669 return FAIL;
670 }
671 p += 3;
672 }
673 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
674 {
675 newl = TRUE;
676 p += 2;
677 }
678 else if (*p == '_')
679 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200680 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200681 p ++;
682 }
683 else if (*p == '\n')
684 {
685 newl = TRUE;
686 p ++;
687 }
688 else
689 return FAIL;
690 } /* while (p < end) */
691
692 if (p != end)
693 return FAIL;
694
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200695 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200696 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200697
698 switch (config)
699 {
700 case CLASS_o9:
701 return extra_newl + NFA_DIGIT;
702 case CLASS_not | CLASS_o9:
703 return extra_newl + NFA_NDIGIT;
704 case CLASS_af | CLASS_AF | CLASS_o9:
705 return extra_newl + NFA_HEX;
706 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
707 return extra_newl + NFA_NHEX;
708 case CLASS_o7:
709 return extra_newl + NFA_OCTAL;
710 case CLASS_not | CLASS_o7:
711 return extra_newl + NFA_NOCTAL;
712 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
713 return extra_newl + NFA_WORD;
714 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
715 return extra_newl + NFA_NWORD;
716 case CLASS_az | CLASS_AZ | CLASS_underscore:
717 return extra_newl + NFA_HEAD;
718 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
719 return extra_newl + NFA_NHEAD;
720 case CLASS_az | CLASS_AZ:
721 return extra_newl + NFA_ALPHA;
722 case CLASS_not | CLASS_az | CLASS_AZ:
723 return extra_newl + NFA_NALPHA;
724 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200725 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200726 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200727 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200728 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200729 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200730 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200731 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200733 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200734}
735
736/*
737 * Produce the bytes for equivalence class "c".
738 * Currently only handles latin1, latin9 and utf-8.
739 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
740 * equivalent to 'a OR b OR c'
741 *
742 * NOTE! When changing this function, also update reg_equi_class()
743 */
744 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200745nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200746 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200747{
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200748#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
749#ifdef FEAT_MBYTE
750# define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT);
751#else
752# define EMITMBC(c)
753#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200754
755#ifdef FEAT_MBYTE
756 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
757 || STRCMP(p_enc, "iso-8859-15") == 0)
758#endif
759 {
760 switch (c)
761 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200762 case 'A': case 0300: case 0301: case 0302:
763 case 0303: case 0304: case 0305:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200764 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
765 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
766 EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302);
767 EMIT2(0303); EMIT2(0304); EMIT2(0305);
768 EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104)
769 EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0)
770 EMITMBC(0x1ea2)
771 return OK;
772
773 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
774 EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200775 return OK;
776
Bram Moolenaar417bad22013-06-07 14:08:30 +0200777 case 'C': case 0307:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200778 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
779 EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108)
780 EMITMBC(0x10a) EMITMBC(0x10c)
781 return OK;
782
783 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
784 CASEMBC(0x1e0e) CASEMBC(0x1e10)
785 EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a)
786 EMITMBC(0x1e0e) EMITMBC(0x1e10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200787 return OK;
788
Bram Moolenaar417bad22013-06-07 14:08:30 +0200789 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200790 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
791 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
792 EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312);
793 EMIT2(0313);
794 EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116)
795 EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba)
796 EMITMBC(0x1ebc)
797 return OK;
798
799 case 'F': CASEMBC(0x1e1e)
800 EMIT2('F'); EMITMBC(0x1e1e)
801 return OK;
802
803 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
804 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
805 CASEMBC(0x1e20)
806 EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120)
807 EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6)
808 EMITMBC(0x1f4) EMITMBC(0x1e20)
809 return OK;
810
811 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
812 CASEMBC(0x1e26) CASEMBC(0x1e28)
813 EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22)
814 EMITMBC(0x1e26) EMITMBC(0x1e28)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200815 return OK;
816
Bram Moolenaar417bad22013-06-07 14:08:30 +0200817 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200818 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
819 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
820 EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316);
821 EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a)
822 EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130)
823 EMITMBC(0x1cf) EMITMBC(0x1ec8)
824 return OK;
825
826 case 'J': CASEMBC(0x134)
827 EMIT2('J'); EMITMBC(0x134)
828 return OK;
829
830 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
831 CASEMBC(0x1e34)
832 EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30)
833 EMITMBC(0x1e34)
834 return OK;
835
836 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
837 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
838 EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d)
839 EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a)
840 return OK;
841
842 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
843 EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200844 return OK;
845
Bram Moolenaar417bad22013-06-07 14:08:30 +0200846 case 'N': case 0321:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200847 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
848 CASEMBC(0x1e48)
849 EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145)
850 EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200851 return OK;
852
Bram Moolenaar417bad22013-06-07 14:08:30 +0200853 case 'O': case 0322: case 0323: case 0324: case 0325:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200854 case 0326: case 0330:
855 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
856 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
857 EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324);
858 EMIT2(0325); EMIT2(0326); EMIT2(0330);
859 EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150)
860 EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea)
861 EMITMBC(0x1ec) EMITMBC(0x1ece)
862 return OK;
863
864 case 'P': case 0x1e54: case 0x1e56:
865 EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56)
866 return OK;
867
868 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
869 CASEMBC(0x1e58) CASEMBC(0x1e5e)
870 EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158)
871 EMITMBC(0x1e58) EMITMBC(0x1e5e)
872 return OK;
873
874 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
875 CASEMBC(0x160) CASEMBC(0x1e60)
876 EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e)
877 EMITMBC(0x160) EMITMBC(0x1e60)
878 return OK;
879
880 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
881 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
882 EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166)
883 EMITMBC(0x1e6a) EMITMBC(0x1e6e)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884 return OK;
885
Bram Moolenaar417bad22013-06-07 14:08:30 +0200886 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200887 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
888 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
889 CASEMBC(0x1ee6)
890 EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333);
891 EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a)
892 EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170)
893 EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3)
894 EMITMBC(0x1ee6)
895 return OK;
896
897 case 'V': CASEMBC(0x1e7c)
898 EMIT2('V'); EMITMBC(0x1e7c)
899 return OK;
900
901 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
902 CASEMBC(0x1e84) CASEMBC(0x1e86)
903 EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82)
904 EMITMBC(0x1e84) EMITMBC(0x1e86)
905 return OK;
906
907 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
908 EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200909 return OK;
910
Bram Moolenaar417bad22013-06-07 14:08:30 +0200911 case 'Y': case 0335:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200912 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
913 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
914 EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178)
915 EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6)
916 EMITMBC(0x1ef8)
917 return OK;
918
919 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
920 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
921 EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d)
922 EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200923 return OK;
924
Bram Moolenaar417bad22013-06-07 14:08:30 +0200925 case 'a': case 0340: case 0341: case 0342:
926 case 0343: case 0344: case 0345:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200927 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
928 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
929 EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342);
930 EMIT2(0343); EMIT2(0344); EMIT2(0345);
931 EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105)
932 EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1)
933 EMITMBC(0x1ea3)
934 return OK;
935
936 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
937 EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200938 return OK;
939
Bram Moolenaar417bad22013-06-07 14:08:30 +0200940 case 'c': case 0347:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200941 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
942 EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109)
943 EMITMBC(0x10b) EMITMBC(0x10d)
944 return OK;
945
946 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
947 CASEMBC(0x1e11)
948 EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) EMITMBC(0x1e0b)
949 EMITMBC(0x01e0f) EMITMBC(0x1e11)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200950 return OK;
951
Bram Moolenaar417bad22013-06-07 14:08:30 +0200952 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200953 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
954 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
955 EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352);
956 EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115)
957 EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b)
958 EMITMBC(0x1ebb) EMITMBC(0x1ebd)
959 return OK;
960
961 case 'f': CASEMBC(0x1e1f)
962 EMIT2('f'); EMITMBC(0x1e1f)
963 return OK;
964
965 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
966 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
967 CASEMBC(0x1e21)
968 EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121)
969 EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7)
970 EMITMBC(0x1f5) EMITMBC(0x1e21)
971 return OK;
972
973 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
974 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
975 EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23)
976 EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200977 return OK;
978
Bram Moolenaar417bad22013-06-07 14:08:30 +0200979 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +0200980 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
981 CASEMBC(0x1d0) CASEMBC(0x1ec9)
982 EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356);
983 EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b)
984 EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0)
985 EMITMBC(0x1ec9)
986 return OK;
987
988 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
989 EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0)
990 return OK;
991
992 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
993 CASEMBC(0x1e35)
994 EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31)
995 EMITMBC(0x1e35)
996 return OK;
997
998 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
999 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1000 EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e)
1001 EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b)
1002 return OK;
1003
1004 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1005 EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001006 return OK;
1007
Bram Moolenaar417bad22013-06-07 14:08:30 +02001008 case 'n': case 0361:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001009 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1010 CASEMBC(0x1e45) CASEMBC(0x1e49)
1011 EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146)
1012 EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45)
1013 EMITMBC(0x1e49)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001014 return OK;
1015
Bram Moolenaar417bad22013-06-07 14:08:30 +02001016 case 'o': case 0362: case 0363: case 0364: case 0365:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001017 case 0366: case 0370:
1018 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1019 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
1020 EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364);
1021 EMIT2(0365); EMIT2(0366); EMIT2(0370);
1022 EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151)
1023 EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb)
1024 EMITMBC(0x1ed) EMITMBC(0x1ecf)
1025 return OK;
1026
1027 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1028 EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57)
1029 return OK;
1030
1031 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1032 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1033 EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159)
1034 EMITMBC(0x1e59) EMITMBC(0x1e5f)
1035 return OK;
1036
1037 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1038 CASEMBC(0x161) CASEMBC(0x1e61)
1039 EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f)
1040 EMITMBC(0x161) EMITMBC(0x1e61)
1041 return OK;
1042
1043 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1044 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1045 EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167)
1046 EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001047 return OK;
1048
Bram Moolenaar417bad22013-06-07 14:08:30 +02001049 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001050 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1051 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1052 CASEMBC(0x1ee7)
1053 EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373);
1054 EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b)
1055 EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171)
1056 EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4)
1057 EMITMBC(0x1ee7)
1058 return OK;
1059
1060 case 'v': CASEMBC(0x1e7d)
1061 EMIT2('v'); EMITMBC(0x1e7d)
1062 return OK;
1063
1064 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1065 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1066 EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83)
1067 EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98)
1068 return OK;
1069
1070 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1071 EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001072 return OK;
1073
Bram Moolenaar417bad22013-06-07 14:08:30 +02001074 case 'y': case 0375: case 0377:
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001075 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1076 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
1077 EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177)
1078 EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3)
1079 EMITMBC(0x1ef7) EMITMBC(0x1ef9)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001080 return OK;
1081
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001082 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1083 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1084 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e)
1085 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95)
1086 return OK;
1087
1088 /* default: character itself */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001089 }
1090 }
1091
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001092 EMIT2(c);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001093 return OK;
1094#undef EMIT2
Bram Moolenaare6a2fa62013-09-19 17:00:20 +02001095#undef EMITMBC
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001096}
1097
1098/*
1099 * Code to parse regular expression.
1100 *
1101 * We try to reuse parsing functions in regexp.c to
1102 * minimize surprise and keep the syntax consistent.
1103 */
1104
1105/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001106 * Parse the lowest level.
1107 *
1108 * An atom can be one of a long list of items. Many atoms match one character
1109 * in the text. It is often an ordinary character or a character class.
1110 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
1111 * is only for syntax highlighting.
1112 *
1113 * atom ::= ordinary-atom
1114 * or \( pattern \)
1115 * or \%( pattern \)
1116 * or \z( pattern \)
1117 */
1118 static int
1119nfa_regatom()
1120{
1121 int c;
1122 int charclass;
1123 int equiclass;
1124 int collclass;
1125 int got_coll_char;
1126 char_u *p;
1127 char_u *endp;
1128#ifdef FEAT_MBYTE
1129 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001130#endif
1131 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001132 int emit_range;
1133 int negated;
1134 int result;
1135 int startc = -1;
1136 int endc = -1;
1137 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001138
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001139 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001140 switch (c)
1141 {
Bram Moolenaar47196582013-05-25 22:04:23 +02001142 case NUL:
Bram Moolenaar174a8482013-11-28 14:20:17 +01001143 EMSG_RET_FAIL(_(e_nul_found));
Bram Moolenaar47196582013-05-25 22:04:23 +02001144
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001145 case Magic('^'):
1146 EMIT(NFA_BOL);
1147 break;
1148
1149 case Magic('$'):
1150 EMIT(NFA_EOL);
1151#if defined(FEAT_SYN_HL) || defined(PROTO)
1152 had_eol = TRUE;
1153#endif
1154 break;
1155
1156 case Magic('<'):
1157 EMIT(NFA_BOW);
1158 break;
1159
1160 case Magic('>'):
1161 EMIT(NFA_EOW);
1162 break;
1163
1164 case Magic('_'):
1165 c = no_Magic(getchr());
Bram Moolenaar174a8482013-11-28 14:20:17 +01001166 if (c == NUL)
1167 EMSG_RET_FAIL(_(e_nul_found));
1168
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001169 if (c == '^') /* "\_^" is start-of-line */
1170 {
1171 EMIT(NFA_BOL);
1172 break;
1173 }
1174 if (c == '$') /* "\_$" is end-of-line */
1175 {
1176 EMIT(NFA_EOL);
1177#if defined(FEAT_SYN_HL) || defined(PROTO)
1178 had_eol = TRUE;
1179#endif
1180 break;
1181 }
1182
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001183 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001184
1185 /* "\_[" is collection plus newline */
1186 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001187 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001188
1189 /* "\_x" is character class plus newline */
1190 /*FALLTHROUGH*/
1191
1192 /*
1193 * Character classes.
1194 */
1195 case Magic('.'):
1196 case Magic('i'):
1197 case Magic('I'):
1198 case Magic('k'):
1199 case Magic('K'):
1200 case Magic('f'):
1201 case Magic('F'):
1202 case Magic('p'):
1203 case Magic('P'):
1204 case Magic('s'):
1205 case Magic('S'):
1206 case Magic('d'):
1207 case Magic('D'):
1208 case Magic('x'):
1209 case Magic('X'):
1210 case Magic('o'):
1211 case Magic('O'):
1212 case Magic('w'):
1213 case Magic('W'):
1214 case Magic('h'):
1215 case Magic('H'):
1216 case Magic('a'):
1217 case Magic('A'):
1218 case Magic('l'):
1219 case Magic('L'):
1220 case Magic('u'):
1221 case Magic('U'):
1222 p = vim_strchr(classchars, no_Magic(c));
1223 if (p == NULL)
1224 {
Bram Moolenaar174a8482013-11-28 14:20:17 +01001225 if (extra == NFA_ADD_NL)
1226 {
1227 EMSGN(_(e_ill_char_class), c);
1228 rc_did_emsg = TRUE;
1229 return FAIL;
1230 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001231 EMSGN("INTERNAL: Unknown character class char: %ld", c);
1232 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001233 }
1234#ifdef FEAT_MBYTE
1235 /* When '.' is followed by a composing char ignore the dot, so that
1236 * the composing char is matched here. */
1237 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1238 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001239 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001240 c = getchr();
1241 goto nfa_do_multibyte;
1242 }
1243#endif
1244 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001245 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246 {
1247 EMIT(NFA_NEWL);
1248 EMIT(NFA_OR);
1249 regflags |= RF_HASNL;
1250 }
1251 break;
1252
1253 case Magic('n'):
1254 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001255 /* In a string "\n" matches a newline character. */
1256 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001257 else
1258 {
1259 /* In buffer text "\n" matches the end of a line. */
1260 EMIT(NFA_NEWL);
1261 regflags |= RF_HASNL;
1262 }
1263 break;
1264
1265 case Magic('('):
1266 if (nfa_reg(REG_PAREN) == FAIL)
1267 return FAIL; /* cascaded error */
1268 break;
1269
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001270 case Magic('|'):
1271 case Magic('&'):
1272 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001273 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001274 return FAIL;
1275
1276 case Magic('='):
1277 case Magic('?'):
1278 case Magic('+'):
1279 case Magic('@'):
1280 case Magic('*'):
1281 case Magic('{'):
1282 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001283 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001284 return FAIL;
1285
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001286 case Magic('~'):
1287 {
1288 char_u *lp;
1289
1290 /* Previous substitute pattern.
1291 * Generated as "\%(pattern\)". */
1292 if (reg_prev_sub == NULL)
1293 {
1294 EMSG(_(e_nopresub));
1295 return FAIL;
1296 }
1297 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1298 {
1299 EMIT(PTR2CHAR(lp));
1300 if (lp != reg_prev_sub)
1301 EMIT(NFA_CONCAT);
1302 }
1303 EMIT(NFA_NOPEN);
1304 break;
1305 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001306
Bram Moolenaar428e9872013-05-30 17:05:39 +02001307 case Magic('1'):
1308 case Magic('2'):
1309 case Magic('3'):
1310 case Magic('4'):
1311 case Magic('5'):
1312 case Magic('6'):
1313 case Magic('7'):
1314 case Magic('8'):
1315 case Magic('9'):
1316 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1317 nfa_has_backref = TRUE;
1318 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001319
1320 case Magic('z'):
1321 c = no_Magic(getchr());
1322 switch (c)
1323 {
1324 case 's':
1325 EMIT(NFA_ZSTART);
1326 break;
1327 case 'e':
1328 EMIT(NFA_ZEND);
1329 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001330 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001331#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001332 case '1':
1333 case '2':
1334 case '3':
1335 case '4':
1336 case '5':
1337 case '6':
1338 case '7':
1339 case '8':
1340 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001341 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001342 if (reg_do_extmatch != REX_USE)
1343 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001344 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1345 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001346 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001347 re_has_z = REX_USE;
1348 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001349 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001350 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001351 if (reg_do_extmatch != REX_SET)
1352 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001353 if (nfa_reg(REG_ZPAREN) == FAIL)
1354 return FAIL; /* cascaded error */
1355 re_has_z = REX_SET;
1356 break;
1357#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001358 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001359 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001360 no_Magic(c));
1361 return FAIL;
1362 }
1363 break;
1364
1365 case Magic('%'):
1366 c = no_Magic(getchr());
1367 switch (c)
1368 {
1369 /* () without a back reference */
1370 case '(':
1371 if (nfa_reg(REG_NPAREN) == FAIL)
1372 return FAIL;
1373 EMIT(NFA_NOPEN);
1374 break;
1375
1376 case 'd': /* %d123 decimal */
1377 case 'o': /* %o123 octal */
1378 case 'x': /* %xab hex 2 */
1379 case 'u': /* %uabcd hex 4 */
1380 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001381 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001382 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001383
Bram Moolenaar47196582013-05-25 22:04:23 +02001384 switch (c)
1385 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001386 case 'd': nr = getdecchrs(); break;
1387 case 'o': nr = getoctchrs(); break;
1388 case 'x': nr = gethexchrs(2); break;
1389 case 'u': nr = gethexchrs(4); break;
1390 case 'U': nr = gethexchrs(8); break;
1391 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001392 }
1393
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001394 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001395 EMSG2_RET_FAIL(
1396 _("E678: Invalid character after %s%%[dxouU]"),
1397 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001398 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001399 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001400 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001401 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001402 break;
1403
1404 /* Catch \%^ and \%$ regardless of where they appear in the
1405 * pattern -- regardless of whether or not it makes sense. */
1406 case '^':
1407 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001408 break;
1409
1410 case '$':
1411 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001412 break;
1413
1414 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001415 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 break;
1417
1418 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001419 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001420 break;
1421
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001422 case 'C':
1423 EMIT(NFA_ANY_COMPOSING);
1424 break;
1425
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001426 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001427 {
1428 int n;
1429
1430 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001431 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001432 {
1433 if (c == NUL)
1434 EMSG2_RET_FAIL(_(e_missing_sb),
1435 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001436 /* recursive call! */
1437 if (nfa_regatom() == FAIL)
1438 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001439 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001440 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001441 if (n == 0)
1442 EMSG2_RET_FAIL(_(e_empty_sb),
1443 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001444 EMIT(NFA_OPT_CHARS);
1445 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001446
1447 /* Emit as "\%(\%[abc]\)" to be able to handle
1448 * "\%[abc]*" which would cause the empty string to be
1449 * matched an unlimited number of times. NFA_NOPEN is
1450 * added only once at a position, while NFA_SPLIT is
1451 * added multiple times. This is more efficient than
1452 * not allowsing NFA_SPLIT multiple times, it is used
1453 * a lot. */
1454 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001455 break;
1456 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001457
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001458 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001459 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001460 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001461 int cmp = c;
1462
1463 if (c == '<' || c == '>')
1464 c = getchr();
1465 while (VIM_ISDIGIT(c))
1466 {
1467 n = n * 10 + (c - '0');
1468 c = getchr();
1469 }
1470 if (c == 'l' || c == 'c' || c == 'v')
1471 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001472 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001473 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001474 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001475 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001476 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001477 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001478 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001479 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001480 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001481 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001482 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001483 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001484 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001485 break;
1486 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001487 else if (c == '\'' && n == 0)
1488 {
1489 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001490 EMIT(cmp == '<' ? NFA_MARK_LT :
1491 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001492 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001493 break;
1494 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001495 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001496 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1497 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001498 return FAIL;
1499 }
1500 break;
1501
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001502 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001503collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001505 * [abc] uses NFA_START_COLL - NFA_END_COLL
1506 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1507 * Each character is produced as a regular state, using
1508 * NFA_CONCAT to bind them together.
1509 * Besides normal characters there can be:
1510 * - character classes NFA_CLASS_*
1511 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001512 */
1513
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001514 p = regparse;
1515 endp = skip_anyof(p);
1516 if (*endp == ']')
1517 {
1518 /*
1519 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001520 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001521 * and perform the necessary substitutions in the NFA.
1522 */
1523 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001524 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001525 if (result != FAIL)
1526 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001527 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001528 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001529 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001530 EMIT(NFA_NEWL);
1531 EMIT(NFA_OR);
1532 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001533 else
1534 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001535 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001536 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537 return OK;
1538 }
1539 /*
1540 * Failed to recognize a character class. Use the simple
1541 * version that turns [abc] into 'a' OR 'b' OR 'c'
1542 */
1543 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001544 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001545 if (*regparse == '^') /* negated range */
1546 {
1547 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001548 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001549 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001550 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001551 else
1552 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001553 if (*regparse == '-')
1554 {
1555 startc = '-';
1556 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001557 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001558 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 }
1560 /* Emit the OR branches for each character in the [] */
1561 emit_range = FALSE;
1562 while (regparse < endp)
1563 {
1564 oldstartc = startc;
1565 startc = -1;
1566 got_coll_char = FALSE;
1567 if (*regparse == '[')
1568 {
1569 /* Check for [: :], [= =], [. .] */
1570 equiclass = collclass = 0;
1571 charclass = get_char_class(&regparse);
1572 if (charclass == CLASS_NONE)
1573 {
1574 equiclass = get_equi_class(&regparse);
1575 if (equiclass == 0)
1576 collclass = get_coll_element(&regparse);
1577 }
1578
1579 /* Character class like [:alpha:] */
1580 if (charclass != CLASS_NONE)
1581 {
1582 switch (charclass)
1583 {
1584 case CLASS_ALNUM:
1585 EMIT(NFA_CLASS_ALNUM);
1586 break;
1587 case CLASS_ALPHA:
1588 EMIT(NFA_CLASS_ALPHA);
1589 break;
1590 case CLASS_BLANK:
1591 EMIT(NFA_CLASS_BLANK);
1592 break;
1593 case CLASS_CNTRL:
1594 EMIT(NFA_CLASS_CNTRL);
1595 break;
1596 case CLASS_DIGIT:
1597 EMIT(NFA_CLASS_DIGIT);
1598 break;
1599 case CLASS_GRAPH:
1600 EMIT(NFA_CLASS_GRAPH);
1601 break;
1602 case CLASS_LOWER:
1603 EMIT(NFA_CLASS_LOWER);
1604 break;
1605 case CLASS_PRINT:
1606 EMIT(NFA_CLASS_PRINT);
1607 break;
1608 case CLASS_PUNCT:
1609 EMIT(NFA_CLASS_PUNCT);
1610 break;
1611 case CLASS_SPACE:
1612 EMIT(NFA_CLASS_SPACE);
1613 break;
1614 case CLASS_UPPER:
1615 EMIT(NFA_CLASS_UPPER);
1616 break;
1617 case CLASS_XDIGIT:
1618 EMIT(NFA_CLASS_XDIGIT);
1619 break;
1620 case CLASS_TAB:
1621 EMIT(NFA_CLASS_TAB);
1622 break;
1623 case CLASS_RETURN:
1624 EMIT(NFA_CLASS_RETURN);
1625 break;
1626 case CLASS_BACKSPACE:
1627 EMIT(NFA_CLASS_BACKSPACE);
1628 break;
1629 case CLASS_ESCAPE:
1630 EMIT(NFA_CLASS_ESCAPE);
1631 break;
1632 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001633 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001634 continue;
1635 }
1636 /* Try equivalence class [=a=] and the like */
1637 if (equiclass != 0)
1638 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001639 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001640 if (result == FAIL)
1641 {
1642 /* should never happen */
1643 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1644 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001645 continue;
1646 }
1647 /* Try collating class like [. .] */
1648 if (collclass != 0)
1649 {
1650 startc = collclass; /* allow [.a.]-x as a range */
1651 /* Will emit the proper atom at the end of the
1652 * while loop. */
1653 }
1654 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001655 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1656 * start character. */
1657 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001658 {
1659 emit_range = TRUE;
1660 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001661 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001662 continue; /* reading the end of the range */
1663 }
1664
1665 /* Now handle simple and escaped characters.
1666 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1667 * accepts "\t", "\e", etc., but only when the 'l' flag in
1668 * 'cpoptions' is not included.
1669 * Posix doesn't recognize backslash at all.
1670 */
1671 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001672 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001673 && regparse + 1 <= endp
1674 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001675 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001676 && vim_strchr(REGEXP_ABBR, regparse[1])
1677 != NULL)
1678 )
1679 )
1680 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001681 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001682
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001683 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001684 startc = reg_string ? NL : NFA_NEWL;
1685 else
1686 if (*regparse == 'd'
1687 || *regparse == 'o'
1688 || *regparse == 'x'
1689 || *regparse == 'u'
1690 || *regparse == 'U'
1691 )
1692 {
1693 /* TODO(RE) This needs more testing */
1694 startc = coll_get_char();
1695 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001696 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001697 }
1698 else
1699 {
1700 /* \r,\t,\e,\b */
1701 startc = backslash_trans(*regparse);
1702 }
1703 }
1704
1705 /* Normal printable char */
1706 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001707 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001708
1709 /* Previous char was '-', so this char is end of range. */
1710 if (emit_range)
1711 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001712 endc = startc;
1713 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001714 if (startc > endc)
1715 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001716
1717 if (endc > startc + 2)
1718 {
1719 /* Emit a range instead of the sequence of
1720 * individual characters. */
1721 if (startc == 0)
1722 /* \x00 is translated to \x0a, start at \x01. */
1723 EMIT(1);
1724 else
1725 --post_ptr; /* remove NFA_CONCAT */
1726 EMIT(endc);
1727 EMIT(NFA_RANGE);
1728 EMIT(NFA_CONCAT);
1729 }
1730 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001731#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001732 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001733 || (*mb_char2len)(endc) > 1))
1734 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001735 /* Emit the characters in the range.
1736 * "startc" was already emitted, so skip it.
1737 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 for (c = startc + 1; c <= endc; c++)
1739 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001740 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001741 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001742 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001743 }
1744 else
1745#endif
1746 {
1747#ifdef EBCDIC
1748 int alpha_only = FALSE;
1749
1750 /* for alphabetical range skip the gaps
1751 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1752 if (isalpha(startc) && isalpha(endc))
1753 alpha_only = TRUE;
1754#endif
1755 /* Emit the range. "startc" was already emitted, so
1756 * skip it. */
1757 for (c = startc + 1; c <= endc; c++)
1758#ifdef EBCDIC
1759 if (!alpha_only || isalpha(startc))
1760#endif
1761 {
1762 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001763 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001764 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001766 emit_range = FALSE;
1767 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001768 }
1769 else
1770 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001771 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001772 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001773 * Normally, simply emit startc. But if we get char
1774 * code=0 from a collating char, then replace it with
1775 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001776 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001777 * the backtracking engine. */
1778 if (startc == NFA_NEWL)
1779 {
1780 /* Line break can't be matched as part of the
1781 * collection, add an OR below. But not for negated
1782 * range. */
1783 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001784 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001785 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001786 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001787 {
1788 if (got_coll_char == TRUE && startc == 0)
1789 EMIT(0x0a);
1790 else
1791 EMIT(startc);
1792 EMIT(NFA_CONCAT);
1793 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001794 }
1795
Bram Moolenaar51a29832013-05-28 22:30:35 +02001796 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001797 } /* while (p < endp) */
1798
Bram Moolenaar51a29832013-05-28 22:30:35 +02001799 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001800 if (*regparse == '-') /* if last, '-' is just a char */
1801 {
1802 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001803 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001804 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001805
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001806 /* skip the trailing ] */
1807 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001808 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001809
1810 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001811 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001812 EMIT(NFA_END_NEG_COLL);
1813 else
1814 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001815
1816 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001817 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001818 {
1819 EMIT(reg_string ? NL : NFA_NEWL);
1820 EMIT(NFA_OR);
1821 }
1822
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001823 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001824 } /* if exists closing ] */
1825
1826 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001827 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001828 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001829
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001830 default:
1831 {
1832#ifdef FEAT_MBYTE
1833 int plen;
1834
1835nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001836 /* plen is length of current char with composing chars */
1837 if (enc_utf8 && ((*mb_char2len)(c)
1838 != (plen = (*mb_ptr2len)(old_regparse))
1839 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001840 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001841 int i = 0;
1842
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001843 /* A base character plus composing characters, or just one
1844 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001845 * This requires creating a separate atom as if enclosing
1846 * the characters in (), where NFA_COMPOSING is the ( and
1847 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001848 * building the postfix form, not the NFA itself;
1849 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001850 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001851 for (;;)
1852 {
1853 EMIT(c);
1854 if (i > 0)
1855 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001856 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001857 break;
1858 c = utf_ptr2char(old_regparse + i);
1859 }
1860 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001861 regparse = old_regparse + plen;
1862 }
1863 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001864#endif
1865 {
1866 c = no_Magic(c);
1867 EMIT(c);
1868 }
1869 return OK;
1870 }
1871 }
1872
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001873 return OK;
1874}
1875
1876/*
1877 * Parse something followed by possible [*+=].
1878 *
1879 * A piece is an atom, possibly followed by a multi, an indication of how many
1880 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1881 * characters: "", "a", "aa", etc.
1882 *
1883 * piece ::= atom
1884 * or atom multi
1885 */
1886 static int
1887nfa_regpiece()
1888{
1889 int i;
1890 int op;
1891 int ret;
1892 long minval, maxval;
1893 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001894 parse_state_T old_state;
1895 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001896 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001897 int old_post_pos;
1898 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001899 int quest;
1900
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001901 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1902 * next. */
1903 save_parse_state(&old_state);
1904
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001905 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001906 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907
1908 ret = nfa_regatom();
1909 if (ret == FAIL)
1910 return FAIL; /* cascaded error */
1911
1912 op = peekchr();
1913 if (re_multi_type(op) == NOT_MULTI)
1914 return OK;
1915
1916 skipchr();
1917 switch (op)
1918 {
1919 case Magic('*'):
1920 EMIT(NFA_STAR);
1921 break;
1922
1923 case Magic('+'):
1924 /*
1925 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1926 * first and only submatch would be "aaa". But the backtracking
1927 * engine interprets the plus as "try matching one more time", and
1928 * a* matches a second time at the end of the input, the empty
1929 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001930 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001931 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001932 * In order to be consistent with the old engine, we replace
1933 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001934 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001935 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001936 curchr = -1;
1937 if (nfa_regatom() == FAIL)
1938 return FAIL;
1939 EMIT(NFA_STAR);
1940 EMIT(NFA_CONCAT);
1941 skipchr(); /* skip the \+ */
1942 break;
1943
1944 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001945 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001946 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001947 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001948 switch(op)
1949 {
1950 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001951 /* \@= */
1952 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001953 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001954 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001955 /* \@! */
1956 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001957 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001958 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001959 op = no_Magic(getchr());
1960 if (op == '=')
1961 /* \@<= */
1962 i = NFA_PREV_ATOM_JUST_BEFORE;
1963 else if (op == '!')
1964 /* \@<! */
1965 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1966 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001967 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001968 /* \@> */
1969 i = NFA_PREV_ATOM_LIKE_PATTERN;
1970 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001971 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001972 if (i == 0)
1973 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001974 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1975 return FAIL;
1976 }
1977 EMIT(i);
1978 if (i == NFA_PREV_ATOM_JUST_BEFORE
1979 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1980 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001981 break;
1982
1983 case Magic('?'):
1984 case Magic('='):
1985 EMIT(NFA_QUEST);
1986 break;
1987
1988 case Magic('{'):
1989 /* a{2,5} will expand to 'aaa?a?a?'
1990 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1991 * version of '?'
1992 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1993 * parenthesis have the same id
1994 */
1995
1996 greedy = TRUE;
1997 c2 = peekchr();
1998 if (c2 == '-' || c2 == Magic('-'))
1999 {
2000 skipchr();
2001 greedy = FALSE;
2002 }
2003 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002004 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002005
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002006 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2007 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002008 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002009 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002010 if (greedy)
2011 /* \{}, \{0,} */
2012 EMIT(NFA_STAR);
2013 else
2014 /* \{-}, \{-0,} */
2015 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002016 break;
2017 }
2018
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002019 /* Special case: x{0} or x{-0} */
2020 if (maxval == 0)
2021 {
2022 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002023 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002024 /* NFA_EMPTY is 0-length and works everywhere */
2025 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002026 return OK;
2027 }
2028
2029 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002030 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002031 /* Save parse state after the repeated atom and the \{} */
2032 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002033
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002034 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2035 for (i = 0; i < maxval; i++)
2036 {
2037 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002038 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002039 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002040 if (nfa_regatom() == FAIL)
2041 return FAIL;
2042 /* after "minval" times, atoms are optional */
2043 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002044 {
2045 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002046 {
2047 if (greedy)
2048 EMIT(NFA_STAR);
2049 else
2050 EMIT(NFA_STAR_NONGREEDY);
2051 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002052 else
2053 EMIT(quest);
2054 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002055 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002056 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002057 if (i + 1 > minval && maxval == MAX_LIMIT)
2058 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002059 }
2060
2061 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002062 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063 curchr = -1;
2064
2065 break;
2066
2067
2068 default:
2069 break;
2070 } /* end switch */
2071
2072 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002073 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002074 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002075
2076 return OK;
2077}
2078
2079/*
2080 * Parse one or more pieces, concatenated. It matches a match for the
2081 * first piece, followed by a match for the second piece, etc. Example:
2082 * "f[0-9]b", first matches "f", then a digit and then "b".
2083 *
2084 * concat ::= piece
2085 * or piece piece
2086 * or piece piece piece
2087 * etc.
2088 */
2089 static int
2090nfa_regconcat()
2091{
2092 int cont = TRUE;
2093 int first = TRUE;
2094
2095 while (cont)
2096 {
2097 switch (peekchr())
2098 {
2099 case NUL:
2100 case Magic('|'):
2101 case Magic('&'):
2102 case Magic(')'):
2103 cont = FALSE;
2104 break;
2105
2106 case Magic('Z'):
2107#ifdef FEAT_MBYTE
2108 regflags |= RF_ICOMBINE;
2109#endif
2110 skipchr_keepstart();
2111 break;
2112 case Magic('c'):
2113 regflags |= RF_ICASE;
2114 skipchr_keepstart();
2115 break;
2116 case Magic('C'):
2117 regflags |= RF_NOICASE;
2118 skipchr_keepstart();
2119 break;
2120 case Magic('v'):
2121 reg_magic = MAGIC_ALL;
2122 skipchr_keepstart();
2123 curchr = -1;
2124 break;
2125 case Magic('m'):
2126 reg_magic = MAGIC_ON;
2127 skipchr_keepstart();
2128 curchr = -1;
2129 break;
2130 case Magic('M'):
2131 reg_magic = MAGIC_OFF;
2132 skipchr_keepstart();
2133 curchr = -1;
2134 break;
2135 case Magic('V'):
2136 reg_magic = MAGIC_NONE;
2137 skipchr_keepstart();
2138 curchr = -1;
2139 break;
2140
2141 default:
2142 if (nfa_regpiece() == FAIL)
2143 return FAIL;
2144 if (first == FALSE)
2145 EMIT(NFA_CONCAT);
2146 else
2147 first = FALSE;
2148 break;
2149 }
2150 }
2151
2152 return OK;
2153}
2154
2155/*
2156 * Parse a branch, one or more concats, separated by "\&". It matches the
2157 * last concat, but only if all the preceding concats also match at the same
2158 * position. Examples:
2159 * "foobeep\&..." matches "foo" in "foobeep".
2160 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2161 *
2162 * branch ::= concat
2163 * or concat \& concat
2164 * or concat \& concat \& concat
2165 * etc.
2166 */
2167 static int
2168nfa_regbranch()
2169{
2170 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002171 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002172
Bram Moolenaar16299b52013-05-30 18:45:23 +02002173 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002174
2175 /* First branch, possibly the only one */
2176 if (nfa_regconcat() == FAIL)
2177 return FAIL;
2178
2179 ch = peekchr();
2180 /* Try next concats */
2181 while (ch == Magic('&'))
2182 {
2183 skipchr();
2184 EMIT(NFA_NOPEN);
2185 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002186 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002187 if (nfa_regconcat() == FAIL)
2188 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002189 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002190 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002191 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002192 EMIT(NFA_CONCAT);
2193 ch = peekchr();
2194 }
2195
Bram Moolenaar699c1202013-09-25 16:41:54 +02002196 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002197 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002198 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002199
2200 return OK;
2201}
2202
2203/*
2204 * Parse a pattern, one or more branches, separated by "\|". It matches
2205 * anything that matches one of the branches. Example: "foo\|beep" matches
2206 * "foo" and matches "beep". If more than one branch matches, the first one
2207 * is used.
2208 *
2209 * pattern ::= branch
2210 * or branch \| branch
2211 * or branch \| branch \| branch
2212 * etc.
2213 */
2214 static int
2215nfa_reg(paren)
2216 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
2217{
2218 int parno = 0;
2219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002220 if (paren == REG_PAREN)
2221 {
2222 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002223 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 parno = regnpar++;
2225 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002226#ifdef FEAT_SYN_HL
2227 else if (paren == REG_ZPAREN)
2228 {
2229 /* Make a ZOPEN node. */
2230 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002231 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002232 parno = regnzpar++;
2233 }
2234#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002235
2236 if (nfa_regbranch() == FAIL)
2237 return FAIL; /* cascaded error */
2238
2239 while (peekchr() == Magic('|'))
2240 {
2241 skipchr();
2242 if (nfa_regbranch() == FAIL)
2243 return FAIL; /* cascaded error */
2244 EMIT(NFA_OR);
2245 }
2246
2247 /* Check for proper termination. */
2248 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2249 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002250 if (paren == REG_NPAREN)
2251 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2252 else
2253 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2254 }
2255 else if (paren == REG_NOPAREN && peekchr() != NUL)
2256 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002257 if (peekchr() == Magic(')'))
2258 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2259 else
2260 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2261 }
2262 /*
2263 * Here we set the flag allowing back references to this set of
2264 * parentheses.
2265 */
2266 if (paren == REG_PAREN)
2267 {
2268 had_endbrace[parno] = TRUE; /* have seen the close paren */
2269 EMIT(NFA_MOPEN + parno);
2270 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002271#ifdef FEAT_SYN_HL
2272 else if (paren == REG_ZPAREN)
2273 EMIT(NFA_ZOPEN + parno);
2274#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002275
2276 return OK;
2277}
2278
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279#ifdef DEBUG
2280static char_u code[50];
2281
2282 static void
2283nfa_set_code(c)
2284 int c;
2285{
2286 int addnl = FALSE;
2287
2288 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2289 {
2290 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002291 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002292 }
2293
2294 STRCPY(code, "");
2295 switch (c)
2296 {
2297 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2298 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2299 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2300 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2301 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2302 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2303
Bram Moolenaar5714b802013-05-28 22:03:20 +02002304 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2305 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2306 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2307 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2308 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2309 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2310 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2311 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2312 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002313#ifdef FEAT_SYN_HL
2314 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2315 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2316 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2317 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2318 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2319 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2320 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2321 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2322 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2323#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002324 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2325
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002326 case NFA_PREV_ATOM_NO_WIDTH:
2327 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002328 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2329 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002330 case NFA_PREV_ATOM_JUST_BEFORE:
2331 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2332 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2333 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002334 case NFA_PREV_ATOM_LIKE_PATTERN:
2335 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2336
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002337 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2338 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002339 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002340 case NFA_START_INVISIBLE_FIRST:
2341 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002342 case NFA_START_INVISIBLE_NEG:
2343 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002344 case NFA_START_INVISIBLE_NEG_FIRST:
2345 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002346 case NFA_START_INVISIBLE_BEFORE:
2347 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002348 case NFA_START_INVISIBLE_BEFORE_FIRST:
2349 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002350 case NFA_START_INVISIBLE_BEFORE_NEG:
2351 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002352 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2353 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002354 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002356 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002357 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002359 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2360 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002361 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002363 case NFA_MOPEN:
2364 case NFA_MOPEN1:
2365 case NFA_MOPEN2:
2366 case NFA_MOPEN3:
2367 case NFA_MOPEN4:
2368 case NFA_MOPEN5:
2369 case NFA_MOPEN6:
2370 case NFA_MOPEN7:
2371 case NFA_MOPEN8:
2372 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002373 STRCPY(code, "NFA_MOPEN(x)");
2374 code[10] = c - NFA_MOPEN + '0';
2375 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002376 case NFA_MCLOSE:
2377 case NFA_MCLOSE1:
2378 case NFA_MCLOSE2:
2379 case NFA_MCLOSE3:
2380 case NFA_MCLOSE4:
2381 case NFA_MCLOSE5:
2382 case NFA_MCLOSE6:
2383 case NFA_MCLOSE7:
2384 case NFA_MCLOSE8:
2385 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002386 STRCPY(code, "NFA_MCLOSE(x)");
2387 code[11] = c - NFA_MCLOSE + '0';
2388 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002389#ifdef FEAT_SYN_HL
2390 case NFA_ZOPEN:
2391 case NFA_ZOPEN1:
2392 case NFA_ZOPEN2:
2393 case NFA_ZOPEN3:
2394 case NFA_ZOPEN4:
2395 case NFA_ZOPEN5:
2396 case NFA_ZOPEN6:
2397 case NFA_ZOPEN7:
2398 case NFA_ZOPEN8:
2399 case NFA_ZOPEN9:
2400 STRCPY(code, "NFA_ZOPEN(x)");
2401 code[10] = c - NFA_ZOPEN + '0';
2402 break;
2403 case NFA_ZCLOSE:
2404 case NFA_ZCLOSE1:
2405 case NFA_ZCLOSE2:
2406 case NFA_ZCLOSE3:
2407 case NFA_ZCLOSE4:
2408 case NFA_ZCLOSE5:
2409 case NFA_ZCLOSE6:
2410 case NFA_ZCLOSE7:
2411 case NFA_ZCLOSE8:
2412 case NFA_ZCLOSE9:
2413 STRCPY(code, "NFA_ZCLOSE(x)");
2414 code[11] = c - NFA_ZCLOSE + '0';
2415 break;
2416#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002417 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2418 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2419 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2420 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002421 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2422 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002423 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2424 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2425 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2426 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2427 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2428 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2429 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2430 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2431 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2432 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2433 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2434 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2435 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2436 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002437 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002438
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002439 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002440 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2441 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2442 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002443 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002444 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002445
2446 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2447 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2448 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2449 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2450 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2451 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2452 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2453
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002454 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2455 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2456 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2457 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2458 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2459 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2460 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2461 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2462 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2463 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2464 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2465 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2466 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2467 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2468 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2469 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2470
2471 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2472 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2473 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2474 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2475 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2476 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2477 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2478 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2479 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2480 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2481 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2482 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2483 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2484 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2485 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2486 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2487 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2488 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2489 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2490 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2491 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2492 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2493 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2494 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2495 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2496 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2497 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002498 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2499 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2500 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2501 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002502
2503 default:
2504 STRCPY(code, "CHAR(x)");
2505 code[5] = c;
2506 }
2507
2508 if (addnl == TRUE)
2509 STRCAT(code, " + NEWLINE ");
2510
2511}
2512
2513#ifdef ENABLE_LOG
2514static FILE *log_fd;
2515
2516/*
2517 * Print the postfix notation of the current regexp.
2518 */
2519 static void
2520nfa_postfix_dump(expr, retval)
2521 char_u *expr;
2522 int retval;
2523{
2524 int *p;
2525 FILE *f;
2526
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002527 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002528 if (f != NULL)
2529 {
2530 fprintf(f, "\n-------------------------\n");
2531 if (retval == FAIL)
2532 fprintf(f, ">>> NFA engine failed ... \n");
2533 else if (retval == OK)
2534 fprintf(f, ">>> NFA engine succeeded !\n");
2535 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
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 {
2538 nfa_set_code(*p);
2539 fprintf(f, "%s, ", code);
2540 }
2541 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002542 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 fprintf(f, "%d ", *p);
2544 fprintf(f, "\n\n");
2545 fclose(f);
2546 }
2547}
2548
2549/*
2550 * Print the NFA starting with a root node "state".
2551 */
2552 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002553nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002554 FILE *debugf;
2555 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002556{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002557 garray_T indent;
2558
2559 ga_init2(&indent, 1, 64);
2560 ga_append(&indent, '\0');
2561 nfa_print_state2(debugf, state, &indent);
2562 ga_clear(&indent);
2563}
2564
2565 static void
2566nfa_print_state2(debugf, state, indent)
2567 FILE *debugf;
2568 nfa_state_T *state;
2569 garray_T *indent;
2570{
2571 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002572
2573 if (state == NULL)
2574 return;
2575
2576 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002577
2578 /* Output indent */
2579 p = (char_u *)indent->ga_data;
2580 if (indent->ga_len >= 3)
2581 {
2582 int last = indent->ga_len - 3;
2583 char_u save[2];
2584
2585 STRNCPY(save, &p[last], 2);
2586 STRNCPY(&p[last], "+-", 2);
2587 fprintf(debugf, " %s", p);
2588 STRNCPY(&p[last], save, 2);
2589 }
2590 else
2591 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002592
2593 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002594 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002595 code,
2596 state->c,
2597 abs(state->id),
2598 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002599 if (state->id < 0)
2600 return;
2601
2602 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002603
2604 /* grow indent for state->out */
2605 indent->ga_len -= 1;
2606 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002607 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002608 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002609 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002610 ga_append(indent, '\0');
2611
2612 nfa_print_state2(debugf, state->out, indent);
2613
2614 /* replace last part of indent for state->out1 */
2615 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002616 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002617 ga_append(indent, '\0');
2618
2619 nfa_print_state2(debugf, state->out1, indent);
2620
2621 /* shrink indent */
2622 indent->ga_len -= 3;
2623 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002624}
2625
2626/*
2627 * Print the NFA state machine.
2628 */
2629 static void
2630nfa_dump(prog)
2631 nfa_regprog_T *prog;
2632{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002633 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002634
2635 if (debugf != NULL)
2636 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002637 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002638
Bram Moolenaar473de612013-06-08 18:19:48 +02002639 if (prog->reganch)
2640 fprintf(debugf, "reganch: %d\n", prog->reganch);
2641 if (prog->regstart != NUL)
2642 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2643 prog->regstart, prog->regstart);
2644 if (prog->match_text != NULL)
2645 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002646
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002647 fclose(debugf);
2648 }
2649}
2650#endif /* ENABLE_LOG */
2651#endif /* DEBUG */
2652
2653/*
2654 * Parse r.e. @expr and convert it into postfix form.
2655 * Return the postfix string on success, NULL otherwise.
2656 */
2657 static int *
2658re2post()
2659{
2660 if (nfa_reg(REG_NOPAREN) == FAIL)
2661 return NULL;
2662 EMIT(NFA_MOPEN);
2663 return post_start;
2664}
2665
2666/* NB. Some of the code below is inspired by Russ's. */
2667
2668/*
2669 * Represents an NFA state plus zero or one or two arrows exiting.
2670 * if c == MATCH, no arrows out; matching state.
2671 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2672 * If c < 256, labeled arrow with character c to out.
2673 */
2674
2675static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2676
2677/*
2678 * Allocate and initialize nfa_state_T.
2679 */
2680 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002681alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002682 int c;
2683 nfa_state_T *out;
2684 nfa_state_T *out1;
2685{
2686 nfa_state_T *s;
2687
2688 if (istate >= nstate)
2689 return NULL;
2690
2691 s = &state_ptr[istate++];
2692
2693 s->c = c;
2694 s->out = out;
2695 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002696 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002697
2698 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002699 s->lastlist[0] = 0;
2700 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002701
2702 return s;
2703}
2704
2705/*
2706 * A partially built NFA without the matching state filled in.
2707 * Frag_T.start points at the start state.
2708 * Frag_T.out is a list of places that need to be set to the
2709 * next state for this fragment.
2710 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002711
2712/* Since the out pointers in the list are always
2713 * uninitialized, we use the pointers themselves
2714 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002715typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002716union Ptrlist
2717{
2718 Ptrlist *next;
2719 nfa_state_T *s;
2720};
2721
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002722struct Frag
2723{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002724 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002725 Ptrlist *out;
2726};
2727typedef struct Frag Frag_T;
2728
2729static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2730static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2731static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2732static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2733static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2734static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2735
2736/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002737 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 */
2739 static Frag_T
2740frag(start, out)
2741 nfa_state_T *start;
2742 Ptrlist *out;
2743{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002744 Frag_T n;
2745
2746 n.start = start;
2747 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748 return n;
2749}
2750
2751/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752 * Create singleton list containing just outp.
2753 */
2754 static Ptrlist *
2755list1(outp)
2756 nfa_state_T **outp;
2757{
2758 Ptrlist *l;
2759
2760 l = (Ptrlist *)outp;
2761 l->next = NULL;
2762 return l;
2763}
2764
2765/*
2766 * Patch the list of states at out to point to start.
2767 */
2768 static void
2769patch(l, s)
2770 Ptrlist *l;
2771 nfa_state_T *s;
2772{
2773 Ptrlist *next;
2774
2775 for (; l; l = next)
2776 {
2777 next = l->next;
2778 l->s = s;
2779 }
2780}
2781
2782
2783/*
2784 * Join the two lists l1 and l2, returning the combination.
2785 */
2786 static Ptrlist *
2787append(l1, l2)
2788 Ptrlist *l1;
2789 Ptrlist *l2;
2790{
2791 Ptrlist *oldl1;
2792
2793 oldl1 = l1;
2794 while (l1->next)
2795 l1 = l1->next;
2796 l1->next = l2;
2797 return oldl1;
2798}
2799
2800/*
2801 * Stack used for transforming postfix form into NFA.
2802 */
2803static Frag_T empty;
2804
2805 static void
2806st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002807 int *postfix UNUSED;
2808 int *end UNUSED;
2809 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002810{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002811#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002812 FILE *df;
2813 int *p2;
2814
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002815 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002816 if (df)
2817 {
2818 fprintf(df, "Error popping the stack!\n");
2819#ifdef DEBUG
2820 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2821#endif
2822 fprintf(df, "Postfix form is: ");
2823#ifdef DEBUG
2824 for (p2 = postfix; p2 < end; p2++)
2825 {
2826 nfa_set_code(*p2);
2827 fprintf(df, "%s, ", code);
2828 }
2829 nfa_set_code(*p);
2830 fprintf(df, "\nCurrent position is: ");
2831 for (p2 = postfix; p2 <= p; p2 ++)
2832 {
2833 nfa_set_code(*p2);
2834 fprintf(df, "%s, ", code);
2835 }
2836#else
2837 for (p2 = postfix; p2 < end; p2++)
2838 {
2839 fprintf(df, "%d, ", *p2);
2840 }
2841 fprintf(df, "\nCurrent position is: ");
2842 for (p2 = postfix; p2 <= p; p2 ++)
2843 {
2844 fprintf(df, "%d, ", *p2);
2845 }
2846#endif
2847 fprintf(df, "\n--------------------------\n");
2848 fclose(df);
2849 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002850#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002851 EMSG(_("E874: (NFA) Could not pop the stack !"));
2852}
2853
2854/*
2855 * Push an item onto the stack.
2856 */
2857 static void
2858st_push(s, p, stack_end)
2859 Frag_T s;
2860 Frag_T **p;
2861 Frag_T *stack_end;
2862{
2863 Frag_T *stackp = *p;
2864
2865 if (stackp >= stack_end)
2866 return;
2867 *stackp = s;
2868 *p = *p + 1;
2869}
2870
2871/*
2872 * Pop an item from the stack.
2873 */
2874 static Frag_T
2875st_pop(p, stack)
2876 Frag_T **p;
2877 Frag_T *stack;
2878{
2879 Frag_T *stackp;
2880
2881 *p = *p - 1;
2882 stackp = *p;
2883 if (stackp < stack)
2884 return empty;
2885 return **p;
2886}
2887
2888/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002889 * Estimate the maximum byte length of anything matching "state".
2890 * When unknown or unlimited return -1.
2891 */
2892 static int
2893nfa_max_width(startstate, depth)
2894 nfa_state_T *startstate;
2895 int depth;
2896{
2897 int l, r;
2898 nfa_state_T *state = startstate;
2899 int len = 0;
2900
2901 /* detect looping in a NFA_SPLIT */
2902 if (depth > 4)
2903 return -1;
2904
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002905 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002906 {
2907 switch (state->c)
2908 {
2909 case NFA_END_INVISIBLE:
2910 case NFA_END_INVISIBLE_NEG:
2911 /* the end, return what we have */
2912 return len;
2913
2914 case NFA_SPLIT:
2915 /* two alternatives, use the maximum */
2916 l = nfa_max_width(state->out, depth + 1);
2917 r = nfa_max_width(state->out1, depth + 1);
2918 if (l < 0 || r < 0)
2919 return -1;
2920 return len + (l > r ? l : r);
2921
2922 case NFA_ANY:
2923 case NFA_START_COLL:
2924 case NFA_START_NEG_COLL:
2925 /* matches some character, including composing chars */
2926#ifdef FEAT_MBYTE
2927 if (enc_utf8)
2928 len += MB_MAXBYTES;
2929 else if (has_mbyte)
2930 len += 2;
2931 else
2932#endif
2933 ++len;
2934 if (state->c != NFA_ANY)
2935 {
2936 /* skip over the characters */
2937 state = state->out1->out;
2938 continue;
2939 }
2940 break;
2941
2942 case NFA_DIGIT:
2943 case NFA_WHITE:
2944 case NFA_HEX:
2945 case NFA_OCTAL:
2946 /* ascii */
2947 ++len;
2948 break;
2949
2950 case NFA_IDENT:
2951 case NFA_SIDENT:
2952 case NFA_KWORD:
2953 case NFA_SKWORD:
2954 case NFA_FNAME:
2955 case NFA_SFNAME:
2956 case NFA_PRINT:
2957 case NFA_SPRINT:
2958 case NFA_NWHITE:
2959 case NFA_NDIGIT:
2960 case NFA_NHEX:
2961 case NFA_NOCTAL:
2962 case NFA_WORD:
2963 case NFA_NWORD:
2964 case NFA_HEAD:
2965 case NFA_NHEAD:
2966 case NFA_ALPHA:
2967 case NFA_NALPHA:
2968 case NFA_LOWER:
2969 case NFA_NLOWER:
2970 case NFA_UPPER:
2971 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002972 case NFA_LOWER_IC:
2973 case NFA_NLOWER_IC:
2974 case NFA_UPPER_IC:
2975 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002976 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002977 /* possibly non-ascii */
2978#ifdef FEAT_MBYTE
2979 if (has_mbyte)
2980 len += 3;
2981 else
2982#endif
2983 ++len;
2984 break;
2985
2986 case NFA_START_INVISIBLE:
2987 case NFA_START_INVISIBLE_NEG:
2988 case NFA_START_INVISIBLE_BEFORE:
2989 case NFA_START_INVISIBLE_BEFORE_NEG:
2990 /* zero-width, out1 points to the END state */
2991 state = state->out1->out;
2992 continue;
2993
2994 case NFA_BACKREF1:
2995 case NFA_BACKREF2:
2996 case NFA_BACKREF3:
2997 case NFA_BACKREF4:
2998 case NFA_BACKREF5:
2999 case NFA_BACKREF6:
3000 case NFA_BACKREF7:
3001 case NFA_BACKREF8:
3002 case NFA_BACKREF9:
3003#ifdef FEAT_SYN_HL
3004 case NFA_ZREF1:
3005 case NFA_ZREF2:
3006 case NFA_ZREF3:
3007 case NFA_ZREF4:
3008 case NFA_ZREF5:
3009 case NFA_ZREF6:
3010 case NFA_ZREF7:
3011 case NFA_ZREF8:
3012 case NFA_ZREF9:
3013#endif
3014 case NFA_NEWL:
3015 case NFA_SKIP:
3016 /* unknown width */
3017 return -1;
3018
3019 case NFA_BOL:
3020 case NFA_EOL:
3021 case NFA_BOF:
3022 case NFA_EOF:
3023 case NFA_BOW:
3024 case NFA_EOW:
3025 case NFA_MOPEN:
3026 case NFA_MOPEN1:
3027 case NFA_MOPEN2:
3028 case NFA_MOPEN3:
3029 case NFA_MOPEN4:
3030 case NFA_MOPEN5:
3031 case NFA_MOPEN6:
3032 case NFA_MOPEN7:
3033 case NFA_MOPEN8:
3034 case NFA_MOPEN9:
3035#ifdef FEAT_SYN_HL
3036 case NFA_ZOPEN:
3037 case NFA_ZOPEN1:
3038 case NFA_ZOPEN2:
3039 case NFA_ZOPEN3:
3040 case NFA_ZOPEN4:
3041 case NFA_ZOPEN5:
3042 case NFA_ZOPEN6:
3043 case NFA_ZOPEN7:
3044 case NFA_ZOPEN8:
3045 case NFA_ZOPEN9:
3046 case NFA_ZCLOSE:
3047 case NFA_ZCLOSE1:
3048 case NFA_ZCLOSE2:
3049 case NFA_ZCLOSE3:
3050 case NFA_ZCLOSE4:
3051 case NFA_ZCLOSE5:
3052 case NFA_ZCLOSE6:
3053 case NFA_ZCLOSE7:
3054 case NFA_ZCLOSE8:
3055 case NFA_ZCLOSE9:
3056#endif
3057 case NFA_MCLOSE:
3058 case NFA_MCLOSE1:
3059 case NFA_MCLOSE2:
3060 case NFA_MCLOSE3:
3061 case NFA_MCLOSE4:
3062 case NFA_MCLOSE5:
3063 case NFA_MCLOSE6:
3064 case NFA_MCLOSE7:
3065 case NFA_MCLOSE8:
3066 case NFA_MCLOSE9:
3067 case NFA_NOPEN:
3068 case NFA_NCLOSE:
3069
3070 case NFA_LNUM_GT:
3071 case NFA_LNUM_LT:
3072 case NFA_COL_GT:
3073 case NFA_COL_LT:
3074 case NFA_VCOL_GT:
3075 case NFA_VCOL_LT:
3076 case NFA_MARK_GT:
3077 case NFA_MARK_LT:
3078 case NFA_VISUAL:
3079 case NFA_LNUM:
3080 case NFA_CURSOR:
3081 case NFA_COL:
3082 case NFA_VCOL:
3083 case NFA_MARK:
3084
3085 case NFA_ZSTART:
3086 case NFA_ZEND:
3087 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003088 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003089 case NFA_START_PATTERN:
3090 case NFA_END_PATTERN:
3091 case NFA_COMPOSING:
3092 case NFA_END_COMPOSING:
3093 /* zero-width */
3094 break;
3095
3096 default:
3097 if (state->c < 0)
3098 /* don't know what this is */
3099 return -1;
3100 /* normal character */
3101 len += MB_CHAR2LEN(state->c);
3102 break;
3103 }
3104
3105 /* normal way to continue */
3106 state = state->out;
3107 }
3108
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003109 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003110 return -1;
3111}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003112
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003113/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003114 * Convert a postfix form into its equivalent NFA.
3115 * Return the NFA start state on success, NULL otherwise.
3116 */
3117 static nfa_state_T *
3118post2nfa(postfix, end, nfa_calc_size)
3119 int *postfix;
3120 int *end;
3121 int nfa_calc_size;
3122{
3123 int *p;
3124 int mopen;
3125 int mclose;
3126 Frag_T *stack = NULL;
3127 Frag_T *stackp = NULL;
3128 Frag_T *stack_end = NULL;
3129 Frag_T e1;
3130 Frag_T e2;
3131 Frag_T e;
3132 nfa_state_T *s;
3133 nfa_state_T *s1;
3134 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003135 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003136
3137 if (postfix == NULL)
3138 return NULL;
3139
Bram Moolenaar053bb602013-05-20 13:55:21 +02003140#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003141#define POP() st_pop(&stackp, stack); \
3142 if (stackp < stack) \
3143 { \
3144 st_error(postfix, end, p); \
3145 return NULL; \
3146 }
3147
3148 if (nfa_calc_size == FALSE)
3149 {
3150 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003151 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003152 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003153 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003154 }
3155
3156 for (p = postfix; p < end; ++p)
3157 {
3158 switch (*p)
3159 {
3160 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003161 /* Concatenation.
3162 * Pay attention: this operator does not exist in the r.e. itself
3163 * (it is implicit, really). It is added when r.e. is translated
3164 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003165 if (nfa_calc_size == TRUE)
3166 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003167 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003168 break;
3169 }
3170 e2 = POP();
3171 e1 = POP();
3172 patch(e1.out, e2.start);
3173 PUSH(frag(e1.start, e2.out));
3174 break;
3175
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003176 case NFA_OR:
3177 /* Alternation */
3178 if (nfa_calc_size == TRUE)
3179 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003180 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003181 break;
3182 }
3183 e2 = POP();
3184 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003185 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003186 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003187 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003188 PUSH(frag(s, append(e1.out, e2.out)));
3189 break;
3190
3191 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003192 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 if (nfa_calc_size == TRUE)
3194 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003195 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003196 break;
3197 }
3198 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003199 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003201 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003202 patch(e.out, s);
3203 PUSH(frag(s, list1(&s->out1)));
3204 break;
3205
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003206 case NFA_STAR_NONGREEDY:
3207 /* Zero or more, prefer zero */
3208 if (nfa_calc_size == TRUE)
3209 {
3210 nstate++;
3211 break;
3212 }
3213 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003214 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003215 if (s == NULL)
3216 goto theend;
3217 patch(e.out, s);
3218 PUSH(frag(s, list1(&s->out)));
3219 break;
3220
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003221 case NFA_QUEST:
3222 /* one or zero atoms=> greedy match */
3223 if (nfa_calc_size == TRUE)
3224 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003225 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003226 break;
3227 }
3228 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003229 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003230 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003231 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003232 PUSH(frag(s, append(e.out, list1(&s->out1))));
3233 break;
3234
3235 case NFA_QUEST_NONGREEDY:
3236 /* zero or one atoms => non-greedy match */
3237 if (nfa_calc_size == TRUE)
3238 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003239 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003240 break;
3241 }
3242 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003243 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003245 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003246 PUSH(frag(s, append(e.out, list1(&s->out))));
3247 break;
3248
Bram Moolenaar417bad22013-06-07 14:08:30 +02003249 case NFA_END_COLL:
3250 case NFA_END_NEG_COLL:
3251 /* On the stack is the sequence starting with NFA_START_COLL or
3252 * NFA_START_NEG_COLL and all possible characters. Patch it to
3253 * add the output to the start. */
3254 if (nfa_calc_size == TRUE)
3255 {
3256 nstate++;
3257 break;
3258 }
3259 e = POP();
3260 s = alloc_state(NFA_END_COLL, NULL, NULL);
3261 if (s == NULL)
3262 goto theend;
3263 patch(e.out, s);
3264 e.start->out1 = s;
3265 PUSH(frag(e.start, list1(&s->out)));
3266 break;
3267
3268 case NFA_RANGE:
3269 /* Before this are two characters, the low and high end of a
3270 * range. Turn them into two states with MIN and MAX. */
3271 if (nfa_calc_size == TRUE)
3272 {
3273 /* nstate += 0; */
3274 break;
3275 }
3276 e2 = POP();
3277 e1 = POP();
3278 e2.start->val = e2.start->c;
3279 e2.start->c = NFA_RANGE_MAX;
3280 e1.start->val = e1.start->c;
3281 e1.start->c = NFA_RANGE_MIN;
3282 patch(e1.out, e2.start);
3283 PUSH(frag(e1.start, e2.out));
3284 break;
3285
Bram Moolenaar699c1202013-09-25 16:41:54 +02003286 case NFA_EMPTY:
3287 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003288 if (nfa_calc_size == TRUE)
3289 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003290 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003291 break;
3292 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003293 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003294 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003295 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003296 PUSH(frag(s, list1(&s->out)));
3297 break;
3298
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003299 case NFA_OPT_CHARS:
3300 {
3301 int n;
3302
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003303 /* \%[abc] implemented as:
3304 * NFA_SPLIT
3305 * +-CHAR(a)
3306 * | +-NFA_SPLIT
3307 * | +-CHAR(b)
3308 * | | +-NFA_SPLIT
3309 * | | +-CHAR(c)
3310 * | | | +-next
3311 * | | +- next
3312 * | +- next
3313 * +- next
3314 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003315 n = *++p; /* get number of characters */
3316 if (nfa_calc_size == TRUE)
3317 {
3318 nstate += n;
3319 break;
3320 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003321 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003322 e1.out = NULL; /* stores list with out1's */
3323 s1 = NULL; /* previous NFA_SPLIT to connect to */
3324 while (n-- > 0)
3325 {
3326 e = POP(); /* get character */
3327 s = alloc_state(NFA_SPLIT, e.start, NULL);
3328 if (s == NULL)
3329 goto theend;
3330 if (e1.out == NULL)
3331 e1 = e;
3332 patch(e.out, s1);
3333 append(e1.out, list1(&s->out1));
3334 s1 = s;
3335 }
3336 PUSH(frag(s, e1.out));
3337 break;
3338 }
3339
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003340 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003341 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003342 case NFA_PREV_ATOM_JUST_BEFORE:
3343 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003344 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003345 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003346 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3347 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003348 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003349 int start_state;
3350 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003351 int n = 0;
3352 nfa_state_T *zend;
3353 nfa_state_T *skip;
3354
Bram Moolenaardecd9542013-06-07 16:31:50 +02003355 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003356 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003357 case NFA_PREV_ATOM_NO_WIDTH:
3358 start_state = NFA_START_INVISIBLE;
3359 end_state = NFA_END_INVISIBLE;
3360 break;
3361 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3362 start_state = NFA_START_INVISIBLE_NEG;
3363 end_state = NFA_END_INVISIBLE_NEG;
3364 break;
3365 case NFA_PREV_ATOM_JUST_BEFORE:
3366 start_state = NFA_START_INVISIBLE_BEFORE;
3367 end_state = NFA_END_INVISIBLE;
3368 break;
3369 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3370 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3371 end_state = NFA_END_INVISIBLE_NEG;
3372 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003373 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003374 start_state = NFA_START_PATTERN;
3375 end_state = NFA_END_PATTERN;
3376 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003377 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003378
3379 if (before)
3380 n = *++p; /* get the count */
3381
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003382 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003383 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003384 * The \@<= operator: match for the preceding atom.
3385 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003386 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003387 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003388
3389 if (nfa_calc_size == TRUE)
3390 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003391 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003392 break;
3393 }
3394 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003395 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003396 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003397 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003398
Bram Moolenaar87953742013-06-05 18:52:40 +02003399 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003401 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003402 if (pattern)
3403 {
3404 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3405 skip = alloc_state(NFA_SKIP, NULL, NULL);
3406 zend = alloc_state(NFA_ZEND, s1, NULL);
3407 s1->out= skip;
3408 patch(e.out, zend);
3409 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003410 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003411 else
3412 {
3413 patch(e.out, s1);
3414 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003415 if (before)
3416 {
3417 if (n <= 0)
3418 /* See if we can guess the maximum width, it avoids a
3419 * lot of pointless tries. */
3420 n = nfa_max_width(e.start, 0);
3421 s->val = n; /* store the count */
3422 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003423 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003424 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003425 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003426
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003427#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003428 case NFA_COMPOSING: /* char with composing char */
3429#if 0
3430 /* TODO */
3431 if (regflags & RF_ICOMBINE)
3432 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003433 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003434 }
3435#endif
3436 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003437#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003438
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003439 case NFA_MOPEN: /* \( \) Submatch */
3440 case NFA_MOPEN1:
3441 case NFA_MOPEN2:
3442 case NFA_MOPEN3:
3443 case NFA_MOPEN4:
3444 case NFA_MOPEN5:
3445 case NFA_MOPEN6:
3446 case NFA_MOPEN7:
3447 case NFA_MOPEN8:
3448 case NFA_MOPEN9:
3449#ifdef FEAT_SYN_HL
3450 case NFA_ZOPEN: /* \z( \) Submatch */
3451 case NFA_ZOPEN1:
3452 case NFA_ZOPEN2:
3453 case NFA_ZOPEN3:
3454 case NFA_ZOPEN4:
3455 case NFA_ZOPEN5:
3456 case NFA_ZOPEN6:
3457 case NFA_ZOPEN7:
3458 case NFA_ZOPEN8:
3459 case NFA_ZOPEN9:
3460#endif
3461 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003462 if (nfa_calc_size == TRUE)
3463 {
3464 nstate += 2;
3465 break;
3466 }
3467
3468 mopen = *p;
3469 switch (*p)
3470 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003471 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3472#ifdef FEAT_SYN_HL
3473 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3474 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3475 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3476 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3477 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3478 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3479 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3480 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3481 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3482 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3483#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003484#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003485 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003486#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003487 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003488 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489 mclose = *p + NSUBEXP;
3490 break;
3491 }
3492
3493 /* Allow "NFA_MOPEN" as a valid postfix representation for
3494 * the empty regexp "". In this case, the NFA will be
3495 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3496 * empty groups of parenthesis, and empty mbyte chars */
3497 if (stackp == stack)
3498 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003499 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003500 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003501 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003502 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003503 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003504 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003505 patch(list1(&s->out), s1);
3506 PUSH(frag(s, list1(&s1->out)));
3507 break;
3508 }
3509
3510 /* At least one node was emitted before NFA_MOPEN, so
3511 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3512 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003513 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003515 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003516
Bram Moolenaar525666f2013-06-02 16:40:55 +02003517 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003519 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 patch(e.out, s1);
3521
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003522#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003523 if (mopen == NFA_COMPOSING)
3524 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003525 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003526#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003527
3528 PUSH(frag(s, list1(&s1->out)));
3529 break;
3530
Bram Moolenaar5714b802013-05-28 22:03:20 +02003531 case NFA_BACKREF1:
3532 case NFA_BACKREF2:
3533 case NFA_BACKREF3:
3534 case NFA_BACKREF4:
3535 case NFA_BACKREF5:
3536 case NFA_BACKREF6:
3537 case NFA_BACKREF7:
3538 case NFA_BACKREF8:
3539 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003540#ifdef FEAT_SYN_HL
3541 case NFA_ZREF1:
3542 case NFA_ZREF2:
3543 case NFA_ZREF3:
3544 case NFA_ZREF4:
3545 case NFA_ZREF5:
3546 case NFA_ZREF6:
3547 case NFA_ZREF7:
3548 case NFA_ZREF8:
3549 case NFA_ZREF9:
3550#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003551 if (nfa_calc_size == TRUE)
3552 {
3553 nstate += 2;
3554 break;
3555 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003556 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003557 if (s == NULL)
3558 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003559 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003560 if (s1 == NULL)
3561 goto theend;
3562 patch(list1(&s->out), s1);
3563 PUSH(frag(s, list1(&s1->out)));
3564 break;
3565
Bram Moolenaar423532e2013-05-29 21:14:42 +02003566 case NFA_LNUM:
3567 case NFA_LNUM_GT:
3568 case NFA_LNUM_LT:
3569 case NFA_VCOL:
3570 case NFA_VCOL_GT:
3571 case NFA_VCOL_LT:
3572 case NFA_COL:
3573 case NFA_COL_GT:
3574 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003575 case NFA_MARK:
3576 case NFA_MARK_GT:
3577 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003578 {
3579 int n = *++p; /* lnum, col or mark name */
3580
Bram Moolenaar423532e2013-05-29 21:14:42 +02003581 if (nfa_calc_size == TRUE)
3582 {
3583 nstate += 1;
3584 break;
3585 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003586 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003587 if (s == NULL)
3588 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003589 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003590 PUSH(frag(s, list1(&s->out)));
3591 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003592 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003593
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003594 case NFA_ZSTART:
3595 case NFA_ZEND:
3596 default:
3597 /* Operands */
3598 if (nfa_calc_size == TRUE)
3599 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003600 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003601 break;
3602 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003603 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003604 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003605 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003606 PUSH(frag(s, list1(&s->out)));
3607 break;
3608
3609 } /* switch(*p) */
3610
3611 } /* for(p = postfix; *p; ++p) */
3612
3613 if (nfa_calc_size == TRUE)
3614 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003615 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003616 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003617 }
3618
3619 e = POP();
3620 if (stackp != stack)
3621 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3622
3623 if (istate >= nstate)
3624 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3625
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003626 matchstate = &state_ptr[istate++]; /* the match state */
3627 matchstate->c = NFA_MATCH;
3628 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003629 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630
3631 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003632 ret = e.start;
3633
3634theend:
3635 vim_free(stack);
3636 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003637
3638#undef POP1
3639#undef PUSH1
3640#undef POP2
3641#undef PUSH2
3642#undef POP
3643#undef PUSH
3644}
3645
Bram Moolenaara2947e22013-06-11 22:44:09 +02003646/*
3647 * After building the NFA program, inspect it to add optimization hints.
3648 */
3649 static void
3650nfa_postprocess(prog)
3651 nfa_regprog_T *prog;
3652{
3653 int i;
3654 int c;
3655
3656 for (i = 0; i < prog->nstate; ++i)
3657 {
3658 c = prog->state[i].c;
3659 if (c == NFA_START_INVISIBLE
3660 || c == NFA_START_INVISIBLE_NEG
3661 || c == NFA_START_INVISIBLE_BEFORE
3662 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3663 {
3664 int directly;
3665
3666 /* Do it directly when what follows is possibly the end of the
3667 * match. */
3668 if (match_follows(prog->state[i].out1->out, 0))
3669 directly = TRUE;
3670 else
3671 {
3672 int ch_invisible = failure_chance(prog->state[i].out, 0);
3673 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3674
3675 /* Postpone when the invisible match is expensive or has a
3676 * lower chance of failing. */
3677 if (c == NFA_START_INVISIBLE_BEFORE
3678 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3679 {
3680 /* "before" matches are very expensive when
3681 * unbounded, always prefer what follows then,
3682 * unless what follows will always match.
3683 * Otherwise strongly prefer what follows. */
3684 if (prog->state[i].val <= 0 && ch_follows > 0)
3685 directly = FALSE;
3686 else
3687 directly = ch_follows * 10 < ch_invisible;
3688 }
3689 else
3690 {
3691 /* normal invisible, first do the one with the
3692 * highest failure chance */
3693 directly = ch_follows < ch_invisible;
3694 }
3695 }
3696 if (directly)
3697 /* switch to the _FIRST state */
3698 ++prog->state[i].c;
3699 }
3700 }
3701}
3702
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003703/****************************************************************
3704 * NFA execution code.
3705 ****************************************************************/
3706
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003707typedef struct
3708{
3709 int in_use; /* number of subexpr with useful info */
3710
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003711 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003712 union
3713 {
3714 struct multipos
3715 {
3716 lpos_T start;
3717 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003718 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003719 struct linepos
3720 {
3721 char_u *start;
3722 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003723 } line[NSUBEXP];
3724 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003725} regsub_T;
3726
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003727typedef struct
3728{
3729 regsub_T norm; /* \( .. \) matches */
3730#ifdef FEAT_SYN_HL
3731 regsub_T synt; /* \z( .. \) matches */
3732#endif
3733} regsubs_T;
3734
Bram Moolenaara2d95102013-06-04 14:23:05 +02003735/* nfa_pim_T stores a Postponed Invisible Match. */
3736typedef struct nfa_pim_S nfa_pim_T;
3737struct nfa_pim_S
3738{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003739 int result; /* NFA_PIM_*, see below */
3740 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003741 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003742 union
3743 {
3744 lpos_T pos;
3745 char_u *ptr;
3746 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003747};
3748
3749/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003750#define NFA_PIM_UNUSED 0 /* pim not used */
3751#define NFA_PIM_TODO 1 /* pim not done yet */
3752#define NFA_PIM_MATCH 2 /* pim executed, matches */
3753#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003754
3755
Bram Moolenaar963fee22013-05-26 21:47:28 +02003756/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003757typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003758{
3759 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003760 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003761 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3762 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003763 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003764} nfa_thread_T;
3765
Bram Moolenaar963fee22013-05-26 21:47:28 +02003766/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003767typedef struct
3768{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003769 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003770 int n; /* nr of states currently in "t" */
3771 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003772 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003773 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003774} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003775
Bram Moolenaar5714b802013-05-28 22:03:20 +02003776#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003777static void log_subsexpr __ARGS((regsubs_T *subs));
3778static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003779static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003780
3781 static void
3782log_subsexpr(subs)
3783 regsubs_T *subs;
3784{
3785 log_subexpr(&subs->norm);
3786# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003787 if (nfa_has_zsubexpr)
3788 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003789# endif
3790}
3791
Bram Moolenaar5714b802013-05-28 22:03:20 +02003792 static void
3793log_subexpr(sub)
3794 regsub_T *sub;
3795{
3796 int j;
3797
3798 for (j = 0; j < sub->in_use; j++)
3799 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003800 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003801 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003802 sub->list.multi[j].start.col,
3803 (int)sub->list.multi[j].start.lnum,
3804 sub->list.multi[j].end.col,
3805 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003806 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003807 {
3808 char *s = (char *)sub->list.line[j].start;
3809 char *e = (char *)sub->list.line[j].end;
3810
Bram Moolenaar87953742013-06-05 18:52:40 +02003811 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003812 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003813 s == NULL ? "NULL" : s,
3814 e == NULL ? "NULL" : e);
3815 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003816}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003817
3818 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003819pim_info(pim)
3820 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003821{
3822 static char buf[30];
3823
3824 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3825 buf[0] = NUL;
3826 else
3827 {
3828 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3829 : (int)(pim->end.ptr - reginput));
3830 }
3831 return buf;
3832}
3833
Bram Moolenaar5714b802013-05-28 22:03:20 +02003834#endif
3835
Bram Moolenaar963fee22013-05-26 21:47:28 +02003836/* Used during execution: whether a match has been found. */
3837static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003838
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003839static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003840static void clear_sub __ARGS((regsub_T *sub));
3841static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3842static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaarf2118842013-09-25 18:16:38 +02003843static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003844static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003845static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003846static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3847static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003848static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003849static 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 +02003850static 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 +02003851
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003852/*
3853 * Copy postponed invisible match info from "from" to "to".
3854 */
3855 static void
3856copy_pim(to, from)
3857 nfa_pim_T *to;
3858 nfa_pim_T *from;
3859{
3860 to->result = from->result;
3861 to->state = from->state;
3862 copy_sub(&to->subs.norm, &from->subs.norm);
3863#ifdef FEAT_SYN_HL
3864 if (nfa_has_zsubexpr)
3865 copy_sub(&to->subs.synt, &from->subs.synt);
3866#endif
3867 to->end = from->end;
3868}
3869
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003870 static void
3871clear_sub(sub)
3872 regsub_T *sub;
3873{
3874 if (REG_MULTI)
3875 /* Use 0xff to set lnum to -1 */
3876 vim_memset(sub->list.multi, 0xff,
3877 sizeof(struct multipos) * nfa_nsubexpr);
3878 else
3879 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3880 sub->in_use = 0;
3881}
3882
3883/*
3884 * Copy the submatches from "from" to "to".
3885 */
3886 static void
3887copy_sub(to, from)
3888 regsub_T *to;
3889 regsub_T *from;
3890{
3891 to->in_use = from->in_use;
3892 if (from->in_use > 0)
3893 {
3894 /* Copy the match start and end positions. */
3895 if (REG_MULTI)
3896 mch_memmove(&to->list.multi[0],
3897 &from->list.multi[0],
3898 sizeof(struct multipos) * from->in_use);
3899 else
3900 mch_memmove(&to->list.line[0],
3901 &from->list.line[0],
3902 sizeof(struct linepos) * from->in_use);
3903 }
3904}
3905
3906/*
3907 * Like copy_sub() but exclude the main match.
3908 */
3909 static void
3910copy_sub_off(to, from)
3911 regsub_T *to;
3912 regsub_T *from;
3913{
3914 if (to->in_use < from->in_use)
3915 to->in_use = from->in_use;
3916 if (from->in_use > 1)
3917 {
3918 /* Copy the match start and end positions. */
3919 if (REG_MULTI)
3920 mch_memmove(&to->list.multi[1],
3921 &from->list.multi[1],
3922 sizeof(struct multipos) * (from->in_use - 1));
3923 else
3924 mch_memmove(&to->list.line[1],
3925 &from->list.line[1],
3926 sizeof(struct linepos) * (from->in_use - 1));
3927 }
3928}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003929
Bram Moolenaar428e9872013-05-30 17:05:39 +02003930/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003931 * Like copy_sub() but only do the end of the main match if \ze is present.
3932 */
3933 static void
3934copy_ze_off(to, from)
3935 regsub_T *to;
3936 regsub_T *from;
3937{
3938 if (nfa_has_zend)
3939 {
3940 if (REG_MULTI)
3941 {
3942 if (from->list.multi[0].end.lnum >= 0)
3943 to->list.multi[0].end = from->list.multi[0].end;
3944 }
3945 else
3946 {
3947 if (from->list.line[0].end != NULL)
3948 to->list.line[0].end = from->list.line[0].end;
3949 }
3950 }
3951}
3952
3953/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003954 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003955 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003956 */
3957 static int
3958sub_equal(sub1, sub2)
3959 regsub_T *sub1;
3960 regsub_T *sub2;
3961{
3962 int i;
3963 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003964 linenr_T s1;
3965 linenr_T s2;
3966 char_u *sp1;
3967 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003968
3969 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3970 if (REG_MULTI)
3971 {
3972 for (i = 0; i < todo; ++i)
3973 {
3974 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003975 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003976 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003977 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003978 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003979 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003980 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003981 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003982 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003983 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02003984 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02003985 != sub2->list.multi[i].start.col)
3986 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02003987
3988 if (nfa_has_backref)
3989 {
3990 if (i < sub1->in_use)
3991 s1 = sub1->list.multi[i].end.lnum;
3992 else
3993 s1 = -1;
3994 if (i < sub2->in_use)
3995 s2 = sub2->list.multi[i].end.lnum;
3996 else
3997 s2 = -1;
3998 if (s1 != s2)
3999 return FALSE;
4000 if (s1 != -1 && sub1->list.multi[i].end.col
4001 != sub2->list.multi[i].end.col)
4002 return FALSE;
4003 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004004 }
4005 }
4006 else
4007 {
4008 for (i = 0; i < todo; ++i)
4009 {
4010 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004011 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004012 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004013 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004014 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004015 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004016 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004017 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004018 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004019 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004020 if (nfa_has_backref)
4021 {
4022 if (i < sub1->in_use)
4023 sp1 = sub1->list.line[i].end;
4024 else
4025 sp1 = NULL;
4026 if (i < sub2->in_use)
4027 sp2 = sub2->list.line[i].end;
4028 else
4029 sp2 = NULL;
4030 if (sp1 != sp2)
4031 return FALSE;
4032 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004033 }
4034 }
4035
4036 return TRUE;
4037}
4038
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004039#ifdef ENABLE_LOG
4040 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004041report_state(char *action,
4042 regsub_T *sub,
4043 nfa_state_T *state,
4044 int lid,
4045 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004046{
4047 int col;
4048
4049 if (sub->in_use <= 0)
4050 col = -1;
4051 else if (REG_MULTI)
4052 col = sub->list.multi[0].start.col;
4053 else
4054 col = (int)(sub->list.line[0].start - regline);
4055 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004056 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4057 action, abs(state->id), lid, state->c, code, col,
4058 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004059}
4060#endif
4061
Bram Moolenaar43e02982013-06-07 17:31:29 +02004062/*
4063 * Return TRUE if the same state is already in list "l" with the same
4064 * positions as "subs".
4065 */
4066 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02004067has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02004068 nfa_list_T *l; /* runtime state list */
4069 nfa_state_T *state; /* state to update */
4070 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004071 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004072{
4073 nfa_thread_T *thread;
4074 int i;
4075
4076 for (i = 0; i < l->n; ++i)
4077 {
4078 thread = &l->t[i];
4079 if (thread->state->id == state->id
4080 && sub_equal(&thread->subs.norm, &subs->norm)
4081#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004082 && (!nfa_has_zsubexpr
4083 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004084#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004085 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004086 return TRUE;
4087 }
4088 return FALSE;
4089}
4090
4091/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004092 * Return TRUE if "one" and "two" are equal. That includes when both are not
4093 * set.
4094 */
4095 static int
4096pim_equal(one, two)
4097 nfa_pim_T *one;
4098 nfa_pim_T *two;
4099{
4100 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4101 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4102
4103 if (one_unused)
4104 /* one is unused: equal when two is also unused */
4105 return two_unused;
4106 if (two_unused)
4107 /* one is used and two is not: not equal */
4108 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004109 /* compare the state id */
4110 if (one->state->id != two->state->id)
4111 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004112 /* compare the position */
4113 if (REG_MULTI)
4114 return one->end.pos.lnum == two->end.pos.lnum
4115 && one->end.pos.col == two->end.pos.col;
4116 return one->end.ptr == two->end.ptr;
4117}
4118
4119/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004120 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4121 */
4122 static int
4123match_follows(startstate, depth)
4124 nfa_state_T *startstate;
4125 int depth;
4126{
4127 nfa_state_T *state = startstate;
4128
4129 /* avoid too much recursion */
4130 if (depth > 10)
4131 return FALSE;
4132
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004133 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004134 {
4135 switch (state->c)
4136 {
4137 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004138 case NFA_MCLOSE:
4139 case NFA_END_INVISIBLE:
4140 case NFA_END_INVISIBLE_NEG:
4141 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004142 return TRUE;
4143
4144 case NFA_SPLIT:
4145 return match_follows(state->out, depth + 1)
4146 || match_follows(state->out1, depth + 1);
4147
4148 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004149 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004150 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004151 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004152 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004153 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004154 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004155 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004156 case NFA_COMPOSING:
4157 /* skip ahead to next state */
4158 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004159 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004160
4161 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004162 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004163 case NFA_IDENT:
4164 case NFA_SIDENT:
4165 case NFA_KWORD:
4166 case NFA_SKWORD:
4167 case NFA_FNAME:
4168 case NFA_SFNAME:
4169 case NFA_PRINT:
4170 case NFA_SPRINT:
4171 case NFA_WHITE:
4172 case NFA_NWHITE:
4173 case NFA_DIGIT:
4174 case NFA_NDIGIT:
4175 case NFA_HEX:
4176 case NFA_NHEX:
4177 case NFA_OCTAL:
4178 case NFA_NOCTAL:
4179 case NFA_WORD:
4180 case NFA_NWORD:
4181 case NFA_HEAD:
4182 case NFA_NHEAD:
4183 case NFA_ALPHA:
4184 case NFA_NALPHA:
4185 case NFA_LOWER:
4186 case NFA_NLOWER:
4187 case NFA_UPPER:
4188 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004189 case NFA_LOWER_IC:
4190 case NFA_NLOWER_IC:
4191 case NFA_UPPER_IC:
4192 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004193 case NFA_START_COLL:
4194 case NFA_START_NEG_COLL:
4195 case NFA_NEWL:
4196 /* state will advance input */
4197 return FALSE;
4198
4199 default:
4200 if (state->c > 0)
4201 /* state will advance input */
4202 return FALSE;
4203
4204 /* Others: zero-width or possibly zero-width, might still find
4205 * a match at the same position, keep looking. */
4206 break;
4207 }
4208 state = state->out;
4209 }
4210 return FALSE;
4211}
4212
4213
4214/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004215 * Return TRUE if "state" is already in list "l".
4216 */
4217 static int
4218state_in_list(l, state, subs)
4219 nfa_list_T *l; /* runtime state list */
4220 nfa_state_T *state; /* state to update */
4221 regsubs_T *subs; /* pointers to subexpressions */
4222{
4223 if (state->lastlist[nfa_ll_index] == l->id)
4224 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004225 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004226 return TRUE;
4227 }
4228 return FALSE;
4229}
4230
Bram Moolenaard05bf562013-06-30 23:24:08 +02004231/*
4232 * Add "state" and possibly what follows to state list ".".
4233 * Returns "subs_arg", possibly copied into temp_subs.
4234 */
4235
4236 static regsubs_T *
4237addstate(l, state, subs_arg, pim, off)
4238 nfa_list_T *l; /* runtime state list */
4239 nfa_state_T *state; /* state to update */
4240 regsubs_T *subs_arg; /* pointers to subexpressions */
4241 nfa_pim_T *pim; /* postponed look-behind match */
4242 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004243{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004244 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004245 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004246 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004247 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004248 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004249 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004250 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004251 regsubs_T *subs = subs_arg;
4252 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004253#ifdef ENABLE_LOG
4254 int did_print = FALSE;
4255#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004256
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004257 switch (state->c)
4258 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004259 case NFA_NCLOSE:
4260 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004261 case NFA_MCLOSE1:
4262 case NFA_MCLOSE2:
4263 case NFA_MCLOSE3:
4264 case NFA_MCLOSE4:
4265 case NFA_MCLOSE5:
4266 case NFA_MCLOSE6:
4267 case NFA_MCLOSE7:
4268 case NFA_MCLOSE8:
4269 case NFA_MCLOSE9:
4270#ifdef FEAT_SYN_HL
4271 case NFA_ZCLOSE:
4272 case NFA_ZCLOSE1:
4273 case NFA_ZCLOSE2:
4274 case NFA_ZCLOSE3:
4275 case NFA_ZCLOSE4:
4276 case NFA_ZCLOSE5:
4277 case NFA_ZCLOSE6:
4278 case NFA_ZCLOSE7:
4279 case NFA_ZCLOSE8:
4280 case NFA_ZCLOSE9:
4281#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004282 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004283 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004284 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004285 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004286 /* These nodes are not added themselves but their "out" and/or
4287 * "out1" may be added below. */
4288 break;
4289
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004290 case NFA_BOL:
4291 case NFA_BOF:
4292 /* "^" won't match past end-of-line, don't bother trying.
4293 * Except when at the end of the line, or when we are going to the
4294 * next line for a look-behind match. */
4295 if (reginput > regline
4296 && *reginput != NUL
4297 && (nfa_endp == NULL
4298 || !REG_MULTI
4299 || reglnum == nfa_endp->se_u.pos.lnum))
4300 goto skip_add;
4301 /* FALLTHROUGH */
4302
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004303 case NFA_MOPEN1:
4304 case NFA_MOPEN2:
4305 case NFA_MOPEN3:
4306 case NFA_MOPEN4:
4307 case NFA_MOPEN5:
4308 case NFA_MOPEN6:
4309 case NFA_MOPEN7:
4310 case NFA_MOPEN8:
4311 case NFA_MOPEN9:
4312#ifdef FEAT_SYN_HL
4313 case NFA_ZOPEN:
4314 case NFA_ZOPEN1:
4315 case NFA_ZOPEN2:
4316 case NFA_ZOPEN3:
4317 case NFA_ZOPEN4:
4318 case NFA_ZOPEN5:
4319 case NFA_ZOPEN6:
4320 case NFA_ZOPEN7:
4321 case NFA_ZOPEN8:
4322 case NFA_ZOPEN9:
4323#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004324 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004325 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004326 /* These nodes need to be added so that we can bail out when it
4327 * was added to this list before at the same position to avoid an
4328 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004330 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004331 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004332 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004333 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004334 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004335 * when there is a PIM. For NFA_MATCH check the position,
4336 * lower position is preferred. */
4337 if (!nfa_has_backref && pim == NULL && !l->has_pim
4338 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004339 {
4340skip_add:
4341#ifdef ENABLE_LOG
4342 nfa_set_code(state->c);
4343 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4344 abs(state->id), l->id, state->c, code);
4345#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004346 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004347 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004348
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004349 /* Do not add the state again when it exists with the same
4350 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004351 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004352 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004353 }
4354
Bram Moolenaara0169122013-06-26 18:16:58 +02004355 /* When there are backreferences or PIMs the number of states may
4356 * be (a lot) bigger than anticipated. */
4357 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004358 {
4359 int newlen = l->len * 3 / 2 + 50;
4360
Bram Moolenaard05bf562013-06-30 23:24:08 +02004361 if (subs != &temp_subs)
4362 {
4363 /* "subs" may point into the current array, need to make a
4364 * copy before it becomes invalid. */
4365 copy_sub(&temp_subs.norm, &subs->norm);
4366#ifdef FEAT_SYN_HL
4367 if (nfa_has_zsubexpr)
4368 copy_sub(&temp_subs.synt, &subs->synt);
4369#endif
4370 subs = &temp_subs;
4371 }
4372
Bram Moolenaar428e9872013-05-30 17:05:39 +02004373 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4374 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004375 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004376
4377 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004378 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004379 thread = &l->t[l->n++];
4380 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004381 if (pim == NULL)
4382 thread->pim.result = NFA_PIM_UNUSED;
4383 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004384 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004385 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004386 l->has_pim = TRUE;
4387 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004388 copy_sub(&thread->subs.norm, &subs->norm);
4389#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004390 if (nfa_has_zsubexpr)
4391 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004392#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004393#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004394 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004395 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004396#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004397 }
4398
4399#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004400 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004401 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004402#endif
4403 switch (state->c)
4404 {
4405 case NFA_MATCH:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004406// nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004407 break;
4408
4409 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004410 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004411 subs = addstate(l, state->out, subs, pim, off);
4412 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004413 break;
4414
Bram Moolenaar699c1202013-09-25 16:41:54 +02004415 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004416 case NFA_NOPEN:
4417 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004418 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004419 break;
4420
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004421 case NFA_MOPEN:
4422 case NFA_MOPEN1:
4423 case NFA_MOPEN2:
4424 case NFA_MOPEN3:
4425 case NFA_MOPEN4:
4426 case NFA_MOPEN5:
4427 case NFA_MOPEN6:
4428 case NFA_MOPEN7:
4429 case NFA_MOPEN8:
4430 case NFA_MOPEN9:
4431#ifdef FEAT_SYN_HL
4432 case NFA_ZOPEN:
4433 case NFA_ZOPEN1:
4434 case NFA_ZOPEN2:
4435 case NFA_ZOPEN3:
4436 case NFA_ZOPEN4:
4437 case NFA_ZOPEN5:
4438 case NFA_ZOPEN6:
4439 case NFA_ZOPEN7:
4440 case NFA_ZOPEN8:
4441 case NFA_ZOPEN9:
4442#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004443 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004444 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004445 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004446 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004447 sub = &subs->norm;
4448 }
4449#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004450 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004451 {
4452 subidx = state->c - NFA_ZOPEN;
4453 sub = &subs->synt;
4454 }
4455#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004456 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004457 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004458 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004459 sub = &subs->norm;
4460 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004461
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004462 /* avoid compiler warnings */
4463 save_ptr = NULL;
4464 save_lpos.lnum = 0;
4465 save_lpos.col = 0;
4466
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004467 /* Set the position (with "off" added) in the subexpression. Save
4468 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004469 if (REG_MULTI)
4470 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004471 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004472 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004473 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004474 save_in_use = -1;
4475 }
4476 else
4477 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004478 save_in_use = sub->in_use;
4479 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004480 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004481 sub->list.multi[i].start.lnum = -1;
4482 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004483 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004484 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004485 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004486 if (off == -1)
4487 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004488 sub->list.multi[subidx].start.lnum = reglnum + 1;
4489 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004490 }
4491 else
4492 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004493 sub->list.multi[subidx].start.lnum = reglnum;
4494 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004495 (colnr_T)(reginput - regline + off);
4496 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004497 }
4498 else
4499 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004500 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004501 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004502 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004503 save_in_use = -1;
4504 }
4505 else
4506 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004507 save_in_use = sub->in_use;
4508 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004509 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004510 sub->list.line[i].start = NULL;
4511 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004512 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004513 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004514 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004515 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004516 }
4517
Bram Moolenaard05bf562013-06-30 23:24:08 +02004518 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004519 /* "subs" may have changed, need to set "sub" again */
4520#ifdef FEAT_SYN_HL
4521 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4522 sub = &subs->synt;
4523 else
4524#endif
4525 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004526
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004527 if (save_in_use == -1)
4528 {
4529 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004530 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004531 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004532 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004533 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004534 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004535 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004536 break;
4537
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004538 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004539 if (nfa_has_zend && (REG_MULTI
4540 ? subs->norm.list.multi[0].end.lnum >= 0
4541 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004542 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004543 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004544 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004545 break;
4546 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004547 case NFA_MCLOSE1:
4548 case NFA_MCLOSE2:
4549 case NFA_MCLOSE3:
4550 case NFA_MCLOSE4:
4551 case NFA_MCLOSE5:
4552 case NFA_MCLOSE6:
4553 case NFA_MCLOSE7:
4554 case NFA_MCLOSE8:
4555 case NFA_MCLOSE9:
4556#ifdef FEAT_SYN_HL
4557 case NFA_ZCLOSE:
4558 case NFA_ZCLOSE1:
4559 case NFA_ZCLOSE2:
4560 case NFA_ZCLOSE3:
4561 case NFA_ZCLOSE4:
4562 case NFA_ZCLOSE5:
4563 case NFA_ZCLOSE6:
4564 case NFA_ZCLOSE7:
4565 case NFA_ZCLOSE8:
4566 case NFA_ZCLOSE9:
4567#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004568 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004569 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004570 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004571 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004572 sub = &subs->norm;
4573 }
4574#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004575 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004576 {
4577 subidx = state->c - NFA_ZCLOSE;
4578 sub = &subs->synt;
4579 }
4580#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004581 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004582 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004583 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004584 sub = &subs->norm;
4585 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004586
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004587 /* We don't fill in gaps here, there must have been an MOPEN that
4588 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004589 save_in_use = sub->in_use;
4590 if (sub->in_use <= subidx)
4591 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004592 if (REG_MULTI)
4593 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004594 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004595 if (off == -1)
4596 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004597 sub->list.multi[subidx].end.lnum = reglnum + 1;
4598 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004599 }
4600 else
4601 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004602 sub->list.multi[subidx].end.lnum = reglnum;
4603 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004604 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004605 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004606 /* avoid compiler warnings */
4607 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004608 }
4609 else
4610 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004611 save_ptr = sub->list.line[subidx].end;
4612 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004613 /* avoid compiler warnings */
4614 save_lpos.lnum = 0;
4615 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004616 }
4617
Bram Moolenaard05bf562013-06-30 23:24:08 +02004618 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004619 /* "subs" may have changed, need to set "sub" again */
4620#ifdef FEAT_SYN_HL
4621 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4622 sub = &subs->synt;
4623 else
4624#endif
4625 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004626
4627 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004628 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004630 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004631 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004632 break;
4633 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004634 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004635}
4636
4637/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004638 * Like addstate(), but the new state(s) are put at position "*ip".
4639 * Used for zero-width matches, next state to use is the added one.
4640 * This makes sure the order of states to be tried does not change, which
4641 * matters for alternatives.
4642 */
4643 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004644addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004645 nfa_list_T *l; /* runtime state list */
4646 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004647 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004648 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004649 int *ip;
4650{
4651 int tlen = l->n;
4652 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004653 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004654
4655 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004656 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004657
Bram Moolenaar4b417062013-05-25 20:19:50 +02004658 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004659 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004660 return;
4661
4662 /* re-order to put the new state at the current position */
4663 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004664 if (count == 0)
4665 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004666 if (count == 1)
4667 {
4668 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004669 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004670 }
4671 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004672 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004673 if (l->n + count - 1 >= l->len)
4674 {
4675 /* not enough space to move the new states, reallocate the list
4676 * and move the states to the right position */
4677 nfa_thread_T *newl;
4678
4679 l->len = l->len * 3 / 2 + 50;
4680 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4681 if (newl == NULL)
4682 return;
4683 mch_memmove(&(newl[0]),
4684 &(l->t[0]),
4685 sizeof(nfa_thread_T) * listidx);
4686 mch_memmove(&(newl[listidx]),
4687 &(l->t[l->n - count]),
4688 sizeof(nfa_thread_T) * count);
4689 mch_memmove(&(newl[listidx + count]),
4690 &(l->t[listidx + 1]),
4691 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4692 vim_free(l->t);
4693 l->t = newl;
4694 }
4695 else
4696 {
4697 /* make space for new states, then move them from the
4698 * end to the current position */
4699 mch_memmove(&(l->t[listidx + count]),
4700 &(l->t[listidx + 1]),
4701 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4702 mch_memmove(&(l->t[listidx]),
4703 &(l->t[l->n - 1]),
4704 sizeof(nfa_thread_T) * count);
4705 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004706 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004707 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004708 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004709}
4710
4711/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004712 * Check character class "class" against current character c.
4713 */
4714 static int
4715check_char_class(class, c)
4716 int class;
4717 int c;
4718{
4719 switch (class)
4720 {
4721 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004722 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004723 return OK;
4724 break;
4725 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004726 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004727 return OK;
4728 break;
4729 case NFA_CLASS_BLANK:
4730 if (c == ' ' || c == '\t')
4731 return OK;
4732 break;
4733 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004734 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004735 return OK;
4736 break;
4737 case NFA_CLASS_DIGIT:
4738 if (VIM_ISDIGIT(c))
4739 return OK;
4740 break;
4741 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004742 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004743 return OK;
4744 break;
4745 case NFA_CLASS_LOWER:
4746 if (MB_ISLOWER(c))
4747 return OK;
4748 break;
4749 case NFA_CLASS_PRINT:
4750 if (vim_isprintc(c))
4751 return OK;
4752 break;
4753 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004754 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004755 return OK;
4756 break;
4757 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004758 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004759 return OK;
4760 break;
4761 case NFA_CLASS_UPPER:
4762 if (MB_ISUPPER(c))
4763 return OK;
4764 break;
4765 case NFA_CLASS_XDIGIT:
4766 if (vim_isxdigit(c))
4767 return OK;
4768 break;
4769 case NFA_CLASS_TAB:
4770 if (c == '\t')
4771 return OK;
4772 break;
4773 case NFA_CLASS_RETURN:
4774 if (c == '\r')
4775 return OK;
4776 break;
4777 case NFA_CLASS_BACKSPACE:
4778 if (c == '\b')
4779 return OK;
4780 break;
4781 case NFA_CLASS_ESCAPE:
4782 if (c == '\033')
4783 return OK;
4784 break;
4785
4786 default:
4787 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004788 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004789 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004790 }
4791 return FAIL;
4792}
4793
Bram Moolenaar5714b802013-05-28 22:03:20 +02004794/*
4795 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004796 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004797 */
4798 static int
4799match_backref(sub, subidx, bytelen)
4800 regsub_T *sub; /* pointers to subexpressions */
4801 int subidx;
4802 int *bytelen; /* out: length of match in bytes */
4803{
4804 int len;
4805
4806 if (sub->in_use <= subidx)
4807 {
4808retempty:
4809 /* backref was not set, match an empty string */
4810 *bytelen = 0;
4811 return TRUE;
4812 }
4813
4814 if (REG_MULTI)
4815 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004816 if (sub->list.multi[subidx].start.lnum < 0
4817 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004818 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004819 if (sub->list.multi[subidx].start.lnum == reglnum
4820 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004821 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004822 len = sub->list.multi[subidx].end.col
4823 - sub->list.multi[subidx].start.col;
4824 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4825 reginput, &len) == 0)
4826 {
4827 *bytelen = len;
4828 return TRUE;
4829 }
4830 }
4831 else
4832 {
4833 if (match_with_backref(
4834 sub->list.multi[subidx].start.lnum,
4835 sub->list.multi[subidx].start.col,
4836 sub->list.multi[subidx].end.lnum,
4837 sub->list.multi[subidx].end.col,
4838 bytelen) == RA_MATCH)
4839 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004840 }
4841 }
4842 else
4843 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004844 if (sub->list.line[subidx].start == NULL
4845 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004846 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004847 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4848 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004849 {
4850 *bytelen = len;
4851 return TRUE;
4852 }
4853 }
4854 return FALSE;
4855}
4856
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004857#ifdef FEAT_SYN_HL
4858
4859static int match_zref __ARGS((int subidx, int *bytelen));
4860
4861/*
4862 * Check for a match with \z subexpression "subidx".
4863 * Return TRUE if it matches.
4864 */
4865 static int
4866match_zref(subidx, bytelen)
4867 int subidx;
4868 int *bytelen; /* out: length of match in bytes */
4869{
4870 int len;
4871
4872 cleanup_zsubexpr();
4873 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4874 {
4875 /* backref was not set, match an empty string */
4876 *bytelen = 0;
4877 return TRUE;
4878 }
4879
4880 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4881 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4882 {
4883 *bytelen = len;
4884 return TRUE;
4885 }
4886 return FALSE;
4887}
4888#endif
4889
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004890/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004891 * Save list IDs for all NFA states of "prog" into "list".
4892 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004893 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004894 */
4895 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004896nfa_save_listids(prog, list)
4897 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004898 int *list;
4899{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004900 int i;
4901 nfa_state_T *p;
4902
4903 /* Order in the list is reverse, it's a bit faster that way. */
4904 p = &prog->state[0];
4905 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004906 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004907 list[i] = p->lastlist[1];
4908 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004909 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004910 }
4911}
4912
4913/*
4914 * Restore list IDs from "list" to all NFA states.
4915 */
4916 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004917nfa_restore_listids(prog, list)
4918 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004919 int *list;
4920{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004921 int i;
4922 nfa_state_T *p;
4923
4924 p = &prog->state[0];
4925 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004926 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004927 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004928 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004929 }
4930}
4931
Bram Moolenaar423532e2013-05-29 21:14:42 +02004932 static int
4933nfa_re_num_cmp(val, op, pos)
4934 long_u val;
4935 int op;
4936 long_u pos;
4937{
4938 if (op == 1) return pos > val;
4939 if (op == 2) return pos < val;
4940 return val == pos;
4941}
4942
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004943static 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 +02004944static 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 +02004945
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004946/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004947 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004948 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4949 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004950 */
4951 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004952recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004953 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004954 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004955 nfa_regprog_T *prog;
4956 regsubs_T *submatch;
4957 regsubs_T *m;
4958 int **listids;
4959{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004960 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004961 int save_reglnum = reglnum;
4962 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004963 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004964 save_se_T *save_nfa_endp = nfa_endp;
4965 save_se_T endpos;
4966 save_se_T *endposp = NULL;
4967 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004968 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004969
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004970 if (pim != NULL)
4971 {
4972 /* start at the position where the postponed match was */
4973 if (REG_MULTI)
4974 reginput = regline + pim->end.pos.col;
4975 else
4976 reginput = pim->end.ptr;
4977 }
4978
Bram Moolenaardecd9542013-06-07 16:31:50 +02004979 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004980 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4981 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4982 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004983 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004984 /* The recursive match must end at the current position. When "pim" is
4985 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004986 endposp = &endpos;
4987 if (REG_MULTI)
4988 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004989 if (pim == NULL)
4990 {
4991 endpos.se_u.pos.col = (int)(reginput - regline);
4992 endpos.se_u.pos.lnum = reglnum;
4993 }
4994 else
4995 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004996 }
4997 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004998 {
4999 if (pim == NULL)
5000 endpos.se_u.ptr = reginput;
5001 else
5002 endpos.se_u.ptr = pim->end.ptr;
5003 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005004
5005 /* Go back the specified number of bytes, or as far as the
5006 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005007 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005008 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005009 if (state->val <= 0)
5010 {
5011 if (REG_MULTI)
5012 {
5013 regline = reg_getline(--reglnum);
5014 if (regline == NULL)
5015 /* can't go before the first line */
5016 regline = reg_getline(++reglnum);
5017 }
5018 reginput = regline;
5019 }
5020 else
5021 {
5022 if (REG_MULTI && (int)(reginput - regline) < state->val)
5023 {
5024 /* Not enough bytes in this line, go to end of
5025 * previous line. */
5026 regline = reg_getline(--reglnum);
5027 if (regline == NULL)
5028 {
5029 /* can't go before the first line */
5030 regline = reg_getline(++reglnum);
5031 reginput = regline;
5032 }
5033 else
5034 reginput = regline + STRLEN(regline);
5035 }
5036 if ((int)(reginput - regline) >= state->val)
5037 {
5038 reginput -= state->val;
5039#ifdef FEAT_MBYTE
5040 if (has_mbyte)
5041 reginput -= mb_head_off(regline, reginput);
5042#endif
5043 }
5044 else
5045 reginput = regline;
5046 }
5047 }
5048
Bram Moolenaarf46da702013-06-02 22:37:42 +02005049#ifdef ENABLE_LOG
5050 if (log_fd != stderr)
5051 fclose(log_fd);
5052 log_fd = NULL;
5053#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005054 /* Have to clear the lastlist field of the NFA nodes, so that
5055 * nfa_regmatch() and addstate() can run properly after recursion. */
5056 if (nfa_ll_index == 1)
5057 {
5058 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5059 * values and clear them. */
5060 if (*listids == NULL)
5061 {
5062 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5063 if (*listids == NULL)
5064 {
5065 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5066 return 0;
5067 }
5068 }
5069 nfa_save_listids(prog, *listids);
5070 need_restore = TRUE;
5071 /* any value of nfa_listid will do */
5072 }
5073 else
5074 {
5075 /* First recursive nfa_regmatch() call, switch to the second lastlist
5076 * entry. Make sure nfa_listid is different from a previous recursive
5077 * call, because some states may still have this ID. */
5078 ++nfa_ll_index;
5079 if (nfa_listid <= nfa_alt_listid)
5080 nfa_listid = nfa_alt_listid;
5081 }
5082
5083 /* Call nfa_regmatch() to check if the current concat matches at this
5084 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005085 nfa_endp = endposp;
5086 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005087
5088 if (need_restore)
5089 nfa_restore_listids(prog, *listids);
5090 else
5091 {
5092 --nfa_ll_index;
5093 nfa_alt_listid = nfa_listid;
5094 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005095
5096 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005097 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005098 if (REG_MULTI)
5099 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005100 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005101 nfa_match = save_nfa_match;
5102 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005103 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005104
5105#ifdef ENABLE_LOG
5106 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5107 if (log_fd != NULL)
5108 {
5109 fprintf(log_fd, "****************************\n");
5110 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5111 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5112 fprintf(log_fd, "****************************\n");
5113 }
5114 else
5115 {
5116 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5117 log_fd = stderr;
5118 }
5119#endif
5120
5121 return result;
5122}
5123
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005124static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005125static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005126
5127/*
5128 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005129 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005130 * NFA_ANY: 1
5131 * specific character: 99
5132 */
5133 static int
5134failure_chance(state, depth)
5135 nfa_state_T *state;
5136 int depth;
5137{
5138 int c = state->c;
5139 int l, r;
5140
5141 /* detect looping */
5142 if (depth > 4)
5143 return 1;
5144
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005145 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005146 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005147 case NFA_SPLIT:
5148 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5149 /* avoid recursive stuff */
5150 return 1;
5151 /* two alternatives, use the lowest failure chance */
5152 l = failure_chance(state->out, depth + 1);
5153 r = failure_chance(state->out1, depth + 1);
5154 return l < r ? l : r;
5155
5156 case NFA_ANY:
5157 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005158 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005159
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005160 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005161 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005162 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005163 /* empty match works always */
5164 return 0;
5165
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005166 case NFA_START_INVISIBLE:
5167 case NFA_START_INVISIBLE_FIRST:
5168 case NFA_START_INVISIBLE_NEG:
5169 case NFA_START_INVISIBLE_NEG_FIRST:
5170 case NFA_START_INVISIBLE_BEFORE:
5171 case NFA_START_INVISIBLE_BEFORE_FIRST:
5172 case NFA_START_INVISIBLE_BEFORE_NEG:
5173 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5174 case NFA_START_PATTERN:
5175 /* recursive regmatch is expensive, use low failure chance */
5176 return 5;
5177
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005178 case NFA_BOL:
5179 case NFA_EOL:
5180 case NFA_BOF:
5181 case NFA_EOF:
5182 case NFA_NEWL:
5183 return 99;
5184
5185 case NFA_BOW:
5186 case NFA_EOW:
5187 return 90;
5188
5189 case NFA_MOPEN:
5190 case NFA_MOPEN1:
5191 case NFA_MOPEN2:
5192 case NFA_MOPEN3:
5193 case NFA_MOPEN4:
5194 case NFA_MOPEN5:
5195 case NFA_MOPEN6:
5196 case NFA_MOPEN7:
5197 case NFA_MOPEN8:
5198 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005199#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005200 case NFA_ZOPEN:
5201 case NFA_ZOPEN1:
5202 case NFA_ZOPEN2:
5203 case NFA_ZOPEN3:
5204 case NFA_ZOPEN4:
5205 case NFA_ZOPEN5:
5206 case NFA_ZOPEN6:
5207 case NFA_ZOPEN7:
5208 case NFA_ZOPEN8:
5209 case NFA_ZOPEN9:
5210 case NFA_ZCLOSE:
5211 case NFA_ZCLOSE1:
5212 case NFA_ZCLOSE2:
5213 case NFA_ZCLOSE3:
5214 case NFA_ZCLOSE4:
5215 case NFA_ZCLOSE5:
5216 case NFA_ZCLOSE6:
5217 case NFA_ZCLOSE7:
5218 case NFA_ZCLOSE8:
5219 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005220#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005221 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005222 case NFA_MCLOSE1:
5223 case NFA_MCLOSE2:
5224 case NFA_MCLOSE3:
5225 case NFA_MCLOSE4:
5226 case NFA_MCLOSE5:
5227 case NFA_MCLOSE6:
5228 case NFA_MCLOSE7:
5229 case NFA_MCLOSE8:
5230 case NFA_MCLOSE9:
5231 case NFA_NCLOSE:
5232 return failure_chance(state->out, depth + 1);
5233
5234 case NFA_BACKREF1:
5235 case NFA_BACKREF2:
5236 case NFA_BACKREF3:
5237 case NFA_BACKREF4:
5238 case NFA_BACKREF5:
5239 case NFA_BACKREF6:
5240 case NFA_BACKREF7:
5241 case NFA_BACKREF8:
5242 case NFA_BACKREF9:
5243#ifdef FEAT_SYN_HL
5244 case NFA_ZREF1:
5245 case NFA_ZREF2:
5246 case NFA_ZREF3:
5247 case NFA_ZREF4:
5248 case NFA_ZREF5:
5249 case NFA_ZREF6:
5250 case NFA_ZREF7:
5251 case NFA_ZREF8:
5252 case NFA_ZREF9:
5253#endif
5254 /* backreferences don't match in many places */
5255 return 94;
5256
5257 case NFA_LNUM_GT:
5258 case NFA_LNUM_LT:
5259 case NFA_COL_GT:
5260 case NFA_COL_LT:
5261 case NFA_VCOL_GT:
5262 case NFA_VCOL_LT:
5263 case NFA_MARK_GT:
5264 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005265 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005266 /* before/after positions don't match very often */
5267 return 85;
5268
5269 case NFA_LNUM:
5270 return 90;
5271
5272 case NFA_CURSOR:
5273 case NFA_COL:
5274 case NFA_VCOL:
5275 case NFA_MARK:
5276 /* specific positions rarely match */
5277 return 98;
5278
5279 case NFA_COMPOSING:
5280 return 95;
5281
5282 default:
5283 if (c > 0)
5284 /* character match fails often */
5285 return 95;
5286 }
5287
5288 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005289 return 50;
5290}
5291
Bram Moolenaarf46da702013-06-02 22:37:42 +02005292/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005293 * Skip until the char "c" we know a match must start with.
5294 */
5295 static int
5296skip_to_start(c, colp)
5297 int c;
5298 colnr_T *colp;
5299{
5300 char_u *s;
5301
5302 /* Used often, do some work to avoid call overhead. */
5303 if (!ireg_ic
5304#ifdef FEAT_MBYTE
5305 && !has_mbyte
5306#endif
5307 )
5308 s = vim_strbyte(regline + *colp, c);
5309 else
5310 s = cstrchr(regline + *colp, c);
5311 if (s == NULL)
5312 return FAIL;
5313 *colp = (int)(s - regline);
5314 return OK;
5315}
5316
5317/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005318 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005319 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005320 * Returns zero for no match, 1 for a match.
5321 */
5322 static long
5323find_match_text(startcol, regstart, match_text)
5324 colnr_T startcol;
5325 int regstart;
5326 char_u *match_text;
5327{
5328 colnr_T col = startcol;
5329 int c1, c2;
5330 int len1, len2;
5331 int match;
5332
5333 for (;;)
5334 {
5335 match = TRUE;
5336 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5337 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5338 {
5339 c1 = PTR2CHAR(match_text + len1);
5340 c2 = PTR2CHAR(regline + col + len2);
5341 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5342 {
5343 match = FALSE;
5344 break;
5345 }
5346 len2 += MB_CHAR2LEN(c2);
5347 }
5348 if (match
5349#ifdef FEAT_MBYTE
5350 /* check that no composing char follows */
5351 && !(enc_utf8
5352 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5353#endif
5354 )
5355 {
5356 cleanup_subexpr();
5357 if (REG_MULTI)
5358 {
5359 reg_startpos[0].lnum = reglnum;
5360 reg_startpos[0].col = col;
5361 reg_endpos[0].lnum = reglnum;
5362 reg_endpos[0].col = col + len2;
5363 }
5364 else
5365 {
5366 reg_startp[0] = regline + col;
5367 reg_endp[0] = regline + col + len2;
5368 }
5369 return 1L;
5370 }
5371
5372 /* Try finding regstart after the current match. */
5373 col += MB_CHAR2LEN(regstart); /* skip regstart */
5374 if (skip_to_start(regstart, &col) == FAIL)
5375 break;
5376 }
5377 return 0L;
5378}
5379
5380/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005381 * Main matching routine.
5382 *
5383 * Run NFA to determine whether it matches reginput.
5384 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005385 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005386 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005387 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005388 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005389 * Note: Caller must ensure that: start != NULL.
5390 */
5391 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005392nfa_regmatch(prog, start, submatch, m)
5393 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005394 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005395 regsubs_T *submatch;
5396 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005397{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005398 int result;
5399 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005400 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005401 int go_to_nextline = FALSE;
5402 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005403 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005404 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005405 nfa_list_T *thislist;
5406 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005407 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005408 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005409 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005410 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005411 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005412 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005413#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005414 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005415
5416 if (debug == NULL)
5417 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005418 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005419 return FALSE;
5420 }
5421#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005422 /* Some patterns may take a long time to match, especially when using
5423 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5424 fast_breakcheck();
5425 if (got_int)
5426 return FALSE;
5427
Bram Moolenaar963fee22013-05-26 21:47:28 +02005428 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005429
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005430 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005431 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005432 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005433 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005434 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005435 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005436 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005437 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005438
5439#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005440 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005441 if (log_fd != NULL)
5442 {
5443 fprintf(log_fd, "**********************************\n");
5444 nfa_set_code(start->c);
5445 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5446 abs(start->id), code);
5447 fprintf(log_fd, "**********************************\n");
5448 }
5449 else
5450 {
5451 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5452 log_fd = stderr;
5453 }
5454#endif
5455
5456 thislist = &list[0];
5457 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005458 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005459 nextlist = &list[1];
5460 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005461 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005462#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005463 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005464#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005465 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005466
5467 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5468 * it's the first MOPEN. */
5469 if (toplevel)
5470 {
5471 if (REG_MULTI)
5472 {
5473 m->norm.list.multi[0].start.lnum = reglnum;
5474 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5475 }
5476 else
5477 m->norm.list.line[0].start = reginput;
5478 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005479 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005480 }
5481 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005482 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005483
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005484#define ADD_STATE_IF_MATCH(state) \
5485 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005486 add_state = state->out; \
5487 add_off = clen; \
5488 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005489
5490 /*
5491 * Run for each character.
5492 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005493 for (;;)
5494 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005495 int curc;
5496 int clen;
5497
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005498#ifdef FEAT_MBYTE
5499 if (has_mbyte)
5500 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005501 curc = (*mb_ptr2char)(reginput);
5502 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005503 }
5504 else
5505#endif
5506 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005507 curc = *reginput;
5508 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005509 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005510 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005511 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005512 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005513 go_to_nextline = FALSE;
5514 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515
5516 /* swap lists */
5517 thislist = &list[flag];
5518 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005519 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005520 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005521 ++nfa_listid;
5522 thislist->id = nfa_listid;
5523 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005524
5525#ifdef ENABLE_LOG
5526 fprintf(log_fd, "------------------------------------------\n");
5527 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005528 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005529 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005530 {
5531 int i;
5532
5533 for (i = 0; i < thislist->n; i++)
5534 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5535 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005536 fprintf(log_fd, "\n");
5537#endif
5538
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005539#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005540 fprintf(debug, "\n-------------------\n");
5541#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005542 /*
5543 * If the state lists are empty we can stop.
5544 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005545 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005546 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005547
5548 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005549 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005551 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005552
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005553#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005554 nfa_set_code(t->state->c);
5555 fprintf(debug, "%s, ", code);
5556#endif
5557#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005558 {
5559 int col;
5560
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005561 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005562 col = -1;
5563 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005564 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005565 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005566 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005567 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005568 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5569 abs(t->state->id), (int)t->state->c, code, col,
5570 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005571 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005572#endif
5573
5574 /*
5575 * Handle the possible codes of the current state.
5576 * The most important is NFA_MATCH.
5577 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005578 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005579 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005580 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005581 switch (t->state->c)
5582 {
5583 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005584 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005585#ifdef FEAT_MBYTE
5586 /* If the match ends before a composing characters and
5587 * ireg_icombine is not set, that is not really a match. */
5588 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5589 break;
5590#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005591 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005592 copy_sub(&submatch->norm, &t->subs.norm);
5593#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005594 if (nfa_has_zsubexpr)
5595 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005596#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005597#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005598 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005599#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005600 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005601 * states at this position. When the list of states is going
5602 * to be empty quit without advancing, so that "reginput" is
5603 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005604 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005605 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005606 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005607 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005608
5609 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005610 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005611 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005612 /*
5613 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005614 * NFA_START_INVISIBLE_BEFORE node.
5615 * They surround a zero-width group, used with "\@=", "\&",
5616 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005617 * If we got here, it means that the current "invisible" group
5618 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005619 * nfa_regmatch(). For a look-behind match only when it ends
5620 * in the position in "nfa_endp".
5621 * Submatches are stored in *m, and used in the parent call.
5622 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005623#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005624 if (nfa_endp != NULL)
5625 {
5626 if (REG_MULTI)
5627 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5628 (int)reglnum,
5629 (int)nfa_endp->se_u.pos.lnum,
5630 (int)(reginput - regline),
5631 nfa_endp->se_u.pos.col);
5632 else
5633 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5634 (int)(reginput - regline),
5635 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005636 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005637#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005638 /* If "nfa_endp" is set it's only a match if it ends at
5639 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005640 if (nfa_endp != NULL && (REG_MULTI
5641 ? (reglnum != nfa_endp->se_u.pos.lnum
5642 || (int)(reginput - regline)
5643 != nfa_endp->se_u.pos.col)
5644 : reginput != nfa_endp->se_u.ptr))
5645 break;
5646
5647 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005648 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005649 {
5650 copy_sub(&m->norm, &t->subs.norm);
5651#ifdef FEAT_SYN_HL
5652 if (nfa_has_zsubexpr)
5653 copy_sub(&m->synt, &t->subs.synt);
5654#endif
5655 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005656#ifdef ENABLE_LOG
5657 fprintf(log_fd, "Match found:\n");
5658 log_subsexpr(m);
5659#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005660 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005661 /* See comment above at "goto nextchar". */
5662 if (nextlist->n == 0)
5663 clen = 0;
5664 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005665
5666 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005667 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005668 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005669 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005670 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005671 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005672 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005673 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005674 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005675#ifdef ENABLE_LOG
5676 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5677 failure_chance(t->state->out, 0),
5678 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005679#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005680 /* Do it directly if there already is a PIM or when
5681 * nfa_postprocess() detected it will work better. */
5682 if (t->pim.result != NFA_PIM_UNUSED
5683 || t->state->c == NFA_START_INVISIBLE_FIRST
5684 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5685 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5686 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005687 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005688 int in_use = m->norm.in_use;
5689
Bram Moolenaar699c1202013-09-25 16:41:54 +02005690 /* Copy submatch info for the recursive call, opposite
5691 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005692 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005693#ifdef FEAT_SYN_HL
5694 if (nfa_has_zsubexpr)
5695 copy_sub_off(&m->synt, &t->subs.synt);
5696#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005697
Bram Moolenaara2d95102013-06-04 14:23:05 +02005698 /*
5699 * First try matching the invisible match, then what
5700 * follows.
5701 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005702 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005703 submatch, m, &listids);
5704
Bram Moolenaardecd9542013-06-07 16:31:50 +02005705 /* for \@! and \@<! it is a match when the result is
5706 * FALSE */
5707 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005708 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5709 || t->state->c
5710 == NFA_START_INVISIBLE_BEFORE_NEG
5711 || t->state->c
5712 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005713 {
5714 /* Copy submatch info from the recursive call */
5715 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005716#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005717 if (nfa_has_zsubexpr)
5718 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005719#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005720 /* If the pattern has \ze and it matched in the
5721 * sub pattern, use it. */
5722 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005723
Bram Moolenaara2d95102013-06-04 14:23:05 +02005724 /* t->state->out1 is the corresponding
5725 * END_INVISIBLE node; Add its out to the current
5726 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005727 add_here = TRUE;
5728 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005729 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005730 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005731 }
5732 else
5733 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005734 nfa_pim_T pim;
5735
Bram Moolenaara2d95102013-06-04 14:23:05 +02005736 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005737 * First try matching what follows. Only if a match
5738 * is found verify the invisible match matches. Add a
5739 * nfa_pim_T to the following states, it contains info
5740 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005741 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005742 pim.state = t->state;
5743 pim.result = NFA_PIM_TODO;
5744 pim.subs.norm.in_use = 0;
5745#ifdef FEAT_SYN_HL
5746 pim.subs.synt.in_use = 0;
5747#endif
5748 if (REG_MULTI)
5749 {
5750 pim.end.pos.col = (int)(reginput - regline);
5751 pim.end.pos.lnum = reglnum;
5752 }
5753 else
5754 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005755
5756 /* t->state->out1 is the corresponding END_INVISIBLE
5757 * node; Add its out to the current list (zero-width
5758 * match). */
5759 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005760 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005761 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005762 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005763 break;
5764
Bram Moolenaar87953742013-06-05 18:52:40 +02005765 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005766 {
5767 nfa_state_T *skip = NULL;
5768#ifdef ENABLE_LOG
5769 int skip_lid = 0;
5770#endif
5771
5772 /* There is no point in trying to match the pattern if the
5773 * output state is not going to be added to the list. */
5774 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5775 {
5776 skip = t->state->out1->out;
5777#ifdef ENABLE_LOG
5778 skip_lid = nextlist->id;
5779#endif
5780 }
5781 else if (state_in_list(nextlist,
5782 t->state->out1->out->out, &t->subs))
5783 {
5784 skip = t->state->out1->out->out;
5785#ifdef ENABLE_LOG
5786 skip_lid = nextlist->id;
5787#endif
5788 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005789 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005790 t->state->out1->out->out, &t->subs))
5791 {
5792 skip = t->state->out1->out->out;
5793#ifdef ENABLE_LOG
5794 skip_lid = thislist->id;
5795#endif
5796 }
5797 if (skip != NULL)
5798 {
5799#ifdef ENABLE_LOG
5800 nfa_set_code(skip->c);
5801 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5802 abs(skip->id), skip_lid, skip->c, code);
5803#endif
5804 break;
5805 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005806 /* Copy submatch info to the recursive call, opposite of what
5807 * happens afterwards. */
5808 copy_sub_off(&m->norm, &t->subs.norm);
5809#ifdef FEAT_SYN_HL
5810 if (nfa_has_zsubexpr)
5811 copy_sub_off(&m->synt, &t->subs.synt);
5812#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005813
Bram Moolenaar87953742013-06-05 18:52:40 +02005814 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005815 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005816 submatch, m, &listids);
5817 if (result)
5818 {
5819 int bytelen;
5820
5821#ifdef ENABLE_LOG
5822 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5823 log_subsexpr(m);
5824#endif
5825 /* Copy submatch info from the recursive call */
5826 copy_sub_off(&t->subs.norm, &m->norm);
5827#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005828 if (nfa_has_zsubexpr)
5829 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005830#endif
5831 /* Now we need to skip over the matched text and then
5832 * continue with what follows. */
5833 if (REG_MULTI)
5834 /* TODO: multi-line match */
5835 bytelen = m->norm.list.multi[0].end.col
5836 - (int)(reginput - regline);
5837 else
5838 bytelen = (int)(m->norm.list.line[0].end - reginput);
5839
5840#ifdef ENABLE_LOG
5841 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5842#endif
5843 if (bytelen == 0)
5844 {
5845 /* empty match, output of corresponding
5846 * NFA_END_PATTERN/NFA_SKIP to be used at current
5847 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005848 add_here = TRUE;
5849 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005850 }
5851 else if (bytelen <= clen)
5852 {
5853 /* match current character, output of corresponding
5854 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005855 add_state = t->state->out1->out->out;
5856 add_off = clen;
5857 }
5858 else
5859 {
5860 /* skip over the matched characters, set character
5861 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005862 add_state = t->state->out1->out;
5863 add_off = bytelen;
5864 add_count = bytelen - clen;
5865 }
5866 }
5867 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005868 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005869
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005870 case NFA_BOL:
5871 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005872 {
5873 add_here = TRUE;
5874 add_state = t->state->out;
5875 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005876 break;
5877
5878 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005879 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005880 {
5881 add_here = TRUE;
5882 add_state = t->state->out;
5883 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005884 break;
5885
5886 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005887 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005888
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005889 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005890 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005891#ifdef FEAT_MBYTE
5892 else if (has_mbyte)
5893 {
5894 int this_class;
5895
5896 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005897 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005898 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005899 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005900 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005901 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005902 }
5903#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005904 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005905 || (reginput > regline
5906 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005907 result = FALSE;
5908 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005909 {
5910 add_here = TRUE;
5911 add_state = t->state->out;
5912 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005913 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005914
5915 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005916 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005917 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005918 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005919#ifdef FEAT_MBYTE
5920 else if (has_mbyte)
5921 {
5922 int this_class, prev_class;
5923
5924 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005925 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005926 prev_class = reg_prev_class();
5927 if (this_class == prev_class
5928 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005929 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005930 }
5931#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005932 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005933 || (reginput[0] != NUL
5934 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005935 result = FALSE;
5936 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005937 {
5938 add_here = TRUE;
5939 add_state = t->state->out;
5940 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005941 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005942
Bram Moolenaar4b780632013-05-31 22:14:52 +02005943 case NFA_BOF:
5944 if (reglnum == 0 && reginput == regline
5945 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005946 {
5947 add_here = TRUE;
5948 add_state = t->state->out;
5949 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005950 break;
5951
5952 case NFA_EOF:
5953 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005954 {
5955 add_here = TRUE;
5956 add_state = t->state->out;
5957 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005958 break;
5959
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005960#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005961 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005962 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005963 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005964 int len = 0;
5965 nfa_state_T *end;
5966 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005967 int cchars[MAX_MCO];
5968 int ccount = 0;
5969 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005970
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005971 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005972 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005973 if (utf_iscomposing(sta->c))
5974 {
5975 /* Only match composing character(s), ignore base
5976 * character. Used for ".{composing}" and "{composing}"
5977 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005978 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005979 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005980 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005981 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005982 /* If \Z was present, then ignore composing characters.
5983 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005984 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005985 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005986 else
5987 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005988 while (sta->c != NFA_END_COMPOSING)
5989 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005990 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005991
5992 /* Check base character matches first, unless ignored. */
5993 else if (len > 0 || mc == sta->c)
5994 {
5995 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005996 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005997 len += mb_char2len(mc);
5998 sta = sta->out;
5999 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006000
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006001 /* We don't care about the order of composing characters.
6002 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006003 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006004 {
6005 mc = mb_ptr2char(reginput + len);
6006 cchars[ccount++] = mc;
6007 len += mb_char2len(mc);
6008 if (ccount == MAX_MCO)
6009 break;
6010 }
6011
6012 /* Check that each composing char in the pattern matches a
6013 * composing char in the text. We do not check if all
6014 * composing chars are matched. */
6015 result = OK;
6016 while (sta->c != NFA_END_COMPOSING)
6017 {
6018 for (j = 0; j < ccount; ++j)
6019 if (cchars[j] == sta->c)
6020 break;
6021 if (j == ccount)
6022 {
6023 result = FAIL;
6024 break;
6025 }
6026 sta = sta->out;
6027 }
6028 }
6029 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006030 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006031
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006032 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006033 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006034 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006035 }
6036#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006037
6038 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006039 if (curc == NUL && !reg_line_lbr && REG_MULTI
6040 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006041 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006042 go_to_nextline = TRUE;
6043 /* Pass -1 for the offset, which means taking the position
6044 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006045 add_state = t->state->out;
6046 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006047 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006048 else if (curc == '\n' && reg_line_lbr)
6049 {
6050 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006051 add_state = t->state->out;
6052 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006053 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054 break;
6055
Bram Moolenaar417bad22013-06-07 14:08:30 +02006056 case NFA_START_COLL:
6057 case NFA_START_NEG_COLL:
6058 {
6059 /* What follows is a list of characters, until NFA_END_COLL.
6060 * One of them must match or none of them must match. */
6061 nfa_state_T *state;
6062 int result_if_matched;
6063 int c1, c2;
6064
6065 /* Never match EOL. If it's part of the collection it is added
6066 * as a separate state with an OR. */
6067 if (curc == NUL)
6068 break;
6069
6070 state = t->state->out;
6071 result_if_matched = (t->state->c == NFA_START_COLL);
6072 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006073 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006074 if (state->c == NFA_END_COLL)
6075 {
6076 result = !result_if_matched;
6077 break;
6078 }
6079 if (state->c == NFA_RANGE_MIN)
6080 {
6081 c1 = state->val;
6082 state = state->out; /* advance to NFA_RANGE_MAX */
6083 c2 = state->val;
6084#ifdef ENABLE_LOG
6085 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6086 curc, c1, c2);
6087#endif
6088 if (curc >= c1 && curc <= c2)
6089 {
6090 result = result_if_matched;
6091 break;
6092 }
6093 if (ireg_ic)
6094 {
6095 int curc_low = MB_TOLOWER(curc);
6096 int done = FALSE;
6097
6098 for ( ; c1 <= c2; ++c1)
6099 if (MB_TOLOWER(c1) == curc_low)
6100 {
6101 result = result_if_matched;
6102 done = TRUE;
6103 break;
6104 }
6105 if (done)
6106 break;
6107 }
6108 }
6109 else if (state->c < 0 ? check_char_class(state->c, curc)
6110 : (curc == state->c
6111 || (ireg_ic && MB_TOLOWER(curc)
6112 == MB_TOLOWER(state->c))))
6113 {
6114 result = result_if_matched;
6115 break;
6116 }
6117 state = state->out;
6118 }
6119 if (result)
6120 {
6121 /* next state is in out of the NFA_END_COLL, out1 of
6122 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006123 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006124 add_off = clen;
6125 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006126 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006127 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006128
6129 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006130 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006131 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006132 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006133 add_state = t->state->out;
6134 add_off = clen;
6135 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006136 break;
6137
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006138 case NFA_ANY_COMPOSING:
6139 /* On a composing character skip over it. Otherwise do
6140 * nothing. Always matches. */
6141#ifdef FEAT_MBYTE
6142 if (enc_utf8 && utf_iscomposing(curc))
6143 {
6144 add_off = clen;
6145 }
6146 else
6147#endif
6148 {
6149 add_here = TRUE;
6150 add_off = 0;
6151 }
6152 add_state = t->state->out;
6153 break;
6154
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006155 /*
6156 * Character classes like \a for alpha, \d for digit etc.
6157 */
6158 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006159 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006160 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006161 break;
6162
6163 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006164 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006165 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006166 break;
6167
6168 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006169 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006170 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006171 break;
6172
6173 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006174 result = !VIM_ISDIGIT(curc)
6175 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006176 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006177 break;
6178
6179 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006180 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006181 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006182 break;
6183
6184 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006185 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006186 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006187 break;
6188
6189 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006190 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006191 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006192 break;
6193
6194 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006195 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006196 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006197 break;
6198
6199 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006200 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006201 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006202 break;
6203
6204 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006205 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006206 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006207 break;
6208
6209 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006210 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006211 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006212 break;
6213
6214 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006215 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006216 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006217 break;
6218
6219 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006220 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006221 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006222 break;
6223
6224 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006225 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006226 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006227 break;
6228
6229 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006230 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006231 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006232 break;
6233
6234 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006235 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006236 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006237 break;
6238
6239 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006240 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006241 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006242 break;
6243
6244 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006245 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006246 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006247 break;
6248
6249 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006250 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006251 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006252 break;
6253
6254 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006255 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006256 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006257 break;
6258
6259 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006260 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006261 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006262 break;
6263
6264 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006265 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006266 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006267 break;
6268
6269 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006270 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006271 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006272 break;
6273
6274 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006275 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006276 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006277 break;
6278
6279 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006280 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006281 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006282 break;
6283
6284 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006285 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006286 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006287 break;
6288
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006289 case NFA_LOWER_IC: /* [a-z] */
6290 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6291 ADD_STATE_IF_MATCH(t->state);
6292 break;
6293
6294 case NFA_NLOWER_IC: /* [^a-z] */
6295 result = curc != NUL
6296 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6297 ADD_STATE_IF_MATCH(t->state);
6298 break;
6299
6300 case NFA_UPPER_IC: /* [A-Z] */
6301 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6302 ADD_STATE_IF_MATCH(t->state);
6303 break;
6304
6305 case NFA_NUPPER_IC: /* ^[A-Z] */
6306 result = curc != NUL
6307 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6308 ADD_STATE_IF_MATCH(t->state);
6309 break;
6310
Bram Moolenaar5714b802013-05-28 22:03:20 +02006311 case NFA_BACKREF1:
6312 case NFA_BACKREF2:
6313 case NFA_BACKREF3:
6314 case NFA_BACKREF4:
6315 case NFA_BACKREF5:
6316 case NFA_BACKREF6:
6317 case NFA_BACKREF7:
6318 case NFA_BACKREF8:
6319 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006320#ifdef FEAT_SYN_HL
6321 case NFA_ZREF1:
6322 case NFA_ZREF2:
6323 case NFA_ZREF3:
6324 case NFA_ZREF4:
6325 case NFA_ZREF5:
6326 case NFA_ZREF6:
6327 case NFA_ZREF7:
6328 case NFA_ZREF8:
6329 case NFA_ZREF9:
6330#endif
6331 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006332 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006333 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006334 int bytelen;
6335
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006336 if (t->state->c <= NFA_BACKREF9)
6337 {
6338 subidx = t->state->c - NFA_BACKREF1 + 1;
6339 result = match_backref(&t->subs.norm, subidx, &bytelen);
6340 }
6341#ifdef FEAT_SYN_HL
6342 else
6343 {
6344 subidx = t->state->c - NFA_ZREF1 + 1;
6345 result = match_zref(subidx, &bytelen);
6346 }
6347#endif
6348
Bram Moolenaar5714b802013-05-28 22:03:20 +02006349 if (result)
6350 {
6351 if (bytelen == 0)
6352 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006353 /* empty match always works, output of NFA_SKIP to be
6354 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006355 add_here = TRUE;
6356 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006357 }
6358 else if (bytelen <= clen)
6359 {
6360 /* match current character, jump ahead to out of
6361 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006362 add_state = t->state->out->out;
6363 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006364 }
6365 else
6366 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006367 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006368 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006369 add_state = t->state->out;
6370 add_off = bytelen;
6371 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006372 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006373 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006374 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006375 }
6376 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006377 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006378 if (t->count - clen <= 0)
6379 {
6380 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006381 add_state = t->state->out;
6382 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006383 }
6384 else
6385 {
6386 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006387 add_state = t->state;
6388 add_off = 0;
6389 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006390 }
6391 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006392
Bram Moolenaar423532e2013-05-29 21:14:42 +02006393 case NFA_LNUM:
6394 case NFA_LNUM_GT:
6395 case NFA_LNUM_LT:
6396 result = (REG_MULTI &&
6397 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6398 (long_u)(reglnum + reg_firstlnum)));
6399 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006400 {
6401 add_here = TRUE;
6402 add_state = t->state->out;
6403 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006404 break;
6405
6406 case NFA_COL:
6407 case NFA_COL_GT:
6408 case NFA_COL_LT:
6409 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6410 (long_u)(reginput - regline) + 1);
6411 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006412 {
6413 add_here = TRUE;
6414 add_state = t->state->out;
6415 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006416 break;
6417
6418 case NFA_VCOL:
6419 case NFA_VCOL_GT:
6420 case NFA_VCOL_LT:
6421 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
6422 (long_u)win_linetabsize(
6423 reg_win == NULL ? curwin : reg_win,
6424 regline, (colnr_T)(reginput - regline)) + 1);
6425 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006426 {
6427 add_here = TRUE;
6428 add_state = t->state->out;
6429 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006430 break;
6431
Bram Moolenaar044aa292013-06-04 21:27:38 +02006432 case NFA_MARK:
6433 case NFA_MARK_GT:
6434 case NFA_MARK_LT:
6435 {
6436 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6437
6438 /* Compare the mark position to the match position. */
6439 result = (pos != NULL /* mark doesn't exist */
6440 && pos->lnum > 0 /* mark isn't set in reg_buf */
6441 && (pos->lnum == reglnum + reg_firstlnum
6442 ? (pos->col == (colnr_T)(reginput - regline)
6443 ? t->state->c == NFA_MARK
6444 : (pos->col < (colnr_T)(reginput - regline)
6445 ? t->state->c == NFA_MARK_GT
6446 : t->state->c == NFA_MARK_LT))
6447 : (pos->lnum < reglnum + reg_firstlnum
6448 ? t->state->c == NFA_MARK_GT
6449 : t->state->c == NFA_MARK_LT)));
6450 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006451 {
6452 add_here = TRUE;
6453 add_state = t->state->out;
6454 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006455 break;
6456 }
6457
Bram Moolenaar423532e2013-05-29 21:14:42 +02006458 case NFA_CURSOR:
6459 result = (reg_win != NULL
6460 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6461 && ((colnr_T)(reginput - regline)
6462 == reg_win->w_cursor.col));
6463 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006464 {
6465 add_here = TRUE;
6466 add_state = t->state->out;
6467 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006468 break;
6469
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006470 case NFA_VISUAL:
6471 result = reg_match_visual();
6472 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006473 {
6474 add_here = TRUE;
6475 add_state = t->state->out;
6476 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006477 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006478
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006479 case NFA_MOPEN1:
6480 case NFA_MOPEN2:
6481 case NFA_MOPEN3:
6482 case NFA_MOPEN4:
6483 case NFA_MOPEN5:
6484 case NFA_MOPEN6:
6485 case NFA_MOPEN7:
6486 case NFA_MOPEN8:
6487 case NFA_MOPEN9:
6488#ifdef FEAT_SYN_HL
6489 case NFA_ZOPEN:
6490 case NFA_ZOPEN1:
6491 case NFA_ZOPEN2:
6492 case NFA_ZOPEN3:
6493 case NFA_ZOPEN4:
6494 case NFA_ZOPEN5:
6495 case NFA_ZOPEN6:
6496 case NFA_ZOPEN7:
6497 case NFA_ZOPEN8:
6498 case NFA_ZOPEN9:
6499#endif
6500 case NFA_NOPEN:
6501 case NFA_ZSTART:
6502 /* These states are only added to be able to bail out when
6503 * they are added again, nothing is to be done. */
6504 break;
6505
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006506 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006507 {
6508 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006509
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006510#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006511 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006512 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006513#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006514 result = (c == curc);
6515
6516 if (!result && ireg_ic)
6517 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006518#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006519 /* If ireg_icombine is not set only skip over the character
6520 * itself. When it is set skip over composing characters. */
6521 if (result && enc_utf8 && !ireg_icombine)
6522 clen = utf_char2len(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006523#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006524 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006525 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006526 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006527
6528 } /* switch (t->state->c) */
6529
6530 if (add_state != NULL)
6531 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006532 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006533 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006534
6535 if (t->pim.result == NFA_PIM_UNUSED)
6536 pim = NULL;
6537 else
6538 pim = &t->pim;
6539
6540 /* Handle the postponed invisible match if the match might end
6541 * without advancing and before the end of the line. */
6542 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006543 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006544 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006545 {
6546#ifdef ENABLE_LOG
6547 fprintf(log_fd, "\n");
6548 fprintf(log_fd, "==================================\n");
6549 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6550 fprintf(log_fd, "\n");
6551#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006552 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006553 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006554 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006555 /* for \@! and \@<! it is a match when the result is
6556 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006557 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006558 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6559 || pim->state->c
6560 == NFA_START_INVISIBLE_BEFORE_NEG
6561 || pim->state->c
6562 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006563 {
6564 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006565 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006566#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006567 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006568 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006569#endif
6570 }
6571 }
6572 else
6573 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006574 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006575#ifdef ENABLE_LOG
6576 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006577 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006578 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6579 fprintf(log_fd, "\n");
6580#endif
6581 }
6582
Bram Moolenaardecd9542013-06-07 16:31:50 +02006583 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006584 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006585 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6586 || pim->state->c
6587 == NFA_START_INVISIBLE_BEFORE_NEG
6588 || pim->state->c
6589 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006590 {
6591 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006592 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006593#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006594 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006595 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006596#endif
6597 }
6598 else
6599 /* look-behind match failed, don't add the state */
6600 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006601
6602 /* Postponed invisible match was handled, don't add it to
6603 * following states. */
6604 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006605 }
6606
Bram Moolenaara951e352013-10-06 15:46:11 +02006607 /* If "pim" points into l->t it will become invalid when
6608 * adding the state causes the list to be reallocated. Make a
6609 * local copy to avoid that. */
6610 if (pim == &t->pim)
6611 {
6612 copy_pim(&pim_copy, pim);
6613 pim = &pim_copy;
6614 }
6615
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006616 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006617 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006618 else
6619 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006620 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006621 if (add_count > 0)
6622 nextlist->t[nextlist->n - 1].count = add_count;
6623 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006624 }
6625
6626 } /* for (thislist = thislist; thislist->state; thislist++) */
6627
Bram Moolenaare23febd2013-05-26 18:40:14 +02006628 /* Look for the start of a match in the current position by adding the
6629 * start state to the list of states.
6630 * The first found match is the leftmost one, thus the order of states
6631 * matters!
6632 * Do not add the start state in recursive calls of nfa_regmatch(),
6633 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006634 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006635 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006636 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006637 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006638 && reglnum == 0
6639 && clen != 0
6640 && (ireg_maxcol == 0
6641 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006642 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006643 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006644 ? (reglnum < nfa_endp->se_u.pos.lnum
6645 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006646 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006647 < nfa_endp->se_u.pos.col))
6648 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006649 {
6650#ifdef ENABLE_LOG
6651 fprintf(log_fd, "(---) STARTSTATE\n");
6652#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006653 /* Inline optimized code for addstate() if we know the state is
6654 * the first MOPEN. */
6655 if (toplevel)
6656 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006657 int add = TRUE;
6658 int c;
6659
6660 if (prog->regstart != NUL && clen != 0)
6661 {
6662 if (nextlist->n == 0)
6663 {
6664 colnr_T col = (colnr_T)(reginput - regline) + clen;
6665
6666 /* Nextlist is empty, we can skip ahead to the
6667 * character that must appear at the start. */
6668 if (skip_to_start(prog->regstart, &col) == FAIL)
6669 break;
6670#ifdef ENABLE_LOG
6671 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6672 col - ((colnr_T)(reginput - regline) + clen));
6673#endif
6674 reginput = regline + col - clen;
6675 }
6676 else
6677 {
6678 /* Checking if the required start character matches is
6679 * cheaper than adding a state that won't match. */
6680 c = PTR2CHAR(reginput + clen);
6681 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6682 != MB_TOLOWER(prog->regstart)))
6683 {
6684#ifdef ENABLE_LOG
6685 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6686#endif
6687 add = FALSE;
6688 }
6689 }
6690 }
6691
6692 if (add)
6693 {
6694 if (REG_MULTI)
6695 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006696 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006697 else
6698 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006699 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006700 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006701 }
6702 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006703 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006704 }
6705
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006706#ifdef ENABLE_LOG
6707 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006708 {
6709 int i;
6710
6711 for (i = 0; i < thislist->n; i++)
6712 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6713 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006714 fprintf(log_fd, "\n");
6715#endif
6716
6717nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006718 /* Advance to the next character, or advance to the next line, or
6719 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006720 if (clen != 0)
6721 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006722 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6723 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006724 reg_nextline();
6725 else
6726 break;
6727 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006728
6729#ifdef ENABLE_LOG
6730 if (log_fd != stderr)
6731 fclose(log_fd);
6732 log_fd = NULL;
6733#endif
6734
6735theend:
6736 /* Free memory */
6737 vim_free(list[0].t);
6738 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006739 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006740#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006741#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006742 fclose(debug);
6743#endif
6744
Bram Moolenaar963fee22013-05-26 21:47:28 +02006745 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006746}
6747
6748/*
6749 * Try match of "prog" with at regline["col"].
6750 * Returns 0 for failure, number of lines contained in the match otherwise.
6751 */
6752 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006753nfa_regtry(prog, col)
6754 nfa_regprog_T *prog;
6755 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006756{
6757 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006758 regsubs_T subs, m;
6759 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006760#ifdef ENABLE_LOG
6761 FILE *f;
6762#endif
6763
6764 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006765
6766#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006767 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006768 if (f != NULL)
6769 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006770 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006771#ifdef DEBUG
6772 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6773#endif
6774 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006775 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006776 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006777 fprintf(f, "\n\n");
6778 fclose(f);
6779 }
6780 else
6781 EMSG(_("Could not open temporary log file for writing "));
6782#endif
6783
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006784 clear_sub(&subs.norm);
6785 clear_sub(&m.norm);
6786#ifdef FEAT_SYN_HL
6787 clear_sub(&subs.synt);
6788 clear_sub(&m.synt);
6789#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006790
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006791 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006792 return 0;
6793
6794 cleanup_subexpr();
6795 if (REG_MULTI)
6796 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006797 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006798 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006799 reg_startpos[i] = subs.norm.list.multi[i].start;
6800 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006801 }
6802
6803 if (reg_startpos[0].lnum < 0)
6804 {
6805 reg_startpos[0].lnum = 0;
6806 reg_startpos[0].col = col;
6807 }
6808 if (reg_endpos[0].lnum < 0)
6809 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006810 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006811 reg_endpos[0].lnum = reglnum;
6812 reg_endpos[0].col = (int)(reginput - regline);
6813 }
6814 else
6815 /* Use line number of "\ze". */
6816 reglnum = reg_endpos[0].lnum;
6817 }
6818 else
6819 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006820 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006821 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006822 reg_startp[i] = subs.norm.list.line[i].start;
6823 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006824 }
6825
6826 if (reg_startp[0] == NULL)
6827 reg_startp[0] = regline + col;
6828 if (reg_endp[0] == NULL)
6829 reg_endp[0] = reginput;
6830 }
6831
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006832#ifdef FEAT_SYN_HL
6833 /* Package any found \z(...\) matches for export. Default is none. */
6834 unref_extmatch(re_extmatch_out);
6835 re_extmatch_out = NULL;
6836
6837 if (prog->reghasz == REX_SET)
6838 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006839 cleanup_zsubexpr();
6840 re_extmatch_out = make_extmatch();
6841 for (i = 0; i < subs.synt.in_use; i++)
6842 {
6843 if (REG_MULTI)
6844 {
6845 struct multipos *mpos = &subs.synt.list.multi[i];
6846
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006847 /* Only accept single line matches that are valid. */
6848 if (mpos->start.lnum >= 0
6849 && mpos->start.lnum == mpos->end.lnum
6850 && mpos->end.col >= mpos->start.col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006851 re_extmatch_out->matches[i] =
6852 vim_strnsave(reg_getline(mpos->start.lnum)
6853 + mpos->start.col,
6854 mpos->end.col - mpos->start.col);
6855 }
6856 else
6857 {
6858 struct linepos *lpos = &subs.synt.list.line[i];
6859
6860 if (lpos->start != NULL && lpos->end != NULL)
6861 re_extmatch_out->matches[i] =
6862 vim_strnsave(lpos->start,
6863 (int)(lpos->end - lpos->start));
6864 }
6865 }
6866 }
6867#endif
6868
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006869 return 1 + reglnum;
6870}
6871
6872/*
6873 * Match a regexp against a string ("line" points to the string) or multiple
6874 * lines ("line" is NULL, use reg_getline()).
6875 *
6876 * Returns 0 for failure, number of lines contained in the match otherwise.
6877 */
6878 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006879nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006880 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006881 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006882{
6883 nfa_regprog_T *prog;
6884 long retval = 0L;
6885 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006886 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006887
6888 if (REG_MULTI)
6889 {
6890 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6891 line = reg_getline((linenr_T)0); /* relative to the cursor */
6892 reg_startpos = reg_mmatch->startpos;
6893 reg_endpos = reg_mmatch->endpos;
6894 }
6895 else
6896 {
6897 prog = (nfa_regprog_T *)reg_match->regprog;
6898 reg_startp = reg_match->startp;
6899 reg_endp = reg_match->endp;
6900 }
6901
6902 /* Be paranoid... */
6903 if (prog == NULL || line == NULL)
6904 {
6905 EMSG(_(e_null));
6906 goto theend;
6907 }
6908
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006909 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6910 if (prog->regflags & RF_ICASE)
6911 ireg_ic = TRUE;
6912 else if (prog->regflags & RF_NOICASE)
6913 ireg_ic = FALSE;
6914
6915#ifdef FEAT_MBYTE
6916 /* If pattern contains "\Z" overrule value of ireg_icombine */
6917 if (prog->regflags & RF_ICOMBINE)
6918 ireg_icombine = TRUE;
6919#endif
6920
6921 regline = line;
6922 reglnum = 0; /* relative to line */
6923
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006924 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006925 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006926 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006927 nfa_listid = 1;
6928 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006929#ifdef DEBUG
6930 nfa_regengine.expr = prog->pattern;
6931#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006932
Bram Moolenaard89616e2013-06-06 18:46:06 +02006933 if (prog->reganch && col > 0)
6934 return 0L;
6935
Bram Moolenaar473de612013-06-08 18:19:48 +02006936 need_clear_subexpr = TRUE;
6937#ifdef FEAT_SYN_HL
6938 /* Clear the external match subpointers if necessary. */
6939 if (prog->reghasz == REX_SET)
6940 {
6941 nfa_has_zsubexpr = TRUE;
6942 need_clear_zsubexpr = TRUE;
6943 }
6944 else
6945 nfa_has_zsubexpr = FALSE;
6946#endif
6947
Bram Moolenaard89616e2013-06-06 18:46:06 +02006948 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006949 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006950 /* Skip ahead until a character we know the match must start with.
6951 * When there is none there is no match. */
6952 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006953 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006954
Bram Moolenaar473de612013-06-08 18:19:48 +02006955 /* If match_text is set it contains the full text that must match.
6956 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006957 if (prog->match_text != NULL
6958#ifdef FEAT_MBYTE
6959 && !ireg_icombine
6960#endif
6961 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006962 return find_match_text(col, prog->regstart, prog->match_text);
6963 }
6964
Bram Moolenaard89616e2013-06-06 18:46:06 +02006965 /* If the start column is past the maximum column: no need to try. */
6966 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6967 goto theend;
6968
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006969 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006970 for (i = 0; i < nstate; ++i)
6971 {
6972 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006973 prog->state[i].lastlist[0] = 0;
6974 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006975 }
6976
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006977 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006978
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006979#ifdef DEBUG
6980 nfa_regengine.expr = NULL;
6981#endif
6982
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006983theend:
6984 return retval;
6985}
6986
6987/*
6988 * Compile a regular expression into internal code for the NFA matcher.
6989 * Returns the program in allocated space. Returns NULL for an error.
6990 */
6991 static regprog_T *
6992nfa_regcomp(expr, re_flags)
6993 char_u *expr;
6994 int re_flags;
6995{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006996 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006997 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006998 int *postfix;
6999
7000 if (expr == NULL)
7001 return NULL;
7002
7003#ifdef DEBUG
7004 nfa_regengine.expr = expr;
7005#endif
7006
7007 init_class_tab();
7008
7009 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7010 return NULL;
7011
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007012 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007013 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007014 postfix = re2post();
7015 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007016 {
7017 /* TODO: only give this error for debugging? */
7018 if (post_ptr >= post_end)
7019 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007020 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007021 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007022
7023 /*
7024 * In order to build the NFA, we parse the input regexp twice:
7025 * 1. first pass to count size (so we can allocate space)
7026 * 2. second to emit code
7027 */
7028#ifdef ENABLE_LOG
7029 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007030 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007031
7032 if (f != NULL)
7033 {
7034 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7035 fclose(f);
7036 }
7037 }
7038#endif
7039
7040 /*
7041 * PASS 1
7042 * Count number of NFA states in "nstate". Do not build the NFA.
7043 */
7044 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007045
Bram Moolenaar16619a22013-06-11 18:42:36 +02007046 /* allocate the regprog with space for the compiled regexp */
7047 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007048 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7049 if (prog == NULL)
7050 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007051 state_ptr = prog->state;
7052
7053 /*
7054 * PASS 2
7055 * Build the NFA
7056 */
7057 prog->start = post2nfa(postfix, post_ptr, FALSE);
7058 if (prog->start == NULL)
7059 goto fail;
7060
7061 prog->regflags = regflags;
7062 prog->engine = &nfa_regengine;
7063 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007064 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007065 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007066 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007067
Bram Moolenaara2947e22013-06-11 22:44:09 +02007068 nfa_postprocess(prog);
7069
Bram Moolenaard89616e2013-06-06 18:46:06 +02007070 prog->reganch = nfa_get_reganch(prog->start, 0);
7071 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007072 prog->match_text = nfa_get_match_text(prog->start);
7073
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007074#ifdef ENABLE_LOG
7075 nfa_postfix_dump(expr, OK);
7076 nfa_dump(prog);
7077#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007078#ifdef FEAT_SYN_HL
7079 /* Remember whether this pattern has any \z specials in it. */
7080 prog->reghasz = re_has_z;
7081#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007082#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02007083 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007084 nfa_regengine.expr = NULL;
7085#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007086
7087out:
7088 vim_free(post_start);
7089 post_start = post_ptr = post_end = NULL;
7090 state_ptr = NULL;
7091 return (regprog_T *)prog;
7092
7093fail:
7094 vim_free(prog);
7095 prog = NULL;
7096#ifdef ENABLE_LOG
7097 nfa_postfix_dump(expr, FAIL);
7098#endif
7099#ifdef DEBUG
7100 nfa_regengine.expr = NULL;
7101#endif
7102 goto out;
7103}
7104
Bram Moolenaar473de612013-06-08 18:19:48 +02007105/*
7106 * Free a compiled regexp program, returned by nfa_regcomp().
7107 */
7108 static void
7109nfa_regfree(prog)
7110 regprog_T *prog;
7111{
7112 if (prog != NULL)
7113 {
7114 vim_free(((nfa_regprog_T *)prog)->match_text);
7115#ifdef DEBUG
7116 vim_free(((nfa_regprog_T *)prog)->pattern);
7117#endif
7118 vim_free(prog);
7119 }
7120}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007121
7122/*
7123 * Match a regexp against a string.
7124 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7125 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007126 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007127 *
7128 * Return TRUE if there is a match, FALSE if not.
7129 */
7130 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007131nfa_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007132 regmatch_T *rmp;
7133 char_u *line; /* string to match against */
7134 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007135 int line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007136{
7137 reg_match = rmp;
7138 reg_mmatch = NULL;
7139 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007140 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007141 reg_buf = curbuf;
7142 reg_win = NULL;
7143 ireg_ic = rmp->rm_ic;
7144#ifdef FEAT_MBYTE
7145 ireg_icombine = FALSE;
7146#endif
7147 ireg_maxcol = 0;
7148 return (nfa_regexec_both(line, col) != 0);
7149}
7150
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007151
7152/*
7153 * Match a regexp against multiple lines.
7154 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7155 * Uses curbuf for line count and 'iskeyword'.
7156 *
7157 * Return zero if there is no match. Return number of lines contained in the
7158 * match otherwise.
7159 *
7160 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7161 *
7162 * ! Also NOTE : match may actually be in another line. e.g.:
7163 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7164 *
7165 * +-------------------------+
7166 * |a |
7167 * |b |
7168 * |c |
7169 * | |
7170 * +-------------------------+
7171 *
7172 * then nfa_regexec_multi() returns 3. while the original
7173 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7174 *
7175 * FIXME if this behavior is not compatible.
7176 */
7177 static long
7178nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7179 regmmatch_T *rmp;
7180 win_T *win; /* window in which to search or NULL */
7181 buf_T *buf; /* buffer in which to search */
7182 linenr_T lnum; /* nr of line to start looking for match */
7183 colnr_T col; /* column to start looking for match */
7184 proftime_T *tm UNUSED; /* timeout limit or NULL */
7185{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007186 reg_match = NULL;
7187 reg_mmatch = rmp;
7188 reg_buf = buf;
7189 reg_win = win;
7190 reg_firstlnum = lnum;
7191 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7192 reg_line_lbr = FALSE;
7193 ireg_ic = rmp->rmm_ic;
7194#ifdef FEAT_MBYTE
7195 ireg_icombine = FALSE;
7196#endif
7197 ireg_maxcol = rmp->rmm_maxcol;
7198
Bram Moolenaarf878bf02013-05-21 21:20:20 +02007199 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007200}
7201
7202#ifdef DEBUG
7203# undef ENABLE_LOG
7204#endif