blob: 5288eb6d3f9a87fe51ac47b93e8a0ae53c998d73 [file] [log] [blame]
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * NFA regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 */
7
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02008/*
9 * Logging of NFA engine.
10 *
11 * The NFA engine can write four log files:
12 * - Error log: Contains NFA engine's fatal errors.
13 * - Dump log: Contains compiled NFA state machine's information.
14 * - Run log: Contains information of matching procedure.
15 * - Debug log: Contains detailed information of matching procedure. Can be
16 * disabled by undefining NFA_REGEXP_DEBUG_LOG.
17 * The first one can also be used without debug mode.
18 * The last three are enabled when compiled as debug mode and individually
19 * disabled by commenting them out.
20 * The log files can get quite big!
21 * Do disable all of this when compiling Vim for debugging, undefine DEBUG in
22 * regexp.c
23 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020024#ifdef DEBUG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020025# define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020026# define ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +020027# define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log"
28# define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log"
29# define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020030#endif
31
Bram Moolenaar1cfad522013-08-14 12:06:49 +020032/* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */
33#define NFA_ADD_NL 31
34
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020035enum
36{
37 NFA_SPLIT = -1024,
38 NFA_MATCH,
39 NFA_SKIP_CHAR, /* matches a 0-length char */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020040
Bram Moolenaar417bad22013-06-07 14:08:30 +020041 NFA_START_COLL, /* [abc] start */
42 NFA_END_COLL, /* [abc] end */
43 NFA_START_NEG_COLL, /* [^abc] start */
Bram Moolenaare7766ee2013-06-08 22:30:03 +020044 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */
45 NFA_RANGE, /* range of the two previous items
46 * (postfix only) */
Bram Moolenaar417bad22013-06-07 14:08:30 +020047 NFA_RANGE_MIN, /* low end of a range */
48 NFA_RANGE_MAX, /* high end of a range */
49
Bram Moolenaare7766ee2013-06-08 22:30:03 +020050 NFA_CONCAT, /* concatenate two previous items (postfix
51 * only) */
52 NFA_OR, /* \| (postfix only) */
53 NFA_STAR, /* greedy * (posfix only) */
54 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */
55 NFA_QUEST, /* greedy \? (postfix only) */
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020057
58 NFA_BOL, /* ^ Begin line */
59 NFA_EOL, /* $ End line */
60 NFA_BOW, /* \< Begin word */
61 NFA_EOW, /* \> End word */
62 NFA_BOF, /* \%^ Begin file */
63 NFA_EOF, /* \%$ End file */
64 NFA_NEWL,
65 NFA_ZSTART, /* Used for \zs */
66 NFA_ZEND, /* Used for \ze */
67 NFA_NOPEN, /* Start of subexpression marked with \%( */
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */
69 NFA_START_INVISIBLE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020070 NFA_START_INVISIBLE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020071 NFA_START_INVISIBLE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020072 NFA_START_INVISIBLE_NEG_FIRST,
Bram Moolenaar61602c52013-06-01 19:54:43 +020073 NFA_START_INVISIBLE_BEFORE,
Bram Moolenaara2947e22013-06-11 22:44:09 +020074 NFA_START_INVISIBLE_BEFORE_FIRST,
Bram Moolenaardecd9542013-06-07 16:31:50 +020075 NFA_START_INVISIBLE_BEFORE_NEG,
Bram Moolenaara2947e22013-06-11 22:44:09 +020076 NFA_START_INVISIBLE_BEFORE_NEG_FIRST,
Bram Moolenaar87953742013-06-05 18:52:40 +020077 NFA_START_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020078 NFA_END_INVISIBLE,
Bram Moolenaardecd9542013-06-07 16:31:50 +020079 NFA_END_INVISIBLE_NEG,
Bram Moolenaar87953742013-06-05 18:52:40 +020080 NFA_END_PATTERN,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020081 NFA_COMPOSING, /* Next nodes in NFA are part of the
82 composing multibyte char */
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */
Bram Moolenaard75799ab72013-06-05 11:05:17 +020084 NFA_OPT_CHARS, /* \%[abc] */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020085
86 /* The following are used only in the postfix form, not in the NFA */
87 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */
88 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */
89 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */
90 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */
91 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */
92
Bram Moolenaar5714b802013-05-28 22:03:20 +020093 NFA_BACKREF1, /* \1 */
94 NFA_BACKREF2, /* \2 */
95 NFA_BACKREF3, /* \3 */
96 NFA_BACKREF4, /* \4 */
97 NFA_BACKREF5, /* \5 */
98 NFA_BACKREF6, /* \6 */
99 NFA_BACKREF7, /* \7 */
100 NFA_BACKREF8, /* \8 */
101 NFA_BACKREF9, /* \9 */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200102#ifdef FEAT_SYN_HL
103 NFA_ZREF1, /* \z1 */
104 NFA_ZREF2, /* \z2 */
105 NFA_ZREF3, /* \z3 */
106 NFA_ZREF4, /* \z4 */
107 NFA_ZREF5, /* \z5 */
108 NFA_ZREF6, /* \z6 */
109 NFA_ZREF7, /* \z7 */
110 NFA_ZREF8, /* \z8 */
111 NFA_ZREF9, /* \z9 */
112#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +0200113 NFA_SKIP, /* Skip characters */
114
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200115 NFA_MOPEN,
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200116 NFA_MOPEN1,
117 NFA_MOPEN2,
118 NFA_MOPEN3,
119 NFA_MOPEN4,
120 NFA_MOPEN5,
121 NFA_MOPEN6,
122 NFA_MOPEN7,
123 NFA_MOPEN8,
124 NFA_MOPEN9,
125
126 NFA_MCLOSE,
127 NFA_MCLOSE1,
128 NFA_MCLOSE2,
129 NFA_MCLOSE3,
130 NFA_MCLOSE4,
131 NFA_MCLOSE5,
132 NFA_MCLOSE6,
133 NFA_MCLOSE7,
134 NFA_MCLOSE8,
135 NFA_MCLOSE9,
136
137#ifdef FEAT_SYN_HL
138 NFA_ZOPEN,
139 NFA_ZOPEN1,
140 NFA_ZOPEN2,
141 NFA_ZOPEN3,
142 NFA_ZOPEN4,
143 NFA_ZOPEN5,
144 NFA_ZOPEN6,
145 NFA_ZOPEN7,
146 NFA_ZOPEN8,
147 NFA_ZOPEN9,
148
149 NFA_ZCLOSE,
150 NFA_ZCLOSE1,
151 NFA_ZCLOSE2,
152 NFA_ZCLOSE3,
153 NFA_ZCLOSE4,
154 NFA_ZCLOSE5,
155 NFA_ZCLOSE6,
156 NFA_ZCLOSE7,
157 NFA_ZCLOSE8,
158 NFA_ZCLOSE9,
159#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200160
161 /* NFA_FIRST_NL */
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200162 NFA_ANY, /* Match any one character. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200163 NFA_IDENT, /* Match identifier char */
164 NFA_SIDENT, /* Match identifier char but no digit */
165 NFA_KWORD, /* Match keyword char */
166 NFA_SKWORD, /* Match word char but no digit */
167 NFA_FNAME, /* Match file name char */
168 NFA_SFNAME, /* Match file name char but no digit */
169 NFA_PRINT, /* Match printable char */
170 NFA_SPRINT, /* Match printable char but no digit */
171 NFA_WHITE, /* Match whitespace char */
172 NFA_NWHITE, /* Match non-whitespace char */
173 NFA_DIGIT, /* Match digit char */
174 NFA_NDIGIT, /* Match non-digit char */
175 NFA_HEX, /* Match hex char */
176 NFA_NHEX, /* Match non-hex char */
177 NFA_OCTAL, /* Match octal char */
178 NFA_NOCTAL, /* Match non-octal char */
179 NFA_WORD, /* Match word char */
180 NFA_NWORD, /* Match non-word char */
181 NFA_HEAD, /* Match head char */
182 NFA_NHEAD, /* Match non-head char */
183 NFA_ALPHA, /* Match alpha char */
184 NFA_NALPHA, /* Match non-alpha char */
185 NFA_LOWER, /* Match lowercase char */
186 NFA_NLOWER, /* Match non-lowercase char */
187 NFA_UPPER, /* Match uppercase char */
188 NFA_NUPPER, /* Match non-uppercase char */
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200189 NFA_LOWER_IC, /* Match [a-z] */
190 NFA_NLOWER_IC, /* Match [^a-z] */
191 NFA_UPPER_IC, /* Match [A-Z] */
192 NFA_NUPPER_IC, /* Match [^A-Z] */
193
194 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL,
195 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL,
Bram Moolenaar423532e2013-05-29 21:14:42 +0200196
197 NFA_CURSOR, /* Match cursor pos */
198 NFA_LNUM, /* Match line number */
199 NFA_LNUM_GT, /* Match > line number */
200 NFA_LNUM_LT, /* Match < line number */
201 NFA_COL, /* Match cursor column */
202 NFA_COL_GT, /* Match > cursor column */
203 NFA_COL_LT, /* Match < cursor column */
204 NFA_VCOL, /* Match cursor virtual column */
205 NFA_VCOL_GT, /* Match > cursor virtual column */
206 NFA_VCOL_LT, /* Match < cursor virtual column */
Bram Moolenaar044aa292013-06-04 21:27:38 +0200207 NFA_MARK, /* Match mark */
208 NFA_MARK_GT, /* Match > mark */
209 NFA_MARK_LT, /* Match < mark */
Bram Moolenaardacd7de2013-06-04 18:28:48 +0200210 NFA_VISUAL, /* Match Visual area */
Bram Moolenaar423532e2013-05-29 21:14:42 +0200211
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200212 /* Character classes [:alnum:] etc */
213 NFA_CLASS_ALNUM,
214 NFA_CLASS_ALPHA,
215 NFA_CLASS_BLANK,
216 NFA_CLASS_CNTRL,
217 NFA_CLASS_DIGIT,
218 NFA_CLASS_GRAPH,
219 NFA_CLASS_LOWER,
220 NFA_CLASS_PRINT,
221 NFA_CLASS_PUNCT,
222 NFA_CLASS_SPACE,
223 NFA_CLASS_UPPER,
224 NFA_CLASS_XDIGIT,
225 NFA_CLASS_TAB,
226 NFA_CLASS_RETURN,
227 NFA_CLASS_BACKSPACE,
228 NFA_CLASS_ESCAPE
229};
230
231/* Keep in sync with classchars. */
232static int nfa_classcodes[] = {
233 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD,
234 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT,
235 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT,
236 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL,
237 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD,
238 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER,
239 NFA_UPPER, NFA_NUPPER
240};
241
242static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c");
243
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200244/* NFA regexp \ze operator encountered. */
Bram Moolenaare0fea9c2013-05-27 20:10:50 +0200245static int nfa_has_zend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200246
Bram Moolenaar428e9872013-05-30 17:05:39 +0200247/* NFA regexp \1 .. \9 encountered. */
248static int nfa_has_backref;
249
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200250#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200251/* NFA regexp has \z( ), set zsubexpr. */
252static int nfa_has_zsubexpr;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200253#endif
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200254
Bram Moolenaar963fee22013-05-26 21:47:28 +0200255/* Number of sub expressions actually being used during execution. 1 if only
256 * the whole match (subexpr 0) is used. */
257static int nfa_nsubexpr;
258
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200259static int *post_start; /* holds the postfix form of r.e. */
260static int *post_end;
261static int *post_ptr;
262
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200263static int nstate; /* Number of states in the NFA. Also used when
264 * executing. */
Bram Moolenaar525666f2013-06-02 16:40:55 +0200265static int istate; /* Index in the state vector, used in alloc_state() */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200266
Bram Moolenaar307aa162013-06-02 16:34:21 +0200267/* If not NULL match must end at this position */
268static save_se_T *nfa_endp = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200269
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +0200270/* listid is global, so that it increases on recursive calls to
271 * nfa_regmatch(), which means we don't have to clear the lastlist field of
272 * all the states. */
273static int nfa_listid;
274static int nfa_alt_listid;
275
276/* 0 for first call to nfa_regmatch(), 1 for recursive call. */
277static int nfa_ll_index = 0;
278
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +0200279static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags));
Bram Moolenaard89616e2013-06-06 18:46:06 +0200280static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth));
281static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth));
Bram Moolenaar473de612013-06-08 18:19:48 +0200282static char_u *nfa_get_match_text __ARGS((nfa_state_T *start));
Bram Moolenaar01b626c2013-06-16 22:49:14 +0200283static int realloc_post_list __ARGS((void));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200284static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl));
Bram Moolenaar417bad22013-06-07 14:08:30 +0200285static int nfa_emit_equi_class __ARGS((int c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200286static int nfa_regatom __ARGS((void));
287static int nfa_regpiece __ARGS((void));
288static int nfa_regconcat __ARGS((void));
289static int nfa_regbranch __ARGS((void));
290static int nfa_reg __ARGS((int paren));
291#ifdef DEBUG
292static void nfa_set_code __ARGS((int c));
293static void nfa_postfix_dump __ARGS((char_u *expr, int retval));
Bram Moolenaar152e7892013-05-25 12:28:11 +0200294static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state));
295static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200296static void nfa_dump __ARGS((nfa_regprog_T *prog));
297#endif
298static int *re2post __ARGS((void));
Bram Moolenaar525666f2013-06-02 16:40:55 +0200299static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +0200300static void st_error __ARGS((int *postfix, int *end, int *p));
301static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200302static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200303static void nfa_postprocess __ARGS((nfa_regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200304static int check_char_class __ARGS((int class, int c));
Bram Moolenaarf6de0322013-06-02 21:30:04 +0200305static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list));
306static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list));
Bram Moolenaar423532e2013-05-29 21:14:42 +0200307static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos));
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200308static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200309static long nfa_regexec_both __ARGS((char_u *line, colnr_T col));
310static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags));
Bram Moolenaar473de612013-06-08 18:19:48 +0200311static void nfa_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200312static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
313static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
Bram Moolenaara2947e22013-06-11 22:44:09 +0200314static int match_follows __ARGS((nfa_state_T *startstate, int depth));
315static int failure_chance __ARGS((nfa_state_T *state, int depth));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200316
317/* helper functions used when doing re2post() ... regatom() parsing */
318#define EMIT(c) do { \
Bram Moolenaar16299b52013-05-30 18:45:23 +0200319 if (post_ptr >= post_end && realloc_post_list() == FAIL) \
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200320 return FAIL; \
321 *post_ptr++ = c; \
322 } while (0)
323
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200324/*
325 * Initialize internal variables before NFA compilation.
326 * Return OK on success, FAIL otherwise.
327 */
328 static int
329nfa_regcomp_start(expr, re_flags)
330 char_u *expr;
331 int re_flags; /* see vim_regcomp() */
332{
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200333 size_t postfix_size;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200334 int nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200335
336 nstate = 0;
337 istate = 0;
Bram Moolenaar61db8b52013-05-26 17:45:49 +0200338 /* A reasonable estimation for maximum size */
Bram Moolenaar54dafde2013-05-31 23:18:00 +0200339 nstate_max = (int)(STRLEN(expr) + 1) * 25;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200340
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200341 /* Some items blow up in size, such as [A-z]. Add more space for that.
Bram Moolenaar16299b52013-05-30 18:45:23 +0200342 * When it is still not enough realloc_post_list() will be used. */
Bram Moolenaarca12d7c2013-05-20 21:26:33 +0200343 nstate_max += 1000;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200344
345 /* Size for postfix representation of expr. */
Bram Moolenaar16299b52013-05-30 18:45:23 +0200346 postfix_size = sizeof(int) * nstate_max;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200347
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200348 post_start = (int *)lalloc(postfix_size, TRUE);
349 if (post_start == NULL)
350 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200351 post_ptr = post_start;
Bram Moolenaarbc0ea8f2013-05-20 13:44:29 +0200352 post_end = post_start + nstate_max;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200353 nfa_has_zend = FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +0200354 nfa_has_backref = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200355
Bram Moolenaarefb23f22013-06-01 23:02:54 +0200356 /* shared with BT engine */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200357 regcomp_start(expr, re_flags);
358
359 return OK;
360}
361
362/*
Bram Moolenaard89616e2013-06-06 18:46:06 +0200363 * Figure out if the NFA state list starts with an anchor, must match at start
364 * of the line.
365 */
366 static int
367nfa_get_reganch(start, depth)
368 nfa_state_T *start;
369 int depth;
370{
371 nfa_state_T *p = start;
372
373 if (depth > 4)
374 return 0;
375
376 while (p != NULL)
377 {
378 switch (p->c)
379 {
380 case NFA_BOL:
381 case NFA_BOF:
382 return 1; /* yes! */
383
384 case NFA_ZSTART:
385 case NFA_ZEND:
386 case NFA_CURSOR:
387 case NFA_VISUAL:
388
389 case NFA_MOPEN:
390 case NFA_MOPEN1:
391 case NFA_MOPEN2:
392 case NFA_MOPEN3:
393 case NFA_MOPEN4:
394 case NFA_MOPEN5:
395 case NFA_MOPEN6:
396 case NFA_MOPEN7:
397 case NFA_MOPEN8:
398 case NFA_MOPEN9:
399 case NFA_NOPEN:
400#ifdef FEAT_SYN_HL
401 case NFA_ZOPEN:
402 case NFA_ZOPEN1:
403 case NFA_ZOPEN2:
404 case NFA_ZOPEN3:
405 case NFA_ZOPEN4:
406 case NFA_ZOPEN5:
407 case NFA_ZOPEN6:
408 case NFA_ZOPEN7:
409 case NFA_ZOPEN8:
410 case NFA_ZOPEN9:
411#endif
412 p = p->out;
413 break;
414
415 case NFA_SPLIT:
416 return nfa_get_reganch(p->out, depth + 1)
417 && nfa_get_reganch(p->out1, depth + 1);
418
419 default:
420 return 0; /* noooo */
421 }
422 }
423 return 0;
424}
425
426/*
427 * Figure out if the NFA state list starts with a character which must match
428 * at start of the match.
429 */
430 static int
431nfa_get_regstart(start, depth)
432 nfa_state_T *start;
433 int depth;
434{
435 nfa_state_T *p = start;
436
437 if (depth > 4)
438 return 0;
439
440 while (p != NULL)
441 {
442 switch (p->c)
443 {
444 /* all kinds of zero-width matches */
445 case NFA_BOL:
446 case NFA_BOF:
447 case NFA_BOW:
448 case NFA_EOW:
449 case NFA_ZSTART:
450 case NFA_ZEND:
451 case NFA_CURSOR:
452 case NFA_VISUAL:
453 case NFA_LNUM:
454 case NFA_LNUM_GT:
455 case NFA_LNUM_LT:
456 case NFA_COL:
457 case NFA_COL_GT:
458 case NFA_COL_LT:
459 case NFA_VCOL:
460 case NFA_VCOL_GT:
461 case NFA_VCOL_LT:
462 case NFA_MARK:
463 case NFA_MARK_GT:
464 case NFA_MARK_LT:
465
466 case NFA_MOPEN:
467 case NFA_MOPEN1:
468 case NFA_MOPEN2:
469 case NFA_MOPEN3:
470 case NFA_MOPEN4:
471 case NFA_MOPEN5:
472 case NFA_MOPEN6:
473 case NFA_MOPEN7:
474 case NFA_MOPEN8:
475 case NFA_MOPEN9:
476 case NFA_NOPEN:
477#ifdef FEAT_SYN_HL
478 case NFA_ZOPEN:
479 case NFA_ZOPEN1:
480 case NFA_ZOPEN2:
481 case NFA_ZOPEN3:
482 case NFA_ZOPEN4:
483 case NFA_ZOPEN5:
484 case NFA_ZOPEN6:
485 case NFA_ZOPEN7:
486 case NFA_ZOPEN8:
487 case NFA_ZOPEN9:
488#endif
489 p = p->out;
490 break;
491
492 case NFA_SPLIT:
493 {
494 int c1 = nfa_get_regstart(p->out, depth + 1);
495 int c2 = nfa_get_regstart(p->out1, depth + 1);
496
497 if (c1 == c2)
498 return c1; /* yes! */
499 return 0;
500 }
501
502 default:
Bram Moolenaardecd9542013-06-07 16:31:50 +0200503 if (p->c > 0)
Bram Moolenaard89616e2013-06-06 18:46:06 +0200504 return p->c; /* yes! */
505 return 0;
506 }
507 }
508 return 0;
509}
510
511/*
Bram Moolenaar473de612013-06-08 18:19:48 +0200512 * Figure out if the NFA state list contains just literal text and nothing
Bram Moolenaare7766ee2013-06-08 22:30:03 +0200513 * else. If so return a string in allocated memory with what must match after
514 * regstart. Otherwise return NULL.
Bram Moolenaar473de612013-06-08 18:19:48 +0200515 */
516 static char_u *
517nfa_get_match_text(start)
518 nfa_state_T *start;
519{
520 nfa_state_T *p = start;
521 int len = 0;
522 char_u *ret;
523 char_u *s;
524
525 if (p->c != NFA_MOPEN)
526 return NULL; /* just in case */
527 p = p->out;
528 while (p->c > 0)
529 {
530 len += MB_CHAR2LEN(p->c);
531 p = p->out;
532 }
533 if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH)
534 return NULL;
535
536 ret = alloc(len);
537 if (ret != NULL)
538 {
539 len = 0;
540 p = start->out->out; /* skip first char, it goes into regstart */
541 s = ret;
542 while (p->c > 0)
543 {
544#ifdef FEAT_MBYTE
545 if (has_mbyte)
546 s += (*mb_char2bytes)(p->c, s);
547 else
548#endif
549 *s++ = p->c;
550 p = p->out;
551 }
552 *s = NUL;
553 }
554 return ret;
555}
556
557/*
Bram Moolenaar16299b52013-05-30 18:45:23 +0200558 * Allocate more space for post_start. Called when
559 * running above the estimated number of states.
560 */
561 static int
562realloc_post_list()
563{
Bram Moolenaar99dc19d2013-05-31 20:49:31 +0200564 int nstate_max = (int)(post_end - post_start);
Bram Moolenaar16299b52013-05-30 18:45:23 +0200565 int new_max = nstate_max + 1000;
566 int *new_start;
567 int *old_start;
568
569 new_start = (int *)lalloc(new_max * sizeof(int), TRUE);
570 if (new_start == NULL)
571 return FAIL;
572 mch_memmove(new_start, post_start, nstate_max * sizeof(int));
Bram Moolenaar16299b52013-05-30 18:45:23 +0200573 old_start = post_start;
574 post_start = new_start;
575 post_ptr = new_start + (post_ptr - old_start);
576 post_end = post_start + new_max;
577 vim_free(old_start);
578 return OK;
579}
580
581/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200582 * Search between "start" and "end" and try to recognize a
583 * character class in expanded form. For example [0-9].
584 * On success, return the id the character class to be emitted.
585 * On failure, return 0 (=FAIL)
586 * Start points to the first char of the range, while end should point
587 * to the closing brace.
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200588 * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may
589 * need to be interpreted as [a-zA-Z].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200590 */
591 static int
592nfa_recognize_char_class(start, end, extra_newl)
593 char_u *start;
594 char_u *end;
595 int extra_newl;
596{
Bram Moolenaarf8115092013-06-04 17:47:05 +0200597# define CLASS_not 0x80
598# define CLASS_af 0x40
599# define CLASS_AF 0x20
600# define CLASS_az 0x10
601# define CLASS_AZ 0x08
602# define CLASS_o7 0x04
603# define CLASS_o9 0x02
604# define CLASS_underscore 0x01
605
606 int newl = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200607 char_u *p;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200608 int config = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200609
610 if (extra_newl == TRUE)
611 newl = TRUE;
612
613 if (*end != ']')
614 return FAIL;
615 p = start;
616 if (*p == '^')
617 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200618 config |= CLASS_not;
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200619 p++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200620 }
621
622 while (p < end)
623 {
624 if (p + 2 < end && *(p + 1) == '-')
625 {
626 switch (*p)
627 {
628 case '0':
629 if (*(p + 2) == '9')
630 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200631 config |= CLASS_o9;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200632 break;
633 }
634 else
635 if (*(p + 2) == '7')
636 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200637 config |= CLASS_o7;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200638 break;
639 }
640 case 'a':
641 if (*(p + 2) == 'z')
642 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200643 config |= CLASS_az;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200644 break;
645 }
646 else
647 if (*(p + 2) == 'f')
648 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200649 config |= CLASS_af;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200650 break;
651 }
652 case 'A':
653 if (*(p + 2) == 'Z')
654 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200655 config |= CLASS_AZ;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200656 break;
657 }
658 else
659 if (*(p + 2) == 'F')
660 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200661 config |= CLASS_AF;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200662 break;
663 }
664 /* FALLTHROUGH */
665 default:
666 return FAIL;
667 }
668 p += 3;
669 }
670 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n')
671 {
672 newl = TRUE;
673 p += 2;
674 }
675 else if (*p == '_')
676 {
Bram Moolenaarf8115092013-06-04 17:47:05 +0200677 config |= CLASS_underscore;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200678 p ++;
679 }
680 else if (*p == '\n')
681 {
682 newl = TRUE;
683 p ++;
684 }
685 else
686 return FAIL;
687 } /* while (p < end) */
688
689 if (p != end)
690 return FAIL;
691
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200692 if (newl == TRUE)
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200693 extra_newl = NFA_ADD_NL;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200694
695 switch (config)
696 {
697 case CLASS_o9:
698 return extra_newl + NFA_DIGIT;
699 case CLASS_not | CLASS_o9:
700 return extra_newl + NFA_NDIGIT;
701 case CLASS_af | CLASS_AF | CLASS_o9:
702 return extra_newl + NFA_HEX;
703 case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9:
704 return extra_newl + NFA_NHEX;
705 case CLASS_o7:
706 return extra_newl + NFA_OCTAL;
707 case CLASS_not | CLASS_o7:
708 return extra_newl + NFA_NOCTAL;
709 case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
710 return extra_newl + NFA_WORD;
711 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore:
712 return extra_newl + NFA_NWORD;
713 case CLASS_az | CLASS_AZ | CLASS_underscore:
714 return extra_newl + NFA_HEAD;
715 case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore:
716 return extra_newl + NFA_NHEAD;
717 case CLASS_az | CLASS_AZ:
718 return extra_newl + NFA_ALPHA;
719 case CLASS_not | CLASS_az | CLASS_AZ:
720 return extra_newl + NFA_NALPHA;
721 case CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200722 return extra_newl + NFA_LOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200723 case CLASS_not | CLASS_az:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200724 return extra_newl + NFA_NLOWER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200725 case CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200726 return extra_newl + NFA_UPPER_IC;
Bram Moolenaarf8115092013-06-04 17:47:05 +0200727 case CLASS_not | CLASS_AZ:
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200728 return extra_newl + NFA_NUPPER_IC;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200729 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200730 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200731}
732
733/*
734 * Produce the bytes for equivalence class "c".
735 * Currently only handles latin1, latin9 and utf-8.
736 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is
737 * equivalent to 'a OR b OR c'
738 *
739 * NOTE! When changing this function, also update reg_equi_class()
740 */
741 static int
Bram Moolenaar417bad22013-06-07 14:08:30 +0200742nfa_emit_equi_class(c)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200743 int c;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200744{
Bram Moolenaar417bad22013-06-07 14:08:30 +0200745#define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200746
747#ifdef FEAT_MBYTE
748 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
749 || STRCMP(p_enc, "iso-8859-15") == 0)
750#endif
751 {
752 switch (c)
753 {
Bram Moolenaar417bad22013-06-07 14:08:30 +0200754 case 'A': case 0300: case 0301: case 0302:
755 case 0303: case 0304: case 0305:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200756 EMIT2('A'); EMIT2(0300); EMIT2(0301);
757 EMIT2(0302); EMIT2(0303); EMIT2(0304);
Bram Moolenaar417bad22013-06-07 14:08:30 +0200758 EMIT2(0305);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200759 return OK;
760
Bram Moolenaar417bad22013-06-07 14:08:30 +0200761 case 'C': case 0307:
762 EMIT2('C'); EMIT2(0307);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200763 return OK;
764
Bram Moolenaar417bad22013-06-07 14:08:30 +0200765 case 'E': case 0310: case 0311: case 0312: case 0313:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200766 EMIT2('E'); EMIT2(0310); EMIT2(0311);
767 EMIT2(0312); EMIT2(0313);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200768 return OK;
769
Bram Moolenaar417bad22013-06-07 14:08:30 +0200770 case 'I': case 0314: case 0315: case 0316: case 0317:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200771 EMIT2('I'); EMIT2(0314); EMIT2(0315);
772 EMIT2(0316); EMIT2(0317);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200773 return OK;
774
Bram Moolenaar417bad22013-06-07 14:08:30 +0200775 case 'N': case 0321:
776 EMIT2('N'); EMIT2(0321);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200777 return OK;
778
Bram Moolenaar417bad22013-06-07 14:08:30 +0200779 case 'O': case 0322: case 0323: case 0324: case 0325:
780 case 0326:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200781 EMIT2('O'); EMIT2(0322); EMIT2(0323);
782 EMIT2(0324); EMIT2(0325); EMIT2(0326);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200783 return OK;
784
Bram Moolenaar417bad22013-06-07 14:08:30 +0200785 case 'U': case 0331: case 0332: case 0333: case 0334:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200786 EMIT2('U'); EMIT2(0331); EMIT2(0332);
787 EMIT2(0333); EMIT2(0334);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200788 return OK;
789
Bram Moolenaar417bad22013-06-07 14:08:30 +0200790 case 'Y': case 0335:
791 EMIT2('Y'); EMIT2(0335);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200792 return OK;
793
Bram Moolenaar417bad22013-06-07 14:08:30 +0200794 case 'a': case 0340: case 0341: case 0342:
795 case 0343: case 0344: case 0345:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200796 EMIT2('a'); EMIT2(0340); EMIT2(0341);
797 EMIT2(0342); EMIT2(0343); EMIT2(0344);
Bram Moolenaar417bad22013-06-07 14:08:30 +0200798 EMIT2(0345);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200799 return OK;
800
Bram Moolenaar417bad22013-06-07 14:08:30 +0200801 case 'c': case 0347:
802 EMIT2('c'); EMIT2(0347);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200803 return OK;
804
Bram Moolenaar417bad22013-06-07 14:08:30 +0200805 case 'e': case 0350: case 0351: case 0352: case 0353:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200806 EMIT2('e'); EMIT2(0350); EMIT2(0351);
807 EMIT2(0352); EMIT2(0353);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 return OK;
809
Bram Moolenaar417bad22013-06-07 14:08:30 +0200810 case 'i': case 0354: case 0355: case 0356: case 0357:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200811 EMIT2('i'); EMIT2(0354); EMIT2(0355);
812 EMIT2(0356); EMIT2(0357);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200813 return OK;
814
Bram Moolenaar417bad22013-06-07 14:08:30 +0200815 case 'n': case 0361:
816 EMIT2('n'); EMIT2(0361);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 return OK;
818
Bram Moolenaar417bad22013-06-07 14:08:30 +0200819 case 'o': case 0362: case 0363: case 0364: case 0365:
820 case 0366:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200821 EMIT2('o'); EMIT2(0362); EMIT2(0363);
822 EMIT2(0364); EMIT2(0365); EMIT2(0366);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200823 return OK;
824
Bram Moolenaar417bad22013-06-07 14:08:30 +0200825 case 'u': case 0371: case 0372: case 0373: case 0374:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200826 EMIT2('u'); EMIT2(0371); EMIT2(0372);
827 EMIT2(0373); EMIT2(0374);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200828 return OK;
829
Bram Moolenaar417bad22013-06-07 14:08:30 +0200830 case 'y': case 0375: case 0377:
Bram Moolenaar6dbe68c2013-08-01 16:21:34 +0200831 EMIT2('y'); EMIT2(0375); EMIT2(0377);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200832 return OK;
833
834 default:
835 return FAIL;
836 }
837 }
838
839 EMIT(c);
840 return OK;
841#undef EMIT2
842}
843
844/*
845 * Code to parse regular expression.
846 *
847 * We try to reuse parsing functions in regexp.c to
848 * minimize surprise and keep the syntax consistent.
849 */
850
851/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200852 * Parse the lowest level.
853 *
854 * An atom can be one of a long list of items. Many atoms match one character
855 * in the text. It is often an ordinary character or a character class.
856 * Braces can be used to make a pattern into an atom. The "\z(\)" construct
857 * is only for syntax highlighting.
858 *
859 * atom ::= ordinary-atom
860 * or \( pattern \)
861 * or \%( pattern \)
862 * or \z( pattern \)
863 */
864 static int
865nfa_regatom()
866{
867 int c;
868 int charclass;
869 int equiclass;
870 int collclass;
871 int got_coll_char;
872 char_u *p;
873 char_u *endp;
874#ifdef FEAT_MBYTE
875 char_u *old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200876#endif
877 int extra = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200878 int emit_range;
879 int negated;
880 int result;
881 int startc = -1;
882 int endc = -1;
883 int oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200884
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200885 c = getchr();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200886 switch (c)
887 {
Bram Moolenaar47196582013-05-25 22:04:23 +0200888 case NUL:
Bram Moolenaar47196582013-05-25 22:04:23 +0200889 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely"));
890
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200891 case Magic('^'):
892 EMIT(NFA_BOL);
893 break;
894
895 case Magic('$'):
896 EMIT(NFA_EOL);
897#if defined(FEAT_SYN_HL) || defined(PROTO)
898 had_eol = TRUE;
899#endif
900 break;
901
902 case Magic('<'):
903 EMIT(NFA_BOW);
904 break;
905
906 case Magic('>'):
907 EMIT(NFA_EOW);
908 break;
909
910 case Magic('_'):
911 c = no_Magic(getchr());
912 if (c == '^') /* "\_^" is start-of-line */
913 {
914 EMIT(NFA_BOL);
915 break;
916 }
917 if (c == '$') /* "\_$" is end-of-line */
918 {
919 EMIT(NFA_EOL);
920#if defined(FEAT_SYN_HL) || defined(PROTO)
921 had_eol = TRUE;
922#endif
923 break;
924 }
925
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200926 extra = NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200927
928 /* "\_[" is collection plus newline */
929 if (c == '[')
Bram Moolenaar307d10a2013-05-23 22:25:15 +0200930 goto collection;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200931
932 /* "\_x" is character class plus newline */
933 /*FALLTHROUGH*/
934
935 /*
936 * Character classes.
937 */
938 case Magic('.'):
939 case Magic('i'):
940 case Magic('I'):
941 case Magic('k'):
942 case Magic('K'):
943 case Magic('f'):
944 case Magic('F'):
945 case Magic('p'):
946 case Magic('P'):
947 case Magic('s'):
948 case Magic('S'):
949 case Magic('d'):
950 case Magic('D'):
951 case Magic('x'):
952 case Magic('X'):
953 case Magic('o'):
954 case Magic('O'):
955 case Magic('w'):
956 case Magic('W'):
957 case Magic('h'):
958 case Magic('H'):
959 case Magic('a'):
960 case Magic('A'):
961 case Magic('l'):
962 case Magic('L'):
963 case Magic('u'):
964 case Magic('U'):
965 p = vim_strchr(classchars, no_Magic(c));
966 if (p == NULL)
967 {
Bram Moolenaar5714b802013-05-28 22:03:20 +0200968 EMSGN("INTERNAL: Unknown character class char: %ld", c);
969 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200970 }
971#ifdef FEAT_MBYTE
972 /* When '.' is followed by a composing char ignore the dot, so that
973 * the composing char is matched here. */
974 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
975 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +0200976 old_regparse = regparse;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200977 c = getchr();
978 goto nfa_do_multibyte;
979 }
980#endif
981 EMIT(nfa_classcodes[p - classchars]);
Bram Moolenaar1cfad522013-08-14 12:06:49 +0200982 if (extra == NFA_ADD_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200983 {
984 EMIT(NFA_NEWL);
985 EMIT(NFA_OR);
986 regflags |= RF_HASNL;
987 }
988 break;
989
990 case Magic('n'):
991 if (reg_string)
Bram Moolenaar417bad22013-06-07 14:08:30 +0200992 /* In a string "\n" matches a newline character. */
993 EMIT(NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200994 else
995 {
996 /* In buffer text "\n" matches the end of a line. */
997 EMIT(NFA_NEWL);
998 regflags |= RF_HASNL;
999 }
1000 break;
1001
1002 case Magic('('):
1003 if (nfa_reg(REG_PAREN) == FAIL)
1004 return FAIL; /* cascaded error */
1005 break;
1006
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001007 case Magic('|'):
1008 case Magic('&'):
1009 case Magic(')'):
Bram Moolenaarba404472013-05-19 22:31:18 +02001010 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001011 return FAIL;
1012
1013 case Magic('='):
1014 case Magic('?'):
1015 case Magic('+'):
1016 case Magic('@'):
1017 case Magic('*'):
1018 case Magic('{'):
1019 /* these should follow an atom, not form an atom */
Bram Moolenaarba404472013-05-19 22:31:18 +02001020 EMSGN(_(e_misplaced), no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001021 return FAIL;
1022
Bram Moolenaarf18fb7a2013-06-02 22:08:03 +02001023 case Magic('~'):
1024 {
1025 char_u *lp;
1026
1027 /* Previous substitute pattern.
1028 * Generated as "\%(pattern\)". */
1029 if (reg_prev_sub == NULL)
1030 {
1031 EMSG(_(e_nopresub));
1032 return FAIL;
1033 }
1034 for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp))
1035 {
1036 EMIT(PTR2CHAR(lp));
1037 if (lp != reg_prev_sub)
1038 EMIT(NFA_CONCAT);
1039 }
1040 EMIT(NFA_NOPEN);
1041 break;
1042 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001043
Bram Moolenaar428e9872013-05-30 17:05:39 +02001044 case Magic('1'):
1045 case Magic('2'):
1046 case Magic('3'):
1047 case Magic('4'):
1048 case Magic('5'):
1049 case Magic('6'):
1050 case Magic('7'):
1051 case Magic('8'):
1052 case Magic('9'):
1053 EMIT(NFA_BACKREF1 + (no_Magic(c) - '1'));
1054 nfa_has_backref = TRUE;
1055 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001056
1057 case Magic('z'):
1058 c = no_Magic(getchr());
1059 switch (c)
1060 {
1061 case 's':
1062 EMIT(NFA_ZSTART);
1063 break;
1064 case 'e':
1065 EMIT(NFA_ZEND);
1066 nfa_has_zend = TRUE;
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02001067 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001068#ifdef FEAT_SYN_HL
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001069 case '1':
1070 case '2':
1071 case '3':
1072 case '4':
1073 case '5':
1074 case '6':
1075 case '7':
1076 case '8':
1077 case '9':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001078 /* \z1...\z9 */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001079 if (reg_do_extmatch != REX_USE)
1080 EMSG_RET_FAIL(_(e_z1_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001081 EMIT(NFA_ZREF1 + (no_Magic(c) - '1'));
1082 /* No need to set nfa_has_backref, the sub-matches don't
Bram Moolenaarf8115092013-06-04 17:47:05 +02001083 * change when \z1 .. \z9 matches or not. */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001084 re_has_z = REX_USE;
1085 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001086 case '(':
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001087 /* \z( */
Bram Moolenaar5de820b2013-06-02 15:01:57 +02001088 if (reg_do_extmatch != REX_SET)
1089 EMSG_RET_FAIL(_(e_z_not_allowed));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001090 if (nfa_reg(REG_ZPAREN) == FAIL)
1091 return FAIL; /* cascaded error */
1092 re_has_z = REX_SET;
1093 break;
1094#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001095 default:
Bram Moolenaarba404472013-05-19 22:31:18 +02001096 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"),
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001097 no_Magic(c));
1098 return FAIL;
1099 }
1100 break;
1101
1102 case Magic('%'):
1103 c = no_Magic(getchr());
1104 switch (c)
1105 {
1106 /* () without a back reference */
1107 case '(':
1108 if (nfa_reg(REG_NPAREN) == FAIL)
1109 return FAIL;
1110 EMIT(NFA_NOPEN);
1111 break;
1112
1113 case 'd': /* %d123 decimal */
1114 case 'o': /* %o123 octal */
1115 case 'x': /* %xab hex 2 */
1116 case 'u': /* %uabcd hex 4 */
1117 case 'U': /* %U1234abcd hex 8 */
Bram Moolenaar47196582013-05-25 22:04:23 +02001118 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001119 int nr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001120
Bram Moolenaar47196582013-05-25 22:04:23 +02001121 switch (c)
1122 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001123 case 'd': nr = getdecchrs(); break;
1124 case 'o': nr = getoctchrs(); break;
1125 case 'x': nr = gethexchrs(2); break;
1126 case 'u': nr = gethexchrs(4); break;
1127 case 'U': nr = gethexchrs(8); break;
1128 default: nr = -1; break;
Bram Moolenaar47196582013-05-25 22:04:23 +02001129 }
1130
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001131 if (nr < 0)
Bram Moolenaar47196582013-05-25 22:04:23 +02001132 EMSG2_RET_FAIL(
1133 _("E678: Invalid character after %s%%[dxouU]"),
1134 reg_magic == MAGIC_ALL);
1135 /* TODO: what if a composing character follows? */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001136 EMIT(nr);
Bram Moolenaar47196582013-05-25 22:04:23 +02001137 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001138 break;
1139
1140 /* Catch \%^ and \%$ regardless of where they appear in the
1141 * pattern -- regardless of whether or not it makes sense. */
1142 case '^':
1143 EMIT(NFA_BOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001144 break;
1145
1146 case '$':
1147 EMIT(NFA_EOF);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001148 break;
1149
1150 case '#':
Bram Moolenaar423532e2013-05-29 21:14:42 +02001151 EMIT(NFA_CURSOR);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001152 break;
1153
1154 case 'V':
Bram Moolenaardacd7de2013-06-04 18:28:48 +02001155 EMIT(NFA_VISUAL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001156 break;
1157
1158 case '[':
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001159 {
1160 int n;
1161
1162 /* \%[abc] */
Bram Moolenaard7986252013-06-17 21:33:41 +02001163 for (n = 0; (c = peekchr()) != ']'; ++n)
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001164 {
1165 if (c == NUL)
1166 EMSG2_RET_FAIL(_(e_missing_sb),
1167 reg_magic == MAGIC_ALL);
Bram Moolenaard7986252013-06-17 21:33:41 +02001168 /* recursive call! */
1169 if (nfa_regatom() == FAIL)
1170 return FAIL;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001171 }
Bram Moolenaard7986252013-06-17 21:33:41 +02001172 getchr(); /* get the ] */
Bram Moolenaar2976c022013-06-05 21:30:37 +02001173 if (n == 0)
1174 EMSG2_RET_FAIL(_(e_empty_sb),
1175 reg_magic == MAGIC_ALL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001176 EMIT(NFA_OPT_CHARS);
1177 EMIT(n);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001178
1179 /* Emit as "\%(\%[abc]\)" to be able to handle
1180 * "\%[abc]*" which would cause the empty string to be
1181 * matched an unlimited number of times. NFA_NOPEN is
1182 * added only once at a position, while NFA_SPLIT is
1183 * added multiple times. This is more efficient than
1184 * not allowsing NFA_SPLIT multiple times, it is used
1185 * a lot. */
1186 EMIT(NFA_NOPEN);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001187 break;
1188 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001189
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001190 default:
Bram Moolenaar423532e2013-05-29 21:14:42 +02001191 {
Bram Moolenaar021e1472013-05-30 19:18:31 +02001192 int n = 0;
Bram Moolenaar423532e2013-05-29 21:14:42 +02001193 int cmp = c;
1194
1195 if (c == '<' || c == '>')
1196 c = getchr();
1197 while (VIM_ISDIGIT(c))
1198 {
1199 n = n * 10 + (c - '0');
1200 c = getchr();
1201 }
1202 if (c == 'l' || c == 'c' || c == 'v')
1203 {
Bram Moolenaar423532e2013-05-29 21:14:42 +02001204 if (c == 'l')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001205 /* \%{n}l \%{n}<l \%{n}>l */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001206 EMIT(cmp == '<' ? NFA_LNUM_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001207 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001208 else if (c == 'c')
Bram Moolenaar044aa292013-06-04 21:27:38 +02001209 /* \%{n}c \%{n}<c \%{n}>c */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001210 EMIT(cmp == '<' ? NFA_COL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001211 cmp == '>' ? NFA_COL_GT : NFA_COL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001212 else
Bram Moolenaar044aa292013-06-04 21:27:38 +02001213 /* \%{n}v \%{n}<v \%{n}>v */
Bram Moolenaar423532e2013-05-29 21:14:42 +02001214 EMIT(cmp == '<' ? NFA_VCOL_LT :
Bram Moolenaar044aa292013-06-04 21:27:38 +02001215 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001216 EMIT(n);
Bram Moolenaar423532e2013-05-29 21:14:42 +02001217 break;
1218 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02001219 else if (c == '\'' && n == 0)
1220 {
1221 /* \%'m \%<'m \%>'m */
Bram Moolenaar044aa292013-06-04 21:27:38 +02001222 EMIT(cmp == '<' ? NFA_MARK_LT :
1223 cmp == '>' ? NFA_MARK_GT : NFA_MARK);
Bram Moolenaard75799ab72013-06-05 11:05:17 +02001224 EMIT(getchr());
Bram Moolenaar044aa292013-06-04 21:27:38 +02001225 break;
1226 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02001227 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02001228 EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"),
1229 no_Magic(c));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001230 return FAIL;
1231 }
1232 break;
1233
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001234 case Magic('['):
Bram Moolenaar307d10a2013-05-23 22:25:15 +02001235collection:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001236 /*
Bram Moolenaar417bad22013-06-07 14:08:30 +02001237 * [abc] uses NFA_START_COLL - NFA_END_COLL
1238 * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL
1239 * Each character is produced as a regular state, using
1240 * NFA_CONCAT to bind them together.
1241 * Besides normal characters there can be:
1242 * - character classes NFA_CLASS_*
1243 * - ranges, two characters followed by NFA_RANGE.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001244 */
1245
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001246 p = regparse;
1247 endp = skip_anyof(p);
1248 if (*endp == ']')
1249 {
1250 /*
1251 * Try to reverse engineer character classes. For example,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001252 * recognize that [0-9] stands for \d and [A-Za-z_] for \h,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001253 * and perform the necessary substitutions in the NFA.
1254 */
1255 result = nfa_recognize_char_class(regparse, endp,
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001256 extra == NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001257 if (result != FAIL)
1258 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001259 if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001260 {
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001261 EMIT(result - NFA_ADD_NL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001262 EMIT(NFA_NEWL);
1263 EMIT(NFA_OR);
1264 }
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001265 else
1266 EMIT(result);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001267 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001268 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001269 return OK;
1270 }
1271 /*
1272 * Failed to recognize a character class. Use the simple
1273 * version that turns [abc] into 'a' OR 'b' OR 'c'
1274 */
1275 startc = endc = oldstartc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001276 negated = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001277 if (*regparse == '^') /* negated range */
1278 {
1279 negated = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001280 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001281 EMIT(NFA_START_NEG_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001282 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001283 else
1284 EMIT(NFA_START_COLL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001285 if (*regparse == '-')
1286 {
1287 startc = '-';
1288 EMIT(startc);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001289 EMIT(NFA_CONCAT);
Bram Moolenaar51a29832013-05-28 22:30:35 +02001290 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001291 }
1292 /* Emit the OR branches for each character in the [] */
1293 emit_range = FALSE;
1294 while (regparse < endp)
1295 {
1296 oldstartc = startc;
1297 startc = -1;
1298 got_coll_char = FALSE;
1299 if (*regparse == '[')
1300 {
1301 /* Check for [: :], [= =], [. .] */
1302 equiclass = collclass = 0;
1303 charclass = get_char_class(&regparse);
1304 if (charclass == CLASS_NONE)
1305 {
1306 equiclass = get_equi_class(&regparse);
1307 if (equiclass == 0)
1308 collclass = get_coll_element(&regparse);
1309 }
1310
1311 /* Character class like [:alpha:] */
1312 if (charclass != CLASS_NONE)
1313 {
1314 switch (charclass)
1315 {
1316 case CLASS_ALNUM:
1317 EMIT(NFA_CLASS_ALNUM);
1318 break;
1319 case CLASS_ALPHA:
1320 EMIT(NFA_CLASS_ALPHA);
1321 break;
1322 case CLASS_BLANK:
1323 EMIT(NFA_CLASS_BLANK);
1324 break;
1325 case CLASS_CNTRL:
1326 EMIT(NFA_CLASS_CNTRL);
1327 break;
1328 case CLASS_DIGIT:
1329 EMIT(NFA_CLASS_DIGIT);
1330 break;
1331 case CLASS_GRAPH:
1332 EMIT(NFA_CLASS_GRAPH);
1333 break;
1334 case CLASS_LOWER:
1335 EMIT(NFA_CLASS_LOWER);
1336 break;
1337 case CLASS_PRINT:
1338 EMIT(NFA_CLASS_PRINT);
1339 break;
1340 case CLASS_PUNCT:
1341 EMIT(NFA_CLASS_PUNCT);
1342 break;
1343 case CLASS_SPACE:
1344 EMIT(NFA_CLASS_SPACE);
1345 break;
1346 case CLASS_UPPER:
1347 EMIT(NFA_CLASS_UPPER);
1348 break;
1349 case CLASS_XDIGIT:
1350 EMIT(NFA_CLASS_XDIGIT);
1351 break;
1352 case CLASS_TAB:
1353 EMIT(NFA_CLASS_TAB);
1354 break;
1355 case CLASS_RETURN:
1356 EMIT(NFA_CLASS_RETURN);
1357 break;
1358 case CLASS_BACKSPACE:
1359 EMIT(NFA_CLASS_BACKSPACE);
1360 break;
1361 case CLASS_ESCAPE:
1362 EMIT(NFA_CLASS_ESCAPE);
1363 break;
1364 }
Bram Moolenaar417bad22013-06-07 14:08:30 +02001365 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001366 continue;
1367 }
1368 /* Try equivalence class [=a=] and the like */
1369 if (equiclass != 0)
1370 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001371 result = nfa_emit_equi_class(equiclass);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001372 if (result == FAIL)
1373 {
1374 /* should never happen */
1375 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!"));
1376 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001377 continue;
1378 }
1379 /* Try collating class like [. .] */
1380 if (collclass != 0)
1381 {
1382 startc = collclass; /* allow [.a.]-x as a range */
1383 /* Will emit the proper atom at the end of the
1384 * while loop. */
1385 }
1386 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001387 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a
1388 * start character. */
1389 if (*regparse == '-' && oldstartc != -1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001390 {
1391 emit_range = TRUE;
1392 startc = oldstartc;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001393 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001394 continue; /* reading the end of the range */
1395 }
1396
1397 /* Now handle simple and escaped characters.
1398 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1399 * accepts "\t", "\e", etc., but only when the 'l' flag in
1400 * 'cpoptions' is not included.
1401 * Posix doesn't recognize backslash at all.
1402 */
1403 if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001404 && !reg_cpo_bsl
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001405 && regparse + 1 <= endp
1406 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001407 || (!reg_cpo_lit
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001408 && vim_strchr(REGEXP_ABBR, regparse[1])
1409 != NULL)
1410 )
1411 )
1412 {
Bram Moolenaar51a29832013-05-28 22:30:35 +02001413 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001414
Bram Moolenaar673af4d2013-05-21 22:00:51 +02001415 if (*regparse == 'n')
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001416 startc = reg_string ? NL : NFA_NEWL;
1417 else
1418 if (*regparse == 'd'
1419 || *regparse == 'o'
1420 || *regparse == 'x'
1421 || *regparse == 'u'
1422 || *regparse == 'U'
1423 )
1424 {
1425 /* TODO(RE) This needs more testing */
1426 startc = coll_get_char();
1427 got_coll_char = TRUE;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001428 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001429 }
1430 else
1431 {
1432 /* \r,\t,\e,\b */
1433 startc = backslash_trans(*regparse);
1434 }
1435 }
1436
1437 /* Normal printable char */
1438 if (startc == -1)
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001439 startc = PTR2CHAR(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440
1441 /* Previous char was '-', so this char is end of range. */
1442 if (emit_range)
1443 {
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001444 endc = startc;
1445 startc = oldstartc;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001446 if (startc > endc)
1447 EMSG_RET_FAIL(_(e_invrange));
Bram Moolenaar417bad22013-06-07 14:08:30 +02001448
1449 if (endc > startc + 2)
1450 {
1451 /* Emit a range instead of the sequence of
1452 * individual characters. */
1453 if (startc == 0)
1454 /* \x00 is translated to \x0a, start at \x01. */
1455 EMIT(1);
1456 else
1457 --post_ptr; /* remove NFA_CONCAT */
1458 EMIT(endc);
1459 EMIT(NFA_RANGE);
1460 EMIT(NFA_CONCAT);
1461 }
1462 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001463#ifdef FEAT_MBYTE
Bram Moolenaar417bad22013-06-07 14:08:30 +02001464 if (has_mbyte && ((*mb_char2len)(startc) > 1
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001465 || (*mb_char2len)(endc) > 1))
1466 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001467 /* Emit the characters in the range.
1468 * "startc" was already emitted, so skip it.
1469 * */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001470 for (c = startc + 1; c <= endc; c++)
1471 {
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001472 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001473 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001474 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001475 }
1476 else
1477#endif
1478 {
1479#ifdef EBCDIC
1480 int alpha_only = FALSE;
1481
1482 /* for alphabetical range skip the gaps
1483 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
1484 if (isalpha(startc) && isalpha(endc))
1485 alpha_only = TRUE;
1486#endif
1487 /* Emit the range. "startc" was already emitted, so
1488 * skip it. */
1489 for (c = startc + 1; c <= endc; c++)
1490#ifdef EBCDIC
1491 if (!alpha_only || isalpha(startc))
1492#endif
1493 {
1494 EMIT(c);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001495 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001496 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001497 }
Bram Moolenaar75d7a062013-06-01 13:24:24 +02001498 emit_range = FALSE;
1499 startc = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001500 }
1501 else
1502 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02001503 /* This char (startc) is not part of a range. Just
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001504 * emit it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001505 * Normally, simply emit startc. But if we get char
1506 * code=0 from a collating char, then replace it with
1507 * 0x0a.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001508 * This is needed to completely mimic the behaviour of
Bram Moolenaar417bad22013-06-07 14:08:30 +02001509 * the backtracking engine. */
1510 if (startc == NFA_NEWL)
1511 {
1512 /* Line break can't be matched as part of the
1513 * collection, add an OR below. But not for negated
1514 * range. */
1515 if (!negated)
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001516 extra = NFA_ADD_NL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02001517 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001518 else
Bram Moolenaar417bad22013-06-07 14:08:30 +02001519 {
1520 if (got_coll_char == TRUE && startc == 0)
1521 EMIT(0x0a);
1522 else
1523 EMIT(startc);
1524 EMIT(NFA_CONCAT);
1525 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001526 }
1527
Bram Moolenaar51a29832013-05-28 22:30:35 +02001528 mb_ptr_adv(regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001529 } /* while (p < endp) */
1530
Bram Moolenaar51a29832013-05-28 22:30:35 +02001531 mb_ptr_back(old_regparse, regparse);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001532 if (*regparse == '-') /* if last, '-' is just a char */
1533 {
1534 EMIT('-');
Bram Moolenaar417bad22013-06-07 14:08:30 +02001535 EMIT(NFA_CONCAT);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001536 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001537
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001538 /* skip the trailing ] */
1539 regparse = endp;
Bram Moolenaar51a29832013-05-28 22:30:35 +02001540 mb_ptr_adv(regparse);
Bram Moolenaar417bad22013-06-07 14:08:30 +02001541
1542 /* Mark end of the collection. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001543 if (negated == TRUE)
Bram Moolenaar417bad22013-06-07 14:08:30 +02001544 EMIT(NFA_END_NEG_COLL);
1545 else
1546 EMIT(NFA_END_COLL);
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001547
1548 /* \_[] also matches \n but it's not negated */
Bram Moolenaar1cfad522013-08-14 12:06:49 +02001549 if (extra == NFA_ADD_NL)
Bram Moolenaarbad704f2013-05-30 11:51:08 +02001550 {
1551 EMIT(reg_string ? NL : NFA_NEWL);
1552 EMIT(NFA_OR);
1553 }
1554
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001555 return OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001556 } /* if exists closing ] */
1557
1558 if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001559 EMSG_RET_FAIL(_(e_missingbracket));
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001560 /* FALLTHROUGH */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001562 default:
1563 {
1564#ifdef FEAT_MBYTE
1565 int plen;
1566
1567nfa_do_multibyte:
Bram Moolenaar47196582013-05-25 22:04:23 +02001568 /* plen is length of current char with composing chars */
1569 if (enc_utf8 && ((*mb_char2len)(c)
1570 != (plen = (*mb_ptr2len)(old_regparse))
1571 || utf_iscomposing(c)))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001572 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02001573 int i = 0;
1574
Bram Moolenaar56d58d52013-05-25 14:42:03 +02001575 /* A base character plus composing characters, or just one
1576 * or more composing characters.
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001577 * This requires creating a separate atom as if enclosing
1578 * the characters in (), where NFA_COMPOSING is the ( and
1579 * NFA_END_COMPOSING is the ). Note that right now we are
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001580 * building the postfix form, not the NFA itself;
1581 * a composing char could be: a, b, c, NFA_COMPOSING
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001582 * where 'b' and 'c' are chars with codes > 256. */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001583 for (;;)
1584 {
1585 EMIT(c);
1586 if (i > 0)
1587 EMIT(NFA_CONCAT);
Bram Moolenaarfad8de02013-05-24 23:10:50 +02001588 if ((i += utf_char2len(c)) >= plen)
Bram Moolenaar3c577f22013-05-24 21:59:54 +02001589 break;
1590 c = utf_ptr2char(old_regparse + i);
1591 }
1592 EMIT(NFA_COMPOSING);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001593 regparse = old_regparse + plen;
1594 }
1595 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001596#endif
1597 {
1598 c = no_Magic(c);
1599 EMIT(c);
1600 }
1601 return OK;
1602 }
1603 }
1604
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001605 return OK;
1606}
1607
1608/*
1609 * Parse something followed by possible [*+=].
1610 *
1611 * A piece is an atom, possibly followed by a multi, an indication of how many
1612 * times the atom can be matched. Example: "a*" matches any sequence of "a"
1613 * characters: "", "a", "aa", etc.
1614 *
1615 * piece ::= atom
1616 * or atom multi
1617 */
1618 static int
1619nfa_regpiece()
1620{
1621 int i;
1622 int op;
1623 int ret;
1624 long minval, maxval;
1625 int greedy = TRUE; /* Braces are prefixed with '-' ? */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001626 parse_state_T old_state;
1627 parse_state_T new_state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001628 int c2;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001629 int old_post_pos;
1630 int my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001631 int quest;
1632
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001633 /* Save the current parse state, so that we can use it if <atom>{m,n} is
1634 * next. */
1635 save_parse_state(&old_state);
1636
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001637 /* store current pos in the postfix form, for \{m,n} involving 0s */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001638 my_post_start = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001639
1640 ret = nfa_regatom();
1641 if (ret == FAIL)
1642 return FAIL; /* cascaded error */
1643
1644 op = peekchr();
1645 if (re_multi_type(op) == NOT_MULTI)
1646 return OK;
1647
1648 skipchr();
1649 switch (op)
1650 {
1651 case Magic('*'):
1652 EMIT(NFA_STAR);
1653 break;
1654
1655 case Magic('+'):
1656 /*
1657 * Trick: Normally, (a*)\+ would match the whole input "aaa". The
1658 * first and only submatch would be "aaa". But the backtracking
1659 * engine interprets the plus as "try matching one more time", and
1660 * a* matches a second time at the end of the input, the empty
1661 * string.
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02001662 * The submatch will be the empty string.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001663 *
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001664 * In order to be consistent with the old engine, we replace
1665 * <atom>+ with <atom><atom>*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001666 */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001667 restore_parse_state(&old_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001668 curchr = -1;
1669 if (nfa_regatom() == FAIL)
1670 return FAIL;
1671 EMIT(NFA_STAR);
1672 EMIT(NFA_CONCAT);
1673 skipchr(); /* skip the \+ */
1674 break;
1675
1676 case Magic('@'):
Bram Moolenaar61602c52013-06-01 19:54:43 +02001677 c2 = getdecchrs();
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001678 op = no_Magic(getchr());
Bram Moolenaar61602c52013-06-01 19:54:43 +02001679 i = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001680 switch(op)
1681 {
1682 case '=':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001683 /* \@= */
1684 i = NFA_PREV_ATOM_NO_WIDTH;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001685 break;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001686 case '!':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001687 /* \@! */
1688 i = NFA_PREV_ATOM_NO_WIDTH_NEG;
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02001689 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001690 case '<':
Bram Moolenaar61602c52013-06-01 19:54:43 +02001691 op = no_Magic(getchr());
1692 if (op == '=')
1693 /* \@<= */
1694 i = NFA_PREV_ATOM_JUST_BEFORE;
1695 else if (op == '!')
1696 /* \@<! */
1697 i = NFA_PREV_ATOM_JUST_BEFORE_NEG;
1698 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001699 case '>':
Bram Moolenaar87953742013-06-05 18:52:40 +02001700 /* \@> */
1701 i = NFA_PREV_ATOM_LIKE_PATTERN;
1702 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001703 }
Bram Moolenaar61602c52013-06-01 19:54:43 +02001704 if (i == 0)
1705 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02001706 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op);
1707 return FAIL;
1708 }
1709 EMIT(i);
1710 if (i == NFA_PREV_ATOM_JUST_BEFORE
1711 || i == NFA_PREV_ATOM_JUST_BEFORE_NEG)
1712 EMIT(c2);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001713 break;
1714
1715 case Magic('?'):
1716 case Magic('='):
1717 EMIT(NFA_QUEST);
1718 break;
1719
1720 case Magic('{'):
1721 /* a{2,5} will expand to 'aaa?a?a?'
1722 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy
1723 * version of '?'
1724 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the
1725 * parenthesis have the same id
1726 */
1727
1728 greedy = TRUE;
1729 c2 = peekchr();
1730 if (c2 == '-' || c2 == Magic('-'))
1731 {
1732 skipchr();
1733 greedy = FALSE;
1734 }
1735 if (!read_limits(&minval, &maxval))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001736 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits"));
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02001737
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001738 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to
1739 * <atom>* */
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001740 if (minval == 0 && maxval == MAX_LIMIT)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001741 {
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001742 if (greedy)
1743 /* \{}, \{0,} */
1744 EMIT(NFA_STAR);
1745 else
1746 /* \{-}, \{-0,} */
1747 EMIT(NFA_STAR_NONGREEDY);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001748 break;
1749 }
1750
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001751 /* Special case: x{0} or x{-0} */
1752 if (maxval == 0)
1753 {
1754 /* Ignore result of previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001755 post_ptr = post_start + my_post_start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001756 /* NFA_SKIP_CHAR has 0-length and works everywhere */
1757 EMIT(NFA_SKIP_CHAR);
1758 return OK;
1759 }
1760
1761 /* Ignore previous call to nfa_regatom() */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001762 post_ptr = post_start + my_post_start;
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001763 /* Save parse state after the repeated atom and the \{} */
1764 save_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001765
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001766 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY);
1767 for (i = 0; i < maxval; i++)
1768 {
1769 /* Goto beginning of the repeated atom */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001770 restore_parse_state(&old_state);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001771 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001772 if (nfa_regatom() == FAIL)
1773 return FAIL;
1774 /* after "minval" times, atoms are optional */
1775 if (i + 1 > minval)
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001776 {
1777 if (maxval == MAX_LIMIT)
Bram Moolenaar36b3a012013-06-01 12:40:20 +02001778 {
1779 if (greedy)
1780 EMIT(NFA_STAR);
1781 else
1782 EMIT(NFA_STAR_NONGREEDY);
1783 }
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001784 else
1785 EMIT(quest);
1786 }
Bram Moolenaar16299b52013-05-30 18:45:23 +02001787 if (old_post_pos != my_post_start)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001788 EMIT(NFA_CONCAT);
Bram Moolenaar54dafde2013-05-31 23:18:00 +02001789 if (i + 1 > minval && maxval == MAX_LIMIT)
1790 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001791 }
1792
1793 /* Go to just after the repeated atom and the \{} */
Bram Moolenaar3737fc12013-06-01 14:42:56 +02001794 restore_parse_state(&new_state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001795 curchr = -1;
1796
1797 break;
1798
1799
1800 default:
1801 break;
1802 } /* end switch */
1803
1804 if (re_multi_type(peekchr()) != NOT_MULTI)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001805 /* Can't have a multi follow a multi. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001806 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001807
1808 return OK;
1809}
1810
1811/*
1812 * Parse one or more pieces, concatenated. It matches a match for the
1813 * first piece, followed by a match for the second piece, etc. Example:
1814 * "f[0-9]b", first matches "f", then a digit and then "b".
1815 *
1816 * concat ::= piece
1817 * or piece piece
1818 * or piece piece piece
1819 * etc.
1820 */
1821 static int
1822nfa_regconcat()
1823{
1824 int cont = TRUE;
1825 int first = TRUE;
1826
1827 while (cont)
1828 {
1829 switch (peekchr())
1830 {
1831 case NUL:
1832 case Magic('|'):
1833 case Magic('&'):
1834 case Magic(')'):
1835 cont = FALSE;
1836 break;
1837
1838 case Magic('Z'):
1839#ifdef FEAT_MBYTE
1840 regflags |= RF_ICOMBINE;
1841#endif
1842 skipchr_keepstart();
1843 break;
1844 case Magic('c'):
1845 regflags |= RF_ICASE;
1846 skipchr_keepstart();
1847 break;
1848 case Magic('C'):
1849 regflags |= RF_NOICASE;
1850 skipchr_keepstart();
1851 break;
1852 case Magic('v'):
1853 reg_magic = MAGIC_ALL;
1854 skipchr_keepstart();
1855 curchr = -1;
1856 break;
1857 case Magic('m'):
1858 reg_magic = MAGIC_ON;
1859 skipchr_keepstart();
1860 curchr = -1;
1861 break;
1862 case Magic('M'):
1863 reg_magic = MAGIC_OFF;
1864 skipchr_keepstart();
1865 curchr = -1;
1866 break;
1867 case Magic('V'):
1868 reg_magic = MAGIC_NONE;
1869 skipchr_keepstart();
1870 curchr = -1;
1871 break;
1872
1873 default:
1874 if (nfa_regpiece() == FAIL)
1875 return FAIL;
1876 if (first == FALSE)
1877 EMIT(NFA_CONCAT);
1878 else
1879 first = FALSE;
1880 break;
1881 }
1882 }
1883
1884 return OK;
1885}
1886
1887/*
1888 * Parse a branch, one or more concats, separated by "\&". It matches the
1889 * last concat, but only if all the preceding concats also match at the same
1890 * position. Examples:
1891 * "foobeep\&..." matches "foo" in "foobeep".
1892 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob"
1893 *
1894 * branch ::= concat
1895 * or concat \& concat
1896 * or concat \& concat \& concat
1897 * etc.
1898 */
1899 static int
1900nfa_regbranch()
1901{
1902 int ch;
Bram Moolenaar16299b52013-05-30 18:45:23 +02001903 int old_post_pos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001904
Bram Moolenaar16299b52013-05-30 18:45:23 +02001905 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001906
1907 /* First branch, possibly the only one */
1908 if (nfa_regconcat() == FAIL)
1909 return FAIL;
1910
1911 ch = peekchr();
1912 /* Try next concats */
1913 while (ch == Magic('&'))
1914 {
1915 skipchr();
1916 EMIT(NFA_NOPEN);
1917 EMIT(NFA_PREV_ATOM_NO_WIDTH);
Bram Moolenaar16299b52013-05-30 18:45:23 +02001918 old_post_pos = (int)(post_ptr - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001919 if (nfa_regconcat() == FAIL)
1920 return FAIL;
1921 /* if concat is empty, skip a input char. But do emit a node */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001922 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001923 EMIT(NFA_SKIP_CHAR);
1924 EMIT(NFA_CONCAT);
1925 ch = peekchr();
1926 }
1927
1928 /* Even if a branch is empty, emit one node for it */
Bram Moolenaar16299b52013-05-30 18:45:23 +02001929 if (old_post_pos == (int)(post_ptr - post_start))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001930 EMIT(NFA_SKIP_CHAR);
1931
1932 return OK;
1933}
1934
1935/*
1936 * Parse a pattern, one or more branches, separated by "\|". It matches
1937 * anything that matches one of the branches. Example: "foo\|beep" matches
1938 * "foo" and matches "beep". If more than one branch matches, the first one
1939 * is used.
1940 *
1941 * pattern ::= branch
1942 * or branch \| branch
1943 * or branch \| branch \| branch
1944 * etc.
1945 */
1946 static int
1947nfa_reg(paren)
1948 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1949{
1950 int parno = 0;
1951
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001952 if (paren == REG_PAREN)
1953 {
1954 if (regnpar >= NSUBEXP) /* Too many `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001955 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('"));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001956 parno = regnpar++;
1957 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001958#ifdef FEAT_SYN_HL
1959 else if (paren == REG_ZPAREN)
1960 {
1961 /* Make a ZOPEN node. */
1962 if (regnzpar >= NSUBEXP)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001963 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z("));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02001964 parno = regnzpar++;
1965 }
1966#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001967
1968 if (nfa_regbranch() == FAIL)
1969 return FAIL; /* cascaded error */
1970
1971 while (peekchr() == Magic('|'))
1972 {
1973 skipchr();
1974 if (nfa_regbranch() == FAIL)
1975 return FAIL; /* cascaded error */
1976 EMIT(NFA_OR);
1977 }
1978
1979 /* Check for proper termination. */
1980 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1981 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001982 if (paren == REG_NPAREN)
1983 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
1984 else
1985 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
1986 }
1987 else if (paren == REG_NOPAREN && peekchr() != NUL)
1988 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001989 if (peekchr() == Magic(')'))
1990 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
1991 else
1992 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error"));
1993 }
1994 /*
1995 * Here we set the flag allowing back references to this set of
1996 * parentheses.
1997 */
1998 if (paren == REG_PAREN)
1999 {
2000 had_endbrace[parno] = TRUE; /* have seen the close paren */
2001 EMIT(NFA_MOPEN + parno);
2002 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002003#ifdef FEAT_SYN_HL
2004 else if (paren == REG_ZPAREN)
2005 EMIT(NFA_ZOPEN + parno);
2006#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002007
2008 return OK;
2009}
2010
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002011#ifdef DEBUG
2012static char_u code[50];
2013
2014 static void
2015nfa_set_code(c)
2016 int c;
2017{
2018 int addnl = FALSE;
2019
2020 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL)
2021 {
2022 addnl = TRUE;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002023 c -= NFA_ADD_NL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002024 }
2025
2026 STRCPY(code, "");
2027 switch (c)
2028 {
2029 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break;
2030 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break;
2031 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break;
2032 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break;
2033 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break;
2034 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break;
2035
Bram Moolenaar5714b802013-05-28 22:03:20 +02002036 case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break;
2037 case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break;
2038 case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break;
2039 case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break;
2040 case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break;
2041 case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break;
2042 case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break;
2043 case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break;
2044 case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002045#ifdef FEAT_SYN_HL
2046 case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break;
2047 case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break;
2048 case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break;
2049 case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break;
2050 case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break;
2051 case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break;
2052 case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break;
2053 case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break;
2054 case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break;
2055#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02002056 case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break;
2057
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002058 case NFA_PREV_ATOM_NO_WIDTH:
2059 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break;
Bram Moolenaar423532e2013-05-29 21:14:42 +02002060 case NFA_PREV_ATOM_NO_WIDTH_NEG:
2061 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002062 case NFA_PREV_ATOM_JUST_BEFORE:
2063 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break;
2064 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
2065 STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002066 case NFA_PREV_ATOM_LIKE_PATTERN:
2067 STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break;
2068
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02002069 case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break;
2070 case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002071 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002072 case NFA_START_INVISIBLE_FIRST:
2073 STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002074 case NFA_START_INVISIBLE_NEG:
2075 STRCPY(code, "NFA_START_INVISIBLE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002076 case NFA_START_INVISIBLE_NEG_FIRST:
2077 STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break;
Bram Moolenaar61602c52013-06-01 19:54:43 +02002078 case NFA_START_INVISIBLE_BEFORE:
2079 STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002080 case NFA_START_INVISIBLE_BEFORE_FIRST:
2081 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002082 case NFA_START_INVISIBLE_BEFORE_NEG:
2083 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break;
Bram Moolenaara2947e22013-06-11 22:44:09 +02002084 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
2085 STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002086 case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002087 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break;
Bram Moolenaardecd9542013-06-07 16:31:50 +02002088 case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break;
Bram Moolenaar87953742013-06-05 18:52:40 +02002089 case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002090
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002091 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break;
2092 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02002093 case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002094
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002095 case NFA_MOPEN:
2096 case NFA_MOPEN1:
2097 case NFA_MOPEN2:
2098 case NFA_MOPEN3:
2099 case NFA_MOPEN4:
2100 case NFA_MOPEN5:
2101 case NFA_MOPEN6:
2102 case NFA_MOPEN7:
2103 case NFA_MOPEN8:
2104 case NFA_MOPEN9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002105 STRCPY(code, "NFA_MOPEN(x)");
2106 code[10] = c - NFA_MOPEN + '0';
2107 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002108 case NFA_MCLOSE:
2109 case NFA_MCLOSE1:
2110 case NFA_MCLOSE2:
2111 case NFA_MCLOSE3:
2112 case NFA_MCLOSE4:
2113 case NFA_MCLOSE5:
2114 case NFA_MCLOSE6:
2115 case NFA_MCLOSE7:
2116 case NFA_MCLOSE8:
2117 case NFA_MCLOSE9:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002118 STRCPY(code, "NFA_MCLOSE(x)");
2119 code[11] = c - NFA_MCLOSE + '0';
2120 break;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02002121#ifdef FEAT_SYN_HL
2122 case NFA_ZOPEN:
2123 case NFA_ZOPEN1:
2124 case NFA_ZOPEN2:
2125 case NFA_ZOPEN3:
2126 case NFA_ZOPEN4:
2127 case NFA_ZOPEN5:
2128 case NFA_ZOPEN6:
2129 case NFA_ZOPEN7:
2130 case NFA_ZOPEN8:
2131 case NFA_ZOPEN9:
2132 STRCPY(code, "NFA_ZOPEN(x)");
2133 code[10] = c - NFA_ZOPEN + '0';
2134 break;
2135 case NFA_ZCLOSE:
2136 case NFA_ZCLOSE1:
2137 case NFA_ZCLOSE2:
2138 case NFA_ZCLOSE3:
2139 case NFA_ZCLOSE4:
2140 case NFA_ZCLOSE5:
2141 case NFA_ZCLOSE6:
2142 case NFA_ZCLOSE7:
2143 case NFA_ZCLOSE8:
2144 case NFA_ZCLOSE9:
2145 STRCPY(code, "NFA_ZCLOSE(x)");
2146 code[11] = c - NFA_ZCLOSE + '0';
2147 break;
2148#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002149 case NFA_EOL: STRCPY(code, "NFA_EOL "); break;
2150 case NFA_BOL: STRCPY(code, "NFA_BOL "); break;
2151 case NFA_EOW: STRCPY(code, "NFA_EOW "); break;
2152 case NFA_BOW: STRCPY(code, "NFA_BOW "); break;
Bram Moolenaar4b780632013-05-31 22:14:52 +02002153 case NFA_EOF: STRCPY(code, "NFA_EOF "); break;
2154 case NFA_BOF: STRCPY(code, "NFA_BOF "); break;
Bram Moolenaar044aa292013-06-04 21:27:38 +02002155 case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break;
2156 case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break;
2157 case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break;
2158 case NFA_COL: STRCPY(code, "NFA_COL "); break;
2159 case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break;
2160 case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break;
2161 case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break;
2162 case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break;
2163 case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break;
2164 case NFA_MARK: STRCPY(code, "NFA_MARK "); break;
2165 case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break;
2166 case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break;
2167 case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break;
2168 case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break;
2169
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002170 case NFA_STAR: STRCPY(code, "NFA_STAR "); break;
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002171 case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break;
2172 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break;
2173 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002174 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break;
2175 case NFA_OR: STRCPY(code, "NFA_OR"); break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002176
2177 case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break;
2178 case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break;
2179 case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break;
2180 case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break;
2181 case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break;
2182 case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break;
2183 case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break;
2184
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002185 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break;
2186 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break;
2187 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break;
2188 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break;
2189 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break;
2190 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break;
2191 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break;
2192 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break;
2193 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break;
2194 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break;
2195 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break;
2196 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break;
2197 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break;
2198 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break;
2199 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break;
2200 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break;
2201
2202 case NFA_ANY: STRCPY(code, "NFA_ANY"); break;
2203 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break;
2204 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break;
2205 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break;
2206 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break;
2207 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break;
2208 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break;
2209 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break;
2210 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break;
2211 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break;
2212 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break;
2213 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break;
2214 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break;
2215 case NFA_HEX: STRCPY(code, "NFA_HEX"); break;
2216 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break;
2217 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break;
2218 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break;
2219 case NFA_WORD: STRCPY(code, "NFA_WORD"); break;
2220 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break;
2221 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break;
2222 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break;
2223 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break;
2224 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break;
2225 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break;
2226 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break;
2227 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break;
2228 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break;
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002229 case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break;
2230 case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break;
2231 case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break;
2232 case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002233
2234 default:
2235 STRCPY(code, "CHAR(x)");
2236 code[5] = c;
2237 }
2238
2239 if (addnl == TRUE)
2240 STRCAT(code, " + NEWLINE ");
2241
2242}
2243
2244#ifdef ENABLE_LOG
2245static FILE *log_fd;
2246
2247/*
2248 * Print the postfix notation of the current regexp.
2249 */
2250 static void
2251nfa_postfix_dump(expr, retval)
2252 char_u *expr;
2253 int retval;
2254{
2255 int *p;
2256 FILE *f;
2257
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002258 f = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002259 if (f != NULL)
2260 {
2261 fprintf(f, "\n-------------------------\n");
2262 if (retval == FAIL)
2263 fprintf(f, ">>> NFA engine failed ... \n");
2264 else if (retval == OK)
2265 fprintf(f, ">>> NFA engine succeeded !\n");
2266 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr);
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002267 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002268 {
2269 nfa_set_code(*p);
2270 fprintf(f, "%s, ", code);
2271 }
2272 fprintf(f, "\"\nPostfix notation (int): ");
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02002273 for (p = post_start; *p && p < post_ptr; p++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002274 fprintf(f, "%d ", *p);
2275 fprintf(f, "\n\n");
2276 fclose(f);
2277 }
2278}
2279
2280/*
2281 * Print the NFA starting with a root node "state".
2282 */
2283 static void
Bram Moolenaar152e7892013-05-25 12:28:11 +02002284nfa_print_state(debugf, state)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002285 FILE *debugf;
2286 nfa_state_T *state;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002287{
Bram Moolenaar152e7892013-05-25 12:28:11 +02002288 garray_T indent;
2289
2290 ga_init2(&indent, 1, 64);
2291 ga_append(&indent, '\0');
2292 nfa_print_state2(debugf, state, &indent);
2293 ga_clear(&indent);
2294}
2295
2296 static void
2297nfa_print_state2(debugf, state, indent)
2298 FILE *debugf;
2299 nfa_state_T *state;
2300 garray_T *indent;
2301{
2302 char_u *p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002303
2304 if (state == NULL)
2305 return;
2306
2307 fprintf(debugf, "(%2d)", abs(state->id));
Bram Moolenaar152e7892013-05-25 12:28:11 +02002308
2309 /* Output indent */
2310 p = (char_u *)indent->ga_data;
2311 if (indent->ga_len >= 3)
2312 {
2313 int last = indent->ga_len - 3;
2314 char_u save[2];
2315
2316 STRNCPY(save, &p[last], 2);
2317 STRNCPY(&p[last], "+-", 2);
2318 fprintf(debugf, " %s", p);
2319 STRNCPY(&p[last], save, 2);
2320 }
2321 else
2322 fprintf(debugf, " %s", p);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002323
2324 nfa_set_code(state->c);
Bram Moolenaardecd9542013-06-07 16:31:50 +02002325 fprintf(debugf, "%s (%d) (id=%d) val=%d\n",
Bram Moolenaar417bad22013-06-07 14:08:30 +02002326 code,
2327 state->c,
2328 abs(state->id),
2329 state->val);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002330 if (state->id < 0)
2331 return;
2332
2333 state->id = abs(state->id) * -1;
Bram Moolenaar152e7892013-05-25 12:28:11 +02002334
2335 /* grow indent for state->out */
2336 indent->ga_len -= 1;
2337 if (state->out1)
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002338 ga_concat(indent, (char_u *)"| ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002339 else
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002340 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002341 ga_append(indent, '\0');
2342
2343 nfa_print_state2(debugf, state->out, indent);
2344
2345 /* replace last part of indent for state->out1 */
2346 indent->ga_len -= 3;
Bram Moolenaarf47ca632013-05-25 15:31:05 +02002347 ga_concat(indent, (char_u *)" ");
Bram Moolenaar152e7892013-05-25 12:28:11 +02002348 ga_append(indent, '\0');
2349
2350 nfa_print_state2(debugf, state->out1, indent);
2351
2352 /* shrink indent */
2353 indent->ga_len -= 3;
2354 ga_append(indent, '\0');
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002355}
2356
2357/*
2358 * Print the NFA state machine.
2359 */
2360 static void
2361nfa_dump(prog)
2362 nfa_regprog_T *prog;
2363{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002364 FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002365
2366 if (debugf != NULL)
2367 {
Bram Moolenaar152e7892013-05-25 12:28:11 +02002368 nfa_print_state(debugf, prog->start);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002369
Bram Moolenaar473de612013-06-08 18:19:48 +02002370 if (prog->reganch)
2371 fprintf(debugf, "reganch: %d\n", prog->reganch);
2372 if (prog->regstart != NUL)
2373 fprintf(debugf, "regstart: %c (decimal: %d)\n",
2374 prog->regstart, prog->regstart);
2375 if (prog->match_text != NULL)
2376 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text);
Bram Moolenaard89616e2013-06-06 18:46:06 +02002377
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002378 fclose(debugf);
2379 }
2380}
2381#endif /* ENABLE_LOG */
2382#endif /* DEBUG */
2383
2384/*
2385 * Parse r.e. @expr and convert it into postfix form.
2386 * Return the postfix string on success, NULL otherwise.
2387 */
2388 static int *
2389re2post()
2390{
2391 if (nfa_reg(REG_NOPAREN) == FAIL)
2392 return NULL;
2393 EMIT(NFA_MOPEN);
2394 return post_start;
2395}
2396
2397/* NB. Some of the code below is inspired by Russ's. */
2398
2399/*
2400 * Represents an NFA state plus zero or one or two arrows exiting.
2401 * if c == MATCH, no arrows out; matching state.
2402 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL).
2403 * If c < 256, labeled arrow with character c to out.
2404 */
2405
2406static nfa_state_T *state_ptr; /* points to nfa_prog->state */
2407
2408/*
2409 * Allocate and initialize nfa_state_T.
2410 */
2411 static nfa_state_T *
Bram Moolenaar525666f2013-06-02 16:40:55 +02002412alloc_state(c, out, out1)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002413 int c;
2414 nfa_state_T *out;
2415 nfa_state_T *out1;
2416{
2417 nfa_state_T *s;
2418
2419 if (istate >= nstate)
2420 return NULL;
2421
2422 s = &state_ptr[istate++];
2423
2424 s->c = c;
2425 s->out = out;
2426 s->out1 = out1;
Bram Moolenaar417bad22013-06-07 14:08:30 +02002427 s->val = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002428
2429 s->id = istate;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02002430 s->lastlist[0] = 0;
2431 s->lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002432
2433 return s;
2434}
2435
2436/*
2437 * A partially built NFA without the matching state filled in.
2438 * Frag_T.start points at the start state.
2439 * Frag_T.out is a list of places that need to be set to the
2440 * next state for this fragment.
2441 */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002442
2443/* Since the out pointers in the list are always
2444 * uninitialized, we use the pointers themselves
2445 * as storage for the Ptrlists. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002446typedef union Ptrlist Ptrlist;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002447union Ptrlist
2448{
2449 Ptrlist *next;
2450 nfa_state_T *s;
2451};
2452
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002453struct Frag
2454{
Bram Moolenaar61db8b52013-05-26 17:45:49 +02002455 nfa_state_T *start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002456 Ptrlist *out;
2457};
2458typedef struct Frag Frag_T;
2459
2460static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out));
2461static Ptrlist *list1 __ARGS((nfa_state_T **outp));
2462static void patch __ARGS((Ptrlist *l, nfa_state_T *s));
2463static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2));
2464static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end));
2465static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack));
2466
2467/*
Bram Moolenaar053bb602013-05-20 13:55:21 +02002468 * Initialize a Frag_T struct and return it.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002469 */
2470 static Frag_T
2471frag(start, out)
2472 nfa_state_T *start;
2473 Ptrlist *out;
2474{
Bram Moolenaar053bb602013-05-20 13:55:21 +02002475 Frag_T n;
2476
2477 n.start = start;
2478 n.out = out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002479 return n;
2480}
2481
2482/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002483 * Create singleton list containing just outp.
2484 */
2485 static Ptrlist *
2486list1(outp)
2487 nfa_state_T **outp;
2488{
2489 Ptrlist *l;
2490
2491 l = (Ptrlist *)outp;
2492 l->next = NULL;
2493 return l;
2494}
2495
2496/*
2497 * Patch the list of states at out to point to start.
2498 */
2499 static void
2500patch(l, s)
2501 Ptrlist *l;
2502 nfa_state_T *s;
2503{
2504 Ptrlist *next;
2505
2506 for (; l; l = next)
2507 {
2508 next = l->next;
2509 l->s = s;
2510 }
2511}
2512
2513
2514/*
2515 * Join the two lists l1 and l2, returning the combination.
2516 */
2517 static Ptrlist *
2518append(l1, l2)
2519 Ptrlist *l1;
2520 Ptrlist *l2;
2521{
2522 Ptrlist *oldl1;
2523
2524 oldl1 = l1;
2525 while (l1->next)
2526 l1 = l1->next;
2527 l1->next = l2;
2528 return oldl1;
2529}
2530
2531/*
2532 * Stack used for transforming postfix form into NFA.
2533 */
2534static Frag_T empty;
2535
2536 static void
2537st_error(postfix, end, p)
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002538 int *postfix UNUSED;
2539 int *end UNUSED;
2540 int *p UNUSED;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002541{
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002542#ifdef NFA_REGEXP_ERROR_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002543 FILE *df;
2544 int *p2;
2545
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002546 df = fopen(NFA_REGEXP_ERROR_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002547 if (df)
2548 {
2549 fprintf(df, "Error popping the stack!\n");
2550#ifdef DEBUG
2551 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr);
2552#endif
2553 fprintf(df, "Postfix form is: ");
2554#ifdef DEBUG
2555 for (p2 = postfix; p2 < end; p2++)
2556 {
2557 nfa_set_code(*p2);
2558 fprintf(df, "%s, ", code);
2559 }
2560 nfa_set_code(*p);
2561 fprintf(df, "\nCurrent position is: ");
2562 for (p2 = postfix; p2 <= p; p2 ++)
2563 {
2564 nfa_set_code(*p2);
2565 fprintf(df, "%s, ", code);
2566 }
2567#else
2568 for (p2 = postfix; p2 < end; p2++)
2569 {
2570 fprintf(df, "%d, ", *p2);
2571 }
2572 fprintf(df, "\nCurrent position is: ");
2573 for (p2 = postfix; p2 <= p; p2 ++)
2574 {
2575 fprintf(df, "%d, ", *p2);
2576 }
2577#endif
2578 fprintf(df, "\n--------------------------\n");
2579 fclose(df);
2580 }
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02002581#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002582 EMSG(_("E874: (NFA) Could not pop the stack !"));
2583}
2584
2585/*
2586 * Push an item onto the stack.
2587 */
2588 static void
2589st_push(s, p, stack_end)
2590 Frag_T s;
2591 Frag_T **p;
2592 Frag_T *stack_end;
2593{
2594 Frag_T *stackp = *p;
2595
2596 if (stackp >= stack_end)
2597 return;
2598 *stackp = s;
2599 *p = *p + 1;
2600}
2601
2602/*
2603 * Pop an item from the stack.
2604 */
2605 static Frag_T
2606st_pop(p, stack)
2607 Frag_T **p;
2608 Frag_T *stack;
2609{
2610 Frag_T *stackp;
2611
2612 *p = *p - 1;
2613 stackp = *p;
2614 if (stackp < stack)
2615 return empty;
2616 return **p;
2617}
2618
2619/*
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002620 * Estimate the maximum byte length of anything matching "state".
2621 * When unknown or unlimited return -1.
2622 */
2623 static int
2624nfa_max_width(startstate, depth)
2625 nfa_state_T *startstate;
2626 int depth;
2627{
2628 int l, r;
2629 nfa_state_T *state = startstate;
2630 int len = 0;
2631
2632 /* detect looping in a NFA_SPLIT */
2633 if (depth > 4)
2634 return -1;
2635
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002636 while (state != NULL)
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002637 {
2638 switch (state->c)
2639 {
2640 case NFA_END_INVISIBLE:
2641 case NFA_END_INVISIBLE_NEG:
2642 /* the end, return what we have */
2643 return len;
2644
2645 case NFA_SPLIT:
2646 /* two alternatives, use the maximum */
2647 l = nfa_max_width(state->out, depth + 1);
2648 r = nfa_max_width(state->out1, depth + 1);
2649 if (l < 0 || r < 0)
2650 return -1;
2651 return len + (l > r ? l : r);
2652
2653 case NFA_ANY:
2654 case NFA_START_COLL:
2655 case NFA_START_NEG_COLL:
2656 /* matches some character, including composing chars */
2657#ifdef FEAT_MBYTE
2658 if (enc_utf8)
2659 len += MB_MAXBYTES;
2660 else if (has_mbyte)
2661 len += 2;
2662 else
2663#endif
2664 ++len;
2665 if (state->c != NFA_ANY)
2666 {
2667 /* skip over the characters */
2668 state = state->out1->out;
2669 continue;
2670 }
2671 break;
2672
2673 case NFA_DIGIT:
2674 case NFA_WHITE:
2675 case NFA_HEX:
2676 case NFA_OCTAL:
2677 /* ascii */
2678 ++len;
2679 break;
2680
2681 case NFA_IDENT:
2682 case NFA_SIDENT:
2683 case NFA_KWORD:
2684 case NFA_SKWORD:
2685 case NFA_FNAME:
2686 case NFA_SFNAME:
2687 case NFA_PRINT:
2688 case NFA_SPRINT:
2689 case NFA_NWHITE:
2690 case NFA_NDIGIT:
2691 case NFA_NHEX:
2692 case NFA_NOCTAL:
2693 case NFA_WORD:
2694 case NFA_NWORD:
2695 case NFA_HEAD:
2696 case NFA_NHEAD:
2697 case NFA_ALPHA:
2698 case NFA_NALPHA:
2699 case NFA_LOWER:
2700 case NFA_NLOWER:
2701 case NFA_UPPER:
2702 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02002703 case NFA_LOWER_IC:
2704 case NFA_NLOWER_IC:
2705 case NFA_UPPER_IC:
2706 case NFA_NUPPER_IC:
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002707 /* possibly non-ascii */
2708#ifdef FEAT_MBYTE
2709 if (has_mbyte)
2710 len += 3;
2711 else
2712#endif
2713 ++len;
2714 break;
2715
2716 case NFA_START_INVISIBLE:
2717 case NFA_START_INVISIBLE_NEG:
2718 case NFA_START_INVISIBLE_BEFORE:
2719 case NFA_START_INVISIBLE_BEFORE_NEG:
2720 /* zero-width, out1 points to the END state */
2721 state = state->out1->out;
2722 continue;
2723
2724 case NFA_BACKREF1:
2725 case NFA_BACKREF2:
2726 case NFA_BACKREF3:
2727 case NFA_BACKREF4:
2728 case NFA_BACKREF5:
2729 case NFA_BACKREF6:
2730 case NFA_BACKREF7:
2731 case NFA_BACKREF8:
2732 case NFA_BACKREF9:
2733#ifdef FEAT_SYN_HL
2734 case NFA_ZREF1:
2735 case NFA_ZREF2:
2736 case NFA_ZREF3:
2737 case NFA_ZREF4:
2738 case NFA_ZREF5:
2739 case NFA_ZREF6:
2740 case NFA_ZREF7:
2741 case NFA_ZREF8:
2742 case NFA_ZREF9:
2743#endif
2744 case NFA_NEWL:
2745 case NFA_SKIP:
2746 /* unknown width */
2747 return -1;
2748
2749 case NFA_BOL:
2750 case NFA_EOL:
2751 case NFA_BOF:
2752 case NFA_EOF:
2753 case NFA_BOW:
2754 case NFA_EOW:
2755 case NFA_MOPEN:
2756 case NFA_MOPEN1:
2757 case NFA_MOPEN2:
2758 case NFA_MOPEN3:
2759 case NFA_MOPEN4:
2760 case NFA_MOPEN5:
2761 case NFA_MOPEN6:
2762 case NFA_MOPEN7:
2763 case NFA_MOPEN8:
2764 case NFA_MOPEN9:
2765#ifdef FEAT_SYN_HL
2766 case NFA_ZOPEN:
2767 case NFA_ZOPEN1:
2768 case NFA_ZOPEN2:
2769 case NFA_ZOPEN3:
2770 case NFA_ZOPEN4:
2771 case NFA_ZOPEN5:
2772 case NFA_ZOPEN6:
2773 case NFA_ZOPEN7:
2774 case NFA_ZOPEN8:
2775 case NFA_ZOPEN9:
2776 case NFA_ZCLOSE:
2777 case NFA_ZCLOSE1:
2778 case NFA_ZCLOSE2:
2779 case NFA_ZCLOSE3:
2780 case NFA_ZCLOSE4:
2781 case NFA_ZCLOSE5:
2782 case NFA_ZCLOSE6:
2783 case NFA_ZCLOSE7:
2784 case NFA_ZCLOSE8:
2785 case NFA_ZCLOSE9:
2786#endif
2787 case NFA_MCLOSE:
2788 case NFA_MCLOSE1:
2789 case NFA_MCLOSE2:
2790 case NFA_MCLOSE3:
2791 case NFA_MCLOSE4:
2792 case NFA_MCLOSE5:
2793 case NFA_MCLOSE6:
2794 case NFA_MCLOSE7:
2795 case NFA_MCLOSE8:
2796 case NFA_MCLOSE9:
2797 case NFA_NOPEN:
2798 case NFA_NCLOSE:
2799
2800 case NFA_LNUM_GT:
2801 case NFA_LNUM_LT:
2802 case NFA_COL_GT:
2803 case NFA_COL_LT:
2804 case NFA_VCOL_GT:
2805 case NFA_VCOL_LT:
2806 case NFA_MARK_GT:
2807 case NFA_MARK_LT:
2808 case NFA_VISUAL:
2809 case NFA_LNUM:
2810 case NFA_CURSOR:
2811 case NFA_COL:
2812 case NFA_VCOL:
2813 case NFA_MARK:
2814
2815 case NFA_ZSTART:
2816 case NFA_ZEND:
2817 case NFA_OPT_CHARS:
2818 case NFA_SKIP_CHAR:
2819 case NFA_START_PATTERN:
2820 case NFA_END_PATTERN:
2821 case NFA_COMPOSING:
2822 case NFA_END_COMPOSING:
2823 /* zero-width */
2824 break;
2825
2826 default:
2827 if (state->c < 0)
2828 /* don't know what this is */
2829 return -1;
2830 /* normal character */
2831 len += MB_CHAR2LEN(state->c);
2832 break;
2833 }
2834
2835 /* normal way to continue */
2836 state = state->out;
2837 }
2838
Bram Moolenaarfe70acb2013-06-21 18:31:23 +02002839 /* unrecognized, "cannot happen" */
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002840 return -1;
2841}
Bram Moolenaar1e02e662013-06-08 23:26:27 +02002842
Bram Moolenaare7766ee2013-06-08 22:30:03 +02002843/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002844 * Convert a postfix form into its equivalent NFA.
2845 * Return the NFA start state on success, NULL otherwise.
2846 */
2847 static nfa_state_T *
2848post2nfa(postfix, end, nfa_calc_size)
2849 int *postfix;
2850 int *end;
2851 int nfa_calc_size;
2852{
2853 int *p;
2854 int mopen;
2855 int mclose;
2856 Frag_T *stack = NULL;
2857 Frag_T *stackp = NULL;
2858 Frag_T *stack_end = NULL;
2859 Frag_T e1;
2860 Frag_T e2;
2861 Frag_T e;
2862 nfa_state_T *s;
2863 nfa_state_T *s1;
2864 nfa_state_T *matchstate;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002865 nfa_state_T *ret = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002866
2867 if (postfix == NULL)
2868 return NULL;
2869
Bram Moolenaar053bb602013-05-20 13:55:21 +02002870#define PUSH(s) st_push((s), &stackp, stack_end)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002871#define POP() st_pop(&stackp, stack); \
2872 if (stackp < stack) \
2873 { \
2874 st_error(postfix, end, p); \
2875 return NULL; \
2876 }
2877
2878 if (nfa_calc_size == FALSE)
2879 {
2880 /* Allocate space for the stack. Max states on the stack : nstate */
Bram Moolenaar61602c52013-06-01 19:54:43 +02002881 stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002882 stackp = stack;
Bram Moolenaare3c7b862013-05-20 21:57:03 +02002883 stack_end = stack + (nstate + 1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002884 }
2885
2886 for (p = postfix; p < end; ++p)
2887 {
2888 switch (*p)
2889 {
2890 case NFA_CONCAT:
Bram Moolenaar417bad22013-06-07 14:08:30 +02002891 /* Concatenation.
2892 * Pay attention: this operator does not exist in the r.e. itself
2893 * (it is implicit, really). It is added when r.e. is translated
2894 * to postfix form in re2post(). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002895 if (nfa_calc_size == TRUE)
2896 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002897 /* nstate += 0; */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002898 break;
2899 }
2900 e2 = POP();
2901 e1 = POP();
2902 patch(e1.out, e2.start);
2903 PUSH(frag(e1.start, e2.out));
2904 break;
2905
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002906 case NFA_OR:
2907 /* Alternation */
2908 if (nfa_calc_size == TRUE)
2909 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002910 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002911 break;
2912 }
2913 e2 = POP();
2914 e1 = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002915 s = alloc_state(NFA_SPLIT, e1.start, e2.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002916 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002917 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002918 PUSH(frag(s, append(e1.out, e2.out)));
2919 break;
2920
2921 case NFA_STAR:
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002922 /* Zero or more, prefer more */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002923 if (nfa_calc_size == TRUE)
2924 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002925 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002926 break;
2927 }
2928 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002929 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002930 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002931 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002932 patch(e.out, s);
2933 PUSH(frag(s, list1(&s->out1)));
2934 break;
2935
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002936 case NFA_STAR_NONGREEDY:
2937 /* Zero or more, prefer zero */
2938 if (nfa_calc_size == TRUE)
2939 {
2940 nstate++;
2941 break;
2942 }
2943 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002944 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaar36b3a012013-06-01 12:40:20 +02002945 if (s == NULL)
2946 goto theend;
2947 patch(e.out, s);
2948 PUSH(frag(s, list1(&s->out)));
2949 break;
2950
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002951 case NFA_QUEST:
2952 /* one or zero atoms=> greedy match */
2953 if (nfa_calc_size == TRUE)
2954 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002955 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956 break;
2957 }
2958 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002959 s = alloc_state(NFA_SPLIT, e.start, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002960 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002961 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002962 PUSH(frag(s, append(e.out, list1(&s->out1))));
2963 break;
2964
2965 case NFA_QUEST_NONGREEDY:
2966 /* zero or one atoms => non-greedy match */
2967 if (nfa_calc_size == TRUE)
2968 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02002969 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002970 break;
2971 }
2972 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02002973 s = alloc_state(NFA_SPLIT, NULL, e.start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002974 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02002975 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002976 PUSH(frag(s, append(e.out, list1(&s->out))));
2977 break;
2978
Bram Moolenaar417bad22013-06-07 14:08:30 +02002979 case NFA_END_COLL:
2980 case NFA_END_NEG_COLL:
2981 /* On the stack is the sequence starting with NFA_START_COLL or
2982 * NFA_START_NEG_COLL and all possible characters. Patch it to
2983 * add the output to the start. */
2984 if (nfa_calc_size == TRUE)
2985 {
2986 nstate++;
2987 break;
2988 }
2989 e = POP();
2990 s = alloc_state(NFA_END_COLL, NULL, NULL);
2991 if (s == NULL)
2992 goto theend;
2993 patch(e.out, s);
2994 e.start->out1 = s;
2995 PUSH(frag(e.start, list1(&s->out)));
2996 break;
2997
2998 case NFA_RANGE:
2999 /* Before this are two characters, the low and high end of a
3000 * range. Turn them into two states with MIN and MAX. */
3001 if (nfa_calc_size == TRUE)
3002 {
3003 /* nstate += 0; */
3004 break;
3005 }
3006 e2 = POP();
3007 e1 = POP();
3008 e2.start->val = e2.start->c;
3009 e2.start->c = NFA_RANGE_MAX;
3010 e1.start->val = e1.start->c;
3011 e1.start->c = NFA_RANGE_MIN;
3012 patch(e1.out, e2.start);
3013 PUSH(frag(e1.start, e2.out));
3014 break;
3015
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003016 case NFA_SKIP_CHAR:
3017 /* Symbol of 0-length, Used in a repetition
3018 * with max/min count of 0 */
3019 if (nfa_calc_size == TRUE)
3020 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003021 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003022 break;
3023 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003024 s = alloc_state(NFA_SKIP_CHAR, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003025 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003026 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003027 PUSH(frag(s, list1(&s->out)));
3028 break;
3029
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003030 case NFA_OPT_CHARS:
3031 {
3032 int n;
3033
Bram Moolenaareec3e1e2013-08-01 18:38:26 +02003034 /* \%[abc] implemented as:
3035 * NFA_SPLIT
3036 * +-CHAR(a)
3037 * | +-NFA_SPLIT
3038 * | +-CHAR(b)
3039 * | | +-NFA_SPLIT
3040 * | | +-CHAR(c)
3041 * | | | +-next
3042 * | | +- next
3043 * | +- next
3044 * +- next
3045 */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003046 n = *++p; /* get number of characters */
3047 if (nfa_calc_size == TRUE)
3048 {
3049 nstate += n;
3050 break;
3051 }
Bram Moolenaarc19b4b52013-06-05 21:23:39 +02003052 s = NULL; /* avoid compiler warning */
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003053 e1.out = NULL; /* stores list with out1's */
3054 s1 = NULL; /* previous NFA_SPLIT to connect to */
3055 while (n-- > 0)
3056 {
3057 e = POP(); /* get character */
3058 s = alloc_state(NFA_SPLIT, e.start, NULL);
3059 if (s == NULL)
3060 goto theend;
3061 if (e1.out == NULL)
3062 e1 = e;
3063 patch(e.out, s1);
3064 append(e1.out, list1(&s->out1));
3065 s1 = s;
3066 }
3067 PUSH(frag(s, e1.out));
3068 break;
3069 }
3070
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003071 case NFA_PREV_ATOM_NO_WIDTH:
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003072 case NFA_PREV_ATOM_NO_WIDTH_NEG:
Bram Moolenaar61602c52013-06-01 19:54:43 +02003073 case NFA_PREV_ATOM_JUST_BEFORE:
3074 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02003075 case NFA_PREV_ATOM_LIKE_PATTERN:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003076 {
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003077 int before = (*p == NFA_PREV_ATOM_JUST_BEFORE
3078 || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG);
Bram Moolenaar87953742013-06-05 18:52:40 +02003079 int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN);
Bram Moolenaardecd9542013-06-07 16:31:50 +02003080 int start_state;
3081 int end_state;
Bram Moolenaar87953742013-06-05 18:52:40 +02003082 int n = 0;
3083 nfa_state_T *zend;
3084 nfa_state_T *skip;
3085
Bram Moolenaardecd9542013-06-07 16:31:50 +02003086 switch (*p)
Bram Moolenaar87953742013-06-05 18:52:40 +02003087 {
Bram Moolenaardecd9542013-06-07 16:31:50 +02003088 case NFA_PREV_ATOM_NO_WIDTH:
3089 start_state = NFA_START_INVISIBLE;
3090 end_state = NFA_END_INVISIBLE;
3091 break;
3092 case NFA_PREV_ATOM_NO_WIDTH_NEG:
3093 start_state = NFA_START_INVISIBLE_NEG;
3094 end_state = NFA_END_INVISIBLE_NEG;
3095 break;
3096 case NFA_PREV_ATOM_JUST_BEFORE:
3097 start_state = NFA_START_INVISIBLE_BEFORE;
3098 end_state = NFA_END_INVISIBLE;
3099 break;
3100 case NFA_PREV_ATOM_JUST_BEFORE_NEG:
3101 start_state = NFA_START_INVISIBLE_BEFORE_NEG;
3102 end_state = NFA_END_INVISIBLE_NEG;
3103 break;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02003104 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */
Bram Moolenaardecd9542013-06-07 16:31:50 +02003105 start_state = NFA_START_PATTERN;
3106 end_state = NFA_END_PATTERN;
3107 break;
Bram Moolenaar87953742013-06-05 18:52:40 +02003108 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003109
3110 if (before)
3111 n = *++p; /* get the count */
3112
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003113 /* The \@= operator: match the preceding atom with zero width.
Bram Moolenaarb06e20e2013-05-30 22:44:02 +02003114 * The \@! operator: no match for the preceding atom.
Bram Moolenaar61602c52013-06-01 19:54:43 +02003115 * The \@<= operator: match for the preceding atom.
3116 * The \@<! operator: no match for the preceding atom.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003117 * Surrounds the preceding atom with START_INVISIBLE and
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003118 * END_INVISIBLE, similarly to MOPEN. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003119
3120 if (nfa_calc_size == TRUE)
3121 {
Bram Moolenaar87953742013-06-05 18:52:40 +02003122 nstate += pattern ? 4 : 2;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003123 break;
3124 }
3125 e = POP();
Bram Moolenaar87953742013-06-05 18:52:40 +02003126 s1 = alloc_state(end_state, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003127 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003128 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003129
Bram Moolenaar87953742013-06-05 18:52:40 +02003130 s = alloc_state(start_state, e.start, s1);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003131 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003132 goto theend;
Bram Moolenaar87953742013-06-05 18:52:40 +02003133 if (pattern)
3134 {
3135 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */
3136 skip = alloc_state(NFA_SKIP, NULL, NULL);
3137 zend = alloc_state(NFA_ZEND, s1, NULL);
3138 s1->out= skip;
3139 patch(e.out, zend);
3140 PUSH(frag(s, list1(&skip->out)));
Bram Moolenaar61602c52013-06-01 19:54:43 +02003141 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003142 else
3143 {
3144 patch(e.out, s1);
3145 PUSH(frag(s, list1(&s1->out)));
Bram Moolenaare7766ee2013-06-08 22:30:03 +02003146 if (before)
3147 {
3148 if (n <= 0)
3149 /* See if we can guess the maximum width, it avoids a
3150 * lot of pointless tries. */
3151 n = nfa_max_width(e.start, 0);
3152 s->val = n; /* store the count */
3153 }
Bram Moolenaar87953742013-06-05 18:52:40 +02003154 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003155 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003156 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003157
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003158#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003159 case NFA_COMPOSING: /* char with composing char */
3160#if 0
3161 /* TODO */
3162 if (regflags & RF_ICOMBINE)
3163 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02003164 /* use the base character only */
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003165 }
3166#endif
3167 /* FALLTHROUGH */
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003168#endif
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003169
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003170 case NFA_MOPEN: /* \( \) Submatch */
3171 case NFA_MOPEN1:
3172 case NFA_MOPEN2:
3173 case NFA_MOPEN3:
3174 case NFA_MOPEN4:
3175 case NFA_MOPEN5:
3176 case NFA_MOPEN6:
3177 case NFA_MOPEN7:
3178 case NFA_MOPEN8:
3179 case NFA_MOPEN9:
3180#ifdef FEAT_SYN_HL
3181 case NFA_ZOPEN: /* \z( \) Submatch */
3182 case NFA_ZOPEN1:
3183 case NFA_ZOPEN2:
3184 case NFA_ZOPEN3:
3185 case NFA_ZOPEN4:
3186 case NFA_ZOPEN5:
3187 case NFA_ZOPEN6:
3188 case NFA_ZOPEN7:
3189 case NFA_ZOPEN8:
3190 case NFA_ZOPEN9:
3191#endif
3192 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003193 if (nfa_calc_size == TRUE)
3194 {
3195 nstate += 2;
3196 break;
3197 }
3198
3199 mopen = *p;
3200 switch (*p)
3201 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003202 case NFA_NOPEN: mclose = NFA_NCLOSE; break;
3203#ifdef FEAT_SYN_HL
3204 case NFA_ZOPEN: mclose = NFA_ZCLOSE; break;
3205 case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break;
3206 case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break;
3207 case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break;
3208 case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break;
3209 case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break;
3210 case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break;
3211 case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break;
3212 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break;
3213 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break;
3214#endif
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003215#ifdef FEAT_MBYTE
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003216 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003217#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003218 default:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003219 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220 mclose = *p + NSUBEXP;
3221 break;
3222 }
3223
3224 /* Allow "NFA_MOPEN" as a valid postfix representation for
3225 * the empty regexp "". In this case, the NFA will be
3226 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows
3227 * empty groups of parenthesis, and empty mbyte chars */
3228 if (stackp == stack)
3229 {
Bram Moolenaar525666f2013-06-02 16:40:55 +02003230 s = alloc_state(mopen, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003231 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003232 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003233 s1 = alloc_state(mclose, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003234 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003235 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003236 patch(list1(&s->out), s1);
3237 PUSH(frag(s, list1(&s1->out)));
3238 break;
3239 }
3240
3241 /* At least one node was emitted before NFA_MOPEN, so
3242 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */
3243 e = POP();
Bram Moolenaar525666f2013-06-02 16:40:55 +02003244 s = alloc_state(mopen, e.start, NULL); /* `(' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003245 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003246 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003247
Bram Moolenaar525666f2013-06-02 16:40:55 +02003248 s1 = alloc_state(mclose, NULL, NULL); /* `)' */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003249 if (s1 == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003250 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003251 patch(e.out, s1);
3252
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003253#ifdef FEAT_MBYTE
Bram Moolenaar3c577f22013-05-24 21:59:54 +02003254 if (mopen == NFA_COMPOSING)
3255 /* COMPOSING->out1 = END_COMPOSING */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003256 patch(list1(&s->out1), s1);
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02003257#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003258
3259 PUSH(frag(s, list1(&s1->out)));
3260 break;
3261
Bram Moolenaar5714b802013-05-28 22:03:20 +02003262 case NFA_BACKREF1:
3263 case NFA_BACKREF2:
3264 case NFA_BACKREF3:
3265 case NFA_BACKREF4:
3266 case NFA_BACKREF5:
3267 case NFA_BACKREF6:
3268 case NFA_BACKREF7:
3269 case NFA_BACKREF8:
3270 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003271#ifdef FEAT_SYN_HL
3272 case NFA_ZREF1:
3273 case NFA_ZREF2:
3274 case NFA_ZREF3:
3275 case NFA_ZREF4:
3276 case NFA_ZREF5:
3277 case NFA_ZREF6:
3278 case NFA_ZREF7:
3279 case NFA_ZREF8:
3280 case NFA_ZREF9:
3281#endif
Bram Moolenaar5714b802013-05-28 22:03:20 +02003282 if (nfa_calc_size == TRUE)
3283 {
3284 nstate += 2;
3285 break;
3286 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003287 s = alloc_state(*p, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003288 if (s == NULL)
3289 goto theend;
Bram Moolenaar525666f2013-06-02 16:40:55 +02003290 s1 = alloc_state(NFA_SKIP, NULL, NULL);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003291 if (s1 == NULL)
3292 goto theend;
3293 patch(list1(&s->out), s1);
3294 PUSH(frag(s, list1(&s1->out)));
3295 break;
3296
Bram Moolenaar423532e2013-05-29 21:14:42 +02003297 case NFA_LNUM:
3298 case NFA_LNUM_GT:
3299 case NFA_LNUM_LT:
3300 case NFA_VCOL:
3301 case NFA_VCOL_GT:
3302 case NFA_VCOL_LT:
3303 case NFA_COL:
3304 case NFA_COL_GT:
3305 case NFA_COL_LT:
Bram Moolenaar044aa292013-06-04 21:27:38 +02003306 case NFA_MARK:
3307 case NFA_MARK_GT:
3308 case NFA_MARK_LT:
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003309 {
3310 int n = *++p; /* lnum, col or mark name */
3311
Bram Moolenaar423532e2013-05-29 21:14:42 +02003312 if (nfa_calc_size == TRUE)
3313 {
3314 nstate += 1;
3315 break;
3316 }
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003317 s = alloc_state(p[-1], NULL, NULL);
Bram Moolenaar423532e2013-05-29 21:14:42 +02003318 if (s == NULL)
3319 goto theend;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003320 s->val = n;
Bram Moolenaar423532e2013-05-29 21:14:42 +02003321 PUSH(frag(s, list1(&s->out)));
3322 break;
Bram Moolenaard75799ab72013-06-05 11:05:17 +02003323 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02003324
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003325 case NFA_ZSTART:
3326 case NFA_ZEND:
3327 default:
3328 /* Operands */
3329 if (nfa_calc_size == TRUE)
3330 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003331 nstate++;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003332 break;
3333 }
Bram Moolenaar525666f2013-06-02 16:40:55 +02003334 s = alloc_state(*p, NULL, NULL);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003335 if (s == NULL)
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003336 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003337 PUSH(frag(s, list1(&s->out)));
3338 break;
3339
3340 } /* switch(*p) */
3341
3342 } /* for(p = postfix; *p; ++p) */
3343
3344 if (nfa_calc_size == TRUE)
3345 {
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02003346 nstate++;
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003347 goto theend; /* Return value when counting size is ignored anyway */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003348 }
3349
3350 e = POP();
3351 if (stackp != stack)
3352 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack"));
3353
3354 if (istate >= nstate)
3355 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA "));
3356
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003357 matchstate = &state_ptr[istate++]; /* the match state */
3358 matchstate->c = NFA_MATCH;
3359 matchstate->out = matchstate->out1 = NULL;
Bram Moolenaar417bad22013-06-07 14:08:30 +02003360 matchstate->id = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003361
3362 patch(e.out, matchstate);
Bram Moolenaarb09d9832013-05-21 16:28:11 +02003363 ret = e.start;
3364
3365theend:
3366 vim_free(stack);
3367 return ret;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003368
3369#undef POP1
3370#undef PUSH1
3371#undef POP2
3372#undef PUSH2
3373#undef POP
3374#undef PUSH
3375}
3376
Bram Moolenaara2947e22013-06-11 22:44:09 +02003377/*
3378 * After building the NFA program, inspect it to add optimization hints.
3379 */
3380 static void
3381nfa_postprocess(prog)
3382 nfa_regprog_T *prog;
3383{
3384 int i;
3385 int c;
3386
3387 for (i = 0; i < prog->nstate; ++i)
3388 {
3389 c = prog->state[i].c;
3390 if (c == NFA_START_INVISIBLE
3391 || c == NFA_START_INVISIBLE_NEG
3392 || c == NFA_START_INVISIBLE_BEFORE
3393 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3394 {
3395 int directly;
3396
3397 /* Do it directly when what follows is possibly the end of the
3398 * match. */
3399 if (match_follows(prog->state[i].out1->out, 0))
3400 directly = TRUE;
3401 else
3402 {
3403 int ch_invisible = failure_chance(prog->state[i].out, 0);
3404 int ch_follows = failure_chance(prog->state[i].out1->out, 0);
3405
3406 /* Postpone when the invisible match is expensive or has a
3407 * lower chance of failing. */
3408 if (c == NFA_START_INVISIBLE_BEFORE
3409 || c == NFA_START_INVISIBLE_BEFORE_NEG)
3410 {
3411 /* "before" matches are very expensive when
3412 * unbounded, always prefer what follows then,
3413 * unless what follows will always match.
3414 * Otherwise strongly prefer what follows. */
3415 if (prog->state[i].val <= 0 && ch_follows > 0)
3416 directly = FALSE;
3417 else
3418 directly = ch_follows * 10 < ch_invisible;
3419 }
3420 else
3421 {
3422 /* normal invisible, first do the one with the
3423 * highest failure chance */
3424 directly = ch_follows < ch_invisible;
3425 }
3426 }
3427 if (directly)
3428 /* switch to the _FIRST state */
3429 ++prog->state[i].c;
3430 }
3431 }
3432}
3433
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003434/****************************************************************
3435 * NFA execution code.
3436 ****************************************************************/
3437
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003438typedef struct
3439{
3440 int in_use; /* number of subexpr with useful info */
3441
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003442 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003443 union
3444 {
3445 struct multipos
3446 {
3447 lpos_T start;
3448 lpos_T end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003449 } multi[NSUBEXP];
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003450 struct linepos
3451 {
3452 char_u *start;
3453 char_u *end;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003454 } line[NSUBEXP];
3455 } list;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003456} regsub_T;
3457
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003458typedef struct
3459{
3460 regsub_T norm; /* \( .. \) matches */
3461#ifdef FEAT_SYN_HL
3462 regsub_T synt; /* \z( .. \) matches */
3463#endif
3464} regsubs_T;
3465
Bram Moolenaara2d95102013-06-04 14:23:05 +02003466/* nfa_pim_T stores a Postponed Invisible Match. */
3467typedef struct nfa_pim_S nfa_pim_T;
3468struct nfa_pim_S
3469{
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003470 int result; /* NFA_PIM_*, see below */
3471 nfa_state_T *state; /* the invisible match start state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003472 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003473 union
3474 {
3475 lpos_T pos;
3476 char_u *ptr;
3477 } end; /* where the match must end */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003478};
3479
3480/* Values for done in nfa_pim_T. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003481#define NFA_PIM_UNUSED 0 /* pim not used */
3482#define NFA_PIM_TODO 1 /* pim not done yet */
3483#define NFA_PIM_MATCH 2 /* pim executed, matches */
3484#define NFA_PIM_NOMATCH 3 /* pim executed, no match */
Bram Moolenaara2d95102013-06-04 14:23:05 +02003485
3486
Bram Moolenaar963fee22013-05-26 21:47:28 +02003487/* nfa_thread_T contains execution information of a NFA state */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003488typedef struct
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003489{
3490 nfa_state_T *state;
Bram Moolenaar5714b802013-05-28 22:03:20 +02003491 int count;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003492 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed
3493 * invisible match */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003494 regsubs_T subs; /* submatch info, only party used */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003495} nfa_thread_T;
3496
Bram Moolenaar963fee22013-05-26 21:47:28 +02003497/* nfa_list_T contains the alternative NFA execution states. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003498typedef struct
3499{
Bram Moolenaar5714b802013-05-28 22:03:20 +02003500 nfa_thread_T *t; /* allocated array of states */
Bram Moolenaar428e9872013-05-30 17:05:39 +02003501 int n; /* nr of states currently in "t" */
3502 int len; /* max nr of states in "t" */
Bram Moolenaar5714b802013-05-28 22:03:20 +02003503 int id; /* ID of the list */
Bram Moolenaar196ed142013-07-21 18:59:24 +02003504 int has_pim; /* TRUE when any state has a PIM */
Bram Moolenaar4b417062013-05-25 20:19:50 +02003505} nfa_list_T;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003506
Bram Moolenaar5714b802013-05-28 22:03:20 +02003507#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003508static void log_subsexpr __ARGS((regsubs_T *subs));
3509static void log_subexpr __ARGS((regsub_T *sub));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003510static char *pim_info __ARGS((nfa_pim_T *pim));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003511
3512 static void
3513log_subsexpr(subs)
3514 regsubs_T *subs;
3515{
3516 log_subexpr(&subs->norm);
3517# ifdef FEAT_SYN_HL
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02003518 if (nfa_has_zsubexpr)
3519 log_subexpr(&subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003520# endif
3521}
3522
Bram Moolenaar5714b802013-05-28 22:03:20 +02003523 static void
3524log_subexpr(sub)
3525 regsub_T *sub;
3526{
3527 int j;
3528
3529 for (j = 0; j < sub->in_use; j++)
3530 if (REG_MULTI)
Bram Moolenaar87953742013-06-05 18:52:40 +02003531 fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003532 j,
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02003533 sub->list.multi[j].start.col,
3534 (int)sub->list.multi[j].start.lnum,
3535 sub->list.multi[j].end.col,
3536 (int)sub->list.multi[j].end.lnum);
Bram Moolenaar5714b802013-05-28 22:03:20 +02003537 else
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003538 {
3539 char *s = (char *)sub->list.line[j].start;
3540 char *e = (char *)sub->list.line[j].end;
3541
Bram Moolenaar87953742013-06-05 18:52:40 +02003542 fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n",
Bram Moolenaar5714b802013-05-28 22:03:20 +02003543 j,
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02003544 s == NULL ? "NULL" : s,
3545 e == NULL ? "NULL" : e);
3546 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02003547}
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003548
3549 static char *
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003550pim_info(pim)
3551 nfa_pim_T *pim;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003552{
3553 static char buf[30];
3554
3555 if (pim == NULL || pim->result == NFA_PIM_UNUSED)
3556 buf[0] = NUL;
3557 else
3558 {
3559 sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col
3560 : (int)(pim->end.ptr - reginput));
3561 }
3562 return buf;
3563}
3564
Bram Moolenaar5714b802013-05-28 22:03:20 +02003565#endif
3566
Bram Moolenaar963fee22013-05-26 21:47:28 +02003567/* Used during execution: whether a match has been found. */
3568static int nfa_match;
Bram Moolenaar4b417062013-05-25 20:19:50 +02003569
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003570static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from));
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003571static void clear_sub __ARGS((regsub_T *sub));
3572static void copy_sub __ARGS((regsub_T *to, regsub_T *from));
3573static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from));
Bram Moolenaar428e9872013-05-30 17:05:39 +02003574static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2));
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02003575static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen));
Bram Moolenaar69b52452013-07-17 21:10:51 +02003576static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim));
3577static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two));
Bram Moolenaar43e02982013-06-07 17:31:29 +02003578static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs));
Bram Moolenaard05bf562013-06-30 23:24:08 +02003579static 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 +02003580static 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 +02003581
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003582/*
3583 * Copy postponed invisible match info from "from" to "to".
3584 */
3585 static void
3586copy_pim(to, from)
3587 nfa_pim_T *to;
3588 nfa_pim_T *from;
3589{
3590 to->result = from->result;
3591 to->state = from->state;
3592 copy_sub(&to->subs.norm, &from->subs.norm);
3593#ifdef FEAT_SYN_HL
3594 if (nfa_has_zsubexpr)
3595 copy_sub(&to->subs.synt, &from->subs.synt);
3596#endif
3597 to->end = from->end;
3598}
3599
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003600 static void
3601clear_sub(sub)
3602 regsub_T *sub;
3603{
3604 if (REG_MULTI)
3605 /* Use 0xff to set lnum to -1 */
3606 vim_memset(sub->list.multi, 0xff,
3607 sizeof(struct multipos) * nfa_nsubexpr);
3608 else
3609 vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr);
3610 sub->in_use = 0;
3611}
3612
3613/*
3614 * Copy the submatches from "from" to "to".
3615 */
3616 static void
3617copy_sub(to, from)
3618 regsub_T *to;
3619 regsub_T *from;
3620{
3621 to->in_use = from->in_use;
3622 if (from->in_use > 0)
3623 {
3624 /* Copy the match start and end positions. */
3625 if (REG_MULTI)
3626 mch_memmove(&to->list.multi[0],
3627 &from->list.multi[0],
3628 sizeof(struct multipos) * from->in_use);
3629 else
3630 mch_memmove(&to->list.line[0],
3631 &from->list.line[0],
3632 sizeof(struct linepos) * from->in_use);
3633 }
3634}
3635
3636/*
3637 * Like copy_sub() but exclude the main match.
3638 */
3639 static void
3640copy_sub_off(to, from)
3641 regsub_T *to;
3642 regsub_T *from;
3643{
3644 if (to->in_use < from->in_use)
3645 to->in_use = from->in_use;
3646 if (from->in_use > 1)
3647 {
3648 /* Copy the match start and end positions. */
3649 if (REG_MULTI)
3650 mch_memmove(&to->list.multi[1],
3651 &from->list.multi[1],
3652 sizeof(struct multipos) * (from->in_use - 1));
3653 else
3654 mch_memmove(&to->list.line[1],
3655 &from->list.line[1],
3656 sizeof(struct linepos) * (from->in_use - 1));
3657 }
3658}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003659
Bram Moolenaar428e9872013-05-30 17:05:39 +02003660/*
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003661 * Return TRUE if "sub1" and "sub2" have the same start positions.
Bram Moolenaar428e9872013-05-30 17:05:39 +02003662 */
3663 static int
3664sub_equal(sub1, sub2)
3665 regsub_T *sub1;
3666 regsub_T *sub2;
3667{
3668 int i;
3669 int todo;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003670 linenr_T s1;
3671 linenr_T s2;
3672 char_u *sp1;
3673 char_u *sp2;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003674
3675 todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use;
3676 if (REG_MULTI)
3677 {
3678 for (i = 0; i < todo; ++i)
3679 {
3680 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003681 s1 = sub1->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003682 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003683 s1 = -1;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003684 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003685 s2 = sub2->list.multi[i].start.lnum;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003686 else
Bram Moolenaara0169122013-06-26 18:16:58 +02003687 s2 = -1;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003688 if (s1 != s2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003689 return FALSE;
Bram Moolenaara0169122013-06-26 18:16:58 +02003690 if (s1 != -1 && sub1->list.multi[i].start.col
Bram Moolenaar428e9872013-05-30 17:05:39 +02003691 != sub2->list.multi[i].start.col)
3692 return FALSE;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003693 }
3694 }
3695 else
3696 {
3697 for (i = 0; i < todo; ++i)
3698 {
3699 if (i < sub1->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003700 sp1 = sub1->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003701 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003702 sp1 = NULL;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003703 if (i < sub2->in_use)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003704 sp2 = sub2->list.line[i].start;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003705 else
Bram Moolenaar428e9872013-05-30 17:05:39 +02003706 sp2 = NULL;
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003707 if (sp1 != sp2)
Bram Moolenaar428e9872013-05-30 17:05:39 +02003708 return FALSE;
3709 }
3710 }
3711
3712 return TRUE;
3713}
3714
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003715#ifdef ENABLE_LOG
3716 static void
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003717report_state(char *action,
3718 regsub_T *sub,
3719 nfa_state_T *state,
3720 int lid,
3721 nfa_pim_T *pim)
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003722{
3723 int col;
3724
3725 if (sub->in_use <= 0)
3726 col = -1;
3727 else if (REG_MULTI)
3728 col = sub->list.multi[0].start.col;
3729 else
3730 col = (int)(sub->list.line[0].start - regline);
3731 nfa_set_code(state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003732 fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n",
3733 action, abs(state->id), lid, state->c, code, col,
3734 pim_info(pim));
Bram Moolenaarf6de0322013-06-02 21:30:04 +02003735}
3736#endif
3737
Bram Moolenaar43e02982013-06-07 17:31:29 +02003738/*
3739 * Return TRUE if the same state is already in list "l" with the same
3740 * positions as "subs".
3741 */
3742 static int
Bram Moolenaar69b52452013-07-17 21:10:51 +02003743has_state_with_pos(l, state, subs, pim)
Bram Moolenaar43e02982013-06-07 17:31:29 +02003744 nfa_list_T *l; /* runtime state list */
3745 nfa_state_T *state; /* state to update */
3746 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaar69b52452013-07-17 21:10:51 +02003747 nfa_pim_T *pim; /* postponed match or NULL */
Bram Moolenaar43e02982013-06-07 17:31:29 +02003748{
3749 nfa_thread_T *thread;
3750 int i;
3751
3752 for (i = 0; i < l->n; ++i)
3753 {
3754 thread = &l->t[i];
3755 if (thread->state->id == state->id
3756 && sub_equal(&thread->subs.norm, &subs->norm)
3757#ifdef FEAT_SYN_HL
Bram Moolenaarc5089bb2013-06-14 21:15:25 +02003758 && (!nfa_has_zsubexpr
3759 || sub_equal(&thread->subs.synt, &subs->synt))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003760#endif
Bram Moolenaar69b52452013-07-17 21:10:51 +02003761 && pim_equal(&thread->pim, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003762 return TRUE;
3763 }
3764 return FALSE;
3765}
3766
3767/*
Bram Moolenaar69b52452013-07-17 21:10:51 +02003768 * Return TRUE if "one" and "two" are equal. That includes when both are not
3769 * set.
3770 */
3771 static int
3772pim_equal(one, two)
3773 nfa_pim_T *one;
3774 nfa_pim_T *two;
3775{
3776 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED);
3777 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED);
3778
3779 if (one_unused)
3780 /* one is unused: equal when two is also unused */
3781 return two_unused;
3782 if (two_unused)
3783 /* one is used and two is not: not equal */
3784 return FALSE;
Bram Moolenaar3f0df062013-08-14 13:34:25 +02003785 /* compare the state id */
3786 if (one->state->id != two->state->id)
3787 return FALSE;
Bram Moolenaar69b52452013-07-17 21:10:51 +02003788 /* compare the position */
3789 if (REG_MULTI)
3790 return one->end.pos.lnum == two->end.pos.lnum
3791 && one->end.pos.col == two->end.pos.col;
3792 return one->end.ptr == two->end.ptr;
3793}
3794
3795/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003796 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3797 */
3798 static int
3799match_follows(startstate, depth)
3800 nfa_state_T *startstate;
3801 int depth;
3802{
3803 nfa_state_T *state = startstate;
3804
3805 /* avoid too much recursion */
3806 if (depth > 10)
3807 return FALSE;
3808
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02003809 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003810 {
3811 switch (state->c)
3812 {
3813 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003814 case NFA_MCLOSE:
3815 case NFA_END_INVISIBLE:
3816 case NFA_END_INVISIBLE_NEG:
3817 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003818 return TRUE;
3819
3820 case NFA_SPLIT:
3821 return match_follows(state->out, depth + 1)
3822 || match_follows(state->out1, depth + 1);
3823
3824 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003825 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003826 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003827 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003828 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003829 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003830 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003831 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003832 case NFA_COMPOSING:
3833 /* skip ahead to next state */
3834 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02003835 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003836
3837 case NFA_ANY:
3838 case NFA_IDENT:
3839 case NFA_SIDENT:
3840 case NFA_KWORD:
3841 case NFA_SKWORD:
3842 case NFA_FNAME:
3843 case NFA_SFNAME:
3844 case NFA_PRINT:
3845 case NFA_SPRINT:
3846 case NFA_WHITE:
3847 case NFA_NWHITE:
3848 case NFA_DIGIT:
3849 case NFA_NDIGIT:
3850 case NFA_HEX:
3851 case NFA_NHEX:
3852 case NFA_OCTAL:
3853 case NFA_NOCTAL:
3854 case NFA_WORD:
3855 case NFA_NWORD:
3856 case NFA_HEAD:
3857 case NFA_NHEAD:
3858 case NFA_ALPHA:
3859 case NFA_NALPHA:
3860 case NFA_LOWER:
3861 case NFA_NLOWER:
3862 case NFA_UPPER:
3863 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003864 case NFA_LOWER_IC:
3865 case NFA_NLOWER_IC:
3866 case NFA_UPPER_IC:
3867 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003868 case NFA_START_COLL:
3869 case NFA_START_NEG_COLL:
3870 case NFA_NEWL:
3871 /* state will advance input */
3872 return FALSE;
3873
3874 default:
3875 if (state->c > 0)
3876 /* state will advance input */
3877 return FALSE;
3878
3879 /* Others: zero-width or possibly zero-width, might still find
3880 * a match at the same position, keep looking. */
3881 break;
3882 }
3883 state = state->out;
3884 }
3885 return FALSE;
3886}
3887
3888
3889/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003890 * Return TRUE if "state" is already in list "l".
3891 */
3892 static int
3893state_in_list(l, state, subs)
3894 nfa_list_T *l; /* runtime state list */
3895 nfa_state_T *state; /* state to update */
3896 regsubs_T *subs; /* pointers to subexpressions */
3897{
3898 if (state->lastlist[nfa_ll_index] == l->id)
3899 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02003900 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003901 return TRUE;
3902 }
3903 return FALSE;
3904}
3905
Bram Moolenaard05bf562013-06-30 23:24:08 +02003906/*
3907 * Add "state" and possibly what follows to state list ".".
3908 * Returns "subs_arg", possibly copied into temp_subs.
3909 */
3910
3911 static regsubs_T *
3912addstate(l, state, subs_arg, pim, off)
3913 nfa_list_T *l; /* runtime state list */
3914 nfa_state_T *state; /* state to update */
3915 regsubs_T *subs_arg; /* pointers to subexpressions */
3916 nfa_pim_T *pim; /* postponed look-behind match */
3917 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003918{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003919 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003920 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003921 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003922 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003923 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003924 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003925 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02003926 regsubs_T *subs = subs_arg;
3927 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003928#ifdef ENABLE_LOG
3929 int did_print = FALSE;
3930#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003931
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003932 switch (state->c)
3933 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003934 case NFA_NCLOSE:
3935 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003936 case NFA_MCLOSE1:
3937 case NFA_MCLOSE2:
3938 case NFA_MCLOSE3:
3939 case NFA_MCLOSE4:
3940 case NFA_MCLOSE5:
3941 case NFA_MCLOSE6:
3942 case NFA_MCLOSE7:
3943 case NFA_MCLOSE8:
3944 case NFA_MCLOSE9:
3945#ifdef FEAT_SYN_HL
3946 case NFA_ZCLOSE:
3947 case NFA_ZCLOSE1:
3948 case NFA_ZCLOSE2:
3949 case NFA_ZCLOSE3:
3950 case NFA_ZCLOSE4:
3951 case NFA_ZCLOSE5:
3952 case NFA_ZCLOSE6:
3953 case NFA_ZCLOSE7:
3954 case NFA_ZCLOSE8:
3955 case NFA_ZCLOSE9:
3956#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003957 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003958 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003959 case NFA_SPLIT:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003960 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003961 /* These nodes are not added themselves but their "out" and/or
3962 * "out1" may be added below. */
3963 break;
3964
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003965 case NFA_BOL:
3966 case NFA_BOF:
3967 /* "^" won't match past end-of-line, don't bother trying.
3968 * Except when at the end of the line, or when we are going to the
3969 * next line for a look-behind match. */
3970 if (reginput > regline
3971 && *reginput != NUL
3972 && (nfa_endp == NULL
3973 || !REG_MULTI
3974 || reglnum == nfa_endp->se_u.pos.lnum))
3975 goto skip_add;
3976 /* FALLTHROUGH */
3977
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003978 case NFA_MOPEN1:
3979 case NFA_MOPEN2:
3980 case NFA_MOPEN3:
3981 case NFA_MOPEN4:
3982 case NFA_MOPEN5:
3983 case NFA_MOPEN6:
3984 case NFA_MOPEN7:
3985 case NFA_MOPEN8:
3986 case NFA_MOPEN9:
3987#ifdef FEAT_SYN_HL
3988 case NFA_ZOPEN:
3989 case NFA_ZOPEN1:
3990 case NFA_ZOPEN2:
3991 case NFA_ZOPEN3:
3992 case NFA_ZOPEN4:
3993 case NFA_ZOPEN5:
3994 case NFA_ZOPEN6:
3995 case NFA_ZOPEN7:
3996 case NFA_ZOPEN8:
3997 case NFA_ZOPEN9:
3998#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003999 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02004000 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02004001 /* These nodes need to be added so that we can bail out when it
4002 * was added to this list before at the same position to avoid an
4003 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004004
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004005 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004006 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004007 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004008 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004009 * unless it is an MOPEN that is used for a backreference or
4010 * when there is a PIM. */
Bram Moolenaar196ed142013-07-21 18:59:24 +02004011 if (!nfa_has_backref && pim == NULL && !l->has_pim)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004012 {
4013skip_add:
4014#ifdef ENABLE_LOG
4015 nfa_set_code(state->c);
4016 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4017 abs(state->id), l->id, state->c, code);
4018#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004019 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004020 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004021
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004022 /* Do not add the state again when it exists with the same
4023 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004024 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004025 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004026 }
4027
Bram Moolenaara0169122013-06-26 18:16:58 +02004028 /* When there are backreferences or PIMs the number of states may
4029 * be (a lot) bigger than anticipated. */
4030 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004031 {
4032 int newlen = l->len * 3 / 2 + 50;
4033
Bram Moolenaard05bf562013-06-30 23:24:08 +02004034 if (subs != &temp_subs)
4035 {
4036 /* "subs" may point into the current array, need to make a
4037 * copy before it becomes invalid. */
4038 copy_sub(&temp_subs.norm, &subs->norm);
4039#ifdef FEAT_SYN_HL
4040 if (nfa_has_zsubexpr)
4041 copy_sub(&temp_subs.synt, &subs->synt);
4042#endif
4043 subs = &temp_subs;
4044 }
4045
Bram Moolenaar428e9872013-05-30 17:05:39 +02004046 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4047 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004048 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004049
4050 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004051 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004052 thread = &l->t[l->n++];
4053 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004054 if (pim == NULL)
4055 thread->pim.result = NFA_PIM_UNUSED;
4056 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004057 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004058 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004059 l->has_pim = TRUE;
4060 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004061 copy_sub(&thread->subs.norm, &subs->norm);
4062#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004063 if (nfa_has_zsubexpr)
4064 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004065#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004066#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004067 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004068 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004069#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004070 }
4071
4072#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004073 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004074 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004075#endif
4076 switch (state->c)
4077 {
4078 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004079 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004080 break;
4081
4082 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004083 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004084 subs = addstate(l, state->out, subs, pim, off);
4085 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004086 break;
4087
Bram Moolenaar5714b802013-05-28 22:03:20 +02004088 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089 case NFA_NOPEN:
4090 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004091 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004092 break;
4093
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004094 case NFA_MOPEN:
4095 case NFA_MOPEN1:
4096 case NFA_MOPEN2:
4097 case NFA_MOPEN3:
4098 case NFA_MOPEN4:
4099 case NFA_MOPEN5:
4100 case NFA_MOPEN6:
4101 case NFA_MOPEN7:
4102 case NFA_MOPEN8:
4103 case NFA_MOPEN9:
4104#ifdef FEAT_SYN_HL
4105 case NFA_ZOPEN:
4106 case NFA_ZOPEN1:
4107 case NFA_ZOPEN2:
4108 case NFA_ZOPEN3:
4109 case NFA_ZOPEN4:
4110 case NFA_ZOPEN5:
4111 case NFA_ZOPEN6:
4112 case NFA_ZOPEN7:
4113 case NFA_ZOPEN8:
4114 case NFA_ZOPEN9:
4115#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004116 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004117 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004118 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004119 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004120 sub = &subs->norm;
4121 }
4122#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004123 else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004124 {
4125 subidx = state->c - NFA_ZOPEN;
4126 sub = &subs->synt;
4127 }
4128#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004129 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004130 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004131 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004132 sub = &subs->norm;
4133 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004134
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004135 /* avoid compiler warnings */
4136 save_ptr = NULL;
4137 save_lpos.lnum = 0;
4138 save_lpos.col = 0;
4139
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004140 /* Set the position (with "off" added) in the subexpression. Save
4141 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004142 if (REG_MULTI)
4143 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004144 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004145 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004146 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004147 save_in_use = -1;
4148 }
4149 else
4150 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004151 save_in_use = sub->in_use;
4152 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004153 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004154 sub->list.multi[i].start.lnum = -1;
4155 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004156 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004157 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004158 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004159 if (off == -1)
4160 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004161 sub->list.multi[subidx].start.lnum = reglnum + 1;
4162 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004163 }
4164 else
4165 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004166 sub->list.multi[subidx].start.lnum = reglnum;
4167 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004168 (colnr_T)(reginput - regline + off);
4169 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004170 }
4171 else
4172 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004173 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004174 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004175 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004176 save_in_use = -1;
4177 }
4178 else
4179 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004180 save_in_use = sub->in_use;
4181 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004182 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004183 sub->list.line[i].start = NULL;
4184 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004185 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004186 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004187 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004188 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004189 }
4190
Bram Moolenaard05bf562013-06-30 23:24:08 +02004191 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004192 /* "subs" may have changed, need to set "sub" again */
4193#ifdef FEAT_SYN_HL
4194 if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9)
4195 sub = &subs->synt;
4196 else
4197#endif
4198 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004199
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004200 if (save_in_use == -1)
4201 {
4202 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004203 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004204 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004205 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004206 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004207 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004208 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004209 break;
4210
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004211 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004212 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004213 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004214 /* Do not overwrite the position set by \ze. If no \ze
4215 * encountered end will be set in nfa_regtry(). */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004216 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004217 break;
4218 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004219 case NFA_MCLOSE1:
4220 case NFA_MCLOSE2:
4221 case NFA_MCLOSE3:
4222 case NFA_MCLOSE4:
4223 case NFA_MCLOSE5:
4224 case NFA_MCLOSE6:
4225 case NFA_MCLOSE7:
4226 case NFA_MCLOSE8:
4227 case NFA_MCLOSE9:
4228#ifdef FEAT_SYN_HL
4229 case NFA_ZCLOSE:
4230 case NFA_ZCLOSE1:
4231 case NFA_ZCLOSE2:
4232 case NFA_ZCLOSE3:
4233 case NFA_ZCLOSE4:
4234 case NFA_ZCLOSE5:
4235 case NFA_ZCLOSE6:
4236 case NFA_ZCLOSE7:
4237 case NFA_ZCLOSE8:
4238 case NFA_ZCLOSE9:
4239#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004240 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004241 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004242 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004243 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004244 sub = &subs->norm;
4245 }
4246#ifdef FEAT_SYN_HL
Bram Moolenaarebefd992013-08-14 14:18:40 +02004247 else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004248 {
4249 subidx = state->c - NFA_ZCLOSE;
4250 sub = &subs->synt;
4251 }
4252#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004253 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004254 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004255 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004256 sub = &subs->norm;
4257 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004258
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004259 /* We don't fill in gaps here, there must have been an MOPEN that
4260 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004261 save_in_use = sub->in_use;
4262 if (sub->in_use <= subidx)
4263 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004264 if (REG_MULTI)
4265 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004266 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004267 if (off == -1)
4268 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004269 sub->list.multi[subidx].end.lnum = reglnum + 1;
4270 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004271 }
4272 else
4273 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004274 sub->list.multi[subidx].end.lnum = reglnum;
4275 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004276 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004277 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004278 /* avoid compiler warnings */
4279 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004280 }
4281 else
4282 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004283 save_ptr = sub->list.line[subidx].end;
4284 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004285 /* avoid compiler warnings */
4286 save_lpos.lnum = 0;
4287 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004288 }
4289
Bram Moolenaard05bf562013-06-30 23:24:08 +02004290 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarebefd992013-08-14 14:18:40 +02004291 /* "subs" may have changed, need to set "sub" again */
4292#ifdef FEAT_SYN_HL
4293 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9)
4294 sub = &subs->synt;
4295 else
4296#endif
4297 sub = &subs->norm;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004298
4299 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004300 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004301 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004302 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004303 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004304 break;
4305 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004306 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004307}
4308
4309/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004310 * Like addstate(), but the new state(s) are put at position "*ip".
4311 * Used for zero-width matches, next state to use is the added one.
4312 * This makes sure the order of states to be tried does not change, which
4313 * matters for alternatives.
4314 */
4315 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004316addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004317 nfa_list_T *l; /* runtime state list */
4318 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004319 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004320 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004321 int *ip;
4322{
4323 int tlen = l->n;
4324 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004325 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004326
4327 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004328 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004329
Bram Moolenaar4b417062013-05-25 20:19:50 +02004330 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004331 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004332 return;
4333
4334 /* re-order to put the new state at the current position */
4335 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004336 if (count == 0)
4337 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004338 if (count == 1)
4339 {
4340 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004341 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004342 }
4343 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004344 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004345 if (l->n + count - 1 >= l->len)
4346 {
4347 /* not enough space to move the new states, reallocate the list
4348 * and move the states to the right position */
4349 nfa_thread_T *newl;
4350
4351 l->len = l->len * 3 / 2 + 50;
4352 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4353 if (newl == NULL)
4354 return;
4355 mch_memmove(&(newl[0]),
4356 &(l->t[0]),
4357 sizeof(nfa_thread_T) * listidx);
4358 mch_memmove(&(newl[listidx]),
4359 &(l->t[l->n - count]),
4360 sizeof(nfa_thread_T) * count);
4361 mch_memmove(&(newl[listidx + count]),
4362 &(l->t[listidx + 1]),
4363 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4364 vim_free(l->t);
4365 l->t = newl;
4366 }
4367 else
4368 {
4369 /* make space for new states, then move them from the
4370 * end to the current position */
4371 mch_memmove(&(l->t[listidx + count]),
4372 &(l->t[listidx + 1]),
4373 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4374 mch_memmove(&(l->t[listidx]),
4375 &(l->t[l->n - 1]),
4376 sizeof(nfa_thread_T) * count);
4377 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004378 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004379 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004380 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004381}
4382
4383/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004384 * Check character class "class" against current character c.
4385 */
4386 static int
4387check_char_class(class, c)
4388 int class;
4389 int c;
4390{
4391 switch (class)
4392 {
4393 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004394 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004395 return OK;
4396 break;
4397 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004398 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004399 return OK;
4400 break;
4401 case NFA_CLASS_BLANK:
4402 if (c == ' ' || c == '\t')
4403 return OK;
4404 break;
4405 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004406 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004407 return OK;
4408 break;
4409 case NFA_CLASS_DIGIT:
4410 if (VIM_ISDIGIT(c))
4411 return OK;
4412 break;
4413 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004414 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004415 return OK;
4416 break;
4417 case NFA_CLASS_LOWER:
4418 if (MB_ISLOWER(c))
4419 return OK;
4420 break;
4421 case NFA_CLASS_PRINT:
4422 if (vim_isprintc(c))
4423 return OK;
4424 break;
4425 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004426 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004427 return OK;
4428 break;
4429 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004430 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004431 return OK;
4432 break;
4433 case NFA_CLASS_UPPER:
4434 if (MB_ISUPPER(c))
4435 return OK;
4436 break;
4437 case NFA_CLASS_XDIGIT:
4438 if (vim_isxdigit(c))
4439 return OK;
4440 break;
4441 case NFA_CLASS_TAB:
4442 if (c == '\t')
4443 return OK;
4444 break;
4445 case NFA_CLASS_RETURN:
4446 if (c == '\r')
4447 return OK;
4448 break;
4449 case NFA_CLASS_BACKSPACE:
4450 if (c == '\b')
4451 return OK;
4452 break;
4453 case NFA_CLASS_ESCAPE:
4454 if (c == '\033')
4455 return OK;
4456 break;
4457
4458 default:
4459 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004460 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4461 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004462 }
4463 return FAIL;
4464}
4465
Bram Moolenaar5714b802013-05-28 22:03:20 +02004466/*
4467 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004468 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004469 */
4470 static int
4471match_backref(sub, subidx, bytelen)
4472 regsub_T *sub; /* pointers to subexpressions */
4473 int subidx;
4474 int *bytelen; /* out: length of match in bytes */
4475{
4476 int len;
4477
4478 if (sub->in_use <= subidx)
4479 {
4480retempty:
4481 /* backref was not set, match an empty string */
4482 *bytelen = 0;
4483 return TRUE;
4484 }
4485
4486 if (REG_MULTI)
4487 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004488 if (sub->list.multi[subidx].start.lnum < 0
4489 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004490 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004491 if (sub->list.multi[subidx].start.lnum == reglnum
4492 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004493 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004494 len = sub->list.multi[subidx].end.col
4495 - sub->list.multi[subidx].start.col;
4496 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4497 reginput, &len) == 0)
4498 {
4499 *bytelen = len;
4500 return TRUE;
4501 }
4502 }
4503 else
4504 {
4505 if (match_with_backref(
4506 sub->list.multi[subidx].start.lnum,
4507 sub->list.multi[subidx].start.col,
4508 sub->list.multi[subidx].end.lnum,
4509 sub->list.multi[subidx].end.col,
4510 bytelen) == RA_MATCH)
4511 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004512 }
4513 }
4514 else
4515 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004516 if (sub->list.line[subidx].start == NULL
4517 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004518 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004519 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4520 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004521 {
4522 *bytelen = len;
4523 return TRUE;
4524 }
4525 }
4526 return FALSE;
4527}
4528
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004529#ifdef FEAT_SYN_HL
4530
4531static int match_zref __ARGS((int subidx, int *bytelen));
4532
4533/*
4534 * Check for a match with \z subexpression "subidx".
4535 * Return TRUE if it matches.
4536 */
4537 static int
4538match_zref(subidx, bytelen)
4539 int subidx;
4540 int *bytelen; /* out: length of match in bytes */
4541{
4542 int len;
4543
4544 cleanup_zsubexpr();
4545 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4546 {
4547 /* backref was not set, match an empty string */
4548 *bytelen = 0;
4549 return TRUE;
4550 }
4551
4552 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4553 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4554 {
4555 *bytelen = len;
4556 return TRUE;
4557 }
4558 return FALSE;
4559}
4560#endif
4561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004562/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004563 * Save list IDs for all NFA states of "prog" into "list".
4564 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004565 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004566 */
4567 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004568nfa_save_listids(prog, list)
4569 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004570 int *list;
4571{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004572 int i;
4573 nfa_state_T *p;
4574
4575 /* Order in the list is reverse, it's a bit faster that way. */
4576 p = &prog->state[0];
4577 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004578 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004579 list[i] = p->lastlist[1];
4580 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004581 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004582 }
4583}
4584
4585/*
4586 * Restore list IDs from "list" to all NFA states.
4587 */
4588 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004589nfa_restore_listids(prog, list)
4590 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004591 int *list;
4592{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004593 int i;
4594 nfa_state_T *p;
4595
4596 p = &prog->state[0];
4597 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004598 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004599 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004600 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004601 }
4602}
4603
Bram Moolenaar423532e2013-05-29 21:14:42 +02004604 static int
4605nfa_re_num_cmp(val, op, pos)
4606 long_u val;
4607 int op;
4608 long_u pos;
4609{
4610 if (op == 1) return pos > val;
4611 if (op == 2) return pos < val;
4612 return val == pos;
4613}
4614
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004615static 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 +02004616static 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 +02004617
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004618/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004619 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004620 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4621 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004622 */
4623 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004624recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004625 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004626 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004627 nfa_regprog_T *prog;
4628 regsubs_T *submatch;
4629 regsubs_T *m;
4630 int **listids;
4631{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004632 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004633 int save_reglnum = reglnum;
4634 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004635 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004636 save_se_T *save_nfa_endp = nfa_endp;
4637 save_se_T endpos;
4638 save_se_T *endposp = NULL;
4639 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004640 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004641
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004642 if (pim != NULL)
4643 {
4644 /* start at the position where the postponed match was */
4645 if (REG_MULTI)
4646 reginput = regline + pim->end.pos.col;
4647 else
4648 reginput = pim->end.ptr;
4649 }
4650
Bram Moolenaardecd9542013-06-07 16:31:50 +02004651 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004652 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4653 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4654 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004655 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004656 /* The recursive match must end at the current position. When "pim" is
4657 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004658 endposp = &endpos;
4659 if (REG_MULTI)
4660 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004661 if (pim == NULL)
4662 {
4663 endpos.se_u.pos.col = (int)(reginput - regline);
4664 endpos.se_u.pos.lnum = reglnum;
4665 }
4666 else
4667 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004668 }
4669 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004670 {
4671 if (pim == NULL)
4672 endpos.se_u.ptr = reginput;
4673 else
4674 endpos.se_u.ptr = pim->end.ptr;
4675 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004676
4677 /* Go back the specified number of bytes, or as far as the
4678 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02004679 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004680 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004681 if (state->val <= 0)
4682 {
4683 if (REG_MULTI)
4684 {
4685 regline = reg_getline(--reglnum);
4686 if (regline == NULL)
4687 /* can't go before the first line */
4688 regline = reg_getline(++reglnum);
4689 }
4690 reginput = regline;
4691 }
4692 else
4693 {
4694 if (REG_MULTI && (int)(reginput - regline) < state->val)
4695 {
4696 /* Not enough bytes in this line, go to end of
4697 * previous line. */
4698 regline = reg_getline(--reglnum);
4699 if (regline == NULL)
4700 {
4701 /* can't go before the first line */
4702 regline = reg_getline(++reglnum);
4703 reginput = regline;
4704 }
4705 else
4706 reginput = regline + STRLEN(regline);
4707 }
4708 if ((int)(reginput - regline) >= state->val)
4709 {
4710 reginput -= state->val;
4711#ifdef FEAT_MBYTE
4712 if (has_mbyte)
4713 reginput -= mb_head_off(regline, reginput);
4714#endif
4715 }
4716 else
4717 reginput = regline;
4718 }
4719 }
4720
Bram Moolenaarf46da702013-06-02 22:37:42 +02004721#ifdef ENABLE_LOG
4722 if (log_fd != stderr)
4723 fclose(log_fd);
4724 log_fd = NULL;
4725#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004726 /* Have to clear the lastlist field of the NFA nodes, so that
4727 * nfa_regmatch() and addstate() can run properly after recursion. */
4728 if (nfa_ll_index == 1)
4729 {
4730 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4731 * values and clear them. */
4732 if (*listids == NULL)
4733 {
4734 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4735 if (*listids == NULL)
4736 {
4737 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4738 return 0;
4739 }
4740 }
4741 nfa_save_listids(prog, *listids);
4742 need_restore = TRUE;
4743 /* any value of nfa_listid will do */
4744 }
4745 else
4746 {
4747 /* First recursive nfa_regmatch() call, switch to the second lastlist
4748 * entry. Make sure nfa_listid is different from a previous recursive
4749 * call, because some states may still have this ID. */
4750 ++nfa_ll_index;
4751 if (nfa_listid <= nfa_alt_listid)
4752 nfa_listid = nfa_alt_listid;
4753 }
4754
4755 /* Call nfa_regmatch() to check if the current concat matches at this
4756 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004757 nfa_endp = endposp;
4758 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004759
4760 if (need_restore)
4761 nfa_restore_listids(prog, *listids);
4762 else
4763 {
4764 --nfa_ll_index;
4765 nfa_alt_listid = nfa_listid;
4766 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004767
4768 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004769 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02004770 if (REG_MULTI)
4771 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004772 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004773 nfa_match = save_nfa_match;
4774 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004775 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004776
4777#ifdef ENABLE_LOG
4778 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4779 if (log_fd != NULL)
4780 {
4781 fprintf(log_fd, "****************************\n");
4782 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4783 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4784 fprintf(log_fd, "****************************\n");
4785 }
4786 else
4787 {
4788 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4789 log_fd = stderr;
4790 }
4791#endif
4792
4793 return result;
4794}
4795
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004796static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004797static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004798
4799/*
4800 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004801 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004802 * NFA_ANY: 1
4803 * specific character: 99
4804 */
4805 static int
4806failure_chance(state, depth)
4807 nfa_state_T *state;
4808 int depth;
4809{
4810 int c = state->c;
4811 int l, r;
4812
4813 /* detect looping */
4814 if (depth > 4)
4815 return 1;
4816
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004817 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004818 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004819 case NFA_SPLIT:
4820 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4821 /* avoid recursive stuff */
4822 return 1;
4823 /* two alternatives, use the lowest failure chance */
4824 l = failure_chance(state->out, depth + 1);
4825 r = failure_chance(state->out1, depth + 1);
4826 return l < r ? l : r;
4827
4828 case NFA_ANY:
4829 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004830 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004831
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004832 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004833 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004834 /* empty match works always */
4835 return 0;
4836
Bram Moolenaar44c71db2013-06-14 22:33:51 +02004837 case NFA_START_INVISIBLE:
4838 case NFA_START_INVISIBLE_FIRST:
4839 case NFA_START_INVISIBLE_NEG:
4840 case NFA_START_INVISIBLE_NEG_FIRST:
4841 case NFA_START_INVISIBLE_BEFORE:
4842 case NFA_START_INVISIBLE_BEFORE_FIRST:
4843 case NFA_START_INVISIBLE_BEFORE_NEG:
4844 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
4845 case NFA_START_PATTERN:
4846 /* recursive regmatch is expensive, use low failure chance */
4847 return 5;
4848
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004849 case NFA_BOL:
4850 case NFA_EOL:
4851 case NFA_BOF:
4852 case NFA_EOF:
4853 case NFA_NEWL:
4854 return 99;
4855
4856 case NFA_BOW:
4857 case NFA_EOW:
4858 return 90;
4859
4860 case NFA_MOPEN:
4861 case NFA_MOPEN1:
4862 case NFA_MOPEN2:
4863 case NFA_MOPEN3:
4864 case NFA_MOPEN4:
4865 case NFA_MOPEN5:
4866 case NFA_MOPEN6:
4867 case NFA_MOPEN7:
4868 case NFA_MOPEN8:
4869 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004870#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004871 case NFA_ZOPEN:
4872 case NFA_ZOPEN1:
4873 case NFA_ZOPEN2:
4874 case NFA_ZOPEN3:
4875 case NFA_ZOPEN4:
4876 case NFA_ZOPEN5:
4877 case NFA_ZOPEN6:
4878 case NFA_ZOPEN7:
4879 case NFA_ZOPEN8:
4880 case NFA_ZOPEN9:
4881 case NFA_ZCLOSE:
4882 case NFA_ZCLOSE1:
4883 case NFA_ZCLOSE2:
4884 case NFA_ZCLOSE3:
4885 case NFA_ZCLOSE4:
4886 case NFA_ZCLOSE5:
4887 case NFA_ZCLOSE6:
4888 case NFA_ZCLOSE7:
4889 case NFA_ZCLOSE8:
4890 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004891#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004892 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004893 case NFA_MCLOSE1:
4894 case NFA_MCLOSE2:
4895 case NFA_MCLOSE3:
4896 case NFA_MCLOSE4:
4897 case NFA_MCLOSE5:
4898 case NFA_MCLOSE6:
4899 case NFA_MCLOSE7:
4900 case NFA_MCLOSE8:
4901 case NFA_MCLOSE9:
4902 case NFA_NCLOSE:
4903 return failure_chance(state->out, depth + 1);
4904
4905 case NFA_BACKREF1:
4906 case NFA_BACKREF2:
4907 case NFA_BACKREF3:
4908 case NFA_BACKREF4:
4909 case NFA_BACKREF5:
4910 case NFA_BACKREF6:
4911 case NFA_BACKREF7:
4912 case NFA_BACKREF8:
4913 case NFA_BACKREF9:
4914#ifdef FEAT_SYN_HL
4915 case NFA_ZREF1:
4916 case NFA_ZREF2:
4917 case NFA_ZREF3:
4918 case NFA_ZREF4:
4919 case NFA_ZREF5:
4920 case NFA_ZREF6:
4921 case NFA_ZREF7:
4922 case NFA_ZREF8:
4923 case NFA_ZREF9:
4924#endif
4925 /* backreferences don't match in many places */
4926 return 94;
4927
4928 case NFA_LNUM_GT:
4929 case NFA_LNUM_LT:
4930 case NFA_COL_GT:
4931 case NFA_COL_LT:
4932 case NFA_VCOL_GT:
4933 case NFA_VCOL_LT:
4934 case NFA_MARK_GT:
4935 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004936 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004937 /* before/after positions don't match very often */
4938 return 85;
4939
4940 case NFA_LNUM:
4941 return 90;
4942
4943 case NFA_CURSOR:
4944 case NFA_COL:
4945 case NFA_VCOL:
4946 case NFA_MARK:
4947 /* specific positions rarely match */
4948 return 98;
4949
4950 case NFA_COMPOSING:
4951 return 95;
4952
4953 default:
4954 if (c > 0)
4955 /* character match fails often */
4956 return 95;
4957 }
4958
4959 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004960 return 50;
4961}
4962
Bram Moolenaarf46da702013-06-02 22:37:42 +02004963/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004964 * Skip until the char "c" we know a match must start with.
4965 */
4966 static int
4967skip_to_start(c, colp)
4968 int c;
4969 colnr_T *colp;
4970{
4971 char_u *s;
4972
4973 /* Used often, do some work to avoid call overhead. */
4974 if (!ireg_ic
4975#ifdef FEAT_MBYTE
4976 && !has_mbyte
4977#endif
4978 )
4979 s = vim_strbyte(regline + *colp, c);
4980 else
4981 s = cstrchr(regline + *colp, c);
4982 if (s == NULL)
4983 return FAIL;
4984 *colp = (int)(s - regline);
4985 return OK;
4986}
4987
4988/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004989 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004990 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004991 * Returns zero for no match, 1 for a match.
4992 */
4993 static long
4994find_match_text(startcol, regstart, match_text)
4995 colnr_T startcol;
4996 int regstart;
4997 char_u *match_text;
4998{
4999 colnr_T col = startcol;
5000 int c1, c2;
5001 int len1, len2;
5002 int match;
5003
5004 for (;;)
5005 {
5006 match = TRUE;
5007 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
5008 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
5009 {
5010 c1 = PTR2CHAR(match_text + len1);
5011 c2 = PTR2CHAR(regline + col + len2);
5012 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
5013 {
5014 match = FALSE;
5015 break;
5016 }
5017 len2 += MB_CHAR2LEN(c2);
5018 }
5019 if (match
5020#ifdef FEAT_MBYTE
5021 /* check that no composing char follows */
5022 && !(enc_utf8
5023 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5024#endif
5025 )
5026 {
5027 cleanup_subexpr();
5028 if (REG_MULTI)
5029 {
5030 reg_startpos[0].lnum = reglnum;
5031 reg_startpos[0].col = col;
5032 reg_endpos[0].lnum = reglnum;
5033 reg_endpos[0].col = col + len2;
5034 }
5035 else
5036 {
5037 reg_startp[0] = regline + col;
5038 reg_endp[0] = regline + col + len2;
5039 }
5040 return 1L;
5041 }
5042
5043 /* Try finding regstart after the current match. */
5044 col += MB_CHAR2LEN(regstart); /* skip regstart */
5045 if (skip_to_start(regstart, &col) == FAIL)
5046 break;
5047 }
5048 return 0L;
5049}
5050
5051/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005052 * Main matching routine.
5053 *
5054 * Run NFA to determine whether it matches reginput.
5055 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005056 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005057 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005058 * Return TRUE if there is a match, FALSE otherwise.
5059 * Note: Caller must ensure that: start != NULL.
5060 */
5061 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005062nfa_regmatch(prog, start, submatch, m)
5063 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005064 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005065 regsubs_T *submatch;
5066 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005067{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068 int result;
5069 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005070 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005071 int go_to_nextline = FALSE;
5072 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005073 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005074 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005075 nfa_list_T *thislist;
5076 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005077 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005078 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005079 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005080 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005081 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005082 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005083#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005084 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005085
5086 if (debug == NULL)
5087 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005088 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005089 return FALSE;
5090 }
5091#endif
Bram Moolenaar41f12052013-08-25 17:01:42 +02005092 /* Some patterns may take a long time to match, especially when using
5093 * recursive_regmatch(). Allow interrupting them with CTRL-C. */
5094 fast_breakcheck();
5095 if (got_int)
5096 return FALSE;
5097
Bram Moolenaar963fee22013-05-26 21:47:28 +02005098 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005099
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005100 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005101 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005102 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005103 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005104 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005105 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005106 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005107 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005108
5109#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005110 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005111 if (log_fd != NULL)
5112 {
5113 fprintf(log_fd, "**********************************\n");
5114 nfa_set_code(start->c);
5115 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5116 abs(start->id), code);
5117 fprintf(log_fd, "**********************************\n");
5118 }
5119 else
5120 {
5121 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5122 log_fd = stderr;
5123 }
5124#endif
5125
5126 thislist = &list[0];
5127 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005128 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005129 nextlist = &list[1];
5130 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005131 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005132#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005133 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005134#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005135 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005136
5137 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5138 * it's the first MOPEN. */
5139 if (toplevel)
5140 {
5141 if (REG_MULTI)
5142 {
5143 m->norm.list.multi[0].start.lnum = reglnum;
5144 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5145 }
5146 else
5147 m->norm.list.line[0].start = reginput;
5148 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005149 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005150 }
5151 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005152 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005153
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005154#define ADD_STATE_IF_MATCH(state) \
5155 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005156 add_state = state->out; \
5157 add_off = clen; \
5158 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005159
5160 /*
5161 * Run for each character.
5162 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005163 for (;;)
5164 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005165 int curc;
5166 int clen;
5167
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005168#ifdef FEAT_MBYTE
5169 if (has_mbyte)
5170 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005171 curc = (*mb_ptr2char)(reginput);
5172 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005173 }
5174 else
5175#endif
5176 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005177 curc = *reginput;
5178 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005179 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005180 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005181 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005182 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005183 go_to_nextline = FALSE;
5184 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005185
5186 /* swap lists */
5187 thislist = &list[flag];
5188 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005189 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005190 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005191 ++nfa_listid;
5192 thislist->id = nfa_listid;
5193 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005194
5195#ifdef ENABLE_LOG
5196 fprintf(log_fd, "------------------------------------------\n");
5197 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005198 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005199 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005200 {
5201 int i;
5202
5203 for (i = 0; i < thislist->n; i++)
5204 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5205 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005206 fprintf(log_fd, "\n");
5207#endif
5208
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005209#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005210 fprintf(debug, "\n-------------------\n");
5211#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005212 /*
5213 * If the state lists are empty we can stop.
5214 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005215 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005216 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005217
5218 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005219 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005220 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005221 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005222
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005223#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005224 nfa_set_code(t->state->c);
5225 fprintf(debug, "%s, ", code);
5226#endif
5227#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005228 {
5229 int col;
5230
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005231 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005232 col = -1;
5233 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005234 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005235 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005236 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005237 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005238 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5239 abs(t->state->id), (int)t->state->c, code, col,
5240 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005241 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005242#endif
5243
5244 /*
5245 * Handle the possible codes of the current state.
5246 * The most important is NFA_MATCH.
5247 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005248 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005249 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005250 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005251 switch (t->state->c)
5252 {
5253 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005254 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005255 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005256 copy_sub(&submatch->norm, &t->subs.norm);
5257#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005258 if (nfa_has_zsubexpr)
5259 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005260#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005261#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005262 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005263#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005264 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005265 * states at this position. When the list of states is going
5266 * to be empty quit without advancing, so that "reginput" is
5267 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005268 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005269 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005270 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005271 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005272
5273 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005274 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005275 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005276 /*
5277 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005278 * NFA_START_INVISIBLE_BEFORE node.
5279 * They surround a zero-width group, used with "\@=", "\&",
5280 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005281 * If we got here, it means that the current "invisible" group
5282 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005283 * nfa_regmatch(). For a look-behind match only when it ends
5284 * in the position in "nfa_endp".
5285 * Submatches are stored in *m, and used in the parent call.
5286 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005287#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005288 if (nfa_endp != NULL)
5289 {
5290 if (REG_MULTI)
5291 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5292 (int)reglnum,
5293 (int)nfa_endp->se_u.pos.lnum,
5294 (int)(reginput - regline),
5295 nfa_endp->se_u.pos.col);
5296 else
5297 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5298 (int)(reginput - regline),
5299 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005300 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005301#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005302 /* If "nfa_endp" is set it's only a match if it ends at
5303 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005304 if (nfa_endp != NULL && (REG_MULTI
5305 ? (reglnum != nfa_endp->se_u.pos.lnum
5306 || (int)(reginput - regline)
5307 != nfa_endp->se_u.pos.col)
5308 : reginput != nfa_endp->se_u.ptr))
5309 break;
5310
5311 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005312 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005313 {
5314 copy_sub(&m->norm, &t->subs.norm);
5315#ifdef FEAT_SYN_HL
5316 if (nfa_has_zsubexpr)
5317 copy_sub(&m->synt, &t->subs.synt);
5318#endif
5319 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005320#ifdef ENABLE_LOG
5321 fprintf(log_fd, "Match found:\n");
5322 log_subsexpr(m);
5323#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005324 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005325 break;
5326
5327 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005328 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005329 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005330 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005331 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005332 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005333 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005334 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005335 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005336#ifdef ENABLE_LOG
5337 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5338 failure_chance(t->state->out, 0),
5339 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005340#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005341 /* Do it directly if there already is a PIM or when
5342 * nfa_postprocess() detected it will work better. */
5343 if (t->pim.result != NFA_PIM_UNUSED
5344 || t->state->c == NFA_START_INVISIBLE_FIRST
5345 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5346 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5347 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005348 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005349 int in_use = m->norm.in_use;
5350
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005351 /* Copy submatch info for the recursive call, so that
5352 * \1 can be matched. */
5353 copy_sub_off(&m->norm, &t->subs.norm);
5354
Bram Moolenaara2d95102013-06-04 14:23:05 +02005355 /*
5356 * First try matching the invisible match, then what
5357 * follows.
5358 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005359 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005360 submatch, m, &listids);
5361
Bram Moolenaardecd9542013-06-07 16:31:50 +02005362 /* for \@! and \@<! it is a match when the result is
5363 * FALSE */
5364 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005365 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5366 || t->state->c
5367 == NFA_START_INVISIBLE_BEFORE_NEG
5368 || t->state->c
5369 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005370 {
5371 /* Copy submatch info from the recursive call */
5372 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005373#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005374 if (nfa_has_zsubexpr)
5375 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005376#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005377
Bram Moolenaara2d95102013-06-04 14:23:05 +02005378 /* t->state->out1 is the corresponding
5379 * END_INVISIBLE node; Add its out to the current
5380 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005381 add_here = TRUE;
5382 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005383 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005384 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005385 }
5386 else
5387 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005388 nfa_pim_T pim;
5389
Bram Moolenaara2d95102013-06-04 14:23:05 +02005390 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005391 * First try matching what follows. Only if a match
5392 * is found verify the invisible match matches. Add a
5393 * nfa_pim_T to the following states, it contains info
5394 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005395 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005396 pim.state = t->state;
5397 pim.result = NFA_PIM_TODO;
5398 pim.subs.norm.in_use = 0;
5399#ifdef FEAT_SYN_HL
5400 pim.subs.synt.in_use = 0;
5401#endif
5402 if (REG_MULTI)
5403 {
5404 pim.end.pos.col = (int)(reginput - regline);
5405 pim.end.pos.lnum = reglnum;
5406 }
5407 else
5408 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005409
5410 /* t->state->out1 is the corresponding END_INVISIBLE
5411 * node; Add its out to the current list (zero-width
5412 * match). */
5413 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005414 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005415 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005416 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005417 break;
5418
Bram Moolenaar87953742013-06-05 18:52:40 +02005419 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005420 {
5421 nfa_state_T *skip = NULL;
5422#ifdef ENABLE_LOG
5423 int skip_lid = 0;
5424#endif
5425
5426 /* There is no point in trying to match the pattern if the
5427 * output state is not going to be added to the list. */
5428 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5429 {
5430 skip = t->state->out1->out;
5431#ifdef ENABLE_LOG
5432 skip_lid = nextlist->id;
5433#endif
5434 }
5435 else if (state_in_list(nextlist,
5436 t->state->out1->out->out, &t->subs))
5437 {
5438 skip = t->state->out1->out->out;
5439#ifdef ENABLE_LOG
5440 skip_lid = nextlist->id;
5441#endif
5442 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005443 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005444 t->state->out1->out->out, &t->subs))
5445 {
5446 skip = t->state->out1->out->out;
5447#ifdef ENABLE_LOG
5448 skip_lid = thislist->id;
5449#endif
5450 }
5451 if (skip != NULL)
5452 {
5453#ifdef ENABLE_LOG
5454 nfa_set_code(skip->c);
5455 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5456 abs(skip->id), skip_lid, skip->c, code);
5457#endif
5458 break;
5459 }
5460
Bram Moolenaar87953742013-06-05 18:52:40 +02005461 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005462 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005463 submatch, m, &listids);
5464 if (result)
5465 {
5466 int bytelen;
5467
5468#ifdef ENABLE_LOG
5469 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5470 log_subsexpr(m);
5471#endif
5472 /* Copy submatch info from the recursive call */
5473 copy_sub_off(&t->subs.norm, &m->norm);
5474#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005475 if (nfa_has_zsubexpr)
5476 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005477#endif
5478 /* Now we need to skip over the matched text and then
5479 * continue with what follows. */
5480 if (REG_MULTI)
5481 /* TODO: multi-line match */
5482 bytelen = m->norm.list.multi[0].end.col
5483 - (int)(reginput - regline);
5484 else
5485 bytelen = (int)(m->norm.list.line[0].end - reginput);
5486
5487#ifdef ENABLE_LOG
5488 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5489#endif
5490 if (bytelen == 0)
5491 {
5492 /* empty match, output of corresponding
5493 * NFA_END_PATTERN/NFA_SKIP to be used at current
5494 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005495 add_here = TRUE;
5496 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005497 }
5498 else if (bytelen <= clen)
5499 {
5500 /* match current character, output of corresponding
5501 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005502 add_state = t->state->out1->out->out;
5503 add_off = clen;
5504 }
5505 else
5506 {
5507 /* skip over the matched characters, set character
5508 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005509 add_state = t->state->out1->out;
5510 add_off = bytelen;
5511 add_count = bytelen - clen;
5512 }
5513 }
5514 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005515 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005516
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005517 case NFA_BOL:
5518 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005519 {
5520 add_here = TRUE;
5521 add_state = t->state->out;
5522 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005523 break;
5524
5525 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005526 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005527 {
5528 add_here = TRUE;
5529 add_state = t->state->out;
5530 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005531 break;
5532
5533 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005534 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005535
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005536 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005537 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538#ifdef FEAT_MBYTE
5539 else if (has_mbyte)
5540 {
5541 int this_class;
5542
5543 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005544 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005545 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005546 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005547 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005548 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005549 }
5550#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005551 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005552 || (reginput > regline
5553 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005554 result = FALSE;
5555 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005556 {
5557 add_here = TRUE;
5558 add_state = t->state->out;
5559 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005560 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005561
5562 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005563 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005564 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005565 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005566#ifdef FEAT_MBYTE
5567 else if (has_mbyte)
5568 {
5569 int this_class, prev_class;
5570
5571 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005572 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005573 prev_class = reg_prev_class();
5574 if (this_class == prev_class
5575 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005576 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005577 }
5578#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005579 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005580 || (reginput[0] != NUL
5581 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005582 result = FALSE;
5583 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005584 {
5585 add_here = TRUE;
5586 add_state = t->state->out;
5587 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005588 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005589
Bram Moolenaar4b780632013-05-31 22:14:52 +02005590 case NFA_BOF:
5591 if (reglnum == 0 && reginput == regline
5592 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005593 {
5594 add_here = TRUE;
5595 add_state = t->state->out;
5596 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005597 break;
5598
5599 case NFA_EOF:
5600 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005601 {
5602 add_here = TRUE;
5603 add_state = t->state->out;
5604 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005605 break;
5606
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005607#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005608 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005609 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005610 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005611 int len = 0;
5612 nfa_state_T *end;
5613 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005614 int cchars[MAX_MCO];
5615 int ccount = 0;
5616 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005617
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005618 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005619 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005620 if (utf_iscomposing(sta->c))
5621 {
5622 /* Only match composing character(s), ignore base
5623 * character. Used for ".{composing}" and "{composing}"
5624 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005625 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005626 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005627 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005628 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005629 /* If \Z was present, then ignore composing characters.
5630 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005631 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005632 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005633 else
5634 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005635 while (sta->c != NFA_END_COMPOSING)
5636 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005637 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005638
5639 /* Check base character matches first, unless ignored. */
5640 else if (len > 0 || mc == sta->c)
5641 {
5642 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005643 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005644 len += mb_char2len(mc);
5645 sta = sta->out;
5646 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005647
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005648 /* We don't care about the order of composing characters.
5649 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005650 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005651 {
5652 mc = mb_ptr2char(reginput + len);
5653 cchars[ccount++] = mc;
5654 len += mb_char2len(mc);
5655 if (ccount == MAX_MCO)
5656 break;
5657 }
5658
5659 /* Check that each composing char in the pattern matches a
5660 * composing char in the text. We do not check if all
5661 * composing chars are matched. */
5662 result = OK;
5663 while (sta->c != NFA_END_COMPOSING)
5664 {
5665 for (j = 0; j < ccount; ++j)
5666 if (cchars[j] == sta->c)
5667 break;
5668 if (j == ccount)
5669 {
5670 result = FAIL;
5671 break;
5672 }
5673 sta = sta->out;
5674 }
5675 }
5676 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005677 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005678
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005679 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005680 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005681 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005682 }
5683#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005684
5685 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005686 if (curc == NUL && !reg_line_lbr && REG_MULTI
5687 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005688 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005689 go_to_nextline = TRUE;
5690 /* Pass -1 for the offset, which means taking the position
5691 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005692 add_state = t->state->out;
5693 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005694 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005695 else if (curc == '\n' && reg_line_lbr)
5696 {
5697 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005698 add_state = t->state->out;
5699 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005700 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005701 break;
5702
Bram Moolenaar417bad22013-06-07 14:08:30 +02005703 case NFA_START_COLL:
5704 case NFA_START_NEG_COLL:
5705 {
5706 /* What follows is a list of characters, until NFA_END_COLL.
5707 * One of them must match or none of them must match. */
5708 nfa_state_T *state;
5709 int result_if_matched;
5710 int c1, c2;
5711
5712 /* Never match EOL. If it's part of the collection it is added
5713 * as a separate state with an OR. */
5714 if (curc == NUL)
5715 break;
5716
5717 state = t->state->out;
5718 result_if_matched = (t->state->c == NFA_START_COLL);
5719 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005720 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005721 if (state->c == NFA_END_COLL)
5722 {
5723 result = !result_if_matched;
5724 break;
5725 }
5726 if (state->c == NFA_RANGE_MIN)
5727 {
5728 c1 = state->val;
5729 state = state->out; /* advance to NFA_RANGE_MAX */
5730 c2 = state->val;
5731#ifdef ENABLE_LOG
5732 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5733 curc, c1, c2);
5734#endif
5735 if (curc >= c1 && curc <= c2)
5736 {
5737 result = result_if_matched;
5738 break;
5739 }
5740 if (ireg_ic)
5741 {
5742 int curc_low = MB_TOLOWER(curc);
5743 int done = FALSE;
5744
5745 for ( ; c1 <= c2; ++c1)
5746 if (MB_TOLOWER(c1) == curc_low)
5747 {
5748 result = result_if_matched;
5749 done = TRUE;
5750 break;
5751 }
5752 if (done)
5753 break;
5754 }
5755 }
5756 else if (state->c < 0 ? check_char_class(state->c, curc)
5757 : (curc == state->c
5758 || (ireg_ic && MB_TOLOWER(curc)
5759 == MB_TOLOWER(state->c))))
5760 {
5761 result = result_if_matched;
5762 break;
5763 }
5764 state = state->out;
5765 }
5766 if (result)
5767 {
5768 /* next state is in out of the NFA_END_COLL, out1 of
5769 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005770 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005771 add_off = clen;
5772 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005773 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005774 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005775
5776 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005777 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005778 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005779 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005780 add_state = t->state->out;
5781 add_off = clen;
5782 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005783 break;
5784
5785 /*
5786 * Character classes like \a for alpha, \d for digit etc.
5787 */
5788 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005789 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005790 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005791 break;
5792
5793 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005794 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005795 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005796 break;
5797
5798 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005799 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005800 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005801 break;
5802
5803 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005804 result = !VIM_ISDIGIT(curc)
5805 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005806 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005807 break;
5808
5809 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005810 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005811 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005812 break;
5813
5814 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005815 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005816 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005817 break;
5818
5819 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005820 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005821 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005822 break;
5823
5824 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005825 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005826 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005827 break;
5828
5829 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005830 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005831 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005832 break;
5833
5834 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005835 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005836 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005837 break;
5838
5839 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005840 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005841 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005842 break;
5843
5844 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005845 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005846 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005847 break;
5848
5849 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005850 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005851 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005852 break;
5853
5854 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005855 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005856 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005857 break;
5858
5859 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005860 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005861 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005862 break;
5863
5864 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005865 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005866 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005867 break;
5868
5869 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005870 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005871 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005872 break;
5873
5874 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005875 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005876 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005877 break;
5878
5879 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005880 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005881 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005882 break;
5883
5884 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005885 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005886 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005887 break;
5888
5889 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005890 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005891 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005892 break;
5893
5894 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005895 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005896 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005897 break;
5898
5899 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005900 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005901 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005902 break;
5903
5904 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005905 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005906 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005907 break;
5908
5909 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005910 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005911 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005912 break;
5913
5914 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005915 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005916 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005917 break;
5918
Bram Moolenaar1cfad522013-08-14 12:06:49 +02005919 case NFA_LOWER_IC: /* [a-z] */
5920 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
5921 ADD_STATE_IF_MATCH(t->state);
5922 break;
5923
5924 case NFA_NLOWER_IC: /* [^a-z] */
5925 result = curc != NUL
5926 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
5927 ADD_STATE_IF_MATCH(t->state);
5928 break;
5929
5930 case NFA_UPPER_IC: /* [A-Z] */
5931 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
5932 ADD_STATE_IF_MATCH(t->state);
5933 break;
5934
5935 case NFA_NUPPER_IC: /* ^[A-Z] */
5936 result = curc != NUL
5937 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
5938 ADD_STATE_IF_MATCH(t->state);
5939 break;
5940
Bram Moolenaar5714b802013-05-28 22:03:20 +02005941 case NFA_BACKREF1:
5942 case NFA_BACKREF2:
5943 case NFA_BACKREF3:
5944 case NFA_BACKREF4:
5945 case NFA_BACKREF5:
5946 case NFA_BACKREF6:
5947 case NFA_BACKREF7:
5948 case NFA_BACKREF8:
5949 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005950#ifdef FEAT_SYN_HL
5951 case NFA_ZREF1:
5952 case NFA_ZREF2:
5953 case NFA_ZREF3:
5954 case NFA_ZREF4:
5955 case NFA_ZREF5:
5956 case NFA_ZREF6:
5957 case NFA_ZREF7:
5958 case NFA_ZREF8:
5959 case NFA_ZREF9:
5960#endif
5961 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005962 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005963 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005964 int bytelen;
5965
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005966 if (t->state->c <= NFA_BACKREF9)
5967 {
5968 subidx = t->state->c - NFA_BACKREF1 + 1;
5969 result = match_backref(&t->subs.norm, subidx, &bytelen);
5970 }
5971#ifdef FEAT_SYN_HL
5972 else
5973 {
5974 subidx = t->state->c - NFA_ZREF1 + 1;
5975 result = match_zref(subidx, &bytelen);
5976 }
5977#endif
5978
Bram Moolenaar5714b802013-05-28 22:03:20 +02005979 if (result)
5980 {
5981 if (bytelen == 0)
5982 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005983 /* empty match always works, output of NFA_SKIP to be
5984 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005985 add_here = TRUE;
5986 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005987 }
5988 else if (bytelen <= clen)
5989 {
5990 /* match current character, jump ahead to out of
5991 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005992 add_state = t->state->out->out;
5993 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005994 }
5995 else
5996 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005997 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005998 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005999 add_state = t->state->out;
6000 add_off = bytelen;
6001 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006002 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02006003 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02006004 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006005 }
6006 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02006007 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02006008 if (t->count - clen <= 0)
6009 {
6010 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006011 add_state = t->state->out;
6012 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006013 }
6014 else
6015 {
6016 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02006017 add_state = t->state;
6018 add_off = 0;
6019 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02006020 }
6021 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006022
Bram Moolenaar423532e2013-05-29 21:14:42 +02006023 case NFA_LNUM:
6024 case NFA_LNUM_GT:
6025 case NFA_LNUM_LT:
6026 result = (REG_MULTI &&
6027 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6028 (long_u)(reglnum + reg_firstlnum)));
6029 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006030 {
6031 add_here = TRUE;
6032 add_state = t->state->out;
6033 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006034 break;
6035
6036 case NFA_COL:
6037 case NFA_COL_GT:
6038 case NFA_COL_LT:
6039 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6040 (long_u)(reginput - regline) + 1);
6041 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006042 {
6043 add_here = TRUE;
6044 add_state = t->state->out;
6045 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006046 break;
6047
6048 case NFA_VCOL:
6049 case NFA_VCOL_GT:
6050 case NFA_VCOL_LT:
6051 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
6052 (long_u)win_linetabsize(
6053 reg_win == NULL ? curwin : reg_win,
6054 regline, (colnr_T)(reginput - regline)) + 1);
6055 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006056 {
6057 add_here = TRUE;
6058 add_state = t->state->out;
6059 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006060 break;
6061
Bram Moolenaar044aa292013-06-04 21:27:38 +02006062 case NFA_MARK:
6063 case NFA_MARK_GT:
6064 case NFA_MARK_LT:
6065 {
6066 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6067
6068 /* Compare the mark position to the match position. */
6069 result = (pos != NULL /* mark doesn't exist */
6070 && pos->lnum > 0 /* mark isn't set in reg_buf */
6071 && (pos->lnum == reglnum + reg_firstlnum
6072 ? (pos->col == (colnr_T)(reginput - regline)
6073 ? t->state->c == NFA_MARK
6074 : (pos->col < (colnr_T)(reginput - regline)
6075 ? t->state->c == NFA_MARK_GT
6076 : t->state->c == NFA_MARK_LT))
6077 : (pos->lnum < reglnum + reg_firstlnum
6078 ? t->state->c == NFA_MARK_GT
6079 : t->state->c == NFA_MARK_LT)));
6080 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006081 {
6082 add_here = TRUE;
6083 add_state = t->state->out;
6084 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006085 break;
6086 }
6087
Bram Moolenaar423532e2013-05-29 21:14:42 +02006088 case NFA_CURSOR:
6089 result = (reg_win != NULL
6090 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6091 && ((colnr_T)(reginput - regline)
6092 == reg_win->w_cursor.col));
6093 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006094 {
6095 add_here = TRUE;
6096 add_state = t->state->out;
6097 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006098 break;
6099
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006100 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02006101#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006102 result = reg_match_visual();
6103 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006104 {
6105 add_here = TRUE;
6106 add_state = t->state->out;
6107 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02006108#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02006109 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006110
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006111 case NFA_MOPEN1:
6112 case NFA_MOPEN2:
6113 case NFA_MOPEN3:
6114 case NFA_MOPEN4:
6115 case NFA_MOPEN5:
6116 case NFA_MOPEN6:
6117 case NFA_MOPEN7:
6118 case NFA_MOPEN8:
6119 case NFA_MOPEN9:
6120#ifdef FEAT_SYN_HL
6121 case NFA_ZOPEN:
6122 case NFA_ZOPEN1:
6123 case NFA_ZOPEN2:
6124 case NFA_ZOPEN3:
6125 case NFA_ZOPEN4:
6126 case NFA_ZOPEN5:
6127 case NFA_ZOPEN6:
6128 case NFA_ZOPEN7:
6129 case NFA_ZOPEN8:
6130 case NFA_ZOPEN9:
6131#endif
6132 case NFA_NOPEN:
6133 case NFA_ZSTART:
6134 /* These states are only added to be able to bail out when
6135 * they are added again, nothing is to be done. */
6136 break;
6137
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006138 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006139 {
6140 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006141
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006142#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006143 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006144 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006145#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006146 result = (c == curc);
6147
6148 if (!result && ireg_ic)
6149 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006150#ifdef FEAT_MBYTE
6151 /* If there is a composing character which is not being
6152 * ignored there can be no match. Match with composing
6153 * character uses NFA_COMPOSING above. */
6154 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006155 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006156 result = FALSE;
6157#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006158 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006159 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006160 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006161
6162 } /* switch (t->state->c) */
6163
6164 if (add_state != NULL)
6165 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006166 nfa_pim_T *pim;
6167
6168 if (t->pim.result == NFA_PIM_UNUSED)
6169 pim = NULL;
6170 else
6171 pim = &t->pim;
6172
6173 /* Handle the postponed invisible match if the match might end
6174 * without advancing and before the end of the line. */
6175 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006176 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006177 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006178 {
6179#ifdef ENABLE_LOG
6180 fprintf(log_fd, "\n");
6181 fprintf(log_fd, "==================================\n");
6182 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6183 fprintf(log_fd, "\n");
6184#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006185 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006186 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006187 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006188 /* for \@! and \@<! it is a match when the result is
6189 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006190 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006191 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6192 || pim->state->c
6193 == NFA_START_INVISIBLE_BEFORE_NEG
6194 || pim->state->c
6195 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006196 {
6197 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006198 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006199#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006200 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006201 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006202#endif
6203 }
6204 }
6205 else
6206 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006207 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006208#ifdef ENABLE_LOG
6209 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006210 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006211 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6212 fprintf(log_fd, "\n");
6213#endif
6214 }
6215
Bram Moolenaardecd9542013-06-07 16:31:50 +02006216 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006217 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006218 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6219 || pim->state->c
6220 == NFA_START_INVISIBLE_BEFORE_NEG
6221 || pim->state->c
6222 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006223 {
6224 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006225 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006226#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006227 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006228 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006229#endif
6230 }
6231 else
6232 /* look-behind match failed, don't add the state */
6233 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006234
6235 /* Postponed invisible match was handled, don't add it to
6236 * following states. */
6237 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006238 }
6239
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006240 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006241 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006242 else
6243 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006244 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006245 if (add_count > 0)
6246 nextlist->t[nextlist->n - 1].count = add_count;
6247 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006248 }
6249
6250 } /* for (thislist = thislist; thislist->state; thislist++) */
6251
Bram Moolenaare23febd2013-05-26 18:40:14 +02006252 /* Look for the start of a match in the current position by adding the
6253 * start state to the list of states.
6254 * The first found match is the leftmost one, thus the order of states
6255 * matters!
6256 * Do not add the start state in recursive calls of nfa_regmatch(),
6257 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006258 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006259 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006260 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006261 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006262 && reglnum == 0
6263 && clen != 0
6264 && (ireg_maxcol == 0
6265 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006266 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006267 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006268 ? (reglnum < nfa_endp->se_u.pos.lnum
6269 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006270 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006271 < nfa_endp->se_u.pos.col))
6272 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006273 {
6274#ifdef ENABLE_LOG
6275 fprintf(log_fd, "(---) STARTSTATE\n");
6276#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006277 /* Inline optimized code for addstate() if we know the state is
6278 * the first MOPEN. */
6279 if (toplevel)
6280 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006281 int add = TRUE;
6282 int c;
6283
6284 if (prog->regstart != NUL && clen != 0)
6285 {
6286 if (nextlist->n == 0)
6287 {
6288 colnr_T col = (colnr_T)(reginput - regline) + clen;
6289
6290 /* Nextlist is empty, we can skip ahead to the
6291 * character that must appear at the start. */
6292 if (skip_to_start(prog->regstart, &col) == FAIL)
6293 break;
6294#ifdef ENABLE_LOG
6295 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6296 col - ((colnr_T)(reginput - regline) + clen));
6297#endif
6298 reginput = regline + col - clen;
6299 }
6300 else
6301 {
6302 /* Checking if the required start character matches is
6303 * cheaper than adding a state that won't match. */
6304 c = PTR2CHAR(reginput + clen);
6305 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6306 != MB_TOLOWER(prog->regstart)))
6307 {
6308#ifdef ENABLE_LOG
6309 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6310#endif
6311 add = FALSE;
6312 }
6313 }
6314 }
6315
6316 if (add)
6317 {
6318 if (REG_MULTI)
6319 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006320 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006321 else
6322 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006323 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006324 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006325 }
6326 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006327 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006328 }
6329
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006330#ifdef ENABLE_LOG
6331 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006332 {
6333 int i;
6334
6335 for (i = 0; i < thislist->n; i++)
6336 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6337 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006338 fprintf(log_fd, "\n");
6339#endif
6340
6341nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006342 /* Advance to the next character, or advance to the next line, or
6343 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006344 if (clen != 0)
6345 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006346 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6347 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006348 reg_nextline();
6349 else
6350 break;
6351 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006352
6353#ifdef ENABLE_LOG
6354 if (log_fd != stderr)
6355 fclose(log_fd);
6356 log_fd = NULL;
6357#endif
6358
6359theend:
6360 /* Free memory */
6361 vim_free(list[0].t);
6362 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006363 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006364#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006365#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366 fclose(debug);
6367#endif
6368
Bram Moolenaar963fee22013-05-26 21:47:28 +02006369 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006370}
6371
6372/*
6373 * Try match of "prog" with at regline["col"].
6374 * Returns 0 for failure, number of lines contained in the match otherwise.
6375 */
6376 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006377nfa_regtry(prog, col)
6378 nfa_regprog_T *prog;
6379 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006380{
6381 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006382 regsubs_T subs, m;
6383 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006384#ifdef ENABLE_LOG
6385 FILE *f;
6386#endif
6387
6388 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006389
6390#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006391 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006392 if (f != NULL)
6393 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006394 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006395#ifdef DEBUG
6396 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6397#endif
6398 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006399 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006400 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006401 fprintf(f, "\n\n");
6402 fclose(f);
6403 }
6404 else
6405 EMSG(_("Could not open temporary log file for writing "));
6406#endif
6407
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006408 clear_sub(&subs.norm);
6409 clear_sub(&m.norm);
6410#ifdef FEAT_SYN_HL
6411 clear_sub(&subs.synt);
6412 clear_sub(&m.synt);
6413#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006414
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006415 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006416 return 0;
6417
6418 cleanup_subexpr();
6419 if (REG_MULTI)
6420 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006421 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006422 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006423 reg_startpos[i] = subs.norm.list.multi[i].start;
6424 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006425 }
6426
6427 if (reg_startpos[0].lnum < 0)
6428 {
6429 reg_startpos[0].lnum = 0;
6430 reg_startpos[0].col = col;
6431 }
6432 if (reg_endpos[0].lnum < 0)
6433 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006434 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006435 reg_endpos[0].lnum = reglnum;
6436 reg_endpos[0].col = (int)(reginput - regline);
6437 }
6438 else
6439 /* Use line number of "\ze". */
6440 reglnum = reg_endpos[0].lnum;
6441 }
6442 else
6443 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006444 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006445 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006446 reg_startp[i] = subs.norm.list.line[i].start;
6447 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006448 }
6449
6450 if (reg_startp[0] == NULL)
6451 reg_startp[0] = regline + col;
6452 if (reg_endp[0] == NULL)
6453 reg_endp[0] = reginput;
6454 }
6455
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006456#ifdef FEAT_SYN_HL
6457 /* Package any found \z(...\) matches for export. Default is none. */
6458 unref_extmatch(re_extmatch_out);
6459 re_extmatch_out = NULL;
6460
6461 if (prog->reghasz == REX_SET)
6462 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006463 cleanup_zsubexpr();
6464 re_extmatch_out = make_extmatch();
6465 for (i = 0; i < subs.synt.in_use; i++)
6466 {
6467 if (REG_MULTI)
6468 {
6469 struct multipos *mpos = &subs.synt.list.multi[i];
6470
6471 /* Only accept single line matches. */
6472 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6473 re_extmatch_out->matches[i] =
6474 vim_strnsave(reg_getline(mpos->start.lnum)
6475 + mpos->start.col,
6476 mpos->end.col - mpos->start.col);
6477 }
6478 else
6479 {
6480 struct linepos *lpos = &subs.synt.list.line[i];
6481
6482 if (lpos->start != NULL && lpos->end != NULL)
6483 re_extmatch_out->matches[i] =
6484 vim_strnsave(lpos->start,
6485 (int)(lpos->end - lpos->start));
6486 }
6487 }
6488 }
6489#endif
6490
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006491 return 1 + reglnum;
6492}
6493
6494/*
6495 * Match a regexp against a string ("line" points to the string) or multiple
6496 * lines ("line" is NULL, use reg_getline()).
6497 *
6498 * Returns 0 for failure, number of lines contained in the match otherwise.
6499 */
6500 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006501nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006502 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006503 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006504{
6505 nfa_regprog_T *prog;
6506 long retval = 0L;
6507 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006508 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006509
6510 if (REG_MULTI)
6511 {
6512 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6513 line = reg_getline((linenr_T)0); /* relative to the cursor */
6514 reg_startpos = reg_mmatch->startpos;
6515 reg_endpos = reg_mmatch->endpos;
6516 }
6517 else
6518 {
6519 prog = (nfa_regprog_T *)reg_match->regprog;
6520 reg_startp = reg_match->startp;
6521 reg_endp = reg_match->endp;
6522 }
6523
6524 /* Be paranoid... */
6525 if (prog == NULL || line == NULL)
6526 {
6527 EMSG(_(e_null));
6528 goto theend;
6529 }
6530
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006531 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6532 if (prog->regflags & RF_ICASE)
6533 ireg_ic = TRUE;
6534 else if (prog->regflags & RF_NOICASE)
6535 ireg_ic = FALSE;
6536
6537#ifdef FEAT_MBYTE
6538 /* If pattern contains "\Z" overrule value of ireg_icombine */
6539 if (prog->regflags & RF_ICOMBINE)
6540 ireg_icombine = TRUE;
6541#endif
6542
6543 regline = line;
6544 reglnum = 0; /* relative to line */
6545
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006546 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006547 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006548 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006549 nfa_listid = 1;
6550 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006551#ifdef DEBUG
6552 nfa_regengine.expr = prog->pattern;
6553#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006554
Bram Moolenaard89616e2013-06-06 18:46:06 +02006555 if (prog->reganch && col > 0)
6556 return 0L;
6557
Bram Moolenaar473de612013-06-08 18:19:48 +02006558 need_clear_subexpr = TRUE;
6559#ifdef FEAT_SYN_HL
6560 /* Clear the external match subpointers if necessary. */
6561 if (prog->reghasz == REX_SET)
6562 {
6563 nfa_has_zsubexpr = TRUE;
6564 need_clear_zsubexpr = TRUE;
6565 }
6566 else
6567 nfa_has_zsubexpr = FALSE;
6568#endif
6569
Bram Moolenaard89616e2013-06-06 18:46:06 +02006570 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006571 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006572 /* Skip ahead until a character we know the match must start with.
6573 * When there is none there is no match. */
6574 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006575 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006576
Bram Moolenaar473de612013-06-08 18:19:48 +02006577 /* If match_text is set it contains the full text that must match.
6578 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006579 if (prog->match_text != NULL
6580#ifdef FEAT_MBYTE
6581 && !ireg_icombine
6582#endif
6583 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006584 return find_match_text(col, prog->regstart, prog->match_text);
6585 }
6586
Bram Moolenaard89616e2013-06-06 18:46:06 +02006587 /* If the start column is past the maximum column: no need to try. */
6588 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6589 goto theend;
6590
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006591 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006592 for (i = 0; i < nstate; ++i)
6593 {
6594 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006595 prog->state[i].lastlist[0] = 0;
6596 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006597 }
6598
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006599 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006600
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006601#ifdef DEBUG
6602 nfa_regengine.expr = NULL;
6603#endif
6604
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006605theend:
6606 return retval;
6607}
6608
6609/*
6610 * Compile a regular expression into internal code for the NFA matcher.
6611 * Returns the program in allocated space. Returns NULL for an error.
6612 */
6613 static regprog_T *
6614nfa_regcomp(expr, re_flags)
6615 char_u *expr;
6616 int re_flags;
6617{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006618 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006619 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006620 int *postfix;
6621
6622 if (expr == NULL)
6623 return NULL;
6624
6625#ifdef DEBUG
6626 nfa_regengine.expr = expr;
6627#endif
6628
6629 init_class_tab();
6630
6631 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6632 return NULL;
6633
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006634 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006635 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006636 postfix = re2post();
6637 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006638 {
6639 /* TODO: only give this error for debugging? */
6640 if (post_ptr >= post_end)
6641 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006642 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006643 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006644
6645 /*
6646 * In order to build the NFA, we parse the input regexp twice:
6647 * 1. first pass to count size (so we can allocate space)
6648 * 2. second to emit code
6649 */
6650#ifdef ENABLE_LOG
6651 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006652 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006653
6654 if (f != NULL)
6655 {
6656 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6657 fclose(f);
6658 }
6659 }
6660#endif
6661
6662 /*
6663 * PASS 1
6664 * Count number of NFA states in "nstate". Do not build the NFA.
6665 */
6666 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006667
Bram Moolenaar16619a22013-06-11 18:42:36 +02006668 /* allocate the regprog with space for the compiled regexp */
6669 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006670 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6671 if (prog == NULL)
6672 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006673 state_ptr = prog->state;
6674
6675 /*
6676 * PASS 2
6677 * Build the NFA
6678 */
6679 prog->start = post2nfa(postfix, post_ptr, FALSE);
6680 if (prog->start == NULL)
6681 goto fail;
6682
6683 prog->regflags = regflags;
6684 prog->engine = &nfa_regengine;
6685 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006686 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006687 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006688 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006689
Bram Moolenaara2947e22013-06-11 22:44:09 +02006690 nfa_postprocess(prog);
6691
Bram Moolenaard89616e2013-06-06 18:46:06 +02006692 prog->reganch = nfa_get_reganch(prog->start, 0);
6693 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006694 prog->match_text = nfa_get_match_text(prog->start);
6695
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006696#ifdef ENABLE_LOG
6697 nfa_postfix_dump(expr, OK);
6698 nfa_dump(prog);
6699#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006700#ifdef FEAT_SYN_HL
6701 /* Remember whether this pattern has any \z specials in it. */
6702 prog->reghasz = re_has_z;
6703#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006704#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006705 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006706 nfa_regengine.expr = NULL;
6707#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006708
6709out:
6710 vim_free(post_start);
6711 post_start = post_ptr = post_end = NULL;
6712 state_ptr = NULL;
6713 return (regprog_T *)prog;
6714
6715fail:
6716 vim_free(prog);
6717 prog = NULL;
6718#ifdef ENABLE_LOG
6719 nfa_postfix_dump(expr, FAIL);
6720#endif
6721#ifdef DEBUG
6722 nfa_regengine.expr = NULL;
6723#endif
6724 goto out;
6725}
6726
Bram Moolenaar473de612013-06-08 18:19:48 +02006727/*
6728 * Free a compiled regexp program, returned by nfa_regcomp().
6729 */
6730 static void
6731nfa_regfree(prog)
6732 regprog_T *prog;
6733{
6734 if (prog != NULL)
6735 {
6736 vim_free(((nfa_regprog_T *)prog)->match_text);
6737#ifdef DEBUG
6738 vim_free(((nfa_regprog_T *)prog)->pattern);
6739#endif
6740 vim_free(prog);
6741 }
6742}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006743
6744/*
6745 * Match a regexp against a string.
6746 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6747 * Uses curbuf for line count and 'iskeyword'.
6748 *
6749 * Return TRUE if there is a match, FALSE if not.
6750 */
6751 static int
6752nfa_regexec(rmp, line, col)
6753 regmatch_T *rmp;
6754 char_u *line; /* string to match against */
6755 colnr_T col; /* column to start looking for match */
6756{
6757 reg_match = rmp;
6758 reg_mmatch = NULL;
6759 reg_maxline = 0;
6760 reg_line_lbr = FALSE;
6761 reg_buf = curbuf;
6762 reg_win = NULL;
6763 ireg_ic = rmp->rm_ic;
6764#ifdef FEAT_MBYTE
6765 ireg_icombine = FALSE;
6766#endif
6767 ireg_maxcol = 0;
6768 return (nfa_regexec_both(line, col) != 0);
6769}
6770
6771#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6772 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6773
6774static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6775
6776/*
6777 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6778 */
6779 static int
6780nfa_regexec_nl(rmp, line, col)
6781 regmatch_T *rmp;
6782 char_u *line; /* string to match against */
6783 colnr_T col; /* column to start looking for match */
6784{
6785 reg_match = rmp;
6786 reg_mmatch = NULL;
6787 reg_maxline = 0;
6788 reg_line_lbr = TRUE;
6789 reg_buf = curbuf;
6790 reg_win = NULL;
6791 ireg_ic = rmp->rm_ic;
6792#ifdef FEAT_MBYTE
6793 ireg_icombine = FALSE;
6794#endif
6795 ireg_maxcol = 0;
6796 return (nfa_regexec_both(line, col) != 0);
6797}
6798#endif
6799
6800
6801/*
6802 * Match a regexp against multiple lines.
6803 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6804 * Uses curbuf for line count and 'iskeyword'.
6805 *
6806 * Return zero if there is no match. Return number of lines contained in the
6807 * match otherwise.
6808 *
6809 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6810 *
6811 * ! Also NOTE : match may actually be in another line. e.g.:
6812 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6813 *
6814 * +-------------------------+
6815 * |a |
6816 * |b |
6817 * |c |
6818 * | |
6819 * +-------------------------+
6820 *
6821 * then nfa_regexec_multi() returns 3. while the original
6822 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6823 *
6824 * FIXME if this behavior is not compatible.
6825 */
6826 static long
6827nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6828 regmmatch_T *rmp;
6829 win_T *win; /* window in which to search or NULL */
6830 buf_T *buf; /* buffer in which to search */
6831 linenr_T lnum; /* nr of line to start looking for match */
6832 colnr_T col; /* column to start looking for match */
6833 proftime_T *tm UNUSED; /* timeout limit or NULL */
6834{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006835 reg_match = NULL;
6836 reg_mmatch = rmp;
6837 reg_buf = buf;
6838 reg_win = win;
6839 reg_firstlnum = lnum;
6840 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6841 reg_line_lbr = FALSE;
6842 ireg_ic = rmp->rmm_ic;
6843#ifdef FEAT_MBYTE
6844 ireg_icombine = FALSE;
6845#endif
6846 ireg_maxcol = rmp->rmm_maxcol;
6847
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006848 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006849}
6850
6851#ifdef DEBUG
6852# undef ENABLE_LOG
6853#endif