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