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