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 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame^] | 2583 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2584 | e1.out = NULL; /* stores list with out1's */ |
| 2585 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 2586 | while (n-- > 0) |
| 2587 | { |
| 2588 | e = POP(); /* get character */ |
| 2589 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 2590 | if (s == NULL) |
| 2591 | goto theend; |
| 2592 | if (e1.out == NULL) |
| 2593 | e1 = e; |
| 2594 | patch(e.out, s1); |
| 2595 | append(e1.out, list1(&s->out1)); |
| 2596 | s1 = s; |
| 2597 | } |
| 2598 | PUSH(frag(s, e1.out)); |
| 2599 | break; |
| 2600 | } |
| 2601 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2602 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 2603 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2604 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2605 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2606 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2607 | { |
| 2608 | int neg = (*p == NFA_PREV_ATOM_NO_WIDTH_NEG |
| 2609 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
| 2610 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 2611 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2612 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
| 2613 | int start_state = NFA_START_INVISIBLE; |
| 2614 | int end_state = NFA_END_INVISIBLE; |
| 2615 | int n = 0; |
| 2616 | nfa_state_T *zend; |
| 2617 | nfa_state_T *skip; |
| 2618 | |
| 2619 | if (before) |
| 2620 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 2621 | else if (pattern) |
| 2622 | { |
| 2623 | start_state = NFA_START_PATTERN; |
| 2624 | end_state = NFA_END_PATTERN; |
| 2625 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2626 | |
| 2627 | if (before) |
| 2628 | n = *++p; /* get the count */ |
| 2629 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2630 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 2631 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2632 | * The \@<= operator: match for the preceding atom. |
| 2633 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2634 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2635 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2636 | |
| 2637 | if (nfa_calc_size == TRUE) |
| 2638 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2639 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2640 | break; |
| 2641 | } |
| 2642 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2643 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2644 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2645 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2646 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2647 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2648 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2649 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2650 | if (neg) |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 2651 | { |
| 2652 | s->negated = TRUE; |
| 2653 | s1->negated = TRUE; |
| 2654 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2655 | if (before) |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2656 | s->val = n; /* store the count */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2657 | if (pattern) |
| 2658 | { |
| 2659 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 2660 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 2661 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 2662 | s1->out= skip; |
| 2663 | patch(e.out, zend); |
| 2664 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2665 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2666 | else |
| 2667 | { |
| 2668 | patch(e.out, s1); |
| 2669 | PUSH(frag(s, list1(&s1->out))); |
| 2670 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2671 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2672 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2673 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2674 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2675 | case NFA_COMPOSING: /* char with composing char */ |
| 2676 | #if 0 |
| 2677 | /* TODO */ |
| 2678 | if (regflags & RF_ICOMBINE) |
| 2679 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 2680 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2681 | } |
| 2682 | #endif |
| 2683 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2684 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2685 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2686 | case NFA_MOPEN: /* \( \) Submatch */ |
| 2687 | case NFA_MOPEN1: |
| 2688 | case NFA_MOPEN2: |
| 2689 | case NFA_MOPEN3: |
| 2690 | case NFA_MOPEN4: |
| 2691 | case NFA_MOPEN5: |
| 2692 | case NFA_MOPEN6: |
| 2693 | case NFA_MOPEN7: |
| 2694 | case NFA_MOPEN8: |
| 2695 | case NFA_MOPEN9: |
| 2696 | #ifdef FEAT_SYN_HL |
| 2697 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 2698 | case NFA_ZOPEN1: |
| 2699 | case NFA_ZOPEN2: |
| 2700 | case NFA_ZOPEN3: |
| 2701 | case NFA_ZOPEN4: |
| 2702 | case NFA_ZOPEN5: |
| 2703 | case NFA_ZOPEN6: |
| 2704 | case NFA_ZOPEN7: |
| 2705 | case NFA_ZOPEN8: |
| 2706 | case NFA_ZOPEN9: |
| 2707 | #endif |
| 2708 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2709 | if (nfa_calc_size == TRUE) |
| 2710 | { |
| 2711 | nstate += 2; |
| 2712 | break; |
| 2713 | } |
| 2714 | |
| 2715 | mopen = *p; |
| 2716 | switch (*p) |
| 2717 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2718 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 2719 | #ifdef FEAT_SYN_HL |
| 2720 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 2721 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 2722 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 2723 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 2724 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 2725 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 2726 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 2727 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 2728 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 2729 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 2730 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2731 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2732 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2733 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2734 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2735 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2736 | mclose = *p + NSUBEXP; |
| 2737 | break; |
| 2738 | } |
| 2739 | |
| 2740 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 2741 | * the empty regexp "". In this case, the NFA will be |
| 2742 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 2743 | * empty groups of parenthesis, and empty mbyte chars */ |
| 2744 | if (stackp == stack) |
| 2745 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2746 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2747 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2748 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2749 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2750 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2751 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2752 | patch(list1(&s->out), s1); |
| 2753 | PUSH(frag(s, list1(&s1->out))); |
| 2754 | break; |
| 2755 | } |
| 2756 | |
| 2757 | /* At least one node was emitted before NFA_MOPEN, so |
| 2758 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 2759 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2760 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2761 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2762 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2763 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2764 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2765 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2766 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2767 | patch(e.out, s1); |
| 2768 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2769 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2770 | if (mopen == NFA_COMPOSING) |
| 2771 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2772 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2773 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2774 | |
| 2775 | PUSH(frag(s, list1(&s1->out))); |
| 2776 | break; |
| 2777 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2778 | case NFA_BACKREF1: |
| 2779 | case NFA_BACKREF2: |
| 2780 | case NFA_BACKREF3: |
| 2781 | case NFA_BACKREF4: |
| 2782 | case NFA_BACKREF5: |
| 2783 | case NFA_BACKREF6: |
| 2784 | case NFA_BACKREF7: |
| 2785 | case NFA_BACKREF8: |
| 2786 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2787 | #ifdef FEAT_SYN_HL |
| 2788 | case NFA_ZREF1: |
| 2789 | case NFA_ZREF2: |
| 2790 | case NFA_ZREF3: |
| 2791 | case NFA_ZREF4: |
| 2792 | case NFA_ZREF5: |
| 2793 | case NFA_ZREF6: |
| 2794 | case NFA_ZREF7: |
| 2795 | case NFA_ZREF8: |
| 2796 | case NFA_ZREF9: |
| 2797 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2798 | if (nfa_calc_size == TRUE) |
| 2799 | { |
| 2800 | nstate += 2; |
| 2801 | break; |
| 2802 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2803 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2804 | if (s == NULL) |
| 2805 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2806 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2807 | if (s1 == NULL) |
| 2808 | goto theend; |
| 2809 | patch(list1(&s->out), s1); |
| 2810 | PUSH(frag(s, list1(&s1->out))); |
| 2811 | break; |
| 2812 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2813 | case NFA_LNUM: |
| 2814 | case NFA_LNUM_GT: |
| 2815 | case NFA_LNUM_LT: |
| 2816 | case NFA_VCOL: |
| 2817 | case NFA_VCOL_GT: |
| 2818 | case NFA_VCOL_LT: |
| 2819 | case NFA_COL: |
| 2820 | case NFA_COL_GT: |
| 2821 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2822 | case NFA_MARK: |
| 2823 | case NFA_MARK_GT: |
| 2824 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2825 | { |
| 2826 | int n = *++p; /* lnum, col or mark name */ |
| 2827 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2828 | if (nfa_calc_size == TRUE) |
| 2829 | { |
| 2830 | nstate += 1; |
| 2831 | break; |
| 2832 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2833 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2834 | if (s == NULL) |
| 2835 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2836 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2837 | PUSH(frag(s, list1(&s->out))); |
| 2838 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2839 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2840 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2841 | case NFA_ZSTART: |
| 2842 | case NFA_ZEND: |
| 2843 | default: |
| 2844 | /* Operands */ |
| 2845 | if (nfa_calc_size == TRUE) |
| 2846 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2847 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2848 | break; |
| 2849 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2850 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2851 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2852 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2853 | PUSH(frag(s, list1(&s->out))); |
| 2854 | break; |
| 2855 | |
| 2856 | } /* switch(*p) */ |
| 2857 | |
| 2858 | } /* for(p = postfix; *p; ++p) */ |
| 2859 | |
| 2860 | if (nfa_calc_size == TRUE) |
| 2861 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2862 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2863 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2864 | } |
| 2865 | |
| 2866 | e = POP(); |
| 2867 | if (stackp != stack) |
| 2868 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 2869 | |
| 2870 | if (istate >= nstate) |
| 2871 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 2872 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2873 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 2874 | matchstate->c = NFA_MATCH; |
| 2875 | matchstate->out = matchstate->out1 = NULL; |
| 2876 | |
| 2877 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2878 | ret = e.start; |
| 2879 | |
| 2880 | theend: |
| 2881 | vim_free(stack); |
| 2882 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2883 | |
| 2884 | #undef POP1 |
| 2885 | #undef PUSH1 |
| 2886 | #undef POP2 |
| 2887 | #undef PUSH2 |
| 2888 | #undef POP |
| 2889 | #undef PUSH |
| 2890 | } |
| 2891 | |
| 2892 | /**************************************************************** |
| 2893 | * NFA execution code. |
| 2894 | ****************************************************************/ |
| 2895 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2896 | typedef struct |
| 2897 | { |
| 2898 | int in_use; /* number of subexpr with useful info */ |
| 2899 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2900 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2901 | union |
| 2902 | { |
| 2903 | struct multipos |
| 2904 | { |
| 2905 | lpos_T start; |
| 2906 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2907 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2908 | struct linepos |
| 2909 | { |
| 2910 | char_u *start; |
| 2911 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2912 | } line[NSUBEXP]; |
| 2913 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2914 | } regsub_T; |
| 2915 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2916 | typedef struct |
| 2917 | { |
| 2918 | regsub_T norm; /* \( .. \) matches */ |
| 2919 | #ifdef FEAT_SYN_HL |
| 2920 | regsub_T synt; /* \z( .. \) matches */ |
| 2921 | #endif |
| 2922 | } regsubs_T; |
| 2923 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 2924 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 2925 | typedef struct nfa_pim_S nfa_pim_T; |
| 2926 | struct nfa_pim_S |
| 2927 | { |
| 2928 | nfa_state_T *state; |
| 2929 | int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */ |
| 2930 | nfa_pim_T *pim; /* another PIM at the same position */ |
| 2931 | regsubs_T subs; /* submatch info, only party used */ |
| 2932 | }; |
| 2933 | |
| 2934 | /* Values for done in nfa_pim_T. */ |
| 2935 | #define NFA_PIM_TODO 0 |
| 2936 | #define NFA_PIM_MATCH 1 |
| 2937 | #define NFA_PIM_NOMATCH -1 |
| 2938 | |
| 2939 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2940 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2941 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2942 | { |
| 2943 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2944 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 2945 | nfa_pim_T *pim; /* if not NULL: postponed invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2946 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2947 | } nfa_thread_T; |
| 2948 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2949 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2950 | typedef struct |
| 2951 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2952 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2953 | int n; /* nr of states currently in "t" */ |
| 2954 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2955 | int id; /* ID of the list */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2956 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2957 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2958 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2959 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 2960 | static void log_subexpr __ARGS((regsub_T *sub)); |
| 2961 | |
| 2962 | static void |
| 2963 | log_subsexpr(subs) |
| 2964 | regsubs_T *subs; |
| 2965 | { |
| 2966 | log_subexpr(&subs->norm); |
| 2967 | # ifdef FEAT_SYN_HL |
| 2968 | log_subexpr(&subs->synt); |
| 2969 | # endif |
| 2970 | } |
| 2971 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2972 | static void |
| 2973 | log_subexpr(sub) |
| 2974 | regsub_T *sub; |
| 2975 | { |
| 2976 | int j; |
| 2977 | |
| 2978 | for (j = 0; j < sub->in_use; j++) |
| 2979 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2980 | 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] | 2981 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2982 | sub->list.multi[j].start.col, |
| 2983 | (int)sub->list.multi[j].start.lnum, |
| 2984 | sub->list.multi[j].end.col, |
| 2985 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2986 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 2987 | { |
| 2988 | char *s = (char *)sub->list.line[j].start; |
| 2989 | char *e = (char *)sub->list.line[j].end; |
| 2990 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2991 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2992 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 2993 | s == NULL ? "NULL" : s, |
| 2994 | e == NULL ? "NULL" : e); |
| 2995 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2996 | } |
| 2997 | #endif |
| 2998 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2999 | /* Used during execution: whether a match has been found. */ |
| 3000 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3001 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3002 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3003 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3004 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3005 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3006 | 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] | 3007 | 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] | 3008 | |
| 3009 | static void |
| 3010 | clear_sub(sub) |
| 3011 | regsub_T *sub; |
| 3012 | { |
| 3013 | if (REG_MULTI) |
| 3014 | /* Use 0xff to set lnum to -1 */ |
| 3015 | vim_memset(sub->list.multi, 0xff, |
| 3016 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3017 | else |
| 3018 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3019 | sub->in_use = 0; |
| 3020 | } |
| 3021 | |
| 3022 | /* |
| 3023 | * Copy the submatches from "from" to "to". |
| 3024 | */ |
| 3025 | static void |
| 3026 | copy_sub(to, from) |
| 3027 | regsub_T *to; |
| 3028 | regsub_T *from; |
| 3029 | { |
| 3030 | to->in_use = from->in_use; |
| 3031 | if (from->in_use > 0) |
| 3032 | { |
| 3033 | /* Copy the match start and end positions. */ |
| 3034 | if (REG_MULTI) |
| 3035 | mch_memmove(&to->list.multi[0], |
| 3036 | &from->list.multi[0], |
| 3037 | sizeof(struct multipos) * from->in_use); |
| 3038 | else |
| 3039 | mch_memmove(&to->list.line[0], |
| 3040 | &from->list.line[0], |
| 3041 | sizeof(struct linepos) * from->in_use); |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | /* |
| 3046 | * Like copy_sub() but exclude the main match. |
| 3047 | */ |
| 3048 | static void |
| 3049 | copy_sub_off(to, from) |
| 3050 | regsub_T *to; |
| 3051 | regsub_T *from; |
| 3052 | { |
| 3053 | if (to->in_use < from->in_use) |
| 3054 | to->in_use = from->in_use; |
| 3055 | if (from->in_use > 1) |
| 3056 | { |
| 3057 | /* Copy the match start and end positions. */ |
| 3058 | if (REG_MULTI) |
| 3059 | mch_memmove(&to->list.multi[1], |
| 3060 | &from->list.multi[1], |
| 3061 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3062 | else |
| 3063 | mch_memmove(&to->list.line[1], |
| 3064 | &from->list.line[1], |
| 3065 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3066 | } |
| 3067 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3068 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3069 | /* |
| 3070 | * Return TRUE if "sub1" and "sub2" have the same positions. |
| 3071 | */ |
| 3072 | static int |
| 3073 | sub_equal(sub1, sub2) |
| 3074 | regsub_T *sub1; |
| 3075 | regsub_T *sub2; |
| 3076 | { |
| 3077 | int i; |
| 3078 | int todo; |
| 3079 | linenr_T s1, e1; |
| 3080 | linenr_T s2, e2; |
| 3081 | char_u *sp1, *ep1; |
| 3082 | char_u *sp2, *ep2; |
| 3083 | |
| 3084 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3085 | if (REG_MULTI) |
| 3086 | { |
| 3087 | for (i = 0; i < todo; ++i) |
| 3088 | { |
| 3089 | if (i < sub1->in_use) |
| 3090 | { |
| 3091 | s1 = sub1->list.multi[i].start.lnum; |
| 3092 | e1 = sub1->list.multi[i].end.lnum; |
| 3093 | } |
| 3094 | else |
| 3095 | { |
| 3096 | s1 = 0; |
| 3097 | e1 = 0; |
| 3098 | } |
| 3099 | if (i < sub2->in_use) |
| 3100 | { |
| 3101 | s2 = sub2->list.multi[i].start.lnum; |
| 3102 | e2 = sub2->list.multi[i].end.lnum; |
| 3103 | } |
| 3104 | else |
| 3105 | { |
| 3106 | s2 = 0; |
| 3107 | e2 = 0; |
| 3108 | } |
| 3109 | if (s1 != s2 || e1 != e2) |
| 3110 | return FALSE; |
| 3111 | if (s1 != 0 && sub1->list.multi[i].start.col |
| 3112 | != sub2->list.multi[i].start.col) |
| 3113 | return FALSE; |
| 3114 | if (e1 != 0 && sub1->list.multi[i].end.col |
| 3115 | != sub2->list.multi[i].end.col) |
| 3116 | return FALSE; |
| 3117 | } |
| 3118 | } |
| 3119 | else |
| 3120 | { |
| 3121 | for (i = 0; i < todo; ++i) |
| 3122 | { |
| 3123 | if (i < sub1->in_use) |
| 3124 | { |
| 3125 | sp1 = sub1->list.line[i].start; |
| 3126 | ep1 = sub1->list.line[i].end; |
| 3127 | } |
| 3128 | else |
| 3129 | { |
| 3130 | sp1 = NULL; |
| 3131 | ep1 = NULL; |
| 3132 | } |
| 3133 | if (i < sub2->in_use) |
| 3134 | { |
| 3135 | sp2 = sub2->list.line[i].start; |
| 3136 | ep2 = sub2->list.line[i].end; |
| 3137 | } |
| 3138 | else |
| 3139 | { |
| 3140 | sp2 = NULL; |
| 3141 | ep2 = NULL; |
| 3142 | } |
| 3143 | if (sp1 != sp2 || ep1 != ep2) |
| 3144 | return FALSE; |
| 3145 | } |
| 3146 | } |
| 3147 | |
| 3148 | return TRUE; |
| 3149 | } |
| 3150 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3151 | #ifdef ENABLE_LOG |
| 3152 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3153 | 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] | 3154 | { |
| 3155 | int col; |
| 3156 | |
| 3157 | if (sub->in_use <= 0) |
| 3158 | col = -1; |
| 3159 | else if (REG_MULTI) |
| 3160 | col = sub->list.multi[0].start.col; |
| 3161 | else |
| 3162 | col = (int)(sub->list.line[0].start - regline); |
| 3163 | nfa_set_code(state->c); |
| 3164 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n", |
| 3165 | action, abs(state->id), lid, state->c, code, col); |
| 3166 | } |
| 3167 | #endif |
| 3168 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3169 | static void |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3170 | addstate(l, state, subs, off) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3171 | nfa_list_T *l; /* runtime state list */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3172 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3173 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3174 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3175 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3176 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3177 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3178 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3179 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3180 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3181 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3182 | regsub_T *sub; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3183 | #ifdef ENABLE_LOG |
| 3184 | int did_print = FALSE; |
| 3185 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3186 | |
| 3187 | if (l == NULL || state == NULL) |
| 3188 | return; |
| 3189 | |
| 3190 | switch (state->c) |
| 3191 | { |
| 3192 | case NFA_SPLIT: |
| 3193 | case NFA_NOT: |
| 3194 | case NFA_NOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3195 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3196 | case NFA_NCLOSE: |
| 3197 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3198 | case NFA_MCLOSE1: |
| 3199 | case NFA_MCLOSE2: |
| 3200 | case NFA_MCLOSE3: |
| 3201 | case NFA_MCLOSE4: |
| 3202 | case NFA_MCLOSE5: |
| 3203 | case NFA_MCLOSE6: |
| 3204 | case NFA_MCLOSE7: |
| 3205 | case NFA_MCLOSE8: |
| 3206 | case NFA_MCLOSE9: |
| 3207 | #ifdef FEAT_SYN_HL |
| 3208 | case NFA_ZCLOSE: |
| 3209 | case NFA_ZCLOSE1: |
| 3210 | case NFA_ZCLOSE2: |
| 3211 | case NFA_ZCLOSE3: |
| 3212 | case NFA_ZCLOSE4: |
| 3213 | case NFA_ZCLOSE5: |
| 3214 | case NFA_ZCLOSE6: |
| 3215 | case NFA_ZCLOSE7: |
| 3216 | case NFA_ZCLOSE8: |
| 3217 | case NFA_ZCLOSE9: |
| 3218 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3219 | case NFA_ZEND: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3220 | /* These nodes are not added themselves but their "out" and/or |
| 3221 | * "out1" may be added below. */ |
| 3222 | break; |
| 3223 | |
| 3224 | case NFA_MOPEN: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3225 | case NFA_MOPEN1: |
| 3226 | case NFA_MOPEN2: |
| 3227 | case NFA_MOPEN3: |
| 3228 | case NFA_MOPEN4: |
| 3229 | case NFA_MOPEN5: |
| 3230 | case NFA_MOPEN6: |
| 3231 | case NFA_MOPEN7: |
| 3232 | case NFA_MOPEN8: |
| 3233 | case NFA_MOPEN9: |
| 3234 | #ifdef FEAT_SYN_HL |
| 3235 | case NFA_ZOPEN: |
| 3236 | case NFA_ZOPEN1: |
| 3237 | case NFA_ZOPEN2: |
| 3238 | case NFA_ZOPEN3: |
| 3239 | case NFA_ZOPEN4: |
| 3240 | case NFA_ZOPEN5: |
| 3241 | case NFA_ZOPEN6: |
| 3242 | case NFA_ZOPEN7: |
| 3243 | case NFA_ZOPEN8: |
| 3244 | case NFA_ZOPEN9: |
| 3245 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3246 | case NFA_ZSTART: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3247 | /* These nodes do not need to be added, but we need to bail out |
| 3248 | * when it was tried to be added to this list before. */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3249 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3250 | goto skip_add; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3251 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3252 | break; |
| 3253 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3254 | case NFA_BOL: |
| 3255 | case NFA_BOF: |
| 3256 | /* "^" won't match past end-of-line, don't bother trying. |
| 3257 | * Except when we are going to the next line for a look-behind |
| 3258 | * match. */ |
| 3259 | if (reginput > regline |
| 3260 | && (nfa_endp == NULL |
| 3261 | || !REG_MULTI |
| 3262 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 3263 | goto skip_add; |
| 3264 | /* FALLTHROUGH */ |
| 3265 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3266 | default: |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3267 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3268 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3269 | /* This state is already in the list, don't add it again, |
| 3270 | * unless it is an MOPEN that is used for a backreference. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3271 | if (!nfa_has_backref) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3272 | { |
| 3273 | skip_add: |
| 3274 | #ifdef ENABLE_LOG |
| 3275 | nfa_set_code(state->c); |
| 3276 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 3277 | abs(state->id), l->id, state->c, code); |
| 3278 | #endif |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3279 | return; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3280 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3281 | |
| 3282 | /* See if the same state is already in the list with the same |
| 3283 | * positions. */ |
| 3284 | for (i = 0; i < l->n; ++i) |
| 3285 | { |
| 3286 | thread = &l->t[i]; |
| 3287 | if (thread->state->id == state->id |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3288 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 3289 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3290 | && (!nfa_has_zsubexpr || |
| 3291 | sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3292 | #endif |
| 3293 | ) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3294 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3295 | } |
| 3296 | } |
| 3297 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3298 | /* when there are backreferences or look-behind matches the number |
| 3299 | * of states may be (a lot) bigger */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3300 | if (nfa_has_backref && l->n == l->len) |
| 3301 | { |
| 3302 | int newlen = l->len * 3 / 2 + 50; |
| 3303 | |
| 3304 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 3305 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3306 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3307 | |
| 3308 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3309 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3310 | thread = &l->t[l->n++]; |
| 3311 | thread->state = state; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3312 | thread->pim = NULL; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3313 | copy_sub(&thread->subs.norm, &subs->norm); |
| 3314 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3315 | if (nfa_has_zsubexpr) |
| 3316 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3317 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3318 | #ifdef ENABLE_LOG |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3319 | report_state("Adding", &thread->subs.norm, state, l->id); |
| 3320 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3321 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3325 | if (!did_print) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3326 | report_state("Processing", &subs->norm, state, l->id); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3327 | #endif |
| 3328 | switch (state->c) |
| 3329 | { |
| 3330 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3331 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3332 | break; |
| 3333 | |
| 3334 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3335 | /* order matters here */ |
| 3336 | addstate(l, state->out, subs, off); |
| 3337 | addstate(l, state->out1, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3338 | break; |
| 3339 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3340 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3341 | case NFA_NOPEN: |
| 3342 | case NFA_NCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3343 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3344 | break; |
| 3345 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3346 | case NFA_MOPEN: |
| 3347 | case NFA_MOPEN1: |
| 3348 | case NFA_MOPEN2: |
| 3349 | case NFA_MOPEN3: |
| 3350 | case NFA_MOPEN4: |
| 3351 | case NFA_MOPEN5: |
| 3352 | case NFA_MOPEN6: |
| 3353 | case NFA_MOPEN7: |
| 3354 | case NFA_MOPEN8: |
| 3355 | case NFA_MOPEN9: |
| 3356 | #ifdef FEAT_SYN_HL |
| 3357 | case NFA_ZOPEN: |
| 3358 | case NFA_ZOPEN1: |
| 3359 | case NFA_ZOPEN2: |
| 3360 | case NFA_ZOPEN3: |
| 3361 | case NFA_ZOPEN4: |
| 3362 | case NFA_ZOPEN5: |
| 3363 | case NFA_ZOPEN6: |
| 3364 | case NFA_ZOPEN7: |
| 3365 | case NFA_ZOPEN8: |
| 3366 | case NFA_ZOPEN9: |
| 3367 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3368 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3369 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3370 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3371 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3372 | sub = &subs->norm; |
| 3373 | } |
| 3374 | #ifdef FEAT_SYN_HL |
| 3375 | else if (state->c >= NFA_ZOPEN) |
| 3376 | { |
| 3377 | subidx = state->c - NFA_ZOPEN; |
| 3378 | sub = &subs->synt; |
| 3379 | } |
| 3380 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3381 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3382 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3383 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3384 | sub = &subs->norm; |
| 3385 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3386 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3387 | /* Set the position (with "off") in the subexpression. Save and |
| 3388 | * restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | 4b6ebe6 | 2013-05-30 17:49:24 +0200 | [diff] [blame] | 3389 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3390 | if (REG_MULTI) |
| 3391 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3392 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3393 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3394 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3395 | save_in_use = -1; |
| 3396 | } |
| 3397 | else |
| 3398 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3399 | save_in_use = sub->in_use; |
| 3400 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3401 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3402 | sub->list.multi[i].start.lnum = -1; |
| 3403 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3404 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3405 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3406 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3407 | if (off == -1) |
| 3408 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3409 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 3410 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3411 | } |
| 3412 | else |
| 3413 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3414 | sub->list.multi[subidx].start.lnum = reglnum; |
| 3415 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3416 | (colnr_T)(reginput - regline + off); |
| 3417 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3418 | } |
| 3419 | else |
| 3420 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3421 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3422 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3423 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3424 | save_in_use = -1; |
| 3425 | } |
| 3426 | else |
| 3427 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3428 | save_in_use = sub->in_use; |
| 3429 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3430 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3431 | sub->list.line[i].start = NULL; |
| 3432 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3433 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3434 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3435 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3436 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3437 | } |
| 3438 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3439 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3440 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3441 | if (save_in_use == -1) |
| 3442 | { |
| 3443 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3444 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3445 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3446 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3447 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3448 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3449 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3450 | break; |
| 3451 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3452 | case NFA_MCLOSE: |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 3453 | if (nfa_has_zend) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3454 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 3455 | /* Do not overwrite the position set by \ze. If no \ze |
| 3456 | * encountered end will be set in nfa_regtry(). */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3457 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3458 | break; |
| 3459 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3460 | case NFA_MCLOSE1: |
| 3461 | case NFA_MCLOSE2: |
| 3462 | case NFA_MCLOSE3: |
| 3463 | case NFA_MCLOSE4: |
| 3464 | case NFA_MCLOSE5: |
| 3465 | case NFA_MCLOSE6: |
| 3466 | case NFA_MCLOSE7: |
| 3467 | case NFA_MCLOSE8: |
| 3468 | case NFA_MCLOSE9: |
| 3469 | #ifdef FEAT_SYN_HL |
| 3470 | case NFA_ZCLOSE: |
| 3471 | case NFA_ZCLOSE1: |
| 3472 | case NFA_ZCLOSE2: |
| 3473 | case NFA_ZCLOSE3: |
| 3474 | case NFA_ZCLOSE4: |
| 3475 | case NFA_ZCLOSE5: |
| 3476 | case NFA_ZCLOSE6: |
| 3477 | case NFA_ZCLOSE7: |
| 3478 | case NFA_ZCLOSE8: |
| 3479 | case NFA_ZCLOSE9: |
| 3480 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3481 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3482 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3483 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3484 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3485 | sub = &subs->norm; |
| 3486 | } |
| 3487 | #ifdef FEAT_SYN_HL |
| 3488 | else if (state->c >= NFA_ZCLOSE) |
| 3489 | { |
| 3490 | subidx = state->c - NFA_ZCLOSE; |
| 3491 | sub = &subs->synt; |
| 3492 | } |
| 3493 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3494 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3495 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3496 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3497 | sub = &subs->norm; |
| 3498 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3499 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3500 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 3501 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3502 | save_in_use = sub->in_use; |
| 3503 | if (sub->in_use <= subidx) |
| 3504 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3505 | if (REG_MULTI) |
| 3506 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3507 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3508 | if (off == -1) |
| 3509 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3510 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 3511 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3512 | } |
| 3513 | else |
| 3514 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3515 | sub->list.multi[subidx].end.lnum = reglnum; |
| 3516 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3517 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3518 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3519 | } |
| 3520 | else |
| 3521 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3522 | save_ptr = sub->list.line[subidx].end; |
| 3523 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3526 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3527 | |
| 3528 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3529 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3530 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3531 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3532 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3533 | break; |
| 3534 | } |
| 3535 | } |
| 3536 | |
| 3537 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3538 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 3539 | * Used for zero-width matches, next state to use is the added one. |
| 3540 | * This makes sure the order of states to be tried does not change, which |
| 3541 | * matters for alternatives. |
| 3542 | */ |
| 3543 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3544 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3545 | nfa_list_T *l; /* runtime state list */ |
| 3546 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3547 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3548 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3549 | int *ip; |
| 3550 | { |
| 3551 | int tlen = l->n; |
| 3552 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3553 | int listidx = *ip; |
| 3554 | int i; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3555 | |
| 3556 | /* 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] | 3557 | addstate(l, state, subs, 0); |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3558 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3559 | /* fill in the "pim" field in the new states */ |
| 3560 | if (pim != NULL) |
| 3561 | for (i = tlen; i < l->n; ++i) |
| 3562 | l->t[i].pim = pim; |
| 3563 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3564 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3565 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3566 | return; |
| 3567 | |
| 3568 | /* re-order to put the new state at the current position */ |
| 3569 | count = l->n - tlen; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3570 | if (count == 1) |
| 3571 | { |
| 3572 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3573 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3574 | } |
| 3575 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3576 | { |
| 3577 | /* make space for new states, then move them from the |
| 3578 | * end to the current position */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3579 | mch_memmove(&(l->t[listidx + count]), |
| 3580 | &(l->t[listidx + 1]), |
| 3581 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 3582 | mch_memmove(&(l->t[listidx]), |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3583 | &(l->t[l->n - 1]), |
| 3584 | sizeof(nfa_thread_T) * count); |
| 3585 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3586 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3587 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3588 | } |
| 3589 | |
| 3590 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3591 | * Check character class "class" against current character c. |
| 3592 | */ |
| 3593 | static int |
| 3594 | check_char_class(class, c) |
| 3595 | int class; |
| 3596 | int c; |
| 3597 | { |
| 3598 | switch (class) |
| 3599 | { |
| 3600 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3601 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3602 | return OK; |
| 3603 | break; |
| 3604 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3605 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3606 | return OK; |
| 3607 | break; |
| 3608 | case NFA_CLASS_BLANK: |
| 3609 | if (c == ' ' || c == '\t') |
| 3610 | return OK; |
| 3611 | break; |
| 3612 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3613 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3614 | return OK; |
| 3615 | break; |
| 3616 | case NFA_CLASS_DIGIT: |
| 3617 | if (VIM_ISDIGIT(c)) |
| 3618 | return OK; |
| 3619 | break; |
| 3620 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3621 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3622 | return OK; |
| 3623 | break; |
| 3624 | case NFA_CLASS_LOWER: |
| 3625 | if (MB_ISLOWER(c)) |
| 3626 | return OK; |
| 3627 | break; |
| 3628 | case NFA_CLASS_PRINT: |
| 3629 | if (vim_isprintc(c)) |
| 3630 | return OK; |
| 3631 | break; |
| 3632 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3633 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3634 | return OK; |
| 3635 | break; |
| 3636 | case NFA_CLASS_SPACE: |
| 3637 | if ((c >=9 && c <= 13) || (c == ' ')) |
| 3638 | return OK; |
| 3639 | break; |
| 3640 | case NFA_CLASS_UPPER: |
| 3641 | if (MB_ISUPPER(c)) |
| 3642 | return OK; |
| 3643 | break; |
| 3644 | case NFA_CLASS_XDIGIT: |
| 3645 | if (vim_isxdigit(c)) |
| 3646 | return OK; |
| 3647 | break; |
| 3648 | case NFA_CLASS_TAB: |
| 3649 | if (c == '\t') |
| 3650 | return OK; |
| 3651 | break; |
| 3652 | case NFA_CLASS_RETURN: |
| 3653 | if (c == '\r') |
| 3654 | return OK; |
| 3655 | break; |
| 3656 | case NFA_CLASS_BACKSPACE: |
| 3657 | if (c == '\b') |
| 3658 | return OK; |
| 3659 | break; |
| 3660 | case NFA_CLASS_ESCAPE: |
| 3661 | if (c == '\033') |
| 3662 | return OK; |
| 3663 | break; |
| 3664 | |
| 3665 | default: |
| 3666 | /* should not be here :P */ |
| 3667 | EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class ")); |
| 3668 | } |
| 3669 | return FAIL; |
| 3670 | } |
| 3671 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3672 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
| 3673 | |
| 3674 | /* |
| 3675 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3676 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3677 | */ |
| 3678 | static int |
| 3679 | match_backref(sub, subidx, bytelen) |
| 3680 | regsub_T *sub; /* pointers to subexpressions */ |
| 3681 | int subidx; |
| 3682 | int *bytelen; /* out: length of match in bytes */ |
| 3683 | { |
| 3684 | int len; |
| 3685 | |
| 3686 | if (sub->in_use <= subidx) |
| 3687 | { |
| 3688 | retempty: |
| 3689 | /* backref was not set, match an empty string */ |
| 3690 | *bytelen = 0; |
| 3691 | return TRUE; |
| 3692 | } |
| 3693 | |
| 3694 | if (REG_MULTI) |
| 3695 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3696 | if (sub->list.multi[subidx].start.lnum < 0 |
| 3697 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3698 | goto retempty; |
| 3699 | /* TODO: line breaks */ |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3700 | len = sub->list.multi[subidx].end.col |
| 3701 | - sub->list.multi[subidx].start.col; |
| 3702 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3703 | reginput, &len) == 0) |
| 3704 | { |
| 3705 | *bytelen = len; |
| 3706 | return TRUE; |
| 3707 | } |
| 3708 | } |
| 3709 | else |
| 3710 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3711 | if (sub->list.line[subidx].start == NULL |
| 3712 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3713 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3714 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 3715 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3716 | { |
| 3717 | *bytelen = len; |
| 3718 | return TRUE; |
| 3719 | } |
| 3720 | } |
| 3721 | return FALSE; |
| 3722 | } |
| 3723 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3724 | #ifdef FEAT_SYN_HL |
| 3725 | |
| 3726 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 3727 | |
| 3728 | /* |
| 3729 | * Check for a match with \z subexpression "subidx". |
| 3730 | * Return TRUE if it matches. |
| 3731 | */ |
| 3732 | static int |
| 3733 | match_zref(subidx, bytelen) |
| 3734 | int subidx; |
| 3735 | int *bytelen; /* out: length of match in bytes */ |
| 3736 | { |
| 3737 | int len; |
| 3738 | |
| 3739 | cleanup_zsubexpr(); |
| 3740 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 3741 | { |
| 3742 | /* backref was not set, match an empty string */ |
| 3743 | *bytelen = 0; |
| 3744 | return TRUE; |
| 3745 | } |
| 3746 | |
| 3747 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 3748 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 3749 | { |
| 3750 | *bytelen = len; |
| 3751 | return TRUE; |
| 3752 | } |
| 3753 | return FALSE; |
| 3754 | } |
| 3755 | #endif |
| 3756 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3757 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3758 | * Save list IDs for all NFA states of "prog" into "list". |
| 3759 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3760 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3761 | */ |
| 3762 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3763 | nfa_save_listids(prog, list) |
| 3764 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3765 | int *list; |
| 3766 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3767 | int i; |
| 3768 | nfa_state_T *p; |
| 3769 | |
| 3770 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 3771 | p = &prog->state[0]; |
| 3772 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3773 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3774 | list[i] = p->lastlist[1]; |
| 3775 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3776 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3777 | } |
| 3778 | } |
| 3779 | |
| 3780 | /* |
| 3781 | * Restore list IDs from "list" to all NFA states. |
| 3782 | */ |
| 3783 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3784 | nfa_restore_listids(prog, list) |
| 3785 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3786 | int *list; |
| 3787 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3788 | int i; |
| 3789 | nfa_state_T *p; |
| 3790 | |
| 3791 | p = &prog->state[0]; |
| 3792 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3793 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3794 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3795 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3799 | static int |
| 3800 | nfa_re_num_cmp(val, op, pos) |
| 3801 | long_u val; |
| 3802 | int op; |
| 3803 | long_u pos; |
| 3804 | { |
| 3805 | if (op == 1) return pos > val; |
| 3806 | if (op == 2) return pos < val; |
| 3807 | return val == pos; |
| 3808 | } |
| 3809 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3810 | 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] | 3811 | 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] | 3812 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3813 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3814 | * Recursively call nfa_regmatch() |
| 3815 | */ |
| 3816 | static int |
| 3817 | recursive_regmatch(state, prog, submatch, m, listids) |
| 3818 | nfa_state_T *state; |
| 3819 | nfa_regprog_T *prog; |
| 3820 | regsubs_T *submatch; |
| 3821 | regsubs_T *m; |
| 3822 | int **listids; |
| 3823 | { |
| 3824 | char_u *save_reginput = reginput; |
| 3825 | char_u *save_regline = regline; |
| 3826 | int save_reglnum = reglnum; |
| 3827 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3828 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3829 | save_se_T *save_nfa_endp = nfa_endp; |
| 3830 | save_se_T endpos; |
| 3831 | save_se_T *endposp = NULL; |
| 3832 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3833 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3834 | |
| 3835 | if (state->c == NFA_START_INVISIBLE_BEFORE) |
| 3836 | { |
| 3837 | /* The recursive match must end at the current position. */ |
| 3838 | endposp = &endpos; |
| 3839 | if (REG_MULTI) |
| 3840 | { |
| 3841 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 3842 | endpos.se_u.pos.lnum = reglnum; |
| 3843 | } |
| 3844 | else |
| 3845 | endpos.se_u.ptr = reginput; |
| 3846 | |
| 3847 | /* Go back the specified number of bytes, or as far as the |
| 3848 | * start of the previous line, to try matching "\@<=" or |
| 3849 | * not matching "\@<!". |
| 3850 | * TODO: This is very inefficient! Would be better to |
| 3851 | * first check for a match with what follows. */ |
| 3852 | if (state->val <= 0) |
| 3853 | { |
| 3854 | if (REG_MULTI) |
| 3855 | { |
| 3856 | regline = reg_getline(--reglnum); |
| 3857 | if (regline == NULL) |
| 3858 | /* can't go before the first line */ |
| 3859 | regline = reg_getline(++reglnum); |
| 3860 | } |
| 3861 | reginput = regline; |
| 3862 | } |
| 3863 | else |
| 3864 | { |
| 3865 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 3866 | { |
| 3867 | /* Not enough bytes in this line, go to end of |
| 3868 | * previous line. */ |
| 3869 | regline = reg_getline(--reglnum); |
| 3870 | if (regline == NULL) |
| 3871 | { |
| 3872 | /* can't go before the first line */ |
| 3873 | regline = reg_getline(++reglnum); |
| 3874 | reginput = regline; |
| 3875 | } |
| 3876 | else |
| 3877 | reginput = regline + STRLEN(regline); |
| 3878 | } |
| 3879 | if ((int)(reginput - regline) >= state->val) |
| 3880 | { |
| 3881 | reginput -= state->val; |
| 3882 | #ifdef FEAT_MBYTE |
| 3883 | if (has_mbyte) |
| 3884 | reginput -= mb_head_off(regline, reginput); |
| 3885 | #endif |
| 3886 | } |
| 3887 | else |
| 3888 | reginput = regline; |
| 3889 | } |
| 3890 | } |
| 3891 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3892 | #ifdef ENABLE_LOG |
| 3893 | if (log_fd != stderr) |
| 3894 | fclose(log_fd); |
| 3895 | log_fd = NULL; |
| 3896 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3897 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 3898 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 3899 | if (nfa_ll_index == 1) |
| 3900 | { |
| 3901 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 3902 | * values and clear them. */ |
| 3903 | if (*listids == NULL) |
| 3904 | { |
| 3905 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 3906 | if (*listids == NULL) |
| 3907 | { |
| 3908 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 3909 | return 0; |
| 3910 | } |
| 3911 | } |
| 3912 | nfa_save_listids(prog, *listids); |
| 3913 | need_restore = TRUE; |
| 3914 | /* any value of nfa_listid will do */ |
| 3915 | } |
| 3916 | else |
| 3917 | { |
| 3918 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 3919 | * entry. Make sure nfa_listid is different from a previous recursive |
| 3920 | * call, because some states may still have this ID. */ |
| 3921 | ++nfa_ll_index; |
| 3922 | if (nfa_listid <= nfa_alt_listid) |
| 3923 | nfa_listid = nfa_alt_listid; |
| 3924 | } |
| 3925 | |
| 3926 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 3927 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3928 | nfa_endp = endposp; |
| 3929 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3930 | |
| 3931 | if (need_restore) |
| 3932 | nfa_restore_listids(prog, *listids); |
| 3933 | else |
| 3934 | { |
| 3935 | --nfa_ll_index; |
| 3936 | nfa_alt_listid = nfa_listid; |
| 3937 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3938 | |
| 3939 | /* restore position in input text */ |
| 3940 | reginput = save_reginput; |
| 3941 | regline = save_regline; |
| 3942 | reglnum = save_reglnum; |
| 3943 | nfa_match = save_nfa_match; |
| 3944 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3945 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 3946 | |
| 3947 | #ifdef ENABLE_LOG |
| 3948 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 3949 | if (log_fd != NULL) |
| 3950 | { |
| 3951 | fprintf(log_fd, "****************************\n"); |
| 3952 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 3953 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 3954 | fprintf(log_fd, "****************************\n"); |
| 3955 | } |
| 3956 | else |
| 3957 | { |
| 3958 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 3959 | log_fd = stderr; |
| 3960 | } |
| 3961 | #endif |
| 3962 | |
| 3963 | return result; |
| 3964 | } |
| 3965 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3966 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
| 3967 | |
| 3968 | /* |
| 3969 | * Estimate the chance of a match with "state" failing. |
| 3970 | * NFA_ANY: 1 |
| 3971 | * specific character: 99 |
| 3972 | */ |
| 3973 | static int |
| 3974 | failure_chance(state, depth) |
| 3975 | nfa_state_T *state; |
| 3976 | int depth; |
| 3977 | { |
| 3978 | int c = state->c; |
| 3979 | int l, r; |
| 3980 | |
| 3981 | /* detect looping */ |
| 3982 | if (depth > 4) |
| 3983 | return 1; |
| 3984 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 3985 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3986 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 3987 | case NFA_SPLIT: |
| 3988 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 3989 | /* avoid recursive stuff */ |
| 3990 | return 1; |
| 3991 | /* two alternatives, use the lowest failure chance */ |
| 3992 | l = failure_chance(state->out, depth + 1); |
| 3993 | r = failure_chance(state->out1, depth + 1); |
| 3994 | return l < r ? l : r; |
| 3995 | |
| 3996 | case NFA_ANY: |
| 3997 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3998 | return 1; |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 3999 | case NFA_MATCH: |
| 4000 | /* empty match works always */ |
| 4001 | return 0; |
| 4002 | |
| 4003 | case NFA_BOL: |
| 4004 | case NFA_EOL: |
| 4005 | case NFA_BOF: |
| 4006 | case NFA_EOF: |
| 4007 | case NFA_NEWL: |
| 4008 | return 99; |
| 4009 | |
| 4010 | case NFA_BOW: |
| 4011 | case NFA_EOW: |
| 4012 | return 90; |
| 4013 | |
| 4014 | case NFA_MOPEN: |
| 4015 | case NFA_MOPEN1: |
| 4016 | case NFA_MOPEN2: |
| 4017 | case NFA_MOPEN3: |
| 4018 | case NFA_MOPEN4: |
| 4019 | case NFA_MOPEN5: |
| 4020 | case NFA_MOPEN6: |
| 4021 | case NFA_MOPEN7: |
| 4022 | case NFA_MOPEN8: |
| 4023 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4024 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4025 | case NFA_ZOPEN: |
| 4026 | case NFA_ZOPEN1: |
| 4027 | case NFA_ZOPEN2: |
| 4028 | case NFA_ZOPEN3: |
| 4029 | case NFA_ZOPEN4: |
| 4030 | case NFA_ZOPEN5: |
| 4031 | case NFA_ZOPEN6: |
| 4032 | case NFA_ZOPEN7: |
| 4033 | case NFA_ZOPEN8: |
| 4034 | case NFA_ZOPEN9: |
| 4035 | case NFA_ZCLOSE: |
| 4036 | case NFA_ZCLOSE1: |
| 4037 | case NFA_ZCLOSE2: |
| 4038 | case NFA_ZCLOSE3: |
| 4039 | case NFA_ZCLOSE4: |
| 4040 | case NFA_ZCLOSE5: |
| 4041 | case NFA_ZCLOSE6: |
| 4042 | case NFA_ZCLOSE7: |
| 4043 | case NFA_ZCLOSE8: |
| 4044 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4045 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4046 | case NFA_NOPEN: |
| 4047 | case NFA_MCLOSE: |
| 4048 | case NFA_MCLOSE1: |
| 4049 | case NFA_MCLOSE2: |
| 4050 | case NFA_MCLOSE3: |
| 4051 | case NFA_MCLOSE4: |
| 4052 | case NFA_MCLOSE5: |
| 4053 | case NFA_MCLOSE6: |
| 4054 | case NFA_MCLOSE7: |
| 4055 | case NFA_MCLOSE8: |
| 4056 | case NFA_MCLOSE9: |
| 4057 | case NFA_NCLOSE: |
| 4058 | return failure_chance(state->out, depth + 1); |
| 4059 | |
| 4060 | case NFA_BACKREF1: |
| 4061 | case NFA_BACKREF2: |
| 4062 | case NFA_BACKREF3: |
| 4063 | case NFA_BACKREF4: |
| 4064 | case NFA_BACKREF5: |
| 4065 | case NFA_BACKREF6: |
| 4066 | case NFA_BACKREF7: |
| 4067 | case NFA_BACKREF8: |
| 4068 | case NFA_BACKREF9: |
| 4069 | #ifdef FEAT_SYN_HL |
| 4070 | case NFA_ZREF1: |
| 4071 | case NFA_ZREF2: |
| 4072 | case NFA_ZREF3: |
| 4073 | case NFA_ZREF4: |
| 4074 | case NFA_ZREF5: |
| 4075 | case NFA_ZREF6: |
| 4076 | case NFA_ZREF7: |
| 4077 | case NFA_ZREF8: |
| 4078 | case NFA_ZREF9: |
| 4079 | #endif |
| 4080 | /* backreferences don't match in many places */ |
| 4081 | return 94; |
| 4082 | |
| 4083 | case NFA_LNUM_GT: |
| 4084 | case NFA_LNUM_LT: |
| 4085 | case NFA_COL_GT: |
| 4086 | case NFA_COL_LT: |
| 4087 | case NFA_VCOL_GT: |
| 4088 | case NFA_VCOL_LT: |
| 4089 | case NFA_MARK_GT: |
| 4090 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4091 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4092 | /* before/after positions don't match very often */ |
| 4093 | return 85; |
| 4094 | |
| 4095 | case NFA_LNUM: |
| 4096 | return 90; |
| 4097 | |
| 4098 | case NFA_CURSOR: |
| 4099 | case NFA_COL: |
| 4100 | case NFA_VCOL: |
| 4101 | case NFA_MARK: |
| 4102 | /* specific positions rarely match */ |
| 4103 | return 98; |
| 4104 | |
| 4105 | case NFA_COMPOSING: |
| 4106 | return 95; |
| 4107 | |
| 4108 | default: |
| 4109 | if (c > 0) |
| 4110 | /* character match fails often */ |
| 4111 | return 95; |
| 4112 | } |
| 4113 | |
| 4114 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4115 | return 50; |
| 4116 | } |
| 4117 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4118 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4119 | * Main matching routine. |
| 4120 | * |
| 4121 | * Run NFA to determine whether it matches reginput. |
| 4122 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4123 | * 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] | 4124 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4125 | * Return TRUE if there is a match, FALSE otherwise. |
| 4126 | * Note: Caller must ensure that: start != NULL. |
| 4127 | */ |
| 4128 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4129 | nfa_regmatch(prog, start, submatch, m) |
| 4130 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4131 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4132 | regsubs_T *submatch; |
| 4133 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4134 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4135 | int result; |
| 4136 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4137 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4138 | int go_to_nextline = FALSE; |
| 4139 | nfa_thread_T *t; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4140 | nfa_list_T list[3]; |
| 4141 | nfa_list_T *listtbl[2][2]; |
| 4142 | nfa_list_T *ll; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4143 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4144 | nfa_list_T *thislist; |
| 4145 | nfa_list_T *nextlist; |
| 4146 | nfa_list_T *neglist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4147 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4148 | nfa_state_T *add_state; |
| 4149 | int add_count; |
| 4150 | int add_off; |
| 4151 | garray_T pimlist; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4152 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4153 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4154 | |
| 4155 | if (debug == NULL) |
| 4156 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4157 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4158 | return FALSE; |
| 4159 | } |
| 4160 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4161 | nfa_match = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4162 | ga_init2(&pimlist, sizeof(nfa_pim_T), 5); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4163 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4164 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4165 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4166 | list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE); |
| 4167 | list[0].len = nstate + 1; |
| 4168 | list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE); |
| 4169 | list[1].len = nstate + 1; |
| 4170 | list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE); |
| 4171 | list[2].len = nstate + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4172 | if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL) |
| 4173 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4174 | |
| 4175 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4176 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4177 | if (log_fd != NULL) |
| 4178 | { |
| 4179 | fprintf(log_fd, "**********************************\n"); |
| 4180 | nfa_set_code(start->c); |
| 4181 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 4182 | abs(start->id), code); |
| 4183 | fprintf(log_fd, "**********************************\n"); |
| 4184 | } |
| 4185 | else |
| 4186 | { |
| 4187 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4188 | log_fd = stderr; |
| 4189 | } |
| 4190 | #endif |
| 4191 | |
| 4192 | thislist = &list[0]; |
| 4193 | thislist->n = 0; |
| 4194 | nextlist = &list[1]; |
| 4195 | nextlist->n = 0; |
| 4196 | neglist = &list[2]; |
| 4197 | neglist->n = 0; |
| 4198 | #ifdef ENABLE_LOG |
| 4199 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 4200 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4201 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4202 | addstate(thislist, start, m, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4203 | |
| 4204 | /* There are two cases when the NFA advances: 1. input char matches the |
| 4205 | * NFA node and 2. input char does not match the NFA node, but the next |
| 4206 | * node is NFA_NOT. The following macro calls addstate() according to |
| 4207 | * these rules. It is used A LOT, so use the "listtbl" table for speed */ |
| 4208 | listtbl[0][0] = NULL; |
| 4209 | listtbl[0][1] = neglist; |
| 4210 | listtbl[1][0] = nextlist; |
| 4211 | listtbl[1][1] = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4212 | #define ADD_POS_NEG_STATE(state) \ |
| 4213 | ll = listtbl[result ? 1 : 0][state->negated]; \ |
| 4214 | if (ll != NULL) { \ |
| 4215 | add_state = state->out; \ |
| 4216 | add_off = clen; \ |
| 4217 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4218 | |
| 4219 | /* |
| 4220 | * Run for each character. |
| 4221 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4222 | for (;;) |
| 4223 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4224 | int curc; |
| 4225 | int clen; |
| 4226 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4227 | #ifdef FEAT_MBYTE |
| 4228 | if (has_mbyte) |
| 4229 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4230 | curc = (*mb_ptr2char)(reginput); |
| 4231 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4232 | } |
| 4233 | else |
| 4234 | #endif |
| 4235 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4236 | curc = *reginput; |
| 4237 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4238 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4239 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4240 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4241 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4242 | go_to_nextline = FALSE; |
| 4243 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4244 | |
| 4245 | /* swap lists */ |
| 4246 | thislist = &list[flag]; |
| 4247 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4248 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4249 | listtbl[1][0] = nextlist; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4250 | ++nfa_listid; |
| 4251 | thislist->id = nfa_listid; |
| 4252 | nextlist->id = nfa_listid + 1; |
| 4253 | neglist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4254 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4255 | pimlist.ga_len = 0; |
| 4256 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4257 | #ifdef ENABLE_LOG |
| 4258 | fprintf(log_fd, "------------------------------------------\n"); |
| 4259 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4260 | 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] | 4261 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4262 | { |
| 4263 | int i; |
| 4264 | |
| 4265 | for (i = 0; i < thislist->n; i++) |
| 4266 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 4267 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4268 | fprintf(log_fd, "\n"); |
| 4269 | #endif |
| 4270 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4271 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4272 | fprintf(debug, "\n-------------------\n"); |
| 4273 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 4274 | /* |
| 4275 | * If the state lists are empty we can stop. |
| 4276 | */ |
| 4277 | if (thislist->n == 0 && neglist->n == 0) |
| 4278 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4279 | |
| 4280 | /* compute nextlist */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4281 | for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4282 | { |
| 4283 | if (neglist->n > 0) |
| 4284 | { |
| 4285 | t = &neglist->t[0]; |
Bram Moolenaar | 0fabe3f | 2013-05-21 12:34:17 +0200 | [diff] [blame] | 4286 | neglist->n--; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4287 | listidx--; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4288 | } |
| 4289 | else |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4290 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4291 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4292 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4293 | nfa_set_code(t->state->c); |
| 4294 | fprintf(debug, "%s, ", code); |
| 4295 | #endif |
| 4296 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4297 | { |
| 4298 | int col; |
| 4299 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4300 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4301 | col = -1; |
| 4302 | else if (REG_MULTI) |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4303 | col = t->subs.norm.list.multi[0].start.col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4304 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4305 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4306 | nfa_set_code(t->state->c); |
| 4307 | fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n", |
| 4308 | abs(t->state->id), (int)t->state->c, code, col); |
| 4309 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4310 | #endif |
| 4311 | |
| 4312 | /* |
| 4313 | * Handle the possible codes of the current state. |
| 4314 | * The most important is NFA_MATCH. |
| 4315 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4316 | add_state = NULL; |
| 4317 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4318 | switch (t->state->c) |
| 4319 | { |
| 4320 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 4321 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4322 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4323 | copy_sub(&submatch->norm, &t->subs.norm); |
| 4324 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4325 | if (nfa_has_zsubexpr) |
| 4326 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4327 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4328 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4329 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4330 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4331 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4332 | * states at this position. When the list of states is going |
| 4333 | * to be empty quit without advancing, so that "reginput" is |
| 4334 | * correct. */ |
| 4335 | if (nextlist->n == 0 && neglist->n == 0) |
| 4336 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4337 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 4338 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4339 | |
| 4340 | case NFA_END_INVISIBLE: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4341 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4342 | /* |
| 4343 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4344 | * NFA_START_INVISIBLE_BEFORE node. |
| 4345 | * They surround a zero-width group, used with "\@=", "\&", |
| 4346 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4347 | * If we got here, it means that the current "invisible" group |
| 4348 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4349 | * nfa_regmatch(). For a look-behind match only when it ends |
| 4350 | * in the position in "nfa_endp". |
| 4351 | * Submatches are stored in *m, and used in the parent call. |
| 4352 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4353 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4354 | if (nfa_endp != NULL) |
| 4355 | { |
| 4356 | if (REG_MULTI) |
| 4357 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 4358 | (int)reglnum, |
| 4359 | (int)nfa_endp->se_u.pos.lnum, |
| 4360 | (int)(reginput - regline), |
| 4361 | nfa_endp->se_u.pos.col); |
| 4362 | else |
| 4363 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 4364 | (int)(reginput - regline), |
| 4365 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4366 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4367 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4368 | /* If "nfa_endp" is set it's only a match if it ends at |
| 4369 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4370 | if (nfa_endp != NULL && (REG_MULTI |
| 4371 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 4372 | || (int)(reginput - regline) |
| 4373 | != nfa_endp->se_u.pos.col) |
| 4374 | : reginput != nfa_endp->se_u.ptr)) |
| 4375 | break; |
| 4376 | |
| 4377 | /* do not set submatches for \@! */ |
| 4378 | if (!t->state->negated) |
| 4379 | { |
| 4380 | copy_sub(&m->norm, &t->subs.norm); |
| 4381 | #ifdef FEAT_SYN_HL |
| 4382 | if (nfa_has_zsubexpr) |
| 4383 | copy_sub(&m->synt, &t->subs.synt); |
| 4384 | #endif |
| 4385 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4386 | #ifdef ENABLE_LOG |
| 4387 | fprintf(log_fd, "Match found:\n"); |
| 4388 | log_subsexpr(m); |
| 4389 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4390 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4391 | break; |
| 4392 | |
| 4393 | case NFA_START_INVISIBLE: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4394 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4395 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4396 | nfa_pim_T *pim; |
| 4397 | int cout = t->state->out1->out->c; |
| 4398 | |
| 4399 | /* Do it directly when what follows is possibly end of |
| 4400 | * match (closing paren). |
| 4401 | * Postpone when it is \@<= or \@<!, these are expensive. |
| 4402 | * TODO: remove the check for t->pim and check multiple |
| 4403 | * where it's used? |
| 4404 | * Otherwise first do the one that has the highest chance |
| 4405 | * of failing. */ |
| 4406 | if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9) |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4407 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4408 | || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9) |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4409 | #endif |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4410 | || cout == NFA_NCLOSE |
| 4411 | || t->pim != NULL |
| 4412 | || (t->state->c != NFA_START_INVISIBLE_BEFORE |
| 4413 | && failure_chance(t->state->out1->out, 0) |
| 4414 | < failure_chance(t->state->out, 0))) |
| 4415 | { |
| 4416 | /* |
| 4417 | * First try matching the invisible match, then what |
| 4418 | * follows. |
| 4419 | */ |
| 4420 | result = recursive_regmatch(t->state, prog, |
| 4421 | submatch, m, &listids); |
| 4422 | |
| 4423 | /* for \@! it is a match when result is FALSE */ |
| 4424 | if (result != t->state->negated) |
| 4425 | { |
| 4426 | /* Copy submatch info from the recursive call */ |
| 4427 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4428 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4429 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4430 | #endif |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4431 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4432 | /* t->state->out1 is the corresponding |
| 4433 | * END_INVISIBLE node; Add its out to the current |
| 4434 | * list (zero-width match). */ |
| 4435 | addstate_here(thislist, t->state->out1->out, |
| 4436 | &t->subs, t->pim, &listidx); |
| 4437 | } |
| 4438 | } |
| 4439 | else |
| 4440 | { |
| 4441 | /* |
| 4442 | * First try matching what follows at the current |
| 4443 | * position. Only if a match is found, addstate() is |
| 4444 | * called, then verify the invisible match matches. |
| 4445 | * Add a nfa_pim_T to the following states, it |
| 4446 | * contains info about the invisible match. |
| 4447 | */ |
| 4448 | if (ga_grow(&pimlist, 1) == FAIL) |
| 4449 | goto theend; |
| 4450 | pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len; |
| 4451 | ++pimlist.ga_len; |
| 4452 | pim->state = t->state; |
| 4453 | pim->pim = NULL; |
| 4454 | pim->result = NFA_PIM_TODO; |
| 4455 | |
| 4456 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 4457 | * node; Add its out to the current list (zero-width |
| 4458 | * match). */ |
| 4459 | addstate_here(thislist, t->state->out1->out, &t->subs, |
| 4460 | pim, &listidx); |
| 4461 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4462 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4463 | break; |
| 4464 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4465 | case NFA_START_PATTERN: |
| 4466 | /* First try matching the pattern. */ |
| 4467 | result = recursive_regmatch(t->state, prog, |
| 4468 | submatch, m, &listids); |
| 4469 | if (result) |
| 4470 | { |
| 4471 | int bytelen; |
| 4472 | |
| 4473 | #ifdef ENABLE_LOG |
| 4474 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 4475 | log_subsexpr(m); |
| 4476 | #endif |
| 4477 | /* Copy submatch info from the recursive call */ |
| 4478 | copy_sub_off(&t->subs.norm, &m->norm); |
| 4479 | #ifdef FEAT_SYN_HL |
| 4480 | copy_sub_off(&t->subs.synt, &m->synt); |
| 4481 | #endif |
| 4482 | /* Now we need to skip over the matched text and then |
| 4483 | * continue with what follows. */ |
| 4484 | if (REG_MULTI) |
| 4485 | /* TODO: multi-line match */ |
| 4486 | bytelen = m->norm.list.multi[0].end.col |
| 4487 | - (int)(reginput - regline); |
| 4488 | else |
| 4489 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 4490 | |
| 4491 | #ifdef ENABLE_LOG |
| 4492 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 4493 | #endif |
| 4494 | if (bytelen == 0) |
| 4495 | { |
| 4496 | /* empty match, output of corresponding |
| 4497 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 4498 | * position */ |
| 4499 | addstate_here(thislist, t->state->out1->out->out, |
| 4500 | &t->subs, t->pim, &listidx); |
| 4501 | } |
| 4502 | else if (bytelen <= clen) |
| 4503 | { |
| 4504 | /* match current character, output of corresponding |
| 4505 | * NFA_END_PATTERN to be used at next position. */ |
| 4506 | ll = nextlist; |
| 4507 | add_state = t->state->out1->out->out; |
| 4508 | add_off = clen; |
| 4509 | } |
| 4510 | else |
| 4511 | { |
| 4512 | /* skip over the matched characters, set character |
| 4513 | * count in NFA_SKIP */ |
| 4514 | ll = nextlist; |
| 4515 | add_state = t->state->out1->out; |
| 4516 | add_off = bytelen; |
| 4517 | add_count = bytelen - clen; |
| 4518 | } |
| 4519 | } |
| 4520 | break; |
| 4521 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4522 | case NFA_BOL: |
| 4523 | if (reginput == regline) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4524 | addstate_here(thislist, t->state->out, &t->subs, |
| 4525 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4526 | break; |
| 4527 | |
| 4528 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4529 | if (curc == NUL) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4530 | addstate_here(thislist, t->state->out, &t->subs, |
| 4531 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4532 | break; |
| 4533 | |
| 4534 | case NFA_BOW: |
| 4535 | { |
| 4536 | int bow = TRUE; |
| 4537 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4538 | if (curc == NUL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4539 | bow = FALSE; |
| 4540 | #ifdef FEAT_MBYTE |
| 4541 | else if (has_mbyte) |
| 4542 | { |
| 4543 | int this_class; |
| 4544 | |
| 4545 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4546 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4547 | if (this_class <= 1) |
| 4548 | bow = FALSE; |
| 4549 | else if (reg_prev_class() == this_class) |
| 4550 | bow = FALSE; |
| 4551 | } |
| 4552 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4553 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4554 | || (reginput > regline |
| 4555 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4556 | bow = FALSE; |
| 4557 | if (bow) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4558 | addstate_here(thislist, t->state->out, &t->subs, |
| 4559 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4560 | break; |
| 4561 | } |
| 4562 | |
| 4563 | case NFA_EOW: |
| 4564 | { |
| 4565 | int eow = TRUE; |
| 4566 | |
| 4567 | if (reginput == regline) |
| 4568 | eow = FALSE; |
| 4569 | #ifdef FEAT_MBYTE |
| 4570 | else if (has_mbyte) |
| 4571 | { |
| 4572 | int this_class, prev_class; |
| 4573 | |
| 4574 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4575 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4576 | prev_class = reg_prev_class(); |
| 4577 | if (this_class == prev_class |
| 4578 | || prev_class == 0 || prev_class == 1) |
| 4579 | eow = FALSE; |
| 4580 | } |
| 4581 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4582 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4583 | || (reginput[0] != NUL |
| 4584 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4585 | eow = FALSE; |
| 4586 | if (eow) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4587 | addstate_here(thislist, t->state->out, &t->subs, |
| 4588 | t->pim, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4589 | break; |
| 4590 | } |
| 4591 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 4592 | case NFA_BOF: |
| 4593 | if (reglnum == 0 && reginput == regline |
| 4594 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4595 | addstate_here(thislist, t->state->out, &t->subs, |
| 4596 | t->pim, &listidx); |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 4597 | break; |
| 4598 | |
| 4599 | case NFA_EOF: |
| 4600 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4601 | addstate_here(thislist, t->state->out, &t->subs, |
| 4602 | t->pim, &listidx); |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 4603 | break; |
| 4604 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4605 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4606 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4607 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4608 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4609 | int len = 0; |
| 4610 | nfa_state_T *end; |
| 4611 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4612 | int cchars[MAX_MCO]; |
| 4613 | int ccount = 0; |
| 4614 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4615 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4616 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4617 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 4618 | if (utf_iscomposing(sta->c)) |
| 4619 | { |
| 4620 | /* Only match composing character(s), ignore base |
| 4621 | * character. Used for ".{composing}" and "{composing}" |
| 4622 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4623 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 4624 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 4625 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4626 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 4627 | /* If \Z was present, then ignore composing characters. |
| 4628 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4629 | /* TODO: How about negated? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4630 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4631 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4632 | else |
| 4633 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4634 | while (sta->c != NFA_END_COMPOSING) |
| 4635 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4636 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4637 | |
| 4638 | /* Check base character matches first, unless ignored. */ |
| 4639 | else if (len > 0 || mc == sta->c) |
| 4640 | { |
| 4641 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4642 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 4643 | len += mb_char2len(mc); |
| 4644 | sta = sta->out; |
| 4645 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4646 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4647 | /* We don't care about the order of composing characters. |
| 4648 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4649 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4650 | { |
| 4651 | mc = mb_ptr2char(reginput + len); |
| 4652 | cchars[ccount++] = mc; |
| 4653 | len += mb_char2len(mc); |
| 4654 | if (ccount == MAX_MCO) |
| 4655 | break; |
| 4656 | } |
| 4657 | |
| 4658 | /* Check that each composing char in the pattern matches a |
| 4659 | * composing char in the text. We do not check if all |
| 4660 | * composing chars are matched. */ |
| 4661 | result = OK; |
| 4662 | while (sta->c != NFA_END_COMPOSING) |
| 4663 | { |
| 4664 | for (j = 0; j < ccount; ++j) |
| 4665 | if (cchars[j] == sta->c) |
| 4666 | break; |
| 4667 | if (j == ccount) |
| 4668 | { |
| 4669 | result = FAIL; |
| 4670 | break; |
| 4671 | } |
| 4672 | sta = sta->out; |
| 4673 | } |
| 4674 | } |
| 4675 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 4676 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 4677 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4678 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4679 | ADD_POS_NEG_STATE(end); |
| 4680 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4681 | } |
| 4682 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4683 | |
| 4684 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4685 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 4686 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4687 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4688 | go_to_nextline = TRUE; |
| 4689 | /* Pass -1 for the offset, which means taking the position |
| 4690 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4691 | ll = nextlist; |
| 4692 | add_state = t->state->out; |
| 4693 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4694 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4695 | else if (curc == '\n' && reg_line_lbr) |
| 4696 | { |
| 4697 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4698 | ll = nextlist; |
| 4699 | add_state = t->state->out; |
| 4700 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4701 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4702 | break; |
| 4703 | |
| 4704 | case NFA_CLASS_ALNUM: |
| 4705 | case NFA_CLASS_ALPHA: |
| 4706 | case NFA_CLASS_BLANK: |
| 4707 | case NFA_CLASS_CNTRL: |
| 4708 | case NFA_CLASS_DIGIT: |
| 4709 | case NFA_CLASS_GRAPH: |
| 4710 | case NFA_CLASS_LOWER: |
| 4711 | case NFA_CLASS_PRINT: |
| 4712 | case NFA_CLASS_PUNCT: |
| 4713 | case NFA_CLASS_SPACE: |
| 4714 | case NFA_CLASS_UPPER: |
| 4715 | case NFA_CLASS_XDIGIT: |
| 4716 | case NFA_CLASS_TAB: |
| 4717 | case NFA_CLASS_RETURN: |
| 4718 | case NFA_CLASS_BACKSPACE: |
| 4719 | case NFA_CLASS_ESCAPE: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4720 | result = check_char_class(t->state->c, curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4721 | ADD_POS_NEG_STATE(t->state); |
| 4722 | break; |
| 4723 | |
| 4724 | case NFA_END_NEG_RANGE: |
| 4725 | /* This follows a series of negated nodes, like: |
| 4726 | * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4727 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4728 | { |
| 4729 | ll = nextlist; |
| 4730 | add_state = t->state->out; |
| 4731 | add_off = clen; |
| 4732 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4733 | break; |
| 4734 | |
| 4735 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4736 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4737 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4738 | { |
| 4739 | ll = nextlist; |
| 4740 | add_state = t->state->out; |
| 4741 | add_off = clen; |
| 4742 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4743 | break; |
| 4744 | |
| 4745 | /* |
| 4746 | * Character classes like \a for alpha, \d for digit etc. |
| 4747 | */ |
| 4748 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4749 | result = vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4750 | ADD_POS_NEG_STATE(t->state); |
| 4751 | break; |
| 4752 | |
| 4753 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4754 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4755 | ADD_POS_NEG_STATE(t->state); |
| 4756 | break; |
| 4757 | |
| 4758 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4759 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4760 | ADD_POS_NEG_STATE(t->state); |
| 4761 | break; |
| 4762 | |
| 4763 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4764 | result = !VIM_ISDIGIT(curc) |
| 4765 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4766 | ADD_POS_NEG_STATE(t->state); |
| 4767 | break; |
| 4768 | |
| 4769 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4770 | result = vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4771 | ADD_POS_NEG_STATE(t->state); |
| 4772 | break; |
| 4773 | |
| 4774 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4775 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4776 | ADD_POS_NEG_STATE(t->state); |
| 4777 | break; |
| 4778 | |
| 4779 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | 0805049 | 2013-05-21 12:43:56 +0200 | [diff] [blame] | 4780 | result = ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4781 | ADD_POS_NEG_STATE(t->state); |
| 4782 | break; |
| 4783 | |
| 4784 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4785 | result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4786 | ADD_POS_NEG_STATE(t->state); |
| 4787 | break; |
| 4788 | |
| 4789 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4790 | result = vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4791 | ADD_POS_NEG_STATE(t->state); |
| 4792 | break; |
| 4793 | |
| 4794 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4795 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4796 | ADD_POS_NEG_STATE(t->state); |
| 4797 | break; |
| 4798 | |
| 4799 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4800 | result = ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4801 | ADD_POS_NEG_STATE(t->state); |
| 4802 | break; |
| 4803 | |
| 4804 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4805 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4806 | ADD_POS_NEG_STATE(t->state); |
| 4807 | break; |
| 4808 | |
| 4809 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4810 | result = ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4811 | ADD_POS_NEG_STATE(t->state); |
| 4812 | break; |
| 4813 | |
| 4814 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4815 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4816 | ADD_POS_NEG_STATE(t->state); |
| 4817 | break; |
| 4818 | |
| 4819 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4820 | result = ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4821 | ADD_POS_NEG_STATE(t->state); |
| 4822 | break; |
| 4823 | |
| 4824 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4825 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4826 | ADD_POS_NEG_STATE(t->state); |
| 4827 | break; |
| 4828 | |
| 4829 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4830 | result = ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4831 | ADD_POS_NEG_STATE(t->state); |
| 4832 | break; |
| 4833 | |
| 4834 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4835 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4836 | ADD_POS_NEG_STATE(t->state); |
| 4837 | break; |
| 4838 | |
| 4839 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4840 | result = ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4841 | ADD_POS_NEG_STATE(t->state); |
| 4842 | break; |
| 4843 | |
| 4844 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4845 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4846 | ADD_POS_NEG_STATE(t->state); |
| 4847 | break; |
| 4848 | |
| 4849 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4850 | result = ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4851 | ADD_POS_NEG_STATE(t->state); |
| 4852 | break; |
| 4853 | |
| 4854 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4855 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4856 | ADD_POS_NEG_STATE(t->state); |
| 4857 | break; |
| 4858 | |
| 4859 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4860 | result = ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4861 | ADD_POS_NEG_STATE(t->state); |
| 4862 | break; |
| 4863 | |
| 4864 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4865 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4866 | ADD_POS_NEG_STATE(t->state); |
| 4867 | break; |
| 4868 | |
| 4869 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4870 | result = ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4871 | ADD_POS_NEG_STATE(t->state); |
| 4872 | break; |
| 4873 | |
| 4874 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4875 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4876 | ADD_POS_NEG_STATE(t->state); |
| 4877 | break; |
| 4878 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4879 | case NFA_BACKREF1: |
| 4880 | case NFA_BACKREF2: |
| 4881 | case NFA_BACKREF3: |
| 4882 | case NFA_BACKREF4: |
| 4883 | case NFA_BACKREF5: |
| 4884 | case NFA_BACKREF6: |
| 4885 | case NFA_BACKREF7: |
| 4886 | case NFA_BACKREF8: |
| 4887 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4888 | #ifdef FEAT_SYN_HL |
| 4889 | case NFA_ZREF1: |
| 4890 | case NFA_ZREF2: |
| 4891 | case NFA_ZREF3: |
| 4892 | case NFA_ZREF4: |
| 4893 | case NFA_ZREF5: |
| 4894 | case NFA_ZREF6: |
| 4895 | case NFA_ZREF7: |
| 4896 | case NFA_ZREF8: |
| 4897 | case NFA_ZREF9: |
| 4898 | #endif |
| 4899 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4900 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4901 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4902 | int bytelen; |
| 4903 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4904 | if (t->state->c <= NFA_BACKREF9) |
| 4905 | { |
| 4906 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 4907 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 4908 | } |
| 4909 | #ifdef FEAT_SYN_HL |
| 4910 | else |
| 4911 | { |
| 4912 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 4913 | result = match_zref(subidx, &bytelen); |
| 4914 | } |
| 4915 | #endif |
| 4916 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4917 | if (result) |
| 4918 | { |
| 4919 | if (bytelen == 0) |
| 4920 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 4921 | /* empty match always works, output of NFA_SKIP to be |
| 4922 | * used next */ |
| 4923 | addstate_here(thislist, t->state->out->out, &t->subs, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4924 | t->pim, &listidx); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4925 | } |
| 4926 | else if (bytelen <= clen) |
| 4927 | { |
| 4928 | /* match current character, jump ahead to out of |
| 4929 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4930 | ll = nextlist; |
| 4931 | add_state = t->state->out->out; |
| 4932 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4933 | } |
| 4934 | else |
| 4935 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 4936 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4937 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4938 | ll = nextlist; |
| 4939 | add_state = t->state->out; |
| 4940 | add_off = bytelen; |
| 4941 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4942 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4943 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 4944 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4945 | } |
| 4946 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4947 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4948 | if (t->count - clen <= 0) |
| 4949 | { |
| 4950 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4951 | ll = nextlist; |
| 4952 | add_state = t->state->out; |
| 4953 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4954 | } |
| 4955 | else |
| 4956 | { |
| 4957 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4958 | ll = nextlist; |
| 4959 | add_state = t->state; |
| 4960 | add_off = 0; |
| 4961 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4962 | } |
| 4963 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 4964 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4965 | case NFA_LNUM: |
| 4966 | case NFA_LNUM_GT: |
| 4967 | case NFA_LNUM_LT: |
| 4968 | result = (REG_MULTI && |
| 4969 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 4970 | (long_u)(reglnum + reg_firstlnum))); |
| 4971 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4972 | addstate_here(thislist, t->state->out, &t->subs, |
| 4973 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4974 | break; |
| 4975 | |
| 4976 | case NFA_COL: |
| 4977 | case NFA_COL_GT: |
| 4978 | case NFA_COL_LT: |
| 4979 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 4980 | (long_u)(reginput - regline) + 1); |
| 4981 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4982 | addstate_here(thislist, t->state->out, &t->subs, |
| 4983 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4984 | break; |
| 4985 | |
| 4986 | case NFA_VCOL: |
| 4987 | case NFA_VCOL_GT: |
| 4988 | case NFA_VCOL_LT: |
| 4989 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 4990 | (long_u)win_linetabsize( |
| 4991 | reg_win == NULL ? curwin : reg_win, |
| 4992 | regline, (colnr_T)(reginput - regline)) + 1); |
| 4993 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4994 | addstate_here(thislist, t->state->out, &t->subs, |
| 4995 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4996 | break; |
| 4997 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 4998 | case NFA_MARK: |
| 4999 | case NFA_MARK_GT: |
| 5000 | case NFA_MARK_LT: |
| 5001 | { |
| 5002 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 5003 | |
| 5004 | /* Compare the mark position to the match position. */ |
| 5005 | result = (pos != NULL /* mark doesn't exist */ |
| 5006 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 5007 | && (pos->lnum == reglnum + reg_firstlnum |
| 5008 | ? (pos->col == (colnr_T)(reginput - regline) |
| 5009 | ? t->state->c == NFA_MARK |
| 5010 | : (pos->col < (colnr_T)(reginput - regline) |
| 5011 | ? t->state->c == NFA_MARK_GT |
| 5012 | : t->state->c == NFA_MARK_LT)) |
| 5013 | : (pos->lnum < reglnum + reg_firstlnum |
| 5014 | ? t->state->c == NFA_MARK_GT |
| 5015 | : t->state->c == NFA_MARK_LT))); |
| 5016 | if (result) |
| 5017 | addstate_here(thislist, t->state->out, &t->subs, |
| 5018 | t->pim, &listidx); |
| 5019 | break; |
| 5020 | } |
| 5021 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5022 | case NFA_CURSOR: |
| 5023 | result = (reg_win != NULL |
| 5024 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 5025 | && ((colnr_T)(reginput - regline) |
| 5026 | == reg_win->w_cursor.col)); |
| 5027 | if (result) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5028 | addstate_here(thislist, t->state->out, &t->subs, |
| 5029 | t->pim, &listidx); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5030 | break; |
| 5031 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5032 | case NFA_VISUAL: |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5033 | #ifdef FEAT_VISUAL |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5034 | result = reg_match_visual(); |
| 5035 | if (result) |
| 5036 | addstate_here(thislist, t->state->out, &t->subs, |
| 5037 | t->pim, &listidx); |
Bram Moolenaar | 78eae9a | 2013-06-05 11:02:05 +0200 | [diff] [blame] | 5038 | #endif |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5039 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5040 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5041 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5042 | { |
| 5043 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5044 | |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5045 | /* TODO: put this in #ifdef later */ |
| 5046 | if (c < -256) |
| 5047 | EMSGN("INTERNAL: Negative state char: %ld", c); |
| 5048 | if (is_Magic(c)) |
| 5049 | c = un_Magic(c); |
| 5050 | result = (c == curc); |
| 5051 | |
| 5052 | if (!result && ireg_ic) |
| 5053 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5054 | #ifdef FEAT_MBYTE |
| 5055 | /* If there is a composing character which is not being |
| 5056 | * ignored there can be no match. Match with composing |
| 5057 | * character uses NFA_COMPOSING above. */ |
| 5058 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5059 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5060 | result = FALSE; |
| 5061 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5062 | ADD_POS_NEG_STATE(t->state); |
| 5063 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5064 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5065 | |
| 5066 | } /* switch (t->state->c) */ |
| 5067 | |
| 5068 | if (add_state != NULL) |
| 5069 | { |
| 5070 | if (t->pim != NULL) |
| 5071 | { |
| 5072 | /* postponed invisible match */ |
| 5073 | /* TODO: also do t->pim->pim recursively? */ |
| 5074 | if (t->pim->result == NFA_PIM_TODO) |
| 5075 | { |
| 5076 | #ifdef ENABLE_LOG |
| 5077 | fprintf(log_fd, "\n"); |
| 5078 | fprintf(log_fd, "==================================\n"); |
| 5079 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 5080 | fprintf(log_fd, "\n"); |
| 5081 | #endif |
| 5082 | result = recursive_regmatch(t->pim->state, |
| 5083 | prog, submatch, m, &listids); |
| 5084 | t->pim->result = result ? NFA_PIM_MATCH |
| 5085 | : NFA_PIM_NOMATCH; |
| 5086 | /* for \@! it is a match when result is FALSE */ |
| 5087 | if (result != t->pim->state->negated) |
| 5088 | { |
| 5089 | /* Copy submatch info from the recursive call */ |
| 5090 | copy_sub_off(&t->pim->subs.norm, &m->norm); |
| 5091 | #ifdef FEAT_SYN_HL |
| 5092 | copy_sub_off(&t->pim->subs.synt, &m->synt); |
| 5093 | #endif |
| 5094 | } |
| 5095 | } |
| 5096 | else |
| 5097 | { |
| 5098 | result = (t->pim->result == NFA_PIM_MATCH); |
| 5099 | #ifdef ENABLE_LOG |
| 5100 | fprintf(log_fd, "\n"); |
| 5101 | fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result); |
| 5102 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5103 | fprintf(log_fd, "\n"); |
| 5104 | #endif |
| 5105 | } |
| 5106 | |
| 5107 | /* for \@! it is a match when result is FALSE */ |
| 5108 | if (result != t->pim->state->negated) |
| 5109 | { |
| 5110 | /* Copy submatch info from the recursive call */ |
| 5111 | copy_sub_off(&t->subs.norm, &t->pim->subs.norm); |
| 5112 | #ifdef FEAT_SYN_HL |
| 5113 | copy_sub_off(&t->subs.synt, &t->pim->subs.synt); |
| 5114 | #endif |
| 5115 | } |
| 5116 | else |
| 5117 | /* look-behind match failed, don't add the state */ |
| 5118 | continue; |
| 5119 | } |
| 5120 | |
| 5121 | addstate(ll, add_state, &t->subs, add_off); |
| 5122 | if (add_count > 0) |
| 5123 | nextlist->t[ll->n - 1].count = add_count; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5124 | } |
| 5125 | |
| 5126 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 5127 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 5128 | /* Look for the start of a match in the current position by adding the |
| 5129 | * start state to the list of states. |
| 5130 | * The first found match is the leftmost one, thus the order of states |
| 5131 | * matters! |
| 5132 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 5133 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5134 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 5135 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5136 | if (nfa_match == FALSE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5137 | && ((start->c == NFA_MOPEN |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5138 | && reglnum == 0 |
| 5139 | && clen != 0 |
| 5140 | && (ireg_maxcol == 0 |
| 5141 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5142 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5143 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5144 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 5145 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5146 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5147 | < nfa_endp->se_u.pos.col)) |
| 5148 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5149 | { |
| 5150 | #ifdef ENABLE_LOG |
| 5151 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 5152 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5153 | addstate(nextlist, start, m, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5154 | } |
| 5155 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5156 | #ifdef ENABLE_LOG |
| 5157 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5158 | { |
| 5159 | int i; |
| 5160 | |
| 5161 | for (i = 0; i < thislist->n; i++) |
| 5162 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5163 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5164 | fprintf(log_fd, "\n"); |
| 5165 | #endif |
| 5166 | |
| 5167 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5168 | /* Advance to the next character, or advance to the next line, or |
| 5169 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5170 | if (clen != 0) |
| 5171 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5172 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 5173 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5174 | reg_nextline(); |
| 5175 | else |
| 5176 | break; |
| 5177 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5178 | |
| 5179 | #ifdef ENABLE_LOG |
| 5180 | if (log_fd != stderr) |
| 5181 | fclose(log_fd); |
| 5182 | log_fd = NULL; |
| 5183 | #endif |
| 5184 | |
| 5185 | theend: |
| 5186 | /* Free memory */ |
| 5187 | vim_free(list[0].t); |
| 5188 | vim_free(list[1].t); |
| 5189 | vim_free(list[2].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5190 | vim_free(listids); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5191 | ga_clear(&pimlist); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5192 | #undef ADD_POS_NEG_STATE |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5193 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5194 | fclose(debug); |
| 5195 | #endif |
| 5196 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5197 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | /* |
| 5201 | * Try match of "prog" with at regline["col"]. |
| 5202 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 5203 | */ |
| 5204 | static long |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5205 | nfa_regtry(prog, col) |
| 5206 | nfa_regprog_T *prog; |
| 5207 | colnr_T col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5208 | { |
| 5209 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5210 | regsubs_T subs, m; |
| 5211 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5212 | #ifdef ENABLE_LOG |
| 5213 | FILE *f; |
| 5214 | #endif |
| 5215 | |
| 5216 | reginput = regline + col; |
| 5217 | need_clear_subexpr = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5218 | #ifdef FEAT_SYN_HL |
| 5219 | /* Clear the external match subpointers if necessary. */ |
| 5220 | if (prog->reghasz == REX_SET) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5221 | { |
| 5222 | nfa_has_zsubexpr = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5223 | need_clear_zsubexpr = TRUE; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5224 | } |
| 5225 | else |
| 5226 | nfa_has_zsubexpr = FALSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5227 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5228 | |
| 5229 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5230 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5231 | if (f != NULL) |
| 5232 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5233 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5234 | #ifdef DEBUG |
| 5235 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 5236 | #endif |
| 5237 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5238 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 5239 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5240 | fprintf(f, "\n\n"); |
| 5241 | fclose(f); |
| 5242 | } |
| 5243 | else |
| 5244 | EMSG(_("Could not open temporary log file for writing ")); |
| 5245 | #endif |
| 5246 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5247 | clear_sub(&subs.norm); |
| 5248 | clear_sub(&m.norm); |
| 5249 | #ifdef FEAT_SYN_HL |
| 5250 | clear_sub(&subs.synt); |
| 5251 | clear_sub(&m.synt); |
| 5252 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5253 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5254 | if (nfa_regmatch(prog, start, &subs, &m) == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5255 | return 0; |
| 5256 | |
| 5257 | cleanup_subexpr(); |
| 5258 | if (REG_MULTI) |
| 5259 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5260 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5261 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5262 | reg_startpos[i] = subs.norm.list.multi[i].start; |
| 5263 | reg_endpos[i] = subs.norm.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5264 | } |
| 5265 | |
| 5266 | if (reg_startpos[0].lnum < 0) |
| 5267 | { |
| 5268 | reg_startpos[0].lnum = 0; |
| 5269 | reg_startpos[0].col = col; |
| 5270 | } |
| 5271 | if (reg_endpos[0].lnum < 0) |
| 5272 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 5273 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5274 | reg_endpos[0].lnum = reglnum; |
| 5275 | reg_endpos[0].col = (int)(reginput - regline); |
| 5276 | } |
| 5277 | else |
| 5278 | /* Use line number of "\ze". */ |
| 5279 | reglnum = reg_endpos[0].lnum; |
| 5280 | } |
| 5281 | else |
| 5282 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5283 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5284 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5285 | reg_startp[i] = subs.norm.list.line[i].start; |
| 5286 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5287 | } |
| 5288 | |
| 5289 | if (reg_startp[0] == NULL) |
| 5290 | reg_startp[0] = regline + col; |
| 5291 | if (reg_endp[0] == NULL) |
| 5292 | reg_endp[0] = reginput; |
| 5293 | } |
| 5294 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5295 | #ifdef FEAT_SYN_HL |
| 5296 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 5297 | unref_extmatch(re_extmatch_out); |
| 5298 | re_extmatch_out = NULL; |
| 5299 | |
| 5300 | if (prog->reghasz == REX_SET) |
| 5301 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5302 | cleanup_zsubexpr(); |
| 5303 | re_extmatch_out = make_extmatch(); |
| 5304 | for (i = 0; i < subs.synt.in_use; i++) |
| 5305 | { |
| 5306 | if (REG_MULTI) |
| 5307 | { |
| 5308 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 5309 | |
| 5310 | /* Only accept single line matches. */ |
| 5311 | if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum) |
| 5312 | re_extmatch_out->matches[i] = |
| 5313 | vim_strnsave(reg_getline(mpos->start.lnum) |
| 5314 | + mpos->start.col, |
| 5315 | mpos->end.col - mpos->start.col); |
| 5316 | } |
| 5317 | else |
| 5318 | { |
| 5319 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 5320 | |
| 5321 | if (lpos->start != NULL && lpos->end != NULL) |
| 5322 | re_extmatch_out->matches[i] = |
| 5323 | vim_strnsave(lpos->start, |
| 5324 | (int)(lpos->end - lpos->start)); |
| 5325 | } |
| 5326 | } |
| 5327 | } |
| 5328 | #endif |
| 5329 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5330 | return 1 + reglnum; |
| 5331 | } |
| 5332 | |
| 5333 | /* |
| 5334 | * Match a regexp against a string ("line" points to the string) or multiple |
| 5335 | * lines ("line" is NULL, use reg_getline()). |
| 5336 | * |
| 5337 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 5338 | */ |
| 5339 | static long |
| 5340 | nfa_regexec_both(line, col) |
| 5341 | char_u *line; |
| 5342 | colnr_T col; /* column to start looking for match */ |
| 5343 | { |
| 5344 | nfa_regprog_T *prog; |
| 5345 | long retval = 0L; |
| 5346 | int i; |
| 5347 | |
| 5348 | if (REG_MULTI) |
| 5349 | { |
| 5350 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 5351 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 5352 | reg_startpos = reg_mmatch->startpos; |
| 5353 | reg_endpos = reg_mmatch->endpos; |
| 5354 | } |
| 5355 | else |
| 5356 | { |
| 5357 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 5358 | reg_startp = reg_match->startp; |
| 5359 | reg_endp = reg_match->endp; |
| 5360 | } |
| 5361 | |
| 5362 | /* Be paranoid... */ |
| 5363 | if (prog == NULL || line == NULL) |
| 5364 | { |
| 5365 | EMSG(_(e_null)); |
| 5366 | goto theend; |
| 5367 | } |
| 5368 | |
| 5369 | /* If the start column is past the maximum column: no need to try. */ |
| 5370 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 5371 | goto theend; |
| 5372 | |
| 5373 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 5374 | if (prog->regflags & RF_ICASE) |
| 5375 | ireg_ic = TRUE; |
| 5376 | else if (prog->regflags & RF_NOICASE) |
| 5377 | ireg_ic = FALSE; |
| 5378 | |
| 5379 | #ifdef FEAT_MBYTE |
| 5380 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 5381 | if (prog->regflags & RF_ICOMBINE) |
| 5382 | ireg_icombine = TRUE; |
| 5383 | #endif |
| 5384 | |
| 5385 | regline = line; |
| 5386 | reglnum = 0; /* relative to line */ |
| 5387 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5388 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5389 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5390 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5391 | nfa_listid = 1; |
| 5392 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5393 | #ifdef DEBUG |
| 5394 | nfa_regengine.expr = prog->pattern; |
| 5395 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5396 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5397 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5398 | for (i = 0; i < nstate; ++i) |
| 5399 | { |
| 5400 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5401 | prog->state[i].lastlist[0] = 0; |
| 5402 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5403 | } |
| 5404 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5405 | retval = nfa_regtry(prog, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5406 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5407 | #ifdef DEBUG |
| 5408 | nfa_regengine.expr = NULL; |
| 5409 | #endif |
| 5410 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5411 | theend: |
| 5412 | return retval; |
| 5413 | } |
| 5414 | |
| 5415 | /* |
| 5416 | * Compile a regular expression into internal code for the NFA matcher. |
| 5417 | * Returns the program in allocated space. Returns NULL for an error. |
| 5418 | */ |
| 5419 | static regprog_T * |
| 5420 | nfa_regcomp(expr, re_flags) |
| 5421 | char_u *expr; |
| 5422 | int re_flags; |
| 5423 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 5424 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 5425 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5426 | int *postfix; |
| 5427 | |
| 5428 | if (expr == NULL) |
| 5429 | return NULL; |
| 5430 | |
| 5431 | #ifdef DEBUG |
| 5432 | nfa_regengine.expr = expr; |
| 5433 | #endif |
| 5434 | |
| 5435 | init_class_tab(); |
| 5436 | |
| 5437 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 5438 | return NULL; |
| 5439 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5440 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 5441 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5442 | postfix = re2post(); |
| 5443 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5444 | { |
| 5445 | /* TODO: only give this error for debugging? */ |
| 5446 | if (post_ptr >= post_end) |
| 5447 | 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] | 5448 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5449 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5450 | |
| 5451 | /* |
| 5452 | * In order to build the NFA, we parse the input regexp twice: |
| 5453 | * 1. first pass to count size (so we can allocate space) |
| 5454 | * 2. second to emit code |
| 5455 | */ |
| 5456 | #ifdef ENABLE_LOG |
| 5457 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5458 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5459 | |
| 5460 | if (f != NULL) |
| 5461 | { |
| 5462 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 5463 | fclose(f); |
| 5464 | } |
| 5465 | } |
| 5466 | #endif |
| 5467 | |
| 5468 | /* |
| 5469 | * PASS 1 |
| 5470 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 5471 | */ |
| 5472 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 5473 | |
| 5474 | /* Space for compiled regexp */ |
| 5475 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate; |
| 5476 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 5477 | if (prog == NULL) |
| 5478 | goto fail; |
| 5479 | vim_memset(prog, 0, prog_size); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5480 | state_ptr = prog->state; |
| 5481 | |
| 5482 | /* |
| 5483 | * PASS 2 |
| 5484 | * Build the NFA |
| 5485 | */ |
| 5486 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 5487 | if (prog->start == NULL) |
| 5488 | goto fail; |
| 5489 | |
| 5490 | prog->regflags = regflags; |
| 5491 | prog->engine = &nfa_regengine; |
| 5492 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5493 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5494 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5495 | prog->nsubexp = regnpar; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5496 | #ifdef ENABLE_LOG |
| 5497 | nfa_postfix_dump(expr, OK); |
| 5498 | nfa_dump(prog); |
| 5499 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5500 | #ifdef FEAT_SYN_HL |
| 5501 | /* Remember whether this pattern has any \z specials in it. */ |
| 5502 | prog->reghasz = re_has_z; |
| 5503 | #endif |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5504 | #ifdef DEBUG |
| 5505 | prog->pattern = vim_strsave(expr); /* memory will leak */ |
| 5506 | nfa_regengine.expr = NULL; |
| 5507 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5508 | |
| 5509 | out: |
| 5510 | vim_free(post_start); |
| 5511 | post_start = post_ptr = post_end = NULL; |
| 5512 | state_ptr = NULL; |
| 5513 | return (regprog_T *)prog; |
| 5514 | |
| 5515 | fail: |
| 5516 | vim_free(prog); |
| 5517 | prog = NULL; |
| 5518 | #ifdef ENABLE_LOG |
| 5519 | nfa_postfix_dump(expr, FAIL); |
| 5520 | #endif |
| 5521 | #ifdef DEBUG |
| 5522 | nfa_regengine.expr = NULL; |
| 5523 | #endif |
| 5524 | goto out; |
| 5525 | } |
| 5526 | |
| 5527 | |
| 5528 | /* |
| 5529 | * Match a regexp against a string. |
| 5530 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 5531 | * Uses curbuf for line count and 'iskeyword'. |
| 5532 | * |
| 5533 | * Return TRUE if there is a match, FALSE if not. |
| 5534 | */ |
| 5535 | static int |
| 5536 | nfa_regexec(rmp, line, col) |
| 5537 | regmatch_T *rmp; |
| 5538 | char_u *line; /* string to match against */ |
| 5539 | colnr_T col; /* column to start looking for match */ |
| 5540 | { |
| 5541 | reg_match = rmp; |
| 5542 | reg_mmatch = NULL; |
| 5543 | reg_maxline = 0; |
| 5544 | reg_line_lbr = FALSE; |
| 5545 | reg_buf = curbuf; |
| 5546 | reg_win = NULL; |
| 5547 | ireg_ic = rmp->rm_ic; |
| 5548 | #ifdef FEAT_MBYTE |
| 5549 | ireg_icombine = FALSE; |
| 5550 | #endif |
| 5551 | ireg_maxcol = 0; |
| 5552 | return (nfa_regexec_both(line, col) != 0); |
| 5553 | } |
| 5554 | |
| 5555 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 5556 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 5557 | |
| 5558 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 5559 | |
| 5560 | /* |
| 5561 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 5562 | */ |
| 5563 | static int |
| 5564 | nfa_regexec_nl(rmp, line, col) |
| 5565 | regmatch_T *rmp; |
| 5566 | char_u *line; /* string to match against */ |
| 5567 | colnr_T col; /* column to start looking for match */ |
| 5568 | { |
| 5569 | reg_match = rmp; |
| 5570 | reg_mmatch = NULL; |
| 5571 | reg_maxline = 0; |
| 5572 | reg_line_lbr = TRUE; |
| 5573 | reg_buf = curbuf; |
| 5574 | reg_win = NULL; |
| 5575 | ireg_ic = rmp->rm_ic; |
| 5576 | #ifdef FEAT_MBYTE |
| 5577 | ireg_icombine = FALSE; |
| 5578 | #endif |
| 5579 | ireg_maxcol = 0; |
| 5580 | return (nfa_regexec_both(line, col) != 0); |
| 5581 | } |
| 5582 | #endif |
| 5583 | |
| 5584 | |
| 5585 | /* |
| 5586 | * Match a regexp against multiple lines. |
| 5587 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 5588 | * Uses curbuf for line count and 'iskeyword'. |
| 5589 | * |
| 5590 | * Return zero if there is no match. Return number of lines contained in the |
| 5591 | * match otherwise. |
| 5592 | * |
| 5593 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 5594 | * |
| 5595 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 5596 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 5597 | * |
| 5598 | * +-------------------------+ |
| 5599 | * |a | |
| 5600 | * |b | |
| 5601 | * |c | |
| 5602 | * | | |
| 5603 | * +-------------------------+ |
| 5604 | * |
| 5605 | * then nfa_regexec_multi() returns 3. while the original |
| 5606 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 5607 | * |
| 5608 | * FIXME if this behavior is not compatible. |
| 5609 | */ |
| 5610 | static long |
| 5611 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 5612 | regmmatch_T *rmp; |
| 5613 | win_T *win; /* window in which to search or NULL */ |
| 5614 | buf_T *buf; /* buffer in which to search */ |
| 5615 | linenr_T lnum; /* nr of line to start looking for match */ |
| 5616 | colnr_T col; /* column to start looking for match */ |
| 5617 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 5618 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5619 | reg_match = NULL; |
| 5620 | reg_mmatch = rmp; |
| 5621 | reg_buf = buf; |
| 5622 | reg_win = win; |
| 5623 | reg_firstlnum = lnum; |
| 5624 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 5625 | reg_line_lbr = FALSE; |
| 5626 | ireg_ic = rmp->rmm_ic; |
| 5627 | #ifdef FEAT_MBYTE |
| 5628 | ireg_icombine = FALSE; |
| 5629 | #endif |
| 5630 | ireg_maxcol = rmp->rmm_maxcol; |
| 5631 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5632 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5633 | } |
| 5634 | |
| 5635 | #ifdef DEBUG |
| 5636 | # undef ENABLE_LOG |
| 5637 | #endif |