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