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