blob: 8e069f35198fd8e95b19cb24b9fdada1c565955b [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);
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001326 if (re_mult_next("\\zs") == FAIL)
1327 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001328 break;
1329 case 'e':
1330 EMIT(NFA_ZEND);
1331 nfa_has_zend = TRUE;
Bram Moolenaar2d46e602014-08-29 11:56:32 +02001332 if (re_mult_next("\\ze") == FAIL)
1333 return FAIL;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001334 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001335#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001336 case '1':
1337 case '2':
1338 case '3':
1339 case '4':
1340 case '5':
1341 case '6':
1342 case '7':
1343 case '8':
1344 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001345 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001346 if (reg_do_extmatch != REX_USE)
1347 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001348 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1349 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001350 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001351 re_has_z = REX_USE;
1352 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001353 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001354 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001355 if (reg_do_extmatch != REX_SET)
1356 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001357 if (nfa_reg(REG_ZPAREN) == FAIL)
1358 return FAIL; /* cascaded error */
1359 re_has_z = REX_SET;
1360 break;
1361#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001362 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001363 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001364 no_Magic(c));
1365 return FAIL;
1366 }
1367 break;
1368
1369 case Magic('%'):
1370 c = no_Magic(getchr());
1371 switch (c)
1372 {
1373 /* () without a back reference */
1374 case '(':
1375 if (nfa_reg(REG_NPAREN) == FAIL)
1376 return FAIL;
1377 EMIT(NFA_NOPEN);
1378 break;
1379
1380 case 'd': /* %d123 decimal */
1381 case 'o': /* %o123 octal */
1382 case 'x': /* %xab hex 2 */
1383 case 'u': /* %uabcd hex 4 */
1384 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001385 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001386 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001387
Bram Moolenaar47196582013-05-25 22:04:23 +02001388 switch (c)
1389 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001390 case 'd': nr = getdecchrs(); break;
1391 case 'o': nr = getoctchrs(); break;
1392 case 'x': nr = gethexchrs(2); break;
1393 case 'u': nr = gethexchrs(4); break;
1394 case 'U': nr = gethexchrs(8); break;
1395 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001396 }
1397
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001398 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001399 EMSG2_RET_FAIL(
1400 _("E678: Invalid character after %s%%[dxouU]"),
1401 reg_magic == MAGIC_ALL);
Bram Moolenaar595cad22013-09-22 13:57:24 +02001402 /* A NUL is stored in the text as NL */
Bram Moolenaar47196582013-05-25 22:04:23 +02001403 /* TODO: what if a composing character follows? */
Bram Moolenaar595cad22013-09-22 13:57:24 +02001404 EMIT(nr == 0 ? 0x0a : nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001405 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001406 break;
1407
1408 /* Catch \%^ and \%$ regardless of where they appear in the
1409 * pattern -- regardless of whether or not it makes sense. */
1410 case '^':
1411 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001412 break;
1413
1414 case '$':
1415 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 break;
1417
1418 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001419 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001420 break;
1421
1422 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001423 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001424 break;
1425
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02001426 case 'C':
1427 EMIT(NFA_ANY_COMPOSING);
1428 break;
1429
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001430 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001431 {
1432 int n;
1433
1434 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001435 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001436 {
1437 if (c == NUL)
1438 EMSG2_RET_FAIL(_(e_missing_sb),
1439 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001440 /* recursive call! */
1441 if (nfa_regatom() == FAIL)
1442 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001443 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001444 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001445 if (n == 0)
1446 EMSG2_RET_FAIL(_(e_empty_sb),
1447 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001448 EMIT(NFA_OPT_CHARS);
1449 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001450
1451 /* Emit as "\%(\%[abc]\)" to be able to handle
1452 * "\%[abc]*" which would cause the empty string to be
1453 * matched an unlimited number of times. NFA_NOPEN is
1454 * added only once at a position, while NFA_SPLIT is
1455 * added multiple times. This is more efficient than
1456 * not allowsing NFA_SPLIT multiple times, it is used
1457 * a lot. */
1458 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001459 break;
1460 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001461
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001462 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001463 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001464 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001465 int cmp = c;
1466
1467 if (c == '<' || c == '>')
1468 c = getchr();
1469 while (VIM_ISDIGIT(c))
1470 {
1471 n = n * 10 + (c - '0');
1472 c = getchr();
1473 }
1474 if (c == 'l' || c == 'c' || c == 'v')
1475 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001476 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001477 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001478 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001479 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001480 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001481 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001482 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001483 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001484 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001485 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001486 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001487 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001488 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001489 break;
1490 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001491 else if (c == '\'' && n == 0)
1492 {
1493 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001494 EMIT(cmp == '<' ? NFA_MARK_LT :
1495 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001496 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001497 break;
1498 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001499 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001500 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1501 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001502 return FAIL;
1503 }
1504 break;
1505
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001506 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001507collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001509 * [abc] uses NFA_START_COLL - NFA_END_COLL
1510 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1511 * Each character is produced as a regular state, using
1512 * NFA_CONCAT to bind them together.
1513 * Besides normal characters there can be:
1514 * - character classes NFA_CLASS_*
1515 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001516 */
1517
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 p = regparse;
1519 endp = skip_anyof(p);
1520 if (*endp == ']')
1521 {
1522 /*
1523 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001524 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001525 * and perform the necessary substitutions in the NFA.
1526 */
1527 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001528 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001529 if (result != FAIL)
1530 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001531 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001533 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001534 EMIT(NFA_NEWL);
1535 EMIT(NFA_OR);
1536 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001537 else
1538 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001539 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001540 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001541 return OK;
1542 }
1543 /*
1544 * Failed to recognize a character class. Use the simple
1545 * version that turns [abc] into 'a' OR 'b' OR 'c'
1546 */
1547 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001548 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001549 if (*regparse == '^') /* negated range */
1550 {
1551 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001552 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001553 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001554 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001555 else
1556 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001557 if (*regparse == '-')
1558 {
1559 startc = '-';
1560 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001561 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001562 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001563 }
1564 /* Emit the OR branches for each character in the [] */
1565 emit_range = FALSE;
1566 while (regparse < endp)
1567 {
1568 oldstartc = startc;
1569 startc = -1;
1570 got_coll_char = FALSE;
1571 if (*regparse == '[')
1572 {
1573 /* Check for [: :], [= =], [. .] */
1574 equiclass = collclass = 0;
1575 charclass = get_char_class(&regparse);
1576 if (charclass == CLASS_NONE)
1577 {
1578 equiclass = get_equi_class(&regparse);
1579 if (equiclass == 0)
1580 collclass = get_coll_element(&regparse);
1581 }
1582
1583 /* Character class like [:alpha:] */
1584 if (charclass != CLASS_NONE)
1585 {
1586 switch (charclass)
1587 {
1588 case CLASS_ALNUM:
1589 EMIT(NFA_CLASS_ALNUM);
1590 break;
1591 case CLASS_ALPHA:
1592 EMIT(NFA_CLASS_ALPHA);
1593 break;
1594 case CLASS_BLANK:
1595 EMIT(NFA_CLASS_BLANK);
1596 break;
1597 case CLASS_CNTRL:
1598 EMIT(NFA_CLASS_CNTRL);
1599 break;
1600 case CLASS_DIGIT:
1601 EMIT(NFA_CLASS_DIGIT);
1602 break;
1603 case CLASS_GRAPH:
1604 EMIT(NFA_CLASS_GRAPH);
1605 break;
1606 case CLASS_LOWER:
1607 EMIT(NFA_CLASS_LOWER);
1608 break;
1609 case CLASS_PRINT:
1610 EMIT(NFA_CLASS_PRINT);
1611 break;
1612 case CLASS_PUNCT:
1613 EMIT(NFA_CLASS_PUNCT);
1614 break;
1615 case CLASS_SPACE:
1616 EMIT(NFA_CLASS_SPACE);
1617 break;
1618 case CLASS_UPPER:
1619 EMIT(NFA_CLASS_UPPER);
1620 break;
1621 case CLASS_XDIGIT:
1622 EMIT(NFA_CLASS_XDIGIT);
1623 break;
1624 case CLASS_TAB:
1625 EMIT(NFA_CLASS_TAB);
1626 break;
1627 case CLASS_RETURN:
1628 EMIT(NFA_CLASS_RETURN);
1629 break;
1630 case CLASS_BACKSPACE:
1631 EMIT(NFA_CLASS_BACKSPACE);
1632 break;
1633 case CLASS_ESCAPE:
1634 EMIT(NFA_CLASS_ESCAPE);
1635 break;
1636 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001637 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001638 continue;
1639 }
1640 /* Try equivalence class [=a=] and the like */
1641 if (equiclass != 0)
1642 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001643 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001644 if (result == FAIL)
1645 {
1646 /* should never happen */
1647 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1648 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001649 continue;
1650 }
1651 /* Try collating class like [. .] */
1652 if (collclass != 0)
1653 {
1654 startc = collclass; /* allow [.a.]-x as a range */
1655 /* Will emit the proper atom at the end of the
1656 * while loop. */
1657 }
1658 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001659 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1660 * start character. */
1661 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001662 {
1663 emit_range = TRUE;
1664 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001665 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 continue; /* reading the end of the range */
1667 }
1668
1669 /* Now handle simple and escaped characters.
1670 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1671 * accepts "\t", "\e", etc., but only when the 'l' flag in
1672 * 'cpoptions' is not included.
1673 * Posix doesn't recognize backslash at all.
1674 */
1675 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001676 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 && regparse + 1 <= endp
1678 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001679 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001680 && vim_strchr(REGEXP_ABBR, regparse[1])
1681 != NULL)
1682 )
1683 )
1684 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001685 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001686
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001687 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001688 startc = reg_string ? NL : NFA_NEWL;
1689 else
1690 if (*regparse == 'd'
1691 || *regparse == 'o'
1692 || *regparse == 'x'
1693 || *regparse == 'u'
1694 || *regparse == 'U'
1695 )
1696 {
1697 /* TODO(RE) This needs more testing */
1698 startc = coll_get_char();
1699 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001700 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001701 }
1702 else
1703 {
1704 /* \r,\t,\e,\b */
1705 startc = backslash_trans(*regparse);
1706 }
1707 }
1708
1709 /* Normal printable char */
1710 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001711 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001712
1713 /* Previous char was '-', so this char is end of range. */
1714 if (emit_range)
1715 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001716 endc = startc;
1717 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001718 if (startc > endc)
1719 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001720
1721 if (endc > startc + 2)
1722 {
1723 /* Emit a range instead of the sequence of
1724 * individual characters. */
1725 if (startc == 0)
1726 /* \x00 is translated to \x0a, start at \x01. */
1727 EMIT(1);
1728 else
1729 --post_ptr; /* remove NFA_CONCAT */
1730 EMIT(endc);
1731 EMIT(NFA_RANGE);
1732 EMIT(NFA_CONCAT);
1733 }
1734 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001735#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001736 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001737 || (*mb_char2len)(endc) > 1))
1738 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001739 /* Emit the characters in the range.
1740 * "startc" was already emitted, so skip it.
1741 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001742 for (c = startc + 1; c <= endc; c++)
1743 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001744 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001745 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001746 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001747 }
1748 else
1749#endif
1750 {
1751#ifdef EBCDIC
1752 int alpha_only = FALSE;
1753
1754 /* for alphabetical range skip the gaps
1755 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1756 if (isalpha(startc) && isalpha(endc))
1757 alpha_only = TRUE;
1758#endif
1759 /* Emit the range. "startc" was already emitted, so
1760 * skip it. */
1761 for (c = startc + 1; c <= endc; c++)
1762#ifdef EBCDIC
1763 if (!alpha_only || isalpha(startc))
1764#endif
1765 {
1766 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001767 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001768 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001769 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001770 emit_range = FALSE;
1771 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001772 }
1773 else
1774 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001775 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001776 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001777 * Normally, simply emit startc. But if we get char
1778 * code=0 from a collating char, then replace it with
1779 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001780 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001781 * the backtracking engine. */
1782 if (startc == NFA_NEWL)
1783 {
1784 /* Line break can't be matched as part of the
1785 * collection, add an OR below. But not for negated
1786 * range. */
1787 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001788 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001789 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001790 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001791 {
1792 if (got_coll_char == TRUE && startc == 0)
1793 EMIT(0x0a);
1794 else
1795 EMIT(startc);
1796 EMIT(NFA_CONCAT);
1797 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001798 }
1799
Bram Moolenaar51a29832013-05-28 22:30:35 +02001800 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001801 } /* while (p < endp) */
1802
Bram Moolenaar51a29832013-05-28 22:30:35 +02001803 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001804 if (*regparse == '-') /* if last, '-' is just a char */
1805 {
1806 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001807 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001808 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001809
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001810 /* skip the trailing ] */
1811 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001812 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001813
1814 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001815 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001816 EMIT(NFA_END_NEG_COLL);
1817 else
1818 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001819
1820 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001821 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001822 {
1823 EMIT(reg_string ? NL : NFA_NEWL);
1824 EMIT(NFA_OR);
1825 }
1826
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001827 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001828 } /* if exists closing ] */
1829
1830 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001831 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001832 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001833
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001834 default:
1835 {
1836#ifdef FEAT_MBYTE
1837 int plen;
1838
1839nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001840 /* plen is length of current char with composing chars */
1841 if (enc_utf8 && ((*mb_char2len)(c)
1842 != (plen = (*mb_ptr2len)(old_regparse))
1843 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001844 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001845 int i = 0;
1846
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001847 /* A base character plus composing characters, or just one
1848 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001849 * This requires creating a separate atom as if enclosing
1850 * the characters in (), where NFA_COMPOSING is the ( and
1851 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001852 * building the postfix form, not the NFA itself;
1853 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001854 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001855 for (;;)
1856 {
1857 EMIT(c);
1858 if (i > 0)
1859 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001860 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001861 break;
1862 c = utf_ptr2char(old_regparse + i);
1863 }
1864 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001865 regparse = old_regparse + plen;
1866 }
1867 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868#endif
1869 {
1870 c = no_Magic(c);
1871 EMIT(c);
1872 }
1873 return OK;
1874 }
1875 }
1876
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001877 return OK;
1878}
1879
1880/*
1881 * Parse something followed by possible [*+=].
1882 *
1883 * A piece is an atom, possibly followed by a multi, an indication of how many
1884 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1885 * characters: "", "a", "aa", etc.
1886 *
1887 * piece ::= atom
1888 * or atom multi
1889 */
1890 static int
1891nfa_regpiece()
1892{
1893 int i;
1894 int op;
1895 int ret;
1896 long minval, maxval;
1897 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001898 parse_state_T old_state;
1899 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001900 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001901 int old_post_pos;
1902 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001903 int quest;
1904
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001905 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1906 * next. */
1907 save_parse_state(&old_state);
1908
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001909 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001910 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001911
1912 ret = nfa_regatom();
1913 if (ret == FAIL)
1914 return FAIL; /* cascaded error */
1915
1916 op = peekchr();
1917 if (re_multi_type(op) == NOT_MULTI)
1918 return OK;
1919
1920 skipchr();
1921 switch (op)
1922 {
1923 case Magic('*'):
1924 EMIT(NFA_STAR);
1925 break;
1926
1927 case Magic('+'):
1928 /*
1929 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1930 * first and only submatch would be "aaa". But the backtracking
1931 * engine interprets the plus as "try matching one more time", and
1932 * a* matches a second time at the end of the input, the empty
1933 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001934 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001935 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001936 * In order to be consistent with the old engine, we replace
1937 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001938 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001939 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001940 curchr = -1;
1941 if (nfa_regatom() == FAIL)
1942 return FAIL;
1943 EMIT(NFA_STAR);
1944 EMIT(NFA_CONCAT);
1945 skipchr(); /* skip the \+ */
1946 break;
1947
1948 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001949 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001950 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001951 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952 switch(op)
1953 {
1954 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001955 /* \@= */
1956 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001957 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001958 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001959 /* \@! */
1960 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001961 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001962 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001963 op = no_Magic(getchr());
1964 if (op == '=')
1965 /* \@<= */
1966 i = NFA_PREV_ATOM_JUST_BEFORE;
1967 else if (op == '!')
1968 /* \@<! */
1969 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1970 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001971 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001972 /* \@> */
1973 i = NFA_PREV_ATOM_LIKE_PATTERN;
1974 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001975 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001976 if (i == 0)
1977 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001978 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1979 return FAIL;
1980 }
1981 EMIT(i);
1982 if (i == NFA_PREV_ATOM_JUST_BEFORE
1983 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1984 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001985 break;
1986
1987 case Magic('?'):
1988 case Magic('='):
1989 EMIT(NFA_QUEST);
1990 break;
1991
1992 case Magic('{'):
1993 /* a{2,5} will expand to 'aaa?a?a?'
1994 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1995 * version of '?'
1996 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1997 * parenthesis have the same id
1998 */
1999
2000 greedy = TRUE;
2001 c2 = peekchr();
2002 if (c2 == '-' || c2 == Magic('-'))
2003 {
2004 skipchr();
2005 greedy = FALSE;
2006 }
2007 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002008 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02002009
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002010 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
2011 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002012 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002013 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002014 if (greedy)
2015 /* \{}, \{0,} */
2016 EMIT(NFA_STAR);
2017 else
2018 /* \{-}, \{-0,} */
2019 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002020 break;
2021 }
2022
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002023 /* Special case: x{0} or x{-0} */
2024 if (maxval == 0)
2025 {
2026 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002027 post_ptr = post_start + my_post_start;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002028 /* NFA_EMPTY is 0-length and works everywhere */
2029 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002030 return OK;
2031 }
2032
2033 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002034 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002035 /* Save parse state after the repeated atom and the \{} */
2036 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002037
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002038 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
2039 for (i = 0; i < maxval; i++)
2040 {
2041 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002042 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002043 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002044 if (nfa_regatom() == FAIL)
2045 return FAIL;
2046 /* after "minval" times, atoms are optional */
2047 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002048 {
2049 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002050 {
2051 if (greedy)
2052 EMIT(NFA_STAR);
2053 else
2054 EMIT(NFA_STAR_NONGREEDY);
2055 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002056 else
2057 EMIT(quest);
2058 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02002059 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002060 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02002061 if (i + 1 > minval && maxval == MAX_LIMIT)
2062 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002063 }
2064
2065 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002066 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002067 curchr = -1;
2068
2069 break;
2070
2071
2072 default:
2073 break;
2074 } /* end switch */
2075
2076 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002077 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002078 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002079
2080 return OK;
2081}
2082
2083/*
2084 * Parse one or more pieces, concatenated. It matches a match for the
2085 * first piece, followed by a match for the second piece, etc. Example:
2086 * "f[0-9]b", first matches "f", then a digit and then "b".
2087 *
2088 * concat ::= piece
2089 * or piece piece
2090 * or piece piece piece
2091 * etc.
2092 */
2093 static int
2094nfa_regconcat()
2095{
2096 int cont = TRUE;
2097 int first = TRUE;
2098
2099 while (cont)
2100 {
2101 switch (peekchr())
2102 {
2103 case NUL:
2104 case Magic('|'):
2105 case Magic('&'):
2106 case Magic(')'):
2107 cont = FALSE;
2108 break;
2109
2110 case Magic('Z'):
2111#ifdef FEAT_MBYTE
2112 regflags |= RF_ICOMBINE;
2113#endif
2114 skipchr_keepstart();
2115 break;
2116 case Magic('c'):
2117 regflags |= RF_ICASE;
2118 skipchr_keepstart();
2119 break;
2120 case Magic('C'):
2121 regflags |= RF_NOICASE;
2122 skipchr_keepstart();
2123 break;
2124 case Magic('v'):
2125 reg_magic = MAGIC_ALL;
2126 skipchr_keepstart();
2127 curchr = -1;
2128 break;
2129 case Magic('m'):
2130 reg_magic = MAGIC_ON;
2131 skipchr_keepstart();
2132 curchr = -1;
2133 break;
2134 case Magic('M'):
2135 reg_magic = MAGIC_OFF;
2136 skipchr_keepstart();
2137 curchr = -1;
2138 break;
2139 case Magic('V'):
2140 reg_magic = MAGIC_NONE;
2141 skipchr_keepstart();
2142 curchr = -1;
2143 break;
2144
2145 default:
2146 if (nfa_regpiece() == FAIL)
2147 return FAIL;
2148 if (first == FALSE)
2149 EMIT(NFA_CONCAT);
2150 else
2151 first = FALSE;
2152 break;
2153 }
2154 }
2155
2156 return OK;
2157}
2158
2159/*
2160 * Parse a branch, one or more concats, separated by "\&". It matches the
2161 * last concat, but only if all the preceding concats also match at the same
2162 * position. Examples:
2163 * "foobeep\&..." matches "foo" in "foobeep".
2164 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
2165 *
2166 * branch ::= concat
2167 * or concat \& concat
2168 * or concat \& concat \& concat
2169 * etc.
2170 */
2171 static int
2172nfa_regbranch()
2173{
2174 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02002175 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002176
Bram Moolenaar16299b52013-05-30 18:45:23 +02002177 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002178
2179 /* First branch, possibly the only one */
2180 if (nfa_regconcat() == FAIL)
2181 return FAIL;
2182
2183 ch = peekchr();
2184 /* Try next concats */
2185 while (ch == Magic('&'))
2186 {
2187 skipchr();
2188 EMIT(NFA_NOPEN);
2189 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02002190 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002191 if (nfa_regconcat() == FAIL)
2192 return FAIL;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002193 /* if concat is empty do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002194 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002195 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002196 EMIT(NFA_CONCAT);
2197 ch = peekchr();
2198 }
2199
Bram Moolenaar699c1202013-09-25 16:41:54 +02002200 /* if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02002201 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaar699c1202013-09-25 16:41:54 +02002202 EMIT(NFA_EMPTY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002203
2204 return OK;
2205}
2206
2207/*
2208 * Parse a pattern, one or more branches, separated by "\|". It matches
2209 * anything that matches one of the branches. Example: "foo\|beep" matches
2210 * "foo" and matches "beep". If more than one branch matches, the first one
2211 * is used.
2212 *
2213 * pattern ::= branch
2214 * or branch \| branch
2215 * or branch \| branch \| branch
2216 * etc.
2217 */
2218 static int
2219nfa_reg(paren)
2220 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
2221{
2222 int parno = 0;
2223
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002224 if (paren == REG_PAREN)
2225 {
2226 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002227 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002228 parno = regnpar++;
2229 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002230#ifdef FEAT_SYN_HL
2231 else if (paren == REG_ZPAREN)
2232 {
2233 /* Make a ZOPEN node. */
2234 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002235 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002236 parno = regnzpar++;
2237 }
2238#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002239
2240 if (nfa_regbranch() == FAIL)
2241 return FAIL; /* cascaded error */
2242
2243 while (peekchr() == Magic('|'))
2244 {
2245 skipchr();
2246 if (nfa_regbranch() == FAIL)
2247 return FAIL; /* cascaded error */
2248 EMIT(NFA_OR);
2249 }
2250
2251 /* Check for proper termination. */
2252 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2253 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002254 if (paren == REG_NPAREN)
2255 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
2256 else
2257 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
2258 }
2259 else if (paren == REG_NOPAREN && peekchr() != NUL)
2260 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002261 if (peekchr() == Magic(')'))
2262 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
2263 else
2264 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
2265 }
2266 /*
2267 * Here we set the flag allowing back references to this set of
2268 * parentheses.
2269 */
2270 if (paren == REG_PAREN)
2271 {
2272 had_endbrace[parno] = TRUE; /* have seen the close paren */
2273 EMIT(NFA_MOPEN + parno);
2274 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002275#ifdef FEAT_SYN_HL
2276 else if (paren == REG_ZPAREN)
2277 EMIT(NFA_ZOPEN + parno);
2278#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002279
2280 return OK;
2281}
2282
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002283#ifdef DEBUG
2284static char_u code[50];
2285
2286 static void
2287nfa_set_code(c)
2288 int c;
2289{
2290 int addnl = FALSE;
2291
2292 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2293 {
2294 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002295 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002296 }
2297
2298 STRCPY(code, "");
2299 switch (c)
2300 {
2301 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2302 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2303 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2304 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2305 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2306 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2307
Bram Moolenaar5714b802013-05-28 22:03:20 +02002308 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2309 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2310 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2311 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2312 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2313 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2314 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2315 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2316 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002317#ifdef FEAT_SYN_HL
2318 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2319 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2320 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2321 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2322 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2323 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2324 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2325 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2326 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2327#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002328 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330 case NFA_PREV_ATOM_NO_WIDTH:
2331 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002332 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2333 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002334 case NFA_PREV_ATOM_JUST_BEFORE:
2335 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2336 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2337 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002338 case NFA_PREV_ATOM_LIKE_PATTERN:
2339 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2340
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002341 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2342 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002343 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002344 case NFA_START_INVISIBLE_FIRST:
2345 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002346 case NFA_START_INVISIBLE_NEG:
2347 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002348 case NFA_START_INVISIBLE_NEG_FIRST:
2349 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002350 case NFA_START_INVISIBLE_BEFORE:
2351 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002352 case NFA_START_INVISIBLE_BEFORE_FIRST:
2353 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002354 case NFA_START_INVISIBLE_BEFORE_NEG:
2355 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002356 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2357 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002358 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002359 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002360 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002361 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002363 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2364 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002365 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002366
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002367 case NFA_MOPEN:
2368 case NFA_MOPEN1:
2369 case NFA_MOPEN2:
2370 case NFA_MOPEN3:
2371 case NFA_MOPEN4:
2372 case NFA_MOPEN5:
2373 case NFA_MOPEN6:
2374 case NFA_MOPEN7:
2375 case NFA_MOPEN8:
2376 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002377 STRCPY(code, "NFA_MOPEN(x)");
2378 code[10] = c - NFA_MOPEN + '0';
2379 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002380 case NFA_MCLOSE:
2381 case NFA_MCLOSE1:
2382 case NFA_MCLOSE2:
2383 case NFA_MCLOSE3:
2384 case NFA_MCLOSE4:
2385 case NFA_MCLOSE5:
2386 case NFA_MCLOSE6:
2387 case NFA_MCLOSE7:
2388 case NFA_MCLOSE8:
2389 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002390 STRCPY(code, "NFA_MCLOSE(x)");
2391 code[11] = c - NFA_MCLOSE + '0';
2392 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002393#ifdef FEAT_SYN_HL
2394 case NFA_ZOPEN:
2395 case NFA_ZOPEN1:
2396 case NFA_ZOPEN2:
2397 case NFA_ZOPEN3:
2398 case NFA_ZOPEN4:
2399 case NFA_ZOPEN5:
2400 case NFA_ZOPEN6:
2401 case NFA_ZOPEN7:
2402 case NFA_ZOPEN8:
2403 case NFA_ZOPEN9:
2404 STRCPY(code, "NFA_ZOPEN(x)");
2405 code[10] = c - NFA_ZOPEN + '0';
2406 break;
2407 case NFA_ZCLOSE:
2408 case NFA_ZCLOSE1:
2409 case NFA_ZCLOSE2:
2410 case NFA_ZCLOSE3:
2411 case NFA_ZCLOSE4:
2412 case NFA_ZCLOSE5:
2413 case NFA_ZCLOSE6:
2414 case NFA_ZCLOSE7:
2415 case NFA_ZCLOSE8:
2416 case NFA_ZCLOSE9:
2417 STRCPY(code, "NFA_ZCLOSE(x)");
2418 code[11] = c - NFA_ZCLOSE + '0';
2419 break;
2420#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002421 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2422 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2423 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2424 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002425 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2426 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002427 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2428 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2429 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2430 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2431 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2432 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2433 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2434 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2435 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2436 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2437 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2438 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2439 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2440 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002441 case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002442
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002443 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002444 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2445 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2446 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaar699c1202013-09-25 16:41:54 +02002447 case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002448 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002449
2450 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2451 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2452 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2453 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2454 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2455 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2456 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2457
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002458 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2459 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2460 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2461 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2462 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2463 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2464 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2465 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2466 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2467 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2468 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2469 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2470 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2471 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2472 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2473 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2474
2475 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2476 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2477 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2478 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2479 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2480 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2481 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2482 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2483 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2484 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2485 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2486 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2487 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2488 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2489 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2490 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2491 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2492 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2493 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2494 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2495 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2496 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2497 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2498 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2499 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2500 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2501 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002502 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2503 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2504 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2505 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002506
2507 default:
2508 STRCPY(code, "CHAR(x)");
2509 code[5] = c;
2510 }
2511
2512 if (addnl == TRUE)
2513 STRCAT(code, " + NEWLINE ");
2514
2515}
2516
2517#ifdef ENABLE_LOG
2518static FILE *log_fd;
2519
2520/*
2521 * Print the postfix notation of the current regexp.
2522 */
2523 static void
2524nfa_postfix_dump(expr, retval)
2525 char_u *expr;
2526 int retval;
2527{
2528 int *p;
2529 FILE *f;
2530
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002531 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002532 if (f != NULL)
2533 {
2534 fprintf(f, "\n-------------------------\n");
2535 if (retval == FAIL)
2536 fprintf(f, ">>> NFA engine failed ... \n");
2537 else if (retval == OK)
2538 fprintf(f, ">>> NFA engine succeeded !\n");
2539 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002540 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002541 {
2542 nfa_set_code(*p);
2543 fprintf(f, "%s, ", code);
2544 }
2545 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002546 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 fprintf(f, "%d ", *p);
2548 fprintf(f, "\n\n");
2549 fclose(f);
2550 }
2551}
2552
2553/*
2554 * Print the NFA starting with a root node "state".
2555 */
2556 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002557nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002558 FILE *debugf;
2559 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002560{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002561 garray_T indent;
2562
2563 ga_init2(&indent, 1, 64);
2564 ga_append(&indent, '\0');
2565 nfa_print_state2(debugf, state, &indent);
2566 ga_clear(&indent);
2567}
2568
2569 static void
2570nfa_print_state2(debugf, state, indent)
2571 FILE *debugf;
2572 nfa_state_T *state;
2573 garray_T *indent;
2574{
2575 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002576
2577 if (state == NULL)
2578 return;
2579
2580 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002581
2582 /* Output indent */
2583 p = (char_u *)indent->ga_data;
2584 if (indent->ga_len >= 3)
2585 {
2586 int last = indent->ga_len - 3;
2587 char_u save[2];
2588
2589 STRNCPY(save, &p[last], 2);
2590 STRNCPY(&p[last], "+-", 2);
2591 fprintf(debugf, " %s", p);
2592 STRNCPY(&p[last], save, 2);
2593 }
2594 else
2595 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002596
2597 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002598 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002599 code,
2600 state->c,
2601 abs(state->id),
2602 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002603 if (state->id < 0)
2604 return;
2605
2606 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002607
2608 /* grow indent for state->out */
2609 indent->ga_len -= 1;
2610 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002611 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002612 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002613 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002614 ga_append(indent, '\0');
2615
2616 nfa_print_state2(debugf, state->out, indent);
2617
2618 /* replace last part of indent for state->out1 */
2619 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002620 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002621 ga_append(indent, '\0');
2622
2623 nfa_print_state2(debugf, state->out1, indent);
2624
2625 /* shrink indent */
2626 indent->ga_len -= 3;
2627 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002628}
2629
2630/*
2631 * Print the NFA state machine.
2632 */
2633 static void
2634nfa_dump(prog)
2635 nfa_regprog_T *prog;
2636{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002637 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002638
2639 if (debugf != NULL)
2640 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002641 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002642
Bram Moolenaar473de612013-06-08 18:19:48 +02002643 if (prog->reganch)
2644 fprintf(debugf, "reganch: %d\n", prog->reganch);
2645 if (prog->regstart != NUL)
2646 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2647 prog->regstart, prog->regstart);
2648 if (prog->match_text != NULL)
2649 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002650
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002651 fclose(debugf);
2652 }
2653}
2654#endif /* ENABLE_LOG */
2655#endif /* DEBUG */
2656
2657/*
2658 * Parse r.e. @expr and convert it into postfix form.
2659 * Return the postfix string on success, NULL otherwise.
2660 */
2661 static int *
2662re2post()
2663{
2664 if (nfa_reg(REG_NOPAREN) == FAIL)
2665 return NULL;
2666 EMIT(NFA_MOPEN);
2667 return post_start;
2668}
2669
2670/* NB. Some of the code below is inspired by Russ's. */
2671
2672/*
2673 * Represents an NFA state plus zero or one or two arrows exiting.
2674 * if c == MATCH, no arrows out; matching state.
2675 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2676 * If c < 256, labeled arrow with character c to out.
2677 */
2678
2679static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2680
2681/*
2682 * Allocate and initialize nfa_state_T.
2683 */
2684 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002685alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002686 int c;
2687 nfa_state_T *out;
2688 nfa_state_T *out1;
2689{
2690 nfa_state_T *s;
2691
2692 if (istate >= nstate)
2693 return NULL;
2694
2695 s = &state_ptr[istate++];
2696
2697 s->c = c;
2698 s->out = out;
2699 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002700 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002701
2702 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002703 s->lastlist[0] = 0;
2704 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002705
2706 return s;
2707}
2708
2709/*
2710 * A partially built NFA without the matching state filled in.
2711 * Frag_T.start points at the start state.
2712 * Frag_T.out is a list of places that need to be set to the
2713 * next state for this fragment.
2714 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002715
2716/* Since the out pointers in the list are always
2717 * uninitialized, we use the pointers themselves
2718 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002719typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002720union Ptrlist
2721{
2722 Ptrlist *next;
2723 nfa_state_T *s;
2724};
2725
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002726struct Frag
2727{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002728 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002729 Ptrlist *out;
2730};
2731typedef struct Frag Frag_T;
2732
2733static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2734static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2735static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2736static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2737static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2738static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2739
2740/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002741 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002742 */
2743 static Frag_T
2744frag(start, out)
2745 nfa_state_T *start;
2746 Ptrlist *out;
2747{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002748 Frag_T n;
2749
2750 n.start = start;
2751 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002752 return n;
2753}
2754
2755/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002756 * Create singleton list containing just outp.
2757 */
2758 static Ptrlist *
2759list1(outp)
2760 nfa_state_T **outp;
2761{
2762 Ptrlist *l;
2763
2764 l = (Ptrlist *)outp;
2765 l->next = NULL;
2766 return l;
2767}
2768
2769/*
2770 * Patch the list of states at out to point to start.
2771 */
2772 static void
2773patch(l, s)
2774 Ptrlist *l;
2775 nfa_state_T *s;
2776{
2777 Ptrlist *next;
2778
2779 for (; l; l = next)
2780 {
2781 next = l->next;
2782 l->s = s;
2783 }
2784}
2785
2786
2787/*
2788 * Join the two lists l1 and l2, returning the combination.
2789 */
2790 static Ptrlist *
2791append(l1, l2)
2792 Ptrlist *l1;
2793 Ptrlist *l2;
2794{
2795 Ptrlist *oldl1;
2796
2797 oldl1 = l1;
2798 while (l1->next)
2799 l1 = l1->next;
2800 l1->next = l2;
2801 return oldl1;
2802}
2803
2804/*
2805 * Stack used for transforming postfix form into NFA.
2806 */
2807static Frag_T empty;
2808
2809 static void
2810st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002811 int *postfix UNUSED;
2812 int *end UNUSED;
2813 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002814{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002815#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002816 FILE *df;
2817 int *p2;
2818
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002819 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002820 if (df)
2821 {
2822 fprintf(df, "Error popping the stack!\n");
2823#ifdef DEBUG
2824 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2825#endif
2826 fprintf(df, "Postfix form is: ");
2827#ifdef DEBUG
2828 for (p2 = postfix; p2 < end; p2++)
2829 {
2830 nfa_set_code(*p2);
2831 fprintf(df, "%s, ", code);
2832 }
2833 nfa_set_code(*p);
2834 fprintf(df, "\nCurrent position is: ");
2835 for (p2 = postfix; p2 <= p; p2 ++)
2836 {
2837 nfa_set_code(*p2);
2838 fprintf(df, "%s, ", code);
2839 }
2840#else
2841 for (p2 = postfix; p2 < end; p2++)
2842 {
2843 fprintf(df, "%d, ", *p2);
2844 }
2845 fprintf(df, "\nCurrent position is: ");
2846 for (p2 = postfix; p2 <= p; p2 ++)
2847 {
2848 fprintf(df, "%d, ", *p2);
2849 }
2850#endif
2851 fprintf(df, "\n--------------------------\n");
2852 fclose(df);
2853 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002854#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002855 EMSG(_("E874: (NFA) Could not pop the stack !"));
2856}
2857
2858/*
2859 * Push an item onto the stack.
2860 */
2861 static void
2862st_push(s, p, stack_end)
2863 Frag_T s;
2864 Frag_T **p;
2865 Frag_T *stack_end;
2866{
2867 Frag_T *stackp = *p;
2868
2869 if (stackp >= stack_end)
2870 return;
2871 *stackp = s;
2872 *p = *p + 1;
2873}
2874
2875/*
2876 * Pop an item from the stack.
2877 */
2878 static Frag_T
2879st_pop(p, stack)
2880 Frag_T **p;
2881 Frag_T *stack;
2882{
2883 Frag_T *stackp;
2884
2885 *p = *p - 1;
2886 stackp = *p;
2887 if (stackp < stack)
2888 return empty;
2889 return **p;
2890}
2891
2892/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002893 * Estimate the maximum byte length of anything matching "state".
2894 * When unknown or unlimited return -1.
2895 */
2896 static int
2897nfa_max_width(startstate, depth)
2898 nfa_state_T *startstate;
2899 int depth;
2900{
2901 int l, r;
2902 nfa_state_T *state = startstate;
2903 int len = 0;
2904
2905 /* detect looping in a NFA_SPLIT */
2906 if (depth > 4)
2907 return -1;
2908
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002909 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002910 {
2911 switch (state->c)
2912 {
2913 case NFA_END_INVISIBLE:
2914 case NFA_END_INVISIBLE_NEG:
2915 /* the end, return what we have */
2916 return len;
2917
2918 case NFA_SPLIT:
2919 /* two alternatives, use the maximum */
2920 l = nfa_max_width(state->out, depth + 1);
2921 r = nfa_max_width(state->out1, depth + 1);
2922 if (l < 0 || r < 0)
2923 return -1;
2924 return len + (l > r ? l : r);
2925
2926 case NFA_ANY:
2927 case NFA_START_COLL:
2928 case NFA_START_NEG_COLL:
2929 /* matches some character, including composing chars */
2930#ifdef FEAT_MBYTE
2931 if (enc_utf8)
2932 len += MB_MAXBYTES;
2933 else if (has_mbyte)
2934 len += 2;
2935 else
2936#endif
2937 ++len;
2938 if (state->c != NFA_ANY)
2939 {
2940 /* skip over the characters */
2941 state = state->out1->out;
2942 continue;
2943 }
2944 break;
2945
2946 case NFA_DIGIT:
2947 case NFA_WHITE:
2948 case NFA_HEX:
2949 case NFA_OCTAL:
2950 /* ascii */
2951 ++len;
2952 break;
2953
2954 case NFA_IDENT:
2955 case NFA_SIDENT:
2956 case NFA_KWORD:
2957 case NFA_SKWORD:
2958 case NFA_FNAME:
2959 case NFA_SFNAME:
2960 case NFA_PRINT:
2961 case NFA_SPRINT:
2962 case NFA_NWHITE:
2963 case NFA_NDIGIT:
2964 case NFA_NHEX:
2965 case NFA_NOCTAL:
2966 case NFA_WORD:
2967 case NFA_NWORD:
2968 case NFA_HEAD:
2969 case NFA_NHEAD:
2970 case NFA_ALPHA:
2971 case NFA_NALPHA:
2972 case NFA_LOWER:
2973 case NFA_NLOWER:
2974 case NFA_UPPER:
2975 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002976 case NFA_LOWER_IC:
2977 case NFA_NLOWER_IC:
2978 case NFA_UPPER_IC:
2979 case NFA_NUPPER_IC:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002980 case NFA_ANY_COMPOSING:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002981 /* possibly non-ascii */
2982#ifdef FEAT_MBYTE
2983 if (has_mbyte)
2984 len += 3;
2985 else
2986#endif
2987 ++len;
2988 break;
2989
2990 case NFA_START_INVISIBLE:
2991 case NFA_START_INVISIBLE_NEG:
2992 case NFA_START_INVISIBLE_BEFORE:
2993 case NFA_START_INVISIBLE_BEFORE_NEG:
2994 /* zero-width, out1 points to the END state */
2995 state = state->out1->out;
2996 continue;
2997
2998 case NFA_BACKREF1:
2999 case NFA_BACKREF2:
3000 case NFA_BACKREF3:
3001 case NFA_BACKREF4:
3002 case NFA_BACKREF5:
3003 case NFA_BACKREF6:
3004 case NFA_BACKREF7:
3005 case NFA_BACKREF8:
3006 case NFA_BACKREF9:
3007#ifdef FEAT_SYN_HL
3008 case NFA_ZREF1:
3009 case NFA_ZREF2:
3010 case NFA_ZREF3:
3011 case NFA_ZREF4:
3012 case NFA_ZREF5:
3013 case NFA_ZREF6:
3014 case NFA_ZREF7:
3015 case NFA_ZREF8:
3016 case NFA_ZREF9:
3017#endif
3018 case NFA_NEWL:
3019 case NFA_SKIP:
3020 /* unknown width */
3021 return -1;
3022
3023 case NFA_BOL:
3024 case NFA_EOL:
3025 case NFA_BOF:
3026 case NFA_EOF:
3027 case NFA_BOW:
3028 case NFA_EOW:
3029 case NFA_MOPEN:
3030 case NFA_MOPEN1:
3031 case NFA_MOPEN2:
3032 case NFA_MOPEN3:
3033 case NFA_MOPEN4:
3034 case NFA_MOPEN5:
3035 case NFA_MOPEN6:
3036 case NFA_MOPEN7:
3037 case NFA_MOPEN8:
3038 case NFA_MOPEN9:
3039#ifdef FEAT_SYN_HL
3040 case NFA_ZOPEN:
3041 case NFA_ZOPEN1:
3042 case NFA_ZOPEN2:
3043 case NFA_ZOPEN3:
3044 case NFA_ZOPEN4:
3045 case NFA_ZOPEN5:
3046 case NFA_ZOPEN6:
3047 case NFA_ZOPEN7:
3048 case NFA_ZOPEN8:
3049 case NFA_ZOPEN9:
3050 case NFA_ZCLOSE:
3051 case NFA_ZCLOSE1:
3052 case NFA_ZCLOSE2:
3053 case NFA_ZCLOSE3:
3054 case NFA_ZCLOSE4:
3055 case NFA_ZCLOSE5:
3056 case NFA_ZCLOSE6:
3057 case NFA_ZCLOSE7:
3058 case NFA_ZCLOSE8:
3059 case NFA_ZCLOSE9:
3060#endif
3061 case NFA_MCLOSE:
3062 case NFA_MCLOSE1:
3063 case NFA_MCLOSE2:
3064 case NFA_MCLOSE3:
3065 case NFA_MCLOSE4:
3066 case NFA_MCLOSE5:
3067 case NFA_MCLOSE6:
3068 case NFA_MCLOSE7:
3069 case NFA_MCLOSE8:
3070 case NFA_MCLOSE9:
3071 case NFA_NOPEN:
3072 case NFA_NCLOSE:
3073
3074 case NFA_LNUM_GT:
3075 case NFA_LNUM_LT:
3076 case NFA_COL_GT:
3077 case NFA_COL_LT:
3078 case NFA_VCOL_GT:
3079 case NFA_VCOL_LT:
3080 case NFA_MARK_GT:
3081 case NFA_MARK_LT:
3082 case NFA_VISUAL:
3083 case NFA_LNUM:
3084 case NFA_CURSOR:
3085 case NFA_COL:
3086 case NFA_VCOL:
3087 case NFA_MARK:
3088
3089 case NFA_ZSTART:
3090 case NFA_ZEND:
3091 case NFA_OPT_CHARS:
Bram Moolenaar699c1202013-09-25 16:41:54 +02003092 case NFA_EMPTY:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003093 case NFA_START_PATTERN:
3094 case NFA_END_PATTERN:
3095 case NFA_COMPOSING:
3096 case NFA_END_COMPOSING:
3097 /* zero-width */
3098 break;
3099
3100 default:
3101 if (state->c < 0)
3102 /* don't know what this is */
3103 return -1;
3104 /* normal character */
3105 len += MB_CHAR2LEN(state->c);
3106 break;
3107 }
3108
3109 /* normal way to continue */
3110 state = state->out;
3111 }
3112
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02003113 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003114 return -1;
3115}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003116
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003117/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003118 * Convert a postfix form into its equivalent NFA.
3119 * Return the NFA start state on success, NULL otherwise.
3120 */
3121 static nfa_state_T *
3122post2nfa(postfix, end, nfa_calc_size)
3123 int *postfix;
3124 int *end;
3125 int nfa_calc_size;
3126{
3127 int *p;
3128 int mopen;
3129 int mclose;
3130 Frag_T *stack = NULL;
3131 Frag_T *stackp = NULL;
3132 Frag_T *stack_end = NULL;
3133 Frag_T e1;
3134 Frag_T e2;
3135 Frag_T e;
3136 nfa_state_T *s;
3137 nfa_state_T *s1;
3138 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003139 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003140
3141 if (postfix == NULL)
3142 return NULL;
3143
Bram Moolenaar053bb602013-05-20 13:55:21 +02003144#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003145#define POP() st_pop(&stackp, stack); \
3146 if (stackp < stack) \
3147 { \
3148 st_error(postfix, end, p); \
3149 return NULL; \
3150 }
3151
3152 if (nfa_calc_size == FALSE)
3153 {
3154 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02003155 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003156 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02003157 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003158 }
3159
3160 for (p = postfix; p < end; ++p)
3161 {
3162 switch (*p)
3163 {
3164 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02003165 /* Concatenation.
3166 * Pay attention: this operator does not exist in the r.e. itself
3167 * (it is implicit, really). It is added when r.e. is translated
3168 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003169 if (nfa_calc_size == TRUE)
3170 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003171 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003172 break;
3173 }
3174 e2 = POP();
3175 e1 = POP();
3176 patch(e1.out, e2.start);
3177 PUSH(frag(e1.start, e2.out));
3178 break;
3179
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003180 case NFA_OR:
3181 /* Alternation */
3182 if (nfa_calc_size == TRUE)
3183 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003184 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003185 break;
3186 }
3187 e2 = POP();
3188 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003189 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003190 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003191 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003192 PUSH(frag(s, append(e1.out, e2.out)));
3193 break;
3194
3195 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003196 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003197 if (nfa_calc_size == TRUE)
3198 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003199 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003200 break;
3201 }
3202 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003203 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003204 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003205 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003206 patch(e.out, s);
3207 PUSH(frag(s, list1(&s->out1)));
3208 break;
3209
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003210 case NFA_STAR_NONGREEDY:
3211 /* Zero or more, prefer zero */
3212 if (nfa_calc_size == TRUE)
3213 {
3214 nstate++;
3215 break;
3216 }
3217 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003218 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02003219 if (s == NULL)
3220 goto theend;
3221 patch(e.out, s);
3222 PUSH(frag(s, list1(&s->out)));
3223 break;
3224
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003225 case NFA_QUEST:
3226 /* one or zero atoms=> greedy match */
3227 if (nfa_calc_size == TRUE)
3228 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003229 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003230 break;
3231 }
3232 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003233 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003235 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236 PUSH(frag(s, append(e.out, list1(&s->out1))));
3237 break;
3238
3239 case NFA_QUEST_NONGREEDY:
3240 /* zero or one atoms => non-greedy match */
3241 if (nfa_calc_size == TRUE)
3242 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003243 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003244 break;
3245 }
3246 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003247 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003248 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003249 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003250 PUSH(frag(s, append(e.out, list1(&s->out))));
3251 break;
3252
Bram Moolenaar417bad22013-06-07 14:08:30 +02003253 case NFA_END_COLL:
3254 case NFA_END_NEG_COLL:
3255 /* On the stack is the sequence starting with NFA_START_COLL or
3256 * NFA_START_NEG_COLL and all possible characters. Patch it to
3257 * add the output to the start. */
3258 if (nfa_calc_size == TRUE)
3259 {
3260 nstate++;
3261 break;
3262 }
3263 e = POP();
3264 s = alloc_state(NFA_END_COLL, NULL, NULL);
3265 if (s == NULL)
3266 goto theend;
3267 patch(e.out, s);
3268 e.start->out1 = s;
3269 PUSH(frag(e.start, list1(&s->out)));
3270 break;
3271
3272 case NFA_RANGE:
3273 /* Before this are two characters, the low and high end of a
3274 * range. Turn them into two states with MIN and MAX. */
3275 if (nfa_calc_size == TRUE)
3276 {
3277 /* nstate += 0; */
3278 break;
3279 }
3280 e2 = POP();
3281 e1 = POP();
3282 e2.start->val = e2.start->c;
3283 e2.start->c = NFA_RANGE_MAX;
3284 e1.start->val = e1.start->c;
3285 e1.start->c = NFA_RANGE_MIN;
3286 patch(e1.out, e2.start);
3287 PUSH(frag(e1.start, e2.out));
3288 break;
3289
Bram Moolenaar699c1202013-09-25 16:41:54 +02003290 case NFA_EMPTY:
3291 /* 0-length, used in a repetition with max/min count of 0 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003292 if (nfa_calc_size == TRUE)
3293 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003294 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003295 break;
3296 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02003297 s = alloc_state(NFA_EMPTY, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003298 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003299 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003300 PUSH(frag(s, list1(&s->out)));
3301 break;
3302
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003303 case NFA_OPT_CHARS:
3304 {
3305 int n;
3306
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003307 /* \%[abc] implemented as:
3308 * NFA_SPLIT
3309 * +-CHAR(a)
3310 * | +-NFA_SPLIT
3311 * | +-CHAR(b)
3312 * | | +-NFA_SPLIT
3313 * | | +-CHAR(c)
3314 * | | | +-next
3315 * | | +- next
3316 * | +- next
3317 * +- next
3318 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003319 n = *++p; /* get number of characters */
3320 if (nfa_calc_size == TRUE)
3321 {
3322 nstate += n;
3323 break;
3324 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003325 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003326 e1.out = NULL; /* stores list with out1's */
3327 s1 = NULL; /* previous NFA_SPLIT to connect to */
3328 while (n-- > 0)
3329 {
3330 e = POP(); /* get character */
3331 s = alloc_state(NFA_SPLIT, e.start, NULL);
3332 if (s == NULL)
3333 goto theend;
3334 if (e1.out == NULL)
3335 e1 = e;
3336 patch(e.out, s1);
3337 append(e1.out, list1(&s->out1));
3338 s1 = s;
3339 }
3340 PUSH(frag(s, e1.out));
3341 break;
3342 }
3343
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003344 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003345 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003346 case NFA_PREV_ATOM_JUST_BEFORE:
3347 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003348 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003349 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003350 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3351 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003352 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003353 int start_state;
3354 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003355 int n = 0;
3356 nfa_state_T *zend;
3357 nfa_state_T *skip;
3358
Bram Moolenaardecd9542013-06-07 16:31:50 +02003359 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003360 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003361 case NFA_PREV_ATOM_NO_WIDTH:
3362 start_state = NFA_START_INVISIBLE;
3363 end_state = NFA_END_INVISIBLE;
3364 break;
3365 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3366 start_state = NFA_START_INVISIBLE_NEG;
3367 end_state = NFA_END_INVISIBLE_NEG;
3368 break;
3369 case NFA_PREV_ATOM_JUST_BEFORE:
3370 start_state = NFA_START_INVISIBLE_BEFORE;
3371 end_state = NFA_END_INVISIBLE;
3372 break;
3373 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3374 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3375 end_state = NFA_END_INVISIBLE_NEG;
3376 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003377 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003378 start_state = NFA_START_PATTERN;
3379 end_state = NFA_END_PATTERN;
3380 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003381 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003382
3383 if (before)
3384 n = *++p; /* get the count */
3385
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003386 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003387 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003388 * The \@<= operator: match for the preceding atom.
3389 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003390 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003391 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003392
3393 if (nfa_calc_size == TRUE)
3394 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003395 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003396 break;
3397 }
3398 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003399 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003400 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003401 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003402
Bram Moolenaar87953742013-06-05 18:52:40 +02003403 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003404 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003405 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003406 if (pattern)
3407 {
3408 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3409 skip = alloc_state(NFA_SKIP, NULL, NULL);
3410 zend = alloc_state(NFA_ZEND, s1, NULL);
3411 s1->out= skip;
3412 patch(e.out, zend);
3413 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003414 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003415 else
3416 {
3417 patch(e.out, s1);
3418 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003419 if (before)
3420 {
3421 if (n <= 0)
3422 /* See if we can guess the maximum width, it avoids a
3423 * lot of pointless tries. */
3424 n = nfa_max_width(e.start, 0);
3425 s->val = n; /* store the count */
3426 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003427 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003428 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003429 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003430
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003431#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003432 case NFA_COMPOSING: /* char with composing char */
3433#if 0
3434 /* TODO */
3435 if (regflags & RF_ICOMBINE)
3436 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003437 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003438 }
3439#endif
3440 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003441#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003442
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003443 case NFA_MOPEN: /* \( \) Submatch */
3444 case NFA_MOPEN1:
3445 case NFA_MOPEN2:
3446 case NFA_MOPEN3:
3447 case NFA_MOPEN4:
3448 case NFA_MOPEN5:
3449 case NFA_MOPEN6:
3450 case NFA_MOPEN7:
3451 case NFA_MOPEN8:
3452 case NFA_MOPEN9:
3453#ifdef FEAT_SYN_HL
3454 case NFA_ZOPEN: /* \z( \) Submatch */
3455 case NFA_ZOPEN1:
3456 case NFA_ZOPEN2:
3457 case NFA_ZOPEN3:
3458 case NFA_ZOPEN4:
3459 case NFA_ZOPEN5:
3460 case NFA_ZOPEN6:
3461 case NFA_ZOPEN7:
3462 case NFA_ZOPEN8:
3463 case NFA_ZOPEN9:
3464#endif
3465 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003466 if (nfa_calc_size == TRUE)
3467 {
3468 nstate += 2;
3469 break;
3470 }
3471
3472 mopen = *p;
3473 switch (*p)
3474 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003475 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3476#ifdef FEAT_SYN_HL
3477 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3478 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3479 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3480 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3481 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3482 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3483 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3484 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3485 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3486 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3487#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003488#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003489 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003490#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003491 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003492 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003493 mclose = *p + NSUBEXP;
3494 break;
3495 }
3496
3497 /* Allow "NFA_MOPEN" as a valid postfix representation for
3498 * the empty regexp "". In this case, the NFA will be
3499 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3500 * empty groups of parenthesis, and empty mbyte chars */
3501 if (stackp == stack)
3502 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003503 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003504 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003505 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003506 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003507 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003508 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003509 patch(list1(&s->out), s1);
3510 PUSH(frag(s, list1(&s1->out)));
3511 break;
3512 }
3513
3514 /* At least one node was emitted before NFA_MOPEN, so
3515 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3516 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003517 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003518 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003519 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520
Bram Moolenaar525666f2013-06-02 16:40:55 +02003521 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003522 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003523 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003524 patch(e.out, s1);
3525
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003526#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003527 if (mopen == NFA_COMPOSING)
3528 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003529 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003530#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003531
3532 PUSH(frag(s, list1(&s1->out)));
3533 break;
3534
Bram Moolenaar5714b802013-05-28 22:03:20 +02003535 case NFA_BACKREF1:
3536 case NFA_BACKREF2:
3537 case NFA_BACKREF3:
3538 case NFA_BACKREF4:
3539 case NFA_BACKREF5:
3540 case NFA_BACKREF6:
3541 case NFA_BACKREF7:
3542 case NFA_BACKREF8:
3543 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003544#ifdef FEAT_SYN_HL
3545 case NFA_ZREF1:
3546 case NFA_ZREF2:
3547 case NFA_ZREF3:
3548 case NFA_ZREF4:
3549 case NFA_ZREF5:
3550 case NFA_ZREF6:
3551 case NFA_ZREF7:
3552 case NFA_ZREF8:
3553 case NFA_ZREF9:
3554#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003555 if (nfa_calc_size == TRUE)
3556 {
3557 nstate += 2;
3558 break;
3559 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003560 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003561 if (s == NULL)
3562 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003563 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003564 if (s1 == NULL)
3565 goto theend;
3566 patch(list1(&s->out), s1);
3567 PUSH(frag(s, list1(&s1->out)));
3568 break;
3569
Bram Moolenaar423532e2013-05-29 21:14:42 +02003570 case NFA_LNUM:
3571 case NFA_LNUM_GT:
3572 case NFA_LNUM_LT:
3573 case NFA_VCOL:
3574 case NFA_VCOL_GT:
3575 case NFA_VCOL_LT:
3576 case NFA_COL:
3577 case NFA_COL_GT:
3578 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003579 case NFA_MARK:
3580 case NFA_MARK_GT:
3581 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003582 {
3583 int n = *++p; /* lnum, col or mark name */
3584
Bram Moolenaar423532e2013-05-29 21:14:42 +02003585 if (nfa_calc_size == TRUE)
3586 {
3587 nstate += 1;
3588 break;
3589 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003590 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003591 if (s == NULL)
3592 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003593 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003594 PUSH(frag(s, list1(&s->out)));
3595 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003596 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003597
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003598 case NFA_ZSTART:
3599 case NFA_ZEND:
3600 default:
3601 /* Operands */
3602 if (nfa_calc_size == TRUE)
3603 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003604 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003605 break;
3606 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003607 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003608 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003609 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003610 PUSH(frag(s, list1(&s->out)));
3611 break;
3612
3613 } /* switch(*p) */
3614
3615 } /* for(p = postfix; *p; ++p) */
3616
3617 if (nfa_calc_size == TRUE)
3618 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003619 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003620 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003621 }
3622
3623 e = POP();
3624 if (stackp != stack)
3625 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3626
3627 if (istate >= nstate)
3628 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3629
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003630 matchstate = &state_ptr[istate++]; /* the match state */
3631 matchstate->c = NFA_MATCH;
3632 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003633 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003634
3635 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003636 ret = e.start;
3637
3638theend:
3639 vim_free(stack);
3640 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003641
3642#undef POP1
3643#undef PUSH1
3644#undef POP2
3645#undef PUSH2
3646#undef POP
3647#undef PUSH
3648}
3649
Bram Moolenaara2947e22013-06-11 22:44:09 +02003650/*
3651 * After building the NFA program, inspect it to add optimization hints.
3652 */
3653 static void
3654nfa_postprocess(prog)
3655 nfa_regprog_T *prog;
3656{
3657 int i;
3658 int c;
3659
3660 for (i = 0; i < prog->nstate; ++i)
3661 {
3662 c = prog->state[i].c;
3663 if (c == NFA_START_INVISIBLE
3664 || c == NFA_START_INVISIBLE_NEG
3665 || c == NFA_START_INVISIBLE_BEFORE
3666 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3667 {
3668 int directly;
3669
3670 /* Do it directly when what follows is possibly the end of the
3671 * match. */
3672 if (match_follows(prog->state[i].out1->out, 0))
3673 directly = TRUE;
3674 else
3675 {
3676 int ch_invisible = failure_chance(prog->state[i].out, 0);
3677 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3678
3679 /* Postpone when the invisible match is expensive or has a
3680 * lower chance of failing. */
3681 if (c == NFA_START_INVISIBLE_BEFORE
3682 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3683 {
3684 /* "before" matches are very expensive when
3685 * unbounded, always prefer what follows then,
3686 * unless what follows will always match.
3687 * Otherwise strongly prefer what follows. */
3688 if (prog->state[i].val <= 0 && ch_follows > 0)
3689 directly = FALSE;
3690 else
3691 directly = ch_follows * 10 < ch_invisible;
3692 }
3693 else
3694 {
3695 /* normal invisible, first do the one with the
3696 * highest failure chance */
3697 directly = ch_follows < ch_invisible;
3698 }
3699 }
3700 if (directly)
3701 /* switch to the _FIRST state */
3702 ++prog->state[i].c;
3703 }
3704 }
3705}
3706
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003707/****************************************************************
3708 * NFA execution code.
3709 ****************************************************************/
3710
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003711typedef struct
3712{
3713 int in_use; /* number of subexpr with useful info */
3714
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003715 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003716 union
3717 {
3718 struct multipos
3719 {
3720 lpos_T start;
3721 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003722 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003723 struct linepos
3724 {
3725 char_u *start;
3726 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003727 } line[NSUBEXP];
3728 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003729} regsub_T;
3730
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003731typedef struct
3732{
3733 regsub_T norm; /* \( .. \) matches */
3734#ifdef FEAT_SYN_HL
3735 regsub_T synt; /* \z( .. \) matches */
3736#endif
3737} regsubs_T;
3738
Bram Moolenaara2d95102013-06-04 14:23:05 +02003739/* nfa_pim_T stores a Postponed Invisible Match. */
3740typedef struct nfa_pim_S nfa_pim_T;
3741struct nfa_pim_S
3742{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003743 int result; /* NFA_PIM_*, see below */
3744 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003745 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003746 union
3747 {
3748 lpos_T pos;
3749 char_u *ptr;
3750 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003751};
3752
3753/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003754#define NFA_PIM_UNUSED 0 /* pim not used */
3755#define NFA_PIM_TODO 1 /* pim not done yet */
3756#define NFA_PIM_MATCH 2 /* pim executed, matches */
3757#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003758
3759
Bram Moolenaar963fee22013-05-26 21:47:28 +02003760/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003761typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003762{
3763 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003764 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003765 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3766 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003767 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003768} nfa_thread_T;
3769
Bram Moolenaar963fee22013-05-26 21:47:28 +02003770/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003771typedef struct
3772{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003773 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003774 int n; /* nr of states currently in "t" */
3775 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003776 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003777 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003778} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003779
Bram Moolenaar5714b802013-05-28 22:03:20 +02003780#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003781static void log_subsexpr __ARGS((regsubs_T *subs));
3782static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003783static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003784
3785 static void
3786log_subsexpr(subs)
3787 regsubs_T *subs;
3788{
3789 log_subexpr(&subs->norm);
3790# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003791 if (nfa_has_zsubexpr)
3792 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003793# endif
3794}
3795
Bram Moolenaar5714b802013-05-28 22:03:20 +02003796 static void
3797log_subexpr(sub)
3798 regsub_T *sub;
3799{
3800 int j;
3801
3802 for (j = 0; j < sub->in_use; j++)
3803 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003804 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003805 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003806 sub->list.multi[j].start.col,
3807 (int)sub->list.multi[j].start.lnum,
3808 sub->list.multi[j].end.col,
3809 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003810 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003811 {
3812 char *s = (char *)sub->list.line[j].start;
3813 char *e = (char *)sub->list.line[j].end;
3814
Bram Moolenaar87953742013-06-05 18:52:40 +02003815 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003816 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003817 s == NULL ? "NULL" : s,
3818 e == NULL ? "NULL" : e);
3819 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003820}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003821
3822 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003823pim_info(pim)
3824 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003825{
3826 static char buf[30];
3827
3828 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3829 buf[0] = NUL;
3830 else
3831 {
3832 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3833 : (int)(pim->end.ptr - reginput));
3834 }
3835 return buf;
3836}
3837
Bram Moolenaar5714b802013-05-28 22:03:20 +02003838#endif
3839
Bram Moolenaar963fee22013-05-26 21:47:28 +02003840/* Used during execution: whether a match has been found. */
3841static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003842
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003843static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003844static void clear_sub __ARGS((regsub_T *sub));
3845static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3846static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaarf2118842013-09-25 18:16:38 +02003847static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003848static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003849static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003850static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3851static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003852static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003853static 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 +02003854static 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 +02003855
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003856/*
3857 * Copy postponed invisible match info from "from" to "to".
3858 */
3859 static void
3860copy_pim(to, from)
3861 nfa_pim_T *to;
3862 nfa_pim_T *from;
3863{
3864 to->result = from->result;
3865 to->state = from->state;
3866 copy_sub(&to->subs.norm, &from->subs.norm);
3867#ifdef FEAT_SYN_HL
3868 if (nfa_has_zsubexpr)
3869 copy_sub(&to->subs.synt, &from->subs.synt);
3870#endif
3871 to->end = from->end;
3872}
3873
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003874 static void
3875clear_sub(sub)
3876 regsub_T *sub;
3877{
3878 if (REG_MULTI)
3879 /* Use 0xff to set lnum to -1 */
3880 vim_memset(sub->list.multi, 0xff,
3881 sizeof(struct multipos) * nfa_nsubexpr);
3882 else
3883 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3884 sub->in_use = 0;
3885}
3886
3887/*
3888 * Copy the submatches from "from" to "to".
3889 */
3890 static void
3891copy_sub(to, from)
3892 regsub_T *to;
3893 regsub_T *from;
3894{
3895 to->in_use = from->in_use;
3896 if (from->in_use > 0)
3897 {
3898 /* Copy the match start and end positions. */
3899 if (REG_MULTI)
3900 mch_memmove(&to->list.multi[0],
3901 &from->list.multi[0],
3902 sizeof(struct multipos) * from->in_use);
3903 else
3904 mch_memmove(&to->list.line[0],
3905 &from->list.line[0],
3906 sizeof(struct linepos) * from->in_use);
3907 }
3908}
3909
3910/*
3911 * Like copy_sub() but exclude the main match.
3912 */
3913 static void
3914copy_sub_off(to, from)
3915 regsub_T *to;
3916 regsub_T *from;
3917{
3918 if (to->in_use < from->in_use)
3919 to->in_use = from->in_use;
3920 if (from->in_use > 1)
3921 {
3922 /* Copy the match start and end positions. */
3923 if (REG_MULTI)
3924 mch_memmove(&to->list.multi[1],
3925 &from->list.multi[1],
3926 sizeof(struct multipos) * (from->in_use - 1));
3927 else
3928 mch_memmove(&to->list.line[1],
3929 &from->list.line[1],
3930 sizeof(struct linepos) * (from->in_use - 1));
3931 }
3932}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003933
Bram Moolenaar428e9872013-05-30 17:05:39 +02003934/*
Bram Moolenaarf2118842013-09-25 18:16:38 +02003935 * Like copy_sub() but only do the end of the main match if \ze is present.
3936 */
3937 static void
3938copy_ze_off(to, from)
3939 regsub_T *to;
3940 regsub_T *from;
3941{
3942 if (nfa_has_zend)
3943 {
3944 if (REG_MULTI)
3945 {
3946 if (from->list.multi[0].end.lnum >= 0)
3947 to->list.multi[0].end = from->list.multi[0].end;
3948 }
3949 else
3950 {
3951 if (from->list.line[0].end != NULL)
3952 to->list.line[0].end = from->list.line[0].end;
3953 }
3954 }
3955}
3956
3957/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003958 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaaree482532014-05-13 15:56:51 +02003959 * When using back-references also check the end position.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003960 */
3961 static int
3962sub_equal(sub1, sub2)
3963 regsub_T *sub1;
3964 regsub_T *sub2;
3965{
3966 int i;
3967 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003968 linenr_T s1;
3969 linenr_T s2;
3970 char_u *sp1;
3971 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003972
3973 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3974 if (REG_MULTI)
3975 {
3976 for (i = 0; i < todo; ++i)
3977 {
3978 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003979 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003980 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003981 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003982 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003983 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003984 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003985 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003986 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003987 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02003988 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02003989 != sub2->list.multi[i].start.col)
3990 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02003991
3992 if (nfa_has_backref)
3993 {
3994 if (i < sub1->in_use)
3995 s1 = sub1->list.multi[i].end.lnum;
3996 else
3997 s1 = -1;
3998 if (i < sub2->in_use)
3999 s2 = sub2->list.multi[i].end.lnum;
4000 else
4001 s2 = -1;
4002 if (s1 != s2)
4003 return FALSE;
4004 if (s1 != -1 && sub1->list.multi[i].end.col
4005 != sub2->list.multi[i].end.col)
4006 return FALSE;
4007 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004008 }
4009 }
4010 else
4011 {
4012 for (i = 0; i < todo; ++i)
4013 {
4014 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004015 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004016 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004017 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004018 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004019 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004020 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02004021 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004022 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004023 return FALSE;
Bram Moolenaaree482532014-05-13 15:56:51 +02004024 if (nfa_has_backref)
4025 {
4026 if (i < sub1->in_use)
4027 sp1 = sub1->list.line[i].end;
4028 else
4029 sp1 = NULL;
4030 if (i < sub2->in_use)
4031 sp2 = sub2->list.line[i].end;
4032 else
4033 sp2 = NULL;
4034 if (sp1 != sp2)
4035 return FALSE;
4036 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004037 }
4038 }
4039
4040 return TRUE;
4041}
4042
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004043#ifdef ENABLE_LOG
4044 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004045report_state(char *action,
4046 regsub_T *sub,
4047 nfa_state_T *state,
4048 int lid,
4049 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004050{
4051 int col;
4052
4053 if (sub->in_use <= 0)
4054 col = -1;
4055 else if (REG_MULTI)
4056 col = sub->list.multi[0].start.col;
4057 else
4058 col = (int)(sub->list.line[0].start - regline);
4059 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004060 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
4061 action, abs(state->id), lid, state->c, code, col,
4062 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004063}
4064#endif
4065
Bram Moolenaar43e02982013-06-07 17:31:29 +02004066/*
4067 * Return TRUE if the same state is already in list "l" with the same
4068 * positions as "subs".
4069 */
4070 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02004071has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02004072 nfa_list_T *l; /* runtime state list */
4073 nfa_state_T *state; /* state to update */
4074 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004075 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02004076{
4077 nfa_thread_T *thread;
4078 int i;
4079
4080 for (i = 0; i < l->n; ++i)
4081 {
4082 thread = &l->t[i];
4083 if (thread->state->id == state->id
4084 && sub_equal(&thread->subs.norm, &subs->norm)
4085#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02004086 && (!nfa_has_zsubexpr
4087 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004088#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02004089 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004090 return TRUE;
4091 }
4092 return FALSE;
4093}
4094
4095/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02004096 * Return TRUE if "one" and "two" are equal. That includes when both are not
4097 * set.
4098 */
4099 static int
4100pim_equal(one, two)
4101 nfa_pim_T *one;
4102 nfa_pim_T *two;
4103{
4104 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
4105 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
4106
4107 if (one_unused)
4108 /* one is unused: equal when two is also unused */
4109 return two_unused;
4110 if (two_unused)
4111 /* one is used and two is not: not equal */
4112 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02004113 /* compare the state id */
4114 if (one->state->id != two->state->id)
4115 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02004116 /* compare the position */
4117 if (REG_MULTI)
4118 return one->end.pos.lnum == two->end.pos.lnum
4119 && one->end.pos.col == two->end.pos.col;
4120 return one->end.ptr == two->end.ptr;
4121}
4122
4123/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004124 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
4125 */
4126 static int
4127match_follows(startstate, depth)
4128 nfa_state_T *startstate;
4129 int depth;
4130{
4131 nfa_state_T *state = startstate;
4132
4133 /* avoid too much recursion */
4134 if (depth > 10)
4135 return FALSE;
4136
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004137 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004138 {
4139 switch (state->c)
4140 {
4141 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004142 case NFA_MCLOSE:
4143 case NFA_END_INVISIBLE:
4144 case NFA_END_INVISIBLE_NEG:
4145 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004146 return TRUE;
4147
4148 case NFA_SPLIT:
4149 return match_follows(state->out, depth + 1)
4150 || match_follows(state->out1, depth + 1);
4151
4152 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004153 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004154 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004155 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004156 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004157 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004158 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02004159 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004160 case NFA_COMPOSING:
4161 /* skip ahead to next state */
4162 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02004163 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004164
4165 case NFA_ANY:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004166 case NFA_ANY_COMPOSING:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004167 case NFA_IDENT:
4168 case NFA_SIDENT:
4169 case NFA_KWORD:
4170 case NFA_SKWORD:
4171 case NFA_FNAME:
4172 case NFA_SFNAME:
4173 case NFA_PRINT:
4174 case NFA_SPRINT:
4175 case NFA_WHITE:
4176 case NFA_NWHITE:
4177 case NFA_DIGIT:
4178 case NFA_NDIGIT:
4179 case NFA_HEX:
4180 case NFA_NHEX:
4181 case NFA_OCTAL:
4182 case NFA_NOCTAL:
4183 case NFA_WORD:
4184 case NFA_NWORD:
4185 case NFA_HEAD:
4186 case NFA_NHEAD:
4187 case NFA_ALPHA:
4188 case NFA_NALPHA:
4189 case NFA_LOWER:
4190 case NFA_NLOWER:
4191 case NFA_UPPER:
4192 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02004193 case NFA_LOWER_IC:
4194 case NFA_NLOWER_IC:
4195 case NFA_UPPER_IC:
4196 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02004197 case NFA_START_COLL:
4198 case NFA_START_NEG_COLL:
4199 case NFA_NEWL:
4200 /* state will advance input */
4201 return FALSE;
4202
4203 default:
4204 if (state->c > 0)
4205 /* state will advance input */
4206 return FALSE;
4207
4208 /* Others: zero-width or possibly zero-width, might still find
4209 * a match at the same position, keep looking. */
4210 break;
4211 }
4212 state = state->out;
4213 }
4214 return FALSE;
4215}
4216
4217
4218/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02004219 * Return TRUE if "state" is already in list "l".
4220 */
4221 static int
4222state_in_list(l, state, subs)
4223 nfa_list_T *l; /* runtime state list */
4224 nfa_state_T *state; /* state to update */
4225 regsubs_T *subs; /* pointers to subexpressions */
4226{
4227 if (state->lastlist[nfa_ll_index] == l->id)
4228 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02004229 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004230 return TRUE;
4231 }
4232 return FALSE;
4233}
4234
Bram Moolenaard05bf562013-06-30 23:24:08 +02004235/*
4236 * Add "state" and possibly what follows to state list ".".
4237 * Returns "subs_arg", possibly copied into temp_subs.
4238 */
4239
4240 static regsubs_T *
4241addstate(l, state, subs_arg, pim, off)
4242 nfa_list_T *l; /* runtime state list */
4243 nfa_state_T *state; /* state to update */
4244 regsubs_T *subs_arg; /* pointers to subexpressions */
4245 nfa_pim_T *pim; /* postponed look-behind match */
4246 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004247{
Bram Moolenaar963fee22013-05-26 21:47:28 +02004248 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004249 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004250 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004251 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02004252 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004253 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004254 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02004255 regsubs_T *subs = subs_arg;
4256 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004257#ifdef ENABLE_LOG
4258 int did_print = FALSE;
4259#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004260
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004261 switch (state->c)
4262 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004263 case NFA_NCLOSE:
4264 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004265 case NFA_MCLOSE1:
4266 case NFA_MCLOSE2:
4267 case NFA_MCLOSE3:
4268 case NFA_MCLOSE4:
4269 case NFA_MCLOSE5:
4270 case NFA_MCLOSE6:
4271 case NFA_MCLOSE7:
4272 case NFA_MCLOSE8:
4273 case NFA_MCLOSE9:
4274#ifdef FEAT_SYN_HL
4275 case NFA_ZCLOSE:
4276 case NFA_ZCLOSE1:
4277 case NFA_ZCLOSE2:
4278 case NFA_ZCLOSE3:
4279 case NFA_ZCLOSE4:
4280 case NFA_ZCLOSE5:
4281 case NFA_ZCLOSE6:
4282 case NFA_ZCLOSE7:
4283 case NFA_ZCLOSE8:
4284 case NFA_ZCLOSE9:
4285#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004286 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004287 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004288 case NFA_SPLIT:
Bram Moolenaar699c1202013-09-25 16:41:54 +02004289 case NFA_EMPTY:
Bram Moolenaar5714b802013-05-28 22:03:20 +02004290 /* These nodes are not added themselves but their "out" and/or
4291 * "out1" may be added below. */
4292 break;
4293
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004294 case NFA_BOL:
4295 case NFA_BOF:
4296 /* "^" won't match past end-of-line, don't bother trying.
4297 * Except when at the end of the line, or when we are going to the
4298 * next line for a look-behind match. */
4299 if (reginput > regline
4300 && *reginput != NUL
4301 && (nfa_endp == NULL
4302 || !REG_MULTI
4303 || reglnum == nfa_endp->se_u.pos.lnum))
4304 goto skip_add;
4305 /* FALLTHROUGH */
4306
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004307 case NFA_MOPEN1:
4308 case NFA_MOPEN2:
4309 case NFA_MOPEN3:
4310 case NFA_MOPEN4:
4311 case NFA_MOPEN5:
4312 case NFA_MOPEN6:
4313 case NFA_MOPEN7:
4314 case NFA_MOPEN8:
4315 case NFA_MOPEN9:
4316#ifdef FEAT_SYN_HL
4317 case NFA_ZOPEN:
4318 case NFA_ZOPEN1:
4319 case NFA_ZOPEN2:
4320 case NFA_ZOPEN3:
4321 case NFA_ZOPEN4:
4322 case NFA_ZOPEN5:
4323 case NFA_ZOPEN6:
4324 case NFA_ZOPEN7:
4325 case NFA_ZOPEN8:
4326 case NFA_ZOPEN9:
4327#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004328 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004329 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004330 /* These nodes need to be added so that we can bail out when it
4331 * was added to this list before at the same position to avoid an
4332 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004333
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004334 default:
Bram Moolenaar272fb582013-11-21 16:03:40 +01004335 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004336 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004337 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004338 * unless it is an MOPEN that is used for a backreference or
Bram Moolenaar9c235062014-05-13 16:44:29 +02004339 * when there is a PIM. For NFA_MATCH check the position,
4340 * lower position is preferred. */
4341 if (!nfa_has_backref && pim == NULL && !l->has_pim
4342 && state->c != NFA_MATCH)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004343 {
4344skip_add:
4345#ifdef ENABLE_LOG
4346 nfa_set_code(state->c);
4347 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4348 abs(state->id), l->id, state->c, code);
4349#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004350 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004351 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004352
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004353 /* Do not add the state again when it exists with the same
4354 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004355 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004356 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004357 }
4358
Bram Moolenaara0169122013-06-26 18:16:58 +02004359 /* When there are backreferences or PIMs the number of states may
4360 * be (a lot) bigger than anticipated. */
4361 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004362 {
4363 int newlen = l->len * 3 / 2 + 50;
4364
Bram Moolenaard05bf562013-06-30 23:24:08 +02004365 if (subs != &temp_subs)
4366 {
4367 /* "subs" may point into the current array, need to make a
4368 * copy before it becomes invalid. */
4369 copy_sub(&temp_subs.norm, &subs->norm);
4370#ifdef FEAT_SYN_HL
4371 if (nfa_has_zsubexpr)
4372 copy_sub(&temp_subs.synt, &subs->synt);
4373#endif
4374 subs = &temp_subs;
4375 }
4376
Bram Moolenaar428e9872013-05-30 17:05:39 +02004377 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4378 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004379 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004380
4381 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004382 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004383 thread = &l->t[l->n++];
4384 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004385 if (pim == NULL)
4386 thread->pim.result = NFA_PIM_UNUSED;
4387 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004388 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004389 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004390 l->has_pim = TRUE;
4391 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004392 copy_sub(&thread->subs.norm, &subs->norm);
4393#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004394 if (nfa_has_zsubexpr)
4395 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004396#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004397#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004398 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004399 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004400#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004401 }
4402
4403#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004404 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004405 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004406#endif
4407 switch (state->c)
4408 {
4409 case NFA_MATCH:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004410 break;
4411
4412 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004413 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004414 subs = addstate(l, state->out, subs, pim, off);
4415 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004416 break;
4417
Bram Moolenaar699c1202013-09-25 16:41:54 +02004418 case NFA_EMPTY:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004419 case NFA_NOPEN:
4420 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004421 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004422 break;
4423
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004424 case NFA_MOPEN:
4425 case NFA_MOPEN1:
4426 case NFA_MOPEN2:
4427 case NFA_MOPEN3:
4428 case NFA_MOPEN4:
4429 case NFA_MOPEN5:
4430 case NFA_MOPEN6:
4431 case NFA_MOPEN7:
4432 case NFA_MOPEN8:
4433 case NFA_MOPEN9:
4434#ifdef FEAT_SYN_HL
4435 case NFA_ZOPEN:
4436 case NFA_ZOPEN1:
4437 case NFA_ZOPEN2:
4438 case NFA_ZOPEN3:
4439 case NFA_ZOPEN4:
4440 case NFA_ZOPEN5:
4441 case NFA_ZOPEN6:
4442 case NFA_ZOPEN7:
4443 case NFA_ZOPEN8:
4444 case NFA_ZOPEN9:
4445#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004446 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004447 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004448 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004449 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004450 sub = &subs->norm;
4451 }
4452#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004453 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004454 {
4455 subidx = state->c - NFA_ZOPEN;
4456 sub = &subs->synt;
4457 }
4458#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004459 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004460 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004461 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004462 sub = &subs->norm;
4463 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004464
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004465 /* avoid compiler warnings */
4466 save_ptr = NULL;
4467 save_lpos.lnum = 0;
4468 save_lpos.col = 0;
4469
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004470 /* Set the position (with "off" added) in the subexpression. Save
4471 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004472 if (REG_MULTI)
4473 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004474 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004475 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004476 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004477 save_in_use = -1;
4478 }
4479 else
4480 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004481 save_in_use = sub->in_use;
4482 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004483 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004484 sub->list.multi[i].start.lnum = -1;
4485 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004486 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004487 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004488 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004489 if (off == -1)
4490 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004491 sub->list.multi[subidx].start.lnum = reglnum + 1;
4492 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004493 }
4494 else
4495 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004496 sub->list.multi[subidx].start.lnum = reglnum;
4497 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004498 (colnr_T)(reginput - regline + off);
4499 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004500 }
4501 else
4502 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004503 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004504 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004505 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004506 save_in_use = -1;
4507 }
4508 else
4509 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004510 save_in_use = sub->in_use;
4511 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004512 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004513 sub->list.line[i].start = NULL;
4514 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004515 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004516 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004517 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004518 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004519 }
4520
Bram Moolenaard05bf562013-06-30 23:24:08 +02004521 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004522 /* "subs" may have changed, need to set "sub" again */
4523#ifdef FEAT_SYN_HL
4524 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4525 sub = &subs->synt;
4526 else
4527#endif
4528 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004529
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004530 if (save_in_use == -1)
4531 {
4532 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004533 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004534 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004535 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004536 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004537 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004538 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004539 break;
4540
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004541 case NFA_MCLOSE:
Bram Moolenaar9be44812013-09-05 21:15:44 +02004542 if (nfa_has_zend && (REG_MULTI
4543 ? subs->norm.list.multi[0].end.lnum >= 0
4544 : subs->norm.list.line[0].end != NULL))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004545 {
Bram Moolenaar9be44812013-09-05 21:15:44 +02004546 /* Do not overwrite the position set by \ze. */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004547 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004548 break;
4549 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004550 case NFA_MCLOSE1:
4551 case NFA_MCLOSE2:
4552 case NFA_MCLOSE3:
4553 case NFA_MCLOSE4:
4554 case NFA_MCLOSE5:
4555 case NFA_MCLOSE6:
4556 case NFA_MCLOSE7:
4557 case NFA_MCLOSE8:
4558 case NFA_MCLOSE9:
4559#ifdef FEAT_SYN_HL
4560 case NFA_ZCLOSE:
4561 case NFA_ZCLOSE1:
4562 case NFA_ZCLOSE2:
4563 case NFA_ZCLOSE3:
4564 case NFA_ZCLOSE4:
4565 case NFA_ZCLOSE5:
4566 case NFA_ZCLOSE6:
4567 case NFA_ZCLOSE7:
4568 case NFA_ZCLOSE8:
4569 case NFA_ZCLOSE9:
4570#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004571 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004572 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004573 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004574 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004575 sub = &subs->norm;
4576 }
4577#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004578 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004579 {
4580 subidx = state->c - NFA_ZCLOSE;
4581 sub = &subs->synt;
4582 }
4583#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004584 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004585 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004586 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004587 sub = &subs->norm;
4588 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004589
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004590 /* We don't fill in gaps here, there must have been an MOPEN that
4591 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004592 save_in_use = sub->in_use;
4593 if (sub->in_use <= subidx)
4594 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004595 if (REG_MULTI)
4596 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004597 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004598 if (off == -1)
4599 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004600 sub->list.multi[subidx].end.lnum = reglnum + 1;
4601 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004602 }
4603 else
4604 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004605 sub->list.multi[subidx].end.lnum = reglnum;
4606 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004607 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004608 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004609 /* avoid compiler warnings */
4610 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004611 }
4612 else
4613 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004614 save_ptr = sub->list.line[subidx].end;
4615 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004616 /* avoid compiler warnings */
4617 save_lpos.lnum = 0;
4618 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004619 }
4620
Bram Moolenaard05bf562013-06-30 23:24:08 +02004621 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004622 /* "subs" may have changed, need to set "sub" again */
4623#ifdef FEAT_SYN_HL
4624 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4625 sub = &subs->synt;
4626 else
4627#endif
4628 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004629
4630 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004631 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004632 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004633 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004634 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004635 break;
4636 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004637 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004638}
4639
4640/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004641 * Like addstate(), but the new state(s) are put at position "*ip".
4642 * Used for zero-width matches, next state to use is the added one.
4643 * This makes sure the order of states to be tried does not change, which
4644 * matters for alternatives.
4645 */
4646 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004647addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004648 nfa_list_T *l; /* runtime state list */
4649 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004650 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004651 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004652 int *ip;
4653{
4654 int tlen = l->n;
4655 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004656 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004657
4658 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004659 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004660
Bram Moolenaar4b417062013-05-25 20:19:50 +02004661 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004662 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004663 return;
4664
4665 /* re-order to put the new state at the current position */
4666 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004667 if (count == 0)
4668 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004669 if (count == 1)
4670 {
4671 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004672 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004673 }
4674 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004675 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004676 if (l->n + count - 1 >= l->len)
4677 {
4678 /* not enough space to move the new states, reallocate the list
4679 * and move the states to the right position */
4680 nfa_thread_T *newl;
4681
4682 l->len = l->len * 3 / 2 + 50;
4683 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4684 if (newl == NULL)
4685 return;
4686 mch_memmove(&(newl[0]),
4687 &(l->t[0]),
4688 sizeof(nfa_thread_T) * listidx);
4689 mch_memmove(&(newl[listidx]),
4690 &(l->t[l->n - count]),
4691 sizeof(nfa_thread_T) * count);
4692 mch_memmove(&(newl[listidx + count]),
4693 &(l->t[listidx + 1]),
4694 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4695 vim_free(l->t);
4696 l->t = newl;
4697 }
4698 else
4699 {
4700 /* make space for new states, then move them from the
4701 * end to the current position */
4702 mch_memmove(&(l->t[listidx + count]),
4703 &(l->t[listidx + 1]),
4704 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4705 mch_memmove(&(l->t[listidx]),
4706 &(l->t[l->n - 1]),
4707 sizeof(nfa_thread_T) * count);
4708 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004709 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004710 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004711 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004712}
4713
4714/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004715 * Check character class "class" against current character c.
4716 */
4717 static int
4718check_char_class(class, c)
4719 int class;
4720 int c;
4721{
4722 switch (class)
4723 {
4724 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004725 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004726 return OK;
4727 break;
4728 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004729 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004730 return OK;
4731 break;
4732 case NFA_CLASS_BLANK:
4733 if (c == ' ' || c == '\t')
4734 return OK;
4735 break;
4736 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004737 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004738 return OK;
4739 break;
4740 case NFA_CLASS_DIGIT:
4741 if (VIM_ISDIGIT(c))
4742 return OK;
4743 break;
4744 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004745 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004746 return OK;
4747 break;
4748 case NFA_CLASS_LOWER:
4749 if (MB_ISLOWER(c))
4750 return OK;
4751 break;
4752 case NFA_CLASS_PRINT:
4753 if (vim_isprintc(c))
4754 return OK;
4755 break;
4756 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004757 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004758 return OK;
4759 break;
4760 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004761 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004762 return OK;
4763 break;
4764 case NFA_CLASS_UPPER:
4765 if (MB_ISUPPER(c))
4766 return OK;
4767 break;
4768 case NFA_CLASS_XDIGIT:
4769 if (vim_isxdigit(c))
4770 return OK;
4771 break;
4772 case NFA_CLASS_TAB:
4773 if (c == '\t')
4774 return OK;
4775 break;
4776 case NFA_CLASS_RETURN:
4777 if (c == '\r')
4778 return OK;
4779 break;
4780 case NFA_CLASS_BACKSPACE:
4781 if (c == '\b')
4782 return OK;
4783 break;
4784 case NFA_CLASS_ESCAPE:
4785 if (c == '\033')
4786 return OK;
4787 break;
4788
4789 default:
4790 /* should not be here :P */
Bram Moolenaar174a8482013-11-28 14:20:17 +01004791 EMSGN(_(e_ill_char_class), class);
Bram Moolenaar417bad22013-06-07 14:08:30 +02004792 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004793 }
4794 return FAIL;
4795}
4796
Bram Moolenaar5714b802013-05-28 22:03:20 +02004797/*
4798 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004799 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004800 */
4801 static int
4802match_backref(sub, subidx, bytelen)
4803 regsub_T *sub; /* pointers to subexpressions */
4804 int subidx;
4805 int *bytelen; /* out: length of match in bytes */
4806{
4807 int len;
4808
4809 if (sub->in_use <= subidx)
4810 {
4811retempty:
4812 /* backref was not set, match an empty string */
4813 *bytelen = 0;
4814 return TRUE;
4815 }
4816
4817 if (REG_MULTI)
4818 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004819 if (sub->list.multi[subidx].start.lnum < 0
4820 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004821 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004822 if (sub->list.multi[subidx].start.lnum == reglnum
4823 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004824 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004825 len = sub->list.multi[subidx].end.col
4826 - sub->list.multi[subidx].start.col;
4827 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4828 reginput, &len) == 0)
4829 {
4830 *bytelen = len;
4831 return TRUE;
4832 }
4833 }
4834 else
4835 {
4836 if (match_with_backref(
4837 sub->list.multi[subidx].start.lnum,
4838 sub->list.multi[subidx].start.col,
4839 sub->list.multi[subidx].end.lnum,
4840 sub->list.multi[subidx].end.col,
4841 bytelen) == RA_MATCH)
4842 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004843 }
4844 }
4845 else
4846 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004847 if (sub->list.line[subidx].start == NULL
4848 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004849 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004850 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4851 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004852 {
4853 *bytelen = len;
4854 return TRUE;
4855 }
4856 }
4857 return FALSE;
4858}
4859
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004860#ifdef FEAT_SYN_HL
4861
4862static int match_zref __ARGS((int subidx, int *bytelen));
4863
4864/*
4865 * Check for a match with \z subexpression "subidx".
4866 * Return TRUE if it matches.
4867 */
4868 static int
4869match_zref(subidx, bytelen)
4870 int subidx;
4871 int *bytelen; /* out: length of match in bytes */
4872{
4873 int len;
4874
4875 cleanup_zsubexpr();
4876 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4877 {
4878 /* backref was not set, match an empty string */
4879 *bytelen = 0;
4880 return TRUE;
4881 }
4882
4883 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4884 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4885 {
4886 *bytelen = len;
4887 return TRUE;
4888 }
4889 return FALSE;
4890}
4891#endif
4892
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004893/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004894 * Save list IDs for all NFA states of "prog" into "list".
4895 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004896 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004897 */
4898 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004899nfa_save_listids(prog, list)
4900 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004901 int *list;
4902{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004903 int i;
4904 nfa_state_T *p;
4905
4906 /* Order in the list is reverse, it's a bit faster that way. */
4907 p = &prog->state[0];
4908 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004909 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004910 list[i] = p->lastlist[1];
4911 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004912 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004913 }
4914}
4915
4916/*
4917 * Restore list IDs from "list" to all NFA states.
4918 */
4919 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004920nfa_restore_listids(prog, list)
4921 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004922 int *list;
4923{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004924 int i;
4925 nfa_state_T *p;
4926
4927 p = &prog->state[0];
4928 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004929 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004930 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004931 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004932 }
4933}
4934
Bram Moolenaar423532e2013-05-29 21:14:42 +02004935 static int
4936nfa_re_num_cmp(val, op, pos)
4937 long_u val;
4938 int op;
4939 long_u pos;
4940{
4941 if (op == 1) return pos > val;
4942 if (op == 2) return pos < val;
4943 return val == pos;
4944}
4945
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004946static 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 +02004947static 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 +02004948
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004949/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004950 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004951 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4952 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004953 */
4954 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004955recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004956 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004957 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004958 nfa_regprog_T *prog;
4959 regsubs_T *submatch;
4960 regsubs_T *m;
4961 int **listids;
4962{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004963 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004964 int save_reglnum = reglnum;
4965 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004966 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004967 save_se_T *save_nfa_endp = nfa_endp;
4968 save_se_T endpos;
4969 save_se_T *endposp = NULL;
4970 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004971 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004972
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004973 if (pim != NULL)
4974 {
4975 /* start at the position where the postponed match was */
4976 if (REG_MULTI)
4977 reginput = regline + pim->end.pos.col;
4978 else
4979 reginput = pim->end.ptr;
4980 }
4981
Bram Moolenaardecd9542013-06-07 16:31:50 +02004982 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004983 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4984 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4985 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004986 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004987 /* The recursive match must end at the current position. When "pim" is
4988 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004989 endposp = &endpos;
4990 if (REG_MULTI)
4991 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004992 if (pim == NULL)
4993 {
4994 endpos.se_u.pos.col = (int)(reginput - regline);
4995 endpos.se_u.pos.lnum = reglnum;
4996 }
4997 else
4998 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004999 }
5000 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005001 {
5002 if (pim == NULL)
5003 endpos.se_u.ptr = reginput;
5004 else
5005 endpos.se_u.ptr = pim->end.ptr;
5006 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005007
5008 /* Go back the specified number of bytes, or as far as the
5009 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02005010 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005011 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005012 if (state->val <= 0)
5013 {
5014 if (REG_MULTI)
5015 {
5016 regline = reg_getline(--reglnum);
5017 if (regline == NULL)
5018 /* can't go before the first line */
5019 regline = reg_getline(++reglnum);
5020 }
5021 reginput = regline;
5022 }
5023 else
5024 {
5025 if (REG_MULTI && (int)(reginput - regline) < state->val)
5026 {
5027 /* Not enough bytes in this line, go to end of
5028 * previous line. */
5029 regline = reg_getline(--reglnum);
5030 if (regline == NULL)
5031 {
5032 /* can't go before the first line */
5033 regline = reg_getline(++reglnum);
5034 reginput = regline;
5035 }
5036 else
5037 reginput = regline + STRLEN(regline);
5038 }
5039 if ((int)(reginput - regline) >= state->val)
5040 {
5041 reginput -= state->val;
5042#ifdef FEAT_MBYTE
5043 if (has_mbyte)
5044 reginput -= mb_head_off(regline, reginput);
5045#endif
5046 }
5047 else
5048 reginput = regline;
5049 }
5050 }
5051
Bram Moolenaarf46da702013-06-02 22:37:42 +02005052#ifdef ENABLE_LOG
5053 if (log_fd != stderr)
5054 fclose(log_fd);
5055 log_fd = NULL;
5056#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005057 /* Have to clear the lastlist field of the NFA nodes, so that
5058 * nfa_regmatch() and addstate() can run properly after recursion. */
5059 if (nfa_ll_index == 1)
5060 {
5061 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
5062 * values and clear them. */
5063 if (*listids == NULL)
5064 {
5065 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
5066 if (*listids == NULL)
5067 {
5068 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
5069 return 0;
5070 }
5071 }
5072 nfa_save_listids(prog, *listids);
5073 need_restore = TRUE;
5074 /* any value of nfa_listid will do */
5075 }
5076 else
5077 {
5078 /* First recursive nfa_regmatch() call, switch to the second lastlist
5079 * entry. Make sure nfa_listid is different from a previous recursive
5080 * call, because some states may still have this ID. */
5081 ++nfa_ll_index;
5082 if (nfa_listid <= nfa_alt_listid)
5083 nfa_listid = nfa_alt_listid;
5084 }
5085
5086 /* Call nfa_regmatch() to check if the current concat matches at this
5087 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005088 nfa_endp = endposp;
5089 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005090
5091 if (need_restore)
5092 nfa_restore_listids(prog, *listids);
5093 else
5094 {
5095 --nfa_ll_index;
5096 nfa_alt_listid = nfa_listid;
5097 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005098
5099 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005100 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02005101 if (REG_MULTI)
5102 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02005103 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005104 nfa_match = save_nfa_match;
5105 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005106 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02005107
5108#ifdef ENABLE_LOG
5109 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
5110 if (log_fd != NULL)
5111 {
5112 fprintf(log_fd, "****************************\n");
5113 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
5114 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
5115 fprintf(log_fd, "****************************\n");
5116 }
5117 else
5118 {
5119 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5120 log_fd = stderr;
5121 }
5122#endif
5123
5124 return result;
5125}
5126
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005127static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02005128static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02005129
5130/*
5131 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005132 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02005133 * NFA_ANY: 1
5134 * specific character: 99
5135 */
5136 static int
5137failure_chance(state, depth)
5138 nfa_state_T *state;
5139 int depth;
5140{
5141 int c = state->c;
5142 int l, r;
5143
5144 /* detect looping */
5145 if (depth > 4)
5146 return 1;
5147
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005148 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005149 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005150 case NFA_SPLIT:
5151 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
5152 /* avoid recursive stuff */
5153 return 1;
5154 /* two alternatives, use the lowest failure chance */
5155 l = failure_chance(state->out, depth + 1);
5156 r = failure_chance(state->out1, depth + 1);
5157 return l < r ? l : r;
5158
5159 case NFA_ANY:
5160 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005161 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005162
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005163 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005164 case NFA_MCLOSE:
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005165 case NFA_ANY_COMPOSING:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005166 /* empty match works always */
5167 return 0;
5168
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005169 case NFA_START_INVISIBLE:
5170 case NFA_START_INVISIBLE_FIRST:
5171 case NFA_START_INVISIBLE_NEG:
5172 case NFA_START_INVISIBLE_NEG_FIRST:
5173 case NFA_START_INVISIBLE_BEFORE:
5174 case NFA_START_INVISIBLE_BEFORE_FIRST:
5175 case NFA_START_INVISIBLE_BEFORE_NEG:
5176 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
5177 case NFA_START_PATTERN:
5178 /* recursive regmatch is expensive, use low failure chance */
5179 return 5;
5180
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005181 case NFA_BOL:
5182 case NFA_EOL:
5183 case NFA_BOF:
5184 case NFA_EOF:
5185 case NFA_NEWL:
5186 return 99;
5187
5188 case NFA_BOW:
5189 case NFA_EOW:
5190 return 90;
5191
5192 case NFA_MOPEN:
5193 case NFA_MOPEN1:
5194 case NFA_MOPEN2:
5195 case NFA_MOPEN3:
5196 case NFA_MOPEN4:
5197 case NFA_MOPEN5:
5198 case NFA_MOPEN6:
5199 case NFA_MOPEN7:
5200 case NFA_MOPEN8:
5201 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005202#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005203 case NFA_ZOPEN:
5204 case NFA_ZOPEN1:
5205 case NFA_ZOPEN2:
5206 case NFA_ZOPEN3:
5207 case NFA_ZOPEN4:
5208 case NFA_ZOPEN5:
5209 case NFA_ZOPEN6:
5210 case NFA_ZOPEN7:
5211 case NFA_ZOPEN8:
5212 case NFA_ZOPEN9:
5213 case NFA_ZCLOSE:
5214 case NFA_ZCLOSE1:
5215 case NFA_ZCLOSE2:
5216 case NFA_ZCLOSE3:
5217 case NFA_ZCLOSE4:
5218 case NFA_ZCLOSE5:
5219 case NFA_ZCLOSE6:
5220 case NFA_ZCLOSE7:
5221 case NFA_ZCLOSE8:
5222 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005223#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005224 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005225 case NFA_MCLOSE1:
5226 case NFA_MCLOSE2:
5227 case NFA_MCLOSE3:
5228 case NFA_MCLOSE4:
5229 case NFA_MCLOSE5:
5230 case NFA_MCLOSE6:
5231 case NFA_MCLOSE7:
5232 case NFA_MCLOSE8:
5233 case NFA_MCLOSE9:
5234 case NFA_NCLOSE:
5235 return failure_chance(state->out, depth + 1);
5236
5237 case NFA_BACKREF1:
5238 case NFA_BACKREF2:
5239 case NFA_BACKREF3:
5240 case NFA_BACKREF4:
5241 case NFA_BACKREF5:
5242 case NFA_BACKREF6:
5243 case NFA_BACKREF7:
5244 case NFA_BACKREF8:
5245 case NFA_BACKREF9:
5246#ifdef FEAT_SYN_HL
5247 case NFA_ZREF1:
5248 case NFA_ZREF2:
5249 case NFA_ZREF3:
5250 case NFA_ZREF4:
5251 case NFA_ZREF5:
5252 case NFA_ZREF6:
5253 case NFA_ZREF7:
5254 case NFA_ZREF8:
5255 case NFA_ZREF9:
5256#endif
5257 /* backreferences don't match in many places */
5258 return 94;
5259
5260 case NFA_LNUM_GT:
5261 case NFA_LNUM_LT:
5262 case NFA_COL_GT:
5263 case NFA_COL_LT:
5264 case NFA_VCOL_GT:
5265 case NFA_VCOL_LT:
5266 case NFA_MARK_GT:
5267 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005268 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02005269 /* before/after positions don't match very often */
5270 return 85;
5271
5272 case NFA_LNUM:
5273 return 90;
5274
5275 case NFA_CURSOR:
5276 case NFA_COL:
5277 case NFA_VCOL:
5278 case NFA_MARK:
5279 /* specific positions rarely match */
5280 return 98;
5281
5282 case NFA_COMPOSING:
5283 return 95;
5284
5285 default:
5286 if (c > 0)
5287 /* character match fails often */
5288 return 95;
5289 }
5290
5291 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005292 return 50;
5293}
5294
Bram Moolenaarf46da702013-06-02 22:37:42 +02005295/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02005296 * Skip until the char "c" we know a match must start with.
5297 */
5298 static int
5299skip_to_start(c, colp)
5300 int c;
5301 colnr_T *colp;
5302{
5303 char_u *s;
5304
5305 /* Used often, do some work to avoid call overhead. */
5306 if (!ireg_ic
5307#ifdef FEAT_MBYTE
5308 && !has_mbyte
5309#endif
5310 )
5311 s = vim_strbyte(regline + *colp, c);
5312 else
5313 s = cstrchr(regline + *colp, c);
5314 if (s == NULL)
5315 return FAIL;
5316 *colp = (int)(s - regline);
5317 return OK;
5318}
5319
5320/*
Bram Moolenaar473de612013-06-08 18:19:48 +02005321 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02005322 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02005323 * Returns zero for no match, 1 for a match.
5324 */
5325 static long
5326find_match_text(startcol, regstart, match_text)
5327 colnr_T startcol;
5328 int regstart;
5329 char_u *match_text;
5330{
5331 colnr_T col = startcol;
5332 int c1, c2;
5333 int len1, len2;
5334 int match;
5335
5336 for (;;)
5337 {
5338 match = TRUE;
5339 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5340 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5341 {
5342 c1 = PTR2CHAR(match_text + len1);
5343 c2 = PTR2CHAR(regline + col + len2);
5344 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5345 {
5346 match = FALSE;
5347 break;
5348 }
5349 len2 += MB_CHAR2LEN(c2);
5350 }
5351 if (match
5352#ifdef FEAT_MBYTE
5353 /* check that no composing char follows */
5354 && !(enc_utf8
5355 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5356#endif
5357 )
5358 {
5359 cleanup_subexpr();
5360 if (REG_MULTI)
5361 {
5362 reg_startpos[0].lnum = reglnum;
5363 reg_startpos[0].col = col;
5364 reg_endpos[0].lnum = reglnum;
5365 reg_endpos[0].col = col + len2;
5366 }
5367 else
5368 {
5369 reg_startp[0] = regline + col;
5370 reg_endp[0] = regline + col + len2;
5371 }
5372 return 1L;
5373 }
5374
5375 /* Try finding regstart after the current match. */
5376 col += MB_CHAR2LEN(regstart); /* skip regstart */
5377 if (skip_to_start(regstart, &col) == FAIL)
5378 break;
5379 }
5380 return 0L;
5381}
5382
5383/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005384 * Main matching routine.
5385 *
5386 * Run NFA to determine whether it matches reginput.
5387 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005388 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005389 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005390 * Return TRUE if there is a match, FALSE otherwise.
Bram Moolenaarf2118842013-09-25 18:16:38 +02005391 * When there is a match "submatch" contains the positions.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005392 * Note: Caller must ensure that: start != NULL.
5393 */
5394 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005395nfa_regmatch(prog, start, submatch, m)
5396 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005397 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005398 regsubs_T *submatch;
5399 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005400{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005401 int result;
5402 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005403 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005404 int go_to_nextline = FALSE;
5405 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005406 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005407 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005408 nfa_list_T *thislist;
5409 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005410 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005411 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005412 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005413 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005414 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005415 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005416#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005417 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005418
5419 if (debug == NULL)
5420 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005421 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005422 return FALSE;
5423 }
5424#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005425 /* Some patterns may take a long time to match, especially when using
5426 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5427 fast_breakcheck();
5428 if (got_int)
5429 return FALSE;
5430
Bram Moolenaar963fee22013-05-26 21:47:28 +02005431 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005432
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005433 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005434 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005435 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005436 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005437 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005438 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005439 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005440 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005441
5442#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005443 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005444 if (log_fd != NULL)
5445 {
5446 fprintf(log_fd, "**********************************\n");
5447 nfa_set_code(start->c);
5448 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5449 abs(start->id), code);
5450 fprintf(log_fd, "**********************************\n");
5451 }
5452 else
5453 {
5454 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5455 log_fd = stderr;
5456 }
5457#endif
5458
5459 thislist = &list[0];
5460 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005461 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005462 nextlist = &list[1];
5463 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005464 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005465#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005466 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005467#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005468 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005469
5470 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5471 * it's the first MOPEN. */
5472 if (toplevel)
5473 {
5474 if (REG_MULTI)
5475 {
5476 m->norm.list.multi[0].start.lnum = reglnum;
5477 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5478 }
5479 else
5480 m->norm.list.line[0].start = reginput;
5481 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005482 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005483 }
5484 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005485 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005486
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005487#define ADD_STATE_IF_MATCH(state) \
5488 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005489 add_state = state->out; \
5490 add_off = clen; \
5491 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005492
5493 /*
5494 * Run for each character.
5495 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005496 for (;;)
5497 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005498 int curc;
5499 int clen;
5500
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005501#ifdef FEAT_MBYTE
5502 if (has_mbyte)
5503 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005504 curc = (*mb_ptr2char)(reginput);
5505 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005506 }
5507 else
5508#endif
5509 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005510 curc = *reginput;
5511 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005513 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005514 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005515 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005516 go_to_nextline = FALSE;
5517 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005518
5519 /* swap lists */
5520 thislist = &list[flag];
5521 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005522 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005523 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005524 ++nfa_listid;
Bram Moolenaarfda37292014-11-05 14:27:36 +01005525 if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES)
5526 {
5527 /* too many states, retry with old engine */
5528 nfa_match = NFA_TOO_EXPENSIVE;
5529 goto theend;
5530 }
5531
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005532 thislist->id = nfa_listid;
5533 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005534
5535#ifdef ENABLE_LOG
5536 fprintf(log_fd, "------------------------------------------\n");
5537 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005538 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005539 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005540 {
5541 int i;
5542
5543 for (i = 0; i < thislist->n; i++)
5544 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5545 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005546 fprintf(log_fd, "\n");
5547#endif
5548
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005549#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550 fprintf(debug, "\n-------------------\n");
5551#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005552 /*
5553 * If the state lists are empty we can stop.
5554 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005555 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005556 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005557
5558 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005559 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005560 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005561 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005562
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005563#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564 nfa_set_code(t->state->c);
5565 fprintf(debug, "%s, ", code);
5566#endif
5567#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005568 {
5569 int col;
5570
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005571 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005572 col = -1;
5573 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005574 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005575 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005576 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005577 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005578 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5579 abs(t->state->id), (int)t->state->c, code, col,
5580 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005581 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005582#endif
5583
5584 /*
5585 * Handle the possible codes of the current state.
5586 * The most important is NFA_MATCH.
5587 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005588 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005589 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005590 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005591 switch (t->state->c)
5592 {
5593 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005594 {
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02005595#ifdef FEAT_MBYTE
5596 /* If the match ends before a composing characters and
5597 * ireg_icombine is not set, that is not really a match. */
5598 if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc))
5599 break;
5600#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005601 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005602 copy_sub(&submatch->norm, &t->subs.norm);
5603#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005604 if (nfa_has_zsubexpr)
5605 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005606#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005607#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005608 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005609#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005610 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005611 * states at this position. When the list of states is going
5612 * to be empty quit without advancing, so that "reginput" is
5613 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005614 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005615 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005616 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005617 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005618
5619 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005620 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005621 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005622 /*
5623 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005624 * NFA_START_INVISIBLE_BEFORE node.
5625 * They surround a zero-width group, used with "\@=", "\&",
5626 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005627 * If we got here, it means that the current "invisible" group
5628 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005629 * nfa_regmatch(). For a look-behind match only when it ends
5630 * in the position in "nfa_endp".
5631 * Submatches are stored in *m, and used in the parent call.
5632 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005633#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005634 if (nfa_endp != NULL)
5635 {
5636 if (REG_MULTI)
5637 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5638 (int)reglnum,
5639 (int)nfa_endp->se_u.pos.lnum,
5640 (int)(reginput - regline),
5641 nfa_endp->se_u.pos.col);
5642 else
5643 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5644 (int)(reginput - regline),
5645 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005646 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005647#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005648 /* If "nfa_endp" is set it's only a match if it ends at
5649 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005650 if (nfa_endp != NULL && (REG_MULTI
5651 ? (reglnum != nfa_endp->se_u.pos.lnum
5652 || (int)(reginput - regline)
5653 != nfa_endp->se_u.pos.col)
5654 : reginput != nfa_endp->se_u.ptr))
5655 break;
5656
5657 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005658 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005659 {
5660 copy_sub(&m->norm, &t->subs.norm);
5661#ifdef FEAT_SYN_HL
5662 if (nfa_has_zsubexpr)
5663 copy_sub(&m->synt, &t->subs.synt);
5664#endif
5665 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005666#ifdef ENABLE_LOG
5667 fprintf(log_fd, "Match found:\n");
5668 log_subsexpr(m);
5669#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005670 nfa_match = TRUE;
Bram Moolenaar78c93e42013-09-05 16:05:36 +02005671 /* See comment above at "goto nextchar". */
5672 if (nextlist->n == 0)
5673 clen = 0;
5674 goto nextchar;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005675
5676 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005677 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005678 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005679 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005680 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005681 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005682 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005683 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005684 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005685#ifdef ENABLE_LOG
5686 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5687 failure_chance(t->state->out, 0),
5688 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005689#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005690 /* Do it directly if there already is a PIM or when
5691 * nfa_postprocess() detected it will work better. */
5692 if (t->pim.result != NFA_PIM_UNUSED
5693 || t->state->c == NFA_START_INVISIBLE_FIRST
5694 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5695 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5696 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005697 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005698 int in_use = m->norm.in_use;
5699
Bram Moolenaar699c1202013-09-25 16:41:54 +02005700 /* Copy submatch info for the recursive call, opposite
5701 * of what happens on success below. */
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005702 copy_sub_off(&m->norm, &t->subs.norm);
Bram Moolenaar699c1202013-09-25 16:41:54 +02005703#ifdef FEAT_SYN_HL
5704 if (nfa_has_zsubexpr)
5705 copy_sub_off(&m->synt, &t->subs.synt);
5706#endif
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005707
Bram Moolenaara2d95102013-06-04 14:23:05 +02005708 /*
5709 * First try matching the invisible match, then what
5710 * follows.
5711 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005712 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005713 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005714 if (result == NFA_TOO_EXPENSIVE)
5715 {
5716 nfa_match = result;
5717 goto theend;
5718 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02005719
Bram Moolenaardecd9542013-06-07 16:31:50 +02005720 /* for \@! and \@<! it is a match when the result is
5721 * FALSE */
5722 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005723 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5724 || t->state->c
5725 == NFA_START_INVISIBLE_BEFORE_NEG
5726 || t->state->c
5727 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005728 {
5729 /* Copy submatch info from the recursive call */
5730 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005731#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005732 if (nfa_has_zsubexpr)
5733 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005734#endif
Bram Moolenaarf2118842013-09-25 18:16:38 +02005735 /* If the pattern has \ze and it matched in the
5736 * sub pattern, use it. */
5737 copy_ze_off(&t->subs.norm, &m->norm);
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005738
Bram Moolenaara2d95102013-06-04 14:23:05 +02005739 /* t->state->out1 is the corresponding
5740 * END_INVISIBLE node; Add its out to the current
5741 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005742 add_here = TRUE;
5743 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005744 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005745 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005746 }
5747 else
5748 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005749 nfa_pim_T pim;
5750
Bram Moolenaara2d95102013-06-04 14:23:05 +02005751 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005752 * First try matching what follows. Only if a match
5753 * is found verify the invisible match matches. Add a
5754 * nfa_pim_T to the following states, it contains info
5755 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005756 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005757 pim.state = t->state;
5758 pim.result = NFA_PIM_TODO;
5759 pim.subs.norm.in_use = 0;
5760#ifdef FEAT_SYN_HL
5761 pim.subs.synt.in_use = 0;
5762#endif
5763 if (REG_MULTI)
5764 {
5765 pim.end.pos.col = (int)(reginput - regline);
5766 pim.end.pos.lnum = reglnum;
5767 }
5768 else
5769 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005770
5771 /* t->state->out1 is the corresponding END_INVISIBLE
5772 * node; Add its out to the current list (zero-width
5773 * match). */
5774 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005775 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005776 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005777 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005778 break;
5779
Bram Moolenaar87953742013-06-05 18:52:40 +02005780 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005781 {
5782 nfa_state_T *skip = NULL;
5783#ifdef ENABLE_LOG
5784 int skip_lid = 0;
5785#endif
5786
5787 /* There is no point in trying to match the pattern if the
5788 * output state is not going to be added to the list. */
5789 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5790 {
5791 skip = t->state->out1->out;
5792#ifdef ENABLE_LOG
5793 skip_lid = nextlist->id;
5794#endif
5795 }
5796 else if (state_in_list(nextlist,
5797 t->state->out1->out->out, &t->subs))
5798 {
5799 skip = t->state->out1->out->out;
5800#ifdef ENABLE_LOG
5801 skip_lid = nextlist->id;
5802#endif
5803 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005804 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005805 t->state->out1->out->out, &t->subs))
5806 {
5807 skip = t->state->out1->out->out;
5808#ifdef ENABLE_LOG
5809 skip_lid = thislist->id;
5810#endif
5811 }
5812 if (skip != NULL)
5813 {
5814#ifdef ENABLE_LOG
5815 nfa_set_code(skip->c);
5816 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5817 abs(skip->id), skip_lid, skip->c, code);
5818#endif
5819 break;
5820 }
Bram Moolenaar699c1202013-09-25 16:41:54 +02005821 /* Copy submatch info to the recursive call, opposite of what
5822 * happens afterwards. */
5823 copy_sub_off(&m->norm, &t->subs.norm);
5824#ifdef FEAT_SYN_HL
5825 if (nfa_has_zsubexpr)
5826 copy_sub_off(&m->synt, &t->subs.synt);
5827#endif
Bram Moolenaar43e02982013-06-07 17:31:29 +02005828
Bram Moolenaar87953742013-06-05 18:52:40 +02005829 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005830 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005831 submatch, m, &listids);
Bram Moolenaarfda37292014-11-05 14:27:36 +01005832 if (result == NFA_TOO_EXPENSIVE)
5833 {
5834 nfa_match = result;
5835 goto theend;
5836 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005837 if (result)
5838 {
5839 int bytelen;
5840
5841#ifdef ENABLE_LOG
5842 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5843 log_subsexpr(m);
5844#endif
5845 /* Copy submatch info from the recursive call */
5846 copy_sub_off(&t->subs.norm, &m->norm);
5847#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005848 if (nfa_has_zsubexpr)
5849 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005850#endif
5851 /* Now we need to skip over the matched text and then
5852 * continue with what follows. */
5853 if (REG_MULTI)
5854 /* TODO: multi-line match */
5855 bytelen = m->norm.list.multi[0].end.col
5856 - (int)(reginput - regline);
5857 else
5858 bytelen = (int)(m->norm.list.line[0].end - reginput);
5859
5860#ifdef ENABLE_LOG
5861 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5862#endif
5863 if (bytelen == 0)
5864 {
5865 /* empty match, output of corresponding
5866 * NFA_END_PATTERN/NFA_SKIP to be used at current
5867 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005868 add_here = TRUE;
5869 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005870 }
5871 else if (bytelen <= clen)
5872 {
5873 /* match current character, output of corresponding
5874 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005875 add_state = t->state->out1->out->out;
5876 add_off = clen;
5877 }
5878 else
5879 {
5880 /* skip over the matched characters, set character
5881 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005882 add_state = t->state->out1->out;
5883 add_off = bytelen;
5884 add_count = bytelen - clen;
5885 }
5886 }
5887 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005888 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005889
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005890 case NFA_BOL:
5891 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005892 {
5893 add_here = TRUE;
5894 add_state = t->state->out;
5895 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005896 break;
5897
5898 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005899 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005900 {
5901 add_here = TRUE;
5902 add_state = t->state->out;
5903 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005904 break;
5905
5906 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005907 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005908
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005909 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005910 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005911#ifdef FEAT_MBYTE
5912 else if (has_mbyte)
5913 {
5914 int this_class;
5915
5916 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005917 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005918 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005919 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005920 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005921 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005922 }
5923#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005924 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005925 || (reginput > regline
5926 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005927 result = FALSE;
5928 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005929 {
5930 add_here = TRUE;
5931 add_state = t->state->out;
5932 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005933 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005934
5935 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005936 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005937 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005938 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005939#ifdef FEAT_MBYTE
5940 else if (has_mbyte)
5941 {
5942 int this_class, prev_class;
5943
5944 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005945 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005946 prev_class = reg_prev_class();
5947 if (this_class == prev_class
5948 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005949 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005950 }
5951#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005952 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005953 || (reginput[0] != NUL
5954 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005955 result = FALSE;
5956 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005957 {
5958 add_here = TRUE;
5959 add_state = t->state->out;
5960 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005961 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005962
Bram Moolenaar4b780632013-05-31 22:14:52 +02005963 case NFA_BOF:
5964 if (reglnum == 0 && reginput == regline
5965 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005966 {
5967 add_here = TRUE;
5968 add_state = t->state->out;
5969 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005970 break;
5971
5972 case NFA_EOF:
5973 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005974 {
5975 add_here = TRUE;
5976 add_state = t->state->out;
5977 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005978 break;
5979
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005980#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005981 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005982 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005983 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005984 int len = 0;
5985 nfa_state_T *end;
5986 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005987 int cchars[MAX_MCO];
5988 int ccount = 0;
5989 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005990
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005991 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005992 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005993 if (utf_iscomposing(sta->c))
5994 {
5995 /* Only match composing character(s), ignore base
5996 * character. Used for ".{composing}" and "{composing}"
5997 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005998 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005999 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02006000 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006001 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02006002 /* If \Z was present, then ignore composing characters.
6003 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006004 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006005 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006006 else
6007 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006008 while (sta->c != NFA_END_COMPOSING)
6009 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006010 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006011
6012 /* Check base character matches first, unless ignored. */
6013 else if (len > 0 || mc == sta->c)
6014 {
6015 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006016 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02006017 len += mb_char2len(mc);
6018 sta = sta->out;
6019 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006020
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006021 /* We don't care about the order of composing characters.
6022 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006023 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006024 {
6025 mc = mb_ptr2char(reginput + len);
6026 cchars[ccount++] = mc;
6027 len += mb_char2len(mc);
6028 if (ccount == MAX_MCO)
6029 break;
6030 }
6031
6032 /* Check that each composing char in the pattern matches a
6033 * composing char in the text. We do not check if all
6034 * composing chars are matched. */
6035 result = OK;
6036 while (sta->c != NFA_END_COMPOSING)
6037 {
6038 for (j = 0; j < ccount; ++j)
6039 if (cchars[j] == sta->c)
6040 break;
6041 if (j == ccount)
6042 {
6043 result = FAIL;
6044 break;
6045 }
6046 sta = sta->out;
6047 }
6048 }
6049 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02006050 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02006051
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006052 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006053 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006054 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006055 }
6056#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006057
6058 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006059 if (curc == NUL && !reg_line_lbr && REG_MULTI
6060 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006061 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02006062 go_to_nextline = TRUE;
6063 /* Pass -1 for the offset, which means taking the position
6064 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006065 add_state = t->state->out;
6066 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006067 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006068 else if (curc == '\n' && reg_line_lbr)
6069 {
6070 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006071 add_state = t->state->out;
6072 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006073 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006074 break;
6075
Bram Moolenaar417bad22013-06-07 14:08:30 +02006076 case NFA_START_COLL:
6077 case NFA_START_NEG_COLL:
6078 {
6079 /* What follows is a list of characters, until NFA_END_COLL.
6080 * One of them must match or none of them must match. */
6081 nfa_state_T *state;
6082 int result_if_matched;
6083 int c1, c2;
6084
6085 /* Never match EOL. If it's part of the collection it is added
6086 * as a separate state with an OR. */
6087 if (curc == NUL)
6088 break;
6089
6090 state = t->state->out;
6091 result_if_matched = (t->state->c == NFA_START_COLL);
6092 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006093 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02006094 if (state->c == NFA_END_COLL)
6095 {
6096 result = !result_if_matched;
6097 break;
6098 }
6099 if (state->c == NFA_RANGE_MIN)
6100 {
6101 c1 = state->val;
6102 state = state->out; /* advance to NFA_RANGE_MAX */
6103 c2 = state->val;
6104#ifdef ENABLE_LOG
6105 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
6106 curc, c1, c2);
6107#endif
6108 if (curc >= c1 && curc <= c2)
6109 {
6110 result = result_if_matched;
6111 break;
6112 }
6113 if (ireg_ic)
6114 {
6115 int curc_low = MB_TOLOWER(curc);
6116 int done = FALSE;
6117
6118 for ( ; c1 <= c2; ++c1)
6119 if (MB_TOLOWER(c1) == curc_low)
6120 {
6121 result = result_if_matched;
6122 done = TRUE;
6123 break;
6124 }
6125 if (done)
6126 break;
6127 }
6128 }
6129 else if (state->c < 0 ? check_char_class(state->c, curc)
6130 : (curc == state->c
6131 || (ireg_ic && MB_TOLOWER(curc)
6132 == MB_TOLOWER(state->c))))
6133 {
6134 result = result_if_matched;
6135 break;
6136 }
6137 state = state->out;
6138 }
6139 if (result)
6140 {
6141 /* next state is in out of the NFA_END_COLL, out1 of
6142 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02006143 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006144 add_off = clen;
6145 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006146 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02006147 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006148
6149 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006150 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006151 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006152 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02006153 add_state = t->state->out;
6154 add_off = clen;
6155 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006156 break;
6157
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006158 case NFA_ANY_COMPOSING:
6159 /* On a composing character skip over it. Otherwise do
6160 * nothing. Always matches. */
6161#ifdef FEAT_MBYTE
6162 if (enc_utf8 && utf_iscomposing(curc))
6163 {
6164 add_off = clen;
6165 }
6166 else
6167#endif
6168 {
6169 add_here = TRUE;
6170 add_off = 0;
6171 }
6172 add_state = t->state->out;
6173 break;
6174
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006175 /*
6176 * Character classes like \a for alpha, \d for digit etc.
6177 */
6178 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006179 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006180 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006181 break;
6182
6183 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006184 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006185 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006186 break;
6187
6188 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006189 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006190 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006191 break;
6192
6193 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006194 result = !VIM_ISDIGIT(curc)
6195 && vim_iswordp_buf(reginput, reg_buf);
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_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006200 result = vim_isfilec(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_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006205 result = !VIM_ISDIGIT(curc) && vim_isfilec(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_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006210 result = vim_isprintc(PTR2CHAR(reginput));
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_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02006215 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
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_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006220 result = vim_iswhite(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_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006225 result = curc != NUL && !vim_iswhite(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_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006230 result = ri_digit(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_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006235 result = curc != NUL && !ri_digit(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_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006240 result = ri_hex(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_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006245 result = curc != NUL && !ri_hex(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_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006250 result = ri_octal(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_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006255 result = curc != NUL && !ri_octal(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_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006260 result = ri_word(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_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006265 result = curc != NUL && !ri_word(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_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006270 result = ri_head(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_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006275 result = curc != NUL && !ri_head(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_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006280 result = ri_alpha(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_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006285 result = curc != NUL && !ri_alpha(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
6289 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006290 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006291 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006292 break;
6293
6294 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006295 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006296 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006297 break;
6298
6299 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006300 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006301 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006302 break;
6303
6304 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006305 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006306 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006307 break;
6308
Bram Moolenaar1cfad522013-08-14 12:06:49 +02006309 case NFA_LOWER_IC: /* [a-z] */
6310 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
6311 ADD_STATE_IF_MATCH(t->state);
6312 break;
6313
6314 case NFA_NLOWER_IC: /* [^a-z] */
6315 result = curc != NUL
6316 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
6317 ADD_STATE_IF_MATCH(t->state);
6318 break;
6319
6320 case NFA_UPPER_IC: /* [A-Z] */
6321 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
6322 ADD_STATE_IF_MATCH(t->state);
6323 break;
6324
6325 case NFA_NUPPER_IC: /* ^[A-Z] */
6326 result = curc != NUL
6327 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
6328 ADD_STATE_IF_MATCH(t->state);
6329 break;
6330
Bram Moolenaar5714b802013-05-28 22:03:20 +02006331 case NFA_BACKREF1:
6332 case NFA_BACKREF2:
6333 case NFA_BACKREF3:
6334 case NFA_BACKREF4:
6335 case NFA_BACKREF5:
6336 case NFA_BACKREF6:
6337 case NFA_BACKREF7:
6338 case NFA_BACKREF8:
6339 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006340#ifdef FEAT_SYN_HL
6341 case NFA_ZREF1:
6342 case NFA_ZREF2:
6343 case NFA_ZREF3:
6344 case NFA_ZREF4:
6345 case NFA_ZREF5:
6346 case NFA_ZREF6:
6347 case NFA_ZREF7:
6348 case NFA_ZREF8:
6349 case NFA_ZREF9:
6350#endif
6351 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006352 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006353 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006354 int bytelen;
6355
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006356 if (t->state->c <= NFA_BACKREF9)
6357 {
6358 subidx = t->state->c - NFA_BACKREF1 + 1;
6359 result = match_backref(&t->subs.norm, subidx, &bytelen);
6360 }
6361#ifdef FEAT_SYN_HL
6362 else
6363 {
6364 subidx = t->state->c - NFA_ZREF1 + 1;
6365 result = match_zref(subidx, &bytelen);
6366 }
6367#endif
6368
Bram Moolenaar5714b802013-05-28 22:03:20 +02006369 if (result)
6370 {
6371 if (bytelen == 0)
6372 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02006373 /* empty match always works, output of NFA_SKIP to be
6374 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006375 add_here = TRUE;
6376 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006377 }
6378 else if (bytelen <= clen)
6379 {
6380 /* match current character, jump ahead to out of
6381 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006382 add_state = t->state->out->out;
6383 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006384 }
6385 else
6386 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02006387 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02006388 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006389 add_state = t->state->out;
6390 add_off = bytelen;
6391 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006392 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006393 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006394 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006395 }
6396 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006397 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006398 if (t->count - clen <= 0)
6399 {
6400 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006401 add_state = t->state->out;
6402 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006403 }
6404 else
6405 {
6406 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006407 add_state = t->state;
6408 add_off = 0;
6409 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006410 }
6411 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006412
Bram Moolenaar423532e2013-05-29 21:14:42 +02006413 case NFA_LNUM:
6414 case NFA_LNUM_GT:
6415 case NFA_LNUM_LT:
6416 result = (REG_MULTI &&
6417 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6418 (long_u)(reglnum + reg_firstlnum)));
6419 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006420 {
6421 add_here = TRUE;
6422 add_state = t->state->out;
6423 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006424 break;
6425
6426 case NFA_COL:
6427 case NFA_COL_GT:
6428 case NFA_COL_LT:
6429 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6430 (long_u)(reginput - regline) + 1);
6431 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006432 {
6433 add_here = TRUE;
6434 add_state = t->state->out;
6435 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006436 break;
6437
6438 case NFA_VCOL:
6439 case NFA_VCOL_GT:
6440 case NFA_VCOL_LT:
6441 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
6442 (long_u)win_linetabsize(
6443 reg_win == NULL ? curwin : reg_win,
6444 regline, (colnr_T)(reginput - regline)) + 1);
6445 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006446 {
6447 add_here = TRUE;
6448 add_state = t->state->out;
6449 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006450 break;
6451
Bram Moolenaar044aa292013-06-04 21:27:38 +02006452 case NFA_MARK:
6453 case NFA_MARK_GT:
6454 case NFA_MARK_LT:
6455 {
6456 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6457
6458 /* Compare the mark position to the match position. */
6459 result = (pos != NULL /* mark doesn't exist */
6460 && pos->lnum > 0 /* mark isn't set in reg_buf */
6461 && (pos->lnum == reglnum + reg_firstlnum
6462 ? (pos->col == (colnr_T)(reginput - regline)
6463 ? t->state->c == NFA_MARK
6464 : (pos->col < (colnr_T)(reginput - regline)
6465 ? t->state->c == NFA_MARK_GT
6466 : t->state->c == NFA_MARK_LT))
6467 : (pos->lnum < reglnum + reg_firstlnum
6468 ? t->state->c == NFA_MARK_GT
6469 : t->state->c == NFA_MARK_LT)));
6470 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006471 {
6472 add_here = TRUE;
6473 add_state = t->state->out;
6474 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006475 break;
6476 }
6477
Bram Moolenaar423532e2013-05-29 21:14:42 +02006478 case NFA_CURSOR:
6479 result = (reg_win != NULL
6480 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6481 && ((colnr_T)(reginput - regline)
6482 == reg_win->w_cursor.col));
6483 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006484 {
6485 add_here = TRUE;
6486 add_state = t->state->out;
6487 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006488 break;
6489
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006490 case NFA_VISUAL:
6491 result = reg_match_visual();
6492 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006493 {
6494 add_here = TRUE;
6495 add_state = t->state->out;
6496 }
Bram Moolenaar973fced2013-06-05 21:10:59 +02006497 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006498
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006499 case NFA_MOPEN1:
6500 case NFA_MOPEN2:
6501 case NFA_MOPEN3:
6502 case NFA_MOPEN4:
6503 case NFA_MOPEN5:
6504 case NFA_MOPEN6:
6505 case NFA_MOPEN7:
6506 case NFA_MOPEN8:
6507 case NFA_MOPEN9:
6508#ifdef FEAT_SYN_HL
6509 case NFA_ZOPEN:
6510 case NFA_ZOPEN1:
6511 case NFA_ZOPEN2:
6512 case NFA_ZOPEN3:
6513 case NFA_ZOPEN4:
6514 case NFA_ZOPEN5:
6515 case NFA_ZOPEN6:
6516 case NFA_ZOPEN7:
6517 case NFA_ZOPEN8:
6518 case NFA_ZOPEN9:
6519#endif
6520 case NFA_NOPEN:
6521 case NFA_ZSTART:
6522 /* These states are only added to be able to bail out when
6523 * they are added again, nothing is to be done. */
6524 break;
6525
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006526 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006527 {
6528 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006529
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006530#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006531 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006532 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006533#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006534 result = (c == curc);
6535
6536 if (!result && ireg_ic)
6537 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006538#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02006539 /* If ireg_icombine is not set only skip over the character
6540 * itself. When it is set skip over composing characters. */
6541 if (result && enc_utf8 && !ireg_icombine)
6542 clen = utf_char2len(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006543#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006544 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006545 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006546 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006547
6548 } /* switch (t->state->c) */
6549
6550 if (add_state != NULL)
6551 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006552 nfa_pim_T *pim;
Bram Moolenaara951e352013-10-06 15:46:11 +02006553 nfa_pim_T pim_copy;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006554
6555 if (t->pim.result == NFA_PIM_UNUSED)
6556 pim = NULL;
6557 else
6558 pim = &t->pim;
6559
6560 /* Handle the postponed invisible match if the match might end
6561 * without advancing and before the end of the line. */
6562 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006563 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006564 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006565 {
6566#ifdef ENABLE_LOG
6567 fprintf(log_fd, "\n");
6568 fprintf(log_fd, "==================================\n");
6569 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6570 fprintf(log_fd, "\n");
6571#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006572 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006573 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006574 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006575 /* for \@! and \@<! it is a match when the result is
6576 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006577 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006578 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6579 || pim->state->c
6580 == NFA_START_INVISIBLE_BEFORE_NEG
6581 || pim->state->c
6582 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006583 {
6584 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006585 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006586#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006587 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006588 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006589#endif
6590 }
6591 }
6592 else
6593 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006594 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006595#ifdef ENABLE_LOG
6596 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006597 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006598 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6599 fprintf(log_fd, "\n");
6600#endif
6601 }
6602
Bram Moolenaardecd9542013-06-07 16:31:50 +02006603 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006604 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006605 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6606 || pim->state->c
6607 == NFA_START_INVISIBLE_BEFORE_NEG
6608 || pim->state->c
6609 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006610 {
6611 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006612 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006613#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006614 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006615 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006616#endif
6617 }
6618 else
6619 /* look-behind match failed, don't add the state */
6620 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006621
6622 /* Postponed invisible match was handled, don't add it to
6623 * following states. */
6624 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006625 }
6626
Bram Moolenaara951e352013-10-06 15:46:11 +02006627 /* If "pim" points into l->t it will become invalid when
6628 * adding the state causes the list to be reallocated. Make a
6629 * local copy to avoid that. */
6630 if (pim == &t->pim)
6631 {
6632 copy_pim(&pim_copy, pim);
6633 pim = &pim_copy;
6634 }
6635
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006636 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006637 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006638 else
6639 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006640 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006641 if (add_count > 0)
6642 nextlist->t[nextlist->n - 1].count = add_count;
6643 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006644 }
6645
6646 } /* for (thislist = thislist; thislist->state; thislist++) */
6647
Bram Moolenaare23febd2013-05-26 18:40:14 +02006648 /* Look for the start of a match in the current position by adding the
6649 * start state to the list of states.
6650 * The first found match is the leftmost one, thus the order of states
6651 * matters!
6652 * Do not add the start state in recursive calls of nfa_regmatch(),
6653 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006654 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006655 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006656 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006657 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006658 && reglnum == 0
6659 && clen != 0
6660 && (ireg_maxcol == 0
6661 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006662 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006663 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006664 ? (reglnum < nfa_endp->se_u.pos.lnum
6665 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006666 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006667 < nfa_endp->se_u.pos.col))
6668 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006669 {
6670#ifdef ENABLE_LOG
6671 fprintf(log_fd, "(---) STARTSTATE\n");
6672#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006673 /* Inline optimized code for addstate() if we know the state is
6674 * the first MOPEN. */
6675 if (toplevel)
6676 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006677 int add = TRUE;
6678 int c;
6679
6680 if (prog->regstart != NUL && clen != 0)
6681 {
6682 if (nextlist->n == 0)
6683 {
6684 colnr_T col = (colnr_T)(reginput - regline) + clen;
6685
6686 /* Nextlist is empty, we can skip ahead to the
6687 * character that must appear at the start. */
6688 if (skip_to_start(prog->regstart, &col) == FAIL)
6689 break;
6690#ifdef ENABLE_LOG
6691 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6692 col - ((colnr_T)(reginput - regline) + clen));
6693#endif
6694 reginput = regline + col - clen;
6695 }
6696 else
6697 {
6698 /* Checking if the required start character matches is
6699 * cheaper than adding a state that won't match. */
6700 c = PTR2CHAR(reginput + clen);
6701 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6702 != MB_TOLOWER(prog->regstart)))
6703 {
6704#ifdef ENABLE_LOG
6705 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6706#endif
6707 add = FALSE;
6708 }
6709 }
6710 }
6711
6712 if (add)
6713 {
6714 if (REG_MULTI)
6715 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006716 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006717 else
6718 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006719 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006720 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006721 }
6722 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006723 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006724 }
6725
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006726#ifdef ENABLE_LOG
6727 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006728 {
6729 int i;
6730
6731 for (i = 0; i < thislist->n; i++)
6732 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6733 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006734 fprintf(log_fd, "\n");
6735#endif
6736
6737nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006738 /* Advance to the next character, or advance to the next line, or
6739 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006740 if (clen != 0)
6741 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006742 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6743 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006744 reg_nextline();
6745 else
6746 break;
6747 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006748
6749#ifdef ENABLE_LOG
6750 if (log_fd != stderr)
6751 fclose(log_fd);
6752 log_fd = NULL;
6753#endif
6754
6755theend:
6756 /* Free memory */
6757 vim_free(list[0].t);
6758 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006759 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006760#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006761#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006762 fclose(debug);
6763#endif
6764
Bram Moolenaar963fee22013-05-26 21:47:28 +02006765 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006766}
6767
6768/*
6769 * Try match of "prog" with at regline["col"].
Bram Moolenaar8c731502014-11-23 15:57:49 +01006770 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006771 */
6772 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006773nfa_regtry(prog, col)
6774 nfa_regprog_T *prog;
6775 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006776{
6777 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006778 regsubs_T subs, m;
6779 nfa_state_T *start = prog->start;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006780 int result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006781#ifdef ENABLE_LOG
6782 FILE *f;
6783#endif
6784
6785 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006786
6787#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006788 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006789 if (f != NULL)
6790 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006791 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006792#ifdef DEBUG
6793 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6794#endif
6795 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006796 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006797 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006798 fprintf(f, "\n\n");
6799 fclose(f);
6800 }
6801 else
6802 EMSG(_("Could not open temporary log file for writing "));
6803#endif
6804
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006805 clear_sub(&subs.norm);
6806 clear_sub(&m.norm);
6807#ifdef FEAT_SYN_HL
6808 clear_sub(&subs.synt);
6809 clear_sub(&m.synt);
6810#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006811
Bram Moolenaarfda37292014-11-05 14:27:36 +01006812 result = nfa_regmatch(prog, start, &subs, &m);
6813 if (result == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006814 return 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01006815 else if (result == NFA_TOO_EXPENSIVE)
6816 return result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006817
6818 cleanup_subexpr();
6819 if (REG_MULTI)
6820 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006821 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006822 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006823 reg_startpos[i] = subs.norm.list.multi[i].start;
6824 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006825 }
6826
6827 if (reg_startpos[0].lnum < 0)
6828 {
6829 reg_startpos[0].lnum = 0;
6830 reg_startpos[0].col = col;
6831 }
6832 if (reg_endpos[0].lnum < 0)
6833 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006834 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006835 reg_endpos[0].lnum = reglnum;
6836 reg_endpos[0].col = (int)(reginput - regline);
6837 }
6838 else
6839 /* Use line number of "\ze". */
6840 reglnum = reg_endpos[0].lnum;
6841 }
6842 else
6843 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006844 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006845 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006846 reg_startp[i] = subs.norm.list.line[i].start;
6847 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006848 }
6849
6850 if (reg_startp[0] == NULL)
6851 reg_startp[0] = regline + col;
6852 if (reg_endp[0] == NULL)
6853 reg_endp[0] = reginput;
6854 }
6855
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006856#ifdef FEAT_SYN_HL
6857 /* Package any found \z(...\) matches for export. Default is none. */
6858 unref_extmatch(re_extmatch_out);
6859 re_extmatch_out = NULL;
6860
6861 if (prog->reghasz == REX_SET)
6862 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006863 cleanup_zsubexpr();
6864 re_extmatch_out = make_extmatch();
6865 for (i = 0; i < subs.synt.in_use; i++)
6866 {
6867 if (REG_MULTI)
6868 {
6869 struct multipos *mpos = &subs.synt.list.multi[i];
6870
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02006871 /* Only accept single line matches that are valid. */
6872 if (mpos->start.lnum >= 0
6873 && mpos->start.lnum == mpos->end.lnum
6874 && mpos->end.col >= mpos->start.col)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006875 re_extmatch_out->matches[i] =
6876 vim_strnsave(reg_getline(mpos->start.lnum)
6877 + mpos->start.col,
6878 mpos->end.col - mpos->start.col);
6879 }
6880 else
6881 {
6882 struct linepos *lpos = &subs.synt.list.line[i];
6883
6884 if (lpos->start != NULL && lpos->end != NULL)
6885 re_extmatch_out->matches[i] =
6886 vim_strnsave(lpos->start,
6887 (int)(lpos->end - lpos->start));
6888 }
6889 }
6890 }
6891#endif
6892
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006893 return 1 + reglnum;
6894}
6895
6896/*
6897 * Match a regexp against a string ("line" points to the string) or multiple
6898 * lines ("line" is NULL, use reg_getline()).
6899 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01006900 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006901 */
6902 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006903nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006904 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006905 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006906{
6907 nfa_regprog_T *prog;
6908 long retval = 0L;
6909 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006910 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006911
6912 if (REG_MULTI)
6913 {
6914 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6915 line = reg_getline((linenr_T)0); /* relative to the cursor */
6916 reg_startpos = reg_mmatch->startpos;
6917 reg_endpos = reg_mmatch->endpos;
6918 }
6919 else
6920 {
6921 prog = (nfa_regprog_T *)reg_match->regprog;
6922 reg_startp = reg_match->startp;
6923 reg_endp = reg_match->endp;
6924 }
6925
6926 /* Be paranoid... */
6927 if (prog == NULL || line == NULL)
6928 {
6929 EMSG(_(e_null));
6930 goto theend;
6931 }
6932
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006933 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6934 if (prog->regflags & RF_ICASE)
6935 ireg_ic = TRUE;
6936 else if (prog->regflags & RF_NOICASE)
6937 ireg_ic = FALSE;
6938
6939#ifdef FEAT_MBYTE
6940 /* If pattern contains "\Z" overrule value of ireg_icombine */
6941 if (prog->regflags & RF_ICOMBINE)
6942 ireg_icombine = TRUE;
6943#endif
6944
6945 regline = line;
6946 reglnum = 0; /* relative to line */
6947
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006948 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006949 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006950 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006951 nfa_listid = 1;
6952 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006953 nfa_regengine.expr = prog->pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006954
Bram Moolenaard89616e2013-06-06 18:46:06 +02006955 if (prog->reganch && col > 0)
6956 return 0L;
6957
Bram Moolenaar473de612013-06-08 18:19:48 +02006958 need_clear_subexpr = TRUE;
6959#ifdef FEAT_SYN_HL
6960 /* Clear the external match subpointers if necessary. */
6961 if (prog->reghasz == REX_SET)
6962 {
6963 nfa_has_zsubexpr = TRUE;
6964 need_clear_zsubexpr = TRUE;
6965 }
6966 else
6967 nfa_has_zsubexpr = FALSE;
6968#endif
6969
Bram Moolenaard89616e2013-06-06 18:46:06 +02006970 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006971 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006972 /* Skip ahead until a character we know the match must start with.
6973 * When there is none there is no match. */
6974 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006975 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006976
Bram Moolenaar473de612013-06-08 18:19:48 +02006977 /* If match_text is set it contains the full text that must match.
6978 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006979 if (prog->match_text != NULL
6980#ifdef FEAT_MBYTE
6981 && !ireg_icombine
6982#endif
6983 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006984 return find_match_text(col, prog->regstart, prog->match_text);
6985 }
6986
Bram Moolenaard89616e2013-06-06 18:46:06 +02006987 /* If the start column is past the maximum column: no need to try. */
6988 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6989 goto theend;
6990
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006991 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006992 for (i = 0; i < nstate; ++i)
6993 {
6994 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006995 prog->state[i].lastlist[0] = 0;
6996 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006997 }
6998
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006999 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007000
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007001 nfa_regengine.expr = NULL;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007002
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007003theend:
7004 return retval;
7005}
7006
7007/*
7008 * Compile a regular expression into internal code for the NFA matcher.
7009 * Returns the program in allocated space. Returns NULL for an error.
7010 */
7011 static regprog_T *
7012nfa_regcomp(expr, re_flags)
7013 char_u *expr;
7014 int re_flags;
7015{
Bram Moolenaaraae48832013-05-25 21:18:34 +02007016 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02007017 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007018 int *postfix;
7019
7020 if (expr == NULL)
7021 return NULL;
7022
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007023 nfa_regengine.expr = expr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007024
7025 init_class_tab();
7026
7027 if (nfa_regcomp_start(expr, re_flags) == FAIL)
7028 return NULL;
7029
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007030 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02007031 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007032 postfix = re2post();
7033 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007034 {
7035 /* TODO: only give this error for debugging? */
7036 if (post_ptr >= post_end)
7037 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007038 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02007039 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007040
7041 /*
7042 * In order to build the NFA, we parse the input regexp twice:
7043 * 1. first pass to count size (so we can allocate space)
7044 * 2. second to emit code
7045 */
7046#ifdef ENABLE_LOG
7047 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02007048 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007049
7050 if (f != NULL)
7051 {
7052 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
7053 fclose(f);
7054 }
7055 }
7056#endif
7057
7058 /*
7059 * PASS 1
7060 * Count number of NFA states in "nstate". Do not build the NFA.
7061 */
7062 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007063
Bram Moolenaar16619a22013-06-11 18:42:36 +02007064 /* allocate the regprog with space for the compiled regexp */
7065 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02007066 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
7067 if (prog == NULL)
7068 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007069 state_ptr = prog->state;
7070
7071 /*
7072 * PASS 2
7073 * Build the NFA
7074 */
7075 prog->start = post2nfa(postfix, post_ptr, FALSE);
7076 if (prog->start == NULL)
7077 goto fail;
7078
7079 prog->regflags = regflags;
7080 prog->engine = &nfa_regengine;
7081 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02007082 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02007083 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02007084 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02007085
Bram Moolenaara2947e22013-06-11 22:44:09 +02007086 nfa_postprocess(prog);
7087
Bram Moolenaard89616e2013-06-06 18:46:06 +02007088 prog->reganch = nfa_get_reganch(prog->start, 0);
7089 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02007090 prog->match_text = nfa_get_match_text(prog->start);
7091
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007092#ifdef ENABLE_LOG
7093 nfa_postfix_dump(expr, OK);
7094 nfa_dump(prog);
7095#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02007096#ifdef FEAT_SYN_HL
7097 /* Remember whether this pattern has any \z specials in it. */
7098 prog->reghasz = re_has_z;
7099#endif
Bram Moolenaar473de612013-06-08 18:19:48 +02007100 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02007101 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007102
7103out:
7104 vim_free(post_start);
7105 post_start = post_ptr = post_end = NULL;
7106 state_ptr = NULL;
7107 return (regprog_T *)prog;
7108
7109fail:
7110 vim_free(prog);
7111 prog = NULL;
7112#ifdef ENABLE_LOG
7113 nfa_postfix_dump(expr, FAIL);
7114#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007115 nfa_regengine.expr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007116 goto out;
7117}
7118
Bram Moolenaar473de612013-06-08 18:19:48 +02007119/*
7120 * Free a compiled regexp program, returned by nfa_regcomp().
7121 */
7122 static void
7123nfa_regfree(prog)
7124 regprog_T *prog;
7125{
7126 if (prog != NULL)
7127 {
7128 vim_free(((nfa_regprog_T *)prog)->match_text);
Bram Moolenaar473de612013-06-08 18:19:48 +02007129 vim_free(((nfa_regprog_T *)prog)->pattern);
Bram Moolenaar473de612013-06-08 18:19:48 +02007130 vim_free(prog);
7131 }
7132}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007133
7134/*
7135 * Match a regexp against a string.
7136 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
7137 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007138 * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007139 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007140 * Returns <= 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007141 */
7142 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007143nfa_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007144 regmatch_T *rmp;
7145 char_u *line; /* string to match against */
7146 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007147 int line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007148{
7149 reg_match = rmp;
7150 reg_mmatch = NULL;
7151 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02007152 reg_line_lbr = line_lbr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007153 reg_buf = curbuf;
7154 reg_win = NULL;
7155 ireg_ic = rmp->rm_ic;
7156#ifdef FEAT_MBYTE
7157 ireg_icombine = FALSE;
7158#endif
7159 ireg_maxcol = 0;
Bram Moolenaar8c731502014-11-23 15:57:49 +01007160 return nfa_regexec_both(line, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007161}
7162
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007163
7164/*
7165 * Match a regexp against multiple lines.
7166 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
7167 * Uses curbuf for line count and 'iskeyword'.
7168 *
Bram Moolenaar8c731502014-11-23 15:57:49 +01007169 * Return <= 0 if there is no match. Return number of lines contained in the
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007170 * match otherwise.
7171 *
7172 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
7173 *
7174 * ! Also NOTE : match may actually be in another line. e.g.:
7175 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
7176 *
7177 * +-------------------------+
7178 * |a |
7179 * |b |
7180 * |c |
7181 * | |
7182 * +-------------------------+
7183 *
7184 * then nfa_regexec_multi() returns 3. while the original
7185 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
7186 *
7187 * FIXME if this behavior is not compatible.
7188 */
7189 static long
7190nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
7191 regmmatch_T *rmp;
7192 win_T *win; /* window in which to search or NULL */
7193 buf_T *buf; /* buffer in which to search */
7194 linenr_T lnum; /* nr of line to start looking for match */
7195 colnr_T col; /* column to start looking for match */
7196 proftime_T *tm UNUSED; /* timeout limit or NULL */
7197{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007198 reg_match = NULL;
7199 reg_mmatch = rmp;
7200 reg_buf = buf;
7201 reg_win = win;
7202 reg_firstlnum = lnum;
7203 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
7204 reg_line_lbr = FALSE;
7205 ireg_ic = rmp->rmm_ic;
7206#ifdef FEAT_MBYTE
7207 ireg_icombine = FALSE;
7208#endif
7209 ireg_maxcol = rmp->rmm_maxcol;
7210
Bram Moolenaarf878bf02013-05-21 21:20:20 +02007211 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007212}
7213
7214#ifdef DEBUG
7215# undef ENABLE_LOG
7216#endif