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