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 | |
| 32 | /* Upper limit allowed for {m,n} repetitions handled by NFA */ |
| 33 | #define NFA_BRACES_MAXLIMIT 10 |
| 34 | /* For allocating space for the postfix representation */ |
| 35 | #define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 36 | |
| 37 | enum |
| 38 | { |
| 39 | NFA_SPLIT = -1024, |
| 40 | NFA_MATCH, |
| 41 | NFA_SKIP_CHAR, /* matches a 0-length char */ |
| 42 | NFA_END_NEG_RANGE, /* Used when expanding [^ab] */ |
| 43 | |
| 44 | NFA_CONCAT, |
| 45 | NFA_OR, |
| 46 | NFA_STAR, |
| 47 | NFA_PLUS, |
| 48 | NFA_QUEST, |
| 49 | NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */ |
| 50 | NFA_NOT, /* used for [^ab] negated char ranges */ |
| 51 | |
| 52 | NFA_BOL, /* ^ Begin line */ |
| 53 | NFA_EOL, /* $ End line */ |
| 54 | NFA_BOW, /* \< Begin word */ |
| 55 | NFA_EOW, /* \> End word */ |
| 56 | NFA_BOF, /* \%^ Begin file */ |
| 57 | NFA_EOF, /* \%$ End file */ |
| 58 | NFA_NEWL, |
| 59 | NFA_ZSTART, /* Used for \zs */ |
| 60 | NFA_ZEND, /* Used for \ze */ |
| 61 | NFA_NOPEN, /* Start of subexpression marked with \%( */ |
| 62 | NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ |
| 63 | NFA_START_INVISIBLE, |
| 64 | NFA_END_INVISIBLE, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 65 | NFA_COMPOSING, /* Next nodes in NFA are part of the |
| 66 | composing multibyte char */ |
| 67 | NFA_END_COMPOSING, /* End of a composing char in the NFA */ |
| 68 | |
| 69 | /* The following are used only in the postfix form, not in the NFA */ |
| 70 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 71 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 72 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 73 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 74 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 75 | |
| 76 | NFA_MOPEN, |
| 77 | NFA_MCLOSE = NFA_MOPEN + NSUBEXP, |
| 78 | |
| 79 | /* NFA_FIRST_NL */ |
| 80 | NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */ |
| 81 | NFA_ANYOF, /* Match any character in this string. */ |
| 82 | NFA_ANYBUT, /* Match any character not in this string. */ |
| 83 | NFA_IDENT, /* Match identifier char */ |
| 84 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 85 | NFA_KWORD, /* Match keyword char */ |
| 86 | NFA_SKWORD, /* Match word char but no digit */ |
| 87 | NFA_FNAME, /* Match file name char */ |
| 88 | NFA_SFNAME, /* Match file name char but no digit */ |
| 89 | NFA_PRINT, /* Match printable char */ |
| 90 | NFA_SPRINT, /* Match printable char but no digit */ |
| 91 | NFA_WHITE, /* Match whitespace char */ |
| 92 | NFA_NWHITE, /* Match non-whitespace char */ |
| 93 | NFA_DIGIT, /* Match digit char */ |
| 94 | NFA_NDIGIT, /* Match non-digit char */ |
| 95 | NFA_HEX, /* Match hex char */ |
| 96 | NFA_NHEX, /* Match non-hex char */ |
| 97 | NFA_OCTAL, /* Match octal char */ |
| 98 | NFA_NOCTAL, /* Match non-octal char */ |
| 99 | NFA_WORD, /* Match word char */ |
| 100 | NFA_NWORD, /* Match non-word char */ |
| 101 | NFA_HEAD, /* Match head char */ |
| 102 | NFA_NHEAD, /* Match non-head char */ |
| 103 | NFA_ALPHA, /* Match alpha char */ |
| 104 | NFA_NALPHA, /* Match non-alpha char */ |
| 105 | NFA_LOWER, /* Match lowercase char */ |
| 106 | NFA_NLOWER, /* Match non-lowercase char */ |
| 107 | NFA_UPPER, /* Match uppercase char */ |
| 108 | NFA_NUPPER, /* Match non-uppercase char */ |
| 109 | NFA_FIRST_NL = NFA_ANY + ADD_NL, |
| 110 | NFA_LAST_NL = NFA_NUPPER + ADD_NL, |
| 111 | |
| 112 | /* Character classes [:alnum:] etc */ |
| 113 | NFA_CLASS_ALNUM, |
| 114 | NFA_CLASS_ALPHA, |
| 115 | NFA_CLASS_BLANK, |
| 116 | NFA_CLASS_CNTRL, |
| 117 | NFA_CLASS_DIGIT, |
| 118 | NFA_CLASS_GRAPH, |
| 119 | NFA_CLASS_LOWER, |
| 120 | NFA_CLASS_PRINT, |
| 121 | NFA_CLASS_PUNCT, |
| 122 | NFA_CLASS_SPACE, |
| 123 | NFA_CLASS_UPPER, |
| 124 | NFA_CLASS_XDIGIT, |
| 125 | NFA_CLASS_TAB, |
| 126 | NFA_CLASS_RETURN, |
| 127 | NFA_CLASS_BACKSPACE, |
| 128 | NFA_CLASS_ESCAPE |
| 129 | }; |
| 130 | |
| 131 | /* Keep in sync with classchars. */ |
| 132 | static int nfa_classcodes[] = { |
| 133 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 134 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 135 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 136 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 137 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 138 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 139 | NFA_UPPER, NFA_NUPPER |
| 140 | }; |
| 141 | |
| 142 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
| 143 | |
| 144 | /* |
| 145 | * NFA errors can be of 3 types: |
| 146 | * *** NFA runtime errors, when something unknown goes wrong. The NFA fails |
| 147 | * silently and revert the to backtracking engine. |
| 148 | * syntax_error = FALSE; |
| 149 | * *** Regexp syntax errors, when the input regexp is not syntactically correct. |
| 150 | * The NFA engine displays an error message, and nothing else happens. |
| 151 | * syntax_error = TRUE |
| 152 | * *** Unsupported features, when the input regexp uses an operator that is not |
| 153 | * implemented in the NFA. The NFA engine fails silently, and reverts to the |
| 154 | * old backtracking engine. |
| 155 | * syntax_error = FALSE |
| 156 | * "The NFA fails" means that "compiling the regexp with the NFA fails": |
| 157 | * nfa_regcomp() returns FAIL. |
| 158 | */ |
| 159 | static int syntax_error = FALSE; |
| 160 | |
| 161 | /* NFA regexp \ze operator encountered. */ |
| 162 | static int nfa_has_zend = FALSE; |
| 163 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 164 | /* Number of sub expressions actually being used during execution. 1 if only |
| 165 | * the whole match (subexpr 0) is used. */ |
| 166 | static int nfa_nsubexpr; |
| 167 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 168 | static int *post_start; /* holds the postfix form of r.e. */ |
| 169 | static int *post_end; |
| 170 | static int *post_ptr; |
| 171 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 172 | static int nstate; /* Number of states in the NFA. Also used when |
| 173 | * executing. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 174 | static int istate; /* Index in the state vector, used in new_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 175 | |
| 176 | |
| 177 | static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags)); |
| 178 | static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl)); |
| 179 | static int nfa_emit_equi_class __ARGS((int c, int neg)); |
| 180 | static void nfa_inc __ARGS((char_u **p)); |
| 181 | static void nfa_dec __ARGS((char_u **p)); |
| 182 | static int nfa_regatom __ARGS((void)); |
| 183 | static int nfa_regpiece __ARGS((void)); |
| 184 | static int nfa_regconcat __ARGS((void)); |
| 185 | static int nfa_regbranch __ARGS((void)); |
| 186 | static int nfa_reg __ARGS((int paren)); |
| 187 | #ifdef DEBUG |
| 188 | static void nfa_set_code __ARGS((int c)); |
| 189 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 190 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 191 | 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] | 192 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 193 | #endif |
| 194 | static int *re2post __ARGS((void)); |
| 195 | static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1)); |
| 196 | static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size)); |
| 197 | static int check_char_class __ARGS((int class, int c)); |
| 198 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
| 199 | static void nfa_save_listids __ARGS((nfa_state_T *start, int *list)); |
| 200 | static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list)); |
| 201 | static void nfa_set_null_listids __ARGS((nfa_state_T *start)); |
| 202 | static void nfa_set_neg_listids __ARGS((nfa_state_T *start)); |
| 203 | static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col)); |
| 204 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 205 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
| 206 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 207 | static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
| 208 | |
| 209 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 210 | #define EMIT(c) do { \ |
| 211 | if (post_ptr >= post_end) \ |
| 212 | return FAIL; \ |
| 213 | *post_ptr++ = c; \ |
| 214 | } while (0) |
| 215 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 216 | /* |
| 217 | * Initialize internal variables before NFA compilation. |
| 218 | * Return OK on success, FAIL otherwise. |
| 219 | */ |
| 220 | static int |
| 221 | nfa_regcomp_start(expr, re_flags) |
| 222 | char_u *expr; |
| 223 | int re_flags; /* see vim_regcomp() */ |
| 224 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 225 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 226 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 227 | |
| 228 | nstate = 0; |
| 229 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 230 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 231 | nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 232 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 233 | /* Some items blow up in size, such as [A-z]. Add more space for that. |
| 234 | * TODO: some patterns may still fail. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 235 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 236 | |
| 237 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 238 | postfix_size = sizeof(*post_start) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 239 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 240 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 241 | if (post_start == NULL) |
| 242 | return FAIL; |
| 243 | vim_memset(post_start, 0, postfix_size); |
| 244 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 245 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 246 | nfa_has_zend = FALSE; |
| 247 | |
| 248 | regcomp_start(expr, re_flags); |
| 249 | |
| 250 | return OK; |
| 251 | } |
| 252 | |
| 253 | /* |
| 254 | * Search between "start" and "end" and try to recognize a |
| 255 | * character class in expanded form. For example [0-9]. |
| 256 | * On success, return the id the character class to be emitted. |
| 257 | * On failure, return 0 (=FAIL) |
| 258 | * Start points to the first char of the range, while end should point |
| 259 | * to the closing brace. |
| 260 | */ |
| 261 | static int |
| 262 | nfa_recognize_char_class(start, end, extra_newl) |
| 263 | char_u *start; |
| 264 | char_u *end; |
| 265 | int extra_newl; |
| 266 | { |
| 267 | int i; |
| 268 | /* Each of these variables takes up a char in "config[]", |
| 269 | * in the order they are here. */ |
| 270 | int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE, |
| 271 | o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE; |
| 272 | char_u *p; |
| 273 | #define NCONFIGS 16 |
| 274 | int classid[NCONFIGS] = { |
| 275 | NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX, |
| 276 | NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD, |
| 277 | NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA, |
| 278 | NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER |
| 279 | }; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 280 | char_u myconfig[10]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 281 | char_u config[NCONFIGS][9] = { |
| 282 | "000000100", /* digit */ |
| 283 | "100000100", /* non digit */ |
| 284 | "011000100", /* hex-digit */ |
| 285 | "111000100", /* non hex-digit */ |
| 286 | "000001000", /* octal-digit */ |
| 287 | "100001000", /* [^0-7] */ |
| 288 | "000110110", /* [0-9A-Za-z_] */ |
| 289 | "100110110", /* [^0-9A-Za-z_] */ |
| 290 | "000110010", /* head of word */ |
| 291 | "100110010", /* not head of word */ |
| 292 | "000110000", /* alphabetic char a-z */ |
| 293 | "100110000", /* non alphabetic char */ |
| 294 | "000100000", /* lowercase letter */ |
| 295 | "100100000", /* non lowercase */ |
| 296 | "000010000", /* uppercase */ |
| 297 | "100010000" /* non uppercase */ |
| 298 | }; |
| 299 | |
| 300 | if (extra_newl == TRUE) |
| 301 | newl = TRUE; |
| 302 | |
| 303 | if (*end != ']') |
| 304 | return FAIL; |
| 305 | p = start; |
| 306 | if (*p == '^') |
| 307 | { |
| 308 | not = TRUE; |
| 309 | p ++; |
| 310 | } |
| 311 | |
| 312 | while (p < end) |
| 313 | { |
| 314 | if (p + 2 < end && *(p + 1) == '-') |
| 315 | { |
| 316 | switch (*p) |
| 317 | { |
| 318 | case '0': |
| 319 | if (*(p + 2) == '9') |
| 320 | { |
| 321 | o9 = TRUE; |
| 322 | break; |
| 323 | } |
| 324 | else |
| 325 | if (*(p + 2) == '7') |
| 326 | { |
| 327 | o7 = TRUE; |
| 328 | break; |
| 329 | } |
| 330 | case 'a': |
| 331 | if (*(p + 2) == 'z') |
| 332 | { |
| 333 | az = TRUE; |
| 334 | break; |
| 335 | } |
| 336 | else |
| 337 | if (*(p + 2) == 'f') |
| 338 | { |
| 339 | af = TRUE; |
| 340 | break; |
| 341 | } |
| 342 | case 'A': |
| 343 | if (*(p + 2) == 'Z') |
| 344 | { |
| 345 | AZ = TRUE; |
| 346 | break; |
| 347 | } |
| 348 | else |
| 349 | if (*(p + 2) == 'F') |
| 350 | { |
| 351 | AF = TRUE; |
| 352 | break; |
| 353 | } |
| 354 | /* FALLTHROUGH */ |
| 355 | default: |
| 356 | return FAIL; |
| 357 | } |
| 358 | p += 3; |
| 359 | } |
| 360 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 361 | { |
| 362 | newl = TRUE; |
| 363 | p += 2; |
| 364 | } |
| 365 | else if (*p == '_') |
| 366 | { |
| 367 | underscore = TRUE; |
| 368 | p ++; |
| 369 | } |
| 370 | else if (*p == '\n') |
| 371 | { |
| 372 | newl = TRUE; |
| 373 | p ++; |
| 374 | } |
| 375 | else |
| 376 | return FAIL; |
| 377 | } /* while (p < end) */ |
| 378 | |
| 379 | if (p != end) |
| 380 | return FAIL; |
| 381 | |
| 382 | /* build the config that represents the ranges we gathered */ |
| 383 | STRCPY(myconfig, "000000000"); |
| 384 | if (not == TRUE) |
| 385 | myconfig[0] = '1'; |
| 386 | if (af == TRUE) |
| 387 | myconfig[1] = '1'; |
| 388 | if (AF == TRUE) |
| 389 | myconfig[2] = '1'; |
| 390 | if (az == TRUE) |
| 391 | myconfig[3] = '1'; |
| 392 | if (AZ == TRUE) |
| 393 | myconfig[4] = '1'; |
| 394 | if (o7 == TRUE) |
| 395 | myconfig[5] = '1'; |
| 396 | if (o9 == TRUE) |
| 397 | myconfig[6] = '1'; |
| 398 | if (underscore == TRUE) |
| 399 | myconfig[7] = '1'; |
| 400 | if (newl == TRUE) |
| 401 | { |
| 402 | myconfig[8] = '1'; |
| 403 | extra_newl = ADD_NL; |
| 404 | } |
| 405 | /* try to recognize character classes */ |
| 406 | for (i = 0; i < NCONFIGS; i++) |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 407 | if (STRNCMP(myconfig, config[i], 8) == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 408 | return classid[i] + extra_newl; |
| 409 | |
| 410 | /* fallthrough => no success so far */ |
| 411 | return FAIL; |
| 412 | |
| 413 | #undef NCONFIGS |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Produce the bytes for equivalence class "c". |
| 418 | * Currently only handles latin1, latin9 and utf-8. |
| 419 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 420 | * equivalent to 'a OR b OR c' |
| 421 | * |
| 422 | * NOTE! When changing this function, also update reg_equi_class() |
| 423 | */ |
| 424 | static int |
| 425 | nfa_emit_equi_class(c, neg) |
| 426 | int c; |
| 427 | int neg; |
| 428 | { |
| 429 | int first = TRUE; |
| 430 | int glue = neg == TRUE ? NFA_CONCAT : NFA_OR; |
| 431 | #define EMIT2(c) \ |
| 432 | EMIT(c); \ |
| 433 | if (neg == TRUE) { \ |
| 434 | EMIT(NFA_NOT); \ |
| 435 | } \ |
| 436 | if (first == FALSE) \ |
| 437 | EMIT(glue); \ |
| 438 | else \ |
| 439 | first = FALSE; \ |
| 440 | |
| 441 | #ifdef FEAT_MBYTE |
| 442 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 443 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 444 | #endif |
| 445 | { |
| 446 | switch (c) |
| 447 | { |
| 448 | case 'A': case '\300': case '\301': case '\302': |
| 449 | case '\303': case '\304': case '\305': |
| 450 | EMIT2('A'); EMIT2('\300'); EMIT2('\301'); |
| 451 | EMIT2('\302'); EMIT2('\303'); EMIT2('\304'); |
| 452 | EMIT2('\305'); |
| 453 | return OK; |
| 454 | |
| 455 | case 'C': case '\307': |
| 456 | EMIT2('C'); EMIT2('\307'); |
| 457 | return OK; |
| 458 | |
| 459 | case 'E': case '\310': case '\311': case '\312': case '\313': |
| 460 | EMIT2('E'); EMIT2('\310'); EMIT2('\311'); |
| 461 | EMIT2('\312'); EMIT2('\313'); |
| 462 | return OK; |
| 463 | |
| 464 | case 'I': case '\314': case '\315': case '\316': case '\317': |
| 465 | EMIT2('I'); EMIT2('\314'); EMIT2('\315'); |
| 466 | EMIT2('\316'); EMIT2('\317'); |
| 467 | return OK; |
| 468 | |
| 469 | case 'N': case '\321': |
| 470 | EMIT2('N'); EMIT2('\321'); |
| 471 | return OK; |
| 472 | |
| 473 | case 'O': case '\322': case '\323': case '\324': case '\325': |
| 474 | case '\326': |
| 475 | EMIT2('O'); EMIT2('\322'); EMIT2('\323'); |
| 476 | EMIT2('\324'); EMIT2('\325'); EMIT2('\326'); |
| 477 | return OK; |
| 478 | |
| 479 | case 'U': case '\331': case '\332': case '\333': case '\334': |
| 480 | EMIT2('U'); EMIT2('\331'); EMIT2('\332'); |
| 481 | EMIT2('\333'); EMIT2('\334'); |
| 482 | return OK; |
| 483 | |
| 484 | case 'Y': case '\335': |
| 485 | EMIT2('Y'); EMIT2('\335'); |
| 486 | return OK; |
| 487 | |
| 488 | case 'a': case '\340': case '\341': case '\342': |
| 489 | case '\343': case '\344': case '\345': |
| 490 | EMIT2('a'); EMIT2('\340'); EMIT2('\341'); |
| 491 | EMIT2('\342'); EMIT2('\343'); EMIT2('\344'); |
| 492 | EMIT2('\345'); |
| 493 | return OK; |
| 494 | |
| 495 | case 'c': case '\347': |
| 496 | EMIT2('c'); EMIT2('\347'); |
| 497 | return OK; |
| 498 | |
| 499 | case 'e': case '\350': case '\351': case '\352': case '\353': |
| 500 | EMIT2('e'); EMIT2('\350'); EMIT2('\351'); |
| 501 | EMIT2('\352'); EMIT2('\353'); |
| 502 | return OK; |
| 503 | |
| 504 | case 'i': case '\354': case '\355': case '\356': case '\357': |
| 505 | EMIT2('i'); EMIT2('\354'); EMIT2('\355'); |
| 506 | EMIT2('\356'); EMIT2('\357'); |
| 507 | return OK; |
| 508 | |
| 509 | case 'n': case '\361': |
| 510 | EMIT2('n'); EMIT2('\361'); |
| 511 | return OK; |
| 512 | |
| 513 | case 'o': case '\362': case '\363': case '\364': case '\365': |
| 514 | case '\366': |
| 515 | EMIT2('o'); EMIT2('\362'); EMIT2('\363'); |
| 516 | EMIT2('\364'); EMIT2('\365'); EMIT2('\366'); |
| 517 | return OK; |
| 518 | |
| 519 | case 'u': case '\371': case '\372': case '\373': case '\374': |
| 520 | EMIT2('u'); EMIT2('\371'); EMIT2('\372'); |
| 521 | EMIT2('\373'); EMIT2('\374'); |
| 522 | return OK; |
| 523 | |
| 524 | case 'y': case '\375': case '\377': |
| 525 | EMIT2('y'); EMIT2('\375'); EMIT2('\377'); |
| 526 | return OK; |
| 527 | |
| 528 | default: |
| 529 | return FAIL; |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | EMIT(c); |
| 534 | return OK; |
| 535 | #undef EMIT2 |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | * Code to parse regular expression. |
| 540 | * |
| 541 | * We try to reuse parsing functions in regexp.c to |
| 542 | * minimize surprise and keep the syntax consistent. |
| 543 | */ |
| 544 | |
| 545 | /* |
| 546 | * Increments the pointer "p" by one (multi-byte) character. |
| 547 | */ |
| 548 | static void |
| 549 | nfa_inc(p) |
| 550 | char_u **p; |
| 551 | { |
| 552 | #ifdef FEAT_MBYTE |
| 553 | if (has_mbyte) |
| 554 | mb_ptr2char_adv(p); |
| 555 | else |
| 556 | #endif |
| 557 | *p = *p + 1; |
| 558 | } |
| 559 | |
| 560 | /* |
| 561 | * Decrements the pointer "p" by one (multi-byte) character. |
| 562 | */ |
| 563 | static void |
| 564 | nfa_dec(p) |
| 565 | char_u **p; |
| 566 | { |
| 567 | #ifdef FEAT_MBYTE |
| 568 | char_u *p2, *oldp; |
| 569 | |
| 570 | if (has_mbyte) |
| 571 | { |
| 572 | oldp = *p; |
| 573 | /* Try to find the multibyte char that advances to the current |
| 574 | * position. */ |
| 575 | do |
| 576 | { |
| 577 | *p = *p - 1; |
| 578 | p2 = *p; |
| 579 | mb_ptr2char_adv(&p2); |
| 580 | } while (p2 != oldp); |
| 581 | } |
| 582 | #else |
| 583 | *p = *p - 1; |
| 584 | #endif |
| 585 | } |
| 586 | |
| 587 | /* |
| 588 | * Parse the lowest level. |
| 589 | * |
| 590 | * An atom can be one of a long list of items. Many atoms match one character |
| 591 | * in the text. It is often an ordinary character or a character class. |
| 592 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 593 | * is only for syntax highlighting. |
| 594 | * |
| 595 | * atom ::= ordinary-atom |
| 596 | * or \( pattern \) |
| 597 | * or \%( pattern \) |
| 598 | * or \z( pattern \) |
| 599 | */ |
| 600 | static int |
| 601 | nfa_regatom() |
| 602 | { |
| 603 | int c; |
| 604 | int charclass; |
| 605 | int equiclass; |
| 606 | int collclass; |
| 607 | int got_coll_char; |
| 608 | char_u *p; |
| 609 | char_u *endp; |
| 610 | #ifdef FEAT_MBYTE |
| 611 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 612 | #endif |
| 613 | int extra = 0; |
| 614 | int first; |
| 615 | int emit_range; |
| 616 | int negated; |
| 617 | int result; |
| 618 | int startc = -1; |
| 619 | int endc = -1; |
| 620 | int oldstartc = -1; |
| 621 | int cpo_lit; /* 'cpoptions' contains 'l' flag */ |
| 622 | int cpo_bsl; /* 'cpoptions' contains '\' flag */ |
| 623 | int glue; /* ID that will "glue" nodes together */ |
| 624 | |
| 625 | cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL; |
| 626 | cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL; |
| 627 | |
| 628 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 629 | switch (c) |
| 630 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 631 | case NUL: |
| 632 | syntax_error = TRUE; |
| 633 | EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); |
| 634 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 635 | case Magic('^'): |
| 636 | EMIT(NFA_BOL); |
| 637 | break; |
| 638 | |
| 639 | case Magic('$'): |
| 640 | EMIT(NFA_EOL); |
| 641 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 642 | had_eol = TRUE; |
| 643 | #endif |
| 644 | break; |
| 645 | |
| 646 | case Magic('<'): |
| 647 | EMIT(NFA_BOW); |
| 648 | break; |
| 649 | |
| 650 | case Magic('>'): |
| 651 | EMIT(NFA_EOW); |
| 652 | break; |
| 653 | |
| 654 | case Magic('_'): |
| 655 | c = no_Magic(getchr()); |
| 656 | if (c == '^') /* "\_^" is start-of-line */ |
| 657 | { |
| 658 | EMIT(NFA_BOL); |
| 659 | break; |
| 660 | } |
| 661 | if (c == '$') /* "\_$" is end-of-line */ |
| 662 | { |
| 663 | EMIT(NFA_EOL); |
| 664 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 665 | had_eol = TRUE; |
| 666 | #endif |
| 667 | break; |
| 668 | } |
| 669 | |
| 670 | extra = ADD_NL; |
| 671 | |
| 672 | /* "\_[" is collection plus newline */ |
| 673 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 674 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 675 | |
| 676 | /* "\_x" is character class plus newline */ |
| 677 | /*FALLTHROUGH*/ |
| 678 | |
| 679 | /* |
| 680 | * Character classes. |
| 681 | */ |
| 682 | case Magic('.'): |
| 683 | case Magic('i'): |
| 684 | case Magic('I'): |
| 685 | case Magic('k'): |
| 686 | case Magic('K'): |
| 687 | case Magic('f'): |
| 688 | case Magic('F'): |
| 689 | case Magic('p'): |
| 690 | case Magic('P'): |
| 691 | case Magic('s'): |
| 692 | case Magic('S'): |
| 693 | case Magic('d'): |
| 694 | case Magic('D'): |
| 695 | case Magic('x'): |
| 696 | case Magic('X'): |
| 697 | case Magic('o'): |
| 698 | case Magic('O'): |
| 699 | case Magic('w'): |
| 700 | case Magic('W'): |
| 701 | case Magic('h'): |
| 702 | case Magic('H'): |
| 703 | case Magic('a'): |
| 704 | case Magic('A'): |
| 705 | case Magic('l'): |
| 706 | case Magic('L'): |
| 707 | case Magic('u'): |
| 708 | case Magic('U'): |
| 709 | p = vim_strchr(classchars, no_Magic(c)); |
| 710 | if (p == NULL) |
| 711 | { |
| 712 | return FAIL; /* runtime error */ |
| 713 | } |
| 714 | #ifdef FEAT_MBYTE |
| 715 | /* When '.' is followed by a composing char ignore the dot, so that |
| 716 | * the composing char is matched here. */ |
| 717 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 718 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 719 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 720 | c = getchr(); |
| 721 | goto nfa_do_multibyte; |
| 722 | } |
| 723 | #endif |
| 724 | EMIT(nfa_classcodes[p - classchars]); |
| 725 | if (extra == ADD_NL) |
| 726 | { |
| 727 | EMIT(NFA_NEWL); |
| 728 | EMIT(NFA_OR); |
| 729 | regflags |= RF_HASNL; |
| 730 | } |
| 731 | break; |
| 732 | |
| 733 | case Magic('n'): |
| 734 | if (reg_string) |
| 735 | /* In a string "\n" matches a newline character. */ |
| 736 | EMIT(NL); |
| 737 | else |
| 738 | { |
| 739 | /* In buffer text "\n" matches the end of a line. */ |
| 740 | EMIT(NFA_NEWL); |
| 741 | regflags |= RF_HASNL; |
| 742 | } |
| 743 | break; |
| 744 | |
| 745 | case Magic('('): |
| 746 | if (nfa_reg(REG_PAREN) == FAIL) |
| 747 | return FAIL; /* cascaded error */ |
| 748 | break; |
| 749 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 750 | case Magic('|'): |
| 751 | case Magic('&'): |
| 752 | case Magic(')'): |
| 753 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 754 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 755 | return FAIL; |
| 756 | |
| 757 | case Magic('='): |
| 758 | case Magic('?'): |
| 759 | case Magic('+'): |
| 760 | case Magic('@'): |
| 761 | case Magic('*'): |
| 762 | case Magic('{'): |
| 763 | /* these should follow an atom, not form an atom */ |
| 764 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 765 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 766 | return FAIL; |
| 767 | |
| 768 | case Magic('~'): /* previous substitute pattern */ |
| 769 | /* Not supported yet */ |
| 770 | return FAIL; |
| 771 | |
| 772 | case Magic('1'): |
| 773 | case Magic('2'): |
| 774 | case Magic('3'): |
| 775 | case Magic('4'): |
| 776 | case Magic('5'): |
| 777 | case Magic('6'): |
| 778 | case Magic('7'): |
| 779 | case Magic('8'): |
| 780 | case Magic('9'): |
| 781 | /* not supported yet */ |
| 782 | return FAIL; |
| 783 | |
| 784 | case Magic('z'): |
| 785 | c = no_Magic(getchr()); |
| 786 | switch (c) |
| 787 | { |
| 788 | case 's': |
| 789 | EMIT(NFA_ZSTART); |
| 790 | break; |
| 791 | case 'e': |
| 792 | EMIT(NFA_ZEND); |
| 793 | nfa_has_zend = TRUE; |
| 794 | /* TODO: Currently \ze does not work properly. */ |
| 795 | return FAIL; |
| 796 | /* break; */ |
| 797 | case '1': |
| 798 | case '2': |
| 799 | case '3': |
| 800 | case '4': |
| 801 | case '5': |
| 802 | case '6': |
| 803 | case '7': |
| 804 | case '8': |
| 805 | case '9': |
| 806 | case '(': |
| 807 | /* \z1...\z9 and \z( not yet supported */ |
| 808 | return FAIL; |
| 809 | default: |
| 810 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 811 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 812 | no_Magic(c)); |
| 813 | return FAIL; |
| 814 | } |
| 815 | break; |
| 816 | |
| 817 | case Magic('%'): |
| 818 | c = no_Magic(getchr()); |
| 819 | switch (c) |
| 820 | { |
| 821 | /* () without a back reference */ |
| 822 | case '(': |
| 823 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 824 | return FAIL; |
| 825 | EMIT(NFA_NOPEN); |
| 826 | break; |
| 827 | |
| 828 | case 'd': /* %d123 decimal */ |
| 829 | case 'o': /* %o123 octal */ |
| 830 | case 'x': /* %xab hex 2 */ |
| 831 | case 'u': /* %uabcd hex 4 */ |
| 832 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 833 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 834 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 835 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 836 | switch (c) |
| 837 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 838 | case 'd': nr = getdecchrs(); break; |
| 839 | case 'o': nr = getoctchrs(); break; |
| 840 | case 'x': nr = gethexchrs(2); break; |
| 841 | case 'u': nr = gethexchrs(4); break; |
| 842 | case 'U': nr = gethexchrs(8); break; |
| 843 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 844 | } |
| 845 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 846 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 847 | EMSG2_RET_FAIL( |
| 848 | _("E678: Invalid character after %s%%[dxouU]"), |
| 849 | reg_magic == MAGIC_ALL); |
| 850 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 851 | EMIT(nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 852 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 853 | break; |
| 854 | |
| 855 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 856 | * pattern -- regardless of whether or not it makes sense. */ |
| 857 | case '^': |
| 858 | EMIT(NFA_BOF); |
| 859 | /* Not yet supported */ |
| 860 | return FAIL; |
| 861 | break; |
| 862 | |
| 863 | case '$': |
| 864 | EMIT(NFA_EOF); |
| 865 | /* Not yet supported */ |
| 866 | return FAIL; |
| 867 | break; |
| 868 | |
| 869 | case '#': |
| 870 | /* not supported yet */ |
| 871 | return FAIL; |
| 872 | break; |
| 873 | |
| 874 | case 'V': |
| 875 | /* not supported yet */ |
| 876 | return FAIL; |
| 877 | break; |
| 878 | |
| 879 | case '[': |
| 880 | /* \%[abc] not supported yet */ |
| 881 | return FAIL; |
| 882 | |
| 883 | default: |
| 884 | /* not supported yet */ |
| 885 | return FAIL; |
| 886 | } |
| 887 | break; |
| 888 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 889 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 890 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 891 | /* |
| 892 | * Glue is emitted between several atoms from the []. |
| 893 | * It is either NFA_OR, or NFA_CONCAT. |
| 894 | * |
| 895 | * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation) |
| 896 | * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT |
| 897 | * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix |
| 898 | * notation) |
| 899 | * |
| 900 | */ |
| 901 | |
| 902 | |
| 903 | /* Emit negation atoms, if needed. |
| 904 | * The CONCAT below merges the NOT with the previous node. */ |
| 905 | #define TRY_NEG() \ |
| 906 | if (negated == TRUE) \ |
| 907 | { \ |
| 908 | EMIT(NFA_NOT); \ |
| 909 | } |
| 910 | |
| 911 | /* Emit glue between important nodes : CONCAT or OR. */ |
| 912 | #define EMIT_GLUE() \ |
| 913 | if (first == FALSE) \ |
| 914 | EMIT(glue); \ |
| 915 | else \ |
| 916 | first = FALSE; |
| 917 | |
| 918 | p = regparse; |
| 919 | endp = skip_anyof(p); |
| 920 | if (*endp == ']') |
| 921 | { |
| 922 | /* |
| 923 | * Try to reverse engineer character classes. For example, |
| 924 | * recognize that [0-9] stands for \d and [A-Za-z_] with \h, |
| 925 | * and perform the necessary substitutions in the NFA. |
| 926 | */ |
| 927 | result = nfa_recognize_char_class(regparse, endp, |
| 928 | extra == ADD_NL); |
| 929 | if (result != FAIL) |
| 930 | { |
| 931 | if (result >= NFA_DIGIT && result <= NFA_NUPPER) |
| 932 | EMIT(result); |
| 933 | else /* must be char class + newline */ |
| 934 | { |
| 935 | EMIT(result - ADD_NL); |
| 936 | EMIT(NFA_NEWL); |
| 937 | EMIT(NFA_OR); |
| 938 | } |
| 939 | regparse = endp; |
| 940 | nfa_inc(®parse); |
| 941 | return OK; |
| 942 | } |
| 943 | /* |
| 944 | * Failed to recognize a character class. Use the simple |
| 945 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 946 | */ |
| 947 | startc = endc = oldstartc = -1; |
| 948 | first = TRUE; /* Emitting first atom in this sequence? */ |
| 949 | negated = FALSE; |
| 950 | glue = NFA_OR; |
| 951 | if (*regparse == '^') /* negated range */ |
| 952 | { |
| 953 | negated = TRUE; |
| 954 | glue = NFA_CONCAT; |
| 955 | nfa_inc(®parse); |
| 956 | } |
| 957 | if (*regparse == '-') |
| 958 | { |
| 959 | startc = '-'; |
| 960 | EMIT(startc); |
| 961 | TRY_NEG(); |
| 962 | EMIT_GLUE(); |
| 963 | nfa_inc(®parse); |
| 964 | } |
| 965 | /* Emit the OR branches for each character in the [] */ |
| 966 | emit_range = FALSE; |
| 967 | while (regparse < endp) |
| 968 | { |
| 969 | oldstartc = startc; |
| 970 | startc = -1; |
| 971 | got_coll_char = FALSE; |
| 972 | if (*regparse == '[') |
| 973 | { |
| 974 | /* Check for [: :], [= =], [. .] */ |
| 975 | equiclass = collclass = 0; |
| 976 | charclass = get_char_class(®parse); |
| 977 | if (charclass == CLASS_NONE) |
| 978 | { |
| 979 | equiclass = get_equi_class(®parse); |
| 980 | if (equiclass == 0) |
| 981 | collclass = get_coll_element(®parse); |
| 982 | } |
| 983 | |
| 984 | /* Character class like [:alpha:] */ |
| 985 | if (charclass != CLASS_NONE) |
| 986 | { |
| 987 | switch (charclass) |
| 988 | { |
| 989 | case CLASS_ALNUM: |
| 990 | EMIT(NFA_CLASS_ALNUM); |
| 991 | break; |
| 992 | case CLASS_ALPHA: |
| 993 | EMIT(NFA_CLASS_ALPHA); |
| 994 | break; |
| 995 | case CLASS_BLANK: |
| 996 | EMIT(NFA_CLASS_BLANK); |
| 997 | break; |
| 998 | case CLASS_CNTRL: |
| 999 | EMIT(NFA_CLASS_CNTRL); |
| 1000 | break; |
| 1001 | case CLASS_DIGIT: |
| 1002 | EMIT(NFA_CLASS_DIGIT); |
| 1003 | break; |
| 1004 | case CLASS_GRAPH: |
| 1005 | EMIT(NFA_CLASS_GRAPH); |
| 1006 | break; |
| 1007 | case CLASS_LOWER: |
| 1008 | EMIT(NFA_CLASS_LOWER); |
| 1009 | break; |
| 1010 | case CLASS_PRINT: |
| 1011 | EMIT(NFA_CLASS_PRINT); |
| 1012 | break; |
| 1013 | case CLASS_PUNCT: |
| 1014 | EMIT(NFA_CLASS_PUNCT); |
| 1015 | break; |
| 1016 | case CLASS_SPACE: |
| 1017 | EMIT(NFA_CLASS_SPACE); |
| 1018 | break; |
| 1019 | case CLASS_UPPER: |
| 1020 | EMIT(NFA_CLASS_UPPER); |
| 1021 | break; |
| 1022 | case CLASS_XDIGIT: |
| 1023 | EMIT(NFA_CLASS_XDIGIT); |
| 1024 | break; |
| 1025 | case CLASS_TAB: |
| 1026 | EMIT(NFA_CLASS_TAB); |
| 1027 | break; |
| 1028 | case CLASS_RETURN: |
| 1029 | EMIT(NFA_CLASS_RETURN); |
| 1030 | break; |
| 1031 | case CLASS_BACKSPACE: |
| 1032 | EMIT(NFA_CLASS_BACKSPACE); |
| 1033 | break; |
| 1034 | case CLASS_ESCAPE: |
| 1035 | EMIT(NFA_CLASS_ESCAPE); |
| 1036 | break; |
| 1037 | } |
| 1038 | TRY_NEG(); |
| 1039 | EMIT_GLUE(); |
| 1040 | continue; |
| 1041 | } |
| 1042 | /* Try equivalence class [=a=] and the like */ |
| 1043 | if (equiclass != 0) |
| 1044 | { |
| 1045 | result = nfa_emit_equi_class(equiclass, negated); |
| 1046 | if (result == FAIL) |
| 1047 | { |
| 1048 | /* should never happen */ |
| 1049 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1050 | } |
| 1051 | EMIT_GLUE(); |
| 1052 | continue; |
| 1053 | } |
| 1054 | /* Try collating class like [. .] */ |
| 1055 | if (collclass != 0) |
| 1056 | { |
| 1057 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1058 | /* Will emit the proper atom at the end of the |
| 1059 | * while loop. */ |
| 1060 | } |
| 1061 | } |
| 1062 | /* Try a range like 'a-x' or '\t-z' */ |
| 1063 | if (*regparse == '-') |
| 1064 | { |
| 1065 | emit_range = TRUE; |
| 1066 | startc = oldstartc; |
| 1067 | nfa_inc(®parse); |
| 1068 | continue; /* reading the end of the range */ |
| 1069 | } |
| 1070 | |
| 1071 | /* Now handle simple and escaped characters. |
| 1072 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1073 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1074 | * 'cpoptions' is not included. |
| 1075 | * Posix doesn't recognize backslash at all. |
| 1076 | */ |
| 1077 | if (*regparse == '\\' |
| 1078 | && !cpo_bsl |
| 1079 | && regparse + 1 <= endp |
| 1080 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
| 1081 | || (!cpo_lit |
| 1082 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1083 | != NULL) |
| 1084 | ) |
| 1085 | ) |
| 1086 | { |
| 1087 | nfa_inc(®parse); |
| 1088 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1089 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1090 | startc = reg_string ? NL : NFA_NEWL; |
| 1091 | else |
| 1092 | if (*regparse == 'd' |
| 1093 | || *regparse == 'o' |
| 1094 | || *regparse == 'x' |
| 1095 | || *regparse == 'u' |
| 1096 | || *regparse == 'U' |
| 1097 | ) |
| 1098 | { |
| 1099 | /* TODO(RE) This needs more testing */ |
| 1100 | startc = coll_get_char(); |
| 1101 | got_coll_char = TRUE; |
| 1102 | nfa_dec(®parse); |
| 1103 | } |
| 1104 | else |
| 1105 | { |
| 1106 | /* \r,\t,\e,\b */ |
| 1107 | startc = backslash_trans(*regparse); |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* Normal printable char */ |
| 1112 | if (startc == -1) |
| 1113 | #ifdef FEAT_MBYTE |
| 1114 | startc = (*mb_ptr2char)(regparse); |
| 1115 | #else |
| 1116 | startc = *regparse; |
| 1117 | #endif |
| 1118 | |
| 1119 | /* Previous char was '-', so this char is end of range. */ |
| 1120 | if (emit_range) |
| 1121 | { |
| 1122 | endc = startc; startc = oldstartc; |
| 1123 | if (startc > endc) |
| 1124 | EMSG_RET_FAIL(_(e_invrange)); |
| 1125 | #ifdef FEAT_MBYTE |
| 1126 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
| 1127 | || (*mb_char2len)(endc) > 1)) |
| 1128 | { |
| 1129 | if (endc > startc + 256) |
| 1130 | EMSG_RET_FAIL(_(e_invrange)); |
| 1131 | /* Emit the range. "startc" was already emitted, so |
| 1132 | * skip it. */ |
| 1133 | for (c = startc + 1; c <= endc; c++) |
| 1134 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1135 | EMIT(c); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1136 | TRY_NEG(); |
| 1137 | EMIT_GLUE(); |
| 1138 | } |
| 1139 | emit_range = FALSE; |
| 1140 | } |
| 1141 | else |
| 1142 | #endif |
| 1143 | { |
| 1144 | #ifdef EBCDIC |
| 1145 | int alpha_only = FALSE; |
| 1146 | |
| 1147 | /* for alphabetical range skip the gaps |
| 1148 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1149 | if (isalpha(startc) && isalpha(endc)) |
| 1150 | alpha_only = TRUE; |
| 1151 | #endif |
| 1152 | /* Emit the range. "startc" was already emitted, so |
| 1153 | * skip it. */ |
| 1154 | for (c = startc + 1; c <= endc; c++) |
| 1155 | #ifdef EBCDIC |
| 1156 | if (!alpha_only || isalpha(startc)) |
| 1157 | #endif |
| 1158 | { |
| 1159 | EMIT(c); |
| 1160 | TRY_NEG(); |
| 1161 | EMIT_GLUE(); |
| 1162 | } |
| 1163 | emit_range = FALSE; |
| 1164 | } |
| 1165 | } |
| 1166 | else |
| 1167 | { |
| 1168 | /* |
| 1169 | * This char (startc) is not part of a range. Just |
| 1170 | * emit it. |
| 1171 | * |
| 1172 | * Normally, simply emit startc. But if we get char |
| 1173 | * code=0 from a collating char, then replace it with |
| 1174 | * 0x0a. |
| 1175 | * |
| 1176 | * This is needed to completely mimic the behaviour of |
| 1177 | * the backtracking engine. |
| 1178 | */ |
| 1179 | if (got_coll_char == TRUE && startc == 0) |
| 1180 | EMIT(0x0a); |
| 1181 | else |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1182 | EMIT(startc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1183 | TRY_NEG(); |
| 1184 | EMIT_GLUE(); |
| 1185 | } |
| 1186 | |
| 1187 | nfa_inc(®parse); |
| 1188 | } /* while (p < endp) */ |
| 1189 | |
| 1190 | nfa_dec(®parse); |
| 1191 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1192 | { |
| 1193 | EMIT('-'); |
| 1194 | TRY_NEG(); |
| 1195 | EMIT_GLUE(); |
| 1196 | } |
| 1197 | nfa_inc(®parse); |
| 1198 | |
| 1199 | if (extra == ADD_NL) /* \_[] also matches \n */ |
| 1200 | { |
| 1201 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1202 | TRY_NEG(); |
| 1203 | EMIT_GLUE(); |
| 1204 | } |
| 1205 | |
| 1206 | /* skip the trailing ] */ |
| 1207 | regparse = endp; |
| 1208 | nfa_inc(®parse); |
| 1209 | if (negated == TRUE) |
| 1210 | { |
| 1211 | /* Mark end of negated char range */ |
| 1212 | EMIT(NFA_END_NEG_RANGE); |
| 1213 | EMIT(NFA_CONCAT); |
| 1214 | } |
| 1215 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1216 | } /* if exists closing ] */ |
| 1217 | |
| 1218 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1219 | { |
| 1220 | syntax_error = TRUE; |
| 1221 | EMSG_RET_FAIL(_(e_missingbracket)); |
| 1222 | } |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1223 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1224 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1225 | default: |
| 1226 | { |
| 1227 | #ifdef FEAT_MBYTE |
| 1228 | int plen; |
| 1229 | |
| 1230 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1231 | /* plen is length of current char with composing chars */ |
| 1232 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1233 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1234 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1235 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1236 | int i = 0; |
| 1237 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1238 | /* A base character plus composing characters, or just one |
| 1239 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1240 | * This requires creating a separate atom as if enclosing |
| 1241 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1242 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1243 | * building the postfix form, not the NFA itself; |
| 1244 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1245 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1246 | for (;;) |
| 1247 | { |
| 1248 | EMIT(c); |
| 1249 | if (i > 0) |
| 1250 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1251 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1252 | break; |
| 1253 | c = utf_ptr2char(old_regparse + i); |
| 1254 | } |
| 1255 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1256 | regparse = old_regparse + plen; |
| 1257 | } |
| 1258 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1259 | #endif |
| 1260 | { |
| 1261 | c = no_Magic(c); |
| 1262 | EMIT(c); |
| 1263 | } |
| 1264 | return OK; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | #undef TRY_NEG |
| 1269 | #undef EMIT_GLUE |
| 1270 | |
| 1271 | return OK; |
| 1272 | } |
| 1273 | |
| 1274 | /* |
| 1275 | * Parse something followed by possible [*+=]. |
| 1276 | * |
| 1277 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1278 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1279 | * characters: "", "a", "aa", etc. |
| 1280 | * |
| 1281 | * piece ::= atom |
| 1282 | * or atom multi |
| 1283 | */ |
| 1284 | static int |
| 1285 | nfa_regpiece() |
| 1286 | { |
| 1287 | int i; |
| 1288 | int op; |
| 1289 | int ret; |
| 1290 | long minval, maxval; |
| 1291 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
| 1292 | char_u *old_regparse, *new_regparse; |
| 1293 | int c2; |
| 1294 | int *old_post_ptr, *my_post_start; |
| 1295 | int old_regnpar; |
| 1296 | int quest; |
| 1297 | |
| 1298 | /* Save the current position in the regexp, so that we can use it if |
| 1299 | * <atom>{m,n} is next. */ |
| 1300 | old_regparse = regparse; |
| 1301 | /* Save current number of open parenthesis, so we can use it if |
| 1302 | * <atom>{m,n} is next */ |
| 1303 | old_regnpar = regnpar; |
| 1304 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
| 1305 | my_post_start = post_ptr; |
| 1306 | |
| 1307 | ret = nfa_regatom(); |
| 1308 | if (ret == FAIL) |
| 1309 | return FAIL; /* cascaded error */ |
| 1310 | |
| 1311 | op = peekchr(); |
| 1312 | if (re_multi_type(op) == NOT_MULTI) |
| 1313 | return OK; |
| 1314 | |
| 1315 | skipchr(); |
| 1316 | switch (op) |
| 1317 | { |
| 1318 | case Magic('*'): |
| 1319 | EMIT(NFA_STAR); |
| 1320 | break; |
| 1321 | |
| 1322 | case Magic('+'): |
| 1323 | /* |
| 1324 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1325 | * first and only submatch would be "aaa". But the backtracking |
| 1326 | * engine interprets the plus as "try matching one more time", and |
| 1327 | * a* matches a second time at the end of the input, the empty |
| 1328 | * string. |
| 1329 | * The submatch will the empty string. |
| 1330 | * |
| 1331 | * In order to be consistent with the old engine, we disable |
| 1332 | * NFA_PLUS, and replace <atom>+ with <atom><atom>* |
| 1333 | */ |
| 1334 | /* EMIT(NFA_PLUS); */ |
| 1335 | regnpar = old_regnpar; |
| 1336 | regparse = old_regparse; |
| 1337 | curchr = -1; |
| 1338 | if (nfa_regatom() == FAIL) |
| 1339 | return FAIL; |
| 1340 | EMIT(NFA_STAR); |
| 1341 | EMIT(NFA_CONCAT); |
| 1342 | skipchr(); /* skip the \+ */ |
| 1343 | break; |
| 1344 | |
| 1345 | case Magic('@'): |
| 1346 | op = no_Magic(getchr()); |
| 1347 | switch(op) |
| 1348 | { |
| 1349 | case '=': |
| 1350 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
| 1351 | break; |
| 1352 | case '!': |
| 1353 | case '<': |
| 1354 | case '>': |
| 1355 | /* Not supported yet */ |
| 1356 | return FAIL; |
| 1357 | default: |
| 1358 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1359 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1360 | return FAIL; |
| 1361 | } |
| 1362 | break; |
| 1363 | |
| 1364 | case Magic('?'): |
| 1365 | case Magic('='): |
| 1366 | EMIT(NFA_QUEST); |
| 1367 | break; |
| 1368 | |
| 1369 | case Magic('{'): |
| 1370 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1371 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1372 | * version of '?' |
| 1373 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1374 | * parenthesis have the same id |
| 1375 | */ |
| 1376 | |
| 1377 | greedy = TRUE; |
| 1378 | c2 = peekchr(); |
| 1379 | if (c2 == '-' || c2 == Magic('-')) |
| 1380 | { |
| 1381 | skipchr(); |
| 1382 | greedy = FALSE; |
| 1383 | } |
| 1384 | if (!read_limits(&minval, &maxval)) |
| 1385 | { |
| 1386 | syntax_error = TRUE; |
| 1387 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
| 1388 | } |
| 1389 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 1390 | * <atom>* */ |
| 1391 | if (minval == 0 && maxval == MAX_LIMIT && greedy) |
| 1392 | { |
| 1393 | EMIT(NFA_STAR); |
| 1394 | break; |
| 1395 | } |
| 1396 | |
| 1397 | if (maxval > NFA_BRACES_MAXLIMIT) |
| 1398 | { |
| 1399 | /* This would yield a huge automaton and use too much memory. |
| 1400 | * Revert to old engine */ |
| 1401 | return FAIL; |
| 1402 | } |
| 1403 | |
| 1404 | /* Special case: x{0} or x{-0} */ |
| 1405 | if (maxval == 0) |
| 1406 | { |
| 1407 | /* Ignore result of previous call to nfa_regatom() */ |
| 1408 | post_ptr = my_post_start; |
| 1409 | /* NFA_SKIP_CHAR has 0-length and works everywhere */ |
| 1410 | EMIT(NFA_SKIP_CHAR); |
| 1411 | return OK; |
| 1412 | } |
| 1413 | |
| 1414 | /* Ignore previous call to nfa_regatom() */ |
| 1415 | post_ptr = my_post_start; |
| 1416 | /* Save pos after the repeated atom and the \{} */ |
| 1417 | new_regparse = regparse; |
| 1418 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1419 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 1420 | for (i = 0; i < maxval; i++) |
| 1421 | { |
| 1422 | /* Goto beginning of the repeated atom */ |
| 1423 | regparse = old_regparse; |
| 1424 | curchr = -1; |
| 1425 | /* Restore count of parenthesis */ |
| 1426 | regnpar = old_regnpar; |
| 1427 | old_post_ptr = post_ptr; |
| 1428 | if (nfa_regatom() == FAIL) |
| 1429 | return FAIL; |
| 1430 | /* after "minval" times, atoms are optional */ |
| 1431 | if (i + 1 > minval) |
| 1432 | EMIT(quest); |
| 1433 | if (old_post_ptr != my_post_start) |
| 1434 | EMIT(NFA_CONCAT); |
| 1435 | } |
| 1436 | |
| 1437 | /* Go to just after the repeated atom and the \{} */ |
| 1438 | regparse = new_regparse; |
| 1439 | curchr = -1; |
| 1440 | |
| 1441 | break; |
| 1442 | |
| 1443 | |
| 1444 | default: |
| 1445 | break; |
| 1446 | } /* end switch */ |
| 1447 | |
| 1448 | if (re_multi_type(peekchr()) != NOT_MULTI) |
| 1449 | { |
| 1450 | /* Can't have a multi follow a multi. */ |
| 1451 | syntax_error = TRUE; |
| 1452 | EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !")); |
| 1453 | } |
| 1454 | |
| 1455 | return OK; |
| 1456 | } |
| 1457 | |
| 1458 | /* |
| 1459 | * Parse one or more pieces, concatenated. It matches a match for the |
| 1460 | * first piece, followed by a match for the second piece, etc. Example: |
| 1461 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 1462 | * |
| 1463 | * concat ::= piece |
| 1464 | * or piece piece |
| 1465 | * or piece piece piece |
| 1466 | * etc. |
| 1467 | */ |
| 1468 | static int |
| 1469 | nfa_regconcat() |
| 1470 | { |
| 1471 | int cont = TRUE; |
| 1472 | int first = TRUE; |
| 1473 | |
| 1474 | while (cont) |
| 1475 | { |
| 1476 | switch (peekchr()) |
| 1477 | { |
| 1478 | case NUL: |
| 1479 | case Magic('|'): |
| 1480 | case Magic('&'): |
| 1481 | case Magic(')'): |
| 1482 | cont = FALSE; |
| 1483 | break; |
| 1484 | |
| 1485 | case Magic('Z'): |
| 1486 | #ifdef FEAT_MBYTE |
| 1487 | regflags |= RF_ICOMBINE; |
| 1488 | #endif |
| 1489 | skipchr_keepstart(); |
| 1490 | break; |
| 1491 | case Magic('c'): |
| 1492 | regflags |= RF_ICASE; |
| 1493 | skipchr_keepstart(); |
| 1494 | break; |
| 1495 | case Magic('C'): |
| 1496 | regflags |= RF_NOICASE; |
| 1497 | skipchr_keepstart(); |
| 1498 | break; |
| 1499 | case Magic('v'): |
| 1500 | reg_magic = MAGIC_ALL; |
| 1501 | skipchr_keepstart(); |
| 1502 | curchr = -1; |
| 1503 | break; |
| 1504 | case Magic('m'): |
| 1505 | reg_magic = MAGIC_ON; |
| 1506 | skipchr_keepstart(); |
| 1507 | curchr = -1; |
| 1508 | break; |
| 1509 | case Magic('M'): |
| 1510 | reg_magic = MAGIC_OFF; |
| 1511 | skipchr_keepstart(); |
| 1512 | curchr = -1; |
| 1513 | break; |
| 1514 | case Magic('V'): |
| 1515 | reg_magic = MAGIC_NONE; |
| 1516 | skipchr_keepstart(); |
| 1517 | curchr = -1; |
| 1518 | break; |
| 1519 | |
| 1520 | default: |
| 1521 | if (nfa_regpiece() == FAIL) |
| 1522 | return FAIL; |
| 1523 | if (first == FALSE) |
| 1524 | EMIT(NFA_CONCAT); |
| 1525 | else |
| 1526 | first = FALSE; |
| 1527 | break; |
| 1528 | } |
| 1529 | } |
| 1530 | |
| 1531 | return OK; |
| 1532 | } |
| 1533 | |
| 1534 | /* |
| 1535 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 1536 | * last concat, but only if all the preceding concats also match at the same |
| 1537 | * position. Examples: |
| 1538 | * "foobeep\&..." matches "foo" in "foobeep". |
| 1539 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 1540 | * |
| 1541 | * branch ::= concat |
| 1542 | * or concat \& concat |
| 1543 | * or concat \& concat \& concat |
| 1544 | * etc. |
| 1545 | */ |
| 1546 | static int |
| 1547 | nfa_regbranch() |
| 1548 | { |
| 1549 | int ch; |
| 1550 | int *old_post_ptr; |
| 1551 | |
| 1552 | old_post_ptr = post_ptr; |
| 1553 | |
| 1554 | /* First branch, possibly the only one */ |
| 1555 | if (nfa_regconcat() == FAIL) |
| 1556 | return FAIL; |
| 1557 | |
| 1558 | ch = peekchr(); |
| 1559 | /* Try next concats */ |
| 1560 | while (ch == Magic('&')) |
| 1561 | { |
| 1562 | skipchr(); |
| 1563 | EMIT(NFA_NOPEN); |
| 1564 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
| 1565 | old_post_ptr = post_ptr; |
| 1566 | if (nfa_regconcat() == FAIL) |
| 1567 | return FAIL; |
| 1568 | /* if concat is empty, skip a input char. But do emit a node */ |
| 1569 | if (old_post_ptr == post_ptr) |
| 1570 | EMIT(NFA_SKIP_CHAR); |
| 1571 | EMIT(NFA_CONCAT); |
| 1572 | ch = peekchr(); |
| 1573 | } |
| 1574 | |
| 1575 | /* Even if a branch is empty, emit one node for it */ |
| 1576 | if (old_post_ptr == post_ptr) |
| 1577 | EMIT(NFA_SKIP_CHAR); |
| 1578 | |
| 1579 | return OK; |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 1584 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 1585 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 1586 | * is used. |
| 1587 | * |
| 1588 | * pattern ::= branch |
| 1589 | * or branch \| branch |
| 1590 | * or branch \| branch \| branch |
| 1591 | * etc. |
| 1592 | */ |
| 1593 | static int |
| 1594 | nfa_reg(paren) |
| 1595 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 1596 | { |
| 1597 | int parno = 0; |
| 1598 | |
| 1599 | #ifdef FEAT_SYN_HL |
| 1600 | #endif |
| 1601 | if (paren == REG_PAREN) |
| 1602 | { |
| 1603 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
| 1604 | { |
| 1605 | syntax_error = TRUE; |
| 1606 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
| 1607 | } |
| 1608 | parno = regnpar++; |
| 1609 | } |
| 1610 | |
| 1611 | if (nfa_regbranch() == FAIL) |
| 1612 | return FAIL; /* cascaded error */ |
| 1613 | |
| 1614 | while (peekchr() == Magic('|')) |
| 1615 | { |
| 1616 | skipchr(); |
| 1617 | if (nfa_regbranch() == FAIL) |
| 1618 | return FAIL; /* cascaded error */ |
| 1619 | EMIT(NFA_OR); |
| 1620 | } |
| 1621 | |
| 1622 | /* Check for proper termination. */ |
| 1623 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 1624 | { |
| 1625 | syntax_error = TRUE; |
| 1626 | if (paren == REG_NPAREN) |
| 1627 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 1628 | else |
| 1629 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 1630 | } |
| 1631 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 1632 | { |
| 1633 | syntax_error = TRUE; |
| 1634 | if (peekchr() == Magic(')')) |
| 1635 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 1636 | else |
| 1637 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 1638 | } |
| 1639 | /* |
| 1640 | * Here we set the flag allowing back references to this set of |
| 1641 | * parentheses. |
| 1642 | */ |
| 1643 | if (paren == REG_PAREN) |
| 1644 | { |
| 1645 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 1646 | EMIT(NFA_MOPEN + parno); |
| 1647 | } |
| 1648 | |
| 1649 | return OK; |
| 1650 | } |
| 1651 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1652 | #ifdef DEBUG |
| 1653 | static char_u code[50]; |
| 1654 | |
| 1655 | static void |
| 1656 | nfa_set_code(c) |
| 1657 | int c; |
| 1658 | { |
| 1659 | int addnl = FALSE; |
| 1660 | |
| 1661 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 1662 | { |
| 1663 | addnl = TRUE; |
| 1664 | c -= ADD_NL; |
| 1665 | } |
| 1666 | |
| 1667 | STRCPY(code, ""); |
| 1668 | switch (c) |
| 1669 | { |
| 1670 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 1671 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 1672 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 1673 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 1674 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 1675 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 1676 | |
| 1677 | case NFA_PREV_ATOM_NO_WIDTH: |
| 1678 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
| 1679 | case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break; |
| 1680 | case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break; |
| 1681 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
| 1682 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
| 1683 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1684 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 1685 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
| 1686 | |
| 1687 | case NFA_MOPEN + 0: |
| 1688 | case NFA_MOPEN + 1: |
| 1689 | case NFA_MOPEN + 2: |
| 1690 | case NFA_MOPEN + 3: |
| 1691 | case NFA_MOPEN + 4: |
| 1692 | case NFA_MOPEN + 5: |
| 1693 | case NFA_MOPEN + 6: |
| 1694 | case NFA_MOPEN + 7: |
| 1695 | case NFA_MOPEN + 8: |
| 1696 | case NFA_MOPEN + 9: |
| 1697 | STRCPY(code, "NFA_MOPEN(x)"); |
| 1698 | code[10] = c - NFA_MOPEN + '0'; |
| 1699 | break; |
| 1700 | case NFA_MCLOSE + 0: |
| 1701 | case NFA_MCLOSE + 1: |
| 1702 | case NFA_MCLOSE + 2: |
| 1703 | case NFA_MCLOSE + 3: |
| 1704 | case NFA_MCLOSE + 4: |
| 1705 | case NFA_MCLOSE + 5: |
| 1706 | case NFA_MCLOSE + 6: |
| 1707 | case NFA_MCLOSE + 7: |
| 1708 | case NFA_MCLOSE + 8: |
| 1709 | case NFA_MCLOSE + 9: |
| 1710 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 1711 | code[11] = c - NFA_MCLOSE + '0'; |
| 1712 | break; |
| 1713 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 1714 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 1715 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 1716 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
| 1717 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
| 1718 | case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break; |
| 1719 | case NFA_NOT: STRCPY(code, "NFA_NOT "); break; |
| 1720 | case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; |
| 1721 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
| 1722 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 1723 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
| 1724 | case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break; |
| 1725 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 1726 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 1727 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 1728 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 1729 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 1730 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 1731 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 1732 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 1733 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 1734 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 1735 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 1736 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 1737 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 1738 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 1739 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 1740 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 1741 | |
| 1742 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 1743 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 1744 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 1745 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 1746 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 1747 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 1748 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 1749 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 1750 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 1751 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 1752 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 1753 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 1754 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 1755 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 1756 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 1757 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 1758 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 1759 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 1760 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 1761 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 1762 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 1763 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 1764 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 1765 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 1766 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 1767 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 1768 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
| 1769 | |
| 1770 | default: |
| 1771 | STRCPY(code, "CHAR(x)"); |
| 1772 | code[5] = c; |
| 1773 | } |
| 1774 | |
| 1775 | if (addnl == TRUE) |
| 1776 | STRCAT(code, " + NEWLINE "); |
| 1777 | |
| 1778 | } |
| 1779 | |
| 1780 | #ifdef ENABLE_LOG |
| 1781 | static FILE *log_fd; |
| 1782 | |
| 1783 | /* |
| 1784 | * Print the postfix notation of the current regexp. |
| 1785 | */ |
| 1786 | static void |
| 1787 | nfa_postfix_dump(expr, retval) |
| 1788 | char_u *expr; |
| 1789 | int retval; |
| 1790 | { |
| 1791 | int *p; |
| 1792 | FILE *f; |
| 1793 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 1794 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1795 | if (f != NULL) |
| 1796 | { |
| 1797 | fprintf(f, "\n-------------------------\n"); |
| 1798 | if (retval == FAIL) |
| 1799 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 1800 | else if (retval == OK) |
| 1801 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 1802 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 1803 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1804 | { |
| 1805 | nfa_set_code(*p); |
| 1806 | fprintf(f, "%s, ", code); |
| 1807 | } |
| 1808 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 1809 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1810 | fprintf(f, "%d ", *p); |
| 1811 | fprintf(f, "\n\n"); |
| 1812 | fclose(f); |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | /* |
| 1817 | * Print the NFA starting with a root node "state". |
| 1818 | */ |
| 1819 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1820 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1821 | FILE *debugf; |
| 1822 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1823 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1824 | garray_T indent; |
| 1825 | |
| 1826 | ga_init2(&indent, 1, 64); |
| 1827 | ga_append(&indent, '\0'); |
| 1828 | nfa_print_state2(debugf, state, &indent); |
| 1829 | ga_clear(&indent); |
| 1830 | } |
| 1831 | |
| 1832 | static void |
| 1833 | nfa_print_state2(debugf, state, indent) |
| 1834 | FILE *debugf; |
| 1835 | nfa_state_T *state; |
| 1836 | garray_T *indent; |
| 1837 | { |
| 1838 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1839 | |
| 1840 | if (state == NULL) |
| 1841 | return; |
| 1842 | |
| 1843 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1844 | |
| 1845 | /* Output indent */ |
| 1846 | p = (char_u *)indent->ga_data; |
| 1847 | if (indent->ga_len >= 3) |
| 1848 | { |
| 1849 | int last = indent->ga_len - 3; |
| 1850 | char_u save[2]; |
| 1851 | |
| 1852 | STRNCPY(save, &p[last], 2); |
| 1853 | STRNCPY(&p[last], "+-", 2); |
| 1854 | fprintf(debugf, " %s", p); |
| 1855 | STRNCPY(&p[last], save, 2); |
| 1856 | } |
| 1857 | else |
| 1858 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1859 | |
| 1860 | nfa_set_code(state->c); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1861 | fprintf(debugf, "%s%s (%d) (id=%d)\n", |
| 1862 | state->negated ? "NOT " : "", code, state->c, abs(state->id)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1863 | if (state->id < 0) |
| 1864 | return; |
| 1865 | |
| 1866 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1867 | |
| 1868 | /* grow indent for state->out */ |
| 1869 | indent->ga_len -= 1; |
| 1870 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 1871 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1872 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 1873 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1874 | ga_append(indent, '\0'); |
| 1875 | |
| 1876 | nfa_print_state2(debugf, state->out, indent); |
| 1877 | |
| 1878 | /* replace last part of indent for state->out1 */ |
| 1879 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 1880 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1881 | ga_append(indent, '\0'); |
| 1882 | |
| 1883 | nfa_print_state2(debugf, state->out1, indent); |
| 1884 | |
| 1885 | /* shrink indent */ |
| 1886 | indent->ga_len -= 3; |
| 1887 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /* |
| 1891 | * Print the NFA state machine. |
| 1892 | */ |
| 1893 | static void |
| 1894 | nfa_dump(prog) |
| 1895 | nfa_regprog_T *prog; |
| 1896 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 1897 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1898 | |
| 1899 | if (debugf != NULL) |
| 1900 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1901 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1902 | fclose(debugf); |
| 1903 | } |
| 1904 | } |
| 1905 | #endif /* ENABLE_LOG */ |
| 1906 | #endif /* DEBUG */ |
| 1907 | |
| 1908 | /* |
| 1909 | * Parse r.e. @expr and convert it into postfix form. |
| 1910 | * Return the postfix string on success, NULL otherwise. |
| 1911 | */ |
| 1912 | static int * |
| 1913 | re2post() |
| 1914 | { |
| 1915 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 1916 | return NULL; |
| 1917 | EMIT(NFA_MOPEN); |
| 1918 | return post_start; |
| 1919 | } |
| 1920 | |
| 1921 | /* NB. Some of the code below is inspired by Russ's. */ |
| 1922 | |
| 1923 | /* |
| 1924 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 1925 | * if c == MATCH, no arrows out; matching state. |
| 1926 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 1927 | * If c < 256, labeled arrow with character c to out. |
| 1928 | */ |
| 1929 | |
| 1930 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 1931 | |
| 1932 | /* |
| 1933 | * Allocate and initialize nfa_state_T. |
| 1934 | */ |
| 1935 | static nfa_state_T * |
| 1936 | new_state(c, out, out1) |
| 1937 | int c; |
| 1938 | nfa_state_T *out; |
| 1939 | nfa_state_T *out1; |
| 1940 | { |
| 1941 | nfa_state_T *s; |
| 1942 | |
| 1943 | if (istate >= nstate) |
| 1944 | return NULL; |
| 1945 | |
| 1946 | s = &state_ptr[istate++]; |
| 1947 | |
| 1948 | s->c = c; |
| 1949 | s->out = out; |
| 1950 | s->out1 = out1; |
| 1951 | |
| 1952 | s->id = istate; |
| 1953 | s->lastlist = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1954 | s->visits = 0; |
| 1955 | s->negated = FALSE; |
| 1956 | |
| 1957 | return s; |
| 1958 | } |
| 1959 | |
| 1960 | /* |
| 1961 | * A partially built NFA without the matching state filled in. |
| 1962 | * Frag_T.start points at the start state. |
| 1963 | * Frag_T.out is a list of places that need to be set to the |
| 1964 | * next state for this fragment. |
| 1965 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 1966 | |
| 1967 | /* Since the out pointers in the list are always |
| 1968 | * uninitialized, we use the pointers themselves |
| 1969 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1970 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 1971 | union Ptrlist |
| 1972 | { |
| 1973 | Ptrlist *next; |
| 1974 | nfa_state_T *s; |
| 1975 | }; |
| 1976 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1977 | struct Frag |
| 1978 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 1979 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1980 | Ptrlist *out; |
| 1981 | }; |
| 1982 | typedef struct Frag Frag_T; |
| 1983 | |
| 1984 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 1985 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 1986 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 1987 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 1988 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 1989 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 1990 | |
| 1991 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 1992 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1993 | */ |
| 1994 | static Frag_T |
| 1995 | frag(start, out) |
| 1996 | nfa_state_T *start; |
| 1997 | Ptrlist *out; |
| 1998 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 1999 | Frag_T n; |
| 2000 | |
| 2001 | n.start = start; |
| 2002 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2003 | return n; |
| 2004 | } |
| 2005 | |
| 2006 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2007 | * Create singleton list containing just outp. |
| 2008 | */ |
| 2009 | static Ptrlist * |
| 2010 | list1(outp) |
| 2011 | nfa_state_T **outp; |
| 2012 | { |
| 2013 | Ptrlist *l; |
| 2014 | |
| 2015 | l = (Ptrlist *)outp; |
| 2016 | l->next = NULL; |
| 2017 | return l; |
| 2018 | } |
| 2019 | |
| 2020 | /* |
| 2021 | * Patch the list of states at out to point to start. |
| 2022 | */ |
| 2023 | static void |
| 2024 | patch(l, s) |
| 2025 | Ptrlist *l; |
| 2026 | nfa_state_T *s; |
| 2027 | { |
| 2028 | Ptrlist *next; |
| 2029 | |
| 2030 | for (; l; l = next) |
| 2031 | { |
| 2032 | next = l->next; |
| 2033 | l->s = s; |
| 2034 | } |
| 2035 | } |
| 2036 | |
| 2037 | |
| 2038 | /* |
| 2039 | * Join the two lists l1 and l2, returning the combination. |
| 2040 | */ |
| 2041 | static Ptrlist * |
| 2042 | append(l1, l2) |
| 2043 | Ptrlist *l1; |
| 2044 | Ptrlist *l2; |
| 2045 | { |
| 2046 | Ptrlist *oldl1; |
| 2047 | |
| 2048 | oldl1 = l1; |
| 2049 | while (l1->next) |
| 2050 | l1 = l1->next; |
| 2051 | l1->next = l2; |
| 2052 | return oldl1; |
| 2053 | } |
| 2054 | |
| 2055 | /* |
| 2056 | * Stack used for transforming postfix form into NFA. |
| 2057 | */ |
| 2058 | static Frag_T empty; |
| 2059 | |
| 2060 | static void |
| 2061 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2062 | int *postfix UNUSED; |
| 2063 | int *end UNUSED; |
| 2064 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2065 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2066 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2067 | FILE *df; |
| 2068 | int *p2; |
| 2069 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2070 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2071 | if (df) |
| 2072 | { |
| 2073 | fprintf(df, "Error popping the stack!\n"); |
| 2074 | #ifdef DEBUG |
| 2075 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2076 | #endif |
| 2077 | fprintf(df, "Postfix form is: "); |
| 2078 | #ifdef DEBUG |
| 2079 | for (p2 = postfix; p2 < end; p2++) |
| 2080 | { |
| 2081 | nfa_set_code(*p2); |
| 2082 | fprintf(df, "%s, ", code); |
| 2083 | } |
| 2084 | nfa_set_code(*p); |
| 2085 | fprintf(df, "\nCurrent position is: "); |
| 2086 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2087 | { |
| 2088 | nfa_set_code(*p2); |
| 2089 | fprintf(df, "%s, ", code); |
| 2090 | } |
| 2091 | #else |
| 2092 | for (p2 = postfix; p2 < end; p2++) |
| 2093 | { |
| 2094 | fprintf(df, "%d, ", *p2); |
| 2095 | } |
| 2096 | fprintf(df, "\nCurrent position is: "); |
| 2097 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2098 | { |
| 2099 | fprintf(df, "%d, ", *p2); |
| 2100 | } |
| 2101 | #endif |
| 2102 | fprintf(df, "\n--------------------------\n"); |
| 2103 | fclose(df); |
| 2104 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2105 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2106 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2107 | } |
| 2108 | |
| 2109 | /* |
| 2110 | * Push an item onto the stack. |
| 2111 | */ |
| 2112 | static void |
| 2113 | st_push(s, p, stack_end) |
| 2114 | Frag_T s; |
| 2115 | Frag_T **p; |
| 2116 | Frag_T *stack_end; |
| 2117 | { |
| 2118 | Frag_T *stackp = *p; |
| 2119 | |
| 2120 | if (stackp >= stack_end) |
| 2121 | return; |
| 2122 | *stackp = s; |
| 2123 | *p = *p + 1; |
| 2124 | } |
| 2125 | |
| 2126 | /* |
| 2127 | * Pop an item from the stack. |
| 2128 | */ |
| 2129 | static Frag_T |
| 2130 | st_pop(p, stack) |
| 2131 | Frag_T **p; |
| 2132 | Frag_T *stack; |
| 2133 | { |
| 2134 | Frag_T *stackp; |
| 2135 | |
| 2136 | *p = *p - 1; |
| 2137 | stackp = *p; |
| 2138 | if (stackp < stack) |
| 2139 | return empty; |
| 2140 | return **p; |
| 2141 | } |
| 2142 | |
| 2143 | /* |
| 2144 | * Convert a postfix form into its equivalent NFA. |
| 2145 | * Return the NFA start state on success, NULL otherwise. |
| 2146 | */ |
| 2147 | static nfa_state_T * |
| 2148 | post2nfa(postfix, end, nfa_calc_size) |
| 2149 | int *postfix; |
| 2150 | int *end; |
| 2151 | int nfa_calc_size; |
| 2152 | { |
| 2153 | int *p; |
| 2154 | int mopen; |
| 2155 | int mclose; |
| 2156 | Frag_T *stack = NULL; |
| 2157 | Frag_T *stackp = NULL; |
| 2158 | Frag_T *stack_end = NULL; |
| 2159 | Frag_T e1; |
| 2160 | Frag_T e2; |
| 2161 | Frag_T e; |
| 2162 | nfa_state_T *s; |
| 2163 | nfa_state_T *s1; |
| 2164 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2165 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2166 | |
| 2167 | if (postfix == NULL) |
| 2168 | return NULL; |
| 2169 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2170 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2171 | #define POP() st_pop(&stackp, stack); \ |
| 2172 | if (stackp < stack) \ |
| 2173 | { \ |
| 2174 | st_error(postfix, end, p); \ |
| 2175 | return NULL; \ |
| 2176 | } |
| 2177 | |
| 2178 | if (nfa_calc_size == FALSE) |
| 2179 | { |
| 2180 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2181 | stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2182 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2183 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2184 | } |
| 2185 | |
| 2186 | for (p = postfix; p < end; ++p) |
| 2187 | { |
| 2188 | switch (*p) |
| 2189 | { |
| 2190 | case NFA_CONCAT: |
| 2191 | /* Catenation. |
| 2192 | * Pay attention: this operator does not exist |
| 2193 | * in the r.e. itself (it is implicit, really). |
| 2194 | * It is added when r.e. is translated to postfix |
| 2195 | * form in re2post(). |
| 2196 | * |
| 2197 | * No new state added here. */ |
| 2198 | if (nfa_calc_size == TRUE) |
| 2199 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2200 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2201 | break; |
| 2202 | } |
| 2203 | e2 = POP(); |
| 2204 | e1 = POP(); |
| 2205 | patch(e1.out, e2.start); |
| 2206 | PUSH(frag(e1.start, e2.out)); |
| 2207 | break; |
| 2208 | |
| 2209 | case NFA_NOT: |
| 2210 | /* Negation of a character */ |
| 2211 | if (nfa_calc_size == TRUE) |
| 2212 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2213 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2214 | break; |
| 2215 | } |
| 2216 | e1 = POP(); |
| 2217 | e1.start->negated = TRUE; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2218 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2219 | if (e1.start->c == NFA_COMPOSING) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2220 | e1.start->out1->negated = TRUE; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2221 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2222 | PUSH(e1); |
| 2223 | break; |
| 2224 | |
| 2225 | case NFA_OR: |
| 2226 | /* Alternation */ |
| 2227 | if (nfa_calc_size == TRUE) |
| 2228 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2229 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2230 | break; |
| 2231 | } |
| 2232 | e2 = POP(); |
| 2233 | e1 = POP(); |
| 2234 | s = new_state(NFA_SPLIT, e1.start, e2.start); |
| 2235 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2236 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2237 | PUSH(frag(s, append(e1.out, e2.out))); |
| 2238 | break; |
| 2239 | |
| 2240 | case NFA_STAR: |
| 2241 | /* Zero or more */ |
| 2242 | if (nfa_calc_size == TRUE) |
| 2243 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2244 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2245 | break; |
| 2246 | } |
| 2247 | e = POP(); |
| 2248 | s = new_state(NFA_SPLIT, e.start, NULL); |
| 2249 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2250 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2251 | patch(e.out, s); |
| 2252 | PUSH(frag(s, list1(&s->out1))); |
| 2253 | break; |
| 2254 | |
| 2255 | case NFA_QUEST: |
| 2256 | /* one or zero atoms=> greedy match */ |
| 2257 | if (nfa_calc_size == TRUE) |
| 2258 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2259 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2260 | break; |
| 2261 | } |
| 2262 | e = POP(); |
| 2263 | s = new_state(NFA_SPLIT, e.start, NULL); |
| 2264 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2265 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2266 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 2267 | break; |
| 2268 | |
| 2269 | case NFA_QUEST_NONGREEDY: |
| 2270 | /* zero or one atoms => non-greedy match */ |
| 2271 | if (nfa_calc_size == TRUE) |
| 2272 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2273 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2274 | break; |
| 2275 | } |
| 2276 | e = POP(); |
| 2277 | s = new_state(NFA_SPLIT, NULL, e.start); |
| 2278 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2279 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2280 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 2281 | break; |
| 2282 | |
| 2283 | case NFA_PLUS: |
| 2284 | /* One or more */ |
| 2285 | if (nfa_calc_size == TRUE) |
| 2286 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2287 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2288 | break; |
| 2289 | } |
| 2290 | e = POP(); |
| 2291 | s = new_state(NFA_SPLIT, e.start, NULL); |
| 2292 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2293 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2294 | patch(e.out, s); |
| 2295 | PUSH(frag(e.start, list1(&s->out1))); |
| 2296 | break; |
| 2297 | |
| 2298 | case NFA_SKIP_CHAR: |
| 2299 | /* Symbol of 0-length, Used in a repetition |
| 2300 | * with max/min count of 0 */ |
| 2301 | if (nfa_calc_size == TRUE) |
| 2302 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2303 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2304 | break; |
| 2305 | } |
| 2306 | s = new_state(NFA_SKIP_CHAR, NULL, NULL); |
| 2307 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2308 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2309 | PUSH(frag(s, list1(&s->out))); |
| 2310 | break; |
| 2311 | |
| 2312 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2313 | /* The \@= operator: match the preceding atom with 0 width. |
| 2314 | * Surrounds the preceding atom with START_INVISIBLE and |
| 2315 | * END_INVISIBLE, similarly to MOPEN. |
| 2316 | */ |
| 2317 | /* TODO: Maybe this drops the speed? */ |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2318 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2319 | |
| 2320 | if (nfa_calc_size == TRUE) |
| 2321 | { |
| 2322 | nstate += 2; |
| 2323 | break; |
| 2324 | } |
| 2325 | e = POP(); |
| 2326 | s1 = new_state(NFA_END_INVISIBLE, NULL, NULL); |
| 2327 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2328 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2329 | patch(e.out, s1); |
| 2330 | |
| 2331 | s = new_state(NFA_START_INVISIBLE, e.start, s1); |
| 2332 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2333 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2334 | PUSH(frag(s, list1(&s1->out))); |
| 2335 | break; |
| 2336 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2337 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2338 | case NFA_COMPOSING: /* char with composing char */ |
| 2339 | #if 0 |
| 2340 | /* TODO */ |
| 2341 | if (regflags & RF_ICOMBINE) |
| 2342 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 2343 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2344 | } |
| 2345 | #endif |
| 2346 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2347 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2348 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2349 | case NFA_MOPEN + 0: /* Submatch */ |
| 2350 | case NFA_MOPEN + 1: |
| 2351 | case NFA_MOPEN + 2: |
| 2352 | case NFA_MOPEN + 3: |
| 2353 | case NFA_MOPEN + 4: |
| 2354 | case NFA_MOPEN + 5: |
| 2355 | case NFA_MOPEN + 6: |
| 2356 | case NFA_MOPEN + 7: |
| 2357 | case NFA_MOPEN + 8: |
| 2358 | case NFA_MOPEN + 9: |
| 2359 | case NFA_NOPEN: /* \%( "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2360 | if (nfa_calc_size == TRUE) |
| 2361 | { |
| 2362 | nstate += 2; |
| 2363 | break; |
| 2364 | } |
| 2365 | |
| 2366 | mopen = *p; |
| 2367 | switch (*p) |
| 2368 | { |
| 2369 | case NFA_NOPEN: |
| 2370 | mclose = NFA_NCLOSE; |
| 2371 | break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2372 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2373 | case NFA_COMPOSING: |
| 2374 | mclose = NFA_END_COMPOSING; |
| 2375 | break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2376 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2377 | default: |
| 2378 | /* NFA_MOPEN(0) ... NFA_MOPEN(9) */ |
| 2379 | mclose = *p + NSUBEXP; |
| 2380 | break; |
| 2381 | } |
| 2382 | |
| 2383 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 2384 | * the empty regexp "". In this case, the NFA will be |
| 2385 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 2386 | * empty groups of parenthesis, and empty mbyte chars */ |
| 2387 | if (stackp == stack) |
| 2388 | { |
| 2389 | s = new_state(mopen, NULL, NULL); |
| 2390 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2391 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2392 | s1 = new_state(mclose, NULL, NULL); |
| 2393 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2394 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2395 | patch(list1(&s->out), s1); |
| 2396 | PUSH(frag(s, list1(&s1->out))); |
| 2397 | break; |
| 2398 | } |
| 2399 | |
| 2400 | /* At least one node was emitted before NFA_MOPEN, so |
| 2401 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 2402 | e = POP(); |
| 2403 | s = new_state(mopen, e.start, NULL); /* `(' */ |
| 2404 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2405 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2406 | |
| 2407 | s1 = new_state(mclose, NULL, NULL); /* `)' */ |
| 2408 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2409 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2410 | patch(e.out, s1); |
| 2411 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2412 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2413 | if (mopen == NFA_COMPOSING) |
| 2414 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2415 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2416 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2417 | |
| 2418 | PUSH(frag(s, list1(&s1->out))); |
| 2419 | break; |
| 2420 | |
| 2421 | case NFA_ZSTART: |
| 2422 | case NFA_ZEND: |
| 2423 | default: |
| 2424 | /* Operands */ |
| 2425 | if (nfa_calc_size == TRUE) |
| 2426 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2427 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2428 | break; |
| 2429 | } |
| 2430 | s = new_state(*p, NULL, NULL); |
| 2431 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2432 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2433 | PUSH(frag(s, list1(&s->out))); |
| 2434 | break; |
| 2435 | |
| 2436 | } /* switch(*p) */ |
| 2437 | |
| 2438 | } /* for(p = postfix; *p; ++p) */ |
| 2439 | |
| 2440 | if (nfa_calc_size == TRUE) |
| 2441 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2442 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2443 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2444 | } |
| 2445 | |
| 2446 | e = POP(); |
| 2447 | if (stackp != stack) |
| 2448 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 2449 | |
| 2450 | if (istate >= nstate) |
| 2451 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 2452 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2453 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 2454 | matchstate->c = NFA_MATCH; |
| 2455 | matchstate->out = matchstate->out1 = NULL; |
| 2456 | |
| 2457 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2458 | ret = e.start; |
| 2459 | |
| 2460 | theend: |
| 2461 | vim_free(stack); |
| 2462 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2463 | |
| 2464 | #undef POP1 |
| 2465 | #undef PUSH1 |
| 2466 | #undef POP2 |
| 2467 | #undef PUSH2 |
| 2468 | #undef POP |
| 2469 | #undef PUSH |
| 2470 | } |
| 2471 | |
| 2472 | /**************************************************************** |
| 2473 | * NFA execution code. |
| 2474 | ****************************************************************/ |
| 2475 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2476 | typedef struct |
| 2477 | { |
| 2478 | int in_use; /* number of subexpr with useful info */ |
| 2479 | |
| 2480 | /* When REG_MULTI is TRUE multilist is used, otherwise linelist. */ |
| 2481 | union |
| 2482 | { |
| 2483 | struct multipos |
| 2484 | { |
| 2485 | lpos_T start; |
| 2486 | lpos_T end; |
| 2487 | } multilist[NSUBEXP]; |
| 2488 | struct linepos |
| 2489 | { |
| 2490 | char_u *start; |
| 2491 | char_u *end; |
| 2492 | } linelist[NSUBEXP]; |
| 2493 | }; |
| 2494 | } regsub_T; |
| 2495 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2496 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2497 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2498 | { |
| 2499 | nfa_state_T *state; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2500 | regsub_T sub; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2501 | } nfa_thread_T; |
| 2502 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2503 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2504 | typedef struct |
| 2505 | { |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2506 | nfa_thread_T *t; |
| 2507 | int n; |
| 2508 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2509 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2510 | /* Used during execution: whether a match has been found. */ |
| 2511 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2512 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2513 | static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int off, int lid)); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2514 | static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *m, int lid, int *ip)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2515 | |
| 2516 | static void |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2517 | addstate(l, state, m, off, lid) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2518 | nfa_list_T *l; /* runtime state list */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2519 | nfa_state_T *state; /* state to update */ |
| 2520 | regsub_T *m; /* pointers to subexpressions */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2521 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2522 | int lid; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2523 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2524 | int subidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2525 | nfa_thread_T *lastthread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2526 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2527 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2528 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2529 | int i; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2530 | |
| 2531 | if (l == NULL || state == NULL) |
| 2532 | return; |
| 2533 | |
| 2534 | switch (state->c) |
| 2535 | { |
| 2536 | case NFA_SPLIT: |
| 2537 | case NFA_NOT: |
| 2538 | case NFA_NOPEN: |
| 2539 | case NFA_NCLOSE: |
| 2540 | case NFA_MCLOSE: |
| 2541 | case NFA_MCLOSE + 1: |
| 2542 | case NFA_MCLOSE + 2: |
| 2543 | case NFA_MCLOSE + 3: |
| 2544 | case NFA_MCLOSE + 4: |
| 2545 | case NFA_MCLOSE + 5: |
| 2546 | case NFA_MCLOSE + 6: |
| 2547 | case NFA_MCLOSE + 7: |
| 2548 | case NFA_MCLOSE + 8: |
| 2549 | case NFA_MCLOSE + 9: |
| 2550 | /* Do not remember these nodes in list "thislist" or "nextlist" */ |
| 2551 | break; |
| 2552 | |
| 2553 | default: |
| 2554 | if (state->lastlist == lid) |
| 2555 | { |
| 2556 | if (++state->visits > 2) |
| 2557 | return; |
| 2558 | } |
| 2559 | else |
| 2560 | { |
| 2561 | /* add the state to the list */ |
| 2562 | state->lastlist = lid; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2563 | lastthread = &l->t[l->n++]; |
| 2564 | lastthread->state = state; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2565 | lastthread->sub.in_use = m->in_use; |
| 2566 | if (m->in_use > 0) |
| 2567 | { |
| 2568 | /* Copy the match start and end positions. */ |
| 2569 | if (REG_MULTI) |
| 2570 | mch_memmove(&lastthread->sub.multilist[0], |
| 2571 | &m->multilist[0], |
| 2572 | sizeof(struct multipos) * m->in_use); |
| 2573 | else |
| 2574 | mch_memmove(&lastthread->sub.linelist[0], |
| 2575 | &m->linelist[0], |
| 2576 | sizeof(struct linepos) * m->in_use); |
| 2577 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2578 | } |
| 2579 | } |
| 2580 | |
| 2581 | #ifdef ENABLE_LOG |
| 2582 | nfa_set_code(state->c); |
| 2583 | fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n", |
| 2584 | abs(state->id), code, state->c); |
| 2585 | #endif |
| 2586 | switch (state->c) |
| 2587 | { |
| 2588 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2589 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2590 | break; |
| 2591 | |
| 2592 | case NFA_SPLIT: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2593 | addstate(l, state->out, m, off, lid); |
| 2594 | addstate(l, state->out1, m, off, lid); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2595 | break; |
| 2596 | |
| 2597 | case NFA_SKIP_CHAR: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2598 | addstate(l, state->out, m, off, lid); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2599 | break; |
| 2600 | |
| 2601 | #if 0 |
| 2602 | case NFA_END_NEG_RANGE: |
| 2603 | /* Nothing to handle here. nfa_regmatch() will take care of it */ |
| 2604 | break; |
| 2605 | |
| 2606 | case NFA_NOT: |
| 2607 | EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !")); |
| 2608 | #ifdef ENABLE_LOG |
| 2609 | fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n"); |
| 2610 | #endif |
| 2611 | break; |
| 2612 | |
| 2613 | case NFA_COMPOSING: |
| 2614 | /* nfa_regmatch() will match all the bytes of this composing char. */ |
| 2615 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2616 | #endif |
| 2617 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2618 | case NFA_NOPEN: |
| 2619 | case NFA_NCLOSE: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2620 | addstate(l, state->out, m, off, lid); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2621 | break; |
| 2622 | |
| 2623 | /* If this state is reached, then a recursive call of nfa_regmatch() |
| 2624 | * succeeded. the next call saves the found submatches in the |
| 2625 | * first state after the "invisible" branch. */ |
| 2626 | #if 0 |
| 2627 | case NFA_END_INVISIBLE: |
| 2628 | break; |
| 2629 | #endif |
| 2630 | |
| 2631 | case NFA_MOPEN + 0: |
| 2632 | case NFA_MOPEN + 1: |
| 2633 | case NFA_MOPEN + 2: |
| 2634 | case NFA_MOPEN + 3: |
| 2635 | case NFA_MOPEN + 4: |
| 2636 | case NFA_MOPEN + 5: |
| 2637 | case NFA_MOPEN + 6: |
| 2638 | case NFA_MOPEN + 7: |
| 2639 | case NFA_MOPEN + 8: |
| 2640 | case NFA_MOPEN + 9: |
| 2641 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2642 | if (state->c == NFA_ZSTART) |
| 2643 | subidx = 0; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2644 | else |
| 2645 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2646 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2647 | /* Set the position (with "off") in the subexpression. Save and |
| 2648 | * restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2649 | if (REG_MULTI) |
| 2650 | { |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2651 | if (subidx < m->in_use) |
| 2652 | { |
| 2653 | save_lpos = m->multilist[subidx].start; |
| 2654 | save_in_use = -1; |
| 2655 | } |
| 2656 | else |
| 2657 | { |
| 2658 | save_in_use = m->in_use; |
| 2659 | for (i = m->in_use; i < subidx; ++i) |
| 2660 | { |
| 2661 | m->multilist[i].start.lnum = -1; |
| 2662 | m->multilist[i].end.lnum = -1; |
| 2663 | } |
| 2664 | m->in_use = subidx + 1; |
| 2665 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2666 | if (off == -1) |
| 2667 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2668 | m->multilist[subidx].start.lnum = reglnum + 1; |
| 2669 | m->multilist[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2670 | } |
| 2671 | else |
| 2672 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2673 | m->multilist[subidx].start.lnum = reglnum; |
| 2674 | m->multilist[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2675 | (colnr_T)(reginput - regline + off); |
| 2676 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2677 | } |
| 2678 | else |
| 2679 | { |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2680 | if (subidx < m->in_use) |
| 2681 | { |
| 2682 | save_ptr = m->linelist[subidx].start; |
| 2683 | save_in_use = -1; |
| 2684 | } |
| 2685 | else |
| 2686 | { |
| 2687 | save_in_use = m->in_use; |
| 2688 | for (i = m->in_use; i < subidx; ++i) |
| 2689 | { |
| 2690 | m->linelist[i].start = NULL; |
| 2691 | m->linelist[i].end = NULL; |
| 2692 | } |
| 2693 | m->in_use = subidx + 1; |
| 2694 | } |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2695 | m->linelist[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2696 | } |
| 2697 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2698 | addstate(l, state->out, m, off, lid); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2699 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2700 | if (save_in_use == -1) |
| 2701 | { |
| 2702 | if (REG_MULTI) |
| 2703 | m->multilist[subidx].start = save_lpos; |
| 2704 | else |
| 2705 | m->linelist[subidx].start = save_ptr; |
| 2706 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2707 | else |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2708 | m->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2709 | break; |
| 2710 | |
| 2711 | case NFA_MCLOSE + 0: |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 2712 | if (nfa_has_zend) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2713 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2714 | addstate(l, state->out, m, off, lid); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2715 | break; |
| 2716 | } |
| 2717 | case NFA_MCLOSE + 1: |
| 2718 | case NFA_MCLOSE + 2: |
| 2719 | case NFA_MCLOSE + 3: |
| 2720 | case NFA_MCLOSE + 4: |
| 2721 | case NFA_MCLOSE + 5: |
| 2722 | case NFA_MCLOSE + 6: |
| 2723 | case NFA_MCLOSE + 7: |
| 2724 | case NFA_MCLOSE + 8: |
| 2725 | case NFA_MCLOSE + 9: |
| 2726 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2727 | if (state->c == NFA_ZEND) |
| 2728 | subidx = 0; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2729 | else |
| 2730 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2731 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2732 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 2733 | * has done that. */ |
| 2734 | save_in_use = m->in_use; |
| 2735 | if (m->in_use <= subidx) |
| 2736 | m->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2737 | if (REG_MULTI) |
| 2738 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2739 | save_lpos = m->multilist[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2740 | if (off == -1) |
| 2741 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2742 | m->multilist[subidx].end.lnum = reglnum + 1; |
| 2743 | m->multilist[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2744 | } |
| 2745 | else |
| 2746 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2747 | m->multilist[subidx].end.lnum = reglnum; |
| 2748 | m->multilist[subidx].end.col = |
| 2749 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2750 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2751 | } |
| 2752 | else |
| 2753 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2754 | save_ptr = m->linelist[subidx].end; |
| 2755 | m->linelist[subidx].end = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2756 | } |
| 2757 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2758 | addstate(l, state->out, m, off, lid); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2759 | |
| 2760 | if (REG_MULTI) |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2761 | m->multilist[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2762 | else |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2763 | m->linelist[subidx].end = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2764 | m->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2765 | break; |
| 2766 | } |
| 2767 | } |
| 2768 | |
| 2769 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2770 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 2771 | * Used for zero-width matches, next state to use is the added one. |
| 2772 | * This makes sure the order of states to be tried does not change, which |
| 2773 | * matters for alternatives. |
| 2774 | */ |
| 2775 | static void |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2776 | addstate_here(l, state, m, lid, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2777 | nfa_list_T *l; /* runtime state list */ |
| 2778 | nfa_state_T *state; /* state to update */ |
| 2779 | regsub_T *m; /* pointers to subexpressions */ |
| 2780 | int lid; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2781 | int *ip; |
| 2782 | { |
| 2783 | int tlen = l->n; |
| 2784 | int count; |
| 2785 | int i = *ip; |
| 2786 | |
| 2787 | /* first add the state(s) at the end, so that we know how many there are */ |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2788 | addstate(l, state, m, 0, lid); |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2789 | |
| 2790 | /* when "*ip" was at the end of the list, nothing to do */ |
| 2791 | if (i + 1 == tlen) |
| 2792 | return; |
| 2793 | |
| 2794 | /* re-order to put the new state at the current position */ |
| 2795 | count = l->n - tlen; |
| 2796 | if (count > 1) |
| 2797 | { |
| 2798 | /* make space for new states, then move them from the |
| 2799 | * end to the current position */ |
| 2800 | mch_memmove(&(l->t[i + count]), |
| 2801 | &(l->t[i + 1]), |
| 2802 | sizeof(nfa_thread_T) * (l->n - i - 1)); |
| 2803 | mch_memmove(&(l->t[i]), |
| 2804 | &(l->t[l->n - 1]), |
| 2805 | sizeof(nfa_thread_T) * count); |
| 2806 | } |
| 2807 | else |
| 2808 | { |
| 2809 | /* overwrite the current state */ |
| 2810 | l->t[i] = l->t[l->n - 1]; |
| 2811 | } |
| 2812 | --l->n; |
| 2813 | *ip = i - 1; |
| 2814 | } |
| 2815 | |
| 2816 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2817 | * Check character class "class" against current character c. |
| 2818 | */ |
| 2819 | static int |
| 2820 | check_char_class(class, c) |
| 2821 | int class; |
| 2822 | int c; |
| 2823 | { |
| 2824 | switch (class) |
| 2825 | { |
| 2826 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2827 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2828 | return OK; |
| 2829 | break; |
| 2830 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2831 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2832 | return OK; |
| 2833 | break; |
| 2834 | case NFA_CLASS_BLANK: |
| 2835 | if (c == ' ' || c == '\t') |
| 2836 | return OK; |
| 2837 | break; |
| 2838 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2839 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2840 | return OK; |
| 2841 | break; |
| 2842 | case NFA_CLASS_DIGIT: |
| 2843 | if (VIM_ISDIGIT(c)) |
| 2844 | return OK; |
| 2845 | break; |
| 2846 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2847 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2848 | return OK; |
| 2849 | break; |
| 2850 | case NFA_CLASS_LOWER: |
| 2851 | if (MB_ISLOWER(c)) |
| 2852 | return OK; |
| 2853 | break; |
| 2854 | case NFA_CLASS_PRINT: |
| 2855 | if (vim_isprintc(c)) |
| 2856 | return OK; |
| 2857 | break; |
| 2858 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2859 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2860 | return OK; |
| 2861 | break; |
| 2862 | case NFA_CLASS_SPACE: |
| 2863 | if ((c >=9 && c <= 13) || (c == ' ')) |
| 2864 | return OK; |
| 2865 | break; |
| 2866 | case NFA_CLASS_UPPER: |
| 2867 | if (MB_ISUPPER(c)) |
| 2868 | return OK; |
| 2869 | break; |
| 2870 | case NFA_CLASS_XDIGIT: |
| 2871 | if (vim_isxdigit(c)) |
| 2872 | return OK; |
| 2873 | break; |
| 2874 | case NFA_CLASS_TAB: |
| 2875 | if (c == '\t') |
| 2876 | return OK; |
| 2877 | break; |
| 2878 | case NFA_CLASS_RETURN: |
| 2879 | if (c == '\r') |
| 2880 | return OK; |
| 2881 | break; |
| 2882 | case NFA_CLASS_BACKSPACE: |
| 2883 | if (c == '\b') |
| 2884 | return OK; |
| 2885 | break; |
| 2886 | case NFA_CLASS_ESCAPE: |
| 2887 | if (c == '\033') |
| 2888 | return OK; |
| 2889 | break; |
| 2890 | |
| 2891 | default: |
| 2892 | /* should not be here :P */ |
| 2893 | EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class ")); |
| 2894 | } |
| 2895 | return FAIL; |
| 2896 | } |
| 2897 | |
| 2898 | /* |
| 2899 | * Set all NFA nodes' list ID equal to -1. |
| 2900 | */ |
| 2901 | static void |
| 2902 | nfa_set_neg_listids(start) |
| 2903 | nfa_state_T *start; |
| 2904 | { |
| 2905 | if (start == NULL) |
| 2906 | return; |
| 2907 | if (start->lastlist >= 0) |
| 2908 | { |
| 2909 | start->lastlist = -1; |
| 2910 | nfa_set_neg_listids(start->out); |
| 2911 | nfa_set_neg_listids(start->out1); |
| 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | /* |
| 2916 | * Set all NFA nodes' list ID equal to 0. |
| 2917 | */ |
| 2918 | static void |
| 2919 | nfa_set_null_listids(start) |
| 2920 | nfa_state_T *start; |
| 2921 | { |
| 2922 | if (start == NULL) |
| 2923 | return; |
| 2924 | if (start->lastlist == -1) |
| 2925 | { |
| 2926 | start->lastlist = 0; |
| 2927 | nfa_set_null_listids(start->out); |
| 2928 | nfa_set_null_listids(start->out1); |
| 2929 | } |
| 2930 | } |
| 2931 | |
| 2932 | /* |
| 2933 | * Save list IDs for all NFA states in "list". |
| 2934 | */ |
| 2935 | static void |
| 2936 | nfa_save_listids(start, list) |
| 2937 | nfa_state_T *start; |
| 2938 | int *list; |
| 2939 | { |
| 2940 | if (start == NULL) |
| 2941 | return; |
| 2942 | if (start->lastlist != -1) |
| 2943 | { |
| 2944 | list[abs(start->id)] = start->lastlist; |
| 2945 | start->lastlist = -1; |
| 2946 | nfa_save_listids(start->out, list); |
| 2947 | nfa_save_listids(start->out1, list); |
| 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | /* |
| 2952 | * Restore list IDs from "list" to all NFA states. |
| 2953 | */ |
| 2954 | static void |
| 2955 | nfa_restore_listids(start, list) |
| 2956 | nfa_state_T *start; |
| 2957 | int *list; |
| 2958 | { |
| 2959 | if (start == NULL) |
| 2960 | return; |
| 2961 | if (start->lastlist == -1) |
| 2962 | { |
| 2963 | start->lastlist = list[abs(start->id)]; |
| 2964 | nfa_restore_listids(start->out, list); |
| 2965 | nfa_restore_listids(start->out1, list); |
| 2966 | } |
| 2967 | } |
| 2968 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2969 | static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m)); |
| 2970 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2971 | /* |
| 2972 | * Main matching routine. |
| 2973 | * |
| 2974 | * Run NFA to determine whether it matches reginput. |
| 2975 | * |
| 2976 | * Return TRUE if there is a match, FALSE otherwise. |
| 2977 | * Note: Caller must ensure that: start != NULL. |
| 2978 | */ |
| 2979 | static int |
| 2980 | nfa_regmatch(start, submatch, m) |
| 2981 | nfa_state_T *start; |
| 2982 | regsub_T *submatch; |
| 2983 | regsub_T *m; |
| 2984 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2985 | int result; |
| 2986 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2987 | int flag = 0; |
| 2988 | int old_reglnum = -1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2989 | int go_to_nextline = FALSE; |
| 2990 | nfa_thread_T *t; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2991 | char_u *old_reginput = NULL; |
| 2992 | char_u *old_regline = NULL; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2993 | nfa_list_T list[3]; |
| 2994 | nfa_list_T *listtbl[2][2]; |
| 2995 | nfa_list_T *ll; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2996 | int listid = 1; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 2997 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2998 | nfa_list_T *thislist; |
| 2999 | nfa_list_T *nextlist; |
| 3000 | nfa_list_T *neglist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3001 | int *listids = NULL; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 3002 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3003 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3004 | |
| 3005 | if (debug == NULL) |
| 3006 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3007 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3008 | return FALSE; |
| 3009 | } |
| 3010 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3011 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3012 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3013 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3014 | size = (nstate + 1) * sizeof(nfa_thread_T); |
| 3015 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
| 3016 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
| 3017 | list[2].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3018 | if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL) |
| 3019 | goto theend; |
| 3020 | vim_memset(list[0].t, 0, size); |
| 3021 | vim_memset(list[1].t, 0, size); |
| 3022 | vim_memset(list[2].t, 0, size); |
| 3023 | |
| 3024 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3025 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3026 | if (log_fd != NULL) |
| 3027 | { |
| 3028 | fprintf(log_fd, "**********************************\n"); |
| 3029 | nfa_set_code(start->c); |
| 3030 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 3031 | abs(start->id), code); |
| 3032 | fprintf(log_fd, "**********************************\n"); |
| 3033 | } |
| 3034 | else |
| 3035 | { |
| 3036 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 3037 | log_fd = stderr; |
| 3038 | } |
| 3039 | #endif |
| 3040 | |
| 3041 | thislist = &list[0]; |
| 3042 | thislist->n = 0; |
| 3043 | nextlist = &list[1]; |
| 3044 | nextlist->n = 0; |
| 3045 | neglist = &list[2]; |
| 3046 | neglist->n = 0; |
| 3047 | #ifdef ENABLE_LOG |
| 3048 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 3049 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3050 | addstate(thislist, start, m, 0, listid); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3051 | |
| 3052 | /* There are two cases when the NFA advances: 1. input char matches the |
| 3053 | * NFA node and 2. input char does not match the NFA node, but the next |
| 3054 | * node is NFA_NOT. The following macro calls addstate() according to |
| 3055 | * these rules. It is used A LOT, so use the "listtbl" table for speed */ |
| 3056 | listtbl[0][0] = NULL; |
| 3057 | listtbl[0][1] = neglist; |
| 3058 | listtbl[1][0] = nextlist; |
| 3059 | listtbl[1][1] = NULL; |
| 3060 | #define ADD_POS_NEG_STATE(node) \ |
| 3061 | ll = listtbl[result ? 1 : 0][node->negated]; \ |
| 3062 | if (ll != NULL) \ |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3063 | addstate(ll, node->out , &t->sub, clen, listid + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3064 | |
| 3065 | |
| 3066 | /* |
| 3067 | * Run for each character. |
| 3068 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3069 | for (;;) |
| 3070 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3071 | int curc; |
| 3072 | int clen; |
| 3073 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3074 | #ifdef FEAT_MBYTE |
| 3075 | if (has_mbyte) |
| 3076 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3077 | curc = (*mb_ptr2char)(reginput); |
| 3078 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3079 | } |
| 3080 | else |
| 3081 | #endif |
| 3082 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3083 | curc = *reginput; |
| 3084 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3085 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3086 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3087 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3088 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3089 | go_to_nextline = FALSE; |
| 3090 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3091 | |
| 3092 | /* swap lists */ |
| 3093 | thislist = &list[flag]; |
| 3094 | nextlist = &list[flag ^= 1]; |
| 3095 | nextlist->n = 0; /* `clear' nextlist */ |
| 3096 | listtbl[1][0] = nextlist; |
| 3097 | ++listid; |
| 3098 | |
| 3099 | #ifdef ENABLE_LOG |
| 3100 | fprintf(log_fd, "------------------------------------------\n"); |
| 3101 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3102 | 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] | 3103 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3104 | { |
| 3105 | int i; |
| 3106 | |
| 3107 | for (i = 0; i < thislist->n; i++) |
| 3108 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 3109 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3110 | fprintf(log_fd, "\n"); |
| 3111 | #endif |
| 3112 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 3113 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3114 | fprintf(debug, "\n-------------------\n"); |
| 3115 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 3116 | /* |
| 3117 | * If the state lists are empty we can stop. |
| 3118 | */ |
| 3119 | if (thislist->n == 0 && neglist->n == 0) |
| 3120 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3121 | |
| 3122 | /* compute nextlist */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3123 | for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3124 | { |
| 3125 | if (neglist->n > 0) |
| 3126 | { |
| 3127 | t = &neglist->t[0]; |
Bram Moolenaar | 0fabe3f | 2013-05-21 12:34:17 +0200 | [diff] [blame] | 3128 | neglist->n--; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3129 | listidx--; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3130 | } |
| 3131 | else |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3132 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3133 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 3134 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3135 | nfa_set_code(t->state->c); |
| 3136 | fprintf(debug, "%s, ", code); |
| 3137 | #endif |
| 3138 | #ifdef ENABLE_LOG |
| 3139 | nfa_set_code(t->state->c); |
| 3140 | fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id), |
| 3141 | code, (int)t->state->c); |
| 3142 | #endif |
| 3143 | |
| 3144 | /* |
| 3145 | * Handle the possible codes of the current state. |
| 3146 | * The most important is NFA_MATCH. |
| 3147 | */ |
| 3148 | switch (t->state->c) |
| 3149 | { |
| 3150 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame^] | 3151 | { |
| 3152 | int j; |
| 3153 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3154 | nfa_match = TRUE; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3155 | submatch->in_use = t->sub.in_use; |
| 3156 | if (REG_MULTI) |
| 3157 | for (j = 0; j < submatch->in_use; j++) |
| 3158 | { |
| 3159 | submatch->multilist[j].start = t->sub.multilist[j].start; |
| 3160 | submatch->multilist[j].end = t->sub.multilist[j].end; |
| 3161 | } |
| 3162 | else |
| 3163 | for (j = 0; j < submatch->in_use; j++) |
| 3164 | { |
| 3165 | submatch->linelist[j].start = t->sub.linelist[j].start; |
| 3166 | submatch->linelist[j].end = t->sub.linelist[j].end; |
| 3167 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3168 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2cb8feb | 2013-05-26 23:13:07 +0200 | [diff] [blame] | 3169 | for (j = 0; j < t->sub.in_use; j++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3170 | if (REG_MULTI) |
| 3171 | fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d", |
| 3172 | j, |
Bram Moolenaar | 2cb8feb | 2013-05-26 23:13:07 +0200 | [diff] [blame] | 3173 | t->sub.multilist[j].start.col, |
| 3174 | (int)t->sub.multilist[j].start.lnum, |
| 3175 | t->sub.multilist[j].end.col, |
| 3176 | (int)t->sub.multilist[j].end.lnum); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3177 | else |
| 3178 | fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"", |
| 3179 | j, |
Bram Moolenaar | 2cb8feb | 2013-05-26 23:13:07 +0200 | [diff] [blame] | 3180 | (char *)t->sub.linelist[j].start, |
| 3181 | (char *)t->sub.linelist[j].end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3182 | fprintf(log_fd, "\n"); |
| 3183 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3184 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 3185 | * states at this position. When the list of states is going |
| 3186 | * to be empty quit without advancing, so that "reginput" is |
| 3187 | * correct. */ |
| 3188 | if (nextlist->n == 0 && neglist->n == 0) |
| 3189 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3190 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame^] | 3191 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3192 | |
| 3193 | case NFA_END_INVISIBLE: |
| 3194 | /* This is only encountered after a NFA_START_INVISIBLE node. |
| 3195 | * They surround a zero-width group, used with "\@=" and "\&". |
| 3196 | * If we got here, it means that the current "invisible" group |
| 3197 | * finished successfully, so return control to the parent |
| 3198 | * nfa_regmatch(). Submatches are stored in *m, and used in |
| 3199 | * the parent call. */ |
| 3200 | if (start->c == NFA_MOPEN + 0) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3201 | addstate_here(thislist, t->state->out, &t->sub, listid, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3202 | &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3203 | else |
| 3204 | { |
| 3205 | *m = t->sub; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3206 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3207 | } |
| 3208 | break; |
| 3209 | |
| 3210 | case NFA_START_INVISIBLE: |
| 3211 | /* Save global variables, and call nfa_regmatch() to check if |
| 3212 | * the current concat matches at this position. The concat |
| 3213 | * ends with the node NFA_END_INVISIBLE */ |
| 3214 | old_reginput = reginput; |
| 3215 | old_regline = regline; |
| 3216 | old_reglnum = reglnum; |
| 3217 | if (listids == NULL) |
| 3218 | { |
| 3219 | listids = (int *) lalloc(sizeof(int) * nstate, TRUE); |
| 3220 | if (listids == NULL) |
| 3221 | { |
| 3222 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 3223 | return 0; |
| 3224 | } |
| 3225 | } |
| 3226 | #ifdef ENABLE_LOG |
| 3227 | if (log_fd != stderr) |
| 3228 | fclose(log_fd); |
| 3229 | log_fd = NULL; |
| 3230 | #endif |
| 3231 | /* Have to clear the listid field of the NFA nodes, so that |
| 3232 | * nfa_regmatch() and addstate() can run properly after |
| 3233 | * recursion. */ |
| 3234 | nfa_save_listids(start, listids); |
| 3235 | nfa_set_null_listids(start); |
| 3236 | result = nfa_regmatch(t->state->out, submatch, m); |
| 3237 | nfa_set_neg_listids(start); |
| 3238 | nfa_restore_listids(start, listids); |
| 3239 | |
| 3240 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3241 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3242 | if (log_fd != NULL) |
| 3243 | { |
| 3244 | fprintf(log_fd, "****************************\n"); |
| 3245 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 3246 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 3247 | fprintf(log_fd, "****************************\n"); |
| 3248 | } |
| 3249 | else |
| 3250 | { |
| 3251 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 3252 | log_fd = stderr; |
| 3253 | } |
| 3254 | #endif |
| 3255 | if (result == TRUE) |
| 3256 | { |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame^] | 3257 | int j; |
| 3258 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3259 | /* Restore position in input text */ |
| 3260 | reginput = old_reginput; |
| 3261 | regline = old_regline; |
| 3262 | reglnum = old_reglnum; |
| 3263 | /* Copy submatch info from the recursive call */ |
| 3264 | if (REG_MULTI) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3265 | for (j = 1; j < m->in_use; j++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3266 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3267 | t->sub.multilist[j].start = m->multilist[j].start; |
| 3268 | t->sub.multilist[j].end = m->multilist[j].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3269 | } |
| 3270 | else |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3271 | for (j = 1; j < m->in_use; j++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3272 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3273 | t->sub.linelist[j].start = m->linelist[j].start; |
| 3274 | t->sub.linelist[j].end = m->linelist[j].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3275 | } |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3276 | t->sub.in_use = m->in_use; |
| 3277 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3278 | /* t->state->out1 is the corresponding END_INVISIBLE node */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3279 | addstate_here(thislist, t->state->out1->out, &t->sub, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3280 | listid, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3281 | } |
| 3282 | else |
| 3283 | { |
| 3284 | /* continue with next input char */ |
| 3285 | reginput = old_reginput; |
| 3286 | } |
| 3287 | break; |
| 3288 | |
| 3289 | case NFA_BOL: |
| 3290 | if (reginput == regline) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3291 | addstate_here(thislist, t->state->out, &t->sub, listid, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3292 | &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3293 | break; |
| 3294 | |
| 3295 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3296 | if (curc == NUL) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3297 | addstate_here(thislist, t->state->out, &t->sub, listid, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3298 | &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3299 | break; |
| 3300 | |
| 3301 | case NFA_BOW: |
| 3302 | { |
| 3303 | int bow = TRUE; |
| 3304 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3305 | if (curc == NUL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3306 | bow = FALSE; |
| 3307 | #ifdef FEAT_MBYTE |
| 3308 | else if (has_mbyte) |
| 3309 | { |
| 3310 | int this_class; |
| 3311 | |
| 3312 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3313 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3314 | if (this_class <= 1) |
| 3315 | bow = FALSE; |
| 3316 | else if (reg_prev_class() == this_class) |
| 3317 | bow = FALSE; |
| 3318 | } |
| 3319 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3320 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3321 | || (reginput > regline |
| 3322 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3323 | bow = FALSE; |
| 3324 | if (bow) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3325 | addstate_here(thislist, t->state->out, &t->sub, listid, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3326 | &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3327 | break; |
| 3328 | } |
| 3329 | |
| 3330 | case NFA_EOW: |
| 3331 | { |
| 3332 | int eow = TRUE; |
| 3333 | |
| 3334 | if (reginput == regline) |
| 3335 | eow = FALSE; |
| 3336 | #ifdef FEAT_MBYTE |
| 3337 | else if (has_mbyte) |
| 3338 | { |
| 3339 | int this_class, prev_class; |
| 3340 | |
| 3341 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3342 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3343 | prev_class = reg_prev_class(); |
| 3344 | if (this_class == prev_class |
| 3345 | || prev_class == 0 || prev_class == 1) |
| 3346 | eow = FALSE; |
| 3347 | } |
| 3348 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3349 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3350 | || (reginput[0] != NUL |
| 3351 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3352 | eow = FALSE; |
| 3353 | if (eow) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3354 | addstate_here(thislist, t->state->out, &t->sub, listid, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3355 | &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3356 | break; |
| 3357 | } |
| 3358 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3359 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3360 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3361 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3362 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3363 | int len = 0; |
| 3364 | nfa_state_T *end; |
| 3365 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3366 | int cchars[MAX_MCO]; |
| 3367 | int ccount = 0; |
| 3368 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3369 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3370 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3371 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 3372 | if (utf_iscomposing(sta->c)) |
| 3373 | { |
| 3374 | /* Only match composing character(s), ignore base |
| 3375 | * character. Used for ".{composing}" and "{composing}" |
| 3376 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3377 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 3378 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 3379 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3380 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 3381 | /* If \Z was present, then ignore composing characters. |
| 3382 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3383 | /* TODO: How about negated? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3384 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3385 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3386 | else |
| 3387 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3388 | while (sta->c != NFA_END_COMPOSING) |
| 3389 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3390 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3391 | |
| 3392 | /* Check base character matches first, unless ignored. */ |
| 3393 | else if (len > 0 || mc == sta->c) |
| 3394 | { |
| 3395 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3396 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3397 | len += mb_char2len(mc); |
| 3398 | sta = sta->out; |
| 3399 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3400 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3401 | /* We don't care about the order of composing characters. |
| 3402 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3403 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3404 | { |
| 3405 | mc = mb_ptr2char(reginput + len); |
| 3406 | cchars[ccount++] = mc; |
| 3407 | len += mb_char2len(mc); |
| 3408 | if (ccount == MAX_MCO) |
| 3409 | break; |
| 3410 | } |
| 3411 | |
| 3412 | /* Check that each composing char in the pattern matches a |
| 3413 | * composing char in the text. We do not check if all |
| 3414 | * composing chars are matched. */ |
| 3415 | result = OK; |
| 3416 | while (sta->c != NFA_END_COMPOSING) |
| 3417 | { |
| 3418 | for (j = 0; j < ccount; ++j) |
| 3419 | if (cchars[j] == sta->c) |
| 3420 | break; |
| 3421 | if (j == ccount) |
| 3422 | { |
| 3423 | result = FAIL; |
| 3424 | break; |
| 3425 | } |
| 3426 | sta = sta->out; |
| 3427 | } |
| 3428 | } |
| 3429 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 3430 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3431 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3432 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3433 | ADD_POS_NEG_STATE(end); |
| 3434 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3435 | } |
| 3436 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3437 | |
| 3438 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3439 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 3440 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3441 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3442 | go_to_nextline = TRUE; |
| 3443 | /* Pass -1 for the offset, which means taking the position |
| 3444 | * at the start of the next line. */ |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3445 | addstate(nextlist, t->state->out, &t->sub, -1, listid + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3446 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3447 | else if (curc == '\n' && reg_line_lbr) |
| 3448 | { |
| 3449 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3450 | addstate(nextlist, t->state->out, &t->sub, 1, listid + 1); |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3451 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3452 | break; |
| 3453 | |
| 3454 | case NFA_CLASS_ALNUM: |
| 3455 | case NFA_CLASS_ALPHA: |
| 3456 | case NFA_CLASS_BLANK: |
| 3457 | case NFA_CLASS_CNTRL: |
| 3458 | case NFA_CLASS_DIGIT: |
| 3459 | case NFA_CLASS_GRAPH: |
| 3460 | case NFA_CLASS_LOWER: |
| 3461 | case NFA_CLASS_PRINT: |
| 3462 | case NFA_CLASS_PUNCT: |
| 3463 | case NFA_CLASS_SPACE: |
| 3464 | case NFA_CLASS_UPPER: |
| 3465 | case NFA_CLASS_XDIGIT: |
| 3466 | case NFA_CLASS_TAB: |
| 3467 | case NFA_CLASS_RETURN: |
| 3468 | case NFA_CLASS_BACKSPACE: |
| 3469 | case NFA_CLASS_ESCAPE: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3470 | result = check_char_class(t->state->c, curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3471 | ADD_POS_NEG_STATE(t->state); |
| 3472 | break; |
| 3473 | |
| 3474 | case NFA_END_NEG_RANGE: |
| 3475 | /* This follows a series of negated nodes, like: |
| 3476 | * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3477 | if (curc > 0) |
| 3478 | addstate(nextlist, t->state->out, &t->sub, clen, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3479 | listid + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3480 | break; |
| 3481 | |
| 3482 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3483 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3484 | if (curc > 0) |
| 3485 | addstate(nextlist, t->state->out, &t->sub, clen, |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3486 | listid + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3487 | break; |
| 3488 | |
| 3489 | /* |
| 3490 | * Character classes like \a for alpha, \d for digit etc. |
| 3491 | */ |
| 3492 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3493 | result = vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3494 | ADD_POS_NEG_STATE(t->state); |
| 3495 | break; |
| 3496 | |
| 3497 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3498 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3499 | ADD_POS_NEG_STATE(t->state); |
| 3500 | break; |
| 3501 | |
| 3502 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3503 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3504 | ADD_POS_NEG_STATE(t->state); |
| 3505 | break; |
| 3506 | |
| 3507 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3508 | result = !VIM_ISDIGIT(curc) |
| 3509 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3510 | ADD_POS_NEG_STATE(t->state); |
| 3511 | break; |
| 3512 | |
| 3513 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3514 | result = vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3515 | ADD_POS_NEG_STATE(t->state); |
| 3516 | break; |
| 3517 | |
| 3518 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3519 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3520 | ADD_POS_NEG_STATE(t->state); |
| 3521 | break; |
| 3522 | |
| 3523 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | 0805049 | 2013-05-21 12:43:56 +0200 | [diff] [blame] | 3524 | result = ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3525 | ADD_POS_NEG_STATE(t->state); |
| 3526 | break; |
| 3527 | |
| 3528 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3529 | result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3530 | ADD_POS_NEG_STATE(t->state); |
| 3531 | break; |
| 3532 | |
| 3533 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3534 | result = vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3535 | ADD_POS_NEG_STATE(t->state); |
| 3536 | break; |
| 3537 | |
| 3538 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3539 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3540 | ADD_POS_NEG_STATE(t->state); |
| 3541 | break; |
| 3542 | |
| 3543 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3544 | result = ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3545 | ADD_POS_NEG_STATE(t->state); |
| 3546 | break; |
| 3547 | |
| 3548 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3549 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3550 | ADD_POS_NEG_STATE(t->state); |
| 3551 | break; |
| 3552 | |
| 3553 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3554 | result = ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3555 | ADD_POS_NEG_STATE(t->state); |
| 3556 | break; |
| 3557 | |
| 3558 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3559 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3560 | ADD_POS_NEG_STATE(t->state); |
| 3561 | break; |
| 3562 | |
| 3563 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3564 | result = ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3565 | ADD_POS_NEG_STATE(t->state); |
| 3566 | break; |
| 3567 | |
| 3568 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3569 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3570 | ADD_POS_NEG_STATE(t->state); |
| 3571 | break; |
| 3572 | |
| 3573 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3574 | result = ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3575 | ADD_POS_NEG_STATE(t->state); |
| 3576 | break; |
| 3577 | |
| 3578 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3579 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3580 | ADD_POS_NEG_STATE(t->state); |
| 3581 | break; |
| 3582 | |
| 3583 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3584 | result = ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3585 | ADD_POS_NEG_STATE(t->state); |
| 3586 | break; |
| 3587 | |
| 3588 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3589 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3590 | ADD_POS_NEG_STATE(t->state); |
| 3591 | break; |
| 3592 | |
| 3593 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3594 | result = ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3595 | ADD_POS_NEG_STATE(t->state); |
| 3596 | break; |
| 3597 | |
| 3598 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3599 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3600 | ADD_POS_NEG_STATE(t->state); |
| 3601 | break; |
| 3602 | |
| 3603 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3604 | result = ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3605 | ADD_POS_NEG_STATE(t->state); |
| 3606 | break; |
| 3607 | |
| 3608 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3609 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3610 | ADD_POS_NEG_STATE(t->state); |
| 3611 | break; |
| 3612 | |
| 3613 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3614 | result = ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3615 | ADD_POS_NEG_STATE(t->state); |
| 3616 | break; |
| 3617 | |
| 3618 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3619 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3620 | ADD_POS_NEG_STATE(t->state); |
| 3621 | break; |
| 3622 | |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 3623 | case NFA_MOPEN + 0: |
| 3624 | case NFA_MOPEN + 1: |
| 3625 | case NFA_MOPEN + 2: |
| 3626 | case NFA_MOPEN + 3: |
| 3627 | case NFA_MOPEN + 4: |
| 3628 | case NFA_MOPEN + 5: |
| 3629 | case NFA_MOPEN + 6: |
| 3630 | case NFA_MOPEN + 7: |
| 3631 | case NFA_MOPEN + 8: |
| 3632 | case NFA_MOPEN + 9: |
| 3633 | /* handled below */ |
| 3634 | break; |
| 3635 | |
| 3636 | case NFA_SKIP_CHAR: |
| 3637 | case NFA_ZSTART: |
| 3638 | /* TODO: should not happen? */ |
| 3639 | break; |
| 3640 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3641 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 3642 | { |
| 3643 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 3644 | |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 3645 | /* TODO: put this in #ifdef later */ |
| 3646 | if (c < -256) |
| 3647 | EMSGN("INTERNAL: Negative state char: %ld", c); |
| 3648 | if (is_Magic(c)) |
| 3649 | c = un_Magic(c); |
| 3650 | result = (c == curc); |
| 3651 | |
| 3652 | if (!result && ireg_ic) |
| 3653 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3654 | #ifdef FEAT_MBYTE |
| 3655 | /* If there is a composing character which is not being |
| 3656 | * ignored there can be no match. Match with composing |
| 3657 | * character uses NFA_COMPOSING above. */ |
| 3658 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3659 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3660 | result = FALSE; |
| 3661 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3662 | ADD_POS_NEG_STATE(t->state); |
| 3663 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 3664 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 3668 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 3669 | /* Look for the start of a match in the current position by adding the |
| 3670 | * start state to the list of states. |
| 3671 | * The first found match is the leftmost one, thus the order of states |
| 3672 | * matters! |
| 3673 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 3674 | * because recursive calls should only start in the first position. |
| 3675 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3676 | if (nfa_match == FALSE && start->c == NFA_MOPEN + 0 |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 3677 | && reglnum == 0 && clen != 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3678 | { |
| 3679 | #ifdef ENABLE_LOG |
| 3680 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 3681 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3682 | addstate(nextlist, start, m, clen, listid + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3683 | } |
| 3684 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3685 | #ifdef ENABLE_LOG |
| 3686 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3687 | { |
| 3688 | int i; |
| 3689 | |
| 3690 | for (i = 0; i < thislist->n; i++) |
| 3691 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 3692 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3693 | fprintf(log_fd, "\n"); |
| 3694 | #endif |
| 3695 | |
| 3696 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3697 | /* Advance to the next character, or advance to the next line, or |
| 3698 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3699 | if (clen != 0) |
| 3700 | reginput += clen; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3701 | else if (go_to_nextline) |
| 3702 | reg_nextline(); |
| 3703 | else |
| 3704 | break; |
| 3705 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3706 | |
| 3707 | #ifdef ENABLE_LOG |
| 3708 | if (log_fd != stderr) |
| 3709 | fclose(log_fd); |
| 3710 | log_fd = NULL; |
| 3711 | #endif |
| 3712 | |
| 3713 | theend: |
| 3714 | /* Free memory */ |
| 3715 | vim_free(list[0].t); |
| 3716 | vim_free(list[1].t); |
| 3717 | vim_free(list[2].t); |
| 3718 | list[0].t = list[1].t = list[2].t = NULL; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3719 | vim_free(listids); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3720 | #undef ADD_POS_NEG_STATE |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 3721 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3722 | fclose(debug); |
| 3723 | #endif |
| 3724 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3725 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3726 | } |
| 3727 | |
| 3728 | /* |
| 3729 | * Try match of "prog" with at regline["col"]. |
| 3730 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 3731 | */ |
| 3732 | static long |
| 3733 | nfa_regtry(start, col) |
| 3734 | nfa_state_T *start; |
| 3735 | colnr_T col; |
| 3736 | { |
| 3737 | int i; |
| 3738 | regsub_T sub, m; |
| 3739 | #ifdef ENABLE_LOG |
| 3740 | FILE *f; |
| 3741 | #endif |
| 3742 | |
| 3743 | reginput = regline + col; |
| 3744 | need_clear_subexpr = TRUE; |
| 3745 | |
| 3746 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3747 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3748 | if (f != NULL) |
| 3749 | { |
| 3750 | fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n"); |
| 3751 | fprintf(f, " =======================================================\n"); |
| 3752 | #ifdef DEBUG |
| 3753 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 3754 | #endif |
| 3755 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
| 3756 | fprintf(f, " =======================================================\n\n\n\n\n\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 3757 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3758 | fprintf(f, "\n\n"); |
| 3759 | fclose(f); |
| 3760 | } |
| 3761 | else |
| 3762 | EMSG(_("Could not open temporary log file for writing ")); |
| 3763 | #endif |
| 3764 | |
| 3765 | if (REG_MULTI) |
| 3766 | { |
| 3767 | /* Use 0xff to set lnum to -1 */ |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3768 | vim_memset(sub.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr); |
| 3769 | vim_memset(m.multilist, 0xff, sizeof(struct multipos) * nfa_nsubexpr); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3770 | } |
| 3771 | else |
| 3772 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3773 | vim_memset(sub.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3774 | vim_memset(m.linelist, 0, sizeof(struct linepos) * nfa_nsubexpr); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3775 | } |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3776 | sub.in_use = 0; |
| 3777 | m.in_use = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3778 | |
| 3779 | if (nfa_regmatch(start, &sub, &m) == FALSE) |
| 3780 | return 0; |
| 3781 | |
| 3782 | cleanup_subexpr(); |
| 3783 | if (REG_MULTI) |
| 3784 | { |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3785 | for (i = 0; i < sub.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3786 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3787 | reg_startpos[i] = sub.multilist[i].start; |
| 3788 | reg_endpos[i] = sub.multilist[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | if (reg_startpos[0].lnum < 0) |
| 3792 | { |
| 3793 | reg_startpos[0].lnum = 0; |
| 3794 | reg_startpos[0].col = col; |
| 3795 | } |
| 3796 | if (reg_endpos[0].lnum < 0) |
| 3797 | { |
| 3798 | reg_endpos[0].lnum = reglnum; |
| 3799 | reg_endpos[0].col = (int)(reginput - regline); |
| 3800 | } |
| 3801 | else |
| 3802 | /* Use line number of "\ze". */ |
| 3803 | reglnum = reg_endpos[0].lnum; |
| 3804 | } |
| 3805 | else |
| 3806 | { |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3807 | for (i = 0; i < sub.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3808 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3809 | reg_startp[i] = sub.linelist[i].start; |
| 3810 | reg_endp[i] = sub.linelist[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3811 | } |
| 3812 | |
| 3813 | if (reg_startp[0] == NULL) |
| 3814 | reg_startp[0] = regline + col; |
| 3815 | if (reg_endp[0] == NULL) |
| 3816 | reg_endp[0] = reginput; |
| 3817 | } |
| 3818 | |
| 3819 | return 1 + reglnum; |
| 3820 | } |
| 3821 | |
| 3822 | /* |
| 3823 | * Match a regexp against a string ("line" points to the string) or multiple |
| 3824 | * lines ("line" is NULL, use reg_getline()). |
| 3825 | * |
| 3826 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 3827 | */ |
| 3828 | static long |
| 3829 | nfa_regexec_both(line, col) |
| 3830 | char_u *line; |
| 3831 | colnr_T col; /* column to start looking for match */ |
| 3832 | { |
| 3833 | nfa_regprog_T *prog; |
| 3834 | long retval = 0L; |
| 3835 | int i; |
| 3836 | |
| 3837 | if (REG_MULTI) |
| 3838 | { |
| 3839 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 3840 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 3841 | reg_startpos = reg_mmatch->startpos; |
| 3842 | reg_endpos = reg_mmatch->endpos; |
| 3843 | } |
| 3844 | else |
| 3845 | { |
| 3846 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 3847 | reg_startp = reg_match->startp; |
| 3848 | reg_endp = reg_match->endp; |
| 3849 | } |
| 3850 | |
| 3851 | /* Be paranoid... */ |
| 3852 | if (prog == NULL || line == NULL) |
| 3853 | { |
| 3854 | EMSG(_(e_null)); |
| 3855 | goto theend; |
| 3856 | } |
| 3857 | |
| 3858 | /* If the start column is past the maximum column: no need to try. */ |
| 3859 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 3860 | goto theend; |
| 3861 | |
| 3862 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 3863 | if (prog->regflags & RF_ICASE) |
| 3864 | ireg_ic = TRUE; |
| 3865 | else if (prog->regflags & RF_NOICASE) |
| 3866 | ireg_ic = FALSE; |
| 3867 | |
| 3868 | #ifdef FEAT_MBYTE |
| 3869 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 3870 | if (prog->regflags & RF_ICOMBINE) |
| 3871 | ireg_icombine = TRUE; |
| 3872 | #endif |
| 3873 | |
| 3874 | regline = line; |
| 3875 | reglnum = 0; /* relative to line */ |
| 3876 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 3877 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3878 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3879 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 3880 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3881 | for (i = 0; i < nstate; ++i) |
| 3882 | { |
| 3883 | prog->state[i].id = i; |
| 3884 | prog->state[i].lastlist = 0; |
| 3885 | prog->state[i].visits = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3886 | } |
| 3887 | |
| 3888 | retval = nfa_regtry(prog->start, col); |
| 3889 | |
| 3890 | theend: |
| 3891 | return retval; |
| 3892 | } |
| 3893 | |
| 3894 | /* |
| 3895 | * Compile a regular expression into internal code for the NFA matcher. |
| 3896 | * Returns the program in allocated space. Returns NULL for an error. |
| 3897 | */ |
| 3898 | static regprog_T * |
| 3899 | nfa_regcomp(expr, re_flags) |
| 3900 | char_u *expr; |
| 3901 | int re_flags; |
| 3902 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 3903 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3904 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3905 | int *postfix; |
| 3906 | |
| 3907 | if (expr == NULL) |
| 3908 | return NULL; |
| 3909 | |
| 3910 | #ifdef DEBUG |
| 3911 | nfa_regengine.expr = expr; |
| 3912 | #endif |
| 3913 | |
| 3914 | init_class_tab(); |
| 3915 | |
| 3916 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 3917 | return NULL; |
| 3918 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3919 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 3920 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3921 | postfix = re2post(); |
| 3922 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3923 | { |
| 3924 | /* TODO: only give this error for debugging? */ |
| 3925 | if (post_ptr >= post_end) |
| 3926 | 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] | 3927 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3928 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3929 | |
| 3930 | /* |
| 3931 | * In order to build the NFA, we parse the input regexp twice: |
| 3932 | * 1. first pass to count size (so we can allocate space) |
| 3933 | * 2. second to emit code |
| 3934 | */ |
| 3935 | #ifdef ENABLE_LOG |
| 3936 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3937 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3938 | |
| 3939 | if (f != NULL) |
| 3940 | { |
| 3941 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 3942 | fclose(f); |
| 3943 | } |
| 3944 | } |
| 3945 | #endif |
| 3946 | |
| 3947 | /* |
| 3948 | * PASS 1 |
| 3949 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 3950 | */ |
| 3951 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 3952 | |
| 3953 | /* Space for compiled regexp */ |
| 3954 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate; |
| 3955 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 3956 | if (prog == NULL) |
| 3957 | goto fail; |
| 3958 | vim_memset(prog, 0, prog_size); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3959 | state_ptr = prog->state; |
| 3960 | |
| 3961 | /* |
| 3962 | * PASS 2 |
| 3963 | * Build the NFA |
| 3964 | */ |
| 3965 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 3966 | if (prog->start == NULL) |
| 3967 | goto fail; |
| 3968 | |
| 3969 | prog->regflags = regflags; |
| 3970 | prog->engine = &nfa_regengine; |
| 3971 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 3972 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3973 | prog->nsubexp = regnpar; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3974 | #ifdef ENABLE_LOG |
| 3975 | nfa_postfix_dump(expr, OK); |
| 3976 | nfa_dump(prog); |
| 3977 | #endif |
| 3978 | |
| 3979 | out: |
| 3980 | vim_free(post_start); |
| 3981 | post_start = post_ptr = post_end = NULL; |
| 3982 | state_ptr = NULL; |
| 3983 | return (regprog_T *)prog; |
| 3984 | |
| 3985 | fail: |
| 3986 | vim_free(prog); |
| 3987 | prog = NULL; |
| 3988 | #ifdef ENABLE_LOG |
| 3989 | nfa_postfix_dump(expr, FAIL); |
| 3990 | #endif |
| 3991 | #ifdef DEBUG |
| 3992 | nfa_regengine.expr = NULL; |
| 3993 | #endif |
| 3994 | goto out; |
| 3995 | } |
| 3996 | |
| 3997 | |
| 3998 | /* |
| 3999 | * Match a regexp against a string. |
| 4000 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 4001 | * Uses curbuf for line count and 'iskeyword'. |
| 4002 | * |
| 4003 | * Return TRUE if there is a match, FALSE if not. |
| 4004 | */ |
| 4005 | static int |
| 4006 | nfa_regexec(rmp, line, col) |
| 4007 | regmatch_T *rmp; |
| 4008 | char_u *line; /* string to match against */ |
| 4009 | colnr_T col; /* column to start looking for match */ |
| 4010 | { |
| 4011 | reg_match = rmp; |
| 4012 | reg_mmatch = NULL; |
| 4013 | reg_maxline = 0; |
| 4014 | reg_line_lbr = FALSE; |
| 4015 | reg_buf = curbuf; |
| 4016 | reg_win = NULL; |
| 4017 | ireg_ic = rmp->rm_ic; |
| 4018 | #ifdef FEAT_MBYTE |
| 4019 | ireg_icombine = FALSE; |
| 4020 | #endif |
| 4021 | ireg_maxcol = 0; |
| 4022 | return (nfa_regexec_both(line, col) != 0); |
| 4023 | } |
| 4024 | |
| 4025 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 4026 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 4027 | |
| 4028 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 4029 | |
| 4030 | /* |
| 4031 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 4032 | */ |
| 4033 | static int |
| 4034 | nfa_regexec_nl(rmp, line, col) |
| 4035 | regmatch_T *rmp; |
| 4036 | char_u *line; /* string to match against */ |
| 4037 | colnr_T col; /* column to start looking for match */ |
| 4038 | { |
| 4039 | reg_match = rmp; |
| 4040 | reg_mmatch = NULL; |
| 4041 | reg_maxline = 0; |
| 4042 | reg_line_lbr = TRUE; |
| 4043 | reg_buf = curbuf; |
| 4044 | reg_win = NULL; |
| 4045 | ireg_ic = rmp->rm_ic; |
| 4046 | #ifdef FEAT_MBYTE |
| 4047 | ireg_icombine = FALSE; |
| 4048 | #endif |
| 4049 | ireg_maxcol = 0; |
| 4050 | return (nfa_regexec_both(line, col) != 0); |
| 4051 | } |
| 4052 | #endif |
| 4053 | |
| 4054 | |
| 4055 | /* |
| 4056 | * Match a regexp against multiple lines. |
| 4057 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 4058 | * Uses curbuf for line count and 'iskeyword'. |
| 4059 | * |
| 4060 | * Return zero if there is no match. Return number of lines contained in the |
| 4061 | * match otherwise. |
| 4062 | * |
| 4063 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 4064 | * |
| 4065 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 4066 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 4067 | * |
| 4068 | * +-------------------------+ |
| 4069 | * |a | |
| 4070 | * |b | |
| 4071 | * |c | |
| 4072 | * | | |
| 4073 | * +-------------------------+ |
| 4074 | * |
| 4075 | * then nfa_regexec_multi() returns 3. while the original |
| 4076 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 4077 | * |
| 4078 | * FIXME if this behavior is not compatible. |
| 4079 | */ |
| 4080 | static long |
| 4081 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 4082 | regmmatch_T *rmp; |
| 4083 | win_T *win; /* window in which to search or NULL */ |
| 4084 | buf_T *buf; /* buffer in which to search */ |
| 4085 | linenr_T lnum; /* nr of line to start looking for match */ |
| 4086 | colnr_T col; /* column to start looking for match */ |
| 4087 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 4088 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4089 | reg_match = NULL; |
| 4090 | reg_mmatch = rmp; |
| 4091 | reg_buf = buf; |
| 4092 | reg_win = win; |
| 4093 | reg_firstlnum = lnum; |
| 4094 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 4095 | reg_line_lbr = FALSE; |
| 4096 | ireg_ic = rmp->rmm_ic; |
| 4097 | #ifdef FEAT_MBYTE |
| 4098 | ireg_icombine = FALSE; |
| 4099 | #endif |
| 4100 | ireg_maxcol = rmp->rmm_maxcol; |
| 4101 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4102 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4103 | } |
| 4104 | |
| 4105 | #ifdef DEBUG |
| 4106 | # undef ENABLE_LOG |
| 4107 | #endif |