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