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