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