blob: 0e172c8ca12634268593e30d527df596c14e1b96 [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;
3785 /* compare the position */
3786 if (REG_MULTI)
3787 return one->end.pos.lnum == two->end.pos.lnum
3788 && one->end.pos.col == two->end.pos.col;
3789 return one->end.ptr == two->end.ptr;
3790}
3791
3792/*
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003793 * Return TRUE if "state" leads to a NFA_MATCH without advancing the input.
3794 */
3795 static int
3796match_follows(startstate, depth)
3797 nfa_state_T *startstate;
3798 int depth;
3799{
3800 nfa_state_T *state = startstate;
3801
3802 /* avoid too much recursion */
3803 if (depth > 10)
3804 return FALSE;
3805
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02003806 while (state != NULL)
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003807 {
3808 switch (state->c)
3809 {
3810 case NFA_MATCH:
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02003811 case NFA_MCLOSE:
3812 case NFA_END_INVISIBLE:
3813 case NFA_END_INVISIBLE_NEG:
3814 case NFA_END_PATTERN:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003815 return TRUE;
3816
3817 case NFA_SPLIT:
3818 return match_follows(state->out, depth + 1)
3819 || match_follows(state->out1, depth + 1);
3820
3821 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003822 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003823 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003824 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003825 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003826 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003827 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02003828 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003829 case NFA_COMPOSING:
3830 /* skip ahead to next state */
3831 state = state->out1->out;
Bram Moolenaar690ae9c2013-07-13 20:58:11 +02003832 continue;
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003833
3834 case NFA_ANY:
3835 case NFA_IDENT:
3836 case NFA_SIDENT:
3837 case NFA_KWORD:
3838 case NFA_SKWORD:
3839 case NFA_FNAME:
3840 case NFA_SFNAME:
3841 case NFA_PRINT:
3842 case NFA_SPRINT:
3843 case NFA_WHITE:
3844 case NFA_NWHITE:
3845 case NFA_DIGIT:
3846 case NFA_NDIGIT:
3847 case NFA_HEX:
3848 case NFA_NHEX:
3849 case NFA_OCTAL:
3850 case NFA_NOCTAL:
3851 case NFA_WORD:
3852 case NFA_NWORD:
3853 case NFA_HEAD:
3854 case NFA_NHEAD:
3855 case NFA_ALPHA:
3856 case NFA_NALPHA:
3857 case NFA_LOWER:
3858 case NFA_NLOWER:
3859 case NFA_UPPER:
3860 case NFA_NUPPER:
Bram Moolenaar1cfad522013-08-14 12:06:49 +02003861 case NFA_LOWER_IC:
3862 case NFA_NLOWER_IC:
3863 case NFA_UPPER_IC:
3864 case NFA_NUPPER_IC:
Bram Moolenaar1e02e662013-06-08 23:26:27 +02003865 case NFA_START_COLL:
3866 case NFA_START_NEG_COLL:
3867 case NFA_NEWL:
3868 /* state will advance input */
3869 return FALSE;
3870
3871 default:
3872 if (state->c > 0)
3873 /* state will advance input */
3874 return FALSE;
3875
3876 /* Others: zero-width or possibly zero-width, might still find
3877 * a match at the same position, keep looking. */
3878 break;
3879 }
3880 state = state->out;
3881 }
3882 return FALSE;
3883}
3884
3885
3886/*
Bram Moolenaar43e02982013-06-07 17:31:29 +02003887 * Return TRUE if "state" is already in list "l".
3888 */
3889 static int
3890state_in_list(l, state, subs)
3891 nfa_list_T *l; /* runtime state list */
3892 nfa_state_T *state; /* state to update */
3893 regsubs_T *subs; /* pointers to subexpressions */
3894{
3895 if (state->lastlist[nfa_ll_index] == l->id)
3896 {
Bram Moolenaar69b52452013-07-17 21:10:51 +02003897 if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL))
Bram Moolenaar43e02982013-06-07 17:31:29 +02003898 return TRUE;
3899 }
3900 return FALSE;
3901}
3902
Bram Moolenaard05bf562013-06-30 23:24:08 +02003903/*
3904 * Add "state" and possibly what follows to state list ".".
3905 * Returns "subs_arg", possibly copied into temp_subs.
3906 */
3907
3908 static regsubs_T *
3909addstate(l, state, subs_arg, pim, off)
3910 nfa_list_T *l; /* runtime state list */
3911 nfa_state_T *state; /* state to update */
3912 regsubs_T *subs_arg; /* pointers to subexpressions */
3913 nfa_pim_T *pim; /* postponed look-behind match */
3914 int off; /* byte offset, when -1 go to next line */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003915{
Bram Moolenaar963fee22013-05-26 21:47:28 +02003916 int subidx;
Bram Moolenaar428e9872013-05-30 17:05:39 +02003917 nfa_thread_T *thread;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003918 lpos_T save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003919 int save_in_use;
Bram Moolenaar963fee22013-05-26 21:47:28 +02003920 char_u *save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02003921 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003922 regsub_T *sub;
Bram Moolenaard05bf562013-06-30 23:24:08 +02003923 regsubs_T *subs = subs_arg;
3924 static regsubs_T temp_subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02003925#ifdef ENABLE_LOG
3926 int did_print = FALSE;
3927#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003928
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003929 switch (state->c)
3930 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003931 case NFA_NCLOSE:
3932 case NFA_MCLOSE:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003933 case NFA_MCLOSE1:
3934 case NFA_MCLOSE2:
3935 case NFA_MCLOSE3:
3936 case NFA_MCLOSE4:
3937 case NFA_MCLOSE5:
3938 case NFA_MCLOSE6:
3939 case NFA_MCLOSE7:
3940 case NFA_MCLOSE8:
3941 case NFA_MCLOSE9:
3942#ifdef FEAT_SYN_HL
3943 case NFA_ZCLOSE:
3944 case NFA_ZCLOSE1:
3945 case NFA_ZCLOSE2:
3946 case NFA_ZCLOSE3:
3947 case NFA_ZCLOSE4:
3948 case NFA_ZCLOSE5:
3949 case NFA_ZCLOSE6:
3950 case NFA_ZCLOSE7:
3951 case NFA_ZCLOSE8:
3952 case NFA_ZCLOSE9:
3953#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003954 case NFA_MOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003955 case NFA_ZEND:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003956 case NFA_SPLIT:
Bram Moolenaar927d4a12013-06-09 17:25:34 +02003957 case NFA_SKIP_CHAR:
Bram Moolenaar5714b802013-05-28 22:03:20 +02003958 /* These nodes are not added themselves but their "out" and/or
3959 * "out1" may be added below. */
3960 break;
3961
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003962 case NFA_BOL:
3963 case NFA_BOF:
3964 /* "^" won't match past end-of-line, don't bother trying.
3965 * Except when at the end of the line, or when we are going to the
3966 * next line for a look-behind match. */
3967 if (reginput > regline
3968 && *reginput != NUL
3969 && (nfa_endp == NULL
3970 || !REG_MULTI
3971 || reglnum == nfa_endp->se_u.pos.lnum))
3972 goto skip_add;
3973 /* FALLTHROUGH */
3974
Bram Moolenaarefb23f22013-06-01 23:02:54 +02003975 case NFA_MOPEN1:
3976 case NFA_MOPEN2:
3977 case NFA_MOPEN3:
3978 case NFA_MOPEN4:
3979 case NFA_MOPEN5:
3980 case NFA_MOPEN6:
3981 case NFA_MOPEN7:
3982 case NFA_MOPEN8:
3983 case NFA_MOPEN9:
3984#ifdef FEAT_SYN_HL
3985 case NFA_ZOPEN:
3986 case NFA_ZOPEN1:
3987 case NFA_ZOPEN2:
3988 case NFA_ZOPEN3:
3989 case NFA_ZOPEN4:
3990 case NFA_ZOPEN5:
3991 case NFA_ZOPEN6:
3992 case NFA_ZOPEN7:
3993 case NFA_ZOPEN8:
3994 case NFA_ZOPEN9:
3995#endif
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003996 case NFA_NOPEN:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02003997 case NFA_ZSTART:
Bram Moolenaar398d53d2013-08-01 15:45:52 +02003998 /* These nodes need to be added so that we can bail out when it
3999 * was added to this list before at the same position to avoid an
4000 * endless loop for "\(\)*" */
Bram Moolenaar307aa162013-06-02 16:34:21 +02004001
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004002 default:
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004003 if (state->lastlist[nfa_ll_index] == l->id)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004004 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004005 /* This state is already in the list, don't add it again,
Bram Moolenaara0169122013-06-26 18:16:58 +02004006 * unless it is an MOPEN that is used for a backreference or
4007 * when there is a PIM. */
Bram Moolenaar196ed142013-07-21 18:59:24 +02004008 if (!nfa_has_backref && pim == NULL && !l->has_pim)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004009 {
4010skip_add:
4011#ifdef ENABLE_LOG
4012 nfa_set_code(state->c);
4013 fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n",
4014 abs(state->id), l->id, state->c, code);
4015#endif
Bram Moolenaard05bf562013-06-30 23:24:08 +02004016 return subs;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004017 }
Bram Moolenaar428e9872013-05-30 17:05:39 +02004018
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004019 /* Do not add the state again when it exists with the same
4020 * positions. */
Bram Moolenaar69b52452013-07-17 21:10:51 +02004021 if (has_state_with_pos(l, state, subs, pim))
Bram Moolenaar43e02982013-06-07 17:31:29 +02004022 goto skip_add;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004023 }
4024
Bram Moolenaara0169122013-06-26 18:16:58 +02004025 /* When there are backreferences or PIMs the number of states may
4026 * be (a lot) bigger than anticipated. */
4027 if (l->n == l->len)
Bram Moolenaar428e9872013-05-30 17:05:39 +02004028 {
4029 int newlen = l->len * 3 / 2 + 50;
4030
Bram Moolenaard05bf562013-06-30 23:24:08 +02004031 if (subs != &temp_subs)
4032 {
4033 /* "subs" may point into the current array, need to make a
4034 * copy before it becomes invalid. */
4035 copy_sub(&temp_subs.norm, &subs->norm);
4036#ifdef FEAT_SYN_HL
4037 if (nfa_has_zsubexpr)
4038 copy_sub(&temp_subs.synt, &subs->synt);
4039#endif
4040 subs = &temp_subs;
4041 }
4042
Bram Moolenaar428e9872013-05-30 17:05:39 +02004043 l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T));
4044 l->len = newlen;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004045 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004046
4047 /* add the state to the list */
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004048 state->lastlist[nfa_ll_index] = l->id;
Bram Moolenaar428e9872013-05-30 17:05:39 +02004049 thread = &l->t[l->n++];
4050 thread->state = state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004051 if (pim == NULL)
4052 thread->pim.result = NFA_PIM_UNUSED;
4053 else
Bram Moolenaar196ed142013-07-21 18:59:24 +02004054 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004055 copy_pim(&thread->pim, pim);
Bram Moolenaar196ed142013-07-21 18:59:24 +02004056 l->has_pim = TRUE;
4057 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004058 copy_sub(&thread->subs.norm, &subs->norm);
4059#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004060 if (nfa_has_zsubexpr)
4061 copy_sub(&thread->subs.synt, &subs->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004062#endif
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004063#ifdef ENABLE_LOG
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004064 report_state("Adding", &thread->subs.norm, state, l->id, pim);
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004065 did_print = TRUE;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004066#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004067 }
4068
4069#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02004070 if (!did_print)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004071 report_state("Processing", &subs->norm, state, l->id, pim);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004072#endif
4073 switch (state->c)
4074 {
4075 case NFA_MATCH:
Bram Moolenaar963fee22013-05-26 21:47:28 +02004076 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004077 break;
4078
4079 case NFA_SPLIT:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004080 /* order matters here */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004081 subs = addstate(l, state->out, subs, pim, off);
4082 subs = addstate(l, state->out1, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004083 break;
4084
Bram Moolenaar5714b802013-05-28 22:03:20 +02004085 case NFA_SKIP_CHAR:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004086 case NFA_NOPEN:
4087 case NFA_NCLOSE:
Bram Moolenaard05bf562013-06-30 23:24:08 +02004088 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004089 break;
4090
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004091 case NFA_MOPEN:
4092 case NFA_MOPEN1:
4093 case NFA_MOPEN2:
4094 case NFA_MOPEN3:
4095 case NFA_MOPEN4:
4096 case NFA_MOPEN5:
4097 case NFA_MOPEN6:
4098 case NFA_MOPEN7:
4099 case NFA_MOPEN8:
4100 case NFA_MOPEN9:
4101#ifdef FEAT_SYN_HL
4102 case NFA_ZOPEN:
4103 case NFA_ZOPEN1:
4104 case NFA_ZOPEN2:
4105 case NFA_ZOPEN3:
4106 case NFA_ZOPEN4:
4107 case NFA_ZOPEN5:
4108 case NFA_ZOPEN6:
4109 case NFA_ZOPEN7:
4110 case NFA_ZOPEN8:
4111 case NFA_ZOPEN9:
4112#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004113 case NFA_ZSTART:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004114 if (state->c == NFA_ZSTART)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004115 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004116 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004117 sub = &subs->norm;
4118 }
4119#ifdef FEAT_SYN_HL
4120 else if (state->c >= NFA_ZOPEN)
4121 {
4122 subidx = state->c - NFA_ZOPEN;
4123 sub = &subs->synt;
4124 }
4125#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004126 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004127 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004128 subidx = state->c - NFA_MOPEN;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004129 sub = &subs->norm;
4130 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004131
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004132 /* avoid compiler warnings */
4133 save_ptr = NULL;
4134 save_lpos.lnum = 0;
4135 save_lpos.col = 0;
4136
Bram Moolenaar927d4a12013-06-09 17:25:34 +02004137 /* Set the position (with "off" added) in the subexpression. Save
4138 * and restore it when it was in use. Otherwise fill any gap. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004139 if (REG_MULTI)
4140 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004141 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004142 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004143 save_lpos = sub->list.multi[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004144 save_in_use = -1;
4145 }
4146 else
4147 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004148 save_in_use = sub->in_use;
4149 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004150 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004151 sub->list.multi[i].start.lnum = -1;
4152 sub->list.multi[i].end.lnum = -1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004153 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004154 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004155 }
Bram Moolenaar35b23862013-05-22 23:00:40 +02004156 if (off == -1)
4157 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004158 sub->list.multi[subidx].start.lnum = reglnum + 1;
4159 sub->list.multi[subidx].start.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004160 }
4161 else
4162 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004163 sub->list.multi[subidx].start.lnum = reglnum;
4164 sub->list.multi[subidx].start.col =
Bram Moolenaar35b23862013-05-22 23:00:40 +02004165 (colnr_T)(reginput - regline + off);
4166 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004167 }
4168 else
4169 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004170 if (subidx < sub->in_use)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004171 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004172 save_ptr = sub->list.line[subidx].start;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004173 save_in_use = -1;
4174 }
4175 else
4176 {
Bram Moolenaar5714b802013-05-28 22:03:20 +02004177 save_in_use = sub->in_use;
4178 for (i = sub->in_use; i < subidx; ++i)
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004179 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004180 sub->list.line[i].start = NULL;
4181 sub->list.line[i].end = NULL;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004182 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02004183 sub->in_use = subidx + 1;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004184 }
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004185 sub->list.line[subidx].start = reginput + off;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004186 }
4187
Bram Moolenaard05bf562013-06-30 23:24:08 +02004188 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004189
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004190 if (save_in_use == -1)
4191 {
4192 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004193 sub->list.multi[subidx].start = save_lpos;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004194 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004195 sub->list.line[subidx].start = save_ptr;
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004196 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004197 else
Bram Moolenaar5714b802013-05-28 22:03:20 +02004198 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004199 break;
4200
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004201 case NFA_MCLOSE:
Bram Moolenaar57a285b2013-05-26 16:57:28 +02004202 if (nfa_has_zend)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004203 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02004204 /* Do not overwrite the position set by \ze. If no \ze
4205 * encountered end will be set in nfa_regtry(). */
Bram Moolenaard05bf562013-06-30 23:24:08 +02004206 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004207 break;
4208 }
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004209 case NFA_MCLOSE1:
4210 case NFA_MCLOSE2:
4211 case NFA_MCLOSE3:
4212 case NFA_MCLOSE4:
4213 case NFA_MCLOSE5:
4214 case NFA_MCLOSE6:
4215 case NFA_MCLOSE7:
4216 case NFA_MCLOSE8:
4217 case NFA_MCLOSE9:
4218#ifdef FEAT_SYN_HL
4219 case NFA_ZCLOSE:
4220 case NFA_ZCLOSE1:
4221 case NFA_ZCLOSE2:
4222 case NFA_ZCLOSE3:
4223 case NFA_ZCLOSE4:
4224 case NFA_ZCLOSE5:
4225 case NFA_ZCLOSE6:
4226 case NFA_ZCLOSE7:
4227 case NFA_ZCLOSE8:
4228 case NFA_ZCLOSE9:
4229#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004230 case NFA_ZEND:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004231 if (state->c == NFA_ZEND)
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004232 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004233 subidx = 0;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004234 sub = &subs->norm;
4235 }
4236#ifdef FEAT_SYN_HL
4237 else if (state->c >= NFA_ZCLOSE)
4238 {
4239 subidx = state->c - NFA_ZCLOSE;
4240 sub = &subs->synt;
4241 }
4242#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02004243 else
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004244 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02004245 subidx = state->c - NFA_MCLOSE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004246 sub = &subs->norm;
4247 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004248
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02004249 /* We don't fill in gaps here, there must have been an MOPEN that
4250 * has done that. */
Bram Moolenaar5714b802013-05-28 22:03:20 +02004251 save_in_use = sub->in_use;
4252 if (sub->in_use <= subidx)
4253 sub->in_use = subidx + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004254 if (REG_MULTI)
4255 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004256 save_lpos = sub->list.multi[subidx].end;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004257 if (off == -1)
4258 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004259 sub->list.multi[subidx].end.lnum = reglnum + 1;
4260 sub->list.multi[subidx].end.col = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02004261 }
4262 else
4263 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004264 sub->list.multi[subidx].end.lnum = reglnum;
4265 sub->list.multi[subidx].end.col =
Bram Moolenaar963fee22013-05-26 21:47:28 +02004266 (colnr_T)(reginput - regline + off);
Bram Moolenaar35b23862013-05-22 23:00:40 +02004267 }
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004268 /* avoid compiler warnings */
4269 save_ptr = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004270 }
4271 else
4272 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004273 save_ptr = sub->list.line[subidx].end;
4274 sub->list.line[subidx].end = reginput + off;
Bram Moolenaarde9149e2013-07-17 19:22:13 +02004275 /* avoid compiler warnings */
4276 save_lpos.lnum = 0;
4277 save_lpos.col = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004278 }
4279
Bram Moolenaard05bf562013-06-30 23:24:08 +02004280 subs = addstate(l, state->out, subs, pim, off);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004281
4282 if (REG_MULTI)
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004283 sub->list.multi[subidx].end = save_lpos;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004284 else
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004285 sub->list.line[subidx].end = save_ptr;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004286 sub->in_use = save_in_use;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004287 break;
4288 }
Bram Moolenaard05bf562013-06-30 23:24:08 +02004289 return subs;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004290}
4291
4292/*
Bram Moolenaar4b417062013-05-25 20:19:50 +02004293 * Like addstate(), but the new state(s) are put at position "*ip".
4294 * Used for zero-width matches, next state to use is the added one.
4295 * This makes sure the order of states to be tried does not change, which
4296 * matters for alternatives.
4297 */
4298 static void
Bram Moolenaara2d95102013-06-04 14:23:05 +02004299addstate_here(l, state, subs, pim, ip)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004300 nfa_list_T *l; /* runtime state list */
4301 nfa_state_T *state; /* state to update */
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004302 regsubs_T *subs; /* pointers to subexpressions */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004303 nfa_pim_T *pim; /* postponed look-behind match */
Bram Moolenaar4b417062013-05-25 20:19:50 +02004304 int *ip;
4305{
4306 int tlen = l->n;
4307 int count;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004308 int listidx = *ip;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004309
4310 /* first add the state(s) at the end, so that we know how many there are */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004311 addstate(l, state, subs, pim, 0);
Bram Moolenaara2d95102013-06-04 14:23:05 +02004312
Bram Moolenaar4b417062013-05-25 20:19:50 +02004313 /* when "*ip" was at the end of the list, nothing to do */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004314 if (listidx + 1 == tlen)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004315 return;
4316
4317 /* re-order to put the new state at the current position */
4318 count = l->n - tlen;
Bram Moolenaara50d02d2013-06-16 15:43:50 +02004319 if (count == 0)
4320 return; /* no state got added */
Bram Moolenaar428e9872013-05-30 17:05:39 +02004321 if (count == 1)
4322 {
4323 /* overwrite the current state */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004324 l->t[listidx] = l->t[l->n - 1];
Bram Moolenaar428e9872013-05-30 17:05:39 +02004325 }
4326 else if (count > 1)
Bram Moolenaar4b417062013-05-25 20:19:50 +02004327 {
Bram Moolenaar55480dc2013-06-30 13:17:24 +02004328 if (l->n + count - 1 >= l->len)
4329 {
4330 /* not enough space to move the new states, reallocate the list
4331 * and move the states to the right position */
4332 nfa_thread_T *newl;
4333
4334 l->len = l->len * 3 / 2 + 50;
4335 newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T));
4336 if (newl == NULL)
4337 return;
4338 mch_memmove(&(newl[0]),
4339 &(l->t[0]),
4340 sizeof(nfa_thread_T) * listidx);
4341 mch_memmove(&(newl[listidx]),
4342 &(l->t[l->n - count]),
4343 sizeof(nfa_thread_T) * count);
4344 mch_memmove(&(newl[listidx + count]),
4345 &(l->t[listidx + 1]),
4346 sizeof(nfa_thread_T) * (l->n - count - listidx - 1));
4347 vim_free(l->t);
4348 l->t = newl;
4349 }
4350 else
4351 {
4352 /* make space for new states, then move them from the
4353 * end to the current position */
4354 mch_memmove(&(l->t[listidx + count]),
4355 &(l->t[listidx + 1]),
4356 sizeof(nfa_thread_T) * (l->n - listidx - 1));
4357 mch_memmove(&(l->t[listidx]),
4358 &(l->t[l->n - 1]),
4359 sizeof(nfa_thread_T) * count);
4360 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004361 }
Bram Moolenaar4b417062013-05-25 20:19:50 +02004362 --l->n;
Bram Moolenaara2d95102013-06-04 14:23:05 +02004363 *ip = listidx - 1;
Bram Moolenaar4b417062013-05-25 20:19:50 +02004364}
4365
4366/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004367 * Check character class "class" against current character c.
4368 */
4369 static int
4370check_char_class(class, c)
4371 int class;
4372 int c;
4373{
4374 switch (class)
4375 {
4376 case NFA_CLASS_ALNUM:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004377 if (c >= 1 && c <= 255 && isalnum(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004378 return OK;
4379 break;
4380 case NFA_CLASS_ALPHA:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004381 if (c >= 1 && c <= 255 && isalpha(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004382 return OK;
4383 break;
4384 case NFA_CLASS_BLANK:
4385 if (c == ' ' || c == '\t')
4386 return OK;
4387 break;
4388 case NFA_CLASS_CNTRL:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004389 if (c >= 1 && c <= 255 && iscntrl(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004390 return OK;
4391 break;
4392 case NFA_CLASS_DIGIT:
4393 if (VIM_ISDIGIT(c))
4394 return OK;
4395 break;
4396 case NFA_CLASS_GRAPH:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004397 if (c >= 1 && c <= 255 && isgraph(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004398 return OK;
4399 break;
4400 case NFA_CLASS_LOWER:
4401 if (MB_ISLOWER(c))
4402 return OK;
4403 break;
4404 case NFA_CLASS_PRINT:
4405 if (vim_isprintc(c))
4406 return OK;
4407 break;
4408 case NFA_CLASS_PUNCT:
Bram Moolenaar745fc022013-05-20 22:20:02 +02004409 if (c >= 1 && c <= 255 && ispunct(c))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004410 return OK;
4411 break;
4412 case NFA_CLASS_SPACE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02004413 if ((c >= 9 && c <= 13) || (c == ' '))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004414 return OK;
4415 break;
4416 case NFA_CLASS_UPPER:
4417 if (MB_ISUPPER(c))
4418 return OK;
4419 break;
4420 case NFA_CLASS_XDIGIT:
4421 if (vim_isxdigit(c))
4422 return OK;
4423 break;
4424 case NFA_CLASS_TAB:
4425 if (c == '\t')
4426 return OK;
4427 break;
4428 case NFA_CLASS_RETURN:
4429 if (c == '\r')
4430 return OK;
4431 break;
4432 case NFA_CLASS_BACKSPACE:
4433 if (c == '\b')
4434 return OK;
4435 break;
4436 case NFA_CLASS_ESCAPE:
4437 if (c == '\033')
4438 return OK;
4439 break;
4440
4441 default:
4442 /* should not be here :P */
Bram Moolenaar417bad22013-06-07 14:08:30 +02004443 EMSGN("E877: (NFA regexp) Invalid character class: %ld", class);
4444 return FAIL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004445 }
4446 return FAIL;
4447}
4448
Bram Moolenaar5714b802013-05-28 22:03:20 +02004449/*
4450 * Check for a match with subexpression "subidx".
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004451 * Return TRUE if it matches.
Bram Moolenaar5714b802013-05-28 22:03:20 +02004452 */
4453 static int
4454match_backref(sub, subidx, bytelen)
4455 regsub_T *sub; /* pointers to subexpressions */
4456 int subidx;
4457 int *bytelen; /* out: length of match in bytes */
4458{
4459 int len;
4460
4461 if (sub->in_use <= subidx)
4462 {
4463retempty:
4464 /* backref was not set, match an empty string */
4465 *bytelen = 0;
4466 return TRUE;
4467 }
4468
4469 if (REG_MULTI)
4470 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004471 if (sub->list.multi[subidx].start.lnum < 0
4472 || sub->list.multi[subidx].end.lnum < 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004473 goto retempty;
Bram Moolenaar580abea2013-06-14 20:31:28 +02004474 if (sub->list.multi[subidx].start.lnum == reglnum
4475 && sub->list.multi[subidx].end.lnum == reglnum)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004476 {
Bram Moolenaar580abea2013-06-14 20:31:28 +02004477 len = sub->list.multi[subidx].end.col
4478 - sub->list.multi[subidx].start.col;
4479 if (cstrncmp(regline + sub->list.multi[subidx].start.col,
4480 reginput, &len) == 0)
4481 {
4482 *bytelen = len;
4483 return TRUE;
4484 }
4485 }
4486 else
4487 {
4488 if (match_with_backref(
4489 sub->list.multi[subidx].start.lnum,
4490 sub->list.multi[subidx].start.col,
4491 sub->list.multi[subidx].end.lnum,
4492 sub->list.multi[subidx].end.col,
4493 bytelen) == RA_MATCH)
4494 return TRUE;
Bram Moolenaar5714b802013-05-28 22:03:20 +02004495 }
4496 }
4497 else
4498 {
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004499 if (sub->list.line[subidx].start == NULL
4500 || sub->list.line[subidx].end == NULL)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004501 goto retempty;
Bram Moolenaarf9e56b22013-05-28 22:52:16 +02004502 len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start);
4503 if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0)
Bram Moolenaar5714b802013-05-28 22:03:20 +02004504 {
4505 *bytelen = len;
4506 return TRUE;
4507 }
4508 }
4509 return FALSE;
4510}
4511
Bram Moolenaarefb23f22013-06-01 23:02:54 +02004512#ifdef FEAT_SYN_HL
4513
4514static int match_zref __ARGS((int subidx, int *bytelen));
4515
4516/*
4517 * Check for a match with \z subexpression "subidx".
4518 * Return TRUE if it matches.
4519 */
4520 static int
4521match_zref(subidx, bytelen)
4522 int subidx;
4523 int *bytelen; /* out: length of match in bytes */
4524{
4525 int len;
4526
4527 cleanup_zsubexpr();
4528 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL)
4529 {
4530 /* backref was not set, match an empty string */
4531 *bytelen = 0;
4532 return TRUE;
4533 }
4534
4535 len = (int)STRLEN(re_extmatch_in->matches[subidx]);
4536 if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0)
4537 {
4538 *bytelen = len;
4539 return TRUE;
4540 }
4541 return FALSE;
4542}
4543#endif
4544
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004545/*
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004546 * Save list IDs for all NFA states of "prog" into "list".
4547 * Also reset the IDs to zero.
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004548 * Only used for the recursive value lastlist[1].
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004549 */
4550 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004551nfa_save_listids(prog, list)
4552 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004553 int *list;
4554{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004555 int i;
4556 nfa_state_T *p;
4557
4558 /* Order in the list is reverse, it's a bit faster that way. */
4559 p = &prog->state[0];
4560 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004561 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004562 list[i] = p->lastlist[1];
4563 p->lastlist[1] = 0;
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004564 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004565 }
4566}
4567
4568/*
4569 * Restore list IDs from "list" to all NFA states.
4570 */
4571 static void
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004572nfa_restore_listids(prog, list)
4573 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004574 int *list;
4575{
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004576 int i;
4577 nfa_state_T *p;
4578
4579 p = &prog->state[0];
4580 for (i = prog->nstate; --i >= 0; )
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004581 {
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004582 p->lastlist[1] = list[i];
Bram Moolenaarf6de0322013-06-02 21:30:04 +02004583 ++p;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004584 }
4585}
4586
Bram Moolenaar423532e2013-05-29 21:14:42 +02004587 static int
4588nfa_re_num_cmp(val, op, pos)
4589 long_u val;
4590 int op;
4591 long_u pos;
4592{
4593 if (op == 1) return pos > val;
4594 if (op == 2) return pos < val;
4595 return val == pos;
4596}
4597
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004598static 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 +02004599static 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 +02004600
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004601/*
Bram Moolenaarf46da702013-06-02 22:37:42 +02004602 * Recursively call nfa_regmatch()
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004603 * "pim" is NULL or contains info about a Postponed Invisible Match (start
4604 * position).
Bram Moolenaarf46da702013-06-02 22:37:42 +02004605 */
4606 static int
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004607recursive_regmatch(state, pim, prog, submatch, m, listids)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004608 nfa_state_T *state;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004609 nfa_pim_T *pim;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004610 nfa_regprog_T *prog;
4611 regsubs_T *submatch;
4612 regsubs_T *m;
4613 int **listids;
4614{
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004615 int save_reginput_col = (int)(reginput - regline);
Bram Moolenaarf46da702013-06-02 22:37:42 +02004616 int save_reglnum = reglnum;
4617 int save_nfa_match = nfa_match;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004618 int save_nfa_listid = nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004619 save_se_T *save_nfa_endp = nfa_endp;
4620 save_se_T endpos;
4621 save_se_T *endposp = NULL;
4622 int result;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004623 int need_restore = FALSE;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004624
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004625 if (pim != NULL)
4626 {
4627 /* start at the position where the postponed match was */
4628 if (REG_MULTI)
4629 reginput = regline + pim->end.pos.col;
4630 else
4631 reginput = pim->end.ptr;
4632 }
4633
Bram Moolenaardecd9542013-06-07 16:31:50 +02004634 if (state->c == NFA_START_INVISIBLE_BEFORE
Bram Moolenaara2947e22013-06-11 22:44:09 +02004635 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST
4636 || state->c == NFA_START_INVISIBLE_BEFORE_NEG
4637 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaarf46da702013-06-02 22:37:42 +02004638 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004639 /* The recursive match must end at the current position. When "pim" is
4640 * not NULL it specifies the current position. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004641 endposp = &endpos;
4642 if (REG_MULTI)
4643 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004644 if (pim == NULL)
4645 {
4646 endpos.se_u.pos.col = (int)(reginput - regline);
4647 endpos.se_u.pos.lnum = reglnum;
4648 }
4649 else
4650 endpos.se_u.pos = pim->end.pos;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004651 }
4652 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02004653 {
4654 if (pim == NULL)
4655 endpos.se_u.ptr = reginput;
4656 else
4657 endpos.se_u.ptr = pim->end.ptr;
4658 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004659
4660 /* Go back the specified number of bytes, or as far as the
4661 * start of the previous line, to try matching "\@<=" or
Bram Moolenaar3fb14bc2013-07-14 12:34:56 +02004662 * not matching "\@<!". This is very inefficient, limit the number of
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004663 * bytes if possible. */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004664 if (state->val <= 0)
4665 {
4666 if (REG_MULTI)
4667 {
4668 regline = reg_getline(--reglnum);
4669 if (regline == NULL)
4670 /* can't go before the first line */
4671 regline = reg_getline(++reglnum);
4672 }
4673 reginput = regline;
4674 }
4675 else
4676 {
4677 if (REG_MULTI && (int)(reginput - regline) < state->val)
4678 {
4679 /* Not enough bytes in this line, go to end of
4680 * previous line. */
4681 regline = reg_getline(--reglnum);
4682 if (regline == NULL)
4683 {
4684 /* can't go before the first line */
4685 regline = reg_getline(++reglnum);
4686 reginput = regline;
4687 }
4688 else
4689 reginput = regline + STRLEN(regline);
4690 }
4691 if ((int)(reginput - regline) >= state->val)
4692 {
4693 reginput -= state->val;
4694#ifdef FEAT_MBYTE
4695 if (has_mbyte)
4696 reginput -= mb_head_off(regline, reginput);
4697#endif
4698 }
4699 else
4700 reginput = regline;
4701 }
4702 }
4703
Bram Moolenaarf46da702013-06-02 22:37:42 +02004704#ifdef ENABLE_LOG
4705 if (log_fd != stderr)
4706 fclose(log_fd);
4707 log_fd = NULL;
4708#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004709 /* Have to clear the lastlist field of the NFA nodes, so that
4710 * nfa_regmatch() and addstate() can run properly after recursion. */
4711 if (nfa_ll_index == 1)
4712 {
4713 /* Already calling nfa_regmatch() recursively. Save the lastlist[1]
4714 * values and clear them. */
4715 if (*listids == NULL)
4716 {
4717 *listids = (int *)lalloc(sizeof(int) * nstate, TRUE);
4718 if (*listids == NULL)
4719 {
4720 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!"));
4721 return 0;
4722 }
4723 }
4724 nfa_save_listids(prog, *listids);
4725 need_restore = TRUE;
4726 /* any value of nfa_listid will do */
4727 }
4728 else
4729 {
4730 /* First recursive nfa_regmatch() call, switch to the second lastlist
4731 * entry. Make sure nfa_listid is different from a previous recursive
4732 * call, because some states may still have this ID. */
4733 ++nfa_ll_index;
4734 if (nfa_listid <= nfa_alt_listid)
4735 nfa_listid = nfa_alt_listid;
4736 }
4737
4738 /* Call nfa_regmatch() to check if the current concat matches at this
4739 * position. The concat ends with the node NFA_END_INVISIBLE */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004740 nfa_endp = endposp;
4741 result = nfa_regmatch(prog, state->out, submatch, m);
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004742
4743 if (need_restore)
4744 nfa_restore_listids(prog, *listids);
4745 else
4746 {
4747 --nfa_ll_index;
4748 nfa_alt_listid = nfa_listid;
4749 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02004750
4751 /* restore position in input text */
Bram Moolenaarf46da702013-06-02 22:37:42 +02004752 reglnum = save_reglnum;
Bram Moolenaar484d2412013-06-13 19:47:07 +02004753 if (REG_MULTI)
4754 regline = reg_getline(reglnum);
Bram Moolenaar4c46b5e2013-06-13 22:59:30 +02004755 reginput = regline + save_reginput_col;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004756 nfa_match = save_nfa_match;
4757 nfa_endp = save_nfa_endp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02004758 nfa_listid = save_nfa_listid;
Bram Moolenaarf46da702013-06-02 22:37:42 +02004759
4760#ifdef ENABLE_LOG
4761 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
4762 if (log_fd != NULL)
4763 {
4764 fprintf(log_fd, "****************************\n");
4765 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n");
4766 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
4767 fprintf(log_fd, "****************************\n");
4768 }
4769 else
4770 {
4771 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
4772 log_fd = stderr;
4773 }
4774#endif
4775
4776 return result;
4777}
4778
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004779static int skip_to_start __ARGS((int c, colnr_T *colp));
Bram Moolenaar473de612013-06-08 18:19:48 +02004780static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text));
Bram Moolenaara2d95102013-06-04 14:23:05 +02004781
4782/*
4783 * Estimate the chance of a match with "state" failing.
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004784 * empty match: 0
Bram Moolenaara2d95102013-06-04 14:23:05 +02004785 * NFA_ANY: 1
4786 * specific character: 99
4787 */
4788 static int
4789failure_chance(state, depth)
4790 nfa_state_T *state;
4791 int depth;
4792{
4793 int c = state->c;
4794 int l, r;
4795
4796 /* detect looping */
4797 if (depth > 4)
4798 return 1;
4799
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004800 switch (c)
Bram Moolenaara2d95102013-06-04 14:23:05 +02004801 {
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004802 case NFA_SPLIT:
4803 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT)
4804 /* avoid recursive stuff */
4805 return 1;
4806 /* two alternatives, use the lowest failure chance */
4807 l = failure_chance(state->out, depth + 1);
4808 r = failure_chance(state->out1, depth + 1);
4809 return l < r ? l : r;
4810
4811 case NFA_ANY:
4812 /* matches anything, unlikely to fail */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004813 return 1;
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004814
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004815 case NFA_MATCH:
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02004816 case NFA_MCLOSE:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004817 /* empty match works always */
4818 return 0;
4819
Bram Moolenaar44c71db2013-06-14 22:33:51 +02004820 case NFA_START_INVISIBLE:
4821 case NFA_START_INVISIBLE_FIRST:
4822 case NFA_START_INVISIBLE_NEG:
4823 case NFA_START_INVISIBLE_NEG_FIRST:
4824 case NFA_START_INVISIBLE_BEFORE:
4825 case NFA_START_INVISIBLE_BEFORE_FIRST:
4826 case NFA_START_INVISIBLE_BEFORE_NEG:
4827 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
4828 case NFA_START_PATTERN:
4829 /* recursive regmatch is expensive, use low failure chance */
4830 return 5;
4831
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004832 case NFA_BOL:
4833 case NFA_EOL:
4834 case NFA_BOF:
4835 case NFA_EOF:
4836 case NFA_NEWL:
4837 return 99;
4838
4839 case NFA_BOW:
4840 case NFA_EOW:
4841 return 90;
4842
4843 case NFA_MOPEN:
4844 case NFA_MOPEN1:
4845 case NFA_MOPEN2:
4846 case NFA_MOPEN3:
4847 case NFA_MOPEN4:
4848 case NFA_MOPEN5:
4849 case NFA_MOPEN6:
4850 case NFA_MOPEN7:
4851 case NFA_MOPEN8:
4852 case NFA_MOPEN9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004853#ifdef FEAT_SYN_HL
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004854 case NFA_ZOPEN:
4855 case NFA_ZOPEN1:
4856 case NFA_ZOPEN2:
4857 case NFA_ZOPEN3:
4858 case NFA_ZOPEN4:
4859 case NFA_ZOPEN5:
4860 case NFA_ZOPEN6:
4861 case NFA_ZOPEN7:
4862 case NFA_ZOPEN8:
4863 case NFA_ZOPEN9:
4864 case NFA_ZCLOSE:
4865 case NFA_ZCLOSE1:
4866 case NFA_ZCLOSE2:
4867 case NFA_ZCLOSE3:
4868 case NFA_ZCLOSE4:
4869 case NFA_ZCLOSE5:
4870 case NFA_ZCLOSE6:
4871 case NFA_ZCLOSE7:
4872 case NFA_ZCLOSE8:
4873 case NFA_ZCLOSE9:
Bram Moolenaarb76591e2013-06-04 21:42:22 +02004874#endif
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004875 case NFA_NOPEN:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004876 case NFA_MCLOSE1:
4877 case NFA_MCLOSE2:
4878 case NFA_MCLOSE3:
4879 case NFA_MCLOSE4:
4880 case NFA_MCLOSE5:
4881 case NFA_MCLOSE6:
4882 case NFA_MCLOSE7:
4883 case NFA_MCLOSE8:
4884 case NFA_MCLOSE9:
4885 case NFA_NCLOSE:
4886 return failure_chance(state->out, depth + 1);
4887
4888 case NFA_BACKREF1:
4889 case NFA_BACKREF2:
4890 case NFA_BACKREF3:
4891 case NFA_BACKREF4:
4892 case NFA_BACKREF5:
4893 case NFA_BACKREF6:
4894 case NFA_BACKREF7:
4895 case NFA_BACKREF8:
4896 case NFA_BACKREF9:
4897#ifdef FEAT_SYN_HL
4898 case NFA_ZREF1:
4899 case NFA_ZREF2:
4900 case NFA_ZREF3:
4901 case NFA_ZREF4:
4902 case NFA_ZREF5:
4903 case NFA_ZREF6:
4904 case NFA_ZREF7:
4905 case NFA_ZREF8:
4906 case NFA_ZREF9:
4907#endif
4908 /* backreferences don't match in many places */
4909 return 94;
4910
4911 case NFA_LNUM_GT:
4912 case NFA_LNUM_LT:
4913 case NFA_COL_GT:
4914 case NFA_COL_LT:
4915 case NFA_VCOL_GT:
4916 case NFA_VCOL_LT:
4917 case NFA_MARK_GT:
4918 case NFA_MARK_LT:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004919 case NFA_VISUAL:
Bram Moolenaare2b8cb32013-06-05 11:46:25 +02004920 /* before/after positions don't match very often */
4921 return 85;
4922
4923 case NFA_LNUM:
4924 return 90;
4925
4926 case NFA_CURSOR:
4927 case NFA_COL:
4928 case NFA_VCOL:
4929 case NFA_MARK:
4930 /* specific positions rarely match */
4931 return 98;
4932
4933 case NFA_COMPOSING:
4934 return 95;
4935
4936 default:
4937 if (c > 0)
4938 /* character match fails often */
4939 return 95;
4940 }
4941
4942 /* something else, includes character classes */
Bram Moolenaara2d95102013-06-04 14:23:05 +02004943 return 50;
4944}
4945
Bram Moolenaarf46da702013-06-02 22:37:42 +02004946/*
Bram Moolenaar87f764a2013-06-08 14:38:27 +02004947 * Skip until the char "c" we know a match must start with.
4948 */
4949 static int
4950skip_to_start(c, colp)
4951 int c;
4952 colnr_T *colp;
4953{
4954 char_u *s;
4955
4956 /* Used often, do some work to avoid call overhead. */
4957 if (!ireg_ic
4958#ifdef FEAT_MBYTE
4959 && !has_mbyte
4960#endif
4961 )
4962 s = vim_strbyte(regline + *colp, c);
4963 else
4964 s = cstrchr(regline + *colp, c);
4965 if (s == NULL)
4966 return FAIL;
4967 *colp = (int)(s - regline);
4968 return OK;
4969}
4970
4971/*
Bram Moolenaar473de612013-06-08 18:19:48 +02004972 * Check for a match with match_text.
Bram Moolenaare7766ee2013-06-08 22:30:03 +02004973 * Called after skip_to_start() has found regstart.
Bram Moolenaar473de612013-06-08 18:19:48 +02004974 * Returns zero for no match, 1 for a match.
4975 */
4976 static long
4977find_match_text(startcol, regstart, match_text)
4978 colnr_T startcol;
4979 int regstart;
4980 char_u *match_text;
4981{
4982 colnr_T col = startcol;
4983 int c1, c2;
4984 int len1, len2;
4985 int match;
4986
4987 for (;;)
4988 {
4989 match = TRUE;
4990 len2 = MB_CHAR2LEN(regstart); /* skip regstart */
4991 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1))
4992 {
4993 c1 = PTR2CHAR(match_text + len1);
4994 c2 = PTR2CHAR(regline + col + len2);
4995 if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2)))
4996 {
4997 match = FALSE;
4998 break;
4999 }
5000 len2 += MB_CHAR2LEN(c2);
5001 }
5002 if (match
5003#ifdef FEAT_MBYTE
5004 /* check that no composing char follows */
5005 && !(enc_utf8
5006 && utf_iscomposing(PTR2CHAR(regline + col + len2)))
5007#endif
5008 )
5009 {
5010 cleanup_subexpr();
5011 if (REG_MULTI)
5012 {
5013 reg_startpos[0].lnum = reglnum;
5014 reg_startpos[0].col = col;
5015 reg_endpos[0].lnum = reglnum;
5016 reg_endpos[0].col = col + len2;
5017 }
5018 else
5019 {
5020 reg_startp[0] = regline + col;
5021 reg_endp[0] = regline + col + len2;
5022 }
5023 return 1L;
5024 }
5025
5026 /* Try finding regstart after the current match. */
5027 col += MB_CHAR2LEN(regstart); /* skip regstart */
5028 if (skip_to_start(regstart, &col) == FAIL)
5029 break;
5030 }
5031 return 0L;
5032}
5033
5034/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005035 * Main matching routine.
5036 *
5037 * Run NFA to determine whether it matches reginput.
5038 *
Bram Moolenaar307aa162013-06-02 16:34:21 +02005039 * When "nfa_endp" is not NULL it is a required end-of-match position.
Bram Moolenaar61602c52013-06-01 19:54:43 +02005040 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005041 * Return TRUE if there is a match, FALSE otherwise.
5042 * Note: Caller must ensure that: start != NULL.
5043 */
5044 static int
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005045nfa_regmatch(prog, start, submatch, m)
5046 nfa_regprog_T *prog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005047 nfa_state_T *start;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005048 regsubs_T *submatch;
5049 regsubs_T *m;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005050{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005051 int result;
5052 int size = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005053 int flag = 0;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005054 int go_to_nextline = FALSE;
5055 nfa_thread_T *t;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005056 nfa_list_T list[2];
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005057 int listidx;
Bram Moolenaar4b417062013-05-25 20:19:50 +02005058 nfa_list_T *thislist;
5059 nfa_list_T *nextlist;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005060 int *listids = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005061 nfa_state_T *add_state;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005062 int add_here;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005063 int add_count;
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02005064 int add_off = 0;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005065 int toplevel = start->c == NFA_MOPEN;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005066#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005067 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005068
5069 if (debug == NULL)
5070 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005071 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005072 return FALSE;
5073 }
5074#endif
Bram Moolenaar963fee22013-05-26 21:47:28 +02005075 nfa_match = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005076
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005077 /* Allocate memory for the lists of nodes. */
Bram Moolenaar4b417062013-05-25 20:19:50 +02005078 size = (nstate + 1) * sizeof(nfa_thread_T);
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005079 list[0].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005080 list[0].len = nstate + 1;
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005081 list[1].t = (nfa_thread_T *)lalloc(size, TRUE);
Bram Moolenaar428e9872013-05-30 17:05:39 +02005082 list[1].len = nstate + 1;
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005083 if (list[0].t == NULL || list[1].t == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005084 goto theend;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005085
5086#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005087 log_fd = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005088 if (log_fd != NULL)
5089 {
5090 fprintf(log_fd, "**********************************\n");
5091 nfa_set_code(start->c);
5092 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n",
5093 abs(start->id), code);
5094 fprintf(log_fd, "**********************************\n");
5095 }
5096 else
5097 {
5098 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... "));
5099 log_fd = stderr;
5100 }
5101#endif
5102
5103 thislist = &list[0];
5104 thislist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005105 thislist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005106 nextlist = &list[1];
5107 nextlist->n = 0;
Bram Moolenaar196ed142013-07-21 18:59:24 +02005108 nextlist->has_pim = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005109#ifdef ENABLE_LOG
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005110 fprintf(log_fd, "(---) STARTSTATE first\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005111#endif
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005112 thislist->id = nfa_listid + 1;
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005113
5114 /* Inline optimized code for addstate(thislist, start, m, 0) if we know
5115 * it's the first MOPEN. */
5116 if (toplevel)
5117 {
5118 if (REG_MULTI)
5119 {
5120 m->norm.list.multi[0].start.lnum = reglnum;
5121 m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline);
5122 }
5123 else
5124 m->norm.list.line[0].start = reginput;
5125 m->norm.in_use = 1;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005126 addstate(thislist, start->out, m, NULL, 0);
Bram Moolenaarf96d1092013-06-07 22:39:40 +02005127 }
5128 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005129 addstate(thislist, start, m, NULL, 0);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005130
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005131#define ADD_STATE_IF_MATCH(state) \
5132 if (result) { \
Bram Moolenaara2d95102013-06-04 14:23:05 +02005133 add_state = state->out; \
5134 add_off = clen; \
5135 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005136
5137 /*
5138 * Run for each character.
5139 */
Bram Moolenaar35b23862013-05-22 23:00:40 +02005140 for (;;)
5141 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005142 int curc;
5143 int clen;
5144
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005145#ifdef FEAT_MBYTE
5146 if (has_mbyte)
5147 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005148 curc = (*mb_ptr2char)(reginput);
5149 clen = (*mb_ptr2len)(reginput);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005150 }
5151 else
5152#endif
5153 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005154 curc = *reginput;
5155 clen = 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005156 }
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005157 if (curc == NUL)
Bram Moolenaar35b23862013-05-22 23:00:40 +02005158 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005159 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005160 go_to_nextline = FALSE;
5161 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005162
5163 /* swap lists */
5164 thislist = &list[flag];
5165 nextlist = &list[flag ^= 1];
Bram Moolenaar5714b802013-05-28 22:03:20 +02005166 nextlist->n = 0; /* clear nextlist */
Bram Moolenaar196ed142013-07-21 18:59:24 +02005167 nextlist->has_pim = FALSE;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02005168 ++nfa_listid;
5169 thislist->id = nfa_listid;
5170 nextlist->id = nfa_listid + 1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005171
5172#ifdef ENABLE_LOG
5173 fprintf(log_fd, "------------------------------------------\n");
5174 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005175 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005176 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005177 {
5178 int i;
5179
5180 for (i = 0; i < thislist->n; i++)
5181 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
5182 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005183 fprintf(log_fd, "\n");
5184#endif
5185
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005186#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005187 fprintf(debug, "\n-------------------\n");
5188#endif
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005189 /*
5190 * If the state lists are empty we can stop.
5191 */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005192 if (thislist->n == 0)
Bram Moolenaar66e83d72013-05-21 14:03:00 +02005193 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005194
5195 /* compute nextlist */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005196 for (listidx = 0; listidx < thislist->n; ++listidx)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005197 {
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005198 t = &thislist->t[listidx];
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005199
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02005200#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005201 nfa_set_code(t->state->c);
5202 fprintf(debug, "%s, ", code);
5203#endif
5204#ifdef ENABLE_LOG
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005205 {
5206 int col;
5207
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005208 if (t->subs.norm.in_use <= 0)
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005209 col = -1;
5210 else if (REG_MULTI)
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005211 col = t->subs.norm.list.multi[0].start.col;
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005212 else
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02005213 col = (int)(t->subs.norm.list.line[0].start - regline);
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005214 nfa_set_code(t->state->c);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005215 fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n",
5216 abs(t->state->id), (int)t->state->c, code, col,
5217 pim_info(&t->pim));
Bram Moolenaar2d5e1122013-05-30 21:42:13 +02005218 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005219#endif
5220
5221 /*
5222 * Handle the possible codes of the current state.
5223 * The most important is NFA_MATCH.
5224 */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005225 add_state = NULL;
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005226 add_here = FALSE;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005227 add_count = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005228 switch (t->state->c)
5229 {
5230 case NFA_MATCH:
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005231 {
Bram Moolenaar963fee22013-05-26 21:47:28 +02005232 nfa_match = TRUE;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005233 copy_sub(&submatch->norm, &t->subs.norm);
5234#ifdef FEAT_SYN_HL
Bram Moolenaarf6de0322013-06-02 21:30:04 +02005235 if (nfa_has_zsubexpr)
5236 copy_sub(&submatch->synt, &t->subs.synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005237#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005238#ifdef ENABLE_LOG
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005239 log_subsexpr(&t->subs);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005240#endif
Bram Moolenaar35b23862013-05-22 23:00:40 +02005241 /* Found the left-most longest match, do not look at any other
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005242 * states at this position. When the list of states is going
5243 * to be empty quit without advancing, so that "reginput" is
5244 * correct. */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005245 if (nextlist->n == 0)
Bram Moolenaar57a285b2013-05-26 16:57:28 +02005246 clen = 0;
Bram Moolenaar35b23862013-05-22 23:00:40 +02005247 goto nextchar;
Bram Moolenaareb3ecae2013-05-27 11:22:04 +02005248 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005249
5250 case NFA_END_INVISIBLE:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005251 case NFA_END_INVISIBLE_NEG:
Bram Moolenaar87953742013-06-05 18:52:40 +02005252 case NFA_END_PATTERN:
Bram Moolenaarf46da702013-06-02 22:37:42 +02005253 /*
5254 * This is only encountered after a NFA_START_INVISIBLE or
Bram Moolenaar61602c52013-06-01 19:54:43 +02005255 * NFA_START_INVISIBLE_BEFORE node.
5256 * They surround a zero-width group, used with "\@=", "\&",
5257 * "\@!", "\@<=" and "\@<!".
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005258 * If we got here, it means that the current "invisible" group
5259 * finished successfully, so return control to the parent
Bram Moolenaarf46da702013-06-02 22:37:42 +02005260 * nfa_regmatch(). For a look-behind match only when it ends
5261 * in the position in "nfa_endp".
5262 * Submatches are stored in *m, and used in the parent call.
5263 */
Bram Moolenaar61602c52013-06-01 19:54:43 +02005264#ifdef ENABLE_LOG
Bram Moolenaarf46da702013-06-02 22:37:42 +02005265 if (nfa_endp != NULL)
5266 {
5267 if (REG_MULTI)
5268 fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n",
5269 (int)reglnum,
5270 (int)nfa_endp->se_u.pos.lnum,
5271 (int)(reginput - regline),
5272 nfa_endp->se_u.pos.col);
5273 else
5274 fprintf(log_fd, "Current col: %d, endp col: %d\n",
5275 (int)(reginput - regline),
5276 (int)(nfa_endp->se_u.ptr - reginput));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005277 }
Bram Moolenaarf46da702013-06-02 22:37:42 +02005278#endif
Bram Moolenaar87953742013-06-05 18:52:40 +02005279 /* If "nfa_endp" is set it's only a match if it ends at
5280 * "nfa_endp" */
Bram Moolenaarf46da702013-06-02 22:37:42 +02005281 if (nfa_endp != NULL && (REG_MULTI
5282 ? (reglnum != nfa_endp->se_u.pos.lnum
5283 || (int)(reginput - regline)
5284 != nfa_endp->se_u.pos.col)
5285 : reginput != nfa_endp->se_u.ptr))
5286 break;
5287
5288 /* do not set submatches for \@! */
Bram Moolenaardecd9542013-06-07 16:31:50 +02005289 if (t->state->c != NFA_END_INVISIBLE_NEG)
Bram Moolenaarf46da702013-06-02 22:37:42 +02005290 {
5291 copy_sub(&m->norm, &t->subs.norm);
5292#ifdef FEAT_SYN_HL
5293 if (nfa_has_zsubexpr)
5294 copy_sub(&m->synt, &t->subs.synt);
5295#endif
5296 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005297#ifdef ENABLE_LOG
5298 fprintf(log_fd, "Match found:\n");
5299 log_subsexpr(m);
5300#endif
Bram Moolenaarf46da702013-06-02 22:37:42 +02005301 nfa_match = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005302 break;
5303
5304 case NFA_START_INVISIBLE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005305 case NFA_START_INVISIBLE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005306 case NFA_START_INVISIBLE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005307 case NFA_START_INVISIBLE_NEG_FIRST:
Bram Moolenaar61602c52013-06-01 19:54:43 +02005308 case NFA_START_INVISIBLE_BEFORE:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005309 case NFA_START_INVISIBLE_BEFORE_FIRST:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005310 case NFA_START_INVISIBLE_BEFORE_NEG:
Bram Moolenaara2947e22013-06-11 22:44:09 +02005311 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST:
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005312 {
Bram Moolenaarbcf4d172013-06-10 16:35:18 +02005313#ifdef ENABLE_LOG
5314 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n",
5315 failure_chance(t->state->out, 0),
5316 failure_chance(t->state->out1->out, 0));
Bram Moolenaarb76591e2013-06-04 21:42:22 +02005317#endif
Bram Moolenaara2947e22013-06-11 22:44:09 +02005318 /* Do it directly if there already is a PIM or when
5319 * nfa_postprocess() detected it will work better. */
5320 if (t->pim.result != NFA_PIM_UNUSED
5321 || t->state->c == NFA_START_INVISIBLE_FIRST
5322 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5323 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST
5324 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005325 {
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005326 int in_use = m->norm.in_use;
5327
Bram Moolenaarf86c0b02013-06-26 12:42:44 +02005328 /* Copy submatch info for the recursive call, so that
5329 * \1 can be matched. */
5330 copy_sub_off(&m->norm, &t->subs.norm);
5331
Bram Moolenaara2d95102013-06-04 14:23:05 +02005332 /*
5333 * First try matching the invisible match, then what
5334 * follows.
5335 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005336 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaara2d95102013-06-04 14:23:05 +02005337 submatch, m, &listids);
5338
Bram Moolenaardecd9542013-06-07 16:31:50 +02005339 /* for \@! and \@<! it is a match when the result is
5340 * FALSE */
5341 if (result != (t->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02005342 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST
5343 || t->state->c
5344 == NFA_START_INVISIBLE_BEFORE_NEG
5345 || t->state->c
5346 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02005347 {
5348 /* Copy submatch info from the recursive call */
5349 copy_sub_off(&t->subs.norm, &m->norm);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005350#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005351 if (nfa_has_zsubexpr)
5352 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005353#endif
Bram Moolenaar26c2f3f2013-05-26 22:56:19 +02005354
Bram Moolenaara2d95102013-06-04 14:23:05 +02005355 /* t->state->out1 is the corresponding
5356 * END_INVISIBLE node; Add its out to the current
5357 * list (zero-width match). */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005358 add_here = TRUE;
5359 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005360 }
Bram Moolenaar4d9ae212013-06-28 23:04:42 +02005361 m->norm.in_use = in_use;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005362 }
5363 else
5364 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005365 nfa_pim_T pim;
5366
Bram Moolenaara2d95102013-06-04 14:23:05 +02005367 /*
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005368 * First try matching what follows. Only if a match
5369 * is found verify the invisible match matches. Add a
5370 * nfa_pim_T to the following states, it contains info
5371 * about the invisible match.
Bram Moolenaara2d95102013-06-04 14:23:05 +02005372 */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005373 pim.state = t->state;
5374 pim.result = NFA_PIM_TODO;
5375 pim.subs.norm.in_use = 0;
5376#ifdef FEAT_SYN_HL
5377 pim.subs.synt.in_use = 0;
5378#endif
5379 if (REG_MULTI)
5380 {
5381 pim.end.pos.col = (int)(reginput - regline);
5382 pim.end.pos.lnum = reglnum;
5383 }
5384 else
5385 pim.end.ptr = reginput;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005386
5387 /* t->state->out1 is the corresponding END_INVISIBLE
5388 * node; Add its out to the current list (zero-width
5389 * match). */
5390 addstate_here(thislist, t->state->out1->out, &t->subs,
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005391 &pim, &listidx);
Bram Moolenaara2d95102013-06-04 14:23:05 +02005392 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005393 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005394 break;
5395
Bram Moolenaar87953742013-06-05 18:52:40 +02005396 case NFA_START_PATTERN:
Bram Moolenaar43e02982013-06-07 17:31:29 +02005397 {
5398 nfa_state_T *skip = NULL;
5399#ifdef ENABLE_LOG
5400 int skip_lid = 0;
5401#endif
5402
5403 /* There is no point in trying to match the pattern if the
5404 * output state is not going to be added to the list. */
5405 if (state_in_list(nextlist, t->state->out1->out, &t->subs))
5406 {
5407 skip = t->state->out1->out;
5408#ifdef ENABLE_LOG
5409 skip_lid = nextlist->id;
5410#endif
5411 }
5412 else if (state_in_list(nextlist,
5413 t->state->out1->out->out, &t->subs))
5414 {
5415 skip = t->state->out1->out->out;
5416#ifdef ENABLE_LOG
5417 skip_lid = nextlist->id;
5418#endif
5419 }
Bram Moolenaar44c71db2013-06-14 22:33:51 +02005420 else if (state_in_list(thislist,
Bram Moolenaar43e02982013-06-07 17:31:29 +02005421 t->state->out1->out->out, &t->subs))
5422 {
5423 skip = t->state->out1->out->out;
5424#ifdef ENABLE_LOG
5425 skip_lid = thislist->id;
5426#endif
5427 }
5428 if (skip != NULL)
5429 {
5430#ifdef ENABLE_LOG
5431 nfa_set_code(skip->c);
5432 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n",
5433 abs(skip->id), skip_lid, skip->c, code);
5434#endif
5435 break;
5436 }
5437
Bram Moolenaar87953742013-06-05 18:52:40 +02005438 /* First try matching the pattern. */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02005439 result = recursive_regmatch(t->state, NULL, prog,
Bram Moolenaar87953742013-06-05 18:52:40 +02005440 submatch, m, &listids);
5441 if (result)
5442 {
5443 int bytelen;
5444
5445#ifdef ENABLE_LOG
5446 fprintf(log_fd, "NFA_START_PATTERN matches:\n");
5447 log_subsexpr(m);
5448#endif
5449 /* Copy submatch info from the recursive call */
5450 copy_sub_off(&t->subs.norm, &m->norm);
5451#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02005452 if (nfa_has_zsubexpr)
5453 copy_sub_off(&t->subs.synt, &m->synt);
Bram Moolenaar87953742013-06-05 18:52:40 +02005454#endif
5455 /* Now we need to skip over the matched text and then
5456 * continue with what follows. */
5457 if (REG_MULTI)
5458 /* TODO: multi-line match */
5459 bytelen = m->norm.list.multi[0].end.col
5460 - (int)(reginput - regline);
5461 else
5462 bytelen = (int)(m->norm.list.line[0].end - reginput);
5463
5464#ifdef ENABLE_LOG
5465 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen);
5466#endif
5467 if (bytelen == 0)
5468 {
5469 /* empty match, output of corresponding
5470 * NFA_END_PATTERN/NFA_SKIP to be used at current
5471 * position */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005472 add_here = TRUE;
5473 add_state = t->state->out1->out->out;
Bram Moolenaar87953742013-06-05 18:52:40 +02005474 }
5475 else if (bytelen <= clen)
5476 {
5477 /* match current character, output of corresponding
5478 * NFA_END_PATTERN to be used at next position. */
Bram Moolenaar87953742013-06-05 18:52:40 +02005479 add_state = t->state->out1->out->out;
5480 add_off = clen;
5481 }
5482 else
5483 {
5484 /* skip over the matched characters, set character
5485 * count in NFA_SKIP */
Bram Moolenaar87953742013-06-05 18:52:40 +02005486 add_state = t->state->out1->out;
5487 add_off = bytelen;
5488 add_count = bytelen - clen;
5489 }
5490 }
5491 break;
Bram Moolenaar43e02982013-06-07 17:31:29 +02005492 }
Bram Moolenaar87953742013-06-05 18:52:40 +02005493
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005494 case NFA_BOL:
5495 if (reginput == regline)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005496 {
5497 add_here = TRUE;
5498 add_state = t->state->out;
5499 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005500 break;
5501
5502 case NFA_EOL:
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005503 if (curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005504 {
5505 add_here = TRUE;
5506 add_state = t->state->out;
5507 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005508 break;
5509
5510 case NFA_BOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005511 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005512
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005513 if (curc == NUL)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005514 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005515#ifdef FEAT_MBYTE
5516 else if (has_mbyte)
5517 {
5518 int this_class;
5519
5520 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005521 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005522 if (this_class <= 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005523 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005524 else if (reg_prev_class() == this_class)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005525 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005526 }
5527#endif
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005528 else if (!vim_iswordc_buf(curc, reg_buf)
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005529 || (reginput > regline
5530 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005531 result = FALSE;
5532 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005533 {
5534 add_here = TRUE;
5535 add_state = t->state->out;
5536 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005537 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005538
5539 case NFA_EOW:
Bram Moolenaardecd9542013-06-07 16:31:50 +02005540 result = TRUE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005541 if (reginput == regline)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005542 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005543#ifdef FEAT_MBYTE
5544 else if (has_mbyte)
5545 {
5546 int this_class, prev_class;
5547
5548 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005549 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005550 prev_class = reg_prev_class();
5551 if (this_class == prev_class
5552 || prev_class == 0 || prev_class == 1)
Bram Moolenaardecd9542013-06-07 16:31:50 +02005553 result = FALSE;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005554 }
5555#endif
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005556 else if (!vim_iswordc_buf(reginput[-1], reg_buf)
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005557 || (reginput[0] != NUL
5558 && vim_iswordc_buf(curc, reg_buf)))
Bram Moolenaardecd9542013-06-07 16:31:50 +02005559 result = FALSE;
5560 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005561 {
5562 add_here = TRUE;
5563 add_state = t->state->out;
5564 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005565 break;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005566
Bram Moolenaar4b780632013-05-31 22:14:52 +02005567 case NFA_BOF:
5568 if (reglnum == 0 && reginput == regline
5569 && (!REG_MULTI || reg_firstlnum == 1))
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005570 {
5571 add_here = TRUE;
5572 add_state = t->state->out;
5573 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005574 break;
5575
5576 case NFA_EOF:
5577 if (reglnum == reg_maxline && curc == NUL)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005578 {
5579 add_here = TRUE;
5580 add_state = t->state->out;
5581 }
Bram Moolenaar4b780632013-05-31 22:14:52 +02005582 break;
5583
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005584#ifdef FEAT_MBYTE
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005585 case NFA_COMPOSING:
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005586 {
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005587 int mc = curc;
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02005588 int len = 0;
5589 nfa_state_T *end;
5590 nfa_state_T *sta;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005591 int cchars[MAX_MCO];
5592 int ccount = 0;
5593 int j;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005594
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005595 sta = t->state->out;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005596 len = 0;
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005597 if (utf_iscomposing(sta->c))
5598 {
5599 /* Only match composing character(s), ignore base
5600 * character. Used for ".{composing}" and "{composing}"
5601 * (no preceding character). */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005602 len += mb_char2len(mc);
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005603 }
Bram Moolenaar3451d662013-05-26 15:14:55 +02005604 if (ireg_icombine && len == 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005605 {
Bram Moolenaar56d58d52013-05-25 14:42:03 +02005606 /* If \Z was present, then ignore composing characters.
5607 * When ignoring the base character this always matches. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005608 if (len == 0 && sta->c != curc)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005609 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005610 else
5611 result = OK;
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005612 while (sta->c != NFA_END_COMPOSING)
5613 sta = sta->out;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005614 }
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005615
5616 /* Check base character matches first, unless ignored. */
5617 else if (len > 0 || mc == sta->c)
5618 {
5619 if (len == 0)
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005620 {
Bram Moolenaarfad8de02013-05-24 23:10:50 +02005621 len += mb_char2len(mc);
5622 sta = sta->out;
5623 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005624
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005625 /* We don't care about the order of composing characters.
5626 * Get them into cchars[] first. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005627 while (len < clen)
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005628 {
5629 mc = mb_ptr2char(reginput + len);
5630 cchars[ccount++] = mc;
5631 len += mb_char2len(mc);
5632 if (ccount == MAX_MCO)
5633 break;
5634 }
5635
5636 /* Check that each composing char in the pattern matches a
5637 * composing char in the text. We do not check if all
5638 * composing chars are matched. */
5639 result = OK;
5640 while (sta->c != NFA_END_COMPOSING)
5641 {
5642 for (j = 0; j < ccount; ++j)
5643 if (cchars[j] == sta->c)
5644 break;
5645 if (j == ccount)
5646 {
5647 result = FAIL;
5648 break;
5649 }
5650 sta = sta->out;
5651 }
5652 }
5653 else
Bram Moolenaar1d814752013-05-24 20:25:33 +02005654 result = FAIL;
Bram Moolenaar3f1682e2013-05-26 14:32:05 +02005655
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005656 end = t->state->out1; /* NFA_END_COMPOSING */
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005657 ADD_STATE_IF_MATCH(end);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005658 break;
Bram Moolenaar3c577f22013-05-24 21:59:54 +02005659 }
5660#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005661
5662 case NFA_NEWL:
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005663 if (curc == NUL && !reg_line_lbr && REG_MULTI
5664 && reglnum <= reg_maxline)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005665 {
Bram Moolenaar35b23862013-05-22 23:00:40 +02005666 go_to_nextline = TRUE;
5667 /* Pass -1 for the offset, which means taking the position
5668 * at the start of the next line. */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005669 add_state = t->state->out;
5670 add_off = -1;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005671 }
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005672 else if (curc == '\n' && reg_line_lbr)
5673 {
5674 /* match \n as if it is an ordinary character */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005675 add_state = t->state->out;
5676 add_off = 1;
Bram Moolenaar61db8b52013-05-26 17:45:49 +02005677 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005678 break;
5679
Bram Moolenaar417bad22013-06-07 14:08:30 +02005680 case NFA_START_COLL:
5681 case NFA_START_NEG_COLL:
5682 {
5683 /* What follows is a list of characters, until NFA_END_COLL.
5684 * One of them must match or none of them must match. */
5685 nfa_state_T *state;
5686 int result_if_matched;
5687 int c1, c2;
5688
5689 /* Never match EOL. If it's part of the collection it is added
5690 * as a separate state with an OR. */
5691 if (curc == NUL)
5692 break;
5693
5694 state = t->state->out;
5695 result_if_matched = (t->state->c == NFA_START_COLL);
5696 for (;;)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005697 {
Bram Moolenaar417bad22013-06-07 14:08:30 +02005698 if (state->c == NFA_END_COLL)
5699 {
5700 result = !result_if_matched;
5701 break;
5702 }
5703 if (state->c == NFA_RANGE_MIN)
5704 {
5705 c1 = state->val;
5706 state = state->out; /* advance to NFA_RANGE_MAX */
5707 c2 = state->val;
5708#ifdef ENABLE_LOG
5709 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n",
5710 curc, c1, c2);
5711#endif
5712 if (curc >= c1 && curc <= c2)
5713 {
5714 result = result_if_matched;
5715 break;
5716 }
5717 if (ireg_ic)
5718 {
5719 int curc_low = MB_TOLOWER(curc);
5720 int done = FALSE;
5721
5722 for ( ; c1 <= c2; ++c1)
5723 if (MB_TOLOWER(c1) == curc_low)
5724 {
5725 result = result_if_matched;
5726 done = TRUE;
5727 break;
5728 }
5729 if (done)
5730 break;
5731 }
5732 }
5733 else if (state->c < 0 ? check_char_class(state->c, curc)
5734 : (curc == state->c
5735 || (ireg_ic && MB_TOLOWER(curc)
5736 == MB_TOLOWER(state->c))))
5737 {
5738 result = result_if_matched;
5739 break;
5740 }
5741 state = state->out;
5742 }
5743 if (result)
5744 {
5745 /* next state is in out of the NFA_END_COLL, out1 of
5746 * START points to the END state */
Bram Moolenaar417bad22013-06-07 14:08:30 +02005747 add_state = t->state->out1->out;
Bram Moolenaara2d95102013-06-04 14:23:05 +02005748 add_off = clen;
5749 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005750 break;
Bram Moolenaar417bad22013-06-07 14:08:30 +02005751 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005752
5753 case NFA_ANY:
Bram Moolenaar35b23862013-05-22 23:00:40 +02005754 /* Any char except '\0', (end of input) does not match. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005755 if (curc > 0)
Bram Moolenaara2d95102013-06-04 14:23:05 +02005756 {
Bram Moolenaara2d95102013-06-04 14:23:05 +02005757 add_state = t->state->out;
5758 add_off = clen;
5759 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005760 break;
5761
5762 /*
5763 * Character classes like \a for alpha, \d for digit etc.
5764 */
5765 case NFA_IDENT: /* \i */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005766 result = vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005767 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005768 break;
5769
5770 case NFA_SIDENT: /* \I */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005771 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005772 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005773 break;
5774
5775 case NFA_KWORD: /* \k */
Bram Moolenaarf878bf02013-05-21 21:20:20 +02005776 result = vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005777 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005778 break;
5779
5780 case NFA_SKWORD: /* \K */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005781 result = !VIM_ISDIGIT(curc)
5782 && vim_iswordp_buf(reginput, reg_buf);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005783 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005784 break;
5785
5786 case NFA_FNAME: /* \f */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005787 result = vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005788 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005789 break;
5790
5791 case NFA_SFNAME: /* \F */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005792 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005793 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005794 break;
5795
5796 case NFA_PRINT: /* \p */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005797 result = vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005798 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005799 break;
5800
5801 case NFA_SPRINT: /* \P */
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005802 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput));
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005803 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005804 break;
5805
5806 case NFA_WHITE: /* \s */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005807 result = vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005808 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005809 break;
5810
5811 case NFA_NWHITE: /* \S */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005812 result = curc != NUL && !vim_iswhite(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005813 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005814 break;
5815
5816 case NFA_DIGIT: /* \d */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005817 result = ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005818 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005819 break;
5820
5821 case NFA_NDIGIT: /* \D */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005822 result = curc != NUL && !ri_digit(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005823 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005824 break;
5825
5826 case NFA_HEX: /* \x */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005827 result = ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005828 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005829 break;
5830
5831 case NFA_NHEX: /* \X */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005832 result = curc != NUL && !ri_hex(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005833 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005834 break;
5835
5836 case NFA_OCTAL: /* \o */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005837 result = ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005838 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005839 break;
5840
5841 case NFA_NOCTAL: /* \O */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005842 result = curc != NUL && !ri_octal(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005843 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005844 break;
5845
5846 case NFA_WORD: /* \w */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005847 result = ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005848 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005849 break;
5850
5851 case NFA_NWORD: /* \W */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005852 result = curc != NUL && !ri_word(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005853 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005854 break;
5855
5856 case NFA_HEAD: /* \h */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005857 result = ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005858 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005859 break;
5860
5861 case NFA_NHEAD: /* \H */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005862 result = curc != NUL && !ri_head(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005863 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005864 break;
5865
5866 case NFA_ALPHA: /* \a */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005867 result = ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005868 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005869 break;
5870
5871 case NFA_NALPHA: /* \A */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005872 result = curc != NUL && !ri_alpha(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005873 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005874 break;
5875
5876 case NFA_LOWER: /* \l */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005877 result = ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005878 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005879 break;
5880
5881 case NFA_NLOWER: /* \L */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005882 result = curc != NUL && !ri_lower(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005883 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005884 break;
5885
5886 case NFA_UPPER: /* \u */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005887 result = ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005888 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005889 break;
5890
5891 case NFA_NUPPER: /* \U */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02005892 result = curc != NUL && !ri_upper(curc);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02005893 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02005894 break;
5895
Bram Moolenaar1cfad522013-08-14 12:06:49 +02005896 case NFA_LOWER_IC: /* [a-z] */
5897 result = ri_lower(curc) || (ireg_ic && ri_upper(curc));
5898 ADD_STATE_IF_MATCH(t->state);
5899 break;
5900
5901 case NFA_NLOWER_IC: /* [^a-z] */
5902 result = curc != NUL
5903 && !(ri_lower(curc) || (ireg_ic && ri_upper(curc)));
5904 ADD_STATE_IF_MATCH(t->state);
5905 break;
5906
5907 case NFA_UPPER_IC: /* [A-Z] */
5908 result = ri_upper(curc) || (ireg_ic && ri_lower(curc));
5909 ADD_STATE_IF_MATCH(t->state);
5910 break;
5911
5912 case NFA_NUPPER_IC: /* ^[A-Z] */
5913 result = curc != NUL
5914 && !(ri_upper(curc) || (ireg_ic && ri_lower(curc)));
5915 ADD_STATE_IF_MATCH(t->state);
5916 break;
5917
Bram Moolenaar5714b802013-05-28 22:03:20 +02005918 case NFA_BACKREF1:
5919 case NFA_BACKREF2:
5920 case NFA_BACKREF3:
5921 case NFA_BACKREF4:
5922 case NFA_BACKREF5:
5923 case NFA_BACKREF6:
5924 case NFA_BACKREF7:
5925 case NFA_BACKREF8:
5926 case NFA_BACKREF9:
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005927#ifdef FEAT_SYN_HL
5928 case NFA_ZREF1:
5929 case NFA_ZREF2:
5930 case NFA_ZREF3:
5931 case NFA_ZREF4:
5932 case NFA_ZREF5:
5933 case NFA_ZREF6:
5934 case NFA_ZREF7:
5935 case NFA_ZREF8:
5936 case NFA_ZREF9:
5937#endif
5938 /* \1 .. \9 \z1 .. \z9 */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005939 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005940 int subidx;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005941 int bytelen;
5942
Bram Moolenaarefb23f22013-06-01 23:02:54 +02005943 if (t->state->c <= NFA_BACKREF9)
5944 {
5945 subidx = t->state->c - NFA_BACKREF1 + 1;
5946 result = match_backref(&t->subs.norm, subidx, &bytelen);
5947 }
5948#ifdef FEAT_SYN_HL
5949 else
5950 {
5951 subidx = t->state->c - NFA_ZREF1 + 1;
5952 result = match_zref(subidx, &bytelen);
5953 }
5954#endif
5955
Bram Moolenaar5714b802013-05-28 22:03:20 +02005956 if (result)
5957 {
5958 if (bytelen == 0)
5959 {
Bram Moolenaarb122e972013-06-02 16:07:10 +02005960 /* empty match always works, output of NFA_SKIP to be
5961 * used next */
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02005962 add_here = TRUE;
5963 add_state = t->state->out->out;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005964 }
5965 else if (bytelen <= clen)
5966 {
5967 /* match current character, jump ahead to out of
5968 * NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005969 add_state = t->state->out->out;
5970 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005971 }
5972 else
5973 {
Bram Moolenaarf8115092013-06-04 17:47:05 +02005974 /* skip over the matched characters, set character
Bram Moolenaar5714b802013-05-28 22:03:20 +02005975 * count in NFA_SKIP */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005976 add_state = t->state->out;
5977 add_off = bytelen;
5978 add_count = bytelen - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005979 }
Bram Moolenaar5714b802013-05-28 22:03:20 +02005980 }
Bram Moolenaar12e40142013-05-21 15:33:41 +02005981 break;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005982 }
5983 case NFA_SKIP:
Bram Moolenaar67604ae2013-06-05 16:51:57 +02005984 /* character of previous matching \1 .. \9 or \@> */
Bram Moolenaar5714b802013-05-28 22:03:20 +02005985 if (t->count - clen <= 0)
5986 {
5987 /* end of match, go to what follows */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005988 add_state = t->state->out;
5989 add_off = clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005990 }
5991 else
5992 {
5993 /* add state again with decremented count */
Bram Moolenaara2d95102013-06-04 14:23:05 +02005994 add_state = t->state;
5995 add_off = 0;
5996 add_count = t->count - clen;
Bram Moolenaar5714b802013-05-28 22:03:20 +02005997 }
5998 break;
Bram Moolenaar12e40142013-05-21 15:33:41 +02005999
Bram Moolenaar423532e2013-05-29 21:14:42 +02006000 case NFA_LNUM:
6001 case NFA_LNUM_GT:
6002 case NFA_LNUM_LT:
6003 result = (REG_MULTI &&
6004 nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM,
6005 (long_u)(reglnum + reg_firstlnum)));
6006 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006007 {
6008 add_here = TRUE;
6009 add_state = t->state->out;
6010 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006011 break;
6012
6013 case NFA_COL:
6014 case NFA_COL_GT:
6015 case NFA_COL_LT:
6016 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL,
6017 (long_u)(reginput - regline) + 1);
6018 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006019 {
6020 add_here = TRUE;
6021 add_state = t->state->out;
6022 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006023 break;
6024
6025 case NFA_VCOL:
6026 case NFA_VCOL_GT:
6027 case NFA_VCOL_LT:
6028 result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL,
6029 (long_u)win_linetabsize(
6030 reg_win == NULL ? curwin : reg_win,
6031 regline, (colnr_T)(reginput - regline)) + 1);
6032 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006033 {
6034 add_here = TRUE;
6035 add_state = t->state->out;
6036 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006037 break;
6038
Bram Moolenaar044aa292013-06-04 21:27:38 +02006039 case NFA_MARK:
6040 case NFA_MARK_GT:
6041 case NFA_MARK_LT:
6042 {
6043 pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE);
6044
6045 /* Compare the mark position to the match position. */
6046 result = (pos != NULL /* mark doesn't exist */
6047 && pos->lnum > 0 /* mark isn't set in reg_buf */
6048 && (pos->lnum == reglnum + reg_firstlnum
6049 ? (pos->col == (colnr_T)(reginput - regline)
6050 ? t->state->c == NFA_MARK
6051 : (pos->col < (colnr_T)(reginput - regline)
6052 ? t->state->c == NFA_MARK_GT
6053 : t->state->c == NFA_MARK_LT))
6054 : (pos->lnum < reglnum + reg_firstlnum
6055 ? t->state->c == NFA_MARK_GT
6056 : t->state->c == NFA_MARK_LT)));
6057 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006058 {
6059 add_here = TRUE;
6060 add_state = t->state->out;
6061 }
Bram Moolenaar044aa292013-06-04 21:27:38 +02006062 break;
6063 }
6064
Bram Moolenaar423532e2013-05-29 21:14:42 +02006065 case NFA_CURSOR:
6066 result = (reg_win != NULL
6067 && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum)
6068 && ((colnr_T)(reginput - regline)
6069 == reg_win->w_cursor.col));
6070 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006071 {
6072 add_here = TRUE;
6073 add_state = t->state->out;
6074 }
Bram Moolenaar423532e2013-05-29 21:14:42 +02006075 break;
6076
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006077 case NFA_VISUAL:
Bram Moolenaar973fced2013-06-05 21:10:59 +02006078#ifdef FEAT_VISUAL
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006079 result = reg_match_visual();
6080 if (result)
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006081 {
6082 add_here = TRUE;
6083 add_state = t->state->out;
6084 }
Bram Moolenaar78eae9a2013-06-05 11:02:05 +02006085#endif
Bram Moolenaar973fced2013-06-05 21:10:59 +02006086 break;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02006087
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006088 case NFA_MOPEN1:
6089 case NFA_MOPEN2:
6090 case NFA_MOPEN3:
6091 case NFA_MOPEN4:
6092 case NFA_MOPEN5:
6093 case NFA_MOPEN6:
6094 case NFA_MOPEN7:
6095 case NFA_MOPEN8:
6096 case NFA_MOPEN9:
6097#ifdef FEAT_SYN_HL
6098 case NFA_ZOPEN:
6099 case NFA_ZOPEN1:
6100 case NFA_ZOPEN2:
6101 case NFA_ZOPEN3:
6102 case NFA_ZOPEN4:
6103 case NFA_ZOPEN5:
6104 case NFA_ZOPEN6:
6105 case NFA_ZOPEN7:
6106 case NFA_ZOPEN8:
6107 case NFA_ZOPEN9:
6108#endif
6109 case NFA_NOPEN:
6110 case NFA_ZSTART:
6111 /* These states are only added to be able to bail out when
6112 * they are added again, nothing is to be done. */
6113 break;
6114
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006115 default: /* regular character */
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006116 {
6117 int c = t->state->c;
Bram Moolenaar12e40142013-05-21 15:33:41 +02006118
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006119#ifdef DEBUG
Bram Moolenaardecd9542013-06-07 16:31:50 +02006120 if (c < 0)
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006121 EMSGN("INTERNAL: Negative state char: %ld", c);
Bram Moolenaar398d53d2013-08-01 15:45:52 +02006122#endif
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006123 result = (c == curc);
6124
6125 if (!result && ireg_ic)
6126 result = MB_TOLOWER(c) == MB_TOLOWER(curc);
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006127#ifdef FEAT_MBYTE
6128 /* If there is a composing character which is not being
6129 * ignored there can be no match. Match with composing
6130 * character uses NFA_COMPOSING above. */
6131 if (result && enc_utf8 && !ireg_icombine
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006132 && clen != utf_char2len(curc))
Bram Moolenaar3c577f22013-05-24 21:59:54 +02006133 result = FALSE;
6134#endif
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006135 ADD_STATE_IF_MATCH(t->state);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006136 break;
Bram Moolenaarc4912e52013-05-26 19:19:52 +02006137 }
Bram Moolenaara2d95102013-06-04 14:23:05 +02006138
6139 } /* switch (t->state->c) */
6140
6141 if (add_state != NULL)
6142 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006143 nfa_pim_T *pim;
6144
6145 if (t->pim.result == NFA_PIM_UNUSED)
6146 pim = NULL;
6147 else
6148 pim = &t->pim;
6149
6150 /* Handle the postponed invisible match if the match might end
6151 * without advancing and before the end of the line. */
6152 if (pim != NULL && (clen == 0 || match_follows(add_state, 0)))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006153 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006154 if (pim->result == NFA_PIM_TODO)
Bram Moolenaara2d95102013-06-04 14:23:05 +02006155 {
6156#ifdef ENABLE_LOG
6157 fprintf(log_fd, "\n");
6158 fprintf(log_fd, "==================================\n");
6159 fprintf(log_fd, "Postponed recursive nfa_regmatch()\n");
6160 fprintf(log_fd, "\n");
6161#endif
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006162 result = recursive_regmatch(pim->state, pim,
Bram Moolenaara2d95102013-06-04 14:23:05 +02006163 prog, submatch, m, &listids);
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006164 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH;
Bram Moolenaardecd9542013-06-07 16:31:50 +02006165 /* for \@! and \@<! it is a match when the result is
6166 * FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006167 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006168 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6169 || pim->state->c
6170 == NFA_START_INVISIBLE_BEFORE_NEG
6171 || pim->state->c
6172 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006173 {
6174 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006175 copy_sub_off(&pim->subs.norm, &m->norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006176#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006177 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006178 copy_sub_off(&pim->subs.synt, &m->synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006179#endif
6180 }
6181 }
6182 else
6183 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006184 result = (pim->result == NFA_PIM_MATCH);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006185#ifdef ENABLE_LOG
6186 fprintf(log_fd, "\n");
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006187 fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006188 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE");
6189 fprintf(log_fd, "\n");
6190#endif
6191 }
6192
Bram Moolenaardecd9542013-06-07 16:31:50 +02006193 /* for \@! and \@<! it is a match when result is FALSE */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006194 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG
Bram Moolenaara2947e22013-06-11 22:44:09 +02006195 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST
6196 || pim->state->c
6197 == NFA_START_INVISIBLE_BEFORE_NEG
6198 || pim->state->c
6199 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST))
Bram Moolenaara2d95102013-06-04 14:23:05 +02006200 {
6201 /* Copy submatch info from the recursive call */
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006202 copy_sub_off(&t->subs.norm, &pim->subs.norm);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006203#ifdef FEAT_SYN_HL
Bram Moolenaar188c57b2013-06-06 16:22:06 +02006204 if (nfa_has_zsubexpr)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006205 copy_sub_off(&t->subs.synt, &pim->subs.synt);
Bram Moolenaara2d95102013-06-04 14:23:05 +02006206#endif
6207 }
6208 else
6209 /* look-behind match failed, don't add the state */
6210 continue;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006211
6212 /* Postponed invisible match was handled, don't add it to
6213 * following states. */
6214 pim = NULL;
Bram Moolenaara2d95102013-06-04 14:23:05 +02006215 }
6216
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006217 if (add_here)
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006218 addstate_here(thislist, add_state, &t->subs, pim, &listidx);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006219 else
6220 {
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006221 addstate(nextlist, add_state, &t->subs, pim, add_off);
Bram Moolenaarb1b284f2013-06-08 13:33:37 +02006222 if (add_count > 0)
6223 nextlist->t[nextlist->n - 1].count = add_count;
6224 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006225 }
6226
6227 } /* for (thislist = thislist; thislist->state; thislist++) */
6228
Bram Moolenaare23febd2013-05-26 18:40:14 +02006229 /* Look for the start of a match in the current position by adding the
6230 * start state to the list of states.
6231 * The first found match is the leftmost one, thus the order of states
6232 * matters!
6233 * Do not add the start state in recursive calls of nfa_regmatch(),
6234 * because recursive calls should only start in the first position.
Bram Moolenaar307aa162013-06-02 16:34:21 +02006235 * Unless "nfa_endp" is not NULL, then we match the end position.
Bram Moolenaare23febd2013-05-26 18:40:14 +02006236 * Also don't start a match past the first line. */
Bram Moolenaar61602c52013-06-01 19:54:43 +02006237 if (nfa_match == FALSE
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006238 && ((toplevel
Bram Moolenaar61602c52013-06-01 19:54:43 +02006239 && reglnum == 0
6240 && clen != 0
6241 && (ireg_maxcol == 0
6242 || (colnr_T)(reginput - regline) < ireg_maxcol))
Bram Moolenaar307aa162013-06-02 16:34:21 +02006243 || (nfa_endp != NULL
Bram Moolenaar61602c52013-06-01 19:54:43 +02006244 && (REG_MULTI
Bram Moolenaar307aa162013-06-02 16:34:21 +02006245 ? (reglnum < nfa_endp->se_u.pos.lnum
6246 || (reglnum == nfa_endp->se_u.pos.lnum
Bram Moolenaar61602c52013-06-01 19:54:43 +02006247 && (int)(reginput - regline)
Bram Moolenaar307aa162013-06-02 16:34:21 +02006248 < nfa_endp->se_u.pos.col))
6249 : reginput < nfa_endp->se_u.ptr))))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006250 {
6251#ifdef ENABLE_LOG
6252 fprintf(log_fd, "(---) STARTSTATE\n");
6253#endif
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006254 /* Inline optimized code for addstate() if we know the state is
6255 * the first MOPEN. */
6256 if (toplevel)
6257 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006258 int add = TRUE;
6259 int c;
6260
6261 if (prog->regstart != NUL && clen != 0)
6262 {
6263 if (nextlist->n == 0)
6264 {
6265 colnr_T col = (colnr_T)(reginput - regline) + clen;
6266
6267 /* Nextlist is empty, we can skip ahead to the
6268 * character that must appear at the start. */
6269 if (skip_to_start(prog->regstart, &col) == FAIL)
6270 break;
6271#ifdef ENABLE_LOG
6272 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n",
6273 col - ((colnr_T)(reginput - regline) + clen));
6274#endif
6275 reginput = regline + col - clen;
6276 }
6277 else
6278 {
6279 /* Checking if the required start character matches is
6280 * cheaper than adding a state that won't match. */
6281 c = PTR2CHAR(reginput + clen);
6282 if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c)
6283 != MB_TOLOWER(prog->regstart)))
6284 {
6285#ifdef ENABLE_LOG
6286 fprintf(log_fd, " Skipping start state, regstart does not match\n");
6287#endif
6288 add = FALSE;
6289 }
6290 }
6291 }
6292
6293 if (add)
6294 {
6295 if (REG_MULTI)
6296 m->norm.list.multi[0].start.col =
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006297 (colnr_T)(reginput - regline) + clen;
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006298 else
6299 m->norm.list.line[0].start = reginput + clen;
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006300 addstate(nextlist, start->out, m, NULL, clen);
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006301 }
Bram Moolenaarf96d1092013-06-07 22:39:40 +02006302 }
6303 else
Bram Moolenaar2a4e98a2013-06-09 16:24:45 +02006304 addstate(nextlist, start, m, NULL, clen);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006305 }
6306
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006307#ifdef ENABLE_LOG
6308 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n);
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006309 {
6310 int i;
6311
6312 for (i = 0; i < thislist->n; i++)
6313 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id));
6314 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006315 fprintf(log_fd, "\n");
6316#endif
6317
6318nextchar:
Bram Moolenaar35b23862013-05-22 23:00:40 +02006319 /* Advance to the next character, or advance to the next line, or
6320 * finish. */
Bram Moolenaar7cd4d9c2013-05-26 14:54:12 +02006321 if (clen != 0)
6322 reginput += clen;
Bram Moolenaar307aa162013-06-02 16:34:21 +02006323 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI
6324 && reglnum < nfa_endp->se_u.pos.lnum))
Bram Moolenaar35b23862013-05-22 23:00:40 +02006325 reg_nextline();
6326 else
6327 break;
6328 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006329
6330#ifdef ENABLE_LOG
6331 if (log_fd != stderr)
6332 fclose(log_fd);
6333 log_fd = NULL;
6334#endif
6335
6336theend:
6337 /* Free memory */
6338 vim_free(list[0].t);
6339 vim_free(list[1].t);
Bram Moolenaar963fee22013-05-26 21:47:28 +02006340 vim_free(listids);
Bram Moolenaar8aca2e92013-06-07 14:59:18 +02006341#undef ADD_STATE_IF_MATCH
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02006342#ifdef NFA_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006343 fclose(debug);
6344#endif
6345
Bram Moolenaar963fee22013-05-26 21:47:28 +02006346 return nfa_match;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006347}
6348
6349/*
6350 * Try match of "prog" with at regline["col"].
6351 * Returns 0 for failure, number of lines contained in the match otherwise.
6352 */
6353 static long
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006354nfa_regtry(prog, col)
6355 nfa_regprog_T *prog;
6356 colnr_T col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006357{
6358 int i;
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006359 regsubs_T subs, m;
6360 nfa_state_T *start = prog->start;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006361#ifdef ENABLE_LOG
6362 FILE *f;
6363#endif
6364
6365 reginput = regline + col;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006366
6367#ifdef ENABLE_LOG
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006368 f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006369 if (f != NULL)
6370 {
Bram Moolenaar87953742013-06-05 18:52:40 +02006371 fprintf(f, "\n\n\t=======================================================\n");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006372#ifdef DEBUG
6373 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr);
6374#endif
6375 fprintf(f, "\tInput text is \"%s\" \n", reginput);
Bram Moolenaar87953742013-06-05 18:52:40 +02006376 fprintf(f, "\t=======================================================\n\n");
Bram Moolenaar152e7892013-05-25 12:28:11 +02006377 nfa_print_state(f, start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006378 fprintf(f, "\n\n");
6379 fclose(f);
6380 }
6381 else
6382 EMSG(_("Could not open temporary log file for writing "));
6383#endif
6384
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006385 clear_sub(&subs.norm);
6386 clear_sub(&m.norm);
6387#ifdef FEAT_SYN_HL
6388 clear_sub(&subs.synt);
6389 clear_sub(&m.synt);
6390#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006391
Bram Moolenaarf6de0322013-06-02 21:30:04 +02006392 if (nfa_regmatch(prog, start, &subs, &m) == FALSE)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006393 return 0;
6394
6395 cleanup_subexpr();
6396 if (REG_MULTI)
6397 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006398 for (i = 0; i < subs.norm.in_use; i++)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006399 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006400 reg_startpos[i] = subs.norm.list.multi[i].start;
6401 reg_endpos[i] = subs.norm.list.multi[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006402 }
6403
6404 if (reg_startpos[0].lnum < 0)
6405 {
6406 reg_startpos[0].lnum = 0;
6407 reg_startpos[0].col = col;
6408 }
6409 if (reg_endpos[0].lnum < 0)
6410 {
Bram Moolenaare0fea9c2013-05-27 20:10:50 +02006411 /* pattern has a \ze but it didn't match, use current end */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006412 reg_endpos[0].lnum = reglnum;
6413 reg_endpos[0].col = (int)(reginput - regline);
6414 }
6415 else
6416 /* Use line number of "\ze". */
6417 reglnum = reg_endpos[0].lnum;
6418 }
6419 else
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_startp[i] = subs.norm.list.line[i].start;
6424 reg_endp[i] = subs.norm.list.line[i].end;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006425 }
6426
6427 if (reg_startp[0] == NULL)
6428 reg_startp[0] = regline + col;
6429 if (reg_endp[0] == NULL)
6430 reg_endp[0] = reginput;
6431 }
6432
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006433#ifdef FEAT_SYN_HL
6434 /* Package any found \z(...\) matches for export. Default is none. */
6435 unref_extmatch(re_extmatch_out);
6436 re_extmatch_out = NULL;
6437
6438 if (prog->reghasz == REX_SET)
6439 {
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006440 cleanup_zsubexpr();
6441 re_extmatch_out = make_extmatch();
6442 for (i = 0; i < subs.synt.in_use; i++)
6443 {
6444 if (REG_MULTI)
6445 {
6446 struct multipos *mpos = &subs.synt.list.multi[i];
6447
6448 /* Only accept single line matches. */
6449 if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum)
6450 re_extmatch_out->matches[i] =
6451 vim_strnsave(reg_getline(mpos->start.lnum)
6452 + mpos->start.col,
6453 mpos->end.col - mpos->start.col);
6454 }
6455 else
6456 {
6457 struct linepos *lpos = &subs.synt.list.line[i];
6458
6459 if (lpos->start != NULL && lpos->end != NULL)
6460 re_extmatch_out->matches[i] =
6461 vim_strnsave(lpos->start,
6462 (int)(lpos->end - lpos->start));
6463 }
6464 }
6465 }
6466#endif
6467
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006468 return 1 + reglnum;
6469}
6470
6471/*
6472 * Match a regexp against a string ("line" points to the string) or multiple
6473 * lines ("line" is NULL, use reg_getline()).
6474 *
6475 * Returns 0 for failure, number of lines contained in the match otherwise.
6476 */
6477 static long
Bram Moolenaard89616e2013-06-06 18:46:06 +02006478nfa_regexec_both(line, startcol)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006479 char_u *line;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006480 colnr_T startcol; /* column to start looking for match */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006481{
6482 nfa_regprog_T *prog;
6483 long retval = 0L;
6484 int i;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006485 colnr_T col = startcol;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006486
6487 if (REG_MULTI)
6488 {
6489 prog = (nfa_regprog_T *)reg_mmatch->regprog;
6490 line = reg_getline((linenr_T)0); /* relative to the cursor */
6491 reg_startpos = reg_mmatch->startpos;
6492 reg_endpos = reg_mmatch->endpos;
6493 }
6494 else
6495 {
6496 prog = (nfa_regprog_T *)reg_match->regprog;
6497 reg_startp = reg_match->startp;
6498 reg_endp = reg_match->endp;
6499 }
6500
6501 /* Be paranoid... */
6502 if (prog == NULL || line == NULL)
6503 {
6504 EMSG(_(e_null));
6505 goto theend;
6506 }
6507
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006508 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
6509 if (prog->regflags & RF_ICASE)
6510 ireg_ic = TRUE;
6511 else if (prog->regflags & RF_NOICASE)
6512 ireg_ic = FALSE;
6513
6514#ifdef FEAT_MBYTE
6515 /* If pattern contains "\Z" overrule value of ireg_icombine */
6516 if (prog->regflags & RF_ICOMBINE)
6517 ireg_icombine = TRUE;
6518#endif
6519
6520 regline = line;
6521 reglnum = 0; /* relative to line */
6522
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006523 nfa_has_zend = prog->has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006524 nfa_has_backref = prog->has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006525 nfa_nsubexpr = prog->nsubexp;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006526 nfa_listid = 1;
6527 nfa_alt_listid = 2;
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006528#ifdef DEBUG
6529 nfa_regengine.expr = prog->pattern;
6530#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006531
Bram Moolenaard89616e2013-06-06 18:46:06 +02006532 if (prog->reganch && col > 0)
6533 return 0L;
6534
Bram Moolenaar473de612013-06-08 18:19:48 +02006535 need_clear_subexpr = TRUE;
6536#ifdef FEAT_SYN_HL
6537 /* Clear the external match subpointers if necessary. */
6538 if (prog->reghasz == REX_SET)
6539 {
6540 nfa_has_zsubexpr = TRUE;
6541 need_clear_zsubexpr = TRUE;
6542 }
6543 else
6544 nfa_has_zsubexpr = FALSE;
6545#endif
6546
Bram Moolenaard89616e2013-06-06 18:46:06 +02006547 if (prog->regstart != NUL)
Bram Moolenaar473de612013-06-08 18:19:48 +02006548 {
Bram Moolenaar87f764a2013-06-08 14:38:27 +02006549 /* Skip ahead until a character we know the match must start with.
6550 * When there is none there is no match. */
6551 if (skip_to_start(prog->regstart, &col) == FAIL)
Bram Moolenaard89616e2013-06-06 18:46:06 +02006552 return 0L;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006553
Bram Moolenaar473de612013-06-08 18:19:48 +02006554 /* If match_text is set it contains the full text that must match.
6555 * Nothing else to try. Doesn't handle combining chars well. */
Bram Moolenaara940aa62013-06-08 23:30:04 +02006556 if (prog->match_text != NULL
6557#ifdef FEAT_MBYTE
6558 && !ireg_icombine
6559#endif
6560 )
Bram Moolenaar473de612013-06-08 18:19:48 +02006561 return find_match_text(col, prog->regstart, prog->match_text);
6562 }
6563
Bram Moolenaard89616e2013-06-06 18:46:06 +02006564 /* If the start column is past the maximum column: no need to try. */
6565 if (ireg_maxcol > 0 && col >= ireg_maxcol)
6566 goto theend;
6567
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006568 nstate = prog->nstate;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006569 for (i = 0; i < nstate; ++i)
6570 {
6571 prog->state[i].id = i;
Bram Moolenaardd2ccdf2013-06-03 12:17:04 +02006572 prog->state[i].lastlist[0] = 0;
6573 prog->state[i].lastlist[1] = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006574 }
6575
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006576 retval = nfa_regtry(prog, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006577
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006578#ifdef DEBUG
6579 nfa_regengine.expr = NULL;
6580#endif
6581
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006582theend:
6583 return retval;
6584}
6585
6586/*
6587 * Compile a regular expression into internal code for the NFA matcher.
6588 * Returns the program in allocated space. Returns NULL for an error.
6589 */
6590 static regprog_T *
6591nfa_regcomp(expr, re_flags)
6592 char_u *expr;
6593 int re_flags;
6594{
Bram Moolenaaraae48832013-05-25 21:18:34 +02006595 nfa_regprog_T *prog = NULL;
Bram Moolenaarca12d7c2013-05-20 21:26:33 +02006596 size_t prog_size;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006597 int *postfix;
6598
6599 if (expr == NULL)
6600 return NULL;
6601
6602#ifdef DEBUG
6603 nfa_regengine.expr = expr;
6604#endif
6605
6606 init_class_tab();
6607
6608 if (nfa_regcomp_start(expr, re_flags) == FAIL)
6609 return NULL;
6610
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006611 /* Build postfix form of the regexp. Needed to build the NFA
Bram Moolenaaraae48832013-05-25 21:18:34 +02006612 * (and count its size). */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006613 postfix = re2post();
6614 if (postfix == NULL)
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006615 {
6616 /* TODO: only give this error for debugging? */
6617 if (post_ptr >= post_end)
6618 EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006619 goto fail; /* Cascaded (syntax?) error */
Bram Moolenaar61db8b52013-05-26 17:45:49 +02006620 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006621
6622 /*
6623 * In order to build the NFA, we parse the input regexp twice:
6624 * 1. first pass to count size (so we can allocate space)
6625 * 2. second to emit code
6626 */
6627#ifdef ENABLE_LOG
6628 {
Bram Moolenaard6c11cb2013-05-25 12:18:39 +02006629 FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006630
6631 if (f != NULL)
6632 {
6633 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr);
6634 fclose(f);
6635 }
6636 }
6637#endif
6638
6639 /*
6640 * PASS 1
6641 * Count number of NFA states in "nstate". Do not build the NFA.
6642 */
6643 post2nfa(postfix, post_ptr, TRUE);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006644
Bram Moolenaar16619a22013-06-11 18:42:36 +02006645 /* allocate the regprog with space for the compiled regexp */
6646 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1);
Bram Moolenaaraae48832013-05-25 21:18:34 +02006647 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE);
6648 if (prog == NULL)
6649 goto fail;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006650 state_ptr = prog->state;
6651
6652 /*
6653 * PASS 2
6654 * Build the NFA
6655 */
6656 prog->start = post2nfa(postfix, post_ptr, FALSE);
6657 if (prog->start == NULL)
6658 goto fail;
6659
6660 prog->regflags = regflags;
6661 prog->engine = &nfa_regengine;
6662 prog->nstate = nstate;
Bram Moolenaar57a285b2013-05-26 16:57:28 +02006663 prog->has_zend = nfa_has_zend;
Bram Moolenaar428e9872013-05-30 17:05:39 +02006664 prog->has_backref = nfa_has_backref;
Bram Moolenaar963fee22013-05-26 21:47:28 +02006665 prog->nsubexp = regnpar;
Bram Moolenaard89616e2013-06-06 18:46:06 +02006666
Bram Moolenaara2947e22013-06-11 22:44:09 +02006667 nfa_postprocess(prog);
6668
Bram Moolenaard89616e2013-06-06 18:46:06 +02006669 prog->reganch = nfa_get_reganch(prog->start, 0);
6670 prog->regstart = nfa_get_regstart(prog->start, 0);
Bram Moolenaar473de612013-06-08 18:19:48 +02006671 prog->match_text = nfa_get_match_text(prog->start);
6672
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006673#ifdef ENABLE_LOG
6674 nfa_postfix_dump(expr, OK);
6675 nfa_dump(prog);
6676#endif
Bram Moolenaarefb23f22013-06-01 23:02:54 +02006677#ifdef FEAT_SYN_HL
6678 /* Remember whether this pattern has any \z specials in it. */
6679 prog->reghasz = re_has_z;
6680#endif
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006681#ifdef DEBUG
Bram Moolenaar473de612013-06-08 18:19:48 +02006682 prog->pattern = vim_strsave(expr);
Bram Moolenaar69afb7b2013-06-02 15:55:55 +02006683 nfa_regengine.expr = NULL;
6684#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006685
6686out:
6687 vim_free(post_start);
6688 post_start = post_ptr = post_end = NULL;
6689 state_ptr = NULL;
6690 return (regprog_T *)prog;
6691
6692fail:
6693 vim_free(prog);
6694 prog = NULL;
6695#ifdef ENABLE_LOG
6696 nfa_postfix_dump(expr, FAIL);
6697#endif
6698#ifdef DEBUG
6699 nfa_regengine.expr = NULL;
6700#endif
6701 goto out;
6702}
6703
Bram Moolenaar473de612013-06-08 18:19:48 +02006704/*
6705 * Free a compiled regexp program, returned by nfa_regcomp().
6706 */
6707 static void
6708nfa_regfree(prog)
6709 regprog_T *prog;
6710{
6711 if (prog != NULL)
6712 {
6713 vim_free(((nfa_regprog_T *)prog)->match_text);
6714#ifdef DEBUG
6715 vim_free(((nfa_regprog_T *)prog)->pattern);
6716#endif
6717 vim_free(prog);
6718 }
6719}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006720
6721/*
6722 * Match a regexp against a string.
6723 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp().
6724 * Uses curbuf for line count and 'iskeyword'.
6725 *
6726 * Return TRUE if there is a match, FALSE if not.
6727 */
6728 static int
6729nfa_regexec(rmp, line, col)
6730 regmatch_T *rmp;
6731 char_u *line; /* string to match against */
6732 colnr_T col; /* column to start looking for match */
6733{
6734 reg_match = rmp;
6735 reg_mmatch = NULL;
6736 reg_maxline = 0;
6737 reg_line_lbr = FALSE;
6738 reg_buf = curbuf;
6739 reg_win = NULL;
6740 ireg_ic = rmp->rm_ic;
6741#ifdef FEAT_MBYTE
6742 ireg_icombine = FALSE;
6743#endif
6744 ireg_maxcol = 0;
6745 return (nfa_regexec_both(line, col) != 0);
6746}
6747
6748#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
6749 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
6750
6751static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col));
6752
6753/*
6754 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break.
6755 */
6756 static int
6757nfa_regexec_nl(rmp, line, col)
6758 regmatch_T *rmp;
6759 char_u *line; /* string to match against */
6760 colnr_T col; /* column to start looking for match */
6761{
6762 reg_match = rmp;
6763 reg_mmatch = NULL;
6764 reg_maxline = 0;
6765 reg_line_lbr = TRUE;
6766 reg_buf = curbuf;
6767 reg_win = NULL;
6768 ireg_ic = rmp->rm_ic;
6769#ifdef FEAT_MBYTE
6770 ireg_icombine = FALSE;
6771#endif
6772 ireg_maxcol = 0;
6773 return (nfa_regexec_both(line, col) != 0);
6774}
6775#endif
6776
6777
6778/*
6779 * Match a regexp against multiple lines.
6780 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
6781 * Uses curbuf for line count and 'iskeyword'.
6782 *
6783 * Return zero if there is no match. Return number of lines contained in the
6784 * match otherwise.
6785 *
6786 * Note: the body is the same as bt_regexec() except for nfa_regexec_both()
6787 *
6788 * ! Also NOTE : match may actually be in another line. e.g.:
6789 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like
6790 *
6791 * +-------------------------+
6792 * |a |
6793 * |b |
6794 * |c |
6795 * | |
6796 * +-------------------------+
6797 *
6798 * then nfa_regexec_multi() returns 3. while the original
6799 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2.
6800 *
6801 * FIXME if this behavior is not compatible.
6802 */
6803 static long
6804nfa_regexec_multi(rmp, win, buf, lnum, col, tm)
6805 regmmatch_T *rmp;
6806 win_T *win; /* window in which to search or NULL */
6807 buf_T *buf; /* buffer in which to search */
6808 linenr_T lnum; /* nr of line to start looking for match */
6809 colnr_T col; /* column to start looking for match */
6810 proftime_T *tm UNUSED; /* timeout limit or NULL */
6811{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006812 reg_match = NULL;
6813 reg_mmatch = rmp;
6814 reg_buf = buf;
6815 reg_win = win;
6816 reg_firstlnum = lnum;
6817 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
6818 reg_line_lbr = FALSE;
6819 ireg_ic = rmp->rmm_ic;
6820#ifdef FEAT_MBYTE
6821 ireg_icombine = FALSE;
6822#endif
6823 ireg_maxcol = rmp->rmm_maxcol;
6824
Bram Moolenaarf878bf02013-05-21 21:20:20 +02006825 return nfa_regexec_both(NULL, col);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006826}
6827
6828#ifdef DEBUG
6829# undef ENABLE_LOG
6830#endif