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