Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1 | /* 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 Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 8 | /* |
| 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 Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 24 | #ifdef DEBUG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 25 | # define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log" |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 26 | # define ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 27 | # 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 Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 32 | enum |
| 33 | { |
| 34 | NFA_SPLIT = -1024, |
| 35 | NFA_MATCH, |
| 36 | NFA_SKIP_CHAR, /* matches a 0-length char */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 37 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 38 | NFA_START_COLL, /* [abc] start */ |
| 39 | NFA_END_COLL, /* [abc] end */ |
| 40 | NFA_START_NEG_COLL, /* [^abc] start */ |
| 41 | NFA_END_NEG_COLL, /* [^abc] end (only used in postfix) */ |
| 42 | NFA_RANGE, /* range of the two previous items (only |
| 43 | * used in postfix) */ |
| 44 | NFA_RANGE_MIN, /* low end of a range */ |
| 45 | NFA_RANGE_MAX, /* high end of a range */ |
| 46 | |
| 47 | NFA_CONCAT, /* concatenate two previous items (only |
| 48 | * used in postfix) */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 49 | NFA_OR, |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 50 | NFA_STAR, /* greedy * */ |
| 51 | NFA_STAR_NONGREEDY, /* non-greedy * */ |
| 52 | NFA_QUEST, /* greedy \? */ |
| 53 | NFA_QUEST_NONGREEDY, /* non-greedy \? */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 54 | |
| 55 | NFA_BOL, /* ^ Begin line */ |
| 56 | NFA_EOL, /* $ End line */ |
| 57 | NFA_BOW, /* \< Begin word */ |
| 58 | NFA_EOW, /* \> End word */ |
| 59 | NFA_BOF, /* \%^ Begin file */ |
| 60 | NFA_EOF, /* \%$ End file */ |
| 61 | NFA_NEWL, |
| 62 | NFA_ZSTART, /* Used for \zs */ |
| 63 | NFA_ZEND, /* Used for \ze */ |
| 64 | NFA_NOPEN, /* Start of subexpression marked with \%( */ |
| 65 | NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ |
| 66 | NFA_START_INVISIBLE, |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 67 | NFA_START_INVISIBLE_BEFORE, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 68 | NFA_START_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 69 | NFA_END_INVISIBLE, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 70 | NFA_END_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 71 | NFA_COMPOSING, /* Next nodes in NFA are part of the |
| 72 | composing multibyte char */ |
| 73 | NFA_END_COMPOSING, /* End of a composing char in the NFA */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 74 | NFA_OPT_CHARS, /* \%[abc] */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 75 | |
| 76 | /* The following are used only in the postfix form, not in the NFA */ |
| 77 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 78 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 79 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 80 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 81 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 82 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 83 | NFA_BACKREF1, /* \1 */ |
| 84 | NFA_BACKREF2, /* \2 */ |
| 85 | NFA_BACKREF3, /* \3 */ |
| 86 | NFA_BACKREF4, /* \4 */ |
| 87 | NFA_BACKREF5, /* \5 */ |
| 88 | NFA_BACKREF6, /* \6 */ |
| 89 | NFA_BACKREF7, /* \7 */ |
| 90 | NFA_BACKREF8, /* \8 */ |
| 91 | NFA_BACKREF9, /* \9 */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 92 | #ifdef FEAT_SYN_HL |
| 93 | NFA_ZREF1, /* \z1 */ |
| 94 | NFA_ZREF2, /* \z2 */ |
| 95 | NFA_ZREF3, /* \z3 */ |
| 96 | NFA_ZREF4, /* \z4 */ |
| 97 | NFA_ZREF5, /* \z5 */ |
| 98 | NFA_ZREF6, /* \z6 */ |
| 99 | NFA_ZREF7, /* \z7 */ |
| 100 | NFA_ZREF8, /* \z8 */ |
| 101 | NFA_ZREF9, /* \z9 */ |
| 102 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 103 | NFA_SKIP, /* Skip characters */ |
| 104 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 105 | NFA_MOPEN, |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 106 | NFA_MOPEN1, |
| 107 | NFA_MOPEN2, |
| 108 | NFA_MOPEN3, |
| 109 | NFA_MOPEN4, |
| 110 | NFA_MOPEN5, |
| 111 | NFA_MOPEN6, |
| 112 | NFA_MOPEN7, |
| 113 | NFA_MOPEN8, |
| 114 | NFA_MOPEN9, |
| 115 | |
| 116 | NFA_MCLOSE, |
| 117 | NFA_MCLOSE1, |
| 118 | NFA_MCLOSE2, |
| 119 | NFA_MCLOSE3, |
| 120 | NFA_MCLOSE4, |
| 121 | NFA_MCLOSE5, |
| 122 | NFA_MCLOSE6, |
| 123 | NFA_MCLOSE7, |
| 124 | NFA_MCLOSE8, |
| 125 | NFA_MCLOSE9, |
| 126 | |
| 127 | #ifdef FEAT_SYN_HL |
| 128 | NFA_ZOPEN, |
| 129 | NFA_ZOPEN1, |
| 130 | NFA_ZOPEN2, |
| 131 | NFA_ZOPEN3, |
| 132 | NFA_ZOPEN4, |
| 133 | NFA_ZOPEN5, |
| 134 | NFA_ZOPEN6, |
| 135 | NFA_ZOPEN7, |
| 136 | NFA_ZOPEN8, |
| 137 | NFA_ZOPEN9, |
| 138 | |
| 139 | NFA_ZCLOSE, |
| 140 | NFA_ZCLOSE1, |
| 141 | NFA_ZCLOSE2, |
| 142 | NFA_ZCLOSE3, |
| 143 | NFA_ZCLOSE4, |
| 144 | NFA_ZCLOSE5, |
| 145 | NFA_ZCLOSE6, |
| 146 | NFA_ZCLOSE7, |
| 147 | NFA_ZCLOSE8, |
| 148 | NFA_ZCLOSE9, |
| 149 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 150 | |
| 151 | /* NFA_FIRST_NL */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 152 | NFA_ANY, /* Match any one character. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 153 | NFA_ANYOF, /* Match any character in this string. */ |
| 154 | NFA_ANYBUT, /* Match any character not in this string. */ |
| 155 | NFA_IDENT, /* Match identifier char */ |
| 156 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 157 | NFA_KWORD, /* Match keyword char */ |
| 158 | NFA_SKWORD, /* Match word char but no digit */ |
| 159 | NFA_FNAME, /* Match file name char */ |
| 160 | NFA_SFNAME, /* Match file name char but no digit */ |
| 161 | NFA_PRINT, /* Match printable char */ |
| 162 | NFA_SPRINT, /* Match printable char but no digit */ |
| 163 | NFA_WHITE, /* Match whitespace char */ |
| 164 | NFA_NWHITE, /* Match non-whitespace char */ |
| 165 | NFA_DIGIT, /* Match digit char */ |
| 166 | NFA_NDIGIT, /* Match non-digit char */ |
| 167 | NFA_HEX, /* Match hex char */ |
| 168 | NFA_NHEX, /* Match non-hex char */ |
| 169 | NFA_OCTAL, /* Match octal char */ |
| 170 | NFA_NOCTAL, /* Match non-octal char */ |
| 171 | NFA_WORD, /* Match word char */ |
| 172 | NFA_NWORD, /* Match non-word char */ |
| 173 | NFA_HEAD, /* Match head char */ |
| 174 | NFA_NHEAD, /* Match non-head char */ |
| 175 | NFA_ALPHA, /* Match alpha char */ |
| 176 | NFA_NALPHA, /* Match non-alpha char */ |
| 177 | NFA_LOWER, /* Match lowercase char */ |
| 178 | NFA_NLOWER, /* Match non-lowercase char */ |
| 179 | NFA_UPPER, /* Match uppercase char */ |
| 180 | NFA_NUPPER, /* Match non-uppercase char */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 181 | |
| 182 | NFA_CURSOR, /* Match cursor pos */ |
| 183 | NFA_LNUM, /* Match line number */ |
| 184 | NFA_LNUM_GT, /* Match > line number */ |
| 185 | NFA_LNUM_LT, /* Match < line number */ |
| 186 | NFA_COL, /* Match cursor column */ |
| 187 | NFA_COL_GT, /* Match > cursor column */ |
| 188 | NFA_COL_LT, /* Match < cursor column */ |
| 189 | NFA_VCOL, /* Match cursor virtual column */ |
| 190 | NFA_VCOL_GT, /* Match > cursor virtual column */ |
| 191 | NFA_VCOL_LT, /* Match < cursor virtual column */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 192 | NFA_MARK, /* Match mark */ |
| 193 | NFA_MARK_GT, /* Match > mark */ |
| 194 | NFA_MARK_LT, /* Match < mark */ |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 195 | NFA_VISUAL, /* Match Visual area */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 196 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 197 | NFA_FIRST_NL = NFA_ANY + ADD_NL, |
| 198 | NFA_LAST_NL = NFA_NUPPER + ADD_NL, |
| 199 | |
| 200 | /* Character classes [:alnum:] etc */ |
| 201 | NFA_CLASS_ALNUM, |
| 202 | NFA_CLASS_ALPHA, |
| 203 | NFA_CLASS_BLANK, |
| 204 | NFA_CLASS_CNTRL, |
| 205 | NFA_CLASS_DIGIT, |
| 206 | NFA_CLASS_GRAPH, |
| 207 | NFA_CLASS_LOWER, |
| 208 | NFA_CLASS_PRINT, |
| 209 | NFA_CLASS_PUNCT, |
| 210 | NFA_CLASS_SPACE, |
| 211 | NFA_CLASS_UPPER, |
| 212 | NFA_CLASS_XDIGIT, |
| 213 | NFA_CLASS_TAB, |
| 214 | NFA_CLASS_RETURN, |
| 215 | NFA_CLASS_BACKSPACE, |
| 216 | NFA_CLASS_ESCAPE |
| 217 | }; |
| 218 | |
| 219 | /* Keep in sync with classchars. */ |
| 220 | static int nfa_classcodes[] = { |
| 221 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 222 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 223 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 224 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 225 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 226 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 227 | NFA_UPPER, NFA_NUPPER |
| 228 | }; |
| 229 | |
| 230 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
| 231 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 232 | /* NFA regexp \ze operator encountered. */ |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 233 | static int nfa_has_zend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 234 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 235 | /* NFA regexp \1 .. \9 encountered. */ |
| 236 | static int nfa_has_backref; |
| 237 | |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 238 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 239 | /* NFA regexp has \z( ), set zsubexpr. */ |
| 240 | static int nfa_has_zsubexpr; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 241 | #endif |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 242 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 243 | /* Number of sub expressions actually being used during execution. 1 if only |
| 244 | * the whole match (subexpr 0) is used. */ |
| 245 | static int nfa_nsubexpr; |
| 246 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 247 | static int *post_start; /* holds the postfix form of r.e. */ |
| 248 | static int *post_end; |
| 249 | static int *post_ptr; |
| 250 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 251 | static int nstate; /* Number of states in the NFA. Also used when |
| 252 | * executing. */ |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 253 | static int istate; /* Index in the state vector, used in alloc_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 254 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 255 | /* If not NULL match must end at this position */ |
| 256 | static save_se_T *nfa_endp = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 257 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 258 | /* listid is global, so that it increases on recursive calls to |
| 259 | * nfa_regmatch(), which means we don't have to clear the lastlist field of |
| 260 | * all the states. */ |
| 261 | static int nfa_listid; |
| 262 | static int nfa_alt_listid; |
| 263 | |
| 264 | /* 0 for first call to nfa_regmatch(), 1 for recursive call. */ |
| 265 | static int nfa_ll_index = 0; |
| 266 | |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 267 | static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 268 | static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth)); |
| 269 | static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 270 | static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 271 | static int nfa_emit_equi_class __ARGS((int c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 272 | static int nfa_regatom __ARGS((void)); |
| 273 | static int nfa_regpiece __ARGS((void)); |
| 274 | static int nfa_regconcat __ARGS((void)); |
| 275 | static int nfa_regbranch __ARGS((void)); |
| 276 | static int nfa_reg __ARGS((int paren)); |
| 277 | #ifdef DEBUG |
| 278 | static void nfa_set_code __ARGS((int c)); |
| 279 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 280 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 281 | static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 282 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 283 | #endif |
| 284 | static int *re2post __ARGS((void)); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 285 | static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 286 | static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size)); |
| 287 | static int check_char_class __ARGS((int class, int c)); |
| 288 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 289 | static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list)); |
| 290 | static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 291 | static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 292 | static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 293 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 294 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
| 295 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 296 | static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
| 297 | |
| 298 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 299 | #define EMIT(c) do { \ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 300 | if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 301 | return FAIL; \ |
| 302 | *post_ptr++ = c; \ |
| 303 | } while (0) |
| 304 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 305 | /* |
| 306 | * Initialize internal variables before NFA compilation. |
| 307 | * Return OK on success, FAIL otherwise. |
| 308 | */ |
| 309 | static int |
| 310 | nfa_regcomp_start(expr, re_flags) |
| 311 | char_u *expr; |
| 312 | int re_flags; /* see vim_regcomp() */ |
| 313 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 314 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 315 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 316 | |
| 317 | nstate = 0; |
| 318 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 319 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 320 | nstate_max = (int)(STRLEN(expr) + 1) * 25; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 321 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 322 | /* Some items blow up in size, such as [A-z]. Add more space for that. |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 323 | * When it is still not enough realloc_post_list() will be used. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 324 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 325 | |
| 326 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 327 | postfix_size = sizeof(int) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 328 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 329 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 330 | if (post_start == NULL) |
| 331 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 332 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 333 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 334 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 335 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 336 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 337 | /* shared with BT engine */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 338 | regcomp_start(expr, re_flags); |
| 339 | |
| 340 | return OK; |
| 341 | } |
| 342 | |
| 343 | /* |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 344 | * Figure out if the NFA state list starts with an anchor, must match at start |
| 345 | * of the line. |
| 346 | */ |
| 347 | static int |
| 348 | nfa_get_reganch(start, depth) |
| 349 | nfa_state_T *start; |
| 350 | int depth; |
| 351 | { |
| 352 | nfa_state_T *p = start; |
| 353 | |
| 354 | if (depth > 4) |
| 355 | return 0; |
| 356 | |
| 357 | while (p != NULL) |
| 358 | { |
| 359 | switch (p->c) |
| 360 | { |
| 361 | case NFA_BOL: |
| 362 | case NFA_BOF: |
| 363 | return 1; /* yes! */ |
| 364 | |
| 365 | case NFA_ZSTART: |
| 366 | case NFA_ZEND: |
| 367 | case NFA_CURSOR: |
| 368 | case NFA_VISUAL: |
| 369 | |
| 370 | case NFA_MOPEN: |
| 371 | case NFA_MOPEN1: |
| 372 | case NFA_MOPEN2: |
| 373 | case NFA_MOPEN3: |
| 374 | case NFA_MOPEN4: |
| 375 | case NFA_MOPEN5: |
| 376 | case NFA_MOPEN6: |
| 377 | case NFA_MOPEN7: |
| 378 | case NFA_MOPEN8: |
| 379 | case NFA_MOPEN9: |
| 380 | case NFA_NOPEN: |
| 381 | #ifdef FEAT_SYN_HL |
| 382 | case NFA_ZOPEN: |
| 383 | case NFA_ZOPEN1: |
| 384 | case NFA_ZOPEN2: |
| 385 | case NFA_ZOPEN3: |
| 386 | case NFA_ZOPEN4: |
| 387 | case NFA_ZOPEN5: |
| 388 | case NFA_ZOPEN6: |
| 389 | case NFA_ZOPEN7: |
| 390 | case NFA_ZOPEN8: |
| 391 | case NFA_ZOPEN9: |
| 392 | #endif |
| 393 | p = p->out; |
| 394 | break; |
| 395 | |
| 396 | case NFA_SPLIT: |
| 397 | return nfa_get_reganch(p->out, depth + 1) |
| 398 | && nfa_get_reganch(p->out1, depth + 1); |
| 399 | |
| 400 | default: |
| 401 | return 0; /* noooo */ |
| 402 | } |
| 403 | } |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Figure out if the NFA state list starts with a character which must match |
| 409 | * at start of the match. |
| 410 | */ |
| 411 | static int |
| 412 | nfa_get_regstart(start, depth) |
| 413 | nfa_state_T *start; |
| 414 | int depth; |
| 415 | { |
| 416 | nfa_state_T *p = start; |
| 417 | |
| 418 | if (depth > 4) |
| 419 | return 0; |
| 420 | |
| 421 | while (p != NULL) |
| 422 | { |
| 423 | switch (p->c) |
| 424 | { |
| 425 | /* all kinds of zero-width matches */ |
| 426 | case NFA_BOL: |
| 427 | case NFA_BOF: |
| 428 | case NFA_BOW: |
| 429 | case NFA_EOW: |
| 430 | case NFA_ZSTART: |
| 431 | case NFA_ZEND: |
| 432 | case NFA_CURSOR: |
| 433 | case NFA_VISUAL: |
| 434 | case NFA_LNUM: |
| 435 | case NFA_LNUM_GT: |
| 436 | case NFA_LNUM_LT: |
| 437 | case NFA_COL: |
| 438 | case NFA_COL_GT: |
| 439 | case NFA_COL_LT: |
| 440 | case NFA_VCOL: |
| 441 | case NFA_VCOL_GT: |
| 442 | case NFA_VCOL_LT: |
| 443 | case NFA_MARK: |
| 444 | case NFA_MARK_GT: |
| 445 | case NFA_MARK_LT: |
| 446 | |
| 447 | case NFA_MOPEN: |
| 448 | case NFA_MOPEN1: |
| 449 | case NFA_MOPEN2: |
| 450 | case NFA_MOPEN3: |
| 451 | case NFA_MOPEN4: |
| 452 | case NFA_MOPEN5: |
| 453 | case NFA_MOPEN6: |
| 454 | case NFA_MOPEN7: |
| 455 | case NFA_MOPEN8: |
| 456 | case NFA_MOPEN9: |
| 457 | case NFA_NOPEN: |
| 458 | #ifdef FEAT_SYN_HL |
| 459 | case NFA_ZOPEN: |
| 460 | case NFA_ZOPEN1: |
| 461 | case NFA_ZOPEN2: |
| 462 | case NFA_ZOPEN3: |
| 463 | case NFA_ZOPEN4: |
| 464 | case NFA_ZOPEN5: |
| 465 | case NFA_ZOPEN6: |
| 466 | case NFA_ZOPEN7: |
| 467 | case NFA_ZOPEN8: |
| 468 | case NFA_ZOPEN9: |
| 469 | #endif |
| 470 | p = p->out; |
| 471 | break; |
| 472 | |
| 473 | case NFA_SPLIT: |
| 474 | { |
| 475 | int c1 = nfa_get_regstart(p->out, depth + 1); |
| 476 | int c2 = nfa_get_regstart(p->out1, depth + 1); |
| 477 | |
| 478 | if (c1 == c2) |
| 479 | return c1; /* yes! */ |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | default: |
| 484 | if (p->c > 0 && !p->negated) |
| 485 | return p->c; /* yes! */ |
| 486 | return 0; |
| 487 | } |
| 488 | } |
| 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 493 | * Allocate more space for post_start. Called when |
| 494 | * running above the estimated number of states. |
| 495 | */ |
| 496 | static int |
| 497 | realloc_post_list() |
| 498 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 499 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 500 | int new_max = nstate_max + 1000; |
| 501 | int *new_start; |
| 502 | int *old_start; |
| 503 | |
| 504 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 505 | if (new_start == NULL) |
| 506 | return FAIL; |
| 507 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 508 | old_start = post_start; |
| 509 | post_start = new_start; |
| 510 | post_ptr = new_start + (post_ptr - old_start); |
| 511 | post_end = post_start + new_max; |
| 512 | vim_free(old_start); |
| 513 | return OK; |
| 514 | } |
| 515 | |
| 516 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 517 | * Search between "start" and "end" and try to recognize a |
| 518 | * character class in expanded form. For example [0-9]. |
| 519 | * On success, return the id the character class to be emitted. |
| 520 | * On failure, return 0 (=FAIL) |
| 521 | * Start points to the first char of the range, while end should point |
| 522 | * to the closing brace. |
| 523 | */ |
| 524 | static int |
| 525 | nfa_recognize_char_class(start, end, extra_newl) |
| 526 | char_u *start; |
| 527 | char_u *end; |
| 528 | int extra_newl; |
| 529 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 530 | # define CLASS_not 0x80 |
| 531 | # define CLASS_af 0x40 |
| 532 | # define CLASS_AF 0x20 |
| 533 | # define CLASS_az 0x10 |
| 534 | # define CLASS_AZ 0x08 |
| 535 | # define CLASS_o7 0x04 |
| 536 | # define CLASS_o9 0x02 |
| 537 | # define CLASS_underscore 0x01 |
| 538 | |
| 539 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 540 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 541 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 542 | |
| 543 | if (extra_newl == TRUE) |
| 544 | newl = TRUE; |
| 545 | |
| 546 | if (*end != ']') |
| 547 | return FAIL; |
| 548 | p = start; |
| 549 | if (*p == '^') |
| 550 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 551 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 552 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | while (p < end) |
| 556 | { |
| 557 | if (p + 2 < end && *(p + 1) == '-') |
| 558 | { |
| 559 | switch (*p) |
| 560 | { |
| 561 | case '0': |
| 562 | if (*(p + 2) == '9') |
| 563 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 564 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 565 | break; |
| 566 | } |
| 567 | else |
| 568 | if (*(p + 2) == '7') |
| 569 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 570 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 571 | break; |
| 572 | } |
| 573 | case 'a': |
| 574 | if (*(p + 2) == 'z') |
| 575 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 576 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 577 | break; |
| 578 | } |
| 579 | else |
| 580 | if (*(p + 2) == 'f') |
| 581 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 582 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 583 | break; |
| 584 | } |
| 585 | case 'A': |
| 586 | if (*(p + 2) == 'Z') |
| 587 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 588 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 589 | break; |
| 590 | } |
| 591 | else |
| 592 | if (*(p + 2) == 'F') |
| 593 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 594 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 595 | break; |
| 596 | } |
| 597 | /* FALLTHROUGH */ |
| 598 | default: |
| 599 | return FAIL; |
| 600 | } |
| 601 | p += 3; |
| 602 | } |
| 603 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 604 | { |
| 605 | newl = TRUE; |
| 606 | p += 2; |
| 607 | } |
| 608 | else if (*p == '_') |
| 609 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 610 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 611 | p ++; |
| 612 | } |
| 613 | else if (*p == '\n') |
| 614 | { |
| 615 | newl = TRUE; |
| 616 | p ++; |
| 617 | } |
| 618 | else |
| 619 | return FAIL; |
| 620 | } /* while (p < end) */ |
| 621 | |
| 622 | if (p != end) |
| 623 | return FAIL; |
| 624 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 625 | if (newl == TRUE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 626 | extra_newl = ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 627 | |
| 628 | switch (config) |
| 629 | { |
| 630 | case CLASS_o9: |
| 631 | return extra_newl + NFA_DIGIT; |
| 632 | case CLASS_not | CLASS_o9: |
| 633 | return extra_newl + NFA_NDIGIT; |
| 634 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 635 | return extra_newl + NFA_HEX; |
| 636 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 637 | return extra_newl + NFA_NHEX; |
| 638 | case CLASS_o7: |
| 639 | return extra_newl + NFA_OCTAL; |
| 640 | case CLASS_not | CLASS_o7: |
| 641 | return extra_newl + NFA_NOCTAL; |
| 642 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 643 | return extra_newl + NFA_WORD; |
| 644 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 645 | return extra_newl + NFA_NWORD; |
| 646 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 647 | return extra_newl + NFA_HEAD; |
| 648 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 649 | return extra_newl + NFA_NHEAD; |
| 650 | case CLASS_az | CLASS_AZ: |
| 651 | return extra_newl + NFA_ALPHA; |
| 652 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 653 | return extra_newl + NFA_NALPHA; |
| 654 | case CLASS_az: |
| 655 | return extra_newl + NFA_LOWER; |
| 656 | case CLASS_not | CLASS_az: |
| 657 | return extra_newl + NFA_NLOWER; |
| 658 | case CLASS_AZ: |
| 659 | return extra_newl + NFA_UPPER; |
| 660 | case CLASS_not | CLASS_AZ: |
| 661 | return extra_newl + NFA_NUPPER; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 662 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 663 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | /* |
| 667 | * Produce the bytes for equivalence class "c". |
| 668 | * Currently only handles latin1, latin9 and utf-8. |
| 669 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 670 | * equivalent to 'a OR b OR c' |
| 671 | * |
| 672 | * NOTE! When changing this function, also update reg_equi_class() |
| 673 | */ |
| 674 | static int |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 675 | nfa_emit_equi_class(c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 676 | int c; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 677 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 678 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 679 | |
| 680 | #ifdef FEAT_MBYTE |
| 681 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 682 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 683 | #endif |
| 684 | { |
| 685 | switch (c) |
| 686 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 687 | case 'A': case 0300: case 0301: case 0302: |
| 688 | case 0303: case 0304: case 0305: |
| 689 | EMIT2('A'); EMIT2(0300); EMIT2(0301); |
| 690 | EMIT2(0302); EMIT2(0303); EMIT2(0304); |
| 691 | EMIT2(0305); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 692 | return OK; |
| 693 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 694 | case 'C': case 0307: |
| 695 | EMIT2('C'); EMIT2(0307); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 696 | return OK; |
| 697 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 698 | case 'E': case 0310: case 0311: case 0312: case 0313: |
| 699 | EMIT2('E'); EMIT2(0310); EMIT2(0311); |
| 700 | EMIT2(0312); EMIT2(0313); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 701 | return OK; |
| 702 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 703 | case 'I': case 0314: case 0315: case 0316: case 0317: |
| 704 | EMIT2('I'); EMIT2(0314); EMIT2(0315); |
| 705 | EMIT2(0316); EMIT2(0317); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 706 | return OK; |
| 707 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 708 | case 'N': case 0321: |
| 709 | EMIT2('N'); EMIT2(0321); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 710 | return OK; |
| 711 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 712 | case 'O': case 0322: case 0323: case 0324: case 0325: |
| 713 | case 0326: |
| 714 | EMIT2('O'); EMIT2(0322); EMIT2(0323); |
| 715 | EMIT2(0324); EMIT2(0325); EMIT2(0326); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 716 | return OK; |
| 717 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 718 | case 'U': case 0331: case 0332: case 0333: case 0334: |
| 719 | EMIT2('U'); EMIT2(0331); EMIT2(0332); |
| 720 | EMIT2(0333); EMIT2(0334); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 721 | return OK; |
| 722 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 723 | case 'Y': case 0335: |
| 724 | EMIT2('Y'); EMIT2(0335); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 725 | return OK; |
| 726 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 727 | case 'a': case 0340: case 0341: case 0342: |
| 728 | case 0343: case 0344: case 0345: |
| 729 | EMIT2('a'); EMIT2(0340); EMIT2(0341); |
| 730 | EMIT2(0342); EMIT2(0343); EMIT2(0344); |
| 731 | EMIT2(0345); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 732 | return OK; |
| 733 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 734 | case 'c': case 0347: |
| 735 | EMIT2('c'); EMIT2(0347); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 736 | return OK; |
| 737 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 738 | case 'e': case 0350: case 0351: case 0352: case 0353: |
| 739 | EMIT2('e'); EMIT2(0350); EMIT2(0351); |
| 740 | EMIT2(0352); EMIT2(0353); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 741 | return OK; |
| 742 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 743 | case 'i': case 0354: case 0355: case 0356: case 0357: |
| 744 | EMIT2('i'); EMIT2(0354); EMIT2(0355); |
| 745 | EMIT2(0356); EMIT2(0357); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 746 | return OK; |
| 747 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 748 | case 'n': case 0361: |
| 749 | EMIT2('n'); EMIT2(0361); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 750 | return OK; |
| 751 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 752 | case 'o': case 0362: case 0363: case 0364: case 0365: |
| 753 | case 0366: |
| 754 | EMIT2('o'); EMIT2(0362); EMIT2(0363); |
| 755 | EMIT2(0364); EMIT2(0365); EMIT2(0366); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 756 | return OK; |
| 757 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 758 | case 'u': case 0371: case 0372: case 0373: case 0374: |
| 759 | EMIT2('u'); EMIT2(0371); EMIT2(0372); |
| 760 | EMIT2(0373); EMIT2(0374); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 761 | return OK; |
| 762 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 763 | case 'y': case 0375: case 0377: |
| 764 | EMIT2('y'); EMIT2(0375); EMIT2(0377); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 765 | return OK; |
| 766 | |
| 767 | default: |
| 768 | return FAIL; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | EMIT(c); |
| 773 | return OK; |
| 774 | #undef EMIT2 |
| 775 | } |
| 776 | |
| 777 | /* |
| 778 | * Code to parse regular expression. |
| 779 | * |
| 780 | * We try to reuse parsing functions in regexp.c to |
| 781 | * minimize surprise and keep the syntax consistent. |
| 782 | */ |
| 783 | |
| 784 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 785 | * Parse the lowest level. |
| 786 | * |
| 787 | * An atom can be one of a long list of items. Many atoms match one character |
| 788 | * in the text. It is often an ordinary character or a character class. |
| 789 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 790 | * is only for syntax highlighting. |
| 791 | * |
| 792 | * atom ::= ordinary-atom |
| 793 | * or \( pattern \) |
| 794 | * or \%( pattern \) |
| 795 | * or \z( pattern \) |
| 796 | */ |
| 797 | static int |
| 798 | nfa_regatom() |
| 799 | { |
| 800 | int c; |
| 801 | int charclass; |
| 802 | int equiclass; |
| 803 | int collclass; |
| 804 | int got_coll_char; |
| 805 | char_u *p; |
| 806 | char_u *endp; |
| 807 | #ifdef FEAT_MBYTE |
| 808 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 809 | #endif |
| 810 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 811 | int emit_range; |
| 812 | int negated; |
| 813 | int result; |
| 814 | int startc = -1; |
| 815 | int endc = -1; |
| 816 | int oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 817 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 818 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 819 | switch (c) |
| 820 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 821 | case NUL: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 822 | EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); |
| 823 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 824 | case Magic('^'): |
| 825 | EMIT(NFA_BOL); |
| 826 | break; |
| 827 | |
| 828 | case Magic('$'): |
| 829 | EMIT(NFA_EOL); |
| 830 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 831 | had_eol = TRUE; |
| 832 | #endif |
| 833 | break; |
| 834 | |
| 835 | case Magic('<'): |
| 836 | EMIT(NFA_BOW); |
| 837 | break; |
| 838 | |
| 839 | case Magic('>'): |
| 840 | EMIT(NFA_EOW); |
| 841 | break; |
| 842 | |
| 843 | case Magic('_'): |
| 844 | c = no_Magic(getchr()); |
| 845 | if (c == '^') /* "\_^" is start-of-line */ |
| 846 | { |
| 847 | EMIT(NFA_BOL); |
| 848 | break; |
| 849 | } |
| 850 | if (c == '$') /* "\_$" is end-of-line */ |
| 851 | { |
| 852 | EMIT(NFA_EOL); |
| 853 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 854 | had_eol = TRUE; |
| 855 | #endif |
| 856 | break; |
| 857 | } |
| 858 | |
| 859 | extra = ADD_NL; |
| 860 | |
| 861 | /* "\_[" is collection plus newline */ |
| 862 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 863 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 864 | |
| 865 | /* "\_x" is character class plus newline */ |
| 866 | /*FALLTHROUGH*/ |
| 867 | |
| 868 | /* |
| 869 | * Character classes. |
| 870 | */ |
| 871 | case Magic('.'): |
| 872 | case Magic('i'): |
| 873 | case Magic('I'): |
| 874 | case Magic('k'): |
| 875 | case Magic('K'): |
| 876 | case Magic('f'): |
| 877 | case Magic('F'): |
| 878 | case Magic('p'): |
| 879 | case Magic('P'): |
| 880 | case Magic('s'): |
| 881 | case Magic('S'): |
| 882 | case Magic('d'): |
| 883 | case Magic('D'): |
| 884 | case Magic('x'): |
| 885 | case Magic('X'): |
| 886 | case Magic('o'): |
| 887 | case Magic('O'): |
| 888 | case Magic('w'): |
| 889 | case Magic('W'): |
| 890 | case Magic('h'): |
| 891 | case Magic('H'): |
| 892 | case Magic('a'): |
| 893 | case Magic('A'): |
| 894 | case Magic('l'): |
| 895 | case Magic('L'): |
| 896 | case Magic('u'): |
| 897 | case Magic('U'): |
| 898 | p = vim_strchr(classchars, no_Magic(c)); |
| 899 | if (p == NULL) |
| 900 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 901 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 902 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 903 | } |
| 904 | #ifdef FEAT_MBYTE |
| 905 | /* When '.' is followed by a composing char ignore the dot, so that |
| 906 | * the composing char is matched here. */ |
| 907 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 908 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 909 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 910 | c = getchr(); |
| 911 | goto nfa_do_multibyte; |
| 912 | } |
| 913 | #endif |
| 914 | EMIT(nfa_classcodes[p - classchars]); |
| 915 | if (extra == ADD_NL) |
| 916 | { |
| 917 | EMIT(NFA_NEWL); |
| 918 | EMIT(NFA_OR); |
| 919 | regflags |= RF_HASNL; |
| 920 | } |
| 921 | break; |
| 922 | |
| 923 | case Magic('n'): |
| 924 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 925 | /* In a string "\n" matches a newline character. */ |
| 926 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 927 | else |
| 928 | { |
| 929 | /* In buffer text "\n" matches the end of a line. */ |
| 930 | EMIT(NFA_NEWL); |
| 931 | regflags |= RF_HASNL; |
| 932 | } |
| 933 | break; |
| 934 | |
| 935 | case Magic('('): |
| 936 | if (nfa_reg(REG_PAREN) == FAIL) |
| 937 | return FAIL; /* cascaded error */ |
| 938 | break; |
| 939 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 940 | case Magic('|'): |
| 941 | case Magic('&'): |
| 942 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 943 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 944 | return FAIL; |
| 945 | |
| 946 | case Magic('='): |
| 947 | case Magic('?'): |
| 948 | case Magic('+'): |
| 949 | case Magic('@'): |
| 950 | case Magic('*'): |
| 951 | case Magic('{'): |
| 952 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 953 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 954 | return FAIL; |
| 955 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 956 | case Magic('~'): |
| 957 | { |
| 958 | char_u *lp; |
| 959 | |
| 960 | /* Previous substitute pattern. |
| 961 | * Generated as "\%(pattern\)". */ |
| 962 | if (reg_prev_sub == NULL) |
| 963 | { |
| 964 | EMSG(_(e_nopresub)); |
| 965 | return FAIL; |
| 966 | } |
| 967 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 968 | { |
| 969 | EMIT(PTR2CHAR(lp)); |
| 970 | if (lp != reg_prev_sub) |
| 971 | EMIT(NFA_CONCAT); |
| 972 | } |
| 973 | EMIT(NFA_NOPEN); |
| 974 | break; |
| 975 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 976 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 977 | case Magic('1'): |
| 978 | case Magic('2'): |
| 979 | case Magic('3'): |
| 980 | case Magic('4'): |
| 981 | case Magic('5'): |
| 982 | case Magic('6'): |
| 983 | case Magic('7'): |
| 984 | case Magic('8'): |
| 985 | case Magic('9'): |
| 986 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 987 | nfa_has_backref = TRUE; |
| 988 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 989 | |
| 990 | case Magic('z'): |
| 991 | c = no_Magic(getchr()); |
| 992 | switch (c) |
| 993 | { |
| 994 | case 's': |
| 995 | EMIT(NFA_ZSTART); |
| 996 | break; |
| 997 | case 'e': |
| 998 | EMIT(NFA_ZEND); |
| 999 | nfa_has_zend = TRUE; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1000 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1001 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1002 | case '1': |
| 1003 | case '2': |
| 1004 | case '3': |
| 1005 | case '4': |
| 1006 | case '5': |
| 1007 | case '6': |
| 1008 | case '7': |
| 1009 | case '8': |
| 1010 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1011 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1012 | if (reg_do_extmatch != REX_USE) |
| 1013 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1014 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1015 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1016 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1017 | re_has_z = REX_USE; |
| 1018 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1019 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1020 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1021 | if (reg_do_extmatch != REX_SET) |
| 1022 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1023 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1024 | return FAIL; /* cascaded error */ |
| 1025 | re_has_z = REX_SET; |
| 1026 | break; |
| 1027 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1028 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1029 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1030 | no_Magic(c)); |
| 1031 | return FAIL; |
| 1032 | } |
| 1033 | break; |
| 1034 | |
| 1035 | case Magic('%'): |
| 1036 | c = no_Magic(getchr()); |
| 1037 | switch (c) |
| 1038 | { |
| 1039 | /* () without a back reference */ |
| 1040 | case '(': |
| 1041 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1042 | return FAIL; |
| 1043 | EMIT(NFA_NOPEN); |
| 1044 | break; |
| 1045 | |
| 1046 | case 'd': /* %d123 decimal */ |
| 1047 | case 'o': /* %o123 octal */ |
| 1048 | case 'x': /* %xab hex 2 */ |
| 1049 | case 'u': /* %uabcd hex 4 */ |
| 1050 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1051 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1052 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1053 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1054 | switch (c) |
| 1055 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1056 | case 'd': nr = getdecchrs(); break; |
| 1057 | case 'o': nr = getoctchrs(); break; |
| 1058 | case 'x': nr = gethexchrs(2); break; |
| 1059 | case 'u': nr = gethexchrs(4); break; |
| 1060 | case 'U': nr = gethexchrs(8); break; |
| 1061 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1062 | } |
| 1063 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1064 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1065 | EMSG2_RET_FAIL( |
| 1066 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1067 | reg_magic == MAGIC_ALL); |
| 1068 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1069 | EMIT(nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1070 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1071 | break; |
| 1072 | |
| 1073 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1074 | * pattern -- regardless of whether or not it makes sense. */ |
| 1075 | case '^': |
| 1076 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1077 | break; |
| 1078 | |
| 1079 | case '$': |
| 1080 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1081 | break; |
| 1082 | |
| 1083 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1084 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1085 | break; |
| 1086 | |
| 1087 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1088 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1089 | break; |
| 1090 | |
| 1091 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1092 | { |
| 1093 | int n; |
| 1094 | |
| 1095 | /* \%[abc] */ |
| 1096 | for (n = 0; (c = getchr()) != ']'; ++n) |
| 1097 | { |
| 1098 | if (c == NUL) |
| 1099 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1100 | reg_magic == MAGIC_ALL); |
| 1101 | EMIT(c); |
| 1102 | } |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1103 | if (n == 0) |
| 1104 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1105 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1106 | EMIT(NFA_OPT_CHARS); |
| 1107 | EMIT(n); |
| 1108 | break; |
| 1109 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1110 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1111 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1112 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1113 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1114 | int cmp = c; |
| 1115 | |
| 1116 | if (c == '<' || c == '>') |
| 1117 | c = getchr(); |
| 1118 | while (VIM_ISDIGIT(c)) |
| 1119 | { |
| 1120 | n = n * 10 + (c - '0'); |
| 1121 | c = getchr(); |
| 1122 | } |
| 1123 | if (c == 'l' || c == 'c' || c == 'v') |
| 1124 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1125 | if (c == 'l') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1126 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1127 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1128 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1129 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1130 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1131 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1132 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1133 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1134 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1135 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1136 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1137 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1138 | break; |
| 1139 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1140 | else if (c == '\'' && n == 0) |
| 1141 | { |
| 1142 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1143 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1144 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1145 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1146 | break; |
| 1147 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1148 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1149 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1150 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1151 | return FAIL; |
| 1152 | } |
| 1153 | break; |
| 1154 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1155 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1156 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1157 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1158 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1159 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1160 | * Each character is produced as a regular state, using |
| 1161 | * NFA_CONCAT to bind them together. |
| 1162 | * Besides normal characters there can be: |
| 1163 | * - character classes NFA_CLASS_* |
| 1164 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1165 | */ |
| 1166 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1167 | p = regparse; |
| 1168 | endp = skip_anyof(p); |
| 1169 | if (*endp == ']') |
| 1170 | { |
| 1171 | /* |
| 1172 | * Try to reverse engineer character classes. For example, |
| 1173 | * recognize that [0-9] stands for \d and [A-Za-z_] with \h, |
| 1174 | * and perform the necessary substitutions in the NFA. |
| 1175 | */ |
| 1176 | result = nfa_recognize_char_class(regparse, endp, |
| 1177 | extra == ADD_NL); |
| 1178 | if (result != FAIL) |
| 1179 | { |
| 1180 | if (result >= NFA_DIGIT && result <= NFA_NUPPER) |
| 1181 | EMIT(result); |
| 1182 | else /* must be char class + newline */ |
| 1183 | { |
| 1184 | EMIT(result - ADD_NL); |
| 1185 | EMIT(NFA_NEWL); |
| 1186 | EMIT(NFA_OR); |
| 1187 | } |
| 1188 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1189 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1190 | return OK; |
| 1191 | } |
| 1192 | /* |
| 1193 | * Failed to recognize a character class. Use the simple |
| 1194 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1195 | */ |
| 1196 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1197 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1198 | if (*regparse == '^') /* negated range */ |
| 1199 | { |
| 1200 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1201 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1202 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1203 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1204 | else |
| 1205 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1206 | if (*regparse == '-') |
| 1207 | { |
| 1208 | startc = '-'; |
| 1209 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1210 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1211 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1212 | } |
| 1213 | /* Emit the OR branches for each character in the [] */ |
| 1214 | emit_range = FALSE; |
| 1215 | while (regparse < endp) |
| 1216 | { |
| 1217 | oldstartc = startc; |
| 1218 | startc = -1; |
| 1219 | got_coll_char = FALSE; |
| 1220 | if (*regparse == '[') |
| 1221 | { |
| 1222 | /* Check for [: :], [= =], [. .] */ |
| 1223 | equiclass = collclass = 0; |
| 1224 | charclass = get_char_class(®parse); |
| 1225 | if (charclass == CLASS_NONE) |
| 1226 | { |
| 1227 | equiclass = get_equi_class(®parse); |
| 1228 | if (equiclass == 0) |
| 1229 | collclass = get_coll_element(®parse); |
| 1230 | } |
| 1231 | |
| 1232 | /* Character class like [:alpha:] */ |
| 1233 | if (charclass != CLASS_NONE) |
| 1234 | { |
| 1235 | switch (charclass) |
| 1236 | { |
| 1237 | case CLASS_ALNUM: |
| 1238 | EMIT(NFA_CLASS_ALNUM); |
| 1239 | break; |
| 1240 | case CLASS_ALPHA: |
| 1241 | EMIT(NFA_CLASS_ALPHA); |
| 1242 | break; |
| 1243 | case CLASS_BLANK: |
| 1244 | EMIT(NFA_CLASS_BLANK); |
| 1245 | break; |
| 1246 | case CLASS_CNTRL: |
| 1247 | EMIT(NFA_CLASS_CNTRL); |
| 1248 | break; |
| 1249 | case CLASS_DIGIT: |
| 1250 | EMIT(NFA_CLASS_DIGIT); |
| 1251 | break; |
| 1252 | case CLASS_GRAPH: |
| 1253 | EMIT(NFA_CLASS_GRAPH); |
| 1254 | break; |
| 1255 | case CLASS_LOWER: |
| 1256 | EMIT(NFA_CLASS_LOWER); |
| 1257 | break; |
| 1258 | case CLASS_PRINT: |
| 1259 | EMIT(NFA_CLASS_PRINT); |
| 1260 | break; |
| 1261 | case CLASS_PUNCT: |
| 1262 | EMIT(NFA_CLASS_PUNCT); |
| 1263 | break; |
| 1264 | case CLASS_SPACE: |
| 1265 | EMIT(NFA_CLASS_SPACE); |
| 1266 | break; |
| 1267 | case CLASS_UPPER: |
| 1268 | EMIT(NFA_CLASS_UPPER); |
| 1269 | break; |
| 1270 | case CLASS_XDIGIT: |
| 1271 | EMIT(NFA_CLASS_XDIGIT); |
| 1272 | break; |
| 1273 | case CLASS_TAB: |
| 1274 | EMIT(NFA_CLASS_TAB); |
| 1275 | break; |
| 1276 | case CLASS_RETURN: |
| 1277 | EMIT(NFA_CLASS_RETURN); |
| 1278 | break; |
| 1279 | case CLASS_BACKSPACE: |
| 1280 | EMIT(NFA_CLASS_BACKSPACE); |
| 1281 | break; |
| 1282 | case CLASS_ESCAPE: |
| 1283 | EMIT(NFA_CLASS_ESCAPE); |
| 1284 | break; |
| 1285 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1286 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1287 | continue; |
| 1288 | } |
| 1289 | /* Try equivalence class [=a=] and the like */ |
| 1290 | if (equiclass != 0) |
| 1291 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1292 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1293 | if (result == FAIL) |
| 1294 | { |
| 1295 | /* should never happen */ |
| 1296 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1297 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1298 | continue; |
| 1299 | } |
| 1300 | /* Try collating class like [. .] */ |
| 1301 | if (collclass != 0) |
| 1302 | { |
| 1303 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1304 | /* Will emit the proper atom at the end of the |
| 1305 | * while loop. */ |
| 1306 | } |
| 1307 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1308 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1309 | * start character. */ |
| 1310 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1311 | { |
| 1312 | emit_range = TRUE; |
| 1313 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1314 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1315 | continue; /* reading the end of the range */ |
| 1316 | } |
| 1317 | |
| 1318 | /* Now handle simple and escaped characters. |
| 1319 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1320 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1321 | * 'cpoptions' is not included. |
| 1322 | * Posix doesn't recognize backslash at all. |
| 1323 | */ |
| 1324 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1325 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1326 | && regparse + 1 <= endp |
| 1327 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1328 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1329 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1330 | != NULL) |
| 1331 | ) |
| 1332 | ) |
| 1333 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1334 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1335 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1336 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1337 | startc = reg_string ? NL : NFA_NEWL; |
| 1338 | else |
| 1339 | if (*regparse == 'd' |
| 1340 | || *regparse == 'o' |
| 1341 | || *regparse == 'x' |
| 1342 | || *regparse == 'u' |
| 1343 | || *regparse == 'U' |
| 1344 | ) |
| 1345 | { |
| 1346 | /* TODO(RE) This needs more testing */ |
| 1347 | startc = coll_get_char(); |
| 1348 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1349 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1350 | } |
| 1351 | else |
| 1352 | { |
| 1353 | /* \r,\t,\e,\b */ |
| 1354 | startc = backslash_trans(*regparse); |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | /* Normal printable char */ |
| 1359 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1360 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1361 | |
| 1362 | /* Previous char was '-', so this char is end of range. */ |
| 1363 | if (emit_range) |
| 1364 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1365 | endc = startc; |
| 1366 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1367 | if (startc > endc) |
| 1368 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1369 | |
| 1370 | if (endc > startc + 2) |
| 1371 | { |
| 1372 | /* Emit a range instead of the sequence of |
| 1373 | * individual characters. */ |
| 1374 | if (startc == 0) |
| 1375 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1376 | EMIT(1); |
| 1377 | else |
| 1378 | --post_ptr; /* remove NFA_CONCAT */ |
| 1379 | EMIT(endc); |
| 1380 | EMIT(NFA_RANGE); |
| 1381 | EMIT(NFA_CONCAT); |
| 1382 | } |
| 1383 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1384 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1385 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1386 | || (*mb_char2len)(endc) > 1)) |
| 1387 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1388 | /* Emit the characters in the range. |
| 1389 | * "startc" was already emitted, so skip it. |
| 1390 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1391 | for (c = startc + 1; c <= endc; c++) |
| 1392 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1393 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1394 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1395 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1396 | } |
| 1397 | else |
| 1398 | #endif |
| 1399 | { |
| 1400 | #ifdef EBCDIC |
| 1401 | int alpha_only = FALSE; |
| 1402 | |
| 1403 | /* for alphabetical range skip the gaps |
| 1404 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1405 | if (isalpha(startc) && isalpha(endc)) |
| 1406 | alpha_only = TRUE; |
| 1407 | #endif |
| 1408 | /* Emit the range. "startc" was already emitted, so |
| 1409 | * skip it. */ |
| 1410 | for (c = startc + 1; c <= endc; c++) |
| 1411 | #ifdef EBCDIC |
| 1412 | if (!alpha_only || isalpha(startc)) |
| 1413 | #endif |
| 1414 | { |
| 1415 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1416 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1417 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1418 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1419 | emit_range = FALSE; |
| 1420 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1421 | } |
| 1422 | else |
| 1423 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1424 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1425 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1426 | * Normally, simply emit startc. But if we get char |
| 1427 | * code=0 from a collating char, then replace it with |
| 1428 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1429 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1430 | * the backtracking engine. */ |
| 1431 | if (startc == NFA_NEWL) |
| 1432 | { |
| 1433 | /* Line break can't be matched as part of the |
| 1434 | * collection, add an OR below. But not for negated |
| 1435 | * range. */ |
| 1436 | if (!negated) |
| 1437 | extra = ADD_NL; |
| 1438 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1439 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1440 | { |
| 1441 | if (got_coll_char == TRUE && startc == 0) |
| 1442 | EMIT(0x0a); |
| 1443 | else |
| 1444 | EMIT(startc); |
| 1445 | EMIT(NFA_CONCAT); |
| 1446 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1449 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1450 | } /* while (p < endp) */ |
| 1451 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1452 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1453 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1454 | { |
| 1455 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1456 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1457 | } |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1458 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1459 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1460 | /* skip the trailing ] */ |
| 1461 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1462 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1463 | |
| 1464 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1465 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 1466 | EMIT(NFA_END_NEG_COLL); |
| 1467 | else |
| 1468 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1469 | |
| 1470 | /* \_[] also matches \n but it's not negated */ |
| 1471 | if (extra == ADD_NL) |
| 1472 | { |
| 1473 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1474 | EMIT(NFA_OR); |
| 1475 | } |
| 1476 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1477 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1478 | } /* if exists closing ] */ |
| 1479 | |
| 1480 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1481 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1482 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1483 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1484 | default: |
| 1485 | { |
| 1486 | #ifdef FEAT_MBYTE |
| 1487 | int plen; |
| 1488 | |
| 1489 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1490 | /* plen is length of current char with composing chars */ |
| 1491 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1492 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1493 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1494 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1495 | int i = 0; |
| 1496 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1497 | /* A base character plus composing characters, or just one |
| 1498 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1499 | * This requires creating a separate atom as if enclosing |
| 1500 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1501 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1502 | * building the postfix form, not the NFA itself; |
| 1503 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1504 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1505 | for (;;) |
| 1506 | { |
| 1507 | EMIT(c); |
| 1508 | if (i > 0) |
| 1509 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1510 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1511 | break; |
| 1512 | c = utf_ptr2char(old_regparse + i); |
| 1513 | } |
| 1514 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1515 | regparse = old_regparse + plen; |
| 1516 | } |
| 1517 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1518 | #endif |
| 1519 | { |
| 1520 | c = no_Magic(c); |
| 1521 | EMIT(c); |
| 1522 | } |
| 1523 | return OK; |
| 1524 | } |
| 1525 | } |
| 1526 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1527 | return OK; |
| 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Parse something followed by possible [*+=]. |
| 1532 | * |
| 1533 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1534 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1535 | * characters: "", "a", "aa", etc. |
| 1536 | * |
| 1537 | * piece ::= atom |
| 1538 | * or atom multi |
| 1539 | */ |
| 1540 | static int |
| 1541 | nfa_regpiece() |
| 1542 | { |
| 1543 | int i; |
| 1544 | int op; |
| 1545 | int ret; |
| 1546 | long minval, maxval; |
| 1547 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1548 | parse_state_T old_state; |
| 1549 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1550 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1551 | int old_post_pos; |
| 1552 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1553 | int quest; |
| 1554 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1555 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1556 | * next. */ |
| 1557 | save_parse_state(&old_state); |
| 1558 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1559 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1560 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1561 | |
| 1562 | ret = nfa_regatom(); |
| 1563 | if (ret == FAIL) |
| 1564 | return FAIL; /* cascaded error */ |
| 1565 | |
| 1566 | op = peekchr(); |
| 1567 | if (re_multi_type(op) == NOT_MULTI) |
| 1568 | return OK; |
| 1569 | |
| 1570 | skipchr(); |
| 1571 | switch (op) |
| 1572 | { |
| 1573 | case Magic('*'): |
| 1574 | EMIT(NFA_STAR); |
| 1575 | break; |
| 1576 | |
| 1577 | case Magic('+'): |
| 1578 | /* |
| 1579 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1580 | * first and only submatch would be "aaa". But the backtracking |
| 1581 | * engine interprets the plus as "try matching one more time", and |
| 1582 | * a* matches a second time at the end of the input, the empty |
| 1583 | * string. |
| 1584 | * The submatch will the empty string. |
| 1585 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1586 | * In order to be consistent with the old engine, we replace |
| 1587 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1588 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1589 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1590 | curchr = -1; |
| 1591 | if (nfa_regatom() == FAIL) |
| 1592 | return FAIL; |
| 1593 | EMIT(NFA_STAR); |
| 1594 | EMIT(NFA_CONCAT); |
| 1595 | skipchr(); /* skip the \+ */ |
| 1596 | break; |
| 1597 | |
| 1598 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1599 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1600 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1601 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1602 | switch(op) |
| 1603 | { |
| 1604 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1605 | /* \@= */ |
| 1606 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1607 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1608 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1609 | /* \@! */ |
| 1610 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1611 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1612 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1613 | op = no_Magic(getchr()); |
| 1614 | if (op == '=') |
| 1615 | /* \@<= */ |
| 1616 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1617 | else if (op == '!') |
| 1618 | /* \@<! */ |
| 1619 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1620 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1621 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1622 | /* \@> */ |
| 1623 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1624 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1625 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1626 | if (i == 0) |
| 1627 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1628 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1629 | return FAIL; |
| 1630 | } |
| 1631 | EMIT(i); |
| 1632 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1633 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1634 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1635 | break; |
| 1636 | |
| 1637 | case Magic('?'): |
| 1638 | case Magic('='): |
| 1639 | EMIT(NFA_QUEST); |
| 1640 | break; |
| 1641 | |
| 1642 | case Magic('{'): |
| 1643 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1644 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1645 | * version of '?' |
| 1646 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1647 | * parenthesis have the same id |
| 1648 | */ |
| 1649 | |
| 1650 | greedy = TRUE; |
| 1651 | c2 = peekchr(); |
| 1652 | if (c2 == '-' || c2 == Magic('-')) |
| 1653 | { |
| 1654 | skipchr(); |
| 1655 | greedy = FALSE; |
| 1656 | } |
| 1657 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1658 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 1659 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1660 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 1661 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1662 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1663 | { |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1664 | if (greedy) |
| 1665 | /* \{}, \{0,} */ |
| 1666 | EMIT(NFA_STAR); |
| 1667 | else |
| 1668 | /* \{-}, \{-0,} */ |
| 1669 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1670 | break; |
| 1671 | } |
| 1672 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1673 | /* Special case: x{0} or x{-0} */ |
| 1674 | if (maxval == 0) |
| 1675 | { |
| 1676 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1677 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1678 | /* NFA_SKIP_CHAR has 0-length and works everywhere */ |
| 1679 | EMIT(NFA_SKIP_CHAR); |
| 1680 | return OK; |
| 1681 | } |
| 1682 | |
| 1683 | /* Ignore previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1684 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1685 | /* Save parse state after the repeated atom and the \{} */ |
| 1686 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1687 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1688 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 1689 | for (i = 0; i < maxval; i++) |
| 1690 | { |
| 1691 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1692 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1693 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1694 | if (nfa_regatom() == FAIL) |
| 1695 | return FAIL; |
| 1696 | /* after "minval" times, atoms are optional */ |
| 1697 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1698 | { |
| 1699 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1700 | { |
| 1701 | if (greedy) |
| 1702 | EMIT(NFA_STAR); |
| 1703 | else |
| 1704 | EMIT(NFA_STAR_NONGREEDY); |
| 1705 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1706 | else |
| 1707 | EMIT(quest); |
| 1708 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1709 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1710 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1711 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 1712 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1716 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1717 | curchr = -1; |
| 1718 | |
| 1719 | break; |
| 1720 | |
| 1721 | |
| 1722 | default: |
| 1723 | break; |
| 1724 | } /* end switch */ |
| 1725 | |
| 1726 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1727 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1728 | EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1729 | |
| 1730 | return OK; |
| 1731 | } |
| 1732 | |
| 1733 | /* |
| 1734 | * Parse one or more pieces, concatenated. It matches a match for the |
| 1735 | * first piece, followed by a match for the second piece, etc. Example: |
| 1736 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 1737 | * |
| 1738 | * concat ::= piece |
| 1739 | * or piece piece |
| 1740 | * or piece piece piece |
| 1741 | * etc. |
| 1742 | */ |
| 1743 | static int |
| 1744 | nfa_regconcat() |
| 1745 | { |
| 1746 | int cont = TRUE; |
| 1747 | int first = TRUE; |
| 1748 | |
| 1749 | while (cont) |
| 1750 | { |
| 1751 | switch (peekchr()) |
| 1752 | { |
| 1753 | case NUL: |
| 1754 | case Magic('|'): |
| 1755 | case Magic('&'): |
| 1756 | case Magic(')'): |
| 1757 | cont = FALSE; |
| 1758 | break; |
| 1759 | |
| 1760 | case Magic('Z'): |
| 1761 | #ifdef FEAT_MBYTE |
| 1762 | regflags |= RF_ICOMBINE; |
| 1763 | #endif |
| 1764 | skipchr_keepstart(); |
| 1765 | break; |
| 1766 | case Magic('c'): |
| 1767 | regflags |= RF_ICASE; |
| 1768 | skipchr_keepstart(); |
| 1769 | break; |
| 1770 | case Magic('C'): |
| 1771 | regflags |= RF_NOICASE; |
| 1772 | skipchr_keepstart(); |
| 1773 | break; |
| 1774 | case Magic('v'): |
| 1775 | reg_magic = MAGIC_ALL; |
| 1776 | skipchr_keepstart(); |
| 1777 | curchr = -1; |
| 1778 | break; |
| 1779 | case Magic('m'): |
| 1780 | reg_magic = MAGIC_ON; |
| 1781 | skipchr_keepstart(); |
| 1782 | curchr = -1; |
| 1783 | break; |
| 1784 | case Magic('M'): |
| 1785 | reg_magic = MAGIC_OFF; |
| 1786 | skipchr_keepstart(); |
| 1787 | curchr = -1; |
| 1788 | break; |
| 1789 | case Magic('V'): |
| 1790 | reg_magic = MAGIC_NONE; |
| 1791 | skipchr_keepstart(); |
| 1792 | curchr = -1; |
| 1793 | break; |
| 1794 | |
| 1795 | default: |
| 1796 | if (nfa_regpiece() == FAIL) |
| 1797 | return FAIL; |
| 1798 | if (first == FALSE) |
| 1799 | EMIT(NFA_CONCAT); |
| 1800 | else |
| 1801 | first = FALSE; |
| 1802 | break; |
| 1803 | } |
| 1804 | } |
| 1805 | |
| 1806 | return OK; |
| 1807 | } |
| 1808 | |
| 1809 | /* |
| 1810 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 1811 | * last concat, but only if all the preceding concats also match at the same |
| 1812 | * position. Examples: |
| 1813 | * "foobeep\&..." matches "foo" in "foobeep". |
| 1814 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 1815 | * |
| 1816 | * branch ::= concat |
| 1817 | * or concat \& concat |
| 1818 | * or concat \& concat \& concat |
| 1819 | * etc. |
| 1820 | */ |
| 1821 | static int |
| 1822 | nfa_regbranch() |
| 1823 | { |
| 1824 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1825 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1826 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1827 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1828 | |
| 1829 | /* First branch, possibly the only one */ |
| 1830 | if (nfa_regconcat() == FAIL) |
| 1831 | return FAIL; |
| 1832 | |
| 1833 | ch = peekchr(); |
| 1834 | /* Try next concats */ |
| 1835 | while (ch == Magic('&')) |
| 1836 | { |
| 1837 | skipchr(); |
| 1838 | EMIT(NFA_NOPEN); |
| 1839 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1840 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1841 | if (nfa_regconcat() == FAIL) |
| 1842 | return FAIL; |
| 1843 | /* if concat is empty, skip a input char. But do emit a node */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1844 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1845 | EMIT(NFA_SKIP_CHAR); |
| 1846 | EMIT(NFA_CONCAT); |
| 1847 | ch = peekchr(); |
| 1848 | } |
| 1849 | |
| 1850 | /* Even if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1851 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1852 | EMIT(NFA_SKIP_CHAR); |
| 1853 | |
| 1854 | return OK; |
| 1855 | } |
| 1856 | |
| 1857 | /* |
| 1858 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 1859 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 1860 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 1861 | * is used. |
| 1862 | * |
| 1863 | * pattern ::= branch |
| 1864 | * or branch \| branch |
| 1865 | * or branch \| branch \| branch |
| 1866 | * etc. |
| 1867 | */ |
| 1868 | static int |
| 1869 | nfa_reg(paren) |
| 1870 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 1871 | { |
| 1872 | int parno = 0; |
| 1873 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1874 | if (paren == REG_PAREN) |
| 1875 | { |
| 1876 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1877 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1878 | parno = regnpar++; |
| 1879 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1880 | #ifdef FEAT_SYN_HL |
| 1881 | else if (paren == REG_ZPAREN) |
| 1882 | { |
| 1883 | /* Make a ZOPEN node. */ |
| 1884 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1885 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1886 | parno = regnzpar++; |
| 1887 | } |
| 1888 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1889 | |
| 1890 | if (nfa_regbranch() == FAIL) |
| 1891 | return FAIL; /* cascaded error */ |
| 1892 | |
| 1893 | while (peekchr() == Magic('|')) |
| 1894 | { |
| 1895 | skipchr(); |
| 1896 | if (nfa_regbranch() == FAIL) |
| 1897 | return FAIL; /* cascaded error */ |
| 1898 | EMIT(NFA_OR); |
| 1899 | } |
| 1900 | |
| 1901 | /* Check for proper termination. */ |
| 1902 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 1903 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1904 | if (paren == REG_NPAREN) |
| 1905 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 1906 | else |
| 1907 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 1908 | } |
| 1909 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 1910 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1911 | if (peekchr() == Magic(')')) |
| 1912 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 1913 | else |
| 1914 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 1915 | } |
| 1916 | /* |
| 1917 | * Here we set the flag allowing back references to this set of |
| 1918 | * parentheses. |
| 1919 | */ |
| 1920 | if (paren == REG_PAREN) |
| 1921 | { |
| 1922 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 1923 | EMIT(NFA_MOPEN + parno); |
| 1924 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1925 | #ifdef FEAT_SYN_HL |
| 1926 | else if (paren == REG_ZPAREN) |
| 1927 | EMIT(NFA_ZOPEN + parno); |
| 1928 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1929 | |
| 1930 | return OK; |
| 1931 | } |
| 1932 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1933 | #ifdef DEBUG |
| 1934 | static char_u code[50]; |
| 1935 | |
| 1936 | static void |
| 1937 | nfa_set_code(c) |
| 1938 | int c; |
| 1939 | { |
| 1940 | int addnl = FALSE; |
| 1941 | |
| 1942 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 1943 | { |
| 1944 | addnl = TRUE; |
| 1945 | c -= ADD_NL; |
| 1946 | } |
| 1947 | |
| 1948 | STRCPY(code, ""); |
| 1949 | switch (c) |
| 1950 | { |
| 1951 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 1952 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 1953 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 1954 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 1955 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 1956 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 1957 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1958 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 1959 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 1960 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 1961 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 1962 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 1963 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 1964 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 1965 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 1966 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1967 | #ifdef FEAT_SYN_HL |
| 1968 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 1969 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 1970 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 1971 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 1972 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 1973 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 1974 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 1975 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 1976 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 1977 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1978 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 1979 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1980 | case NFA_PREV_ATOM_NO_WIDTH: |
| 1981 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1982 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 1983 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1984 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 1985 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 1986 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 1987 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1988 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 1989 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 1990 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 1991 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 1992 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1993 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1994 | case NFA_START_INVISIBLE_BEFORE: |
| 1995 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1996 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1997 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1998 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1999 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2000 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2001 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2002 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2003 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2004 | case NFA_MOPEN: |
| 2005 | case NFA_MOPEN1: |
| 2006 | case NFA_MOPEN2: |
| 2007 | case NFA_MOPEN3: |
| 2008 | case NFA_MOPEN4: |
| 2009 | case NFA_MOPEN5: |
| 2010 | case NFA_MOPEN6: |
| 2011 | case NFA_MOPEN7: |
| 2012 | case NFA_MOPEN8: |
| 2013 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2014 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2015 | code[10] = c - NFA_MOPEN + '0'; |
| 2016 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2017 | case NFA_MCLOSE: |
| 2018 | case NFA_MCLOSE1: |
| 2019 | case NFA_MCLOSE2: |
| 2020 | case NFA_MCLOSE3: |
| 2021 | case NFA_MCLOSE4: |
| 2022 | case NFA_MCLOSE5: |
| 2023 | case NFA_MCLOSE6: |
| 2024 | case NFA_MCLOSE7: |
| 2025 | case NFA_MCLOSE8: |
| 2026 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2027 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2028 | code[11] = c - NFA_MCLOSE + '0'; |
| 2029 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2030 | #ifdef FEAT_SYN_HL |
| 2031 | case NFA_ZOPEN: |
| 2032 | case NFA_ZOPEN1: |
| 2033 | case NFA_ZOPEN2: |
| 2034 | case NFA_ZOPEN3: |
| 2035 | case NFA_ZOPEN4: |
| 2036 | case NFA_ZOPEN5: |
| 2037 | case NFA_ZOPEN6: |
| 2038 | case NFA_ZOPEN7: |
| 2039 | case NFA_ZOPEN8: |
| 2040 | case NFA_ZOPEN9: |
| 2041 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2042 | code[10] = c - NFA_ZOPEN + '0'; |
| 2043 | break; |
| 2044 | case NFA_ZCLOSE: |
| 2045 | case NFA_ZCLOSE1: |
| 2046 | case NFA_ZCLOSE2: |
| 2047 | case NFA_ZCLOSE3: |
| 2048 | case NFA_ZCLOSE4: |
| 2049 | case NFA_ZCLOSE5: |
| 2050 | case NFA_ZCLOSE6: |
| 2051 | case NFA_ZCLOSE7: |
| 2052 | case NFA_ZCLOSE8: |
| 2053 | case NFA_ZCLOSE9: |
| 2054 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2055 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2056 | break; |
| 2057 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2058 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2059 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2060 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2061 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2062 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2063 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2064 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2065 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2066 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2067 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2068 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2069 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2070 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2071 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2072 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2073 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2074 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2075 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2076 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2077 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
| 2078 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2079 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2080 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2081 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2082 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2083 | case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; |
| 2084 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 2085 | |
| 2086 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2087 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2088 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2089 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2090 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2091 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2092 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2093 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2094 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2095 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2096 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2097 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2098 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2099 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2100 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2101 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2102 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2103 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2104 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2105 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2106 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2107 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2108 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2109 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2110 | |
| 2111 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2112 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2113 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2114 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2115 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2116 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2117 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2118 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2119 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2120 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2121 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2122 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2123 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2124 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2125 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2126 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2127 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2128 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2129 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2130 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2131 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2132 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2133 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2134 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2135 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2136 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2137 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
| 2138 | |
| 2139 | default: |
| 2140 | STRCPY(code, "CHAR(x)"); |
| 2141 | code[5] = c; |
| 2142 | } |
| 2143 | |
| 2144 | if (addnl == TRUE) |
| 2145 | STRCAT(code, " + NEWLINE "); |
| 2146 | |
| 2147 | } |
| 2148 | |
| 2149 | #ifdef ENABLE_LOG |
| 2150 | static FILE *log_fd; |
| 2151 | |
| 2152 | /* |
| 2153 | * Print the postfix notation of the current regexp. |
| 2154 | */ |
| 2155 | static void |
| 2156 | nfa_postfix_dump(expr, retval) |
| 2157 | char_u *expr; |
| 2158 | int retval; |
| 2159 | { |
| 2160 | int *p; |
| 2161 | FILE *f; |
| 2162 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2163 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2164 | if (f != NULL) |
| 2165 | { |
| 2166 | fprintf(f, "\n-------------------------\n"); |
| 2167 | if (retval == FAIL) |
| 2168 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2169 | else if (retval == OK) |
| 2170 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2171 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2172 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2173 | { |
| 2174 | nfa_set_code(*p); |
| 2175 | fprintf(f, "%s, ", code); |
| 2176 | } |
| 2177 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2178 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2179 | fprintf(f, "%d ", *p); |
| 2180 | fprintf(f, "\n\n"); |
| 2181 | fclose(f); |
| 2182 | } |
| 2183 | } |
| 2184 | |
| 2185 | /* |
| 2186 | * Print the NFA starting with a root node "state". |
| 2187 | */ |
| 2188 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2189 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2190 | FILE *debugf; |
| 2191 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2192 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2193 | garray_T indent; |
| 2194 | |
| 2195 | ga_init2(&indent, 1, 64); |
| 2196 | ga_append(&indent, '\0'); |
| 2197 | nfa_print_state2(debugf, state, &indent); |
| 2198 | ga_clear(&indent); |
| 2199 | } |
| 2200 | |
| 2201 | static void |
| 2202 | nfa_print_state2(debugf, state, indent) |
| 2203 | FILE *debugf; |
| 2204 | nfa_state_T *state; |
| 2205 | garray_T *indent; |
| 2206 | { |
| 2207 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2208 | |
| 2209 | if (state == NULL) |
| 2210 | return; |
| 2211 | |
| 2212 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2213 | |
| 2214 | /* Output indent */ |
| 2215 | p = (char_u *)indent->ga_data; |
| 2216 | if (indent->ga_len >= 3) |
| 2217 | { |
| 2218 | int last = indent->ga_len - 3; |
| 2219 | char_u save[2]; |
| 2220 | |
| 2221 | STRNCPY(save, &p[last], 2); |
| 2222 | STRNCPY(&p[last], "+-", 2); |
| 2223 | fprintf(debugf, " %s", p); |
| 2224 | STRNCPY(&p[last], save, 2); |
| 2225 | } |
| 2226 | else |
| 2227 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2228 | |
| 2229 | nfa_set_code(state->c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 2230 | fprintf(debugf, "%s%s (%d) (id=%d) val=%d\n", |
| 2231 | state->negated ? "NOT " : "", |
| 2232 | code, |
| 2233 | state->c, |
| 2234 | abs(state->id), |
| 2235 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2236 | if (state->id < 0) |
| 2237 | return; |
| 2238 | |
| 2239 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2240 | |
| 2241 | /* grow indent for state->out */ |
| 2242 | indent->ga_len -= 1; |
| 2243 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2244 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2245 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2246 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2247 | ga_append(indent, '\0'); |
| 2248 | |
| 2249 | nfa_print_state2(debugf, state->out, indent); |
| 2250 | |
| 2251 | /* replace last part of indent for state->out1 */ |
| 2252 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2253 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2254 | ga_append(indent, '\0'); |
| 2255 | |
| 2256 | nfa_print_state2(debugf, state->out1, indent); |
| 2257 | |
| 2258 | /* shrink indent */ |
| 2259 | indent->ga_len -= 3; |
| 2260 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* |
| 2264 | * Print the NFA state machine. |
| 2265 | */ |
| 2266 | static void |
| 2267 | nfa_dump(prog) |
| 2268 | nfa_regprog_T *prog; |
| 2269 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2270 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2271 | |
| 2272 | if (debugf != NULL) |
| 2273 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2274 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2275 | |
| 2276 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2277 | fprintf(debugf, "regstart: %d\n", prog->regstart); |
| 2278 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2279 | fclose(debugf); |
| 2280 | } |
| 2281 | } |
| 2282 | #endif /* ENABLE_LOG */ |
| 2283 | #endif /* DEBUG */ |
| 2284 | |
| 2285 | /* |
| 2286 | * Parse r.e. @expr and convert it into postfix form. |
| 2287 | * Return the postfix string on success, NULL otherwise. |
| 2288 | */ |
| 2289 | static int * |
| 2290 | re2post() |
| 2291 | { |
| 2292 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2293 | return NULL; |
| 2294 | EMIT(NFA_MOPEN); |
| 2295 | return post_start; |
| 2296 | } |
| 2297 | |
| 2298 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2299 | |
| 2300 | /* |
| 2301 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2302 | * if c == MATCH, no arrows out; matching state. |
| 2303 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2304 | * If c < 256, labeled arrow with character c to out. |
| 2305 | */ |
| 2306 | |
| 2307 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2308 | |
| 2309 | /* |
| 2310 | * Allocate and initialize nfa_state_T. |
| 2311 | */ |
| 2312 | static nfa_state_T * |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2313 | alloc_state(c, out, out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2314 | int c; |
| 2315 | nfa_state_T *out; |
| 2316 | nfa_state_T *out1; |
| 2317 | { |
| 2318 | nfa_state_T *s; |
| 2319 | |
| 2320 | if (istate >= nstate) |
| 2321 | return NULL; |
| 2322 | |
| 2323 | s = &state_ptr[istate++]; |
| 2324 | |
| 2325 | s->c = c; |
| 2326 | s->out = out; |
| 2327 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 2328 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2329 | |
| 2330 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2331 | s->lastlist[0] = 0; |
| 2332 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2333 | s->negated = FALSE; |
| 2334 | |
| 2335 | return s; |
| 2336 | } |
| 2337 | |
| 2338 | /* |
| 2339 | * A partially built NFA without the matching state filled in. |
| 2340 | * Frag_T.start points at the start state. |
| 2341 | * Frag_T.out is a list of places that need to be set to the |
| 2342 | * next state for this fragment. |
| 2343 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2344 | |
| 2345 | /* Since the out pointers in the list are always |
| 2346 | * uninitialized, we use the pointers themselves |
| 2347 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2348 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2349 | union Ptrlist |
| 2350 | { |
| 2351 | Ptrlist *next; |
| 2352 | nfa_state_T *s; |
| 2353 | }; |
| 2354 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2355 | struct Frag |
| 2356 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2357 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2358 | Ptrlist *out; |
| 2359 | }; |
| 2360 | typedef struct Frag Frag_T; |
| 2361 | |
| 2362 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2363 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2364 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2365 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2366 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2367 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2368 | |
| 2369 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2370 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2371 | */ |
| 2372 | static Frag_T |
| 2373 | frag(start, out) |
| 2374 | nfa_state_T *start; |
| 2375 | Ptrlist *out; |
| 2376 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2377 | Frag_T n; |
| 2378 | |
| 2379 | n.start = start; |
| 2380 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2381 | return n; |
| 2382 | } |
| 2383 | |
| 2384 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2385 | * Create singleton list containing just outp. |
| 2386 | */ |
| 2387 | static Ptrlist * |
| 2388 | list1(outp) |
| 2389 | nfa_state_T **outp; |
| 2390 | { |
| 2391 | Ptrlist *l; |
| 2392 | |
| 2393 | l = (Ptrlist *)outp; |
| 2394 | l->next = NULL; |
| 2395 | return l; |
| 2396 | } |
| 2397 | |
| 2398 | /* |
| 2399 | * Patch the list of states at out to point to start. |
| 2400 | */ |
| 2401 | static void |
| 2402 | patch(l, s) |
| 2403 | Ptrlist *l; |
| 2404 | nfa_state_T *s; |
| 2405 | { |
| 2406 | Ptrlist *next; |
| 2407 | |
| 2408 | for (; l; l = next) |
| 2409 | { |
| 2410 | next = l->next; |
| 2411 | l->s = s; |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | |
| 2416 | /* |
| 2417 | * Join the two lists l1 and l2, returning the combination. |
| 2418 | */ |
| 2419 | static Ptrlist * |
| 2420 | append(l1, l2) |
| 2421 | Ptrlist *l1; |
| 2422 | Ptrlist *l2; |
| 2423 | { |
| 2424 | Ptrlist *oldl1; |
| 2425 | |
| 2426 | oldl1 = l1; |
| 2427 | while (l1->next) |
| 2428 | l1 = l1->next; |
| 2429 | l1->next = l2; |
| 2430 | return oldl1; |
| 2431 | } |
| 2432 | |
| 2433 | /* |
| 2434 | * Stack used for transforming postfix form into NFA. |
| 2435 | */ |
| 2436 | static Frag_T empty; |
| 2437 | |
| 2438 | static void |
| 2439 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2440 | int *postfix UNUSED; |
| 2441 | int *end UNUSED; |
| 2442 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2443 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2444 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2445 | FILE *df; |
| 2446 | int *p2; |
| 2447 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2448 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2449 | if (df) |
| 2450 | { |
| 2451 | fprintf(df, "Error popping the stack!\n"); |
| 2452 | #ifdef DEBUG |
| 2453 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2454 | #endif |
| 2455 | fprintf(df, "Postfix form is: "); |
| 2456 | #ifdef DEBUG |
| 2457 | for (p2 = postfix; p2 < end; p2++) |
| 2458 | { |
| 2459 | nfa_set_code(*p2); |
| 2460 | fprintf(df, "%s, ", code); |
| 2461 | } |
| 2462 | nfa_set_code(*p); |
| 2463 | fprintf(df, "\nCurrent position is: "); |
| 2464 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2465 | { |
| 2466 | nfa_set_code(*p2); |
| 2467 | fprintf(df, "%s, ", code); |
| 2468 | } |
| 2469 | #else |
| 2470 | for (p2 = postfix; p2 < end; p2++) |
| 2471 | { |
| 2472 | fprintf(df, "%d, ", *p2); |
| 2473 | } |
| 2474 | fprintf(df, "\nCurrent position is: "); |
| 2475 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2476 | { |
| 2477 | fprintf(df, "%d, ", *p2); |
| 2478 | } |
| 2479 | #endif |
| 2480 | fprintf(df, "\n--------------------------\n"); |
| 2481 | fclose(df); |
| 2482 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2483 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2484 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2485 | } |
| 2486 | |
| 2487 | /* |
| 2488 | * Push an item onto the stack. |
| 2489 | */ |
| 2490 | static void |
| 2491 | st_push(s, p, stack_end) |
| 2492 | Frag_T s; |
| 2493 | Frag_T **p; |
| 2494 | Frag_T *stack_end; |
| 2495 | { |
| 2496 | Frag_T *stackp = *p; |
| 2497 | |
| 2498 | if (stackp >= stack_end) |
| 2499 | return; |
| 2500 | *stackp = s; |
| 2501 | *p = *p + 1; |
| 2502 | } |
| 2503 | |
| 2504 | /* |
| 2505 | * Pop an item from the stack. |
| 2506 | */ |
| 2507 | static Frag_T |
| 2508 | st_pop(p, stack) |
| 2509 | Frag_T **p; |
| 2510 | Frag_T *stack; |
| 2511 | { |
| 2512 | Frag_T *stackp; |
| 2513 | |
| 2514 | *p = *p - 1; |
| 2515 | stackp = *p; |
| 2516 | if (stackp < stack) |
| 2517 | return empty; |
| 2518 | return **p; |
| 2519 | } |
| 2520 | |
| 2521 | /* |
| 2522 | * Convert a postfix form into its equivalent NFA. |
| 2523 | * Return the NFA start state on success, NULL otherwise. |
| 2524 | */ |
| 2525 | static nfa_state_T * |
| 2526 | post2nfa(postfix, end, nfa_calc_size) |
| 2527 | int *postfix; |
| 2528 | int *end; |
| 2529 | int nfa_calc_size; |
| 2530 | { |
| 2531 | int *p; |
| 2532 | int mopen; |
| 2533 | int mclose; |
| 2534 | Frag_T *stack = NULL; |
| 2535 | Frag_T *stackp = NULL; |
| 2536 | Frag_T *stack_end = NULL; |
| 2537 | Frag_T e1; |
| 2538 | Frag_T e2; |
| 2539 | Frag_T e; |
| 2540 | nfa_state_T *s; |
| 2541 | nfa_state_T *s1; |
| 2542 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2543 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2544 | |
| 2545 | if (postfix == NULL) |
| 2546 | return NULL; |
| 2547 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2548 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2549 | #define POP() st_pop(&stackp, stack); \ |
| 2550 | if (stackp < stack) \ |
| 2551 | { \ |
| 2552 | st_error(postfix, end, p); \ |
| 2553 | return NULL; \ |
| 2554 | } |
| 2555 | |
| 2556 | if (nfa_calc_size == FALSE) |
| 2557 | { |
| 2558 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2559 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2560 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2561 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | for (p = postfix; p < end; ++p) |
| 2565 | { |
| 2566 | switch (*p) |
| 2567 | { |
| 2568 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 2569 | /* Concatenation. |
| 2570 | * Pay attention: this operator does not exist in the r.e. itself |
| 2571 | * (it is implicit, really). It is added when r.e. is translated |
| 2572 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2573 | if (nfa_calc_size == TRUE) |
| 2574 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2575 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2576 | break; |
| 2577 | } |
| 2578 | e2 = POP(); |
| 2579 | e1 = POP(); |
| 2580 | patch(e1.out, e2.start); |
| 2581 | PUSH(frag(e1.start, e2.out)); |
| 2582 | break; |
| 2583 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2584 | case NFA_OR: |
| 2585 | /* Alternation */ |
| 2586 | if (nfa_calc_size == TRUE) |
| 2587 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2588 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2589 | break; |
| 2590 | } |
| 2591 | e2 = POP(); |
| 2592 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2593 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2594 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2595 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2596 | PUSH(frag(s, append(e1.out, e2.out))); |
| 2597 | break; |
| 2598 | |
| 2599 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2600 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2601 | if (nfa_calc_size == TRUE) |
| 2602 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2603 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2604 | break; |
| 2605 | } |
| 2606 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2607 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2608 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2609 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2610 | patch(e.out, s); |
| 2611 | PUSH(frag(s, list1(&s->out1))); |
| 2612 | break; |
| 2613 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2614 | case NFA_STAR_NONGREEDY: |
| 2615 | /* Zero or more, prefer zero */ |
| 2616 | if (nfa_calc_size == TRUE) |
| 2617 | { |
| 2618 | nstate++; |
| 2619 | break; |
| 2620 | } |
| 2621 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2622 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2623 | if (s == NULL) |
| 2624 | goto theend; |
| 2625 | patch(e.out, s); |
| 2626 | PUSH(frag(s, list1(&s->out))); |
| 2627 | break; |
| 2628 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2629 | case NFA_QUEST: |
| 2630 | /* one or zero atoms=> greedy match */ |
| 2631 | if (nfa_calc_size == TRUE) |
| 2632 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2633 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2634 | break; |
| 2635 | } |
| 2636 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2637 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2638 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2639 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2640 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 2641 | break; |
| 2642 | |
| 2643 | case NFA_QUEST_NONGREEDY: |
| 2644 | /* zero or one atoms => non-greedy match */ |
| 2645 | if (nfa_calc_size == TRUE) |
| 2646 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2647 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2648 | break; |
| 2649 | } |
| 2650 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2651 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2652 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2653 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2654 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 2655 | break; |
| 2656 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 2657 | case NFA_END_COLL: |
| 2658 | case NFA_END_NEG_COLL: |
| 2659 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 2660 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 2661 | * add the output to the start. */ |
| 2662 | if (nfa_calc_size == TRUE) |
| 2663 | { |
| 2664 | nstate++; |
| 2665 | break; |
| 2666 | } |
| 2667 | e = POP(); |
| 2668 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 2669 | if (s == NULL) |
| 2670 | goto theend; |
| 2671 | patch(e.out, s); |
| 2672 | e.start->out1 = s; |
| 2673 | PUSH(frag(e.start, list1(&s->out))); |
| 2674 | break; |
| 2675 | |
| 2676 | case NFA_RANGE: |
| 2677 | /* Before this are two characters, the low and high end of a |
| 2678 | * range. Turn them into two states with MIN and MAX. */ |
| 2679 | if (nfa_calc_size == TRUE) |
| 2680 | { |
| 2681 | /* nstate += 0; */ |
| 2682 | break; |
| 2683 | } |
| 2684 | e2 = POP(); |
| 2685 | e1 = POP(); |
| 2686 | e2.start->val = e2.start->c; |
| 2687 | e2.start->c = NFA_RANGE_MAX; |
| 2688 | e1.start->val = e1.start->c; |
| 2689 | e1.start->c = NFA_RANGE_MIN; |
| 2690 | patch(e1.out, e2.start); |
| 2691 | PUSH(frag(e1.start, e2.out)); |
| 2692 | break; |
| 2693 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2694 | case NFA_SKIP_CHAR: |
| 2695 | /* Symbol of 0-length, Used in a repetition |
| 2696 | * with max/min count of 0 */ |
| 2697 | if (nfa_calc_size == TRUE) |
| 2698 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2699 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2700 | break; |
| 2701 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2702 | s = alloc_state(NFA_SKIP_CHAR, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2703 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2704 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2705 | PUSH(frag(s, list1(&s->out))); |
| 2706 | break; |
| 2707 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2708 | case NFA_OPT_CHARS: |
| 2709 | { |
| 2710 | int n; |
| 2711 | |
| 2712 | /* \%[abc] */ |
| 2713 | n = *++p; /* get number of characters */ |
| 2714 | if (nfa_calc_size == TRUE) |
| 2715 | { |
| 2716 | nstate += n; |
| 2717 | break; |
| 2718 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 2719 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2720 | e1.out = NULL; /* stores list with out1's */ |
| 2721 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 2722 | while (n-- > 0) |
| 2723 | { |
| 2724 | e = POP(); /* get character */ |
| 2725 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 2726 | if (s == NULL) |
| 2727 | goto theend; |
| 2728 | if (e1.out == NULL) |
| 2729 | e1 = e; |
| 2730 | patch(e.out, s1); |
| 2731 | append(e1.out, list1(&s->out1)); |
| 2732 | s1 = s; |
| 2733 | } |
| 2734 | PUSH(frag(s, e1.out)); |
| 2735 | break; |
| 2736 | } |
| 2737 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2738 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 2739 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2740 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2741 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2742 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2743 | { |
| 2744 | int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG |
| 2745 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
| 2746 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 2747 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2748 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
| 2749 | int start_state = NFA_START_INVISIBLE; |
| 2750 | int end_state = NFA_END_INVISIBLE; |
| 2751 | int n = 0; |
| 2752 | nfa_state_T *zend; |
| 2753 | nfa_state_T *skip; |
| 2754 | |
| 2755 | if (before) |
| 2756 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 2757 | else if (pattern) |
| 2758 | { |
| 2759 | start_state = NFA_START_PATTERN; |
| 2760 | end_state = NFA_END_PATTERN; |
| 2761 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2762 | |
| 2763 | if (before) |
| 2764 | n = *++p; /* get the count */ |
| 2765 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2766 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 2767 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2768 | * The \@<= operator: match for the preceding atom. |
| 2769 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2770 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2771 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2772 | |
| 2773 | if (nfa_calc_size == TRUE) |
| 2774 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2775 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2776 | break; |
| 2777 | } |
| 2778 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2779 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2780 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2781 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2782 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2783 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2784 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2785 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2786 | if (neg) |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 2787 | { |
| 2788 | s->negated = TRUE; |
| 2789 | s1->negated = TRUE; |
| 2790 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2791 | if (before) |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2792 | s->val = n; /* store the count */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2793 | if (pattern) |
| 2794 | { |
| 2795 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 2796 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 2797 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 2798 | s1->out= skip; |
| 2799 | patch(e.out, zend); |
| 2800 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2801 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2802 | else |
| 2803 | { |
| 2804 | patch(e.out, s1); |
| 2805 | PUSH(frag(s, list1(&s1->out))); |
| 2806 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2807 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2808 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2809 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2810 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2811 | case NFA_COMPOSING: /* char with composing char */ |
| 2812 | #if 0 |
| 2813 | /* TODO */ |
| 2814 | if (regflags & RF_ICOMBINE) |
| 2815 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 2816 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2817 | } |
| 2818 | #endif |
| 2819 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2820 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2821 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2822 | case NFA_MOPEN: /* \( \) Submatch */ |
| 2823 | case NFA_MOPEN1: |
| 2824 | case NFA_MOPEN2: |
| 2825 | case NFA_MOPEN3: |
| 2826 | case NFA_MOPEN4: |
| 2827 | case NFA_MOPEN5: |
| 2828 | case NFA_MOPEN6: |
| 2829 | case NFA_MOPEN7: |
| 2830 | case NFA_MOPEN8: |
| 2831 | case NFA_MOPEN9: |
| 2832 | #ifdef FEAT_SYN_HL |
| 2833 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 2834 | case NFA_ZOPEN1: |
| 2835 | case NFA_ZOPEN2: |
| 2836 | case NFA_ZOPEN3: |
| 2837 | case NFA_ZOPEN4: |
| 2838 | case NFA_ZOPEN5: |
| 2839 | case NFA_ZOPEN6: |
| 2840 | case NFA_ZOPEN7: |
| 2841 | case NFA_ZOPEN8: |
| 2842 | case NFA_ZOPEN9: |
| 2843 | #endif |
| 2844 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2845 | if (nfa_calc_size == TRUE) |
| 2846 | { |
| 2847 | nstate += 2; |
| 2848 | break; |
| 2849 | } |
| 2850 | |
| 2851 | mopen = *p; |
| 2852 | switch (*p) |
| 2853 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2854 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 2855 | #ifdef FEAT_SYN_HL |
| 2856 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 2857 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 2858 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 2859 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 2860 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 2861 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 2862 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 2863 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 2864 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 2865 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 2866 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2867 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2868 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2869 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2870 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2871 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2872 | mclose = *p + NSUBEXP; |
| 2873 | break; |
| 2874 | } |
| 2875 | |
| 2876 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 2877 | * the empty regexp "". In this case, the NFA will be |
| 2878 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 2879 | * empty groups of parenthesis, and empty mbyte chars */ |
| 2880 | if (stackp == stack) |
| 2881 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2882 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2883 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2884 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2885 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2886 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2887 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2888 | patch(list1(&s->out), s1); |
| 2889 | PUSH(frag(s, list1(&s1->out))); |
| 2890 | break; |
| 2891 | } |
| 2892 | |
| 2893 | /* At least one node was emitted before NFA_MOPEN, so |
| 2894 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 2895 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2896 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2897 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2898 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2899 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2900 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2901 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2902 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2903 | patch(e.out, s1); |
| 2904 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2905 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2906 | if (mopen == NFA_COMPOSING) |
| 2907 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2908 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2909 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2910 | |
| 2911 | PUSH(frag(s, list1(&s1->out))); |
| 2912 | break; |
| 2913 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2914 | case NFA_BACKREF1: |
| 2915 | case NFA_BACKREF2: |
| 2916 | case NFA_BACKREF3: |
| 2917 | case NFA_BACKREF4: |
| 2918 | case NFA_BACKREF5: |
| 2919 | case NFA_BACKREF6: |
| 2920 | case NFA_BACKREF7: |
| 2921 | case NFA_BACKREF8: |
| 2922 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2923 | #ifdef FEAT_SYN_HL |
| 2924 | case NFA_ZREF1: |
| 2925 | case NFA_ZREF2: |
| 2926 | case NFA_ZREF3: |
| 2927 | case NFA_ZREF4: |
| 2928 | case NFA_ZREF5: |
| 2929 | case NFA_ZREF6: |
| 2930 | case NFA_ZREF7: |
| 2931 | case NFA_ZREF8: |
| 2932 | case NFA_ZREF9: |
| 2933 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2934 | if (nfa_calc_size == TRUE) |
| 2935 | { |
| 2936 | nstate += 2; |
| 2937 | break; |
| 2938 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2939 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2940 | if (s == NULL) |
| 2941 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2942 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2943 | if (s1 == NULL) |
| 2944 | goto theend; |
| 2945 | patch(list1(&s->out), s1); |
| 2946 | PUSH(frag(s, list1(&s1->out))); |
| 2947 | break; |
| 2948 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2949 | case NFA_LNUM: |
| 2950 | case NFA_LNUM_GT: |
| 2951 | case NFA_LNUM_LT: |
| 2952 | case NFA_VCOL: |
| 2953 | case NFA_VCOL_GT: |
| 2954 | case NFA_VCOL_LT: |
| 2955 | case NFA_COL: |
| 2956 | case NFA_COL_GT: |
| 2957 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2958 | case NFA_MARK: |
| 2959 | case NFA_MARK_GT: |
| 2960 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2961 | { |
| 2962 | int n = *++p; /* lnum, col or mark name */ |
| 2963 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2964 | if (nfa_calc_size == TRUE) |
| 2965 | { |
| 2966 | nstate += 1; |
| 2967 | break; |
| 2968 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2969 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2970 | if (s == NULL) |
| 2971 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2972 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2973 | PUSH(frag(s, list1(&s->out))); |
| 2974 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2975 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2976 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2977 | case NFA_ZSTART: |
| 2978 | case NFA_ZEND: |
| 2979 | default: |
| 2980 | /* Operands */ |
| 2981 | if (nfa_calc_size == TRUE) |
| 2982 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2983 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2984 | break; |
| 2985 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2986 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2987 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2988 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2989 | PUSH(frag(s, list1(&s->out))); |
| 2990 | break; |
| 2991 | |
| 2992 | } /* switch(*p) */ |
| 2993 | |
| 2994 | } /* for(p = postfix; *p; ++p) */ |
| 2995 | |
| 2996 | if (nfa_calc_size == TRUE) |
| 2997 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2998 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2999 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3000 | } |
| 3001 | |
| 3002 | e = POP(); |
| 3003 | if (stackp != stack) |
| 3004 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 3005 | |
| 3006 | if (istate >= nstate) |
| 3007 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 3008 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3009 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3010 | matchstate->c = NFA_MATCH; |
| 3011 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 3012 | matchstate->negated = FALSE; |
| 3013 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3014 | |
| 3015 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3016 | ret = e.start; |
| 3017 | |
| 3018 | theend: |
| 3019 | vim_free(stack); |
| 3020 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3021 | |
| 3022 | #undef POP1 |
| 3023 | #undef PUSH1 |
| 3024 | #undef POP2 |
| 3025 | #undef PUSH2 |
| 3026 | #undef POP |
| 3027 | #undef PUSH |
| 3028 | } |
| 3029 | |
| 3030 | /**************************************************************** |
| 3031 | * NFA execution code. |
| 3032 | ****************************************************************/ |
| 3033 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3034 | typedef struct |
| 3035 | { |
| 3036 | int in_use; /* number of subexpr with useful info */ |
| 3037 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3038 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3039 | union |
| 3040 | { |
| 3041 | struct multipos |
| 3042 | { |
| 3043 | lpos_T start; |
| 3044 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3045 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3046 | struct linepos |
| 3047 | { |
| 3048 | char_u *start; |
| 3049 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3050 | } line[NSUBEXP]; |
| 3051 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3052 | } regsub_T; |
| 3053 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3054 | typedef struct |
| 3055 | { |
| 3056 | regsub_T norm; /* \( .. \) matches */ |
| 3057 | #ifdef FEAT_SYN_HL |
| 3058 | regsub_T synt; /* \z( .. \) matches */ |
| 3059 | #endif |
| 3060 | } regsubs_T; |
| 3061 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3062 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3063 | typedef struct nfa_pim_S nfa_pim_T; |
| 3064 | struct nfa_pim_S |
| 3065 | { |
| 3066 | nfa_state_T *state; |
| 3067 | int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */ |
| 3068 | nfa_pim_T *pim; /* another PIM at the same position */ |
| 3069 | regsubs_T subs; /* submatch info, only party used */ |
| 3070 | }; |
| 3071 | |
| 3072 | /* Values for done in nfa_pim_T. */ |
| 3073 | #define NFA_PIM_TODO 0 |
| 3074 | #define NFA_PIM_MATCH 1 |
| 3075 | #define NFA_PIM_NOMATCH -1 |
| 3076 | |
| 3077 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3078 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3079 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3080 | { |
| 3081 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3082 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3083 | nfa_pim_T *pim; /* if not NULL: postponed invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3084 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3085 | } nfa_thread_T; |
| 3086 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3087 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3088 | typedef struct |
| 3089 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3090 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3091 | int n; /* nr of states currently in "t" */ |
| 3092 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3093 | int id; /* ID of the list */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3094 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3095 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3096 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3097 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 3098 | static void log_subexpr __ARGS((regsub_T *sub)); |
| 3099 | |
| 3100 | static void |
| 3101 | log_subsexpr(subs) |
| 3102 | regsubs_T *subs; |
| 3103 | { |
| 3104 | log_subexpr(&subs->norm); |
| 3105 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3106 | if (nfa_has_zsubexpr) |
| 3107 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3108 | # endif |
| 3109 | } |
| 3110 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3111 | static void |
| 3112 | log_subexpr(sub) |
| 3113 | regsub_T *sub; |
| 3114 | { |
| 3115 | int j; |
| 3116 | |
| 3117 | for (j = 0; j < sub->in_use; j++) |
| 3118 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3119 | fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3120 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3121 | sub->list.multi[j].start.col, |
| 3122 | (int)sub->list.multi[j].start.lnum, |
| 3123 | sub->list.multi[j].end.col, |
| 3124 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3125 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3126 | { |
| 3127 | char *s = (char *)sub->list.line[j].start; |
| 3128 | char *e = (char *)sub->list.line[j].end; |
| 3129 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3130 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3131 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3132 | s == NULL ? "NULL" : s, |
| 3133 | e == NULL ? "NULL" : e); |
| 3134 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3135 | } |
| 3136 | #endif |
| 3137 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3138 | /* Used during execution: whether a match has been found. */ |
| 3139 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3140 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3141 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3142 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3143 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3144 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3145 | static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off)); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3146 | static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3147 | |
| 3148 | static void |
| 3149 | clear_sub(sub) |
| 3150 | regsub_T *sub; |
| 3151 | { |
| 3152 | if (REG_MULTI) |
| 3153 | /* Use 0xff to set lnum to -1 */ |
| 3154 | vim_memset(sub->list.multi, 0xff, |
| 3155 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3156 | else |
| 3157 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3158 | sub->in_use = 0; |
| 3159 | } |
| 3160 | |
| 3161 | /* |
| 3162 | * Copy the submatches from "from" to "to". |
| 3163 | */ |
| 3164 | static void |
| 3165 | copy_sub(to, from) |
| 3166 | regsub_T *to; |
| 3167 | regsub_T *from; |
| 3168 | { |
| 3169 | to->in_use = from->in_use; |
| 3170 | if (from->in_use > 0) |
| 3171 | { |
| 3172 | /* Copy the match start and end positions. */ |
| 3173 | if (REG_MULTI) |
| 3174 | mch_memmove(&to->list.multi[0], |
| 3175 | &from->list.multi[0], |
| 3176 | sizeof(struct multipos) * from->in_use); |
| 3177 | else |
| 3178 | mch_memmove(&to->list.line[0], |
| 3179 | &from->list.line[0], |
| 3180 | sizeof(struct linepos) * from->in_use); |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | /* |
| 3185 | * Like copy_sub() but exclude the main match. |
| 3186 | */ |
| 3187 | static void |
| 3188 | copy_sub_off(to, from) |
| 3189 | regsub_T *to; |
| 3190 | regsub_T *from; |
| 3191 | { |
| 3192 | if (to->in_use < from->in_use) |
| 3193 | to->in_use = from->in_use; |
| 3194 | if (from->in_use > 1) |
| 3195 | { |
| 3196 | /* Copy the match start and end positions. */ |
| 3197 | if (REG_MULTI) |
| 3198 | mch_memmove(&to->list.multi[1], |
| 3199 | &from->list.multi[1], |
| 3200 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3201 | else |
| 3202 | mch_memmove(&to->list.line[1], |
| 3203 | &from->list.line[1], |
| 3204 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3205 | } |
| 3206 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3207 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3208 | /* |
| 3209 | * Return TRUE if "sub1" and "sub2" have the same positions. |
| 3210 | */ |
| 3211 | static int |
| 3212 | sub_equal(sub1, sub2) |
| 3213 | regsub_T *sub1; |
| 3214 | regsub_T *sub2; |
| 3215 | { |
| 3216 | int i; |
| 3217 | int todo; |
| 3218 | linenr_T s1, e1; |
| 3219 | linenr_T s2, e2; |
| 3220 | char_u *sp1, *ep1; |
| 3221 | char_u *sp2, *ep2; |
| 3222 | |
| 3223 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3224 | if (REG_MULTI) |
| 3225 | { |
| 3226 | for (i = 0; i < todo; ++i) |
| 3227 | { |
| 3228 | if (i < sub1->in_use) |
| 3229 | { |
| 3230 | s1 = sub1->list.multi[i].start.lnum; |
| 3231 | e1 = sub1->list.multi[i].end.lnum; |
| 3232 | } |
| 3233 | else |
| 3234 | { |
| 3235 | s1 = 0; |
| 3236 | e1 = 0; |
| 3237 | } |
| 3238 | if (i < sub2->in_use) |
| 3239 | { |
| 3240 | s2 = sub2->list.multi[i].start.lnum; |
| 3241 | e2 = sub2->list.multi[i].end.lnum; |
| 3242 | } |
| 3243 | else |
| 3244 | { |
| 3245 | s2 = 0; |
| 3246 | e2 = 0; |
| 3247 | } |
| 3248 | if (s1 != s2 || e1 != e2) |
| 3249 | return FALSE; |
| 3250 | if (s1 != 0 && sub1->list.multi[i].start.col |
| 3251 | != sub2->list.multi[i].start.col) |
| 3252 | return FALSE; |
| 3253 | if (e1 != 0 && sub1->list.multi[i].end.col |
| 3254 | != sub2->list.multi[i].end.col) |
| 3255 | return FALSE; |
| 3256 | } |
| 3257 | } |
| 3258 | else |
| 3259 | { |
| 3260 | for (i = 0; i < todo; ++i) |
| 3261 | { |
| 3262 | if (i < sub1->in_use) |
| 3263 | { |
| 3264 | sp1 = sub1->list.line[i].start; |
| 3265 | ep1 = sub1->list.line[i].end; |
| 3266 | } |
| 3267 | else |
| 3268 | { |
| 3269 | sp1 = NULL; |
| 3270 | ep1 = NULL; |
| 3271 | } |
| 3272 | if (i < sub2->in_use) |
| 3273 | { |
| 3274 | sp2 = sub2->list.line[i].start; |
| 3275 | ep2 = sub2->list.line[i].end; |
| 3276 | } |
| 3277 | else |
| 3278 | { |
| 3279 | sp2 = NULL; |
| 3280 | ep2 = NULL; |
| 3281 | } |
| 3282 | if (sp1 != sp2 || ep1 != ep2) |
| 3283 | return FALSE; |
| 3284 | } |
| 3285 | } |
| 3286 | |
| 3287 | return TRUE; |
| 3288 | } |
| 3289 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3290 | #ifdef ENABLE_LOG |
| 3291 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3292 | report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3293 | { |
| 3294 | int col; |
| 3295 | |
| 3296 | if (sub->in_use <= 0) |
| 3297 | col = -1; |
| 3298 | else if (REG_MULTI) |
| 3299 | col = sub->list.multi[0].start.col; |
| 3300 | else |
| 3301 | col = (int)(sub->list.line[0].start - regline); |
| 3302 | nfa_set_code(state->c); |
| 3303 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n", |
| 3304 | action, abs(state->id), lid, state->c, code, col); |
| 3305 | } |
| 3306 | #endif |
| 3307 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3308 | static void |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3309 | addstate(l, state, subs, off) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3310 | nfa_list_T *l; /* runtime state list */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3311 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3312 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3313 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3314 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3315 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3316 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3317 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3318 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3319 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3320 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3321 | regsub_T *sub; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3322 | #ifdef ENABLE_LOG |
| 3323 | int did_print = FALSE; |
| 3324 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3325 | |
| 3326 | if (l == NULL || state == NULL) |
| 3327 | return; |
| 3328 | |
| 3329 | switch (state->c) |
| 3330 | { |
| 3331 | case NFA_SPLIT: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3332 | case NFA_NOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3333 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3334 | case NFA_NCLOSE: |
| 3335 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3336 | case NFA_MCLOSE1: |
| 3337 | case NFA_MCLOSE2: |
| 3338 | case NFA_MCLOSE3: |
| 3339 | case NFA_MCLOSE4: |
| 3340 | case NFA_MCLOSE5: |
| 3341 | case NFA_MCLOSE6: |
| 3342 | case NFA_MCLOSE7: |
| 3343 | case NFA_MCLOSE8: |
| 3344 | case NFA_MCLOSE9: |
| 3345 | #ifdef FEAT_SYN_HL |
| 3346 | case NFA_ZCLOSE: |
| 3347 | case NFA_ZCLOSE1: |
| 3348 | case NFA_ZCLOSE2: |
| 3349 | case NFA_ZCLOSE3: |
| 3350 | case NFA_ZCLOSE4: |
| 3351 | case NFA_ZCLOSE5: |
| 3352 | case NFA_ZCLOSE6: |
| 3353 | case NFA_ZCLOSE7: |
| 3354 | case NFA_ZCLOSE8: |
| 3355 | case NFA_ZCLOSE9: |
| 3356 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3357 | case NFA_ZEND: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3358 | /* These nodes are not added themselves but their "out" and/or |
| 3359 | * "out1" may be added below. */ |
| 3360 | break; |
| 3361 | |
| 3362 | case NFA_MOPEN: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3363 | case NFA_MOPEN1: |
| 3364 | case NFA_MOPEN2: |
| 3365 | case NFA_MOPEN3: |
| 3366 | case NFA_MOPEN4: |
| 3367 | case NFA_MOPEN5: |
| 3368 | case NFA_MOPEN6: |
| 3369 | case NFA_MOPEN7: |
| 3370 | case NFA_MOPEN8: |
| 3371 | case NFA_MOPEN9: |
| 3372 | #ifdef FEAT_SYN_HL |
| 3373 | case NFA_ZOPEN: |
| 3374 | case NFA_ZOPEN1: |
| 3375 | case NFA_ZOPEN2: |
| 3376 | case NFA_ZOPEN3: |
| 3377 | case NFA_ZOPEN4: |
| 3378 | case NFA_ZOPEN5: |
| 3379 | case NFA_ZOPEN6: |
| 3380 | case NFA_ZOPEN7: |
| 3381 | case NFA_ZOPEN8: |
| 3382 | case NFA_ZOPEN9: |
| 3383 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3384 | case NFA_ZSTART: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3385 | /* These nodes do not need to be added, but we need to bail out |
| 3386 | * when it was tried to be added to this list before. */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3387 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3388 | goto skip_add; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3389 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3390 | break; |
| 3391 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3392 | case NFA_BOL: |
| 3393 | case NFA_BOF: |
| 3394 | /* "^" won't match past end-of-line, don't bother trying. |
| 3395 | * Except when we are going to the next line for a look-behind |
| 3396 | * match. */ |
| 3397 | if (reginput > regline |
| 3398 | && (nfa_endp == NULL |
| 3399 | || !REG_MULTI |
| 3400 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 3401 | goto skip_add; |
| 3402 | /* FALLTHROUGH */ |
| 3403 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3404 | default: |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3405 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3406 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3407 | /* This state is already in the list, don't add it again, |
| 3408 | * unless it is an MOPEN that is used for a backreference. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3409 | if (!nfa_has_backref) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3410 | { |
| 3411 | skip_add: |
| 3412 | #ifdef ENABLE_LOG |
| 3413 | nfa_set_code(state->c); |
| 3414 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 3415 | abs(state->id), l->id, state->c, code); |
| 3416 | #endif |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3417 | return; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3418 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3419 | |
| 3420 | /* See if the same state is already in the list with the same |
| 3421 | * positions. */ |
| 3422 | for (i = 0; i < l->n; ++i) |
| 3423 | { |
| 3424 | thread = &l->t[i]; |
| 3425 | if (thread->state->id == state->id |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3426 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 3427 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3428 | && (!nfa_has_zsubexpr || |
| 3429 | sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3430 | #endif |
| 3431 | ) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3432 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3433 | } |
| 3434 | } |
| 3435 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3436 | /* when there are backreferences or look-behind matches the number |
| 3437 | * of states may be (a lot) bigger */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3438 | if (nfa_has_backref && l->n == l->len) |
| 3439 | { |
| 3440 | int newlen = l->len * 3 / 2 + 50; |
| 3441 | |
| 3442 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 3443 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3444 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3445 | |
| 3446 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3447 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3448 | thread = &l->t[l->n++]; |
| 3449 | thread->state = state; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3450 | thread->pim = NULL; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3451 | copy_sub(&thread->subs.norm, &subs->norm); |
| 3452 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3453 | if (nfa_has_zsubexpr) |
| 3454 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3455 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3456 | #ifdef ENABLE_LOG |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3457 | report_state("Adding", &thread->subs.norm, state, l->id); |
| 3458 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3459 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3460 | } |
| 3461 | |
| 3462 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3463 | if (!did_print) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3464 | report_state("Processing", &subs->norm, state, l->id); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3465 | #endif |
| 3466 | switch (state->c) |
| 3467 | { |
| 3468 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3469 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3470 | break; |
| 3471 | |
| 3472 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3473 | /* order matters here */ |
| 3474 | addstate(l, state->out, subs, off); |
| 3475 | addstate(l, state->out1, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3476 | break; |
| 3477 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3478 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3479 | case NFA_NOPEN: |
| 3480 | case NFA_NCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3481 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3482 | break; |
| 3483 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3484 | case NFA_MOPEN: |
| 3485 | case NFA_MOPEN1: |
| 3486 | case NFA_MOPEN2: |
| 3487 | case NFA_MOPEN3: |
| 3488 | case NFA_MOPEN4: |
| 3489 | case NFA_MOPEN5: |
| 3490 | case NFA_MOPEN6: |
| 3491 | case NFA_MOPEN7: |
| 3492 | case NFA_MOPEN8: |
| 3493 | case NFA_MOPEN9: |
| 3494 | #ifdef FEAT_SYN_HL |
| 3495 | case NFA_ZOPEN: |
| 3496 | case NFA_ZOPEN1: |
| 3497 | case NFA_ZOPEN2: |
| 3498 | case NFA_ZOPEN3: |
| 3499 | case NFA_ZOPEN4: |
| 3500 | case NFA_ZOPEN5: |
| 3501 | case NFA_ZOPEN6: |
| 3502 | case NFA_ZOPEN7: |
| 3503 | case NFA_ZOPEN8: |
| 3504 | case NFA_ZOPEN9: |
| 3505 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3506 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3507 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3508 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3509 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3510 | sub = &subs->norm; |
| 3511 | } |
| 3512 | #ifdef FEAT_SYN_HL |
| 3513 | else if (state->c >= NFA_ZOPEN) |
| 3514 | { |
| 3515 | subidx = state->c - NFA_ZOPEN; |
| 3516 | sub = &subs->synt; |
| 3517 | } |
| 3518 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3519 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3520 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3521 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3522 | sub = &subs->norm; |
| 3523 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3524 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3525 | /* Set the position (with "off") in the subexpression. Save and |
| 3526 | * restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | 4b6ebe6 | 2013-05-30 17:49:24 +0200 | [diff] [blame] | 3527 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3528 | if (REG_MULTI) |
| 3529 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3530 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3531 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3532 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3533 | save_in_use = -1; |
| 3534 | } |
| 3535 | else |
| 3536 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3537 | save_in_use = sub->in_use; |
| 3538 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3539 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3540 | sub->list.multi[i].start.lnum = -1; |
| 3541 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3542 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3543 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3544 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3545 | if (off == -1) |
| 3546 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3547 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 3548 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3549 | } |
| 3550 | else |
| 3551 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3552 | sub->list.multi[subidx].start.lnum = reglnum; |
| 3553 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3554 | (colnr_T)(reginput - regline + off); |
| 3555 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3556 | } |
| 3557 | else |
| 3558 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3559 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3560 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3561 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3562 | save_in_use = -1; |
| 3563 | } |
| 3564 | else |
| 3565 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3566 | save_in_use = sub->in_use; |
| 3567 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3568 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3569 | sub->list.line[i].start = NULL; |
| 3570 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3571 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3572 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3573 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3574 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3575 | } |
| 3576 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3577 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3578 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3579 | if (save_in_use == -1) |
| 3580 | { |
| 3581 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3582 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3583 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3584 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3585 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3586 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3587 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3588 | break; |
| 3589 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3590 | case NFA_MCLOSE: |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 3591 | if (nfa_has_zend) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3592 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 3593 | /* Do not overwrite the position set by \ze. If no \ze |
| 3594 | * encountered end will be set in nfa_regtry(). */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3595 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3596 | break; |
| 3597 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3598 | case NFA_MCLOSE1: |
| 3599 | case NFA_MCLOSE2: |
| 3600 | case NFA_MCLOSE3: |
| 3601 | case NFA_MCLOSE4: |
| 3602 | case NFA_MCLOSE5: |
| 3603 | case NFA_MCLOSE6: |
| 3604 | case NFA_MCLOSE7: |
| 3605 | case NFA_MCLOSE8: |
| 3606 | case NFA_MCLOSE9: |
| 3607 | #ifdef FEAT_SYN_HL |
| 3608 | case NFA_ZCLOSE: |
| 3609 | case NFA_ZCLOSE1: |
| 3610 | case NFA_ZCLOSE2: |
| 3611 | case NFA_ZCLOSE3: |
| 3612 | case NFA_ZCLOSE4: |
| 3613 | case NFA_ZCLOSE5: |
| 3614 | case NFA_ZCLOSE6: |
| 3615 | case NFA_ZCLOSE7: |
| 3616 | case NFA_ZCLOSE8: |
| 3617 | case NFA_ZCLOSE9: |
| 3618 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3619 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3620 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3621 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3622 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3623 | sub = &subs->norm; |
| 3624 | } |
| 3625 | #ifdef FEAT_SYN_HL |
| 3626 | else if (state->c >= NFA_ZCLOSE) |
| 3627 | { |
| 3628 | subidx = state->c - NFA_ZCLOSE; |
| 3629 | sub = &subs->synt; |
| 3630 | } |
| 3631 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3632 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3633 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3634 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3635 | sub = &subs->norm; |
| 3636 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3637 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3638 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 3639 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3640 | save_in_use = sub->in_use; |
| 3641 | if (sub->in_use <= subidx) |
| 3642 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3643 | if (REG_MULTI) |
| 3644 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3645 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3646 | if (off == -1) |
| 3647 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3648 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 3649 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3650 | } |
| 3651 | else |
| 3652 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3653 | sub->list.multi[subidx].end.lnum = reglnum; |
| 3654 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3655 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3656 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3657 | } |
| 3658 | else |
| 3659 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3660 | save_ptr = sub->list.line[subidx].end; |
| 3661 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3662 | } |
| 3663 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3664 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3665 | |
| 3666 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3667 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3668 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3669 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3670 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3671 | break; |
| 3672 | } |
| 3673 | } |
| 3674 | |
| 3675 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3676 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 3677 | * Used for zero-width matches, next state to use is the added one. |
| 3678 | * This makes sure the order of states to be tried does not change, which |
| 3679 | * matters for alternatives. |
| 3680 | */ |
| 3681 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3682 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3683 | nfa_list_T *l; /* runtime state list */ |
| 3684 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3685 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3686 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3687 | int *ip; |
| 3688 | { |
| 3689 | int tlen = l->n; |
| 3690 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3691 | int listidx = *ip; |
| 3692 | int i; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3693 | |
| 3694 | /* first add the state(s) at the end, so that we know how many there are */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3695 | addstate(l, state, subs, 0); |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3696 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3697 | /* fill in the "pim" field in the new states */ |
| 3698 | if (pim != NULL) |
| 3699 | for (i = tlen; i < l->n; ++i) |
| 3700 | l->t[i].pim = pim; |
| 3701 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3702 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3703 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3704 | return; |
| 3705 | |
| 3706 | /* re-order to put the new state at the current position */ |
| 3707 | count = l->n - tlen; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3708 | if (count == 1) |
| 3709 | { |
| 3710 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3711 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3712 | } |
| 3713 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3714 | { |
| 3715 | /* make space for new states, then move them from the |
| 3716 | * end to the current position */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3717 | mch_memmove(&(l->t[listidx + count]), |
| 3718 | &(l->t[listidx + 1]), |
| 3719 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 3720 | mch_memmove(&(l->t[listidx]), |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3721 | &(l->t[l->n - 1]), |
| 3722 | sizeof(nfa_thread_T) * count); |
| 3723 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3724 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3725 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3729 | * Check character class "class" against current character c. |
| 3730 | */ |
| 3731 | static int |
| 3732 | check_char_class(class, c) |
| 3733 | int class; |
| 3734 | int c; |
| 3735 | { |
| 3736 | switch (class) |
| 3737 | { |
| 3738 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3739 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3740 | return OK; |
| 3741 | break; |
| 3742 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3743 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3744 | return OK; |
| 3745 | break; |
| 3746 | case NFA_CLASS_BLANK: |
| 3747 | if (c == ' ' || c == '\t') |
| 3748 | return OK; |
| 3749 | break; |
| 3750 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3751 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3752 | return OK; |
| 3753 | break; |
| 3754 | case NFA_CLASS_DIGIT: |
| 3755 | if (VIM_ISDIGIT(c)) |
| 3756 | return OK; |
| 3757 | break; |
| 3758 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3759 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3760 | return OK; |
| 3761 | break; |
| 3762 | case NFA_CLASS_LOWER: |
| 3763 | if (MB_ISLOWER(c)) |
| 3764 | return OK; |
| 3765 | break; |
| 3766 | case NFA_CLASS_PRINT: |
| 3767 | if (vim_isprintc(c)) |
| 3768 | return OK; |
| 3769 | break; |
| 3770 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3771 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3772 | return OK; |
| 3773 | break; |
| 3774 | case NFA_CLASS_SPACE: |
| 3775 | if ((c >=9 && c <= 13) || (c == ' ')) |
| 3776 | return OK; |
| 3777 | break; |
| 3778 | case NFA_CLASS_UPPER: |
| 3779 | if (MB_ISUPPER(c)) |
| 3780 | return OK; |
| 3781 | break; |
| 3782 | case NFA_CLASS_XDIGIT: |
| 3783 | if (vim_isxdigit(c)) |
| 3784 | return OK; |
| 3785 | break; |
| 3786 | case NFA_CLASS_TAB: |
| 3787 | if (c == '\t') |
| 3788 | return OK; |
| 3789 | break; |
| 3790 | case NFA_CLASS_RETURN: |
| 3791 | if (c == '\r') |
| 3792 | return OK; |
| 3793 | break; |
| 3794 | case NFA_CLASS_BACKSPACE: |
| 3795 | if (c == '\b') |
| 3796 | return OK; |
| 3797 | break; |
| 3798 | case NFA_CLASS_ESCAPE: |
| 3799 | if (c == '\033') |
| 3800 | return OK; |
| 3801 | break; |
| 3802 | |
| 3803 | default: |
| 3804 | /* should not be here :P */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 3805 | EMSGN("E877: (NFA regexp) Invalid character class: %ld", class); |
| 3806 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3807 | } |
| 3808 | return FAIL; |
| 3809 | } |
| 3810 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3811 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
| 3812 | |
| 3813 | /* |
| 3814 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3815 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3816 | */ |
| 3817 | static int |
| 3818 | match_backref(sub, subidx, bytelen) |
| 3819 | regsub_T *sub; /* pointers to subexpressions */ |
| 3820 | int subidx; |
| 3821 | int *bytelen; /* out: length of match in bytes */ |
| 3822 | { |
| 3823 | int len; |
| 3824 | |
| 3825 | if (sub->in_use <= subidx) |
| 3826 | { |
| 3827 | retempty: |
| 3828 | /* backref was not set, match an empty string */ |
| 3829 | *bytelen = 0; |
| 3830 | return TRUE; |
| 3831 | } |
| 3832 | |
| 3833 | if (REG_MULTI) |
| 3834 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3835 | if (sub->list.multi[subidx].start.lnum < 0 |
| 3836 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3837 | goto retempty; |
| 3838 | /* TODO: line breaks */ |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3839 | len = sub->list.multi[subidx].end.col |
| 3840 | - sub->list.multi[subidx].start.col; |
| 3841 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3842 | reginput, &len) == 0) |
| 3843 | { |
| 3844 | *bytelen = len; |
| 3845 | return TRUE; |
| 3846 | } |
| 3847 | } |
| 3848 | else |
| 3849 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3850 | if (sub->list.line[subidx].start == NULL |
| 3851 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3852 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3853 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 3854 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3855 | { |
| 3856 | *bytelen = len; |
| 3857 | return TRUE; |
| 3858 | } |
| 3859 | } |
| 3860 | return FALSE; |
| 3861 | } |
| 3862 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3863 | #ifdef FEAT_SYN_HL |
| 3864 | |
| 3865 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 3866 | |
| 3867 | /* |
| 3868 | * Check for a match with \z subexpression "subidx". |
| 3869 | * Return TRUE if it matches. |
| 3870 | */ |
| 3871 | static int |
| 3872 | match_zref(subidx, bytelen) |
| 3873 | int subidx; |
| 3874 | int *bytelen; /* out: length of match in bytes */ |
| 3875 | { |
| 3876 | int len; |
| 3877 | |
| 3878 | cleanup_zsubexpr(); |
| 3879 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 3880 | { |
| 3881 | /* backref was not set, match an empty string */ |
| 3882 | *bytelen = 0; |
| 3883 | return TRUE; |
| 3884 | } |
| 3885 | |
| 3886 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 3887 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 3888 | { |
| 3889 | *bytelen = len; |
| 3890 | return TRUE; |
| 3891 | } |
| 3892 | return FALSE; |
| 3893 | } |
| 3894 | #endif |
| 3895 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3896 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3897 | * Save list IDs for all NFA states of "prog" into "list". |
| 3898 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3899 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3900 | */ |
| 3901 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3902 | nfa_save_listids(prog, list) |
| 3903 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3904 | int *list; |
| 3905 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3906 | int i; |
| 3907 | nfa_state_T *p; |
| 3908 | |
| 3909 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 3910 | p = &prog->state[0]; |
| 3911 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3912 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3913 | list[i] = p->lastlist[1]; |
| 3914 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3915 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3916 | } |
| 3917 | } |
| 3918 | |
| 3919 | /* |
| 3920 | * Restore list IDs from "list" to all NFA states. |
| 3921 | */ |
| 3922 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3923 | nfa_restore_listids(prog, list) |
| 3924 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3925 | int *list; |
| 3926 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3927 | int i; |
| 3928 | nfa_state_T *p; |
| 3929 | |
| 3930 | p = &prog->state[0]; |
| 3931 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3932 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3933 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3934 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3935 | } |
| 3936 | } |
| 3937 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3938 | static int |
| 3939 | nfa_re_num_cmp(val, op, pos) |
| 3940 | long_u val; |
| 3941 | int op; |
| 3942 | long_u pos; |
| 3943 | { |
| 3944 | if (op == 1) return pos > val; |
| 3945 | if (op == 2) return pos < val; |
| 3946 | return val == pos; |
| 3947 | } |
| 3948 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3949 | static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3950 | static int nfa_regmatch __ARGS((nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m)); |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3951 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3952 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3953 | * Recursively call nfa_regmatch() |
| 3954 | */ |
| 3955 | static int |
| 3956 | recursive_regmatch(state, prog, submatch, m, listids) |
| 3957 | nfa_state_T *state; |
| 3958 | nfa_regprog_T *prog; |
| 3959 | regsubs_T *submatch; |
| 3960 | regsubs_T *m; |
| 3961 | int **listids; |
| 3962 | { |
| 3963 | char_u *save_reginput = reginput; |
| 3964 | char_u *save_regline = regline; |
| 3965 | int save_reglnum = reglnum; |
| 3966 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3967 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3968 | save_se_T *save_nfa_endp = nfa_endp; |
| 3969 | save_se_T endpos; |
| 3970 | save_se_T *endposp = NULL; |
| 3971 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3972 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3973 | |
| 3974 | if (state->c == NFA_START_INVISIBLE_BEFORE) |
| 3975 | { |
| 3976 | /* The recursive match must end at the current position. */ |
| 3977 | endposp = &endpos; |
| 3978 | if (REG_MULTI) |
| 3979 | { |
| 3980 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 3981 | endpos.se_u.pos.lnum = reglnum; |
| 3982 | } |
| 3983 | else |
| 3984 | endpos.se_u.ptr = reginput; |
| 3985 | |
| 3986 | /* Go back the specified number of bytes, or as far as the |
| 3987 | * start of the previous line, to try matching "\@<=" or |
| 3988 | * not matching "\@<!". |
| 3989 | * TODO: This is very inefficient! Would be better to |
| 3990 | * first check for a match with what follows. */ |
| 3991 | if (state->val <= 0) |
| 3992 | { |
| 3993 | if (REG_MULTI) |
| 3994 | { |
| 3995 | regline = reg_getline(--reglnum); |
| 3996 | if (regline == NULL) |
| 3997 | /* can't go before the first line */ |
| 3998 | regline = reg_getline(++reglnum); |
| 3999 | } |
| 4000 | reginput = regline; |
| 4001 | } |
| 4002 | else |
| 4003 | { |
| 4004 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 4005 | { |
| 4006 | /* Not enough bytes in this line, go to end of |
| 4007 | * previous line. */ |
| 4008 | regline = reg_getline(--reglnum); |
| 4009 | if (regline == NULL) |
| 4010 | { |
| 4011 | /* can't go before the first line */ |
| 4012 | regline = reg_getline(++reglnum); |
| 4013 | reginput = regline; |
| 4014 | } |
| 4015 | else |
| 4016 | reginput = regline + STRLEN(regline); |
| 4017 | } |
| 4018 | if ((int)(reginput - regline) >= state->val) |
| 4019 | { |
| 4020 | reginput -= state->val; |
| 4021 | #ifdef FEAT_MBYTE |
| 4022 | if (has_mbyte) |
| 4023 | reginput -= mb_head_off(regline, reginput); |
| 4024 | #endif |
| 4025 | } |
| 4026 | else |
| 4027 | reginput = regline; |
| 4028 | } |
| 4029 | } |
| 4030 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4031 | #ifdef ENABLE_LOG |
| 4032 | if (log_fd != stderr) |
| 4033 | fclose(log_fd); |
| 4034 | log_fd = NULL; |
| 4035 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4036 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 4037 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 4038 | if (nfa_ll_index == 1) |
| 4039 | { |
| 4040 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 4041 | * values and clear them. */ |
| 4042 | if (*listids == NULL) |
| 4043 | { |
| 4044 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 4045 | if (*listids == NULL) |
| 4046 | { |
| 4047 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 4048 | return 0; |
| 4049 | } |
| 4050 | } |
| 4051 | nfa_save_listids(prog, *listids); |
| 4052 | need_restore = TRUE; |
| 4053 | /* any value of nfa_listid will do */ |
| 4054 | } |
| 4055 | else |
| 4056 | { |
| 4057 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 4058 | * entry. Make sure nfa_listid is different from a previous recursive |
| 4059 | * call, because some states may still have this ID. */ |
| 4060 | ++nfa_ll_index; |
| 4061 | if (nfa_listid <= nfa_alt_listid) |
| 4062 | nfa_listid = nfa_alt_listid; |
| 4063 | } |
| 4064 | |
| 4065 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 4066 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4067 | nfa_endp = endposp; |
| 4068 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4069 | |
| 4070 | if (need_restore) |
| 4071 | nfa_restore_listids(prog, *listids); |
| 4072 | else |
| 4073 | { |
| 4074 | --nfa_ll_index; |
| 4075 | nfa_alt_listid = nfa_listid; |
| 4076 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4077 | |
| 4078 | /* restore position in input text */ |
| 4079 | reginput = save_reginput; |
| 4080 | regline = save_regline; |
| 4081 | reglnum = save_reglnum; |
| 4082 | nfa_match = save_nfa_match; |
| 4083 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4084 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4085 | |
| 4086 | #ifdef ENABLE_LOG |
| 4087 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 4088 | if (log_fd != NULL) |
| 4089 | { |
| 4090 | fprintf(log_fd, "****************************\n"); |
| 4091 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 4092 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 4093 | fprintf(log_fd, "****************************\n"); |
| 4094 | } |
| 4095 | else |
| 4096 | { |
| 4097 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4098 | log_fd = stderr; |
| 4099 | } |
| 4100 | #endif |
| 4101 | |
| 4102 | return result; |
| 4103 | } |
| 4104 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4105 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
| 4106 | |
| 4107 | /* |
| 4108 | * Estimate the chance of a match with "state" failing. |
| 4109 | * NFA_ANY: 1 |
| 4110 | * specific character: 99 |
| 4111 | */ |
| 4112 | static int |
| 4113 | failure_chance(state, depth) |
| 4114 | nfa_state_T *state; |
| 4115 | int depth; |
| 4116 | { |
| 4117 | int c = state->c; |
| 4118 | int l, r; |
| 4119 | |
| 4120 | /* detect looping */ |
| 4121 | if (depth > 4) |
| 4122 | return 1; |
| 4123 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4124 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4125 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4126 | case NFA_SPLIT: |
| 4127 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 4128 | /* avoid recursive stuff */ |
| 4129 | return 1; |
| 4130 | /* two alternatives, use the lowest failure chance */ |
| 4131 | l = failure_chance(state->out, depth + 1); |
| 4132 | r = failure_chance(state->out1, depth + 1); |
| 4133 | return l < r ? l : r; |
| 4134 | |
| 4135 | case NFA_ANY: |
| 4136 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4137 | return 1; |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4138 | case NFA_MATCH: |
| 4139 | /* empty match works always */ |
| 4140 | return 0; |
| 4141 | |
| 4142 | case NFA_BOL: |
| 4143 | case NFA_EOL: |
| 4144 | case NFA_BOF: |
| 4145 | case NFA_EOF: |
| 4146 | case NFA_NEWL: |
| 4147 | return 99; |
| 4148 | |
| 4149 | case NFA_BOW: |
| 4150 | case NFA_EOW: |
| 4151 | return 90; |
| 4152 | |
| 4153 | case NFA_MOPEN: |
| 4154 | case NFA_MOPEN1: |
| 4155 | case NFA_MOPEN2: |
| 4156 | case NFA_MOPEN3: |
| 4157 | case NFA_MOPEN4: |
| 4158 | case NFA_MOPEN5: |
| 4159 | case NFA_MOPEN6: |
| 4160 | case NFA_MOPEN7: |
| 4161 | case NFA_MOPEN8: |
| 4162 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4163 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4164 | case NFA_ZOPEN: |
| 4165 | case NFA_ZOPEN1: |
| 4166 | case NFA_ZOPEN2: |
| 4167 | case NFA_ZOPEN3: |
| 4168 | case NFA_ZOPEN4: |
| 4169 | case NFA_ZOPEN5: |
| 4170 | case NFA_ZOPEN6: |
| 4171 | case NFA_ZOPEN7: |
| 4172 | case NFA_ZOPEN8: |
| 4173 | case NFA_ZOPEN9: |
| 4174 | case NFA_ZCLOSE: |
| 4175 | case NFA_ZCLOSE1: |
| 4176 | case NFA_ZCLOSE2: |
| 4177 | case NFA_ZCLOSE3: |
| 4178 | case NFA_ZCLOSE4: |
| 4179 | case NFA_ZCLOSE5: |
| 4180 | case NFA_ZCLOSE6: |
| 4181 | case NFA_ZCLOSE7: |
| 4182 | case NFA_ZCLOSE8: |
| 4183 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4184 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4185 | case NFA_NOPEN: |
| 4186 | case NFA_MCLOSE: |
| 4187 | case NFA_MCLOSE1: |
| 4188 | case NFA_MCLOSE2: |
| 4189 | case NFA_MCLOSE3: |
| 4190 | case NFA_MCLOSE4: |
| 4191 | case NFA_MCLOSE5: |
| 4192 | case NFA_MCLOSE6: |
| 4193 | case NFA_MCLOSE7: |
| 4194 | case NFA_MCLOSE8: |
| 4195 | case NFA_MCLOSE9: |
| 4196 | case NFA_NCLOSE: |
| 4197 | return failure_chance(state->out, depth + 1); |
| 4198 | |
| 4199 | case NFA_BACKREF1: |
| 4200 | case NFA_BACKREF2: |
| 4201 | case NFA_BACKREF3: |
| 4202 | case NFA_BACKREF4: |
| 4203 | case NFA_BACKREF5: |
| 4204 | case NFA_BACKREF6: |
| 4205 | case NFA_BACKREF7: |
| 4206 | case NFA_BACKREF8: |
| 4207 | case NFA_BACKREF9: |
| 4208 | #ifdef FEAT_SYN_HL |
| 4209 | case NFA_ZREF1: |
| 4210 | case NFA_ZREF2: |
| 4211 | case NFA_ZREF3: |
| 4212 | case NFA_ZREF4: |
| 4213 | case NFA_ZREF5: |
| 4214 | case NFA_ZREF6: |
| 4215 | case NFA_ZREF7: |
| 4216 | case NFA_ZREF8: |
| 4217 | case NFA_ZREF9: |
| 4218 | #endif |
| 4219 | /* backreferences don't match in many places */ |
| 4220 | return 94; |
| 4221 | |
| 4222 | case NFA_LNUM_GT: |
| 4223 | case NFA_LNUM_LT: |
| 4224 | case NFA_COL_GT: |
| 4225 | case NFA_COL_LT: |
| 4226 | case NFA_VCOL_GT: |
| 4227 | case NFA_VCOL_LT: |
| 4228 | case NFA_MARK_GT: |
| 4229 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4230 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4231 | /* before/after positions don't match very often */ |
| 4232 | return 85; |
| 4233 | |
| 4234 | case NFA_LNUM: |
| 4235 | return 90; |
| 4236 | |
| 4237 | case NFA_CURSOR: |
| 4238 | case NFA_COL: |
| 4239 | case NFA_VCOL: |
| 4240 | case NFA_MARK: |
| 4241 | /* specific positions rarely match */ |
| 4242 | return 98; |
| 4243 | |
| 4244 | case NFA_COMPOSING: |
| 4245 | return 95; |
| 4246 | |
| 4247 | default: |
| 4248 | if (c > 0) |
| 4249 | /* character match fails often */ |
| 4250 | return 95; |
| 4251 | } |
| 4252 | |
| 4253 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4254 | return 50; |
| 4255 | } |
| 4256 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4257 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4258 | * Main matching routine. |
| 4259 | * |
| 4260 | * Run NFA to determine whether it matches reginput. |
| 4261 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4262 | * When "nfa_endp" is not NULL it is a required end-of-match position. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4263 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4264 | * Return TRUE if there is a match, FALSE otherwise. |
| 4265 | * Note: Caller must ensure that: start != NULL. |
| 4266 | */ |
| 4267 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4268 | nfa_regmatch(prog, start, submatch, m) |
| 4269 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4270 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4271 | regsubs_T *submatch; |
| 4272 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4273 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4274 | int result; |
| 4275 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4276 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4277 | int go_to_nextline = FALSE; |
| 4278 | nfa_thread_T *t; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4279 | nfa_list_T list[3]; |
| 4280 | nfa_list_T *listtbl[2][2]; |
| 4281 | nfa_list_T *ll; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4282 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4283 | nfa_list_T *thislist; |
| 4284 | nfa_list_T *nextlist; |
| 4285 | nfa_list_T *neglist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4286 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4287 | nfa_state_T *add_state; |
| 4288 | int add_count; |
| 4289 | int add_off; |
| 4290 | garray_T pimlist; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4291 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4292 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4293 | |
| 4294 | if (debug == NULL) |
| 4295 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4296 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4297 | return FALSE; |
| 4298 | } |
| 4299 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4300 | nfa_match = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4301 | ga_init2(&pimlist, sizeof(nfa_pim_T), 5); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4302 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4303 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4304 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4305 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4306 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4307 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4308 | list[1].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4309 | list[2].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4310 | list[2].len = nstate + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4311 | if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL) |
| 4312 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4313 | |
| 4314 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4315 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4316 | if (log_fd != NULL) |
| 4317 | { |
| 4318 | fprintf(log_fd, "**********************************\n"); |
| 4319 | nfa_set_code(start->c); |
| 4320 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 4321 | abs(start->id), code); |
| 4322 | fprintf(log_fd, "**********************************\n"); |
| 4323 | } |
| 4324 | else |
| 4325 | { |
| 4326 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4327 | log_fd = stderr; |
| 4328 | } |
| 4329 | #endif |
| 4330 | |
| 4331 | thislist = &list[0]; |
| 4332 | thislist->n = 0; |
| 4333 | nextlist = &list[1]; |
| 4334 | nextlist->n = 0; |
| 4335 | neglist = &list[2]; |
| 4336 | neglist->n = 0; |
| 4337 | #ifdef ENABLE_LOG |
| 4338 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 4339 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4340 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4341 | addstate(thislist, start, m, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4342 | |
| 4343 | /* There are two cases when the NFA advances: 1. input char matches the |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 4344 | * NFA node and 2. input char does not match the NFA node and the state |
| 4345 | * has the negated flag. The following macro calls addstate() according to |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4346 | * these rules. It is used A LOT, so use the "listtbl" table for speed */ |
| 4347 | listtbl[0][0] = NULL; |
| 4348 | listtbl[0][1] = neglist; |
| 4349 | listtbl[1][0] = nextlist; |
| 4350 | listtbl[1][1] = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4351 | #define ADD_POS_NEG_STATE(state) \ |
| 4352 | ll = listtbl[result ? 1 : 0][state->negated]; \ |
| 4353 | if (ll != NULL) { \ |
| 4354 | add_state = state->out; \ |
| 4355 | add_off = clen; \ |
| 4356 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4357 | |
| 4358 | /* |
| 4359 | * Run for each character. |
| 4360 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4361 | for (;;) |
| 4362 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4363 | int curc; |
| 4364 | int clen; |
| 4365 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4366 | #ifdef FEAT_MBYTE |
| 4367 | if (has_mbyte) |
| 4368 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4369 | curc = (*mb_ptr2char)(reginput); |
| 4370 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4371 | } |
| 4372 | else |
| 4373 | #endif |
| 4374 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4375 | curc = *reginput; |
| 4376 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4377 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4378 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4379 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4380 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4381 | go_to_nextline = FALSE; |
| 4382 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4383 | |
| 4384 | /* swap lists */ |
| 4385 | thislist = &list[flag]; |
| 4386 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4387 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4388 | listtbl[1][0] = nextlist; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4389 | ++nfa_listid; |
| 4390 | thislist->id = nfa_listid; |
| 4391 | nextlist->id = nfa_listid + 1; |
| 4392 | neglist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4393 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4394 | pimlist.ga_len = 0; |
| 4395 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4396 | #ifdef ENABLE_LOG |
| 4397 | fprintf(log_fd, "------------------------------------------\n"); |
| 4398 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4399 | fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", curc, (int)curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4400 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4401 | { |
| 4402 | int i; |
| 4403 | |
| 4404 | for (i = 0; i < thislist->n; i++) |
| 4405 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 4406 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4407 | fprintf(log_fd, "\n"); |
| 4408 | #endif |
| 4409 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4410 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4411 | fprintf(debug, "\n-------------------\n"); |
| 4412 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 4413 | /* |
| 4414 | * If the state lists are empty we can stop. |
| 4415 | */ |
| 4416 | if (thislist->n == 0 && neglist->n == 0) |
| 4417 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4418 | |
| 4419 | /* compute nextlist */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4420 | for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4421 | { |
| 4422 | if (neglist->n > 0) |
| 4423 | { |
| 4424 | t = &neglist->t[0]; |
Bram Moolenaar | 0fabe3f | 2013-05-21 12:34:17 +0200 | [diff] [blame] | 4425 | neglist->n--; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4426 | listidx--; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 4427 | #ifdef ENABLE_LOG |
| 4428 | fprintf(log_fd, " using neglist entry, %d remaining\n", |
| 4429 | neglist->n); |
| 4430 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4431 | } |
| 4432 | else |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4433 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4434 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4435 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4436 | nfa_set_code(t->state->c); |
| 4437 | fprintf(debug, "%s, ", code); |
| 4438 | #endif |
| 4439 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4440 | { |
| 4441 | int col; |
| 4442 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4443 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4444 | col = -1; |
| 4445 | else if (REG_MULTI) |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4446 | col = t->subs.norm.list.multi[0].start.col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4447 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4448 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4449 | nfa_set_code(t->state->c); |
| 4450 | fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n", |
| 4451 | abs(t->state->id), (int)t->state->c, code, col); |
| 4452 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4453 | #endif |
| 4454 | |
| 4455 | /* |
| 4456 | * Handle the possible codes of the current state. |
| 4457 | * The most important is NFA_MATCH. |
| 4458 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4459 | add_state = NULL; |
| 4460 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4461 | switch (t->state->c) |
| 4462 | { |
| 4463 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 4464 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4465 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4466 | copy_sub(&submatch->norm, &t->subs.norm); |
| 4467 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4468 | if (nfa_has_zsubexpr) |
| 4469 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4470 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4471 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4472 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4473 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4474 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4475 | * states at this position. When the list of states is going |
| 4476 | * to be empty quit without advancing, so that "reginput" is |
| 4477 | * correct. */ |
| 4478 | if (nextlist->n == 0 && neglist->n == 0) |
| 4479 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4480 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 4481 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4482 | |
| 4483 | case NFA_END_INVISIBLE: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4484 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4485 | /* |
| 4486 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4487 | * NFA_START_INVISIBLE_BEFORE node. |
| 4488 | * They surround a zero-width group, used with "\@=", "\&", |
| 4489 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4490 | * If we got here, it means that the current "invisible" group |
| 4491 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4492 | * nfa_regmatch(). For a look-behind match only when it ends |
| 4493 | * in the position in "nfa_endp". |
| 4494 | * Submatches are stored in *m, and used in the parent call. |
| 4495 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4496 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4497 | if (nfa_endp != NULL) |
| 4498 | { |
| 4499 | if (REG_MULTI) |
| 4500 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 4501 | (int)reglnum, |
| 4502 | (int)nfa_endp->se_u.pos.lnum, |
| 4503 | (int)(reginput - regline), |
| 4504 | nfa_endp->se_u.pos.col); |
| 4505 | else |
| 4506 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 4507 | (int)(reginput - regline), |
| 4508 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4509 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4510 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4511 | /* If "nfa_endp" is set it's only a match if it ends at |
| 4512 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4513 | if (nfa_endp != NULL && (REG_MULTI |
| 4514 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 4515 | || (int)(reginput - regline) |
| 4516 | != nfa_endp->se_u.pos.col) |
| 4517 | : reginput != nfa_endp->se_u.ptr)) |
| 4518 | break; |
| 4519 | |
| 4520 | /* do not set submatches for \@! */ |
| 4521 | if (!t->state->negated) |
| 4522 | { |
| 4523 | copy_sub(&m->norm, &t->subs.norm); |
| 4524 | #ifdef FEAT_SYN_HL |
| 4525 | if (nfa_has_zsubexpr) |
| 4526 | copy_sub(&m->synt, &t->subs.synt); |
| 4527 | #endif |
| 4528 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4529 | #ifdef ENABLE_LOG |
| 4530 | fprintf(log_fd, "Match found:\n"); |
| 4531 | log_subsexpr(m); |
| 4532 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4533 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4534 | break; |
| 4535 | |
| 4536 | case NFA_START_INVISIBLE: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4537 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4538 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4539 | nfa_pim_T *pim; |
| 4540 | int cout = t->state->out1->out->c; |
| 4541 | |
| 4542 | /* Do it directly when what follows is possibly end of |
| 4543 | * match (closing paren). |
| 4544 | * Postpone when it is \@<= or \@<!, these are expensive. |
| 4545 | * TODO: remove the check for t->pim and check multiple |
| 4546 | * where it's used? |
| 4547 | * Otherwise first do the one that has the highest chance |
| 4548 | * of failing. */ |
| 4549 | if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9) |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4550 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4551 | || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9) |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4552 | #endif |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4553 | || cout == NFA_NCLOSE |
| 4554 | || t->pim != NULL |
| 4555 | || (t->state->c != NFA_START_INVISIBLE_BEFORE |
| 4556 | && failure_chance(t->state->out1->out, 0) |
| 4557 | < failure_chance(t->state->out, 0))) |
| 4558 | { |
| 4559 | /* |
| 4560 | * First try matching the invisible match, then what |
| 4561 | * follows. |
| 4562 | */ |
| 4563 | result = recursive_regmatch(t->state, prog, |
| 4564 | submatch, m, &listids); |
| 4565 | |
| 4566 | /* for \@! it is a match when result is FALSE */ |
| 4567 | if (result != t->state->negated) |
| 4568 | { |
| 4569 | /* Copy submatch info from the recursive call */ |
| 4570 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4571 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4572 | if (nfa_has_zsubexpr) |
| 4573 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4574 | #endif |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4575 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4576 | /* t->state->out1 is the corresponding |
| 4577 | * END_INVISIBLE node; Add its out to the current |
| 4578 | * list (zero-width match). */ |
| 4579 | addstate_here(thislist, t->state->out1->out, |
| 4580 | &t->subs, t->pim, &listidx); |
| 4581 | } |
| 4582 | } |
| 4583 | else |
| 4584 | { |
| 4585 | /* |
| 4586 | * First try matching what follows at the current |
| 4587 | * position. Only if a match is found, addstate() is |
| 4588 | * called, then verify the invisible match matches. |
| 4589 | * Add a nfa_pim_T to the following states, it |
| 4590 | * contains info about the invisible match. |
| 4591 | */ |
| 4592 | if (ga_grow(&pimlist, 1) == FAIL) |
| 4593 | goto theend; |
| 4594 | pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len; |
| 4595 | ++pimlist.ga_len; |
| 4596 | pim->state = t->state; |
| 4597 | pim->pim = NULL; |
| 4598 | pim->result = NFA_PIM_TODO; |
| 4599 | |
| 4600 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 4601 | * node; Add its out to the current list (zero-width |
| 4602 | * match). */ |
| 4603 | addstate_here(thislist, t->state->out1->out, &t->subs, |
| 4604 | pim, &listidx); |
| 4605 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4606 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4607 | break; |
| 4608 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4609 | case NFA_START_PATTERN: |
| 4610 | /* First try matching the pattern. */ |
| 4611 | result = recursive_regmatch(t->state, prog, |
| 4612 | submatch, m, &listids); |
| 4613 | if (result) |
| 4614 | { |
| 4615 | int bytelen; |
| 4616 | |
| 4617 | #ifdef ENABLE_LOG |
| 4618 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 4619 | log_subsexpr(m); |
| 4620 | #endif |
| 4621 | /* Copy submatch info from the recursive call */ |
| 4622 | copy_sub_off(&t->subs.norm, &m->norm); |
| 4623 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4624 | if (nfa_has_zsubexpr) |
| 4625 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4626 | #endif |
| 4627 | /* Now we need to skip over the matched text and then |
| 4628 | * continue with what follows. */ |
| 4629 | if (REG_MULTI) |
| 4630 | /* TODO: multi-line match */ |
| 4631 | bytelen = m->norm.list.multi[0].end.col |
| 4632 | - (int)(reginput - regline); |
| 4633 | else |
| 4634 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 4635 | |
| 4636 | #ifdef ENABLE_LOG |
| 4637 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 4638 | #endif |
| 4639 | if (bytelen == 0) |
| 4640 | { |
| 4641 | /* empty match, output of corresponding |
| 4642 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 4643 | * position */ |
| 4644 | addstate_here(thislist, t->state->out1->out->out, |
| 4645 | &t->subs, t->pim, &listidx); |
| 4646 | } |
| 4647 | else if (bytelen <= clen) |
| 4648 | { |
| 4649 | /* match current character, output of corresponding |
| 4650 | * NFA_END_PATTERN to be used at next position. */ |
| 4651 | ll = nextlist; |
| 4652 | add_state = t->state->out1->out->out; |
| 4653 | add_off = clen; |
| 4654 | } |
| 4655 | else |
| 4656 | { |
| 4657 | /* skip over the matched characters, set character |
| 4658 | * count in NFA_SKIP */ |
| 4659 | ll = nextlist; |
| 4660 | add_state = t->state->out1->out; |
| 4661 | add_off = bytelen; |
| 4662 | add_count = bytelen - clen; |
| 4663 | } |
| 4664 | } |
| 4665 | break; |
| 4666 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4667 | case NFA_BOL: |
| 4668 | if (reginput == regline) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4669 | addstate_here(thislist, t->state->out, &t->subs, |
| 4670 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4671 | break; |
| 4672 | |
| 4673 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4674 | if (curc == NUL) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4675 | addstate_here(thislist, t->state->out, &t->subs, |
| 4676 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4677 | break; |
| 4678 | |
| 4679 | case NFA_BOW: |
| 4680 | { |
| 4681 | int bow = TRUE; |
| 4682 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4683 | if (curc == NUL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4684 | bow = FALSE; |
| 4685 | #ifdef FEAT_MBYTE |
| 4686 | else if (has_mbyte) |
| 4687 | { |
| 4688 | int this_class; |
| 4689 | |
| 4690 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4691 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4692 | if (this_class <= 1) |
| 4693 | bow = FALSE; |
| 4694 | else if (reg_prev_class() == this_class) |
| 4695 | bow = FALSE; |
| 4696 | } |
| 4697 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4698 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4699 | || (reginput > regline |
| 4700 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4701 | bow = FALSE; |
| 4702 | if (bow) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4703 | addstate_here(thislist, t->state->out, &t->subs, |
| 4704 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4705 | break; |
| 4706 | } |
| 4707 | |
| 4708 | case NFA_EOW: |
| 4709 | { |
| 4710 | int eow = TRUE; |
| 4711 | |
| 4712 | if (reginput == regline) |
| 4713 | eow = FALSE; |
| 4714 | #ifdef FEAT_MBYTE |
| 4715 | else if (has_mbyte) |
| 4716 | { |
| 4717 | int this_class, prev_class; |
| 4718 | |
| 4719 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4720 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4721 | prev_class = reg_prev_class(); |
| 4722 | if (this_class == prev_class |
| 4723 | || prev_class == 0 || prev_class == 1) |
| 4724 | eow = FALSE; |
| 4725 | } |
| 4726 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4727 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4728 | || (reginput[0] != NUL |
| 4729 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4730 | eow = FALSE; |
| 4731 | if (eow) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4732 | addstate_here(thislist, t->state->out, &t->subs, |
| 4733 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4734 | break; |
| 4735 | } |
| 4736 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 4737 | case NFA_BOF: |
| 4738 | if (reglnum == 0 && reginput == regline |
| 4739 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4740 | addstate_here(thislist, t->state->out, &t->subs, |
| 4741 | t->pim, &listidx); |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 4742 | break; |
| 4743 | |
| 4744 | case NFA_EOF: |
| 4745 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4746 | addstate_here(thislist, t->state->out, &t->subs, |
| 4747 | t->pim, &listidx); |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 4748 | break; |
| 4749 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4750 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4751 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4752 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4753 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4754 | int len = 0; |
| 4755 | nfa_state_T *end; |
| 4756 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4757 | int cchars[MAX_MCO]; |
| 4758 | int ccount = 0; |
| 4759 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4760 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4761 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4762 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 4763 | if (utf_iscomposing(sta->c)) |
| 4764 | { |
| 4765 | /* Only match composing character(s), ignore base |
| 4766 | * character. Used for ".{composing}" and "{composing}" |
| 4767 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4768 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 4769 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 4770 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4771 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 4772 | /* If \Z was present, then ignore composing characters. |
| 4773 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4774 | /* TODO: How about negated? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4775 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4776 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4777 | else |
| 4778 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4779 | while (sta->c != NFA_END_COMPOSING) |
| 4780 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4781 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4782 | |
| 4783 | /* Check base character matches first, unless ignored. */ |
| 4784 | else if (len > 0 || mc == sta->c) |
| 4785 | { |
| 4786 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4787 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4788 | len += mb_char2len(mc); |
| 4789 | sta = sta->out; |
| 4790 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4791 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4792 | /* We don't care about the order of composing characters. |
| 4793 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4794 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4795 | { |
| 4796 | mc = mb_ptr2char(reginput + len); |
| 4797 | cchars[ccount++] = mc; |
| 4798 | len += mb_char2len(mc); |
| 4799 | if (ccount == MAX_MCO) |
| 4800 | break; |
| 4801 | } |
| 4802 | |
| 4803 | /* Check that each composing char in the pattern matches a |
| 4804 | * composing char in the text. We do not check if all |
| 4805 | * composing chars are matched. */ |
| 4806 | result = OK; |
| 4807 | while (sta->c != NFA_END_COMPOSING) |
| 4808 | { |
| 4809 | for (j = 0; j < ccount; ++j) |
| 4810 | if (cchars[j] == sta->c) |
| 4811 | break; |
| 4812 | if (j == ccount) |
| 4813 | { |
| 4814 | result = FAIL; |
| 4815 | break; |
| 4816 | } |
| 4817 | sta = sta->out; |
| 4818 | } |
| 4819 | } |
| 4820 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 4821 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4822 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4823 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4824 | ADD_POS_NEG_STATE(end); |
| 4825 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4826 | } |
| 4827 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4828 | |
| 4829 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4830 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 4831 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4832 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4833 | go_to_nextline = TRUE; |
| 4834 | /* Pass -1 for the offset, which means taking the position |
| 4835 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4836 | ll = nextlist; |
| 4837 | add_state = t->state->out; |
| 4838 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4839 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4840 | else if (curc == '\n' && reg_line_lbr) |
| 4841 | { |
| 4842 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4843 | ll = nextlist; |
| 4844 | add_state = t->state->out; |
| 4845 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4846 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4847 | break; |
| 4848 | |
| 4849 | case NFA_CLASS_ALNUM: |
| 4850 | case NFA_CLASS_ALPHA: |
| 4851 | case NFA_CLASS_BLANK: |
| 4852 | case NFA_CLASS_CNTRL: |
| 4853 | case NFA_CLASS_DIGIT: |
| 4854 | case NFA_CLASS_GRAPH: |
| 4855 | case NFA_CLASS_LOWER: |
| 4856 | case NFA_CLASS_PRINT: |
| 4857 | case NFA_CLASS_PUNCT: |
| 4858 | case NFA_CLASS_SPACE: |
| 4859 | case NFA_CLASS_UPPER: |
| 4860 | case NFA_CLASS_XDIGIT: |
| 4861 | case NFA_CLASS_TAB: |
| 4862 | case NFA_CLASS_RETURN: |
| 4863 | case NFA_CLASS_BACKSPACE: |
| 4864 | case NFA_CLASS_ESCAPE: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4865 | result = check_char_class(t->state->c, curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4866 | ADD_POS_NEG_STATE(t->state); |
| 4867 | break; |
| 4868 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 4869 | case NFA_START_COLL: |
| 4870 | case NFA_START_NEG_COLL: |
| 4871 | { |
| 4872 | /* What follows is a list of characters, until NFA_END_COLL. |
| 4873 | * One of them must match or none of them must match. */ |
| 4874 | nfa_state_T *state; |
| 4875 | int result_if_matched; |
| 4876 | int c1, c2; |
| 4877 | |
| 4878 | /* Never match EOL. If it's part of the collection it is added |
| 4879 | * as a separate state with an OR. */ |
| 4880 | if (curc == NUL) |
| 4881 | break; |
| 4882 | |
| 4883 | state = t->state->out; |
| 4884 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 4885 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4886 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 4887 | if (state->c == NFA_END_COLL) |
| 4888 | { |
| 4889 | result = !result_if_matched; |
| 4890 | break; |
| 4891 | } |
| 4892 | if (state->c == NFA_RANGE_MIN) |
| 4893 | { |
| 4894 | c1 = state->val; |
| 4895 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 4896 | c2 = state->val; |
| 4897 | #ifdef ENABLE_LOG |
| 4898 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 4899 | curc, c1, c2); |
| 4900 | #endif |
| 4901 | if (curc >= c1 && curc <= c2) |
| 4902 | { |
| 4903 | result = result_if_matched; |
| 4904 | break; |
| 4905 | } |
| 4906 | if (ireg_ic) |
| 4907 | { |
| 4908 | int curc_low = MB_TOLOWER(curc); |
| 4909 | int done = FALSE; |
| 4910 | |
| 4911 | for ( ; c1 <= c2; ++c1) |
| 4912 | if (MB_TOLOWER(c1) == curc_low) |
| 4913 | { |
| 4914 | result = result_if_matched; |
| 4915 | done = TRUE; |
| 4916 | break; |
| 4917 | } |
| 4918 | if (done) |
| 4919 | break; |
| 4920 | } |
| 4921 | } |
| 4922 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 4923 | : (curc == state->c |
| 4924 | || (ireg_ic && MB_TOLOWER(curc) |
| 4925 | == MB_TOLOWER(state->c)))) |
| 4926 | { |
| 4927 | result = result_if_matched; |
| 4928 | break; |
| 4929 | } |
| 4930 | state = state->out; |
| 4931 | } |
| 4932 | if (result) |
| 4933 | { |
| 4934 | /* next state is in out of the NFA_END_COLL, out1 of |
| 4935 | * START points to the END state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4936 | ll = nextlist; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 4937 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4938 | add_off = clen; |
| 4939 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4940 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame^] | 4941 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4942 | |
| 4943 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4944 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4945 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4946 | { |
| 4947 | ll = nextlist; |
| 4948 | add_state = t->state->out; |
| 4949 | add_off = clen; |
| 4950 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4951 | break; |
| 4952 | |
| 4953 | /* |
| 4954 | * Character classes like \a for alpha, \d for digit etc. |
| 4955 | */ |
| 4956 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4957 | result = vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4958 | ADD_POS_NEG_STATE(t->state); |
| 4959 | break; |
| 4960 | |
| 4961 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4962 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4963 | ADD_POS_NEG_STATE(t->state); |
| 4964 | break; |
| 4965 | |
| 4966 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4967 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4968 | ADD_POS_NEG_STATE(t->state); |
| 4969 | break; |
| 4970 | |
| 4971 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4972 | result = !VIM_ISDIGIT(curc) |
| 4973 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4974 | ADD_POS_NEG_STATE(t->state); |
| 4975 | break; |
| 4976 | |
| 4977 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4978 | result = vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4979 | ADD_POS_NEG_STATE(t->state); |
| 4980 | break; |
| 4981 | |
| 4982 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4983 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4984 | ADD_POS_NEG_STATE(t->state); |
| 4985 | break; |
| 4986 | |
| 4987 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | 0805049 | 2013-05-21 12:43:56 +0200 | [diff] [blame] | 4988 | result = ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4989 | ADD_POS_NEG_STATE(t->state); |
| 4990 | break; |
| 4991 | |
| 4992 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4993 | result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4994 | ADD_POS_NEG_STATE(t->state); |
| 4995 | break; |
| 4996 | |
| 4997 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4998 | result = vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4999 | ADD_POS_NEG_STATE(t->state); |
| 5000 | break; |
| 5001 | |
| 5002 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5003 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5004 | ADD_POS_NEG_STATE(t->state); |
| 5005 | break; |
| 5006 | |
| 5007 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5008 | result = ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5009 | ADD_POS_NEG_STATE(t->state); |
| 5010 | break; |
| 5011 | |
| 5012 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5013 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5014 | ADD_POS_NEG_STATE(t->state); |
| 5015 | break; |
| 5016 | |
| 5017 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5018 | result = ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5019 | ADD_POS_NEG_STATE(t->state); |
| 5020 | break; |
| 5021 | |
| 5022 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5023 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5024 | ADD_POS_NEG_STATE(t->state); |
| 5025 | break; |
| 5026 | |
| 5027 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5028 | result = ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5029 | ADD_POS_NEG_STATE(t->state); |
| 5030 | break; |
| 5031 | |
| 5032 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5033 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5034 | ADD_POS_NEG_STATE(t->state); |
| 5035 | break; |
| 5036 | |
| 5037 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5038 | result = ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5039 | ADD_POS_NEG_STATE(t->state); |
| 5040 | break; |
| 5041 | |
| 5042 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5043 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5044 | ADD_POS_NEG_STATE(t->state); |
| 5045 | break; |
| 5046 | |
| 5047 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5048 | result = ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5049 | ADD_POS_NEG_STATE(t->state); |
| 5050 | break; |
| 5051 | |
| 5052 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5053 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5054 | ADD_POS_NEG_STATE(t->state); |
| 5055 | break; |
| 5056 | |
| 5057 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5058 | result = ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5059 | ADD_POS_NEG_STATE(t->state); |
| 5060 | break; |
| 5061 | |
| 5062 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5063 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5064 | ADD_POS_NEG_STATE(t->state); |
| 5065 | break; |
| 5066 | |
| 5067 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5068 | result = ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5069 | ADD_POS_NEG_STATE(t->state); |
| 5070 | break; |
| 5071 | |
| 5072 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5073 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5074 | ADD_POS_NEG_STATE(t->state); |
| 5075 | break; |
| 5076 | |
| 5077 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5078 | result = ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5079 | ADD_POS_NEG_STATE(t->state); |
| 5080 | break; |
| 5081 | |
| 5082 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5083 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5084 | ADD_POS_NEG_STATE(t->state); |
| 5085 | break; |
| 5086 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5087 | case NFA_BACKREF1: |
| 5088 | case NFA_BACKREF2: |
| 5089 | case NFA_BACKREF3: |
| 5090 | case NFA_BACKREF4: |
| 5091 | case NFA_BACKREF5: |
| 5092 | case NFA_BACKREF6: |
| 5093 | case NFA_BACKREF7: |
| 5094 | case NFA_BACKREF8: |
| 5095 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5096 | #ifdef FEAT_SYN_HL |
| 5097 | case NFA_ZREF1: |
| 5098 | case NFA_ZREF2: |
| 5099 | case NFA_ZREF3: |
| 5100 | case NFA_ZREF4: |
| 5101 | case NFA_ZREF5: |
| 5102 | case NFA_ZREF6: |
| 5103 | case NFA_ZREF7: |
| 5104 | case NFA_ZREF8: |
| 5105 | case NFA_ZREF9: |
| 5106 | #endif |
| 5107 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5108 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5109 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5110 | int bytelen; |
| 5111 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5112 | if (t->state->c <= NFA_BACKREF9) |
| 5113 | { |
| 5114 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 5115 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 5116 | } |
| 5117 | #ifdef FEAT_SYN_HL |
| 5118 | else |
| 5119 | { |
| 5120 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 5121 | result = match_zref(subidx, &bytelen); |
| 5122 | } |
| 5123 | #endif |
| 5124 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5125 | if (result) |
| 5126 | { |
| 5127 | if (bytelen == 0) |
| 5128 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 5129 | /* empty match always works, output of NFA_SKIP to be |
| 5130 | * used next */ |
| 5131 | addstate_here(thislist, t->state->out->out, &t->subs, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5132 | t->pim, &listidx); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5133 | } |
| 5134 | else if (bytelen <= clen) |
| 5135 | { |
| 5136 | /* match current character, jump ahead to out of |
| 5137 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5138 | ll = nextlist; |
| 5139 | add_state = t->state->out->out; |
| 5140 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5141 | } |
| 5142 | else |
| 5143 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 5144 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5145 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5146 | ll = nextlist; |
| 5147 | add_state = t->state->out; |
| 5148 | add_off = bytelen; |
| 5149 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5150 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5151 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5152 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5153 | } |
| 5154 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 5155 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5156 | if (t->count - clen <= 0) |
| 5157 | { |
| 5158 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5159 | ll = nextlist; |
| 5160 | add_state = t->state->out; |
| 5161 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5162 | } |
| 5163 | else |
| 5164 | { |
| 5165 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5166 | ll = nextlist; |
| 5167 | add_state = t->state; |
| 5168 | add_off = 0; |
| 5169 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5170 | } |
| 5171 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5172 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5173 | case NFA_LNUM: |
| 5174 | case NFA_LNUM_GT: |
| 5175 | case NFA_LNUM_LT: |
| 5176 | result = (REG_MULTI && |
| 5177 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 5178 | (long_u)(reglnum + reg_firstlnum))); |
| 5179 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5180 | addstate_here(thislist, t->state->out, &t->subs, |
| 5181 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5182 | break; |
| 5183 | |
| 5184 | case NFA_COL: |
| 5185 | case NFA_COL_GT: |
| 5186 | case NFA_COL_LT: |
| 5187 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 5188 | (long_u)(reginput - regline) + 1); |
| 5189 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5190 | addstate_here(thislist, t->state->out, &t->subs, |
| 5191 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5192 | break; |
| 5193 | |
| 5194 | case NFA_VCOL: |
| 5195 | case NFA_VCOL_GT: |
| 5196 | case NFA_VCOL_LT: |
| 5197 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 5198 | (long_u)win_linetabsize( |
| 5199 | reg_win == NULL ? curwin : reg_win, |
| 5200 | regline, (colnr_T)(reginput - regline)) + 1); |
| 5201 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5202 | addstate_here(thislist, t->state->out, &t->subs, |
| 5203 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5204 | break; |
| 5205 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 5206 | case NFA_MARK: |
| 5207 | case NFA_MARK_GT: |
| 5208 | case NFA_MARK_LT: |
| 5209 | { |
| 5210 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 5211 | |
| 5212 | /* Compare the mark position to the match position. */ |
| 5213 | result = (pos != NULL /* mark doesn't exist */ |
| 5214 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 5215 | && (pos->lnum == reglnum + reg_firstlnum |
| 5216 | ? (pos->col == (colnr_T)(reginput - regline) |
| 5217 | ? t->state->c == NFA_MARK |
| 5218 | : (pos->col < (colnr_T)(reginput - regline) |
| 5219 | ? t->state->c == NFA_MARK_GT |
| 5220 | : t->state->c == NFA_MARK_LT)) |
| 5221 | : (pos->lnum < reglnum + reg_firstlnum |
| 5222 | ? t->state->c == NFA_MARK_GT |
| 5223 | : t->state->c == NFA_MARK_LT))); |
| 5224 | if (result) |
| 5225 | addstate_here(thislist, t->state->out, &t->subs, |
| 5226 | t->pim, &listidx); |
| 5227 | break; |
| 5228 | } |
| 5229 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5230 | case NFA_CURSOR: |
| 5231 | result = (reg_win != NULL |
| 5232 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 5233 | && ((colnr_T)(reginput - regline) |
| 5234 | == reg_win->w_cursor.col)); |
| 5235 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5236 | addstate_here(thislist, t->state->out, &t->subs, |
| 5237 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5238 | break; |
| 5239 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5240 | case NFA_VISUAL: |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5241 | #ifdef FEAT_VISUAL |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5242 | result = reg_match_visual(); |
| 5243 | if (result) |
| 5244 | addstate_here(thislist, t->state->out, &t->subs, |
| 5245 | t->pim, &listidx); |
Bram Moolenaar | 78eae9a | 2013-06-05 11:02:05 +0200 | [diff] [blame] | 5246 | #endif |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5247 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5248 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5249 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5250 | { |
| 5251 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5252 | |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5253 | /* TODO: put this in #ifdef later */ |
| 5254 | if (c < -256) |
| 5255 | EMSGN("INTERNAL: Negative state char: %ld", c); |
| 5256 | if (is_Magic(c)) |
| 5257 | c = un_Magic(c); |
| 5258 | result = (c == curc); |
| 5259 | |
| 5260 | if (!result && ireg_ic) |
| 5261 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5262 | #ifdef FEAT_MBYTE |
| 5263 | /* If there is a composing character which is not being |
| 5264 | * ignored there can be no match. Match with composing |
| 5265 | * character uses NFA_COMPOSING above. */ |
| 5266 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5267 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5268 | result = FALSE; |
| 5269 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5270 | ADD_POS_NEG_STATE(t->state); |
| 5271 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5272 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5273 | |
| 5274 | } /* switch (t->state->c) */ |
| 5275 | |
| 5276 | if (add_state != NULL) |
| 5277 | { |
| 5278 | if (t->pim != NULL) |
| 5279 | { |
| 5280 | /* postponed invisible match */ |
| 5281 | /* TODO: also do t->pim->pim recursively? */ |
| 5282 | if (t->pim->result == NFA_PIM_TODO) |
| 5283 | { |
| 5284 | #ifdef ENABLE_LOG |
| 5285 | fprintf(log_fd, "\n"); |
| 5286 | fprintf(log_fd, "==================================\n"); |
| 5287 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 5288 | fprintf(log_fd, "\n"); |
| 5289 | #endif |
| 5290 | result = recursive_regmatch(t->pim->state, |
| 5291 | prog, submatch, m, &listids); |
| 5292 | t->pim->result = result ? NFA_PIM_MATCH |
| 5293 | : NFA_PIM_NOMATCH; |
| 5294 | /* for \@! it is a match when result is FALSE */ |
| 5295 | if (result != t->pim->state->negated) |
| 5296 | { |
| 5297 | /* Copy submatch info from the recursive call */ |
| 5298 | copy_sub_off(&t->pim->subs.norm, &m->norm); |
| 5299 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5300 | if (nfa_has_zsubexpr) |
| 5301 | copy_sub_off(&t->pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5302 | #endif |
| 5303 | } |
| 5304 | } |
| 5305 | else |
| 5306 | { |
| 5307 | result = (t->pim->result == NFA_PIM_MATCH); |
| 5308 | #ifdef ENABLE_LOG |
| 5309 | fprintf(log_fd, "\n"); |
| 5310 | fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result); |
| 5311 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5312 | fprintf(log_fd, "\n"); |
| 5313 | #endif |
| 5314 | } |
| 5315 | |
| 5316 | /* for \@! it is a match when result is FALSE */ |
| 5317 | if (result != t->pim->state->negated) |
| 5318 | { |
| 5319 | /* Copy submatch info from the recursive call */ |
| 5320 | copy_sub_off(&t->subs.norm, &t->pim->subs.norm); |
| 5321 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5322 | if (nfa_has_zsubexpr) |
| 5323 | copy_sub_off(&t->subs.synt, &t->pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5324 | #endif |
| 5325 | } |
| 5326 | else |
| 5327 | /* look-behind match failed, don't add the state */ |
| 5328 | continue; |
| 5329 | } |
| 5330 | |
| 5331 | addstate(ll, add_state, &t->subs, add_off); |
| 5332 | if (add_count > 0) |
| 5333 | nextlist->t[ll->n - 1].count = add_count; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5334 | } |
| 5335 | |
| 5336 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 5337 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 5338 | /* Look for the start of a match in the current position by adding the |
| 5339 | * start state to the list of states. |
| 5340 | * The first found match is the leftmost one, thus the order of states |
| 5341 | * matters! |
| 5342 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 5343 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5344 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 5345 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5346 | if (nfa_match == FALSE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5347 | && ((start->c == NFA_MOPEN |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5348 | && reglnum == 0 |
| 5349 | && clen != 0 |
| 5350 | && (ireg_maxcol == 0 |
| 5351 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5352 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5353 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5354 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 5355 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5356 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5357 | < nfa_endp->se_u.pos.col)) |
| 5358 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5359 | { |
| 5360 | #ifdef ENABLE_LOG |
| 5361 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 5362 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5363 | addstate(nextlist, start, m, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5364 | } |
| 5365 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5366 | #ifdef ENABLE_LOG |
| 5367 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5368 | { |
| 5369 | int i; |
| 5370 | |
| 5371 | for (i = 0; i < thislist->n; i++) |
| 5372 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5373 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5374 | fprintf(log_fd, "\n"); |
| 5375 | #endif |
| 5376 | |
| 5377 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5378 | /* Advance to the next character, or advance to the next line, or |
| 5379 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5380 | if (clen != 0) |
| 5381 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5382 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 5383 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5384 | reg_nextline(); |
| 5385 | else |
| 5386 | break; |
| 5387 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5388 | |
| 5389 | #ifdef ENABLE_LOG |
| 5390 | if (log_fd != stderr) |
| 5391 | fclose(log_fd); |
| 5392 | log_fd = NULL; |
| 5393 | #endif |
| 5394 | |
| 5395 | theend: |
| 5396 | /* Free memory */ |
| 5397 | vim_free(list[0].t); |
| 5398 | vim_free(list[1].t); |
| 5399 | vim_free(list[2].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5400 | vim_free(listids); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5401 | ga_clear(&pimlist); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5402 | #undef ADD_POS_NEG_STATE |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5403 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5404 | fclose(debug); |
| 5405 | #endif |
| 5406 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5407 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5408 | } |
| 5409 | |
| 5410 | /* |
| 5411 | * Try match of "prog" with at regline["col"]. |
| 5412 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 5413 | */ |
| 5414 | static long |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5415 | nfa_regtry(prog, col) |
| 5416 | nfa_regprog_T *prog; |
| 5417 | colnr_T col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5418 | { |
| 5419 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5420 | regsubs_T subs, m; |
| 5421 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5422 | #ifdef ENABLE_LOG |
| 5423 | FILE *f; |
| 5424 | #endif |
| 5425 | |
| 5426 | reginput = regline + col; |
| 5427 | need_clear_subexpr = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5428 | #ifdef FEAT_SYN_HL |
| 5429 | /* Clear the external match subpointers if necessary. */ |
| 5430 | if (prog->reghasz == REX_SET) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5431 | { |
| 5432 | nfa_has_zsubexpr = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5433 | need_clear_zsubexpr = TRUE; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5434 | } |
| 5435 | else |
| 5436 | nfa_has_zsubexpr = FALSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5437 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5438 | |
| 5439 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5440 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5441 | if (f != NULL) |
| 5442 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5443 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5444 | #ifdef DEBUG |
| 5445 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 5446 | #endif |
| 5447 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5448 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 5449 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5450 | fprintf(f, "\n\n"); |
| 5451 | fclose(f); |
| 5452 | } |
| 5453 | else |
| 5454 | EMSG(_("Could not open temporary log file for writing ")); |
| 5455 | #endif |
| 5456 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5457 | clear_sub(&subs.norm); |
| 5458 | clear_sub(&m.norm); |
| 5459 | #ifdef FEAT_SYN_HL |
| 5460 | clear_sub(&subs.synt); |
| 5461 | clear_sub(&m.synt); |
| 5462 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5463 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5464 | if (nfa_regmatch(prog, start, &subs, &m) == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5465 | return 0; |
| 5466 | |
| 5467 | cleanup_subexpr(); |
| 5468 | if (REG_MULTI) |
| 5469 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5470 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5471 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5472 | reg_startpos[i] = subs.norm.list.multi[i].start; |
| 5473 | reg_endpos[i] = subs.norm.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | if (reg_startpos[0].lnum < 0) |
| 5477 | { |
| 5478 | reg_startpos[0].lnum = 0; |
| 5479 | reg_startpos[0].col = col; |
| 5480 | } |
| 5481 | if (reg_endpos[0].lnum < 0) |
| 5482 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 5483 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5484 | reg_endpos[0].lnum = reglnum; |
| 5485 | reg_endpos[0].col = (int)(reginput - regline); |
| 5486 | } |
| 5487 | else |
| 5488 | /* Use line number of "\ze". */ |
| 5489 | reglnum = reg_endpos[0].lnum; |
| 5490 | } |
| 5491 | else |
| 5492 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5493 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5494 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5495 | reg_startp[i] = subs.norm.list.line[i].start; |
| 5496 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5497 | } |
| 5498 | |
| 5499 | if (reg_startp[0] == NULL) |
| 5500 | reg_startp[0] = regline + col; |
| 5501 | if (reg_endp[0] == NULL) |
| 5502 | reg_endp[0] = reginput; |
| 5503 | } |
| 5504 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5505 | #ifdef FEAT_SYN_HL |
| 5506 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 5507 | unref_extmatch(re_extmatch_out); |
| 5508 | re_extmatch_out = NULL; |
| 5509 | |
| 5510 | if (prog->reghasz == REX_SET) |
| 5511 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5512 | cleanup_zsubexpr(); |
| 5513 | re_extmatch_out = make_extmatch(); |
| 5514 | for (i = 0; i < subs.synt.in_use; i++) |
| 5515 | { |
| 5516 | if (REG_MULTI) |
| 5517 | { |
| 5518 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 5519 | |
| 5520 | /* Only accept single line matches. */ |
| 5521 | if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum) |
| 5522 | re_extmatch_out->matches[i] = |
| 5523 | vim_strnsave(reg_getline(mpos->start.lnum) |
| 5524 | + mpos->start.col, |
| 5525 | mpos->end.col - mpos->start.col); |
| 5526 | } |
| 5527 | else |
| 5528 | { |
| 5529 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 5530 | |
| 5531 | if (lpos->start != NULL && lpos->end != NULL) |
| 5532 | re_extmatch_out->matches[i] = |
| 5533 | vim_strnsave(lpos->start, |
| 5534 | (int)(lpos->end - lpos->start)); |
| 5535 | } |
| 5536 | } |
| 5537 | } |
| 5538 | #endif |
| 5539 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5540 | return 1 + reglnum; |
| 5541 | } |
| 5542 | |
| 5543 | /* |
| 5544 | * Match a regexp against a string ("line" points to the string) or multiple |
| 5545 | * lines ("line" is NULL, use reg_getline()). |
| 5546 | * |
| 5547 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 5548 | */ |
| 5549 | static long |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 5550 | nfa_regexec_both(line, startcol) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5551 | char_u *line; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 5552 | colnr_T startcol; /* column to start looking for match */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5553 | { |
| 5554 | nfa_regprog_T *prog; |
| 5555 | long retval = 0L; |
| 5556 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 5557 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5558 | |
| 5559 | if (REG_MULTI) |
| 5560 | { |
| 5561 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 5562 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 5563 | reg_startpos = reg_mmatch->startpos; |
| 5564 | reg_endpos = reg_mmatch->endpos; |
| 5565 | } |
| 5566 | else |
| 5567 | { |
| 5568 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 5569 | reg_startp = reg_match->startp; |
| 5570 | reg_endp = reg_match->endp; |
| 5571 | } |
| 5572 | |
| 5573 | /* Be paranoid... */ |
| 5574 | if (prog == NULL || line == NULL) |
| 5575 | { |
| 5576 | EMSG(_(e_null)); |
| 5577 | goto theend; |
| 5578 | } |
| 5579 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5580 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 5581 | if (prog->regflags & RF_ICASE) |
| 5582 | ireg_ic = TRUE; |
| 5583 | else if (prog->regflags & RF_NOICASE) |
| 5584 | ireg_ic = FALSE; |
| 5585 | |
| 5586 | #ifdef FEAT_MBYTE |
| 5587 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 5588 | if (prog->regflags & RF_ICOMBINE) |
| 5589 | ireg_icombine = TRUE; |
| 5590 | #endif |
| 5591 | |
| 5592 | regline = line; |
| 5593 | reglnum = 0; /* relative to line */ |
| 5594 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5595 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5596 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5597 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5598 | nfa_listid = 1; |
| 5599 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5600 | #ifdef DEBUG |
| 5601 | nfa_regengine.expr = prog->pattern; |
| 5602 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5603 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 5604 | if (prog->reganch && col > 0) |
| 5605 | return 0L; |
| 5606 | |
| 5607 | if (prog->regstart != NUL) |
| 5608 | { |
| 5609 | char_u *s; |
| 5610 | |
| 5611 | /* Skip until the char we know it must start with. |
| 5612 | * Used often, do some work to avoid call overhead. */ |
| 5613 | if (!ireg_ic |
| 5614 | #ifdef FEAT_MBYTE |
| 5615 | && !has_mbyte |
| 5616 | #endif |
| 5617 | ) |
| 5618 | s = vim_strbyte(regline + col, prog->regstart); |
| 5619 | else |
| 5620 | s = cstrchr(regline + col, prog->regstart); |
| 5621 | if (s == NULL) |
| 5622 | return 0L; |
| 5623 | col = (int)(s - regline); |
| 5624 | } |
| 5625 | |
| 5626 | /* If the start column is past the maximum column: no need to try. */ |
| 5627 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 5628 | goto theend; |
| 5629 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5630 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5631 | for (i = 0; i < nstate; ++i) |
| 5632 | { |
| 5633 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5634 | prog->state[i].lastlist[0] = 0; |
| 5635 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5636 | } |
| 5637 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5638 | retval = nfa_regtry(prog, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5639 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5640 | #ifdef DEBUG |
| 5641 | nfa_regengine.expr = NULL; |
| 5642 | #endif |
| 5643 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5644 | theend: |
| 5645 | return retval; |
| 5646 | } |
| 5647 | |
| 5648 | /* |
| 5649 | * Compile a regular expression into internal code for the NFA matcher. |
| 5650 | * Returns the program in allocated space. Returns NULL for an error. |
| 5651 | */ |
| 5652 | static regprog_T * |
| 5653 | nfa_regcomp(expr, re_flags) |
| 5654 | char_u *expr; |
| 5655 | int re_flags; |
| 5656 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 5657 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 5658 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5659 | int *postfix; |
| 5660 | |
| 5661 | if (expr == NULL) |
| 5662 | return NULL; |
| 5663 | |
| 5664 | #ifdef DEBUG |
| 5665 | nfa_regengine.expr = expr; |
| 5666 | #endif |
| 5667 | |
| 5668 | init_class_tab(); |
| 5669 | |
| 5670 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 5671 | return NULL; |
| 5672 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5673 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 5674 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5675 | postfix = re2post(); |
| 5676 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5677 | { |
| 5678 | /* TODO: only give this error for debugging? */ |
| 5679 | if (post_ptr >= post_end) |
| 5680 | EMSGN("Internal error: estimated max number of states insufficient: %ld", post_end - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5681 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5682 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5683 | |
| 5684 | /* |
| 5685 | * In order to build the NFA, we parse the input regexp twice: |
| 5686 | * 1. first pass to count size (so we can allocate space) |
| 5687 | * 2. second to emit code |
| 5688 | */ |
| 5689 | #ifdef ENABLE_LOG |
| 5690 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5691 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5692 | |
| 5693 | if (f != NULL) |
| 5694 | { |
| 5695 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 5696 | fclose(f); |
| 5697 | } |
| 5698 | } |
| 5699 | #endif |
| 5700 | |
| 5701 | /* |
| 5702 | * PASS 1 |
| 5703 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 5704 | */ |
| 5705 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 5706 | |
| 5707 | /* Space for compiled regexp */ |
| 5708 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate; |
| 5709 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 5710 | if (prog == NULL) |
| 5711 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5712 | state_ptr = prog->state; |
| 5713 | |
| 5714 | /* |
| 5715 | * PASS 2 |
| 5716 | * Build the NFA |
| 5717 | */ |
| 5718 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 5719 | if (prog->start == NULL) |
| 5720 | goto fail; |
| 5721 | |
| 5722 | prog->regflags = regflags; |
| 5723 | prog->engine = &nfa_regengine; |
| 5724 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5725 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5726 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5727 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 5728 | |
| 5729 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 5730 | prog->regstart = nfa_get_regstart(prog->start, 0); |
| 5731 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5732 | #ifdef ENABLE_LOG |
| 5733 | nfa_postfix_dump(expr, OK); |
| 5734 | nfa_dump(prog); |
| 5735 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5736 | #ifdef FEAT_SYN_HL |
| 5737 | /* Remember whether this pattern has any \z specials in it. */ |
| 5738 | prog->reghasz = re_has_z; |
| 5739 | #endif |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5740 | #ifdef DEBUG |
| 5741 | prog->pattern = vim_strsave(expr); /* memory will leak */ |
| 5742 | nfa_regengine.expr = NULL; |
| 5743 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5744 | |
| 5745 | out: |
| 5746 | vim_free(post_start); |
| 5747 | post_start = post_ptr = post_end = NULL; |
| 5748 | state_ptr = NULL; |
| 5749 | return (regprog_T *)prog; |
| 5750 | |
| 5751 | fail: |
| 5752 | vim_free(prog); |
| 5753 | prog = NULL; |
| 5754 | #ifdef ENABLE_LOG |
| 5755 | nfa_postfix_dump(expr, FAIL); |
| 5756 | #endif |
| 5757 | #ifdef DEBUG |
| 5758 | nfa_regengine.expr = NULL; |
| 5759 | #endif |
| 5760 | goto out; |
| 5761 | } |
| 5762 | |
| 5763 | |
| 5764 | /* |
| 5765 | * Match a regexp against a string. |
| 5766 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 5767 | * Uses curbuf for line count and 'iskeyword'. |
| 5768 | * |
| 5769 | * Return TRUE if there is a match, FALSE if not. |
| 5770 | */ |
| 5771 | static int |
| 5772 | nfa_regexec(rmp, line, col) |
| 5773 | regmatch_T *rmp; |
| 5774 | char_u *line; /* string to match against */ |
| 5775 | colnr_T col; /* column to start looking for match */ |
| 5776 | { |
| 5777 | reg_match = rmp; |
| 5778 | reg_mmatch = NULL; |
| 5779 | reg_maxline = 0; |
| 5780 | reg_line_lbr = FALSE; |
| 5781 | reg_buf = curbuf; |
| 5782 | reg_win = NULL; |
| 5783 | ireg_ic = rmp->rm_ic; |
| 5784 | #ifdef FEAT_MBYTE |
| 5785 | ireg_icombine = FALSE; |
| 5786 | #endif |
| 5787 | ireg_maxcol = 0; |
| 5788 | return (nfa_regexec_both(line, col) != 0); |
| 5789 | } |
| 5790 | |
| 5791 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 5792 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 5793 | |
| 5794 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 5795 | |
| 5796 | /* |
| 5797 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 5798 | */ |
| 5799 | static int |
| 5800 | nfa_regexec_nl(rmp, line, col) |
| 5801 | regmatch_T *rmp; |
| 5802 | char_u *line; /* string to match against */ |
| 5803 | colnr_T col; /* column to start looking for match */ |
| 5804 | { |
| 5805 | reg_match = rmp; |
| 5806 | reg_mmatch = NULL; |
| 5807 | reg_maxline = 0; |
| 5808 | reg_line_lbr = TRUE; |
| 5809 | reg_buf = curbuf; |
| 5810 | reg_win = NULL; |
| 5811 | ireg_ic = rmp->rm_ic; |
| 5812 | #ifdef FEAT_MBYTE |
| 5813 | ireg_icombine = FALSE; |
| 5814 | #endif |
| 5815 | ireg_maxcol = 0; |
| 5816 | return (nfa_regexec_both(line, col) != 0); |
| 5817 | } |
| 5818 | #endif |
| 5819 | |
| 5820 | |
| 5821 | /* |
| 5822 | * Match a regexp against multiple lines. |
| 5823 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 5824 | * Uses curbuf for line count and 'iskeyword'. |
| 5825 | * |
| 5826 | * Return zero if there is no match. Return number of lines contained in the |
| 5827 | * match otherwise. |
| 5828 | * |
| 5829 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 5830 | * |
| 5831 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 5832 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 5833 | * |
| 5834 | * +-------------------------+ |
| 5835 | * |a | |
| 5836 | * |b | |
| 5837 | * |c | |
| 5838 | * | | |
| 5839 | * +-------------------------+ |
| 5840 | * |
| 5841 | * then nfa_regexec_multi() returns 3. while the original |
| 5842 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 5843 | * |
| 5844 | * FIXME if this behavior is not compatible. |
| 5845 | */ |
| 5846 | static long |
| 5847 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 5848 | regmmatch_T *rmp; |
| 5849 | win_T *win; /* window in which to search or NULL */ |
| 5850 | buf_T *buf; /* buffer in which to search */ |
| 5851 | linenr_T lnum; /* nr of line to start looking for match */ |
| 5852 | colnr_T col; /* column to start looking for match */ |
| 5853 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 5854 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5855 | reg_match = NULL; |
| 5856 | reg_mmatch = rmp; |
| 5857 | reg_buf = buf; |
| 5858 | reg_win = win; |
| 5859 | reg_firstlnum = lnum; |
| 5860 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 5861 | reg_line_lbr = FALSE; |
| 5862 | ireg_ic = rmp->rmm_ic; |
| 5863 | #ifdef FEAT_MBYTE |
| 5864 | ireg_icombine = FALSE; |
| 5865 | #endif |
| 5866 | ireg_maxcol = rmp->rmm_maxcol; |
| 5867 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5868 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | #ifdef DEBUG |
| 5872 | # undef ENABLE_LOG |
| 5873 | #endif |