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