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