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 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 37 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 38 | NFA_START_COLL, /* [abc] start */ |
| 39 | NFA_END_COLL, /* [abc] end */ |
| 40 | NFA_START_NEG_COLL, /* [^abc] start */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 41 | NFA_END_NEG_COLL, /* [^abc] end (postfix only) */ |
| 42 | NFA_RANGE, /* range of the two previous items |
| 43 | * (postfix only) */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 44 | NFA_RANGE_MIN, /* low end of a range */ |
| 45 | NFA_RANGE_MAX, /* high end of a range */ |
| 46 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 47 | NFA_CONCAT, /* concatenate two previous items (postfix |
| 48 | * only) */ |
| 49 | NFA_OR, /* \| (postfix only) */ |
| 50 | NFA_STAR, /* greedy * (posfix only) */ |
| 51 | NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */ |
| 52 | NFA_QUEST, /* greedy \? (postfix only) */ |
| 53 | NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 54 | |
| 55 | NFA_BOL, /* ^ Begin line */ |
| 56 | NFA_EOL, /* $ End line */ |
| 57 | NFA_BOW, /* \< Begin word */ |
| 58 | NFA_EOW, /* \> End word */ |
| 59 | NFA_BOF, /* \%^ Begin file */ |
| 60 | NFA_EOF, /* \%$ End file */ |
| 61 | NFA_NEWL, |
| 62 | NFA_ZSTART, /* Used for \zs */ |
| 63 | NFA_ZEND, /* Used for \ze */ |
| 64 | NFA_NOPEN, /* Start of subexpression marked with \%( */ |
| 65 | NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ |
| 66 | NFA_START_INVISIBLE, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 67 | NFA_START_INVISIBLE_NEG, |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 68 | NFA_START_INVISIBLE_BEFORE, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 69 | NFA_START_INVISIBLE_BEFORE_NEG, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 70 | NFA_START_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 71 | NFA_END_INVISIBLE, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 72 | NFA_END_INVISIBLE_NEG, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 73 | NFA_END_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 74 | NFA_COMPOSING, /* Next nodes in NFA are part of the |
| 75 | composing multibyte char */ |
| 76 | NFA_END_COMPOSING, /* End of a composing char in the NFA */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 77 | NFA_OPT_CHARS, /* \%[abc] */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 78 | |
| 79 | /* The following are used only in the postfix form, not in the NFA */ |
| 80 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 81 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 82 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 83 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 84 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 85 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 86 | NFA_BACKREF1, /* \1 */ |
| 87 | NFA_BACKREF2, /* \2 */ |
| 88 | NFA_BACKREF3, /* \3 */ |
| 89 | NFA_BACKREF4, /* \4 */ |
| 90 | NFA_BACKREF5, /* \5 */ |
| 91 | NFA_BACKREF6, /* \6 */ |
| 92 | NFA_BACKREF7, /* \7 */ |
| 93 | NFA_BACKREF8, /* \8 */ |
| 94 | NFA_BACKREF9, /* \9 */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 95 | #ifdef FEAT_SYN_HL |
| 96 | NFA_ZREF1, /* \z1 */ |
| 97 | NFA_ZREF2, /* \z2 */ |
| 98 | NFA_ZREF3, /* \z3 */ |
| 99 | NFA_ZREF4, /* \z4 */ |
| 100 | NFA_ZREF5, /* \z5 */ |
| 101 | NFA_ZREF6, /* \z6 */ |
| 102 | NFA_ZREF7, /* \z7 */ |
| 103 | NFA_ZREF8, /* \z8 */ |
| 104 | NFA_ZREF9, /* \z9 */ |
| 105 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 106 | NFA_SKIP, /* Skip characters */ |
| 107 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 108 | NFA_MOPEN, |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 109 | NFA_MOPEN1, |
| 110 | NFA_MOPEN2, |
| 111 | NFA_MOPEN3, |
| 112 | NFA_MOPEN4, |
| 113 | NFA_MOPEN5, |
| 114 | NFA_MOPEN6, |
| 115 | NFA_MOPEN7, |
| 116 | NFA_MOPEN8, |
| 117 | NFA_MOPEN9, |
| 118 | |
| 119 | NFA_MCLOSE, |
| 120 | NFA_MCLOSE1, |
| 121 | NFA_MCLOSE2, |
| 122 | NFA_MCLOSE3, |
| 123 | NFA_MCLOSE4, |
| 124 | NFA_MCLOSE5, |
| 125 | NFA_MCLOSE6, |
| 126 | NFA_MCLOSE7, |
| 127 | NFA_MCLOSE8, |
| 128 | NFA_MCLOSE9, |
| 129 | |
| 130 | #ifdef FEAT_SYN_HL |
| 131 | NFA_ZOPEN, |
| 132 | NFA_ZOPEN1, |
| 133 | NFA_ZOPEN2, |
| 134 | NFA_ZOPEN3, |
| 135 | NFA_ZOPEN4, |
| 136 | NFA_ZOPEN5, |
| 137 | NFA_ZOPEN6, |
| 138 | NFA_ZOPEN7, |
| 139 | NFA_ZOPEN8, |
| 140 | NFA_ZOPEN9, |
| 141 | |
| 142 | NFA_ZCLOSE, |
| 143 | NFA_ZCLOSE1, |
| 144 | NFA_ZCLOSE2, |
| 145 | NFA_ZCLOSE3, |
| 146 | NFA_ZCLOSE4, |
| 147 | NFA_ZCLOSE5, |
| 148 | NFA_ZCLOSE6, |
| 149 | NFA_ZCLOSE7, |
| 150 | NFA_ZCLOSE8, |
| 151 | NFA_ZCLOSE9, |
| 152 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 153 | |
| 154 | /* NFA_FIRST_NL */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 155 | NFA_ANY, /* Match any one character. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 156 | NFA_IDENT, /* Match identifier char */ |
| 157 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 158 | NFA_KWORD, /* Match keyword char */ |
| 159 | NFA_SKWORD, /* Match word char but no digit */ |
| 160 | NFA_FNAME, /* Match file name char */ |
| 161 | NFA_SFNAME, /* Match file name char but no digit */ |
| 162 | NFA_PRINT, /* Match printable char */ |
| 163 | NFA_SPRINT, /* Match printable char but no digit */ |
| 164 | NFA_WHITE, /* Match whitespace char */ |
| 165 | NFA_NWHITE, /* Match non-whitespace char */ |
| 166 | NFA_DIGIT, /* Match digit char */ |
| 167 | NFA_NDIGIT, /* Match non-digit char */ |
| 168 | NFA_HEX, /* Match hex char */ |
| 169 | NFA_NHEX, /* Match non-hex char */ |
| 170 | NFA_OCTAL, /* Match octal char */ |
| 171 | NFA_NOCTAL, /* Match non-octal char */ |
| 172 | NFA_WORD, /* Match word char */ |
| 173 | NFA_NWORD, /* Match non-word char */ |
| 174 | NFA_HEAD, /* Match head char */ |
| 175 | NFA_NHEAD, /* Match non-head char */ |
| 176 | NFA_ALPHA, /* Match alpha char */ |
| 177 | NFA_NALPHA, /* Match non-alpha char */ |
| 178 | NFA_LOWER, /* Match lowercase char */ |
| 179 | NFA_NLOWER, /* Match non-lowercase char */ |
| 180 | NFA_UPPER, /* Match uppercase char */ |
| 181 | NFA_NUPPER, /* Match non-uppercase char */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 182 | |
| 183 | NFA_CURSOR, /* Match cursor pos */ |
| 184 | NFA_LNUM, /* Match line number */ |
| 185 | NFA_LNUM_GT, /* Match > line number */ |
| 186 | NFA_LNUM_LT, /* Match < line number */ |
| 187 | NFA_COL, /* Match cursor column */ |
| 188 | NFA_COL_GT, /* Match > cursor column */ |
| 189 | NFA_COL_LT, /* Match < cursor column */ |
| 190 | NFA_VCOL, /* Match cursor virtual column */ |
| 191 | NFA_VCOL_GT, /* Match > cursor virtual column */ |
| 192 | NFA_VCOL_LT, /* Match < cursor virtual column */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 193 | NFA_MARK, /* Match mark */ |
| 194 | NFA_MARK_GT, /* Match > mark */ |
| 195 | NFA_MARK_LT, /* Match < mark */ |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 196 | NFA_VISUAL, /* Match Visual area */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 197 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 198 | NFA_FIRST_NL = NFA_ANY + ADD_NL, |
| 199 | NFA_LAST_NL = NFA_NUPPER + ADD_NL, |
| 200 | |
| 201 | /* Character classes [:alnum:] etc */ |
| 202 | NFA_CLASS_ALNUM, |
| 203 | NFA_CLASS_ALPHA, |
| 204 | NFA_CLASS_BLANK, |
| 205 | NFA_CLASS_CNTRL, |
| 206 | NFA_CLASS_DIGIT, |
| 207 | NFA_CLASS_GRAPH, |
| 208 | NFA_CLASS_LOWER, |
| 209 | NFA_CLASS_PRINT, |
| 210 | NFA_CLASS_PUNCT, |
| 211 | NFA_CLASS_SPACE, |
| 212 | NFA_CLASS_UPPER, |
| 213 | NFA_CLASS_XDIGIT, |
| 214 | NFA_CLASS_TAB, |
| 215 | NFA_CLASS_RETURN, |
| 216 | NFA_CLASS_BACKSPACE, |
| 217 | NFA_CLASS_ESCAPE |
| 218 | }; |
| 219 | |
| 220 | /* Keep in sync with classchars. */ |
| 221 | static int nfa_classcodes[] = { |
| 222 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 223 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 224 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 225 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 226 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 227 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 228 | NFA_UPPER, NFA_NUPPER |
| 229 | }; |
| 230 | |
| 231 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
| 232 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 233 | /* NFA regexp \ze operator encountered. */ |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 234 | static int nfa_has_zend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 235 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 236 | /* NFA regexp \1 .. \9 encountered. */ |
| 237 | static int nfa_has_backref; |
| 238 | |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 239 | #ifdef FEAT_SYN_HL |
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; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 242 | #endif |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 243 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 244 | /* Number of sub expressions actually being used during execution. 1 if only |
| 245 | * the whole match (subexpr 0) is used. */ |
| 246 | static int nfa_nsubexpr; |
| 247 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 248 | static int *post_start; /* holds the postfix form of r.e. */ |
| 249 | static int *post_end; |
| 250 | static int *post_ptr; |
| 251 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 252 | static int nstate; /* Number of states in the NFA. Also used when |
| 253 | * executing. */ |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 254 | static int istate; /* Index in the state vector, used in alloc_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 255 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 256 | /* If not NULL match must end at this position */ |
| 257 | static save_se_T *nfa_endp = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 258 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 259 | /* listid is global, so that it increases on recursive calls to |
| 260 | * nfa_regmatch(), which means we don't have to clear the lastlist field of |
| 261 | * all the states. */ |
| 262 | static int nfa_listid; |
| 263 | static int nfa_alt_listid; |
| 264 | |
| 265 | /* 0 for first call to nfa_regmatch(), 1 for recursive call. */ |
| 266 | static int nfa_ll_index = 0; |
| 267 | |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 268 | static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 269 | static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth)); |
| 270 | static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 271 | static char_u *nfa_get_match_text __ARGS((nfa_state_T *start)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 272 | static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 273 | static int nfa_emit_equi_class __ARGS((int c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 274 | static int nfa_regatom __ARGS((void)); |
| 275 | static int nfa_regpiece __ARGS((void)); |
| 276 | static int nfa_regconcat __ARGS((void)); |
| 277 | static int nfa_regbranch __ARGS((void)); |
| 278 | static int nfa_reg __ARGS((int paren)); |
| 279 | #ifdef DEBUG |
| 280 | static void nfa_set_code __ARGS((int c)); |
| 281 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 282 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 283 | 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] | 284 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 285 | #endif |
| 286 | static int *re2post __ARGS((void)); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 287 | 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] | 288 | static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size)); |
| 289 | static int check_char_class __ARGS((int class, int c)); |
| 290 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 291 | static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list)); |
| 292 | static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 293 | 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] | 294 | static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 295 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 296 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 297 | static void nfa_regfree __ARGS((regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 298 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 299 | static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
| 300 | |
| 301 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 302 | #define EMIT(c) do { \ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 303 | if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 304 | return FAIL; \ |
| 305 | *post_ptr++ = c; \ |
| 306 | } while (0) |
| 307 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 308 | /* |
| 309 | * Initialize internal variables before NFA compilation. |
| 310 | * Return OK on success, FAIL otherwise. |
| 311 | */ |
| 312 | static int |
| 313 | nfa_regcomp_start(expr, re_flags) |
| 314 | char_u *expr; |
| 315 | int re_flags; /* see vim_regcomp() */ |
| 316 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 317 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 318 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 319 | |
| 320 | nstate = 0; |
| 321 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 322 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 323 | nstate_max = (int)(STRLEN(expr) + 1) * 25; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 324 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 325 | /* 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] | 326 | * When it is still not enough realloc_post_list() will be used. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 327 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 328 | |
| 329 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 330 | postfix_size = sizeof(int) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 331 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 332 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 333 | if (post_start == NULL) |
| 334 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 335 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 336 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 337 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 338 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 339 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 340 | /* shared with BT engine */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 341 | regcomp_start(expr, re_flags); |
| 342 | |
| 343 | return OK; |
| 344 | } |
| 345 | |
| 346 | /* |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 347 | * Figure out if the NFA state list starts with an anchor, must match at start |
| 348 | * of the line. |
| 349 | */ |
| 350 | static int |
| 351 | nfa_get_reganch(start, depth) |
| 352 | nfa_state_T *start; |
| 353 | int depth; |
| 354 | { |
| 355 | nfa_state_T *p = start; |
| 356 | |
| 357 | if (depth > 4) |
| 358 | return 0; |
| 359 | |
| 360 | while (p != NULL) |
| 361 | { |
| 362 | switch (p->c) |
| 363 | { |
| 364 | case NFA_BOL: |
| 365 | case NFA_BOF: |
| 366 | return 1; /* yes! */ |
| 367 | |
| 368 | case NFA_ZSTART: |
| 369 | case NFA_ZEND: |
| 370 | case NFA_CURSOR: |
| 371 | case NFA_VISUAL: |
| 372 | |
| 373 | case NFA_MOPEN: |
| 374 | case NFA_MOPEN1: |
| 375 | case NFA_MOPEN2: |
| 376 | case NFA_MOPEN3: |
| 377 | case NFA_MOPEN4: |
| 378 | case NFA_MOPEN5: |
| 379 | case NFA_MOPEN6: |
| 380 | case NFA_MOPEN7: |
| 381 | case NFA_MOPEN8: |
| 382 | case NFA_MOPEN9: |
| 383 | case NFA_NOPEN: |
| 384 | #ifdef FEAT_SYN_HL |
| 385 | case NFA_ZOPEN: |
| 386 | case NFA_ZOPEN1: |
| 387 | case NFA_ZOPEN2: |
| 388 | case NFA_ZOPEN3: |
| 389 | case NFA_ZOPEN4: |
| 390 | case NFA_ZOPEN5: |
| 391 | case NFA_ZOPEN6: |
| 392 | case NFA_ZOPEN7: |
| 393 | case NFA_ZOPEN8: |
| 394 | case NFA_ZOPEN9: |
| 395 | #endif |
| 396 | p = p->out; |
| 397 | break; |
| 398 | |
| 399 | case NFA_SPLIT: |
| 400 | return nfa_get_reganch(p->out, depth + 1) |
| 401 | && nfa_get_reganch(p->out1, depth + 1); |
| 402 | |
| 403 | default: |
| 404 | return 0; /* noooo */ |
| 405 | } |
| 406 | } |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | /* |
| 411 | * Figure out if the NFA state list starts with a character which must match |
| 412 | * at start of the match. |
| 413 | */ |
| 414 | static int |
| 415 | nfa_get_regstart(start, depth) |
| 416 | nfa_state_T *start; |
| 417 | int depth; |
| 418 | { |
| 419 | nfa_state_T *p = start; |
| 420 | |
| 421 | if (depth > 4) |
| 422 | return 0; |
| 423 | |
| 424 | while (p != NULL) |
| 425 | { |
| 426 | switch (p->c) |
| 427 | { |
| 428 | /* all kinds of zero-width matches */ |
| 429 | case NFA_BOL: |
| 430 | case NFA_BOF: |
| 431 | case NFA_BOW: |
| 432 | case NFA_EOW: |
| 433 | case NFA_ZSTART: |
| 434 | case NFA_ZEND: |
| 435 | case NFA_CURSOR: |
| 436 | case NFA_VISUAL: |
| 437 | case NFA_LNUM: |
| 438 | case NFA_LNUM_GT: |
| 439 | case NFA_LNUM_LT: |
| 440 | case NFA_COL: |
| 441 | case NFA_COL_GT: |
| 442 | case NFA_COL_LT: |
| 443 | case NFA_VCOL: |
| 444 | case NFA_VCOL_GT: |
| 445 | case NFA_VCOL_LT: |
| 446 | case NFA_MARK: |
| 447 | case NFA_MARK_GT: |
| 448 | case NFA_MARK_LT: |
| 449 | |
| 450 | case NFA_MOPEN: |
| 451 | case NFA_MOPEN1: |
| 452 | case NFA_MOPEN2: |
| 453 | case NFA_MOPEN3: |
| 454 | case NFA_MOPEN4: |
| 455 | case NFA_MOPEN5: |
| 456 | case NFA_MOPEN6: |
| 457 | case NFA_MOPEN7: |
| 458 | case NFA_MOPEN8: |
| 459 | case NFA_MOPEN9: |
| 460 | case NFA_NOPEN: |
| 461 | #ifdef FEAT_SYN_HL |
| 462 | case NFA_ZOPEN: |
| 463 | case NFA_ZOPEN1: |
| 464 | case NFA_ZOPEN2: |
| 465 | case NFA_ZOPEN3: |
| 466 | case NFA_ZOPEN4: |
| 467 | case NFA_ZOPEN5: |
| 468 | case NFA_ZOPEN6: |
| 469 | case NFA_ZOPEN7: |
| 470 | case NFA_ZOPEN8: |
| 471 | case NFA_ZOPEN9: |
| 472 | #endif |
| 473 | p = p->out; |
| 474 | break; |
| 475 | |
| 476 | case NFA_SPLIT: |
| 477 | { |
| 478 | int c1 = nfa_get_regstart(p->out, depth + 1); |
| 479 | int c2 = nfa_get_regstart(p->out1, depth + 1); |
| 480 | |
| 481 | if (c1 == c2) |
| 482 | return c1; /* yes! */ |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | default: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 487 | if (p->c > 0) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 488 | return p->c; /* yes! */ |
| 489 | return 0; |
| 490 | } |
| 491 | } |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 496 | * Figure out if the NFA state list contains just literal text and nothing |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 497 | * else. If so return a string in allocated memory with what must match after |
| 498 | * regstart. Otherwise return NULL. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 499 | */ |
| 500 | static char_u * |
| 501 | nfa_get_match_text(start) |
| 502 | nfa_state_T *start; |
| 503 | { |
| 504 | nfa_state_T *p = start; |
| 505 | int len = 0; |
| 506 | char_u *ret; |
| 507 | char_u *s; |
| 508 | |
| 509 | if (p->c != NFA_MOPEN) |
| 510 | return NULL; /* just in case */ |
| 511 | p = p->out; |
| 512 | while (p->c > 0) |
| 513 | { |
| 514 | len += MB_CHAR2LEN(p->c); |
| 515 | p = p->out; |
| 516 | } |
| 517 | if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH) |
| 518 | return NULL; |
| 519 | |
| 520 | ret = alloc(len); |
| 521 | if (ret != NULL) |
| 522 | { |
| 523 | len = 0; |
| 524 | p = start->out->out; /* skip first char, it goes into regstart */ |
| 525 | s = ret; |
| 526 | while (p->c > 0) |
| 527 | { |
| 528 | #ifdef FEAT_MBYTE |
| 529 | if (has_mbyte) |
| 530 | s += (*mb_char2bytes)(p->c, s); |
| 531 | else |
| 532 | #endif |
| 533 | *s++ = p->c; |
| 534 | p = p->out; |
| 535 | } |
| 536 | *s = NUL; |
| 537 | } |
| 538 | return ret; |
| 539 | } |
| 540 | |
| 541 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 542 | * Allocate more space for post_start. Called when |
| 543 | * running above the estimated number of states. |
| 544 | */ |
| 545 | static int |
| 546 | realloc_post_list() |
| 547 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 548 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 549 | int new_max = nstate_max + 1000; |
| 550 | int *new_start; |
| 551 | int *old_start; |
| 552 | |
| 553 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 554 | if (new_start == NULL) |
| 555 | return FAIL; |
| 556 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 557 | old_start = post_start; |
| 558 | post_start = new_start; |
| 559 | post_ptr = new_start + (post_ptr - old_start); |
| 560 | post_end = post_start + new_max; |
| 561 | vim_free(old_start); |
| 562 | return OK; |
| 563 | } |
| 564 | |
| 565 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 566 | * Search between "start" and "end" and try to recognize a |
| 567 | * character class in expanded form. For example [0-9]. |
| 568 | * On success, return the id the character class to be emitted. |
| 569 | * On failure, return 0 (=FAIL) |
| 570 | * Start points to the first char of the range, while end should point |
| 571 | * to the closing brace. |
| 572 | */ |
| 573 | static int |
| 574 | nfa_recognize_char_class(start, end, extra_newl) |
| 575 | char_u *start; |
| 576 | char_u *end; |
| 577 | int extra_newl; |
| 578 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 579 | # define CLASS_not 0x80 |
| 580 | # define CLASS_af 0x40 |
| 581 | # define CLASS_AF 0x20 |
| 582 | # define CLASS_az 0x10 |
| 583 | # define CLASS_AZ 0x08 |
| 584 | # define CLASS_o7 0x04 |
| 585 | # define CLASS_o9 0x02 |
| 586 | # define CLASS_underscore 0x01 |
| 587 | |
| 588 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 589 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 590 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 591 | |
| 592 | if (extra_newl == TRUE) |
| 593 | newl = TRUE; |
| 594 | |
| 595 | if (*end != ']') |
| 596 | return FAIL; |
| 597 | p = start; |
| 598 | if (*p == '^') |
| 599 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 600 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 601 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | while (p < end) |
| 605 | { |
| 606 | if (p + 2 < end && *(p + 1) == '-') |
| 607 | { |
| 608 | switch (*p) |
| 609 | { |
| 610 | case '0': |
| 611 | if (*(p + 2) == '9') |
| 612 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 613 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 614 | break; |
| 615 | } |
| 616 | else |
| 617 | if (*(p + 2) == '7') |
| 618 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 619 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | case 'a': |
| 623 | if (*(p + 2) == 'z') |
| 624 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 625 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 626 | break; |
| 627 | } |
| 628 | else |
| 629 | if (*(p + 2) == 'f') |
| 630 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 631 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | case 'A': |
| 635 | if (*(p + 2) == 'Z') |
| 636 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 637 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 638 | break; |
| 639 | } |
| 640 | else |
| 641 | if (*(p + 2) == 'F') |
| 642 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 643 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | /* FALLTHROUGH */ |
| 647 | default: |
| 648 | return FAIL; |
| 649 | } |
| 650 | p += 3; |
| 651 | } |
| 652 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 653 | { |
| 654 | newl = TRUE; |
| 655 | p += 2; |
| 656 | } |
| 657 | else if (*p == '_') |
| 658 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 659 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 660 | p ++; |
| 661 | } |
| 662 | else if (*p == '\n') |
| 663 | { |
| 664 | newl = TRUE; |
| 665 | p ++; |
| 666 | } |
| 667 | else |
| 668 | return FAIL; |
| 669 | } /* while (p < end) */ |
| 670 | |
| 671 | if (p != end) |
| 672 | return FAIL; |
| 673 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 674 | if (newl == TRUE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 675 | extra_newl = ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 676 | |
| 677 | switch (config) |
| 678 | { |
| 679 | case CLASS_o9: |
| 680 | return extra_newl + NFA_DIGIT; |
| 681 | case CLASS_not | CLASS_o9: |
| 682 | return extra_newl + NFA_NDIGIT; |
| 683 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 684 | return extra_newl + NFA_HEX; |
| 685 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 686 | return extra_newl + NFA_NHEX; |
| 687 | case CLASS_o7: |
| 688 | return extra_newl + NFA_OCTAL; |
| 689 | case CLASS_not | CLASS_o7: |
| 690 | return extra_newl + NFA_NOCTAL; |
| 691 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 692 | return extra_newl + NFA_WORD; |
| 693 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 694 | return extra_newl + NFA_NWORD; |
| 695 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 696 | return extra_newl + NFA_HEAD; |
| 697 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 698 | return extra_newl + NFA_NHEAD; |
| 699 | case CLASS_az | CLASS_AZ: |
| 700 | return extra_newl + NFA_ALPHA; |
| 701 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 702 | return extra_newl + NFA_NALPHA; |
| 703 | case CLASS_az: |
| 704 | return extra_newl + NFA_LOWER; |
| 705 | case CLASS_not | CLASS_az: |
| 706 | return extra_newl + NFA_NLOWER; |
| 707 | case CLASS_AZ: |
| 708 | return extra_newl + NFA_UPPER; |
| 709 | case CLASS_not | CLASS_AZ: |
| 710 | return extra_newl + NFA_NUPPER; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 711 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 712 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* |
| 716 | * Produce the bytes for equivalence class "c". |
| 717 | * Currently only handles latin1, latin9 and utf-8. |
| 718 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 719 | * equivalent to 'a OR b OR c' |
| 720 | * |
| 721 | * NOTE! When changing this function, also update reg_equi_class() |
| 722 | */ |
| 723 | static int |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 724 | nfa_emit_equi_class(c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 725 | int c; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 726 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 727 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 728 | |
| 729 | #ifdef FEAT_MBYTE |
| 730 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 731 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 732 | #endif |
| 733 | { |
| 734 | switch (c) |
| 735 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 736 | case 'A': case 0300: case 0301: case 0302: |
| 737 | case 0303: case 0304: case 0305: |
| 738 | EMIT2('A'); EMIT2(0300); EMIT2(0301); |
| 739 | EMIT2(0302); EMIT2(0303); EMIT2(0304); |
| 740 | EMIT2(0305); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 741 | return OK; |
| 742 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 743 | case 'C': case 0307: |
| 744 | EMIT2('C'); EMIT2(0307); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 745 | return OK; |
| 746 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 747 | case 'E': case 0310: case 0311: case 0312: case 0313: |
| 748 | EMIT2('E'); EMIT2(0310); EMIT2(0311); |
| 749 | EMIT2(0312); EMIT2(0313); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 750 | return OK; |
| 751 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 752 | case 'I': case 0314: case 0315: case 0316: case 0317: |
| 753 | EMIT2('I'); EMIT2(0314); EMIT2(0315); |
| 754 | EMIT2(0316); EMIT2(0317); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 755 | return OK; |
| 756 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 757 | case 'N': case 0321: |
| 758 | EMIT2('N'); EMIT2(0321); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 759 | return OK; |
| 760 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 761 | case 'O': case 0322: case 0323: case 0324: case 0325: |
| 762 | case 0326: |
| 763 | EMIT2('O'); EMIT2(0322); EMIT2(0323); |
| 764 | EMIT2(0324); EMIT2(0325); EMIT2(0326); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 765 | return OK; |
| 766 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 767 | case 'U': case 0331: case 0332: case 0333: case 0334: |
| 768 | EMIT2('U'); EMIT2(0331); EMIT2(0332); |
| 769 | EMIT2(0333); EMIT2(0334); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 770 | return OK; |
| 771 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 772 | case 'Y': case 0335: |
| 773 | EMIT2('Y'); EMIT2(0335); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 774 | return OK; |
| 775 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 776 | case 'a': case 0340: case 0341: case 0342: |
| 777 | case 0343: case 0344: case 0345: |
| 778 | EMIT2('a'); EMIT2(0340); EMIT2(0341); |
| 779 | EMIT2(0342); EMIT2(0343); EMIT2(0344); |
| 780 | EMIT2(0345); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 781 | return OK; |
| 782 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 783 | case 'c': case 0347: |
| 784 | EMIT2('c'); EMIT2(0347); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 785 | return OK; |
| 786 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 787 | case 'e': case 0350: case 0351: case 0352: case 0353: |
| 788 | EMIT2('e'); EMIT2(0350); EMIT2(0351); |
| 789 | EMIT2(0352); EMIT2(0353); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 790 | return OK; |
| 791 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 792 | case 'i': case 0354: case 0355: case 0356: case 0357: |
| 793 | EMIT2('i'); EMIT2(0354); EMIT2(0355); |
| 794 | EMIT2(0356); EMIT2(0357); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 795 | return OK; |
| 796 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 797 | case 'n': case 0361: |
| 798 | EMIT2('n'); EMIT2(0361); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 799 | return OK; |
| 800 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 801 | case 'o': case 0362: case 0363: case 0364: case 0365: |
| 802 | case 0366: |
| 803 | EMIT2('o'); EMIT2(0362); EMIT2(0363); |
| 804 | EMIT2(0364); EMIT2(0365); EMIT2(0366); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 805 | return OK; |
| 806 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 807 | case 'u': case 0371: case 0372: case 0373: case 0374: |
| 808 | EMIT2('u'); EMIT2(0371); EMIT2(0372); |
| 809 | EMIT2(0373); EMIT2(0374); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 810 | return OK; |
| 811 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 812 | case 'y': case 0375: case 0377: |
| 813 | EMIT2('y'); EMIT2(0375); EMIT2(0377); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 814 | return OK; |
| 815 | |
| 816 | default: |
| 817 | return FAIL; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | EMIT(c); |
| 822 | return OK; |
| 823 | #undef EMIT2 |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Code to parse regular expression. |
| 828 | * |
| 829 | * We try to reuse parsing functions in regexp.c to |
| 830 | * minimize surprise and keep the syntax consistent. |
| 831 | */ |
| 832 | |
| 833 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 834 | * Parse the lowest level. |
| 835 | * |
| 836 | * An atom can be one of a long list of items. Many atoms match one character |
| 837 | * in the text. It is often an ordinary character or a character class. |
| 838 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 839 | * is only for syntax highlighting. |
| 840 | * |
| 841 | * atom ::= ordinary-atom |
| 842 | * or \( pattern \) |
| 843 | * or \%( pattern \) |
| 844 | * or \z( pattern \) |
| 845 | */ |
| 846 | static int |
| 847 | nfa_regatom() |
| 848 | { |
| 849 | int c; |
| 850 | int charclass; |
| 851 | int equiclass; |
| 852 | int collclass; |
| 853 | int got_coll_char; |
| 854 | char_u *p; |
| 855 | char_u *endp; |
| 856 | #ifdef FEAT_MBYTE |
| 857 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 858 | #endif |
| 859 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 860 | int emit_range; |
| 861 | int negated; |
| 862 | int result; |
| 863 | int startc = -1; |
| 864 | int endc = -1; |
| 865 | int oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 866 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 867 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 868 | switch (c) |
| 869 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 870 | case NUL: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 871 | EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); |
| 872 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 873 | case Magic('^'): |
| 874 | EMIT(NFA_BOL); |
| 875 | break; |
| 876 | |
| 877 | case Magic('$'): |
| 878 | EMIT(NFA_EOL); |
| 879 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 880 | had_eol = TRUE; |
| 881 | #endif |
| 882 | break; |
| 883 | |
| 884 | case Magic('<'): |
| 885 | EMIT(NFA_BOW); |
| 886 | break; |
| 887 | |
| 888 | case Magic('>'): |
| 889 | EMIT(NFA_EOW); |
| 890 | break; |
| 891 | |
| 892 | case Magic('_'): |
| 893 | c = no_Magic(getchr()); |
| 894 | if (c == '^') /* "\_^" is start-of-line */ |
| 895 | { |
| 896 | EMIT(NFA_BOL); |
| 897 | break; |
| 898 | } |
| 899 | if (c == '$') /* "\_$" is end-of-line */ |
| 900 | { |
| 901 | EMIT(NFA_EOL); |
| 902 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 903 | had_eol = TRUE; |
| 904 | #endif |
| 905 | break; |
| 906 | } |
| 907 | |
| 908 | extra = ADD_NL; |
| 909 | |
| 910 | /* "\_[" is collection plus newline */ |
| 911 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 912 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 913 | |
| 914 | /* "\_x" is character class plus newline */ |
| 915 | /*FALLTHROUGH*/ |
| 916 | |
| 917 | /* |
| 918 | * Character classes. |
| 919 | */ |
| 920 | case Magic('.'): |
| 921 | case Magic('i'): |
| 922 | case Magic('I'): |
| 923 | case Magic('k'): |
| 924 | case Magic('K'): |
| 925 | case Magic('f'): |
| 926 | case Magic('F'): |
| 927 | case Magic('p'): |
| 928 | case Magic('P'): |
| 929 | case Magic('s'): |
| 930 | case Magic('S'): |
| 931 | case Magic('d'): |
| 932 | case Magic('D'): |
| 933 | case Magic('x'): |
| 934 | case Magic('X'): |
| 935 | case Magic('o'): |
| 936 | case Magic('O'): |
| 937 | case Magic('w'): |
| 938 | case Magic('W'): |
| 939 | case Magic('h'): |
| 940 | case Magic('H'): |
| 941 | case Magic('a'): |
| 942 | case Magic('A'): |
| 943 | case Magic('l'): |
| 944 | case Magic('L'): |
| 945 | case Magic('u'): |
| 946 | case Magic('U'): |
| 947 | p = vim_strchr(classchars, no_Magic(c)); |
| 948 | if (p == NULL) |
| 949 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 950 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 951 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 952 | } |
| 953 | #ifdef FEAT_MBYTE |
| 954 | /* When '.' is followed by a composing char ignore the dot, so that |
| 955 | * the composing char is matched here. */ |
| 956 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 957 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 958 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 959 | c = getchr(); |
| 960 | goto nfa_do_multibyte; |
| 961 | } |
| 962 | #endif |
| 963 | EMIT(nfa_classcodes[p - classchars]); |
| 964 | if (extra == ADD_NL) |
| 965 | { |
| 966 | EMIT(NFA_NEWL); |
| 967 | EMIT(NFA_OR); |
| 968 | regflags |= RF_HASNL; |
| 969 | } |
| 970 | break; |
| 971 | |
| 972 | case Magic('n'): |
| 973 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 974 | /* In a string "\n" matches a newline character. */ |
| 975 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 976 | else |
| 977 | { |
| 978 | /* In buffer text "\n" matches the end of a line. */ |
| 979 | EMIT(NFA_NEWL); |
| 980 | regflags |= RF_HASNL; |
| 981 | } |
| 982 | break; |
| 983 | |
| 984 | case Magic('('): |
| 985 | if (nfa_reg(REG_PAREN) == FAIL) |
| 986 | return FAIL; /* cascaded error */ |
| 987 | break; |
| 988 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 989 | case Magic('|'): |
| 990 | case Magic('&'): |
| 991 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 992 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 993 | return FAIL; |
| 994 | |
| 995 | case Magic('='): |
| 996 | case Magic('?'): |
| 997 | case Magic('+'): |
| 998 | case Magic('@'): |
| 999 | case Magic('*'): |
| 1000 | case Magic('{'): |
| 1001 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1002 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1003 | return FAIL; |
| 1004 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 1005 | case Magic('~'): |
| 1006 | { |
| 1007 | char_u *lp; |
| 1008 | |
| 1009 | /* Previous substitute pattern. |
| 1010 | * Generated as "\%(pattern\)". */ |
| 1011 | if (reg_prev_sub == NULL) |
| 1012 | { |
| 1013 | EMSG(_(e_nopresub)); |
| 1014 | return FAIL; |
| 1015 | } |
| 1016 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 1017 | { |
| 1018 | EMIT(PTR2CHAR(lp)); |
| 1019 | if (lp != reg_prev_sub) |
| 1020 | EMIT(NFA_CONCAT); |
| 1021 | } |
| 1022 | EMIT(NFA_NOPEN); |
| 1023 | break; |
| 1024 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1025 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 1026 | case Magic('1'): |
| 1027 | case Magic('2'): |
| 1028 | case Magic('3'): |
| 1029 | case Magic('4'): |
| 1030 | case Magic('5'): |
| 1031 | case Magic('6'): |
| 1032 | case Magic('7'): |
| 1033 | case Magic('8'): |
| 1034 | case Magic('9'): |
| 1035 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 1036 | nfa_has_backref = TRUE; |
| 1037 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1038 | |
| 1039 | case Magic('z'): |
| 1040 | c = no_Magic(getchr()); |
| 1041 | switch (c) |
| 1042 | { |
| 1043 | case 's': |
| 1044 | EMIT(NFA_ZSTART); |
| 1045 | break; |
| 1046 | case 'e': |
| 1047 | EMIT(NFA_ZEND); |
| 1048 | nfa_has_zend = TRUE; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1049 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1050 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1051 | case '1': |
| 1052 | case '2': |
| 1053 | case '3': |
| 1054 | case '4': |
| 1055 | case '5': |
| 1056 | case '6': |
| 1057 | case '7': |
| 1058 | case '8': |
| 1059 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1060 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1061 | if (reg_do_extmatch != REX_USE) |
| 1062 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1063 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1064 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1065 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1066 | re_has_z = REX_USE; |
| 1067 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1068 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1069 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1070 | if (reg_do_extmatch != REX_SET) |
| 1071 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1072 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1073 | return FAIL; /* cascaded error */ |
| 1074 | re_has_z = REX_SET; |
| 1075 | break; |
| 1076 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1077 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1078 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1079 | no_Magic(c)); |
| 1080 | return FAIL; |
| 1081 | } |
| 1082 | break; |
| 1083 | |
| 1084 | case Magic('%'): |
| 1085 | c = no_Magic(getchr()); |
| 1086 | switch (c) |
| 1087 | { |
| 1088 | /* () without a back reference */ |
| 1089 | case '(': |
| 1090 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1091 | return FAIL; |
| 1092 | EMIT(NFA_NOPEN); |
| 1093 | break; |
| 1094 | |
| 1095 | case 'd': /* %d123 decimal */ |
| 1096 | case 'o': /* %o123 octal */ |
| 1097 | case 'x': /* %xab hex 2 */ |
| 1098 | case 'u': /* %uabcd hex 4 */ |
| 1099 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1100 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1101 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1102 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1103 | switch (c) |
| 1104 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1105 | case 'd': nr = getdecchrs(); break; |
| 1106 | case 'o': nr = getoctchrs(); break; |
| 1107 | case 'x': nr = gethexchrs(2); break; |
| 1108 | case 'u': nr = gethexchrs(4); break; |
| 1109 | case 'U': nr = gethexchrs(8); break; |
| 1110 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1111 | } |
| 1112 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1113 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1114 | EMSG2_RET_FAIL( |
| 1115 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1116 | reg_magic == MAGIC_ALL); |
| 1117 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1118 | EMIT(nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1119 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1120 | break; |
| 1121 | |
| 1122 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1123 | * pattern -- regardless of whether or not it makes sense. */ |
| 1124 | case '^': |
| 1125 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1126 | break; |
| 1127 | |
| 1128 | case '$': |
| 1129 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1130 | break; |
| 1131 | |
| 1132 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1133 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1134 | break; |
| 1135 | |
| 1136 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1137 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1138 | break; |
| 1139 | |
| 1140 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1141 | { |
| 1142 | int n; |
| 1143 | |
| 1144 | /* \%[abc] */ |
| 1145 | for (n = 0; (c = getchr()) != ']'; ++n) |
| 1146 | { |
| 1147 | if (c == NUL) |
| 1148 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1149 | reg_magic == MAGIC_ALL); |
| 1150 | EMIT(c); |
| 1151 | } |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1152 | if (n == 0) |
| 1153 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1154 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1155 | EMIT(NFA_OPT_CHARS); |
| 1156 | EMIT(n); |
| 1157 | break; |
| 1158 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1159 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1160 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1161 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1162 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1163 | int cmp = c; |
| 1164 | |
| 1165 | if (c == '<' || c == '>') |
| 1166 | c = getchr(); |
| 1167 | while (VIM_ISDIGIT(c)) |
| 1168 | { |
| 1169 | n = n * 10 + (c - '0'); |
| 1170 | c = getchr(); |
| 1171 | } |
| 1172 | if (c == 'l' || c == 'c' || c == 'v') |
| 1173 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1174 | if (c == 'l') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1175 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1176 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1177 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1178 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1179 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1180 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1181 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1182 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1183 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1184 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1185 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1186 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1187 | break; |
| 1188 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1189 | else if (c == '\'' && n == 0) |
| 1190 | { |
| 1191 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1192 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1193 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1194 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1195 | break; |
| 1196 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1197 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1198 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1199 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1200 | return FAIL; |
| 1201 | } |
| 1202 | break; |
| 1203 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1204 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1205 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1206 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1207 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1208 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1209 | * Each character is produced as a regular state, using |
| 1210 | * NFA_CONCAT to bind them together. |
| 1211 | * Besides normal characters there can be: |
| 1212 | * - character classes NFA_CLASS_* |
| 1213 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1214 | */ |
| 1215 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1216 | p = regparse; |
| 1217 | endp = skip_anyof(p); |
| 1218 | if (*endp == ']') |
| 1219 | { |
| 1220 | /* |
| 1221 | * Try to reverse engineer character classes. For example, |
| 1222 | * recognize that [0-9] stands for \d and [A-Za-z_] with \h, |
| 1223 | * and perform the necessary substitutions in the NFA. |
| 1224 | */ |
| 1225 | result = nfa_recognize_char_class(regparse, endp, |
| 1226 | extra == ADD_NL); |
| 1227 | if (result != FAIL) |
| 1228 | { |
| 1229 | if (result >= NFA_DIGIT && result <= NFA_NUPPER) |
| 1230 | EMIT(result); |
| 1231 | else /* must be char class + newline */ |
| 1232 | { |
| 1233 | EMIT(result - ADD_NL); |
| 1234 | EMIT(NFA_NEWL); |
| 1235 | EMIT(NFA_OR); |
| 1236 | } |
| 1237 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1238 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1239 | return OK; |
| 1240 | } |
| 1241 | /* |
| 1242 | * Failed to recognize a character class. Use the simple |
| 1243 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1244 | */ |
| 1245 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1246 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1247 | if (*regparse == '^') /* negated range */ |
| 1248 | { |
| 1249 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1250 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1251 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1252 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1253 | else |
| 1254 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1255 | if (*regparse == '-') |
| 1256 | { |
| 1257 | startc = '-'; |
| 1258 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1259 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1260 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1261 | } |
| 1262 | /* Emit the OR branches for each character in the [] */ |
| 1263 | emit_range = FALSE; |
| 1264 | while (regparse < endp) |
| 1265 | { |
| 1266 | oldstartc = startc; |
| 1267 | startc = -1; |
| 1268 | got_coll_char = FALSE; |
| 1269 | if (*regparse == '[') |
| 1270 | { |
| 1271 | /* Check for [: :], [= =], [. .] */ |
| 1272 | equiclass = collclass = 0; |
| 1273 | charclass = get_char_class(®parse); |
| 1274 | if (charclass == CLASS_NONE) |
| 1275 | { |
| 1276 | equiclass = get_equi_class(®parse); |
| 1277 | if (equiclass == 0) |
| 1278 | collclass = get_coll_element(®parse); |
| 1279 | } |
| 1280 | |
| 1281 | /* Character class like [:alpha:] */ |
| 1282 | if (charclass != CLASS_NONE) |
| 1283 | { |
| 1284 | switch (charclass) |
| 1285 | { |
| 1286 | case CLASS_ALNUM: |
| 1287 | EMIT(NFA_CLASS_ALNUM); |
| 1288 | break; |
| 1289 | case CLASS_ALPHA: |
| 1290 | EMIT(NFA_CLASS_ALPHA); |
| 1291 | break; |
| 1292 | case CLASS_BLANK: |
| 1293 | EMIT(NFA_CLASS_BLANK); |
| 1294 | break; |
| 1295 | case CLASS_CNTRL: |
| 1296 | EMIT(NFA_CLASS_CNTRL); |
| 1297 | break; |
| 1298 | case CLASS_DIGIT: |
| 1299 | EMIT(NFA_CLASS_DIGIT); |
| 1300 | break; |
| 1301 | case CLASS_GRAPH: |
| 1302 | EMIT(NFA_CLASS_GRAPH); |
| 1303 | break; |
| 1304 | case CLASS_LOWER: |
| 1305 | EMIT(NFA_CLASS_LOWER); |
| 1306 | break; |
| 1307 | case CLASS_PRINT: |
| 1308 | EMIT(NFA_CLASS_PRINT); |
| 1309 | break; |
| 1310 | case CLASS_PUNCT: |
| 1311 | EMIT(NFA_CLASS_PUNCT); |
| 1312 | break; |
| 1313 | case CLASS_SPACE: |
| 1314 | EMIT(NFA_CLASS_SPACE); |
| 1315 | break; |
| 1316 | case CLASS_UPPER: |
| 1317 | EMIT(NFA_CLASS_UPPER); |
| 1318 | break; |
| 1319 | case CLASS_XDIGIT: |
| 1320 | EMIT(NFA_CLASS_XDIGIT); |
| 1321 | break; |
| 1322 | case CLASS_TAB: |
| 1323 | EMIT(NFA_CLASS_TAB); |
| 1324 | break; |
| 1325 | case CLASS_RETURN: |
| 1326 | EMIT(NFA_CLASS_RETURN); |
| 1327 | break; |
| 1328 | case CLASS_BACKSPACE: |
| 1329 | EMIT(NFA_CLASS_BACKSPACE); |
| 1330 | break; |
| 1331 | case CLASS_ESCAPE: |
| 1332 | EMIT(NFA_CLASS_ESCAPE); |
| 1333 | break; |
| 1334 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1335 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1336 | continue; |
| 1337 | } |
| 1338 | /* Try equivalence class [=a=] and the like */ |
| 1339 | if (equiclass != 0) |
| 1340 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1341 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1342 | if (result == FAIL) |
| 1343 | { |
| 1344 | /* should never happen */ |
| 1345 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1346 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1347 | continue; |
| 1348 | } |
| 1349 | /* Try collating class like [. .] */ |
| 1350 | if (collclass != 0) |
| 1351 | { |
| 1352 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1353 | /* Will emit the proper atom at the end of the |
| 1354 | * while loop. */ |
| 1355 | } |
| 1356 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1357 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1358 | * start character. */ |
| 1359 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1360 | { |
| 1361 | emit_range = TRUE; |
| 1362 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1363 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1364 | continue; /* reading the end of the range */ |
| 1365 | } |
| 1366 | |
| 1367 | /* Now handle simple and escaped characters. |
| 1368 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1369 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1370 | * 'cpoptions' is not included. |
| 1371 | * Posix doesn't recognize backslash at all. |
| 1372 | */ |
| 1373 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1374 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1375 | && regparse + 1 <= endp |
| 1376 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1377 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1378 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1379 | != NULL) |
| 1380 | ) |
| 1381 | ) |
| 1382 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1383 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1384 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1385 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1386 | startc = reg_string ? NL : NFA_NEWL; |
| 1387 | else |
| 1388 | if (*regparse == 'd' |
| 1389 | || *regparse == 'o' |
| 1390 | || *regparse == 'x' |
| 1391 | || *regparse == 'u' |
| 1392 | || *regparse == 'U' |
| 1393 | ) |
| 1394 | { |
| 1395 | /* TODO(RE) This needs more testing */ |
| 1396 | startc = coll_get_char(); |
| 1397 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1398 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1399 | } |
| 1400 | else |
| 1401 | { |
| 1402 | /* \r,\t,\e,\b */ |
| 1403 | startc = backslash_trans(*regparse); |
| 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | /* Normal printable char */ |
| 1408 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1409 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1410 | |
| 1411 | /* Previous char was '-', so this char is end of range. */ |
| 1412 | if (emit_range) |
| 1413 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1414 | endc = startc; |
| 1415 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1416 | if (startc > endc) |
| 1417 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1418 | |
| 1419 | if (endc > startc + 2) |
| 1420 | { |
| 1421 | /* Emit a range instead of the sequence of |
| 1422 | * individual characters. */ |
| 1423 | if (startc == 0) |
| 1424 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1425 | EMIT(1); |
| 1426 | else |
| 1427 | --post_ptr; /* remove NFA_CONCAT */ |
| 1428 | EMIT(endc); |
| 1429 | EMIT(NFA_RANGE); |
| 1430 | EMIT(NFA_CONCAT); |
| 1431 | } |
| 1432 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1433 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1434 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1435 | || (*mb_char2len)(endc) > 1)) |
| 1436 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1437 | /* Emit the characters in the range. |
| 1438 | * "startc" was already emitted, so skip it. |
| 1439 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1440 | for (c = startc + 1; c <= endc; c++) |
| 1441 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1442 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1443 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1444 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1445 | } |
| 1446 | else |
| 1447 | #endif |
| 1448 | { |
| 1449 | #ifdef EBCDIC |
| 1450 | int alpha_only = FALSE; |
| 1451 | |
| 1452 | /* for alphabetical range skip the gaps |
| 1453 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1454 | if (isalpha(startc) && isalpha(endc)) |
| 1455 | alpha_only = TRUE; |
| 1456 | #endif |
| 1457 | /* Emit the range. "startc" was already emitted, so |
| 1458 | * skip it. */ |
| 1459 | for (c = startc + 1; c <= endc; c++) |
| 1460 | #ifdef EBCDIC |
| 1461 | if (!alpha_only || isalpha(startc)) |
| 1462 | #endif |
| 1463 | { |
| 1464 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1465 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1466 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1467 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1468 | emit_range = FALSE; |
| 1469 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1470 | } |
| 1471 | else |
| 1472 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1473 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1474 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1475 | * Normally, simply emit startc. But if we get char |
| 1476 | * code=0 from a collating char, then replace it with |
| 1477 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1478 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1479 | * the backtracking engine. */ |
| 1480 | if (startc == NFA_NEWL) |
| 1481 | { |
| 1482 | /* Line break can't be matched as part of the |
| 1483 | * collection, add an OR below. But not for negated |
| 1484 | * range. */ |
| 1485 | if (!negated) |
| 1486 | extra = ADD_NL; |
| 1487 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1488 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1489 | { |
| 1490 | if (got_coll_char == TRUE && startc == 0) |
| 1491 | EMIT(0x0a); |
| 1492 | else |
| 1493 | EMIT(startc); |
| 1494 | EMIT(NFA_CONCAT); |
| 1495 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1496 | } |
| 1497 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1498 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1499 | } /* while (p < endp) */ |
| 1500 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1501 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1502 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1503 | { |
| 1504 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1505 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1506 | } |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1507 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1508 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1509 | /* skip the trailing ] */ |
| 1510 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1511 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1512 | |
| 1513 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1514 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1515 | EMIT(NFA_END_NEG_COLL); |
| 1516 | else |
| 1517 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1518 | |
| 1519 | /* \_[] also matches \n but it's not negated */ |
| 1520 | if (extra == ADD_NL) |
| 1521 | { |
| 1522 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1523 | EMIT(NFA_OR); |
| 1524 | } |
| 1525 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1526 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1527 | } /* if exists closing ] */ |
| 1528 | |
| 1529 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1530 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1531 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1532 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1533 | default: |
| 1534 | { |
| 1535 | #ifdef FEAT_MBYTE |
| 1536 | int plen; |
| 1537 | |
| 1538 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1539 | /* plen is length of current char with composing chars */ |
| 1540 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1541 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1542 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1543 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1544 | int i = 0; |
| 1545 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1546 | /* A base character plus composing characters, or just one |
| 1547 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1548 | * This requires creating a separate atom as if enclosing |
| 1549 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1550 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1551 | * building the postfix form, not the NFA itself; |
| 1552 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1553 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1554 | for (;;) |
| 1555 | { |
| 1556 | EMIT(c); |
| 1557 | if (i > 0) |
| 1558 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1559 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1560 | break; |
| 1561 | c = utf_ptr2char(old_regparse + i); |
| 1562 | } |
| 1563 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1564 | regparse = old_regparse + plen; |
| 1565 | } |
| 1566 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1567 | #endif |
| 1568 | { |
| 1569 | c = no_Magic(c); |
| 1570 | EMIT(c); |
| 1571 | } |
| 1572 | return OK; |
| 1573 | } |
| 1574 | } |
| 1575 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1576 | return OK; |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * Parse something followed by possible [*+=]. |
| 1581 | * |
| 1582 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1583 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1584 | * characters: "", "a", "aa", etc. |
| 1585 | * |
| 1586 | * piece ::= atom |
| 1587 | * or atom multi |
| 1588 | */ |
| 1589 | static int |
| 1590 | nfa_regpiece() |
| 1591 | { |
| 1592 | int i; |
| 1593 | int op; |
| 1594 | int ret; |
| 1595 | long minval, maxval; |
| 1596 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1597 | parse_state_T old_state; |
| 1598 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1599 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1600 | int old_post_pos; |
| 1601 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1602 | int quest; |
| 1603 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1604 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1605 | * next. */ |
| 1606 | save_parse_state(&old_state); |
| 1607 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1608 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1609 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1610 | |
| 1611 | ret = nfa_regatom(); |
| 1612 | if (ret == FAIL) |
| 1613 | return FAIL; /* cascaded error */ |
| 1614 | |
| 1615 | op = peekchr(); |
| 1616 | if (re_multi_type(op) == NOT_MULTI) |
| 1617 | return OK; |
| 1618 | |
| 1619 | skipchr(); |
| 1620 | switch (op) |
| 1621 | { |
| 1622 | case Magic('*'): |
| 1623 | EMIT(NFA_STAR); |
| 1624 | break; |
| 1625 | |
| 1626 | case Magic('+'): |
| 1627 | /* |
| 1628 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1629 | * first and only submatch would be "aaa". But the backtracking |
| 1630 | * engine interprets the plus as "try matching one more time", and |
| 1631 | * a* matches a second time at the end of the input, the empty |
| 1632 | * string. |
| 1633 | * The submatch will the empty string. |
| 1634 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1635 | * In order to be consistent with the old engine, we replace |
| 1636 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1637 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1638 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1639 | curchr = -1; |
| 1640 | if (nfa_regatom() == FAIL) |
| 1641 | return FAIL; |
| 1642 | EMIT(NFA_STAR); |
| 1643 | EMIT(NFA_CONCAT); |
| 1644 | skipchr(); /* skip the \+ */ |
| 1645 | break; |
| 1646 | |
| 1647 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1648 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1649 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1650 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1651 | switch(op) |
| 1652 | { |
| 1653 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1654 | /* \@= */ |
| 1655 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1656 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1657 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1658 | /* \@! */ |
| 1659 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1660 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1661 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1662 | op = no_Magic(getchr()); |
| 1663 | if (op == '=') |
| 1664 | /* \@<= */ |
| 1665 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1666 | else if (op == '!') |
| 1667 | /* \@<! */ |
| 1668 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1669 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1670 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1671 | /* \@> */ |
| 1672 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1673 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1674 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1675 | if (i == 0) |
| 1676 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1677 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1678 | return FAIL; |
| 1679 | } |
| 1680 | EMIT(i); |
| 1681 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1682 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1683 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1684 | break; |
| 1685 | |
| 1686 | case Magic('?'): |
| 1687 | case Magic('='): |
| 1688 | EMIT(NFA_QUEST); |
| 1689 | break; |
| 1690 | |
| 1691 | case Magic('{'): |
| 1692 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1693 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1694 | * version of '?' |
| 1695 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1696 | * parenthesis have the same id |
| 1697 | */ |
| 1698 | |
| 1699 | greedy = TRUE; |
| 1700 | c2 = peekchr(); |
| 1701 | if (c2 == '-' || c2 == Magic('-')) |
| 1702 | { |
| 1703 | skipchr(); |
| 1704 | greedy = FALSE; |
| 1705 | } |
| 1706 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1707 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 1708 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1709 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 1710 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1711 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1712 | { |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1713 | if (greedy) |
| 1714 | /* \{}, \{0,} */ |
| 1715 | EMIT(NFA_STAR); |
| 1716 | else |
| 1717 | /* \{-}, \{-0,} */ |
| 1718 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1719 | break; |
| 1720 | } |
| 1721 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1722 | /* Special case: x{0} or x{-0} */ |
| 1723 | if (maxval == 0) |
| 1724 | { |
| 1725 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1726 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1727 | /* NFA_SKIP_CHAR has 0-length and works everywhere */ |
| 1728 | EMIT(NFA_SKIP_CHAR); |
| 1729 | return OK; |
| 1730 | } |
| 1731 | |
| 1732 | /* Ignore previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1733 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1734 | /* Save parse state after the repeated atom and the \{} */ |
| 1735 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1736 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1737 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 1738 | for (i = 0; i < maxval; i++) |
| 1739 | { |
| 1740 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1741 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1742 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1743 | if (nfa_regatom() == FAIL) |
| 1744 | return FAIL; |
| 1745 | /* after "minval" times, atoms are optional */ |
| 1746 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1747 | { |
| 1748 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1749 | { |
| 1750 | if (greedy) |
| 1751 | EMIT(NFA_STAR); |
| 1752 | else |
| 1753 | EMIT(NFA_STAR_NONGREEDY); |
| 1754 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1755 | else |
| 1756 | EMIT(quest); |
| 1757 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1758 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1759 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1760 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 1761 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1762 | } |
| 1763 | |
| 1764 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1765 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1766 | curchr = -1; |
| 1767 | |
| 1768 | break; |
| 1769 | |
| 1770 | |
| 1771 | default: |
| 1772 | break; |
| 1773 | } /* end switch */ |
| 1774 | |
| 1775 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1776 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1777 | EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1778 | |
| 1779 | return OK; |
| 1780 | } |
| 1781 | |
| 1782 | /* |
| 1783 | * Parse one or more pieces, concatenated. It matches a match for the |
| 1784 | * first piece, followed by a match for the second piece, etc. Example: |
| 1785 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 1786 | * |
| 1787 | * concat ::= piece |
| 1788 | * or piece piece |
| 1789 | * or piece piece piece |
| 1790 | * etc. |
| 1791 | */ |
| 1792 | static int |
| 1793 | nfa_regconcat() |
| 1794 | { |
| 1795 | int cont = TRUE; |
| 1796 | int first = TRUE; |
| 1797 | |
| 1798 | while (cont) |
| 1799 | { |
| 1800 | switch (peekchr()) |
| 1801 | { |
| 1802 | case NUL: |
| 1803 | case Magic('|'): |
| 1804 | case Magic('&'): |
| 1805 | case Magic(')'): |
| 1806 | cont = FALSE; |
| 1807 | break; |
| 1808 | |
| 1809 | case Magic('Z'): |
| 1810 | #ifdef FEAT_MBYTE |
| 1811 | regflags |= RF_ICOMBINE; |
| 1812 | #endif |
| 1813 | skipchr_keepstart(); |
| 1814 | break; |
| 1815 | case Magic('c'): |
| 1816 | regflags |= RF_ICASE; |
| 1817 | skipchr_keepstart(); |
| 1818 | break; |
| 1819 | case Magic('C'): |
| 1820 | regflags |= RF_NOICASE; |
| 1821 | skipchr_keepstart(); |
| 1822 | break; |
| 1823 | case Magic('v'): |
| 1824 | reg_magic = MAGIC_ALL; |
| 1825 | skipchr_keepstart(); |
| 1826 | curchr = -1; |
| 1827 | break; |
| 1828 | case Magic('m'): |
| 1829 | reg_magic = MAGIC_ON; |
| 1830 | skipchr_keepstart(); |
| 1831 | curchr = -1; |
| 1832 | break; |
| 1833 | case Magic('M'): |
| 1834 | reg_magic = MAGIC_OFF; |
| 1835 | skipchr_keepstart(); |
| 1836 | curchr = -1; |
| 1837 | break; |
| 1838 | case Magic('V'): |
| 1839 | reg_magic = MAGIC_NONE; |
| 1840 | skipchr_keepstart(); |
| 1841 | curchr = -1; |
| 1842 | break; |
| 1843 | |
| 1844 | default: |
| 1845 | if (nfa_regpiece() == FAIL) |
| 1846 | return FAIL; |
| 1847 | if (first == FALSE) |
| 1848 | EMIT(NFA_CONCAT); |
| 1849 | else |
| 1850 | first = FALSE; |
| 1851 | break; |
| 1852 | } |
| 1853 | } |
| 1854 | |
| 1855 | return OK; |
| 1856 | } |
| 1857 | |
| 1858 | /* |
| 1859 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 1860 | * last concat, but only if all the preceding concats also match at the same |
| 1861 | * position. Examples: |
| 1862 | * "foobeep\&..." matches "foo" in "foobeep". |
| 1863 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 1864 | * |
| 1865 | * branch ::= concat |
| 1866 | * or concat \& concat |
| 1867 | * or concat \& concat \& concat |
| 1868 | * etc. |
| 1869 | */ |
| 1870 | static int |
| 1871 | nfa_regbranch() |
| 1872 | { |
| 1873 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1874 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1875 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1876 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1877 | |
| 1878 | /* First branch, possibly the only one */ |
| 1879 | if (nfa_regconcat() == FAIL) |
| 1880 | return FAIL; |
| 1881 | |
| 1882 | ch = peekchr(); |
| 1883 | /* Try next concats */ |
| 1884 | while (ch == Magic('&')) |
| 1885 | { |
| 1886 | skipchr(); |
| 1887 | EMIT(NFA_NOPEN); |
| 1888 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1889 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1890 | if (nfa_regconcat() == FAIL) |
| 1891 | return FAIL; |
| 1892 | /* 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] | 1893 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1894 | EMIT(NFA_SKIP_CHAR); |
| 1895 | EMIT(NFA_CONCAT); |
| 1896 | ch = peekchr(); |
| 1897 | } |
| 1898 | |
| 1899 | /* Even if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1900 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1901 | EMIT(NFA_SKIP_CHAR); |
| 1902 | |
| 1903 | return OK; |
| 1904 | } |
| 1905 | |
| 1906 | /* |
| 1907 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 1908 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 1909 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 1910 | * is used. |
| 1911 | * |
| 1912 | * pattern ::= branch |
| 1913 | * or branch \| branch |
| 1914 | * or branch \| branch \| branch |
| 1915 | * etc. |
| 1916 | */ |
| 1917 | static int |
| 1918 | nfa_reg(paren) |
| 1919 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 1920 | { |
| 1921 | int parno = 0; |
| 1922 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1923 | if (paren == REG_PAREN) |
| 1924 | { |
| 1925 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1926 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1927 | parno = regnpar++; |
| 1928 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1929 | #ifdef FEAT_SYN_HL |
| 1930 | else if (paren == REG_ZPAREN) |
| 1931 | { |
| 1932 | /* Make a ZOPEN node. */ |
| 1933 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1934 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1935 | parno = regnzpar++; |
| 1936 | } |
| 1937 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1938 | |
| 1939 | if (nfa_regbranch() == FAIL) |
| 1940 | return FAIL; /* cascaded error */ |
| 1941 | |
| 1942 | while (peekchr() == Magic('|')) |
| 1943 | { |
| 1944 | skipchr(); |
| 1945 | if (nfa_regbranch() == FAIL) |
| 1946 | return FAIL; /* cascaded error */ |
| 1947 | EMIT(NFA_OR); |
| 1948 | } |
| 1949 | |
| 1950 | /* Check for proper termination. */ |
| 1951 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 1952 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1953 | if (paren == REG_NPAREN) |
| 1954 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 1955 | else |
| 1956 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 1957 | } |
| 1958 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 1959 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1960 | if (peekchr() == Magic(')')) |
| 1961 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 1962 | else |
| 1963 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 1964 | } |
| 1965 | /* |
| 1966 | * Here we set the flag allowing back references to this set of |
| 1967 | * parentheses. |
| 1968 | */ |
| 1969 | if (paren == REG_PAREN) |
| 1970 | { |
| 1971 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 1972 | EMIT(NFA_MOPEN + parno); |
| 1973 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1974 | #ifdef FEAT_SYN_HL |
| 1975 | else if (paren == REG_ZPAREN) |
| 1976 | EMIT(NFA_ZOPEN + parno); |
| 1977 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1978 | |
| 1979 | return OK; |
| 1980 | } |
| 1981 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1982 | #ifdef DEBUG |
| 1983 | static char_u code[50]; |
| 1984 | |
| 1985 | static void |
| 1986 | nfa_set_code(c) |
| 1987 | int c; |
| 1988 | { |
| 1989 | int addnl = FALSE; |
| 1990 | |
| 1991 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 1992 | { |
| 1993 | addnl = TRUE; |
| 1994 | c -= ADD_NL; |
| 1995 | } |
| 1996 | |
| 1997 | STRCPY(code, ""); |
| 1998 | switch (c) |
| 1999 | { |
| 2000 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 2001 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 2002 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 2003 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 2004 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 2005 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 2006 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2007 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 2008 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 2009 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 2010 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 2011 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 2012 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 2013 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 2014 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 2015 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2016 | #ifdef FEAT_SYN_HL |
| 2017 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 2018 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 2019 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 2020 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 2021 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 2022 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 2023 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 2024 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 2025 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 2026 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2027 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 2028 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2029 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2030 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2031 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 2032 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2033 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2034 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 2035 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 2036 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2037 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 2038 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 2039 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2040 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 2041 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2042 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2043 | case NFA_START_INVISIBLE_NEG: |
| 2044 | STRCPY(code, "NFA_START_INVISIBLE_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2045 | case NFA_START_INVISIBLE_BEFORE: |
| 2046 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2047 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2048 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2049 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2050 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2051 | case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2052 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2053 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2054 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2055 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2056 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2057 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2058 | case NFA_MOPEN: |
| 2059 | case NFA_MOPEN1: |
| 2060 | case NFA_MOPEN2: |
| 2061 | case NFA_MOPEN3: |
| 2062 | case NFA_MOPEN4: |
| 2063 | case NFA_MOPEN5: |
| 2064 | case NFA_MOPEN6: |
| 2065 | case NFA_MOPEN7: |
| 2066 | case NFA_MOPEN8: |
| 2067 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2068 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2069 | code[10] = c - NFA_MOPEN + '0'; |
| 2070 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2071 | case NFA_MCLOSE: |
| 2072 | case NFA_MCLOSE1: |
| 2073 | case NFA_MCLOSE2: |
| 2074 | case NFA_MCLOSE3: |
| 2075 | case NFA_MCLOSE4: |
| 2076 | case NFA_MCLOSE5: |
| 2077 | case NFA_MCLOSE6: |
| 2078 | case NFA_MCLOSE7: |
| 2079 | case NFA_MCLOSE8: |
| 2080 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2081 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2082 | code[11] = c - NFA_MCLOSE + '0'; |
| 2083 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2084 | #ifdef FEAT_SYN_HL |
| 2085 | case NFA_ZOPEN: |
| 2086 | case NFA_ZOPEN1: |
| 2087 | case NFA_ZOPEN2: |
| 2088 | case NFA_ZOPEN3: |
| 2089 | case NFA_ZOPEN4: |
| 2090 | case NFA_ZOPEN5: |
| 2091 | case NFA_ZOPEN6: |
| 2092 | case NFA_ZOPEN7: |
| 2093 | case NFA_ZOPEN8: |
| 2094 | case NFA_ZOPEN9: |
| 2095 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2096 | code[10] = c - NFA_ZOPEN + '0'; |
| 2097 | break; |
| 2098 | case NFA_ZCLOSE: |
| 2099 | case NFA_ZCLOSE1: |
| 2100 | case NFA_ZCLOSE2: |
| 2101 | case NFA_ZCLOSE3: |
| 2102 | case NFA_ZCLOSE4: |
| 2103 | case NFA_ZCLOSE5: |
| 2104 | case NFA_ZCLOSE6: |
| 2105 | case NFA_ZCLOSE7: |
| 2106 | case NFA_ZCLOSE8: |
| 2107 | case NFA_ZCLOSE9: |
| 2108 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2109 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2110 | break; |
| 2111 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2112 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2113 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2114 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2115 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2116 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2117 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2118 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2119 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2120 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2121 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2122 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2123 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2124 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2125 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2126 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2127 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2128 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2129 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2130 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2131 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
| 2132 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2133 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2134 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2135 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2136 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2137 | case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; |
| 2138 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2139 | |
| 2140 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2141 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2142 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2143 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2144 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2145 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2146 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2147 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2148 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2149 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2150 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2151 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2152 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2153 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2154 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2155 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2156 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2157 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2158 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2159 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2160 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2161 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2162 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2163 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2164 | |
| 2165 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2166 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2167 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2168 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2169 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2170 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2171 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2172 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2173 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2174 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2175 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2176 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2177 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2178 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2179 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2180 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2181 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2182 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2183 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2184 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2185 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2186 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2187 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2188 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2189 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2190 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2191 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
| 2192 | |
| 2193 | default: |
| 2194 | STRCPY(code, "CHAR(x)"); |
| 2195 | code[5] = c; |
| 2196 | } |
| 2197 | |
| 2198 | if (addnl == TRUE) |
| 2199 | STRCAT(code, " + NEWLINE "); |
| 2200 | |
| 2201 | } |
| 2202 | |
| 2203 | #ifdef ENABLE_LOG |
| 2204 | static FILE *log_fd; |
| 2205 | |
| 2206 | /* |
| 2207 | * Print the postfix notation of the current regexp. |
| 2208 | */ |
| 2209 | static void |
| 2210 | nfa_postfix_dump(expr, retval) |
| 2211 | char_u *expr; |
| 2212 | int retval; |
| 2213 | { |
| 2214 | int *p; |
| 2215 | FILE *f; |
| 2216 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2217 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2218 | if (f != NULL) |
| 2219 | { |
| 2220 | fprintf(f, "\n-------------------------\n"); |
| 2221 | if (retval == FAIL) |
| 2222 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2223 | else if (retval == OK) |
| 2224 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2225 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2226 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2227 | { |
| 2228 | nfa_set_code(*p); |
| 2229 | fprintf(f, "%s, ", code); |
| 2230 | } |
| 2231 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2232 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2233 | fprintf(f, "%d ", *p); |
| 2234 | fprintf(f, "\n\n"); |
| 2235 | fclose(f); |
| 2236 | } |
| 2237 | } |
| 2238 | |
| 2239 | /* |
| 2240 | * Print the NFA starting with a root node "state". |
| 2241 | */ |
| 2242 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2243 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2244 | FILE *debugf; |
| 2245 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2246 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2247 | garray_T indent; |
| 2248 | |
| 2249 | ga_init2(&indent, 1, 64); |
| 2250 | ga_append(&indent, '\0'); |
| 2251 | nfa_print_state2(debugf, state, &indent); |
| 2252 | ga_clear(&indent); |
| 2253 | } |
| 2254 | |
| 2255 | static void |
| 2256 | nfa_print_state2(debugf, state, indent) |
| 2257 | FILE *debugf; |
| 2258 | nfa_state_T *state; |
| 2259 | garray_T *indent; |
| 2260 | { |
| 2261 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2262 | |
| 2263 | if (state == NULL) |
| 2264 | return; |
| 2265 | |
| 2266 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2267 | |
| 2268 | /* Output indent */ |
| 2269 | p = (char_u *)indent->ga_data; |
| 2270 | if (indent->ga_len >= 3) |
| 2271 | { |
| 2272 | int last = indent->ga_len - 3; |
| 2273 | char_u save[2]; |
| 2274 | |
| 2275 | STRNCPY(save, &p[last], 2); |
| 2276 | STRNCPY(&p[last], "+-", 2); |
| 2277 | fprintf(debugf, " %s", p); |
| 2278 | STRNCPY(&p[last], save, 2); |
| 2279 | } |
| 2280 | else |
| 2281 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2282 | |
| 2283 | nfa_set_code(state->c); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2284 | fprintf(debugf, "%s (%d) (id=%d) val=%d\n", |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2285 | code, |
| 2286 | state->c, |
| 2287 | abs(state->id), |
| 2288 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2289 | if (state->id < 0) |
| 2290 | return; |
| 2291 | |
| 2292 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2293 | |
| 2294 | /* grow indent for state->out */ |
| 2295 | indent->ga_len -= 1; |
| 2296 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2297 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2298 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2299 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2300 | ga_append(indent, '\0'); |
| 2301 | |
| 2302 | nfa_print_state2(debugf, state->out, indent); |
| 2303 | |
| 2304 | /* replace last part of indent for state->out1 */ |
| 2305 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2306 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2307 | ga_append(indent, '\0'); |
| 2308 | |
| 2309 | nfa_print_state2(debugf, state->out1, indent); |
| 2310 | |
| 2311 | /* shrink indent */ |
| 2312 | indent->ga_len -= 3; |
| 2313 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | /* |
| 2317 | * Print the NFA state machine. |
| 2318 | */ |
| 2319 | static void |
| 2320 | nfa_dump(prog) |
| 2321 | nfa_regprog_T *prog; |
| 2322 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2323 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2324 | |
| 2325 | if (debugf != NULL) |
| 2326 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2327 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2328 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 2329 | if (prog->reganch) |
| 2330 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2331 | if (prog->regstart != NUL) |
| 2332 | fprintf(debugf, "regstart: %c (decimal: %d)\n", |
| 2333 | prog->regstart, prog->regstart); |
| 2334 | if (prog->match_text != NULL) |
| 2335 | fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2336 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2337 | fclose(debugf); |
| 2338 | } |
| 2339 | } |
| 2340 | #endif /* ENABLE_LOG */ |
| 2341 | #endif /* DEBUG */ |
| 2342 | |
| 2343 | /* |
| 2344 | * Parse r.e. @expr and convert it into postfix form. |
| 2345 | * Return the postfix string on success, NULL otherwise. |
| 2346 | */ |
| 2347 | static int * |
| 2348 | re2post() |
| 2349 | { |
| 2350 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2351 | return NULL; |
| 2352 | EMIT(NFA_MOPEN); |
| 2353 | return post_start; |
| 2354 | } |
| 2355 | |
| 2356 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2357 | |
| 2358 | /* |
| 2359 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2360 | * if c == MATCH, no arrows out; matching state. |
| 2361 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2362 | * If c < 256, labeled arrow with character c to out. |
| 2363 | */ |
| 2364 | |
| 2365 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2366 | |
| 2367 | /* |
| 2368 | * Allocate and initialize nfa_state_T. |
| 2369 | */ |
| 2370 | static nfa_state_T * |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2371 | alloc_state(c, out, out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2372 | int c; |
| 2373 | nfa_state_T *out; |
| 2374 | nfa_state_T *out1; |
| 2375 | { |
| 2376 | nfa_state_T *s; |
| 2377 | |
| 2378 | if (istate >= nstate) |
| 2379 | return NULL; |
| 2380 | |
| 2381 | s = &state_ptr[istate++]; |
| 2382 | |
| 2383 | s->c = c; |
| 2384 | s->out = out; |
| 2385 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2386 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2387 | |
| 2388 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2389 | s->lastlist[0] = 0; |
| 2390 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2391 | |
| 2392 | return s; |
| 2393 | } |
| 2394 | |
| 2395 | /* |
| 2396 | * A partially built NFA without the matching state filled in. |
| 2397 | * Frag_T.start points at the start state. |
| 2398 | * Frag_T.out is a list of places that need to be set to the |
| 2399 | * next state for this fragment. |
| 2400 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2401 | |
| 2402 | /* Since the out pointers in the list are always |
| 2403 | * uninitialized, we use the pointers themselves |
| 2404 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2405 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2406 | union Ptrlist |
| 2407 | { |
| 2408 | Ptrlist *next; |
| 2409 | nfa_state_T *s; |
| 2410 | }; |
| 2411 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2412 | struct Frag |
| 2413 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2414 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2415 | Ptrlist *out; |
| 2416 | }; |
| 2417 | typedef struct Frag Frag_T; |
| 2418 | |
| 2419 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2420 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2421 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2422 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2423 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2424 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2425 | |
| 2426 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2427 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2428 | */ |
| 2429 | static Frag_T |
| 2430 | frag(start, out) |
| 2431 | nfa_state_T *start; |
| 2432 | Ptrlist *out; |
| 2433 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2434 | Frag_T n; |
| 2435 | |
| 2436 | n.start = start; |
| 2437 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2438 | return n; |
| 2439 | } |
| 2440 | |
| 2441 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2442 | * Create singleton list containing just outp. |
| 2443 | */ |
| 2444 | static Ptrlist * |
| 2445 | list1(outp) |
| 2446 | nfa_state_T **outp; |
| 2447 | { |
| 2448 | Ptrlist *l; |
| 2449 | |
| 2450 | l = (Ptrlist *)outp; |
| 2451 | l->next = NULL; |
| 2452 | return l; |
| 2453 | } |
| 2454 | |
| 2455 | /* |
| 2456 | * Patch the list of states at out to point to start. |
| 2457 | */ |
| 2458 | static void |
| 2459 | patch(l, s) |
| 2460 | Ptrlist *l; |
| 2461 | nfa_state_T *s; |
| 2462 | { |
| 2463 | Ptrlist *next; |
| 2464 | |
| 2465 | for (; l; l = next) |
| 2466 | { |
| 2467 | next = l->next; |
| 2468 | l->s = s; |
| 2469 | } |
| 2470 | } |
| 2471 | |
| 2472 | |
| 2473 | /* |
| 2474 | * Join the two lists l1 and l2, returning the combination. |
| 2475 | */ |
| 2476 | static Ptrlist * |
| 2477 | append(l1, l2) |
| 2478 | Ptrlist *l1; |
| 2479 | Ptrlist *l2; |
| 2480 | { |
| 2481 | Ptrlist *oldl1; |
| 2482 | |
| 2483 | oldl1 = l1; |
| 2484 | while (l1->next) |
| 2485 | l1 = l1->next; |
| 2486 | l1->next = l2; |
| 2487 | return oldl1; |
| 2488 | } |
| 2489 | |
| 2490 | /* |
| 2491 | * Stack used for transforming postfix form into NFA. |
| 2492 | */ |
| 2493 | static Frag_T empty; |
| 2494 | |
| 2495 | static void |
| 2496 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2497 | int *postfix UNUSED; |
| 2498 | int *end UNUSED; |
| 2499 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2500 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2501 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2502 | FILE *df; |
| 2503 | int *p2; |
| 2504 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2505 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2506 | if (df) |
| 2507 | { |
| 2508 | fprintf(df, "Error popping the stack!\n"); |
| 2509 | #ifdef DEBUG |
| 2510 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2511 | #endif |
| 2512 | fprintf(df, "Postfix form is: "); |
| 2513 | #ifdef DEBUG |
| 2514 | for (p2 = postfix; p2 < end; p2++) |
| 2515 | { |
| 2516 | nfa_set_code(*p2); |
| 2517 | fprintf(df, "%s, ", code); |
| 2518 | } |
| 2519 | nfa_set_code(*p); |
| 2520 | fprintf(df, "\nCurrent position is: "); |
| 2521 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2522 | { |
| 2523 | nfa_set_code(*p2); |
| 2524 | fprintf(df, "%s, ", code); |
| 2525 | } |
| 2526 | #else |
| 2527 | for (p2 = postfix; p2 < end; p2++) |
| 2528 | { |
| 2529 | fprintf(df, "%d, ", *p2); |
| 2530 | } |
| 2531 | fprintf(df, "\nCurrent position is: "); |
| 2532 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2533 | { |
| 2534 | fprintf(df, "%d, ", *p2); |
| 2535 | } |
| 2536 | #endif |
| 2537 | fprintf(df, "\n--------------------------\n"); |
| 2538 | fclose(df); |
| 2539 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2540 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2541 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2542 | } |
| 2543 | |
| 2544 | /* |
| 2545 | * Push an item onto the stack. |
| 2546 | */ |
| 2547 | static void |
| 2548 | st_push(s, p, stack_end) |
| 2549 | Frag_T s; |
| 2550 | Frag_T **p; |
| 2551 | Frag_T *stack_end; |
| 2552 | { |
| 2553 | Frag_T *stackp = *p; |
| 2554 | |
| 2555 | if (stackp >= stack_end) |
| 2556 | return; |
| 2557 | *stackp = s; |
| 2558 | *p = *p + 1; |
| 2559 | } |
| 2560 | |
| 2561 | /* |
| 2562 | * Pop an item from the stack. |
| 2563 | */ |
| 2564 | static Frag_T |
| 2565 | st_pop(p, stack) |
| 2566 | Frag_T **p; |
| 2567 | Frag_T *stack; |
| 2568 | { |
| 2569 | Frag_T *stackp; |
| 2570 | |
| 2571 | *p = *p - 1; |
| 2572 | stackp = *p; |
| 2573 | if (stackp < stack) |
| 2574 | return empty; |
| 2575 | return **p; |
| 2576 | } |
| 2577 | |
| 2578 | /* |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2579 | * Estimate the maximum byte length of anything matching "state". |
| 2580 | * When unknown or unlimited return -1. |
| 2581 | */ |
| 2582 | static int |
| 2583 | nfa_max_width(startstate, depth) |
| 2584 | nfa_state_T *startstate; |
| 2585 | int depth; |
| 2586 | { |
| 2587 | int l, r; |
| 2588 | nfa_state_T *state = startstate; |
| 2589 | int len = 0; |
| 2590 | |
| 2591 | /* detect looping in a NFA_SPLIT */ |
| 2592 | if (depth > 4) |
| 2593 | return -1; |
| 2594 | |
| 2595 | for (;;) |
| 2596 | { |
| 2597 | switch (state->c) |
| 2598 | { |
| 2599 | case NFA_END_INVISIBLE: |
| 2600 | case NFA_END_INVISIBLE_NEG: |
| 2601 | /* the end, return what we have */ |
| 2602 | return len; |
| 2603 | |
| 2604 | case NFA_SPLIT: |
| 2605 | /* two alternatives, use the maximum */ |
| 2606 | l = nfa_max_width(state->out, depth + 1); |
| 2607 | r = nfa_max_width(state->out1, depth + 1); |
| 2608 | if (l < 0 || r < 0) |
| 2609 | return -1; |
| 2610 | return len + (l > r ? l : r); |
| 2611 | |
| 2612 | case NFA_ANY: |
| 2613 | case NFA_START_COLL: |
| 2614 | case NFA_START_NEG_COLL: |
| 2615 | /* matches some character, including composing chars */ |
| 2616 | #ifdef FEAT_MBYTE |
| 2617 | if (enc_utf8) |
| 2618 | len += MB_MAXBYTES; |
| 2619 | else if (has_mbyte) |
| 2620 | len += 2; |
| 2621 | else |
| 2622 | #endif |
| 2623 | ++len; |
| 2624 | if (state->c != NFA_ANY) |
| 2625 | { |
| 2626 | /* skip over the characters */ |
| 2627 | state = state->out1->out; |
| 2628 | continue; |
| 2629 | } |
| 2630 | break; |
| 2631 | |
| 2632 | case NFA_DIGIT: |
| 2633 | case NFA_WHITE: |
| 2634 | case NFA_HEX: |
| 2635 | case NFA_OCTAL: |
| 2636 | /* ascii */ |
| 2637 | ++len; |
| 2638 | break; |
| 2639 | |
| 2640 | case NFA_IDENT: |
| 2641 | case NFA_SIDENT: |
| 2642 | case NFA_KWORD: |
| 2643 | case NFA_SKWORD: |
| 2644 | case NFA_FNAME: |
| 2645 | case NFA_SFNAME: |
| 2646 | case NFA_PRINT: |
| 2647 | case NFA_SPRINT: |
| 2648 | case NFA_NWHITE: |
| 2649 | case NFA_NDIGIT: |
| 2650 | case NFA_NHEX: |
| 2651 | case NFA_NOCTAL: |
| 2652 | case NFA_WORD: |
| 2653 | case NFA_NWORD: |
| 2654 | case NFA_HEAD: |
| 2655 | case NFA_NHEAD: |
| 2656 | case NFA_ALPHA: |
| 2657 | case NFA_NALPHA: |
| 2658 | case NFA_LOWER: |
| 2659 | case NFA_NLOWER: |
| 2660 | case NFA_UPPER: |
| 2661 | case NFA_NUPPER: |
| 2662 | /* possibly non-ascii */ |
| 2663 | #ifdef FEAT_MBYTE |
| 2664 | if (has_mbyte) |
| 2665 | len += 3; |
| 2666 | else |
| 2667 | #endif |
| 2668 | ++len; |
| 2669 | break; |
| 2670 | |
| 2671 | case NFA_START_INVISIBLE: |
| 2672 | case NFA_START_INVISIBLE_NEG: |
| 2673 | case NFA_START_INVISIBLE_BEFORE: |
| 2674 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2675 | /* zero-width, out1 points to the END state */ |
| 2676 | state = state->out1->out; |
| 2677 | continue; |
| 2678 | |
| 2679 | case NFA_BACKREF1: |
| 2680 | case NFA_BACKREF2: |
| 2681 | case NFA_BACKREF3: |
| 2682 | case NFA_BACKREF4: |
| 2683 | case NFA_BACKREF5: |
| 2684 | case NFA_BACKREF6: |
| 2685 | case NFA_BACKREF7: |
| 2686 | case NFA_BACKREF8: |
| 2687 | case NFA_BACKREF9: |
| 2688 | #ifdef FEAT_SYN_HL |
| 2689 | case NFA_ZREF1: |
| 2690 | case NFA_ZREF2: |
| 2691 | case NFA_ZREF3: |
| 2692 | case NFA_ZREF4: |
| 2693 | case NFA_ZREF5: |
| 2694 | case NFA_ZREF6: |
| 2695 | case NFA_ZREF7: |
| 2696 | case NFA_ZREF8: |
| 2697 | case NFA_ZREF9: |
| 2698 | #endif |
| 2699 | case NFA_NEWL: |
| 2700 | case NFA_SKIP: |
| 2701 | /* unknown width */ |
| 2702 | return -1; |
| 2703 | |
| 2704 | case NFA_BOL: |
| 2705 | case NFA_EOL: |
| 2706 | case NFA_BOF: |
| 2707 | case NFA_EOF: |
| 2708 | case NFA_BOW: |
| 2709 | case NFA_EOW: |
| 2710 | case NFA_MOPEN: |
| 2711 | case NFA_MOPEN1: |
| 2712 | case NFA_MOPEN2: |
| 2713 | case NFA_MOPEN3: |
| 2714 | case NFA_MOPEN4: |
| 2715 | case NFA_MOPEN5: |
| 2716 | case NFA_MOPEN6: |
| 2717 | case NFA_MOPEN7: |
| 2718 | case NFA_MOPEN8: |
| 2719 | case NFA_MOPEN9: |
| 2720 | #ifdef FEAT_SYN_HL |
| 2721 | case NFA_ZOPEN: |
| 2722 | case NFA_ZOPEN1: |
| 2723 | case NFA_ZOPEN2: |
| 2724 | case NFA_ZOPEN3: |
| 2725 | case NFA_ZOPEN4: |
| 2726 | case NFA_ZOPEN5: |
| 2727 | case NFA_ZOPEN6: |
| 2728 | case NFA_ZOPEN7: |
| 2729 | case NFA_ZOPEN8: |
| 2730 | case NFA_ZOPEN9: |
| 2731 | case NFA_ZCLOSE: |
| 2732 | case NFA_ZCLOSE1: |
| 2733 | case NFA_ZCLOSE2: |
| 2734 | case NFA_ZCLOSE3: |
| 2735 | case NFA_ZCLOSE4: |
| 2736 | case NFA_ZCLOSE5: |
| 2737 | case NFA_ZCLOSE6: |
| 2738 | case NFA_ZCLOSE7: |
| 2739 | case NFA_ZCLOSE8: |
| 2740 | case NFA_ZCLOSE9: |
| 2741 | #endif |
| 2742 | case NFA_MCLOSE: |
| 2743 | case NFA_MCLOSE1: |
| 2744 | case NFA_MCLOSE2: |
| 2745 | case NFA_MCLOSE3: |
| 2746 | case NFA_MCLOSE4: |
| 2747 | case NFA_MCLOSE5: |
| 2748 | case NFA_MCLOSE6: |
| 2749 | case NFA_MCLOSE7: |
| 2750 | case NFA_MCLOSE8: |
| 2751 | case NFA_MCLOSE9: |
| 2752 | case NFA_NOPEN: |
| 2753 | case NFA_NCLOSE: |
| 2754 | |
| 2755 | case NFA_LNUM_GT: |
| 2756 | case NFA_LNUM_LT: |
| 2757 | case NFA_COL_GT: |
| 2758 | case NFA_COL_LT: |
| 2759 | case NFA_VCOL_GT: |
| 2760 | case NFA_VCOL_LT: |
| 2761 | case NFA_MARK_GT: |
| 2762 | case NFA_MARK_LT: |
| 2763 | case NFA_VISUAL: |
| 2764 | case NFA_LNUM: |
| 2765 | case NFA_CURSOR: |
| 2766 | case NFA_COL: |
| 2767 | case NFA_VCOL: |
| 2768 | case NFA_MARK: |
| 2769 | |
| 2770 | case NFA_ZSTART: |
| 2771 | case NFA_ZEND: |
| 2772 | case NFA_OPT_CHARS: |
| 2773 | case NFA_SKIP_CHAR: |
| 2774 | case NFA_START_PATTERN: |
| 2775 | case NFA_END_PATTERN: |
| 2776 | case NFA_COMPOSING: |
| 2777 | case NFA_END_COMPOSING: |
| 2778 | /* zero-width */ |
| 2779 | break; |
| 2780 | |
| 2781 | default: |
| 2782 | if (state->c < 0) |
| 2783 | /* don't know what this is */ |
| 2784 | return -1; |
| 2785 | /* normal character */ |
| 2786 | len += MB_CHAR2LEN(state->c); |
| 2787 | break; |
| 2788 | } |
| 2789 | |
| 2790 | /* normal way to continue */ |
| 2791 | state = state->out; |
| 2792 | } |
| 2793 | |
| 2794 | /* unrecognized */ |
| 2795 | return -1; |
| 2796 | } |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame^] | 2797 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2798 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2799 | * Convert a postfix form into its equivalent NFA. |
| 2800 | * Return the NFA start state on success, NULL otherwise. |
| 2801 | */ |
| 2802 | static nfa_state_T * |
| 2803 | post2nfa(postfix, end, nfa_calc_size) |
| 2804 | int *postfix; |
| 2805 | int *end; |
| 2806 | int nfa_calc_size; |
| 2807 | { |
| 2808 | int *p; |
| 2809 | int mopen; |
| 2810 | int mclose; |
| 2811 | Frag_T *stack = NULL; |
| 2812 | Frag_T *stackp = NULL; |
| 2813 | Frag_T *stack_end = NULL; |
| 2814 | Frag_T e1; |
| 2815 | Frag_T e2; |
| 2816 | Frag_T e; |
| 2817 | nfa_state_T *s; |
| 2818 | nfa_state_T *s1; |
| 2819 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2820 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2821 | |
| 2822 | if (postfix == NULL) |
| 2823 | return NULL; |
| 2824 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2825 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2826 | #define POP() st_pop(&stackp, stack); \ |
| 2827 | if (stackp < stack) \ |
| 2828 | { \ |
| 2829 | st_error(postfix, end, p); \ |
| 2830 | return NULL; \ |
| 2831 | } |
| 2832 | |
| 2833 | if (nfa_calc_size == FALSE) |
| 2834 | { |
| 2835 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2836 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2837 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2838 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | for (p = postfix; p < end; ++p) |
| 2842 | { |
| 2843 | switch (*p) |
| 2844 | { |
| 2845 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2846 | /* Concatenation. |
| 2847 | * Pay attention: this operator does not exist in the r.e. itself |
| 2848 | * (it is implicit, really). It is added when r.e. is translated |
| 2849 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2850 | if (nfa_calc_size == TRUE) |
| 2851 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2852 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2853 | break; |
| 2854 | } |
| 2855 | e2 = POP(); |
| 2856 | e1 = POP(); |
| 2857 | patch(e1.out, e2.start); |
| 2858 | PUSH(frag(e1.start, e2.out)); |
| 2859 | break; |
| 2860 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2861 | case NFA_OR: |
| 2862 | /* Alternation */ |
| 2863 | if (nfa_calc_size == TRUE) |
| 2864 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2865 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2866 | break; |
| 2867 | } |
| 2868 | e2 = POP(); |
| 2869 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2870 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2871 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2872 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2873 | PUSH(frag(s, append(e1.out, e2.out))); |
| 2874 | break; |
| 2875 | |
| 2876 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2877 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2878 | if (nfa_calc_size == TRUE) |
| 2879 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2880 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2881 | break; |
| 2882 | } |
| 2883 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2884 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2885 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2886 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2887 | patch(e.out, s); |
| 2888 | PUSH(frag(s, list1(&s->out1))); |
| 2889 | break; |
| 2890 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2891 | case NFA_STAR_NONGREEDY: |
| 2892 | /* Zero or more, prefer zero */ |
| 2893 | if (nfa_calc_size == TRUE) |
| 2894 | { |
| 2895 | nstate++; |
| 2896 | break; |
| 2897 | } |
| 2898 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2899 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2900 | if (s == NULL) |
| 2901 | goto theend; |
| 2902 | patch(e.out, s); |
| 2903 | PUSH(frag(s, list1(&s->out))); |
| 2904 | break; |
| 2905 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2906 | case NFA_QUEST: |
| 2907 | /* one or zero atoms=> greedy match */ |
| 2908 | if (nfa_calc_size == TRUE) |
| 2909 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2910 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2911 | break; |
| 2912 | } |
| 2913 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2914 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2915 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2916 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2917 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 2918 | break; |
| 2919 | |
| 2920 | case NFA_QUEST_NONGREEDY: |
| 2921 | /* zero or one atoms => non-greedy match */ |
| 2922 | if (nfa_calc_size == TRUE) |
| 2923 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2924 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2925 | break; |
| 2926 | } |
| 2927 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2928 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2929 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2930 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2931 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 2932 | break; |
| 2933 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2934 | case NFA_END_COLL: |
| 2935 | case NFA_END_NEG_COLL: |
| 2936 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 2937 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 2938 | * add the output to the start. */ |
| 2939 | if (nfa_calc_size == TRUE) |
| 2940 | { |
| 2941 | nstate++; |
| 2942 | break; |
| 2943 | } |
| 2944 | e = POP(); |
| 2945 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 2946 | if (s == NULL) |
| 2947 | goto theend; |
| 2948 | patch(e.out, s); |
| 2949 | e.start->out1 = s; |
| 2950 | PUSH(frag(e.start, list1(&s->out))); |
| 2951 | break; |
| 2952 | |
| 2953 | case NFA_RANGE: |
| 2954 | /* Before this are two characters, the low and high end of a |
| 2955 | * range. Turn them into two states with MIN and MAX. */ |
| 2956 | if (nfa_calc_size == TRUE) |
| 2957 | { |
| 2958 | /* nstate += 0; */ |
| 2959 | break; |
| 2960 | } |
| 2961 | e2 = POP(); |
| 2962 | e1 = POP(); |
| 2963 | e2.start->val = e2.start->c; |
| 2964 | e2.start->c = NFA_RANGE_MAX; |
| 2965 | e1.start->val = e1.start->c; |
| 2966 | e1.start->c = NFA_RANGE_MIN; |
| 2967 | patch(e1.out, e2.start); |
| 2968 | PUSH(frag(e1.start, e2.out)); |
| 2969 | break; |
| 2970 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2971 | case NFA_SKIP_CHAR: |
| 2972 | /* Symbol of 0-length, Used in a repetition |
| 2973 | * with max/min count of 0 */ |
| 2974 | if (nfa_calc_size == TRUE) |
| 2975 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2976 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2977 | break; |
| 2978 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2979 | s = alloc_state(NFA_SKIP_CHAR, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2980 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2981 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2982 | PUSH(frag(s, list1(&s->out))); |
| 2983 | break; |
| 2984 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2985 | case NFA_OPT_CHARS: |
| 2986 | { |
| 2987 | int n; |
| 2988 | |
| 2989 | /* \%[abc] */ |
| 2990 | n = *++p; /* get number of characters */ |
| 2991 | if (nfa_calc_size == TRUE) |
| 2992 | { |
| 2993 | nstate += n; |
| 2994 | break; |
| 2995 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 2996 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2997 | e1.out = NULL; /* stores list with out1's */ |
| 2998 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 2999 | while (n-- > 0) |
| 3000 | { |
| 3001 | e = POP(); /* get character */ |
| 3002 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 3003 | if (s == NULL) |
| 3004 | goto theend; |
| 3005 | if (e1.out == NULL) |
| 3006 | e1 = e; |
| 3007 | patch(e.out, s1); |
| 3008 | append(e1.out, list1(&s->out1)); |
| 3009 | s1 = s; |
| 3010 | } |
| 3011 | PUSH(frag(s, e1.out)); |
| 3012 | break; |
| 3013 | } |
| 3014 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3015 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3016 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3017 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3018 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3019 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3020 | { |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3021 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 3022 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3023 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3024 | int start_state; |
| 3025 | int end_state; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3026 | int n = 0; |
| 3027 | nfa_state_T *zend; |
| 3028 | nfa_state_T *skip; |
| 3029 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3030 | switch (*p) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3031 | { |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3032 | case NFA_PREV_ATOM_NO_WIDTH: |
| 3033 | start_state = NFA_START_INVISIBLE; |
| 3034 | end_state = NFA_END_INVISIBLE; |
| 3035 | break; |
| 3036 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 3037 | start_state = NFA_START_INVISIBLE_NEG; |
| 3038 | end_state = NFA_END_INVISIBLE_NEG; |
| 3039 | break; |
| 3040 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3041 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 3042 | end_state = NFA_END_INVISIBLE; |
| 3043 | break; |
| 3044 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 3045 | start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
| 3046 | end_state = NFA_END_INVISIBLE_NEG; |
| 3047 | break; |
| 3048 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 3049 | start_state = NFA_START_PATTERN; |
| 3050 | end_state = NFA_END_PATTERN; |
| 3051 | break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3052 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3053 | |
| 3054 | if (before) |
| 3055 | n = *++p; /* get the count */ |
| 3056 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3057 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3058 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3059 | * The \@<= operator: match for the preceding atom. |
| 3060 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3061 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3062 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3063 | |
| 3064 | if (nfa_calc_size == TRUE) |
| 3065 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3066 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3067 | break; |
| 3068 | } |
| 3069 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3070 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3071 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3072 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3073 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3074 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3075 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3076 | goto theend; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3077 | if (pattern) |
| 3078 | { |
| 3079 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 3080 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 3081 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 3082 | s1->out= skip; |
| 3083 | patch(e.out, zend); |
| 3084 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3085 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3086 | else |
| 3087 | { |
| 3088 | patch(e.out, s1); |
| 3089 | PUSH(frag(s, list1(&s1->out))); |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3090 | if (before) |
| 3091 | { |
| 3092 | if (n <= 0) |
| 3093 | /* See if we can guess the maximum width, it avoids a |
| 3094 | * lot of pointless tries. */ |
| 3095 | n = nfa_max_width(e.start, 0); |
| 3096 | s->val = n; /* store the count */ |
| 3097 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3098 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3099 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3100 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3101 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3102 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3103 | case NFA_COMPOSING: /* char with composing char */ |
| 3104 | #if 0 |
| 3105 | /* TODO */ |
| 3106 | if (regflags & RF_ICOMBINE) |
| 3107 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3108 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3109 | } |
| 3110 | #endif |
| 3111 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3112 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3113 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3114 | case NFA_MOPEN: /* \( \) Submatch */ |
| 3115 | case NFA_MOPEN1: |
| 3116 | case NFA_MOPEN2: |
| 3117 | case NFA_MOPEN3: |
| 3118 | case NFA_MOPEN4: |
| 3119 | case NFA_MOPEN5: |
| 3120 | case NFA_MOPEN6: |
| 3121 | case NFA_MOPEN7: |
| 3122 | case NFA_MOPEN8: |
| 3123 | case NFA_MOPEN9: |
| 3124 | #ifdef FEAT_SYN_HL |
| 3125 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 3126 | case NFA_ZOPEN1: |
| 3127 | case NFA_ZOPEN2: |
| 3128 | case NFA_ZOPEN3: |
| 3129 | case NFA_ZOPEN4: |
| 3130 | case NFA_ZOPEN5: |
| 3131 | case NFA_ZOPEN6: |
| 3132 | case NFA_ZOPEN7: |
| 3133 | case NFA_ZOPEN8: |
| 3134 | case NFA_ZOPEN9: |
| 3135 | #endif |
| 3136 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3137 | if (nfa_calc_size == TRUE) |
| 3138 | { |
| 3139 | nstate += 2; |
| 3140 | break; |
| 3141 | } |
| 3142 | |
| 3143 | mopen = *p; |
| 3144 | switch (*p) |
| 3145 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3146 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 3147 | #ifdef FEAT_SYN_HL |
| 3148 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 3149 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 3150 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 3151 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 3152 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 3153 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 3154 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 3155 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 3156 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 3157 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 3158 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3159 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3160 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3161 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3162 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3163 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3164 | mclose = *p + NSUBEXP; |
| 3165 | break; |
| 3166 | } |
| 3167 | |
| 3168 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 3169 | * the empty regexp "". In this case, the NFA will be |
| 3170 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 3171 | * empty groups of parenthesis, and empty mbyte chars */ |
| 3172 | if (stackp == stack) |
| 3173 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3174 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3175 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3176 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3177 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3178 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3179 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3180 | patch(list1(&s->out), s1); |
| 3181 | PUSH(frag(s, list1(&s1->out))); |
| 3182 | break; |
| 3183 | } |
| 3184 | |
| 3185 | /* At least one node was emitted before NFA_MOPEN, so |
| 3186 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 3187 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3188 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3189 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3190 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3191 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3192 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3193 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3194 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3195 | patch(e.out, s1); |
| 3196 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3197 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3198 | if (mopen == NFA_COMPOSING) |
| 3199 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3200 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3201 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3202 | |
| 3203 | PUSH(frag(s, list1(&s1->out))); |
| 3204 | break; |
| 3205 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3206 | case NFA_BACKREF1: |
| 3207 | case NFA_BACKREF2: |
| 3208 | case NFA_BACKREF3: |
| 3209 | case NFA_BACKREF4: |
| 3210 | case NFA_BACKREF5: |
| 3211 | case NFA_BACKREF6: |
| 3212 | case NFA_BACKREF7: |
| 3213 | case NFA_BACKREF8: |
| 3214 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3215 | #ifdef FEAT_SYN_HL |
| 3216 | case NFA_ZREF1: |
| 3217 | case NFA_ZREF2: |
| 3218 | case NFA_ZREF3: |
| 3219 | case NFA_ZREF4: |
| 3220 | case NFA_ZREF5: |
| 3221 | case NFA_ZREF6: |
| 3222 | case NFA_ZREF7: |
| 3223 | case NFA_ZREF8: |
| 3224 | case NFA_ZREF9: |
| 3225 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3226 | if (nfa_calc_size == TRUE) |
| 3227 | { |
| 3228 | nstate += 2; |
| 3229 | break; |
| 3230 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3231 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3232 | if (s == NULL) |
| 3233 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3234 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3235 | if (s1 == NULL) |
| 3236 | goto theend; |
| 3237 | patch(list1(&s->out), s1); |
| 3238 | PUSH(frag(s, list1(&s1->out))); |
| 3239 | break; |
| 3240 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3241 | case NFA_LNUM: |
| 3242 | case NFA_LNUM_GT: |
| 3243 | case NFA_LNUM_LT: |
| 3244 | case NFA_VCOL: |
| 3245 | case NFA_VCOL_GT: |
| 3246 | case NFA_VCOL_LT: |
| 3247 | case NFA_COL: |
| 3248 | case NFA_COL_GT: |
| 3249 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 3250 | case NFA_MARK: |
| 3251 | case NFA_MARK_GT: |
| 3252 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3253 | { |
| 3254 | int n = *++p; /* lnum, col or mark name */ |
| 3255 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3256 | if (nfa_calc_size == TRUE) |
| 3257 | { |
| 3258 | nstate += 1; |
| 3259 | break; |
| 3260 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3261 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3262 | if (s == NULL) |
| 3263 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3264 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3265 | PUSH(frag(s, list1(&s->out))); |
| 3266 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3267 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3268 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3269 | case NFA_ZSTART: |
| 3270 | case NFA_ZEND: |
| 3271 | default: |
| 3272 | /* Operands */ |
| 3273 | if (nfa_calc_size == TRUE) |
| 3274 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3275 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3276 | break; |
| 3277 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3278 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3279 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3280 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3281 | PUSH(frag(s, list1(&s->out))); |
| 3282 | break; |
| 3283 | |
| 3284 | } /* switch(*p) */ |
| 3285 | |
| 3286 | } /* for(p = postfix; *p; ++p) */ |
| 3287 | |
| 3288 | if (nfa_calc_size == TRUE) |
| 3289 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3290 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3291 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | e = POP(); |
| 3295 | if (stackp != stack) |
| 3296 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 3297 | |
| 3298 | if (istate >= nstate) |
| 3299 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 3300 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3301 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3302 | matchstate->c = NFA_MATCH; |
| 3303 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3304 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3305 | |
| 3306 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3307 | ret = e.start; |
| 3308 | |
| 3309 | theend: |
| 3310 | vim_free(stack); |
| 3311 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3312 | |
| 3313 | #undef POP1 |
| 3314 | #undef PUSH1 |
| 3315 | #undef POP2 |
| 3316 | #undef PUSH2 |
| 3317 | #undef POP |
| 3318 | #undef PUSH |
| 3319 | } |
| 3320 | |
| 3321 | /**************************************************************** |
| 3322 | * NFA execution code. |
| 3323 | ****************************************************************/ |
| 3324 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3325 | typedef struct |
| 3326 | { |
| 3327 | int in_use; /* number of subexpr with useful info */ |
| 3328 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3329 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3330 | union |
| 3331 | { |
| 3332 | struct multipos |
| 3333 | { |
| 3334 | lpos_T start; |
| 3335 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3336 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3337 | struct linepos |
| 3338 | { |
| 3339 | char_u *start; |
| 3340 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3341 | } line[NSUBEXP]; |
| 3342 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3343 | } regsub_T; |
| 3344 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3345 | typedef struct |
| 3346 | { |
| 3347 | regsub_T norm; /* \( .. \) matches */ |
| 3348 | #ifdef FEAT_SYN_HL |
| 3349 | regsub_T synt; /* \z( .. \) matches */ |
| 3350 | #endif |
| 3351 | } regsubs_T; |
| 3352 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3353 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3354 | typedef struct nfa_pim_S nfa_pim_T; |
| 3355 | struct nfa_pim_S |
| 3356 | { |
| 3357 | nfa_state_T *state; |
| 3358 | int result; /* NFA_PIM_TODO, NFA_PIM_[NO]MATCH */ |
| 3359 | nfa_pim_T *pim; /* another PIM at the same position */ |
| 3360 | regsubs_T subs; /* submatch info, only party used */ |
| 3361 | }; |
| 3362 | |
| 3363 | /* Values for done in nfa_pim_T. */ |
| 3364 | #define NFA_PIM_TODO 0 |
| 3365 | #define NFA_PIM_MATCH 1 |
| 3366 | #define NFA_PIM_NOMATCH -1 |
| 3367 | |
| 3368 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3369 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3370 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3371 | { |
| 3372 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3373 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3374 | nfa_pim_T *pim; /* if not NULL: postponed invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3375 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3376 | } nfa_thread_T; |
| 3377 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3378 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3379 | typedef struct |
| 3380 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3381 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3382 | int n; /* nr of states currently in "t" */ |
| 3383 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3384 | int id; /* ID of the list */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3385 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3386 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3387 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3388 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 3389 | static void log_subexpr __ARGS((regsub_T *sub)); |
| 3390 | |
| 3391 | static void |
| 3392 | log_subsexpr(subs) |
| 3393 | regsubs_T *subs; |
| 3394 | { |
| 3395 | log_subexpr(&subs->norm); |
| 3396 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3397 | if (nfa_has_zsubexpr) |
| 3398 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3399 | # endif |
| 3400 | } |
| 3401 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3402 | static void |
| 3403 | log_subexpr(sub) |
| 3404 | regsub_T *sub; |
| 3405 | { |
| 3406 | int j; |
| 3407 | |
| 3408 | for (j = 0; j < sub->in_use; j++) |
| 3409 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3410 | fprintf(log_fd, "*** group %d, start: c=%d, l=%d, end: c=%d, l=%d\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3411 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3412 | sub->list.multi[j].start.col, |
| 3413 | (int)sub->list.multi[j].start.lnum, |
| 3414 | sub->list.multi[j].end.col, |
| 3415 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3416 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3417 | { |
| 3418 | char *s = (char *)sub->list.line[j].start; |
| 3419 | char *e = (char *)sub->list.line[j].end; |
| 3420 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3421 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3422 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3423 | s == NULL ? "NULL" : s, |
| 3424 | e == NULL ? "NULL" : e); |
| 3425 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3426 | } |
| 3427 | #endif |
| 3428 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3429 | /* Used during execution: whether a match has been found. */ |
| 3430 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3431 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3432 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3433 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3434 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3435 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3436 | static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs)); |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame^] | 3437 | static int match_follows __ARGS((nfa_state_T *startstate, int depth)); |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3438 | static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3439 | static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, int off)); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3440 | static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3441 | |
| 3442 | static void |
| 3443 | clear_sub(sub) |
| 3444 | regsub_T *sub; |
| 3445 | { |
| 3446 | if (REG_MULTI) |
| 3447 | /* Use 0xff to set lnum to -1 */ |
| 3448 | vim_memset(sub->list.multi, 0xff, |
| 3449 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3450 | else |
| 3451 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3452 | sub->in_use = 0; |
| 3453 | } |
| 3454 | |
| 3455 | /* |
| 3456 | * Copy the submatches from "from" to "to". |
| 3457 | */ |
| 3458 | static void |
| 3459 | copy_sub(to, from) |
| 3460 | regsub_T *to; |
| 3461 | regsub_T *from; |
| 3462 | { |
| 3463 | to->in_use = from->in_use; |
| 3464 | if (from->in_use > 0) |
| 3465 | { |
| 3466 | /* Copy the match start and end positions. */ |
| 3467 | if (REG_MULTI) |
| 3468 | mch_memmove(&to->list.multi[0], |
| 3469 | &from->list.multi[0], |
| 3470 | sizeof(struct multipos) * from->in_use); |
| 3471 | else |
| 3472 | mch_memmove(&to->list.line[0], |
| 3473 | &from->list.line[0], |
| 3474 | sizeof(struct linepos) * from->in_use); |
| 3475 | } |
| 3476 | } |
| 3477 | |
| 3478 | /* |
| 3479 | * Like copy_sub() but exclude the main match. |
| 3480 | */ |
| 3481 | static void |
| 3482 | copy_sub_off(to, from) |
| 3483 | regsub_T *to; |
| 3484 | regsub_T *from; |
| 3485 | { |
| 3486 | if (to->in_use < from->in_use) |
| 3487 | to->in_use = from->in_use; |
| 3488 | if (from->in_use > 1) |
| 3489 | { |
| 3490 | /* Copy the match start and end positions. */ |
| 3491 | if (REG_MULTI) |
| 3492 | mch_memmove(&to->list.multi[1], |
| 3493 | &from->list.multi[1], |
| 3494 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3495 | else |
| 3496 | mch_memmove(&to->list.line[1], |
| 3497 | &from->list.line[1], |
| 3498 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3499 | } |
| 3500 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3501 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3502 | /* |
| 3503 | * Return TRUE if "sub1" and "sub2" have the same positions. |
| 3504 | */ |
| 3505 | static int |
| 3506 | sub_equal(sub1, sub2) |
| 3507 | regsub_T *sub1; |
| 3508 | regsub_T *sub2; |
| 3509 | { |
| 3510 | int i; |
| 3511 | int todo; |
| 3512 | linenr_T s1, e1; |
| 3513 | linenr_T s2, e2; |
| 3514 | char_u *sp1, *ep1; |
| 3515 | char_u *sp2, *ep2; |
| 3516 | |
| 3517 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3518 | if (REG_MULTI) |
| 3519 | { |
| 3520 | for (i = 0; i < todo; ++i) |
| 3521 | { |
| 3522 | if (i < sub1->in_use) |
| 3523 | { |
| 3524 | s1 = sub1->list.multi[i].start.lnum; |
| 3525 | e1 = sub1->list.multi[i].end.lnum; |
| 3526 | } |
| 3527 | else |
| 3528 | { |
| 3529 | s1 = 0; |
| 3530 | e1 = 0; |
| 3531 | } |
| 3532 | if (i < sub2->in_use) |
| 3533 | { |
| 3534 | s2 = sub2->list.multi[i].start.lnum; |
| 3535 | e2 = sub2->list.multi[i].end.lnum; |
| 3536 | } |
| 3537 | else |
| 3538 | { |
| 3539 | s2 = 0; |
| 3540 | e2 = 0; |
| 3541 | } |
| 3542 | if (s1 != s2 || e1 != e2) |
| 3543 | return FALSE; |
| 3544 | if (s1 != 0 && sub1->list.multi[i].start.col |
| 3545 | != sub2->list.multi[i].start.col) |
| 3546 | return FALSE; |
| 3547 | if (e1 != 0 && sub1->list.multi[i].end.col |
| 3548 | != sub2->list.multi[i].end.col) |
| 3549 | return FALSE; |
| 3550 | } |
| 3551 | } |
| 3552 | else |
| 3553 | { |
| 3554 | for (i = 0; i < todo; ++i) |
| 3555 | { |
| 3556 | if (i < sub1->in_use) |
| 3557 | { |
| 3558 | sp1 = sub1->list.line[i].start; |
| 3559 | ep1 = sub1->list.line[i].end; |
| 3560 | } |
| 3561 | else |
| 3562 | { |
| 3563 | sp1 = NULL; |
| 3564 | ep1 = NULL; |
| 3565 | } |
| 3566 | if (i < sub2->in_use) |
| 3567 | { |
| 3568 | sp2 = sub2->list.line[i].start; |
| 3569 | ep2 = sub2->list.line[i].end; |
| 3570 | } |
| 3571 | else |
| 3572 | { |
| 3573 | sp2 = NULL; |
| 3574 | ep2 = NULL; |
| 3575 | } |
| 3576 | if (sp1 != sp2 || ep1 != ep2) |
| 3577 | return FALSE; |
| 3578 | } |
| 3579 | } |
| 3580 | |
| 3581 | return TRUE; |
| 3582 | } |
| 3583 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3584 | #ifdef ENABLE_LOG |
| 3585 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3586 | report_state(char *action, regsub_T *sub, nfa_state_T *state, int lid) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3587 | { |
| 3588 | int col; |
| 3589 | |
| 3590 | if (sub->in_use <= 0) |
| 3591 | col = -1; |
| 3592 | else if (REG_MULTI) |
| 3593 | col = sub->list.multi[0].start.col; |
| 3594 | else |
| 3595 | col = (int)(sub->list.line[0].start - regline); |
| 3596 | nfa_set_code(state->c); |
| 3597 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)\n", |
| 3598 | action, abs(state->id), lid, state->c, code, col); |
| 3599 | } |
| 3600 | #endif |
| 3601 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3602 | /* |
| 3603 | * Return TRUE if the same state is already in list "l" with the same |
| 3604 | * positions as "subs". |
| 3605 | */ |
| 3606 | static int |
| 3607 | has_state_with_pos(l, state, subs) |
| 3608 | nfa_list_T *l; /* runtime state list */ |
| 3609 | nfa_state_T *state; /* state to update */ |
| 3610 | regsubs_T *subs; /* pointers to subexpressions */ |
| 3611 | { |
| 3612 | nfa_thread_T *thread; |
| 3613 | int i; |
| 3614 | |
| 3615 | for (i = 0; i < l->n; ++i) |
| 3616 | { |
| 3617 | thread = &l->t[i]; |
| 3618 | if (thread->state->id == state->id |
| 3619 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 3620 | #ifdef FEAT_SYN_HL |
| 3621 | && (!nfa_has_zsubexpr || |
| 3622 | sub_equal(&thread->subs.synt, &subs->synt)) |
| 3623 | #endif |
| 3624 | ) |
| 3625 | return TRUE; |
| 3626 | } |
| 3627 | return FALSE; |
| 3628 | } |
| 3629 | |
| 3630 | /* |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame^] | 3631 | * Return TRUE if "state" leads to a NFA_MATCH without advancing the input. |
| 3632 | */ |
| 3633 | static int |
| 3634 | match_follows(startstate, depth) |
| 3635 | nfa_state_T *startstate; |
| 3636 | int depth; |
| 3637 | { |
| 3638 | nfa_state_T *state = startstate; |
| 3639 | |
| 3640 | /* avoid too much recursion */ |
| 3641 | if (depth > 10) |
| 3642 | return FALSE; |
| 3643 | |
| 3644 | for (;;) |
| 3645 | { |
| 3646 | switch (state->c) |
| 3647 | { |
| 3648 | case NFA_MATCH: |
| 3649 | return TRUE; |
| 3650 | |
| 3651 | case NFA_SPLIT: |
| 3652 | return match_follows(state->out, depth + 1) |
| 3653 | || match_follows(state->out1, depth + 1); |
| 3654 | |
| 3655 | case NFA_START_INVISIBLE: |
| 3656 | case NFA_START_INVISIBLE_BEFORE: |
| 3657 | case NFA_START_INVISIBLE_NEG: |
| 3658 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 3659 | case NFA_COMPOSING: |
| 3660 | /* skip ahead to next state */ |
| 3661 | state = state->out1->out; |
| 3662 | break; |
| 3663 | |
| 3664 | case NFA_ANY: |
| 3665 | case NFA_IDENT: |
| 3666 | case NFA_SIDENT: |
| 3667 | case NFA_KWORD: |
| 3668 | case NFA_SKWORD: |
| 3669 | case NFA_FNAME: |
| 3670 | case NFA_SFNAME: |
| 3671 | case NFA_PRINT: |
| 3672 | case NFA_SPRINT: |
| 3673 | case NFA_WHITE: |
| 3674 | case NFA_NWHITE: |
| 3675 | case NFA_DIGIT: |
| 3676 | case NFA_NDIGIT: |
| 3677 | case NFA_HEX: |
| 3678 | case NFA_NHEX: |
| 3679 | case NFA_OCTAL: |
| 3680 | case NFA_NOCTAL: |
| 3681 | case NFA_WORD: |
| 3682 | case NFA_NWORD: |
| 3683 | case NFA_HEAD: |
| 3684 | case NFA_NHEAD: |
| 3685 | case NFA_ALPHA: |
| 3686 | case NFA_NALPHA: |
| 3687 | case NFA_LOWER: |
| 3688 | case NFA_NLOWER: |
| 3689 | case NFA_UPPER: |
| 3690 | case NFA_NUPPER: |
| 3691 | case NFA_START_COLL: |
| 3692 | case NFA_START_NEG_COLL: |
| 3693 | case NFA_NEWL: |
| 3694 | /* state will advance input */ |
| 3695 | return FALSE; |
| 3696 | |
| 3697 | default: |
| 3698 | if (state->c > 0) |
| 3699 | /* state will advance input */ |
| 3700 | return FALSE; |
| 3701 | |
| 3702 | /* Others: zero-width or possibly zero-width, might still find |
| 3703 | * a match at the same position, keep looking. */ |
| 3704 | break; |
| 3705 | } |
| 3706 | state = state->out; |
| 3707 | } |
| 3708 | return FALSE; |
| 3709 | } |
| 3710 | |
| 3711 | |
| 3712 | /* |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3713 | * Return TRUE if "state" is already in list "l". |
| 3714 | */ |
| 3715 | static int |
| 3716 | state_in_list(l, state, subs) |
| 3717 | nfa_list_T *l; /* runtime state list */ |
| 3718 | nfa_state_T *state; /* state to update */ |
| 3719 | regsubs_T *subs; /* pointers to subexpressions */ |
| 3720 | { |
| 3721 | if (state->lastlist[nfa_ll_index] == l->id) |
| 3722 | { |
| 3723 | if (!nfa_has_backref || has_state_with_pos(l, state, subs)) |
| 3724 | return TRUE; |
| 3725 | } |
| 3726 | return FALSE; |
| 3727 | } |
| 3728 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3729 | static void |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3730 | addstate(l, state, subs, off) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3731 | nfa_list_T *l; /* runtime state list */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3732 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3733 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3734 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3735 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3736 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3737 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3738 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3739 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3740 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3741 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3742 | regsub_T *sub; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3743 | #ifdef ENABLE_LOG |
| 3744 | int did_print = FALSE; |
| 3745 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3746 | |
| 3747 | if (l == NULL || state == NULL) |
| 3748 | return; |
| 3749 | |
| 3750 | switch (state->c) |
| 3751 | { |
| 3752 | case NFA_SPLIT: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3753 | case NFA_NOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3754 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3755 | case NFA_NCLOSE: |
| 3756 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3757 | case NFA_MCLOSE1: |
| 3758 | case NFA_MCLOSE2: |
| 3759 | case NFA_MCLOSE3: |
| 3760 | case NFA_MCLOSE4: |
| 3761 | case NFA_MCLOSE5: |
| 3762 | case NFA_MCLOSE6: |
| 3763 | case NFA_MCLOSE7: |
| 3764 | case NFA_MCLOSE8: |
| 3765 | case NFA_MCLOSE9: |
| 3766 | #ifdef FEAT_SYN_HL |
| 3767 | case NFA_ZCLOSE: |
| 3768 | case NFA_ZCLOSE1: |
| 3769 | case NFA_ZCLOSE2: |
| 3770 | case NFA_ZCLOSE3: |
| 3771 | case NFA_ZCLOSE4: |
| 3772 | case NFA_ZCLOSE5: |
| 3773 | case NFA_ZCLOSE6: |
| 3774 | case NFA_ZCLOSE7: |
| 3775 | case NFA_ZCLOSE8: |
| 3776 | case NFA_ZCLOSE9: |
| 3777 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3778 | case NFA_ZEND: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3779 | /* These nodes are not added themselves but their "out" and/or |
| 3780 | * "out1" may be added below. */ |
| 3781 | break; |
| 3782 | |
| 3783 | case NFA_MOPEN: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3784 | case NFA_MOPEN1: |
| 3785 | case NFA_MOPEN2: |
| 3786 | case NFA_MOPEN3: |
| 3787 | case NFA_MOPEN4: |
| 3788 | case NFA_MOPEN5: |
| 3789 | case NFA_MOPEN6: |
| 3790 | case NFA_MOPEN7: |
| 3791 | case NFA_MOPEN8: |
| 3792 | case NFA_MOPEN9: |
| 3793 | #ifdef FEAT_SYN_HL |
| 3794 | case NFA_ZOPEN: |
| 3795 | case NFA_ZOPEN1: |
| 3796 | case NFA_ZOPEN2: |
| 3797 | case NFA_ZOPEN3: |
| 3798 | case NFA_ZOPEN4: |
| 3799 | case NFA_ZOPEN5: |
| 3800 | case NFA_ZOPEN6: |
| 3801 | case NFA_ZOPEN7: |
| 3802 | case NFA_ZOPEN8: |
| 3803 | case NFA_ZOPEN9: |
| 3804 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3805 | case NFA_ZSTART: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3806 | /* These nodes do not need to be added, but we need to bail out |
| 3807 | * when it was tried to be added to this list before. */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3808 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3809 | goto skip_add; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3810 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3811 | break; |
| 3812 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3813 | case NFA_BOL: |
| 3814 | case NFA_BOF: |
| 3815 | /* "^" won't match past end-of-line, don't bother trying. |
| 3816 | * Except when we are going to the next line for a look-behind |
| 3817 | * match. */ |
| 3818 | if (reginput > regline |
| 3819 | && (nfa_endp == NULL |
| 3820 | || !REG_MULTI |
| 3821 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 3822 | goto skip_add; |
| 3823 | /* FALLTHROUGH */ |
| 3824 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3825 | default: |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3826 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3827 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3828 | /* This state is already in the list, don't add it again, |
| 3829 | * unless it is an MOPEN that is used for a backreference. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3830 | if (!nfa_has_backref) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3831 | { |
| 3832 | skip_add: |
| 3833 | #ifdef ENABLE_LOG |
| 3834 | nfa_set_code(state->c); |
| 3835 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 3836 | abs(state->id), l->id, state->c, code); |
| 3837 | #endif |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3838 | return; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3839 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3840 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3841 | if (has_state_with_pos(l, state, subs)) |
| 3842 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3843 | } |
| 3844 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3845 | /* when there are backreferences or look-behind matches the number |
| 3846 | * of states may be (a lot) bigger */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3847 | if (nfa_has_backref && l->n == l->len) |
| 3848 | { |
| 3849 | int newlen = l->len * 3 / 2 + 50; |
| 3850 | |
| 3851 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 3852 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3853 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3854 | |
| 3855 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3856 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3857 | thread = &l->t[l->n++]; |
| 3858 | thread->state = state; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3859 | thread->pim = NULL; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3860 | copy_sub(&thread->subs.norm, &subs->norm); |
| 3861 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3862 | if (nfa_has_zsubexpr) |
| 3863 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3864 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3865 | #ifdef ENABLE_LOG |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3866 | report_state("Adding", &thread->subs.norm, state, l->id); |
| 3867 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3868 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3869 | } |
| 3870 | |
| 3871 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3872 | if (!did_print) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3873 | report_state("Processing", &subs->norm, state, l->id); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3874 | #endif |
| 3875 | switch (state->c) |
| 3876 | { |
| 3877 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3878 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3879 | break; |
| 3880 | |
| 3881 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3882 | /* order matters here */ |
| 3883 | addstate(l, state->out, subs, off); |
| 3884 | addstate(l, state->out1, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3885 | break; |
| 3886 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3887 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3888 | case NFA_NOPEN: |
| 3889 | case NFA_NCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3890 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3891 | break; |
| 3892 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3893 | case NFA_MOPEN: |
| 3894 | case NFA_MOPEN1: |
| 3895 | case NFA_MOPEN2: |
| 3896 | case NFA_MOPEN3: |
| 3897 | case NFA_MOPEN4: |
| 3898 | case NFA_MOPEN5: |
| 3899 | case NFA_MOPEN6: |
| 3900 | case NFA_MOPEN7: |
| 3901 | case NFA_MOPEN8: |
| 3902 | case NFA_MOPEN9: |
| 3903 | #ifdef FEAT_SYN_HL |
| 3904 | case NFA_ZOPEN: |
| 3905 | case NFA_ZOPEN1: |
| 3906 | case NFA_ZOPEN2: |
| 3907 | case NFA_ZOPEN3: |
| 3908 | case NFA_ZOPEN4: |
| 3909 | case NFA_ZOPEN5: |
| 3910 | case NFA_ZOPEN6: |
| 3911 | case NFA_ZOPEN7: |
| 3912 | case NFA_ZOPEN8: |
| 3913 | case NFA_ZOPEN9: |
| 3914 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3915 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3916 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3917 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3918 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3919 | sub = &subs->norm; |
| 3920 | } |
| 3921 | #ifdef FEAT_SYN_HL |
| 3922 | else if (state->c >= NFA_ZOPEN) |
| 3923 | { |
| 3924 | subidx = state->c - NFA_ZOPEN; |
| 3925 | sub = &subs->synt; |
| 3926 | } |
| 3927 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3928 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3929 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3930 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3931 | sub = &subs->norm; |
| 3932 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3933 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3934 | /* Set the position (with "off") in the subexpression. Save and |
| 3935 | * restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | 4b6ebe6 | 2013-05-30 17:49:24 +0200 | [diff] [blame] | 3936 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3937 | if (REG_MULTI) |
| 3938 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3939 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3940 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3941 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3942 | save_in_use = -1; |
| 3943 | } |
| 3944 | else |
| 3945 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3946 | save_in_use = sub->in_use; |
| 3947 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3948 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3949 | sub->list.multi[i].start.lnum = -1; |
| 3950 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3951 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3952 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3953 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3954 | if (off == -1) |
| 3955 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3956 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 3957 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3958 | } |
| 3959 | else |
| 3960 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3961 | sub->list.multi[subidx].start.lnum = reglnum; |
| 3962 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3963 | (colnr_T)(reginput - regline + off); |
| 3964 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3965 | } |
| 3966 | else |
| 3967 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3968 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3969 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3970 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3971 | save_in_use = -1; |
| 3972 | } |
| 3973 | else |
| 3974 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3975 | save_in_use = sub->in_use; |
| 3976 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3977 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3978 | sub->list.line[i].start = NULL; |
| 3979 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3980 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3981 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3982 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3983 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3984 | } |
| 3985 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3986 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3987 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3988 | if (save_in_use == -1) |
| 3989 | { |
| 3990 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3991 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3992 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3993 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3994 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3995 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3996 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3997 | break; |
| 3998 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3999 | case NFA_MCLOSE: |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4000 | if (nfa_has_zend) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4001 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 4002 | /* Do not overwrite the position set by \ze. If no \ze |
| 4003 | * encountered end will be set in nfa_regtry(). */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4004 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4005 | break; |
| 4006 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4007 | case NFA_MCLOSE1: |
| 4008 | case NFA_MCLOSE2: |
| 4009 | case NFA_MCLOSE3: |
| 4010 | case NFA_MCLOSE4: |
| 4011 | case NFA_MCLOSE5: |
| 4012 | case NFA_MCLOSE6: |
| 4013 | case NFA_MCLOSE7: |
| 4014 | case NFA_MCLOSE8: |
| 4015 | case NFA_MCLOSE9: |
| 4016 | #ifdef FEAT_SYN_HL |
| 4017 | case NFA_ZCLOSE: |
| 4018 | case NFA_ZCLOSE1: |
| 4019 | case NFA_ZCLOSE2: |
| 4020 | case NFA_ZCLOSE3: |
| 4021 | case NFA_ZCLOSE4: |
| 4022 | case NFA_ZCLOSE5: |
| 4023 | case NFA_ZCLOSE6: |
| 4024 | case NFA_ZCLOSE7: |
| 4025 | case NFA_ZCLOSE8: |
| 4026 | case NFA_ZCLOSE9: |
| 4027 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4028 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4029 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4030 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4031 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4032 | sub = &subs->norm; |
| 4033 | } |
| 4034 | #ifdef FEAT_SYN_HL |
| 4035 | else if (state->c >= NFA_ZCLOSE) |
| 4036 | { |
| 4037 | subidx = state->c - NFA_ZCLOSE; |
| 4038 | sub = &subs->synt; |
| 4039 | } |
| 4040 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4041 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4042 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4043 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4044 | sub = &subs->norm; |
| 4045 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4046 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4047 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 4048 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4049 | save_in_use = sub->in_use; |
| 4050 | if (sub->in_use <= subidx) |
| 4051 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4052 | if (REG_MULTI) |
| 4053 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4054 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4055 | if (off == -1) |
| 4056 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4057 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 4058 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4059 | } |
| 4060 | else |
| 4061 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4062 | sub->list.multi[subidx].end.lnum = reglnum; |
| 4063 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4064 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4065 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4066 | } |
| 4067 | else |
| 4068 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4069 | save_ptr = sub->list.line[subidx].end; |
| 4070 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4071 | } |
| 4072 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4073 | addstate(l, state->out, subs, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4074 | |
| 4075 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4076 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4077 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4078 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4079 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4080 | break; |
| 4081 | } |
| 4082 | } |
| 4083 | |
| 4084 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4085 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 4086 | * Used for zero-width matches, next state to use is the added one. |
| 4087 | * This makes sure the order of states to be tried does not change, which |
| 4088 | * matters for alternatives. |
| 4089 | */ |
| 4090 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4091 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4092 | nfa_list_T *l; /* runtime state list */ |
| 4093 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4094 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4095 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4096 | int *ip; |
| 4097 | { |
| 4098 | int tlen = l->n; |
| 4099 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4100 | int listidx = *ip; |
| 4101 | int i; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4102 | |
| 4103 | /* 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] | 4104 | addstate(l, state, subs, 0); |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4105 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4106 | /* fill in the "pim" field in the new states */ |
| 4107 | if (pim != NULL) |
| 4108 | for (i = tlen; i < l->n; ++i) |
| 4109 | l->t[i].pim = pim; |
| 4110 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4111 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4112 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4113 | return; |
| 4114 | |
| 4115 | /* re-order to put the new state at the current position */ |
| 4116 | count = l->n - tlen; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4117 | if (count == 1) |
| 4118 | { |
| 4119 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4120 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4121 | } |
| 4122 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4123 | { |
| 4124 | /* make space for new states, then move them from the |
| 4125 | * end to the current position */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4126 | mch_memmove(&(l->t[listidx + count]), |
| 4127 | &(l->t[listidx + 1]), |
| 4128 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 4129 | mch_memmove(&(l->t[listidx]), |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4130 | &(l->t[l->n - 1]), |
| 4131 | sizeof(nfa_thread_T) * count); |
| 4132 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4133 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4134 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4135 | } |
| 4136 | |
| 4137 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4138 | * Check character class "class" against current character c. |
| 4139 | */ |
| 4140 | static int |
| 4141 | check_char_class(class, c) |
| 4142 | int class; |
| 4143 | int c; |
| 4144 | { |
| 4145 | switch (class) |
| 4146 | { |
| 4147 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4148 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4149 | return OK; |
| 4150 | break; |
| 4151 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4152 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4153 | return OK; |
| 4154 | break; |
| 4155 | case NFA_CLASS_BLANK: |
| 4156 | if (c == ' ' || c == '\t') |
| 4157 | return OK; |
| 4158 | break; |
| 4159 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4160 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4161 | return OK; |
| 4162 | break; |
| 4163 | case NFA_CLASS_DIGIT: |
| 4164 | if (VIM_ISDIGIT(c)) |
| 4165 | return OK; |
| 4166 | break; |
| 4167 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4168 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4169 | return OK; |
| 4170 | break; |
| 4171 | case NFA_CLASS_LOWER: |
| 4172 | if (MB_ISLOWER(c)) |
| 4173 | return OK; |
| 4174 | break; |
| 4175 | case NFA_CLASS_PRINT: |
| 4176 | if (vim_isprintc(c)) |
| 4177 | return OK; |
| 4178 | break; |
| 4179 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4180 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4181 | return OK; |
| 4182 | break; |
| 4183 | case NFA_CLASS_SPACE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4184 | if ((c >= 9 && c <= 13) || (c == ' ')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4185 | return OK; |
| 4186 | break; |
| 4187 | case NFA_CLASS_UPPER: |
| 4188 | if (MB_ISUPPER(c)) |
| 4189 | return OK; |
| 4190 | break; |
| 4191 | case NFA_CLASS_XDIGIT: |
| 4192 | if (vim_isxdigit(c)) |
| 4193 | return OK; |
| 4194 | break; |
| 4195 | case NFA_CLASS_TAB: |
| 4196 | if (c == '\t') |
| 4197 | return OK; |
| 4198 | break; |
| 4199 | case NFA_CLASS_RETURN: |
| 4200 | if (c == '\r') |
| 4201 | return OK; |
| 4202 | break; |
| 4203 | case NFA_CLASS_BACKSPACE: |
| 4204 | if (c == '\b') |
| 4205 | return OK; |
| 4206 | break; |
| 4207 | case NFA_CLASS_ESCAPE: |
| 4208 | if (c == '\033') |
| 4209 | return OK; |
| 4210 | break; |
| 4211 | |
| 4212 | default: |
| 4213 | /* should not be here :P */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 4214 | EMSGN("E877: (NFA regexp) Invalid character class: %ld", class); |
| 4215 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4216 | } |
| 4217 | return FAIL; |
| 4218 | } |
| 4219 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4220 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
| 4221 | |
| 4222 | /* |
| 4223 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4224 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4225 | */ |
| 4226 | static int |
| 4227 | match_backref(sub, subidx, bytelen) |
| 4228 | regsub_T *sub; /* pointers to subexpressions */ |
| 4229 | int subidx; |
| 4230 | int *bytelen; /* out: length of match in bytes */ |
| 4231 | { |
| 4232 | int len; |
| 4233 | |
| 4234 | if (sub->in_use <= subidx) |
| 4235 | { |
| 4236 | retempty: |
| 4237 | /* backref was not set, match an empty string */ |
| 4238 | *bytelen = 0; |
| 4239 | return TRUE; |
| 4240 | } |
| 4241 | |
| 4242 | if (REG_MULTI) |
| 4243 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4244 | if (sub->list.multi[subidx].start.lnum < 0 |
| 4245 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4246 | goto retempty; |
| 4247 | /* TODO: line breaks */ |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4248 | len = sub->list.multi[subidx].end.col |
| 4249 | - sub->list.multi[subidx].start.col; |
| 4250 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4251 | reginput, &len) == 0) |
| 4252 | { |
| 4253 | *bytelen = len; |
| 4254 | return TRUE; |
| 4255 | } |
| 4256 | } |
| 4257 | else |
| 4258 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4259 | if (sub->list.line[subidx].start == NULL |
| 4260 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4261 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4262 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 4263 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4264 | { |
| 4265 | *bytelen = len; |
| 4266 | return TRUE; |
| 4267 | } |
| 4268 | } |
| 4269 | return FALSE; |
| 4270 | } |
| 4271 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4272 | #ifdef FEAT_SYN_HL |
| 4273 | |
| 4274 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 4275 | |
| 4276 | /* |
| 4277 | * Check for a match with \z subexpression "subidx". |
| 4278 | * Return TRUE if it matches. |
| 4279 | */ |
| 4280 | static int |
| 4281 | match_zref(subidx, bytelen) |
| 4282 | int subidx; |
| 4283 | int *bytelen; /* out: length of match in bytes */ |
| 4284 | { |
| 4285 | int len; |
| 4286 | |
| 4287 | cleanup_zsubexpr(); |
| 4288 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 4289 | { |
| 4290 | /* backref was not set, match an empty string */ |
| 4291 | *bytelen = 0; |
| 4292 | return TRUE; |
| 4293 | } |
| 4294 | |
| 4295 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 4296 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 4297 | { |
| 4298 | *bytelen = len; |
| 4299 | return TRUE; |
| 4300 | } |
| 4301 | return FALSE; |
| 4302 | } |
| 4303 | #endif |
| 4304 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4305 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4306 | * Save list IDs for all NFA states of "prog" into "list". |
| 4307 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4308 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4309 | */ |
| 4310 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4311 | nfa_save_listids(prog, list) |
| 4312 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4313 | int *list; |
| 4314 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4315 | int i; |
| 4316 | nfa_state_T *p; |
| 4317 | |
| 4318 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 4319 | p = &prog->state[0]; |
| 4320 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4321 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4322 | list[i] = p->lastlist[1]; |
| 4323 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4324 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4325 | } |
| 4326 | } |
| 4327 | |
| 4328 | /* |
| 4329 | * Restore list IDs from "list" to all NFA states. |
| 4330 | */ |
| 4331 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4332 | nfa_restore_listids(prog, list) |
| 4333 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4334 | int *list; |
| 4335 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4336 | int i; |
| 4337 | nfa_state_T *p; |
| 4338 | |
| 4339 | p = &prog->state[0]; |
| 4340 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4341 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4342 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4343 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4344 | } |
| 4345 | } |
| 4346 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4347 | static int |
| 4348 | nfa_re_num_cmp(val, op, pos) |
| 4349 | long_u val; |
| 4350 | int op; |
| 4351 | long_u pos; |
| 4352 | { |
| 4353 | if (op == 1) return pos > val; |
| 4354 | if (op == 2) return pos < val; |
| 4355 | return val == pos; |
| 4356 | } |
| 4357 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4358 | 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] | 4359 | 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] | 4360 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4361 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4362 | * Recursively call nfa_regmatch() |
| 4363 | */ |
| 4364 | static int |
| 4365 | recursive_regmatch(state, prog, submatch, m, listids) |
| 4366 | nfa_state_T *state; |
| 4367 | nfa_regprog_T *prog; |
| 4368 | regsubs_T *submatch; |
| 4369 | regsubs_T *m; |
| 4370 | int **listids; |
| 4371 | { |
| 4372 | char_u *save_reginput = reginput; |
| 4373 | char_u *save_regline = regline; |
| 4374 | int save_reglnum = reglnum; |
| 4375 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4376 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4377 | save_se_T *save_nfa_endp = nfa_endp; |
| 4378 | save_se_T endpos; |
| 4379 | save_se_T *endposp = NULL; |
| 4380 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4381 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4382 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4383 | if (state->c == NFA_START_INVISIBLE_BEFORE |
| 4384 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4385 | { |
| 4386 | /* The recursive match must end at the current position. */ |
| 4387 | endposp = &endpos; |
| 4388 | if (REG_MULTI) |
| 4389 | { |
| 4390 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 4391 | endpos.se_u.pos.lnum = reglnum; |
| 4392 | } |
| 4393 | else |
| 4394 | endpos.se_u.ptr = reginput; |
| 4395 | |
| 4396 | /* Go back the specified number of bytes, or as far as the |
| 4397 | * start of the previous line, to try matching "\@<=" or |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4398 | * not matching "\@<!". This is very ineffecient, limit the number of |
| 4399 | * bytes if possible. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4400 | if (state->val <= 0) |
| 4401 | { |
| 4402 | if (REG_MULTI) |
| 4403 | { |
| 4404 | regline = reg_getline(--reglnum); |
| 4405 | if (regline == NULL) |
| 4406 | /* can't go before the first line */ |
| 4407 | regline = reg_getline(++reglnum); |
| 4408 | } |
| 4409 | reginput = regline; |
| 4410 | } |
| 4411 | else |
| 4412 | { |
| 4413 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 4414 | { |
| 4415 | /* Not enough bytes in this line, go to end of |
| 4416 | * previous line. */ |
| 4417 | regline = reg_getline(--reglnum); |
| 4418 | if (regline == NULL) |
| 4419 | { |
| 4420 | /* can't go before the first line */ |
| 4421 | regline = reg_getline(++reglnum); |
| 4422 | reginput = regline; |
| 4423 | } |
| 4424 | else |
| 4425 | reginput = regline + STRLEN(regline); |
| 4426 | } |
| 4427 | if ((int)(reginput - regline) >= state->val) |
| 4428 | { |
| 4429 | reginput -= state->val; |
| 4430 | #ifdef FEAT_MBYTE |
| 4431 | if (has_mbyte) |
| 4432 | reginput -= mb_head_off(regline, reginput); |
| 4433 | #endif |
| 4434 | } |
| 4435 | else |
| 4436 | reginput = regline; |
| 4437 | } |
| 4438 | } |
| 4439 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4440 | #ifdef ENABLE_LOG |
| 4441 | if (log_fd != stderr) |
| 4442 | fclose(log_fd); |
| 4443 | log_fd = NULL; |
| 4444 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4445 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 4446 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 4447 | if (nfa_ll_index == 1) |
| 4448 | { |
| 4449 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 4450 | * values and clear them. */ |
| 4451 | if (*listids == NULL) |
| 4452 | { |
| 4453 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 4454 | if (*listids == NULL) |
| 4455 | { |
| 4456 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 4457 | return 0; |
| 4458 | } |
| 4459 | } |
| 4460 | nfa_save_listids(prog, *listids); |
| 4461 | need_restore = TRUE; |
| 4462 | /* any value of nfa_listid will do */ |
| 4463 | } |
| 4464 | else |
| 4465 | { |
| 4466 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 4467 | * entry. Make sure nfa_listid is different from a previous recursive |
| 4468 | * call, because some states may still have this ID. */ |
| 4469 | ++nfa_ll_index; |
| 4470 | if (nfa_listid <= nfa_alt_listid) |
| 4471 | nfa_listid = nfa_alt_listid; |
| 4472 | } |
| 4473 | |
| 4474 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 4475 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4476 | nfa_endp = endposp; |
| 4477 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4478 | |
| 4479 | if (need_restore) |
| 4480 | nfa_restore_listids(prog, *listids); |
| 4481 | else |
| 4482 | { |
| 4483 | --nfa_ll_index; |
| 4484 | nfa_alt_listid = nfa_listid; |
| 4485 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4486 | |
| 4487 | /* restore position in input text */ |
| 4488 | reginput = save_reginput; |
| 4489 | regline = save_regline; |
| 4490 | reglnum = save_reglnum; |
| 4491 | nfa_match = save_nfa_match; |
| 4492 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4493 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4494 | |
| 4495 | #ifdef ENABLE_LOG |
| 4496 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 4497 | if (log_fd != NULL) |
| 4498 | { |
| 4499 | fprintf(log_fd, "****************************\n"); |
| 4500 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 4501 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 4502 | fprintf(log_fd, "****************************\n"); |
| 4503 | } |
| 4504 | else |
| 4505 | { |
| 4506 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4507 | log_fd = stderr; |
| 4508 | } |
| 4509 | #endif |
| 4510 | |
| 4511 | return result; |
| 4512 | } |
| 4513 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4514 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 4515 | static int skip_to_start __ARGS((int c, colnr_T *colp)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4516 | static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text)); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4517 | |
| 4518 | /* |
| 4519 | * Estimate the chance of a match with "state" failing. |
| 4520 | * NFA_ANY: 1 |
| 4521 | * specific character: 99 |
| 4522 | */ |
| 4523 | static int |
| 4524 | failure_chance(state, depth) |
| 4525 | nfa_state_T *state; |
| 4526 | int depth; |
| 4527 | { |
| 4528 | int c = state->c; |
| 4529 | int l, r; |
| 4530 | |
| 4531 | /* detect looping */ |
| 4532 | if (depth > 4) |
| 4533 | return 1; |
| 4534 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4535 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4536 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4537 | case NFA_SPLIT: |
| 4538 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 4539 | /* avoid recursive stuff */ |
| 4540 | return 1; |
| 4541 | /* two alternatives, use the lowest failure chance */ |
| 4542 | l = failure_chance(state->out, depth + 1); |
| 4543 | r = failure_chance(state->out1, depth + 1); |
| 4544 | return l < r ? l : r; |
| 4545 | |
| 4546 | case NFA_ANY: |
| 4547 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4548 | return 1; |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4549 | case NFA_MATCH: |
| 4550 | /* empty match works always */ |
| 4551 | return 0; |
| 4552 | |
| 4553 | case NFA_BOL: |
| 4554 | case NFA_EOL: |
| 4555 | case NFA_BOF: |
| 4556 | case NFA_EOF: |
| 4557 | case NFA_NEWL: |
| 4558 | return 99; |
| 4559 | |
| 4560 | case NFA_BOW: |
| 4561 | case NFA_EOW: |
| 4562 | return 90; |
| 4563 | |
| 4564 | case NFA_MOPEN: |
| 4565 | case NFA_MOPEN1: |
| 4566 | case NFA_MOPEN2: |
| 4567 | case NFA_MOPEN3: |
| 4568 | case NFA_MOPEN4: |
| 4569 | case NFA_MOPEN5: |
| 4570 | case NFA_MOPEN6: |
| 4571 | case NFA_MOPEN7: |
| 4572 | case NFA_MOPEN8: |
| 4573 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4574 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4575 | case NFA_ZOPEN: |
| 4576 | case NFA_ZOPEN1: |
| 4577 | case NFA_ZOPEN2: |
| 4578 | case NFA_ZOPEN3: |
| 4579 | case NFA_ZOPEN4: |
| 4580 | case NFA_ZOPEN5: |
| 4581 | case NFA_ZOPEN6: |
| 4582 | case NFA_ZOPEN7: |
| 4583 | case NFA_ZOPEN8: |
| 4584 | case NFA_ZOPEN9: |
| 4585 | case NFA_ZCLOSE: |
| 4586 | case NFA_ZCLOSE1: |
| 4587 | case NFA_ZCLOSE2: |
| 4588 | case NFA_ZCLOSE3: |
| 4589 | case NFA_ZCLOSE4: |
| 4590 | case NFA_ZCLOSE5: |
| 4591 | case NFA_ZCLOSE6: |
| 4592 | case NFA_ZCLOSE7: |
| 4593 | case NFA_ZCLOSE8: |
| 4594 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4595 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4596 | case NFA_NOPEN: |
| 4597 | case NFA_MCLOSE: |
| 4598 | case NFA_MCLOSE1: |
| 4599 | case NFA_MCLOSE2: |
| 4600 | case NFA_MCLOSE3: |
| 4601 | case NFA_MCLOSE4: |
| 4602 | case NFA_MCLOSE5: |
| 4603 | case NFA_MCLOSE6: |
| 4604 | case NFA_MCLOSE7: |
| 4605 | case NFA_MCLOSE8: |
| 4606 | case NFA_MCLOSE9: |
| 4607 | case NFA_NCLOSE: |
| 4608 | return failure_chance(state->out, depth + 1); |
| 4609 | |
| 4610 | case NFA_BACKREF1: |
| 4611 | case NFA_BACKREF2: |
| 4612 | case NFA_BACKREF3: |
| 4613 | case NFA_BACKREF4: |
| 4614 | case NFA_BACKREF5: |
| 4615 | case NFA_BACKREF6: |
| 4616 | case NFA_BACKREF7: |
| 4617 | case NFA_BACKREF8: |
| 4618 | case NFA_BACKREF9: |
| 4619 | #ifdef FEAT_SYN_HL |
| 4620 | case NFA_ZREF1: |
| 4621 | case NFA_ZREF2: |
| 4622 | case NFA_ZREF3: |
| 4623 | case NFA_ZREF4: |
| 4624 | case NFA_ZREF5: |
| 4625 | case NFA_ZREF6: |
| 4626 | case NFA_ZREF7: |
| 4627 | case NFA_ZREF8: |
| 4628 | case NFA_ZREF9: |
| 4629 | #endif |
| 4630 | /* backreferences don't match in many places */ |
| 4631 | return 94; |
| 4632 | |
| 4633 | case NFA_LNUM_GT: |
| 4634 | case NFA_LNUM_LT: |
| 4635 | case NFA_COL_GT: |
| 4636 | case NFA_COL_LT: |
| 4637 | case NFA_VCOL_GT: |
| 4638 | case NFA_VCOL_LT: |
| 4639 | case NFA_MARK_GT: |
| 4640 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4641 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4642 | /* before/after positions don't match very often */ |
| 4643 | return 85; |
| 4644 | |
| 4645 | case NFA_LNUM: |
| 4646 | return 90; |
| 4647 | |
| 4648 | case NFA_CURSOR: |
| 4649 | case NFA_COL: |
| 4650 | case NFA_VCOL: |
| 4651 | case NFA_MARK: |
| 4652 | /* specific positions rarely match */ |
| 4653 | return 98; |
| 4654 | |
| 4655 | case NFA_COMPOSING: |
| 4656 | return 95; |
| 4657 | |
| 4658 | default: |
| 4659 | if (c > 0) |
| 4660 | /* character match fails often */ |
| 4661 | return 95; |
| 4662 | } |
| 4663 | |
| 4664 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4665 | return 50; |
| 4666 | } |
| 4667 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4668 | /* |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 4669 | * Skip until the char "c" we know a match must start with. |
| 4670 | */ |
| 4671 | static int |
| 4672 | skip_to_start(c, colp) |
| 4673 | int c; |
| 4674 | colnr_T *colp; |
| 4675 | { |
| 4676 | char_u *s; |
| 4677 | |
| 4678 | /* Used often, do some work to avoid call overhead. */ |
| 4679 | if (!ireg_ic |
| 4680 | #ifdef FEAT_MBYTE |
| 4681 | && !has_mbyte |
| 4682 | #endif |
| 4683 | ) |
| 4684 | s = vim_strbyte(regline + *colp, c); |
| 4685 | else |
| 4686 | s = cstrchr(regline + *colp, c); |
| 4687 | if (s == NULL) |
| 4688 | return FAIL; |
| 4689 | *colp = (int)(s - regline); |
| 4690 | return OK; |
| 4691 | } |
| 4692 | |
| 4693 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4694 | * Check for a match with match_text. |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4695 | * Called after skip_to_start() has found regstart. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4696 | * Returns zero for no match, 1 for a match. |
| 4697 | */ |
| 4698 | static long |
| 4699 | find_match_text(startcol, regstart, match_text) |
| 4700 | colnr_T startcol; |
| 4701 | int regstart; |
| 4702 | char_u *match_text; |
| 4703 | { |
| 4704 | colnr_T col = startcol; |
| 4705 | int c1, c2; |
| 4706 | int len1, len2; |
| 4707 | int match; |
| 4708 | |
| 4709 | for (;;) |
| 4710 | { |
| 4711 | match = TRUE; |
| 4712 | len2 = MB_CHAR2LEN(regstart); /* skip regstart */ |
| 4713 | for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
| 4714 | { |
| 4715 | c1 = PTR2CHAR(match_text + len1); |
| 4716 | c2 = PTR2CHAR(regline + col + len2); |
| 4717 | if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
| 4718 | { |
| 4719 | match = FALSE; |
| 4720 | break; |
| 4721 | } |
| 4722 | len2 += MB_CHAR2LEN(c2); |
| 4723 | } |
| 4724 | if (match |
| 4725 | #ifdef FEAT_MBYTE |
| 4726 | /* check that no composing char follows */ |
| 4727 | && !(enc_utf8 |
| 4728 | && utf_iscomposing(PTR2CHAR(regline + col + len2))) |
| 4729 | #endif |
| 4730 | ) |
| 4731 | { |
| 4732 | cleanup_subexpr(); |
| 4733 | if (REG_MULTI) |
| 4734 | { |
| 4735 | reg_startpos[0].lnum = reglnum; |
| 4736 | reg_startpos[0].col = col; |
| 4737 | reg_endpos[0].lnum = reglnum; |
| 4738 | reg_endpos[0].col = col + len2; |
| 4739 | } |
| 4740 | else |
| 4741 | { |
| 4742 | reg_startp[0] = regline + col; |
| 4743 | reg_endp[0] = regline + col + len2; |
| 4744 | } |
| 4745 | return 1L; |
| 4746 | } |
| 4747 | |
| 4748 | /* Try finding regstart after the current match. */ |
| 4749 | col += MB_CHAR2LEN(regstart); /* skip regstart */ |
| 4750 | if (skip_to_start(regstart, &col) == FAIL) |
| 4751 | break; |
| 4752 | } |
| 4753 | return 0L; |
| 4754 | } |
| 4755 | |
| 4756 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4757 | * Main matching routine. |
| 4758 | * |
| 4759 | * Run NFA to determine whether it matches reginput. |
| 4760 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4761 | * 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] | 4762 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4763 | * Return TRUE if there is a match, FALSE otherwise. |
| 4764 | * Note: Caller must ensure that: start != NULL. |
| 4765 | */ |
| 4766 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4767 | nfa_regmatch(prog, start, submatch, m) |
| 4768 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4769 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4770 | regsubs_T *submatch; |
| 4771 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4772 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4773 | int result; |
| 4774 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4775 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4776 | int go_to_nextline = FALSE; |
| 4777 | nfa_thread_T *t; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4778 | nfa_list_T list[2]; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4779 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4780 | nfa_list_T *thislist; |
| 4781 | nfa_list_T *nextlist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4782 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4783 | nfa_state_T *add_state; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 4784 | int add_here; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4785 | int add_count; |
| 4786 | int add_off; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4787 | garray_T pimlist; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4788 | int toplevel = start->c == NFA_MOPEN; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4789 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4790 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4791 | |
| 4792 | if (debug == NULL) |
| 4793 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4794 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4795 | return FALSE; |
| 4796 | } |
| 4797 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4798 | nfa_match = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4799 | ga_init2(&pimlist, sizeof(nfa_pim_T), 5); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4800 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4801 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4802 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4803 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4804 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4805 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4806 | list[1].len = nstate + 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4807 | if (list[0].t == NULL || list[1].t == NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4808 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4809 | |
| 4810 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4811 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4812 | if (log_fd != NULL) |
| 4813 | { |
| 4814 | fprintf(log_fd, "**********************************\n"); |
| 4815 | nfa_set_code(start->c); |
| 4816 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 4817 | abs(start->id), code); |
| 4818 | fprintf(log_fd, "**********************************\n"); |
| 4819 | } |
| 4820 | else |
| 4821 | { |
| 4822 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4823 | log_fd = stderr; |
| 4824 | } |
| 4825 | #endif |
| 4826 | |
| 4827 | thislist = &list[0]; |
| 4828 | thislist->n = 0; |
| 4829 | nextlist = &list[1]; |
| 4830 | nextlist->n = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4831 | #ifdef ENABLE_LOG |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4832 | fprintf(log_fd, "(---) STARTSTATE first\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4833 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4834 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4835 | |
| 4836 | /* Inline optimized code for addstate(thislist, start, m, 0) if we know |
| 4837 | * it's the first MOPEN. */ |
| 4838 | if (toplevel) |
| 4839 | { |
| 4840 | if (REG_MULTI) |
| 4841 | { |
| 4842 | m->norm.list.multi[0].start.lnum = reglnum; |
| 4843 | m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline); |
| 4844 | } |
| 4845 | else |
| 4846 | m->norm.list.line[0].start = reginput; |
| 4847 | m->norm.in_use = 1; |
| 4848 | addstate(thislist, start->out, m, 0); |
| 4849 | } |
| 4850 | else |
| 4851 | addstate(thislist, start, m, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4852 | |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4853 | #define ADD_STATE_IF_MATCH(state) \ |
| 4854 | if (result) { \ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4855 | add_state = state->out; \ |
| 4856 | add_off = clen; \ |
| 4857 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4858 | |
| 4859 | /* |
| 4860 | * Run for each character. |
| 4861 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4862 | for (;;) |
| 4863 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4864 | int curc; |
| 4865 | int clen; |
| 4866 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4867 | #ifdef FEAT_MBYTE |
| 4868 | if (has_mbyte) |
| 4869 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4870 | curc = (*mb_ptr2char)(reginput); |
| 4871 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4872 | } |
| 4873 | else |
| 4874 | #endif |
| 4875 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4876 | curc = *reginput; |
| 4877 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4878 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4879 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4880 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4881 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4882 | go_to_nextline = FALSE; |
| 4883 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4884 | |
| 4885 | /* swap lists */ |
| 4886 | thislist = &list[flag]; |
| 4887 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4888 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4889 | ++nfa_listid; |
| 4890 | thislist->id = nfa_listid; |
| 4891 | nextlist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4892 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4893 | pimlist.ga_len = 0; |
| 4894 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4895 | #ifdef ENABLE_LOG |
| 4896 | fprintf(log_fd, "------------------------------------------\n"); |
| 4897 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4898 | 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] | 4899 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4900 | { |
| 4901 | int i; |
| 4902 | |
| 4903 | for (i = 0; i < thislist->n; i++) |
| 4904 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 4905 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4906 | fprintf(log_fd, "\n"); |
| 4907 | #endif |
| 4908 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4909 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4910 | fprintf(debug, "\n-------------------\n"); |
| 4911 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 4912 | /* |
| 4913 | * If the state lists are empty we can stop. |
| 4914 | */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4915 | if (thislist->n == 0) |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 4916 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4917 | |
| 4918 | /* compute nextlist */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4919 | for (listidx = 0; listidx < thislist->n; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4920 | { |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4921 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4922 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4923 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4924 | nfa_set_code(t->state->c); |
| 4925 | fprintf(debug, "%s, ", code); |
| 4926 | #endif |
| 4927 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4928 | { |
| 4929 | int col; |
| 4930 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4931 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4932 | col = -1; |
| 4933 | else if (REG_MULTI) |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4934 | col = t->subs.norm.list.multi[0].start.col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4935 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 4936 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4937 | nfa_set_code(t->state->c); |
| 4938 | fprintf(log_fd, "(%d) char %d %s (start col %d) ... \n", |
| 4939 | abs(t->state->id), (int)t->state->c, code, col); |
| 4940 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4941 | #endif |
| 4942 | |
| 4943 | /* |
| 4944 | * Handle the possible codes of the current state. |
| 4945 | * The most important is NFA_MATCH. |
| 4946 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4947 | add_state = NULL; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 4948 | add_here = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4949 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4950 | switch (t->state->c) |
| 4951 | { |
| 4952 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 4953 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4954 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4955 | copy_sub(&submatch->norm, &t->subs.norm); |
| 4956 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4957 | if (nfa_has_zsubexpr) |
| 4958 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4959 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4960 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4961 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4962 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4963 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4964 | * states at this position. When the list of states is going |
| 4965 | * to be empty quit without advancing, so that "reginput" is |
| 4966 | * correct. */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4967 | if (nextlist->n == 0) |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4968 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4969 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 4970 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4971 | |
| 4972 | case NFA_END_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4973 | case NFA_END_INVISIBLE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 4974 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4975 | /* |
| 4976 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4977 | * NFA_START_INVISIBLE_BEFORE node. |
| 4978 | * They surround a zero-width group, used with "\@=", "\&", |
| 4979 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4980 | * If we got here, it means that the current "invisible" group |
| 4981 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4982 | * nfa_regmatch(). For a look-behind match only when it ends |
| 4983 | * in the position in "nfa_endp". |
| 4984 | * Submatches are stored in *m, and used in the parent call. |
| 4985 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 4986 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4987 | if (nfa_endp != NULL) |
| 4988 | { |
| 4989 | if (REG_MULTI) |
| 4990 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 4991 | (int)reglnum, |
| 4992 | (int)nfa_endp->se_u.pos.lnum, |
| 4993 | (int)(reginput - regline), |
| 4994 | nfa_endp->se_u.pos.col); |
| 4995 | else |
| 4996 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 4997 | (int)(reginput - regline), |
| 4998 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4999 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5000 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5001 | /* If "nfa_endp" is set it's only a match if it ends at |
| 5002 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5003 | if (nfa_endp != NULL && (REG_MULTI |
| 5004 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 5005 | || (int)(reginput - regline) |
| 5006 | != nfa_endp->se_u.pos.col) |
| 5007 | : reginput != nfa_endp->se_u.ptr)) |
| 5008 | break; |
| 5009 | |
| 5010 | /* do not set submatches for \@! */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5011 | if (t->state->c != NFA_END_INVISIBLE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5012 | { |
| 5013 | copy_sub(&m->norm, &t->subs.norm); |
| 5014 | #ifdef FEAT_SYN_HL |
| 5015 | if (nfa_has_zsubexpr) |
| 5016 | copy_sub(&m->synt, &t->subs.synt); |
| 5017 | #endif |
| 5018 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5019 | #ifdef ENABLE_LOG |
| 5020 | fprintf(log_fd, "Match found:\n"); |
| 5021 | log_subsexpr(m); |
| 5022 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5023 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5024 | break; |
| 5025 | |
| 5026 | case NFA_START_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5027 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5028 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5029 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5030 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5031 | nfa_pim_T *pim; |
| 5032 | int cout = t->state->out1->out->c; |
| 5033 | |
| 5034 | /* Do it directly when what follows is possibly end of |
| 5035 | * match (closing paren). |
| 5036 | * Postpone when it is \@<= or \@<!, these are expensive. |
| 5037 | * TODO: remove the check for t->pim and check multiple |
| 5038 | * where it's used? |
| 5039 | * Otherwise first do the one that has the highest chance |
| 5040 | * of failing. */ |
| 5041 | if ((cout >= NFA_MCLOSE && cout <= NFA_MCLOSE9) |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5042 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5043 | || (cout >= NFA_ZCLOSE && cout <= NFA_ZCLOSE9) |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5044 | #endif |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5045 | || t->pim != NULL |
| 5046 | || (t->state->c != NFA_START_INVISIBLE_BEFORE |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5047 | && t->state->c != NFA_START_INVISIBLE_BEFORE_NEG |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5048 | && failure_chance(t->state->out1->out, 0) |
| 5049 | < failure_chance(t->state->out, 0))) |
| 5050 | { |
| 5051 | /* |
| 5052 | * First try matching the invisible match, then what |
| 5053 | * follows. |
| 5054 | */ |
| 5055 | result = recursive_regmatch(t->state, prog, |
| 5056 | submatch, m, &listids); |
| 5057 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5058 | /* for \@! and \@<! it is a match when the result is |
| 5059 | * FALSE */ |
| 5060 | if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
| 5061 | || t->state->c |
| 5062 | == NFA_START_INVISIBLE_BEFORE_NEG)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5063 | { |
| 5064 | /* Copy submatch info from the recursive call */ |
| 5065 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5066 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5067 | if (nfa_has_zsubexpr) |
| 5068 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5069 | #endif |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5070 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5071 | /* t->state->out1 is the corresponding |
| 5072 | * END_INVISIBLE node; Add its out to the current |
| 5073 | * list (zero-width match). */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5074 | add_here = TRUE; |
| 5075 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5076 | } |
| 5077 | } |
| 5078 | else |
| 5079 | { |
| 5080 | /* |
| 5081 | * First try matching what follows at the current |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5082 | * position. Only if a match is found, before |
| 5083 | * addstate() is called, then verify the invisible |
| 5084 | * match matches. Add a nfa_pim_T to the following |
| 5085 | * states, it contains info about the invisible match. |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5086 | */ |
| 5087 | if (ga_grow(&pimlist, 1) == FAIL) |
| 5088 | goto theend; |
| 5089 | pim = (nfa_pim_T *)pimlist.ga_data + pimlist.ga_len; |
| 5090 | ++pimlist.ga_len; |
| 5091 | pim->state = t->state; |
| 5092 | pim->pim = NULL; |
| 5093 | pim->result = NFA_PIM_TODO; |
| 5094 | |
| 5095 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 5096 | * node; Add its out to the current list (zero-width |
| 5097 | * match). */ |
| 5098 | addstate_here(thislist, t->state->out1->out, &t->subs, |
| 5099 | pim, &listidx); |
| 5100 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5101 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5102 | break; |
| 5103 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5104 | case NFA_START_PATTERN: |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5105 | { |
| 5106 | nfa_state_T *skip = NULL; |
| 5107 | #ifdef ENABLE_LOG |
| 5108 | int skip_lid = 0; |
| 5109 | #endif |
| 5110 | |
| 5111 | /* There is no point in trying to match the pattern if the |
| 5112 | * output state is not going to be added to the list. */ |
| 5113 | if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
| 5114 | { |
| 5115 | skip = t->state->out1->out; |
| 5116 | #ifdef ENABLE_LOG |
| 5117 | skip_lid = nextlist->id; |
| 5118 | #endif |
| 5119 | } |
| 5120 | else if (state_in_list(nextlist, |
| 5121 | t->state->out1->out->out, &t->subs)) |
| 5122 | { |
| 5123 | skip = t->state->out1->out->out; |
| 5124 | #ifdef ENABLE_LOG |
| 5125 | skip_lid = nextlist->id; |
| 5126 | #endif |
| 5127 | } |
| 5128 | else if(state_in_list(thislist, |
| 5129 | t->state->out1->out->out, &t->subs)) |
| 5130 | { |
| 5131 | skip = t->state->out1->out->out; |
| 5132 | #ifdef ENABLE_LOG |
| 5133 | skip_lid = thislist->id; |
| 5134 | #endif |
| 5135 | } |
| 5136 | if (skip != NULL) |
| 5137 | { |
| 5138 | #ifdef ENABLE_LOG |
| 5139 | nfa_set_code(skip->c); |
| 5140 | fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
| 5141 | abs(skip->id), skip_lid, skip->c, code); |
| 5142 | #endif |
| 5143 | break; |
| 5144 | } |
| 5145 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5146 | /* First try matching the pattern. */ |
| 5147 | result = recursive_regmatch(t->state, prog, |
| 5148 | submatch, m, &listids); |
| 5149 | if (result) |
| 5150 | { |
| 5151 | int bytelen; |
| 5152 | |
| 5153 | #ifdef ENABLE_LOG |
| 5154 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 5155 | log_subsexpr(m); |
| 5156 | #endif |
| 5157 | /* Copy submatch info from the recursive call */ |
| 5158 | copy_sub_off(&t->subs.norm, &m->norm); |
| 5159 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5160 | if (nfa_has_zsubexpr) |
| 5161 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5162 | #endif |
| 5163 | /* Now we need to skip over the matched text and then |
| 5164 | * continue with what follows. */ |
| 5165 | if (REG_MULTI) |
| 5166 | /* TODO: multi-line match */ |
| 5167 | bytelen = m->norm.list.multi[0].end.col |
| 5168 | - (int)(reginput - regline); |
| 5169 | else |
| 5170 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 5171 | |
| 5172 | #ifdef ENABLE_LOG |
| 5173 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 5174 | #endif |
| 5175 | if (bytelen == 0) |
| 5176 | { |
| 5177 | /* empty match, output of corresponding |
| 5178 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 5179 | * position */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5180 | add_here = TRUE; |
| 5181 | add_state = t->state->out1->out->out; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5182 | } |
| 5183 | else if (bytelen <= clen) |
| 5184 | { |
| 5185 | /* match current character, output of corresponding |
| 5186 | * NFA_END_PATTERN to be used at next position. */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5187 | add_state = t->state->out1->out->out; |
| 5188 | add_off = clen; |
| 5189 | } |
| 5190 | else |
| 5191 | { |
| 5192 | /* skip over the matched characters, set character |
| 5193 | * count in NFA_SKIP */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5194 | add_state = t->state->out1->out; |
| 5195 | add_off = bytelen; |
| 5196 | add_count = bytelen - clen; |
| 5197 | } |
| 5198 | } |
| 5199 | break; |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5200 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5201 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5202 | case NFA_BOL: |
| 5203 | if (reginput == regline) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5204 | { |
| 5205 | add_here = TRUE; |
| 5206 | add_state = t->state->out; |
| 5207 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5208 | break; |
| 5209 | |
| 5210 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5211 | if (curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5212 | { |
| 5213 | add_here = TRUE; |
| 5214 | add_state = t->state->out; |
| 5215 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5216 | break; |
| 5217 | |
| 5218 | case NFA_BOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5219 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5220 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5221 | if (curc == NUL) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5222 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5223 | #ifdef FEAT_MBYTE |
| 5224 | else if (has_mbyte) |
| 5225 | { |
| 5226 | int this_class; |
| 5227 | |
| 5228 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5229 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5230 | if (this_class <= 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5231 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5232 | else if (reg_prev_class() == this_class) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5233 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5234 | } |
| 5235 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5236 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5237 | || (reginput > regline |
| 5238 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5239 | result = FALSE; |
| 5240 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5241 | { |
| 5242 | add_here = TRUE; |
| 5243 | add_state = t->state->out; |
| 5244 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5245 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5246 | |
| 5247 | case NFA_EOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5248 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5249 | if (reginput == regline) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5250 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5251 | #ifdef FEAT_MBYTE |
| 5252 | else if (has_mbyte) |
| 5253 | { |
| 5254 | int this_class, prev_class; |
| 5255 | |
| 5256 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5257 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5258 | prev_class = reg_prev_class(); |
| 5259 | if (this_class == prev_class |
| 5260 | || prev_class == 0 || prev_class == 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5261 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5262 | } |
| 5263 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5264 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5265 | || (reginput[0] != NUL |
| 5266 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5267 | result = FALSE; |
| 5268 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5269 | { |
| 5270 | add_here = TRUE; |
| 5271 | add_state = t->state->out; |
| 5272 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5273 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5274 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5275 | case NFA_BOF: |
| 5276 | if (reglnum == 0 && reginput == regline |
| 5277 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5278 | { |
| 5279 | add_here = TRUE; |
| 5280 | add_state = t->state->out; |
| 5281 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5282 | break; |
| 5283 | |
| 5284 | case NFA_EOF: |
| 5285 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5286 | { |
| 5287 | add_here = TRUE; |
| 5288 | add_state = t->state->out; |
| 5289 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5290 | break; |
| 5291 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5292 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5293 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5294 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5295 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5296 | int len = 0; |
| 5297 | nfa_state_T *end; |
| 5298 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5299 | int cchars[MAX_MCO]; |
| 5300 | int ccount = 0; |
| 5301 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5302 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5303 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5304 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5305 | if (utf_iscomposing(sta->c)) |
| 5306 | { |
| 5307 | /* Only match composing character(s), ignore base |
| 5308 | * character. Used for ".{composing}" and "{composing}" |
| 5309 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5310 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5311 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 5312 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5313 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5314 | /* If \Z was present, then ignore composing characters. |
| 5315 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5316 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5317 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5318 | else |
| 5319 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5320 | while (sta->c != NFA_END_COMPOSING) |
| 5321 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5322 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5323 | |
| 5324 | /* Check base character matches first, unless ignored. */ |
| 5325 | else if (len > 0 || mc == sta->c) |
| 5326 | { |
| 5327 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5328 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5329 | len += mb_char2len(mc); |
| 5330 | sta = sta->out; |
| 5331 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5332 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5333 | /* We don't care about the order of composing characters. |
| 5334 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5335 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5336 | { |
| 5337 | mc = mb_ptr2char(reginput + len); |
| 5338 | cchars[ccount++] = mc; |
| 5339 | len += mb_char2len(mc); |
| 5340 | if (ccount == MAX_MCO) |
| 5341 | break; |
| 5342 | } |
| 5343 | |
| 5344 | /* Check that each composing char in the pattern matches a |
| 5345 | * composing char in the text. We do not check if all |
| 5346 | * composing chars are matched. */ |
| 5347 | result = OK; |
| 5348 | while (sta->c != NFA_END_COMPOSING) |
| 5349 | { |
| 5350 | for (j = 0; j < ccount; ++j) |
| 5351 | if (cchars[j] == sta->c) |
| 5352 | break; |
| 5353 | if (j == ccount) |
| 5354 | { |
| 5355 | result = FAIL; |
| 5356 | break; |
| 5357 | } |
| 5358 | sta = sta->out; |
| 5359 | } |
| 5360 | } |
| 5361 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 5362 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5363 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5364 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5365 | ADD_STATE_IF_MATCH(end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5366 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5367 | } |
| 5368 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5369 | |
| 5370 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5371 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 5372 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5373 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5374 | go_to_nextline = TRUE; |
| 5375 | /* Pass -1 for the offset, which means taking the position |
| 5376 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5377 | add_state = t->state->out; |
| 5378 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5379 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5380 | else if (curc == '\n' && reg_line_lbr) |
| 5381 | { |
| 5382 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5383 | add_state = t->state->out; |
| 5384 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5385 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5386 | break; |
| 5387 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5388 | case NFA_START_COLL: |
| 5389 | case NFA_START_NEG_COLL: |
| 5390 | { |
| 5391 | /* What follows is a list of characters, until NFA_END_COLL. |
| 5392 | * One of them must match or none of them must match. */ |
| 5393 | nfa_state_T *state; |
| 5394 | int result_if_matched; |
| 5395 | int c1, c2; |
| 5396 | |
| 5397 | /* Never match EOL. If it's part of the collection it is added |
| 5398 | * as a separate state with an OR. */ |
| 5399 | if (curc == NUL) |
| 5400 | break; |
| 5401 | |
| 5402 | state = t->state->out; |
| 5403 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 5404 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5405 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5406 | if (state->c == NFA_END_COLL) |
| 5407 | { |
| 5408 | result = !result_if_matched; |
| 5409 | break; |
| 5410 | } |
| 5411 | if (state->c == NFA_RANGE_MIN) |
| 5412 | { |
| 5413 | c1 = state->val; |
| 5414 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 5415 | c2 = state->val; |
| 5416 | #ifdef ENABLE_LOG |
| 5417 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 5418 | curc, c1, c2); |
| 5419 | #endif |
| 5420 | if (curc >= c1 && curc <= c2) |
| 5421 | { |
| 5422 | result = result_if_matched; |
| 5423 | break; |
| 5424 | } |
| 5425 | if (ireg_ic) |
| 5426 | { |
| 5427 | int curc_low = MB_TOLOWER(curc); |
| 5428 | int done = FALSE; |
| 5429 | |
| 5430 | for ( ; c1 <= c2; ++c1) |
| 5431 | if (MB_TOLOWER(c1) == curc_low) |
| 5432 | { |
| 5433 | result = result_if_matched; |
| 5434 | done = TRUE; |
| 5435 | break; |
| 5436 | } |
| 5437 | if (done) |
| 5438 | break; |
| 5439 | } |
| 5440 | } |
| 5441 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 5442 | : (curc == state->c |
| 5443 | || (ireg_ic && MB_TOLOWER(curc) |
| 5444 | == MB_TOLOWER(state->c)))) |
| 5445 | { |
| 5446 | result = result_if_matched; |
| 5447 | break; |
| 5448 | } |
| 5449 | state = state->out; |
| 5450 | } |
| 5451 | if (result) |
| 5452 | { |
| 5453 | /* next state is in out of the NFA_END_COLL, out1 of |
| 5454 | * START points to the END state */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5455 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5456 | add_off = clen; |
| 5457 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5458 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5459 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5460 | |
| 5461 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5462 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5463 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5464 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5465 | add_state = t->state->out; |
| 5466 | add_off = clen; |
| 5467 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5468 | break; |
| 5469 | |
| 5470 | /* |
| 5471 | * Character classes like \a for alpha, \d for digit etc. |
| 5472 | */ |
| 5473 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5474 | result = vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5475 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5476 | break; |
| 5477 | |
| 5478 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5479 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5480 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5481 | break; |
| 5482 | |
| 5483 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5484 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5485 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5486 | break; |
| 5487 | |
| 5488 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5489 | result = !VIM_ISDIGIT(curc) |
| 5490 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5491 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5492 | break; |
| 5493 | |
| 5494 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5495 | result = vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5496 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5497 | break; |
| 5498 | |
| 5499 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5500 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5501 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5502 | break; |
| 5503 | |
| 5504 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | 0805049 | 2013-05-21 12:43:56 +0200 | [diff] [blame] | 5505 | result = ptr2cells(reginput) == 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5506 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5507 | break; |
| 5508 | |
| 5509 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5510 | result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5511 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5512 | break; |
| 5513 | |
| 5514 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5515 | result = vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5516 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5517 | break; |
| 5518 | |
| 5519 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5520 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5521 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5522 | break; |
| 5523 | |
| 5524 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5525 | result = ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5526 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5527 | break; |
| 5528 | |
| 5529 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5530 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5531 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5532 | break; |
| 5533 | |
| 5534 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5535 | result = ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5536 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5537 | break; |
| 5538 | |
| 5539 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5540 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5541 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5542 | break; |
| 5543 | |
| 5544 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5545 | result = ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5546 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5547 | break; |
| 5548 | |
| 5549 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5550 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5551 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5552 | break; |
| 5553 | |
| 5554 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5555 | result = ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5556 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5557 | break; |
| 5558 | |
| 5559 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5560 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5561 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5562 | break; |
| 5563 | |
| 5564 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5565 | result = ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5566 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5567 | break; |
| 5568 | |
| 5569 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5570 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5571 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5572 | break; |
| 5573 | |
| 5574 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5575 | result = ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5576 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5577 | break; |
| 5578 | |
| 5579 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5580 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5581 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5582 | break; |
| 5583 | |
| 5584 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5585 | result = ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5586 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5587 | break; |
| 5588 | |
| 5589 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5590 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5591 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5592 | break; |
| 5593 | |
| 5594 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5595 | result = ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5596 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5597 | break; |
| 5598 | |
| 5599 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5600 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5601 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5602 | break; |
| 5603 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5604 | case NFA_BACKREF1: |
| 5605 | case NFA_BACKREF2: |
| 5606 | case NFA_BACKREF3: |
| 5607 | case NFA_BACKREF4: |
| 5608 | case NFA_BACKREF5: |
| 5609 | case NFA_BACKREF6: |
| 5610 | case NFA_BACKREF7: |
| 5611 | case NFA_BACKREF8: |
| 5612 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5613 | #ifdef FEAT_SYN_HL |
| 5614 | case NFA_ZREF1: |
| 5615 | case NFA_ZREF2: |
| 5616 | case NFA_ZREF3: |
| 5617 | case NFA_ZREF4: |
| 5618 | case NFA_ZREF5: |
| 5619 | case NFA_ZREF6: |
| 5620 | case NFA_ZREF7: |
| 5621 | case NFA_ZREF8: |
| 5622 | case NFA_ZREF9: |
| 5623 | #endif |
| 5624 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5625 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5626 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5627 | int bytelen; |
| 5628 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5629 | if (t->state->c <= NFA_BACKREF9) |
| 5630 | { |
| 5631 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 5632 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 5633 | } |
| 5634 | #ifdef FEAT_SYN_HL |
| 5635 | else |
| 5636 | { |
| 5637 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 5638 | result = match_zref(subidx, &bytelen); |
| 5639 | } |
| 5640 | #endif |
| 5641 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5642 | if (result) |
| 5643 | { |
| 5644 | if (bytelen == 0) |
| 5645 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 5646 | /* empty match always works, output of NFA_SKIP to be |
| 5647 | * used next */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5648 | add_here = TRUE; |
| 5649 | add_state = t->state->out->out; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5650 | } |
| 5651 | else if (bytelen <= clen) |
| 5652 | { |
| 5653 | /* match current character, jump ahead to out of |
| 5654 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5655 | add_state = t->state->out->out; |
| 5656 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5657 | } |
| 5658 | else |
| 5659 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 5660 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5661 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5662 | add_state = t->state->out; |
| 5663 | add_off = bytelen; |
| 5664 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5665 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5666 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5667 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5668 | } |
| 5669 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 5670 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5671 | if (t->count - clen <= 0) |
| 5672 | { |
| 5673 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5674 | add_state = t->state->out; |
| 5675 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5676 | } |
| 5677 | else |
| 5678 | { |
| 5679 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5680 | add_state = t->state; |
| 5681 | add_off = 0; |
| 5682 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5683 | } |
| 5684 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5685 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5686 | case NFA_LNUM: |
| 5687 | case NFA_LNUM_GT: |
| 5688 | case NFA_LNUM_LT: |
| 5689 | result = (REG_MULTI && |
| 5690 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 5691 | (long_u)(reglnum + reg_firstlnum))); |
| 5692 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5693 | { |
| 5694 | add_here = TRUE; |
| 5695 | add_state = t->state->out; |
| 5696 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5697 | break; |
| 5698 | |
| 5699 | case NFA_COL: |
| 5700 | case NFA_COL_GT: |
| 5701 | case NFA_COL_LT: |
| 5702 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 5703 | (long_u)(reginput - regline) + 1); |
| 5704 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5705 | { |
| 5706 | add_here = TRUE; |
| 5707 | add_state = t->state->out; |
| 5708 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5709 | break; |
| 5710 | |
| 5711 | case NFA_VCOL: |
| 5712 | case NFA_VCOL_GT: |
| 5713 | case NFA_VCOL_LT: |
| 5714 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 5715 | (long_u)win_linetabsize( |
| 5716 | reg_win == NULL ? curwin : reg_win, |
| 5717 | regline, (colnr_T)(reginput - regline)) + 1); |
| 5718 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5719 | { |
| 5720 | add_here = TRUE; |
| 5721 | add_state = t->state->out; |
| 5722 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5723 | break; |
| 5724 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 5725 | case NFA_MARK: |
| 5726 | case NFA_MARK_GT: |
| 5727 | case NFA_MARK_LT: |
| 5728 | { |
| 5729 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 5730 | |
| 5731 | /* Compare the mark position to the match position. */ |
| 5732 | result = (pos != NULL /* mark doesn't exist */ |
| 5733 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 5734 | && (pos->lnum == reglnum + reg_firstlnum |
| 5735 | ? (pos->col == (colnr_T)(reginput - regline) |
| 5736 | ? t->state->c == NFA_MARK |
| 5737 | : (pos->col < (colnr_T)(reginput - regline) |
| 5738 | ? t->state->c == NFA_MARK_GT |
| 5739 | : t->state->c == NFA_MARK_LT)) |
| 5740 | : (pos->lnum < reglnum + reg_firstlnum |
| 5741 | ? t->state->c == NFA_MARK_GT |
| 5742 | : t->state->c == NFA_MARK_LT))); |
| 5743 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5744 | { |
| 5745 | add_here = TRUE; |
| 5746 | add_state = t->state->out; |
| 5747 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 5748 | break; |
| 5749 | } |
| 5750 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5751 | case NFA_CURSOR: |
| 5752 | result = (reg_win != NULL |
| 5753 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 5754 | && ((colnr_T)(reginput - regline) |
| 5755 | == reg_win->w_cursor.col)); |
| 5756 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5757 | { |
| 5758 | add_here = TRUE; |
| 5759 | add_state = t->state->out; |
| 5760 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5761 | break; |
| 5762 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5763 | case NFA_VISUAL: |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5764 | #ifdef FEAT_VISUAL |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5765 | result = reg_match_visual(); |
| 5766 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5767 | { |
| 5768 | add_here = TRUE; |
| 5769 | add_state = t->state->out; |
| 5770 | } |
Bram Moolenaar | 78eae9a | 2013-06-05 11:02:05 +0200 | [diff] [blame] | 5771 | #endif |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5772 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5773 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5774 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5775 | { |
| 5776 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5777 | |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5778 | /* TODO: put this in #ifdef later */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5779 | if (c < 0) |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5780 | EMSGN("INTERNAL: Negative state char: %ld", c); |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5781 | result = (c == curc); |
| 5782 | |
| 5783 | if (!result && ireg_ic) |
| 5784 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5785 | #ifdef FEAT_MBYTE |
| 5786 | /* If there is a composing character which is not being |
| 5787 | * ignored there can be no match. Match with composing |
| 5788 | * character uses NFA_COMPOSING above. */ |
| 5789 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5790 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5791 | result = FALSE; |
| 5792 | #endif |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5793 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5794 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5795 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5796 | |
| 5797 | } /* switch (t->state->c) */ |
| 5798 | |
| 5799 | if (add_state != NULL) |
| 5800 | { |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame^] | 5801 | /* Handle the postponed invisible match before advancing to |
| 5802 | * the next character and for a zero-width match if the match |
| 5803 | * might end without advancing. */ |
| 5804 | if (t->pim != NULL && (!add_here || match_follows(add_state, 0))) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5805 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5806 | if (t->pim->result == NFA_PIM_TODO) |
| 5807 | { |
| 5808 | #ifdef ENABLE_LOG |
| 5809 | fprintf(log_fd, "\n"); |
| 5810 | fprintf(log_fd, "==================================\n"); |
| 5811 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 5812 | fprintf(log_fd, "\n"); |
| 5813 | #endif |
| 5814 | result = recursive_regmatch(t->pim->state, |
| 5815 | prog, submatch, m, &listids); |
| 5816 | t->pim->result = result ? NFA_PIM_MATCH |
| 5817 | : NFA_PIM_NOMATCH; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5818 | /* for \@! and \@<! it is a match when the result is |
| 5819 | * FALSE */ |
| 5820 | if (result != (t->pim->state->c |
| 5821 | == NFA_START_INVISIBLE_NEG |
| 5822 | || t->pim->state->c |
| 5823 | == NFA_START_INVISIBLE_BEFORE_NEG)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5824 | { |
| 5825 | /* Copy submatch info from the recursive call */ |
| 5826 | copy_sub_off(&t->pim->subs.norm, &m->norm); |
| 5827 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5828 | if (nfa_has_zsubexpr) |
| 5829 | copy_sub_off(&t->pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5830 | #endif |
| 5831 | } |
| 5832 | } |
| 5833 | else |
| 5834 | { |
| 5835 | result = (t->pim->result == NFA_PIM_MATCH); |
| 5836 | #ifdef ENABLE_LOG |
| 5837 | fprintf(log_fd, "\n"); |
| 5838 | fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", t->pim->result); |
| 5839 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5840 | fprintf(log_fd, "\n"); |
| 5841 | #endif |
| 5842 | } |
| 5843 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5844 | /* for \@! and \@<! it is a match when result is FALSE */ |
| 5845 | if (result != (t->pim->state->c == NFA_START_INVISIBLE_NEG |
| 5846 | || t->pim->state->c |
| 5847 | == NFA_START_INVISIBLE_BEFORE_NEG)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5848 | { |
| 5849 | /* Copy submatch info from the recursive call */ |
| 5850 | copy_sub_off(&t->subs.norm, &t->pim->subs.norm); |
| 5851 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5852 | if (nfa_has_zsubexpr) |
| 5853 | copy_sub_off(&t->subs.synt, &t->pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5854 | #endif |
| 5855 | } |
| 5856 | else |
| 5857 | /* look-behind match failed, don't add the state */ |
| 5858 | continue; |
| 5859 | } |
| 5860 | |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5861 | if (add_here) |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame^] | 5862 | addstate_here(thislist, add_state, &t->subs, t->pim, &listidx); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5863 | else |
| 5864 | { |
| 5865 | addstate(nextlist, add_state, &t->subs, add_off); |
| 5866 | if (add_count > 0) |
| 5867 | nextlist->t[nextlist->n - 1].count = add_count; |
| 5868 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 5872 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 5873 | /* Look for the start of a match in the current position by adding the |
| 5874 | * start state to the list of states. |
| 5875 | * The first found match is the leftmost one, thus the order of states |
| 5876 | * matters! |
| 5877 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 5878 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5879 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 5880 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5881 | if (nfa_match == FALSE |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5882 | && ((toplevel |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5883 | && reglnum == 0 |
| 5884 | && clen != 0 |
| 5885 | && (ireg_maxcol == 0 |
| 5886 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5887 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5888 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5889 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 5890 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5891 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5892 | < nfa_endp->se_u.pos.col)) |
| 5893 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5894 | { |
| 5895 | #ifdef ENABLE_LOG |
| 5896 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 5897 | #endif |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5898 | /* Inline optimized code for addstate() if we know the state is |
| 5899 | * the first MOPEN. */ |
| 5900 | if (toplevel) |
| 5901 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5902 | int add = TRUE; |
| 5903 | int c; |
| 5904 | |
| 5905 | if (prog->regstart != NUL && clen != 0) |
| 5906 | { |
| 5907 | if (nextlist->n == 0) |
| 5908 | { |
| 5909 | colnr_T col = (colnr_T)(reginput - regline) + clen; |
| 5910 | |
| 5911 | /* Nextlist is empty, we can skip ahead to the |
| 5912 | * character that must appear at the start. */ |
| 5913 | if (skip_to_start(prog->regstart, &col) == FAIL) |
| 5914 | break; |
| 5915 | #ifdef ENABLE_LOG |
| 5916 | fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
| 5917 | col - ((colnr_T)(reginput - regline) + clen)); |
| 5918 | #endif |
| 5919 | reginput = regline + col - clen; |
| 5920 | } |
| 5921 | else |
| 5922 | { |
| 5923 | /* Checking if the required start character matches is |
| 5924 | * cheaper than adding a state that won't match. */ |
| 5925 | c = PTR2CHAR(reginput + clen); |
| 5926 | if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c) |
| 5927 | != MB_TOLOWER(prog->regstart))) |
| 5928 | { |
| 5929 | #ifdef ENABLE_LOG |
| 5930 | fprintf(log_fd, " Skipping start state, regstart does not match\n"); |
| 5931 | #endif |
| 5932 | add = FALSE; |
| 5933 | } |
| 5934 | } |
| 5935 | } |
| 5936 | |
| 5937 | if (add) |
| 5938 | { |
| 5939 | if (REG_MULTI) |
| 5940 | m->norm.list.multi[0].start.col = |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5941 | (colnr_T)(reginput - regline) + clen; |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5942 | else |
| 5943 | m->norm.list.line[0].start = reginput + clen; |
| 5944 | addstate(nextlist, start->out, m, clen); |
| 5945 | } |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5946 | } |
| 5947 | else |
| 5948 | addstate(nextlist, start, m, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5949 | } |
| 5950 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5951 | #ifdef ENABLE_LOG |
| 5952 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5953 | { |
| 5954 | int i; |
| 5955 | |
| 5956 | for (i = 0; i < thislist->n; i++) |
| 5957 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5958 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5959 | fprintf(log_fd, "\n"); |
| 5960 | #endif |
| 5961 | |
| 5962 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5963 | /* Advance to the next character, or advance to the next line, or |
| 5964 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5965 | if (clen != 0) |
| 5966 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5967 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 5968 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5969 | reg_nextline(); |
| 5970 | else |
| 5971 | break; |
| 5972 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5973 | |
| 5974 | #ifdef ENABLE_LOG |
| 5975 | if (log_fd != stderr) |
| 5976 | fclose(log_fd); |
| 5977 | log_fd = NULL; |
| 5978 | #endif |
| 5979 | |
| 5980 | theend: |
| 5981 | /* Free memory */ |
| 5982 | vim_free(list[0].t); |
| 5983 | vim_free(list[1].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5984 | vim_free(listids); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5985 | ga_clear(&pimlist); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5986 | #undef ADD_STATE_IF_MATCH |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5987 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5988 | fclose(debug); |
| 5989 | #endif |
| 5990 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5991 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | /* |
| 5995 | * Try match of "prog" with at regline["col"]. |
| 5996 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 5997 | */ |
| 5998 | static long |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5999 | nfa_regtry(prog, col) |
| 6000 | nfa_regprog_T *prog; |
| 6001 | colnr_T col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6002 | { |
| 6003 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6004 | regsubs_T subs, m; |
| 6005 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6006 | #ifdef ENABLE_LOG |
| 6007 | FILE *f; |
| 6008 | #endif |
| 6009 | |
| 6010 | reginput = regline + col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6011 | |
| 6012 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6013 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6014 | if (f != NULL) |
| 6015 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6016 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6017 | #ifdef DEBUG |
| 6018 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 6019 | #endif |
| 6020 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6021 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 6022 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6023 | fprintf(f, "\n\n"); |
| 6024 | fclose(f); |
| 6025 | } |
| 6026 | else |
| 6027 | EMSG(_("Could not open temporary log file for writing ")); |
| 6028 | #endif |
| 6029 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6030 | clear_sub(&subs.norm); |
| 6031 | clear_sub(&m.norm); |
| 6032 | #ifdef FEAT_SYN_HL |
| 6033 | clear_sub(&subs.synt); |
| 6034 | clear_sub(&m.synt); |
| 6035 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6036 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 6037 | if (nfa_regmatch(prog, start, &subs, &m) == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6038 | return 0; |
| 6039 | |
| 6040 | cleanup_subexpr(); |
| 6041 | if (REG_MULTI) |
| 6042 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6043 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6044 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6045 | reg_startpos[i] = subs.norm.list.multi[i].start; |
| 6046 | reg_endpos[i] = subs.norm.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | if (reg_startpos[0].lnum < 0) |
| 6050 | { |
| 6051 | reg_startpos[0].lnum = 0; |
| 6052 | reg_startpos[0].col = col; |
| 6053 | } |
| 6054 | if (reg_endpos[0].lnum < 0) |
| 6055 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 6056 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6057 | reg_endpos[0].lnum = reglnum; |
| 6058 | reg_endpos[0].col = (int)(reginput - regline); |
| 6059 | } |
| 6060 | else |
| 6061 | /* Use line number of "\ze". */ |
| 6062 | reglnum = reg_endpos[0].lnum; |
| 6063 | } |
| 6064 | else |
| 6065 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6066 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6067 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6068 | reg_startp[i] = subs.norm.list.line[i].start; |
| 6069 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | if (reg_startp[0] == NULL) |
| 6073 | reg_startp[0] = regline + col; |
| 6074 | if (reg_endp[0] == NULL) |
| 6075 | reg_endp[0] = reginput; |
| 6076 | } |
| 6077 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6078 | #ifdef FEAT_SYN_HL |
| 6079 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 6080 | unref_extmatch(re_extmatch_out); |
| 6081 | re_extmatch_out = NULL; |
| 6082 | |
| 6083 | if (prog->reghasz == REX_SET) |
| 6084 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6085 | cleanup_zsubexpr(); |
| 6086 | re_extmatch_out = make_extmatch(); |
| 6087 | for (i = 0; i < subs.synt.in_use; i++) |
| 6088 | { |
| 6089 | if (REG_MULTI) |
| 6090 | { |
| 6091 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 6092 | |
| 6093 | /* Only accept single line matches. */ |
| 6094 | if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum) |
| 6095 | re_extmatch_out->matches[i] = |
| 6096 | vim_strnsave(reg_getline(mpos->start.lnum) |
| 6097 | + mpos->start.col, |
| 6098 | mpos->end.col - mpos->start.col); |
| 6099 | } |
| 6100 | else |
| 6101 | { |
| 6102 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 6103 | |
| 6104 | if (lpos->start != NULL && lpos->end != NULL) |
| 6105 | re_extmatch_out->matches[i] = |
| 6106 | vim_strnsave(lpos->start, |
| 6107 | (int)(lpos->end - lpos->start)); |
| 6108 | } |
| 6109 | } |
| 6110 | } |
| 6111 | #endif |
| 6112 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6113 | return 1 + reglnum; |
| 6114 | } |
| 6115 | |
| 6116 | /* |
| 6117 | * Match a regexp against a string ("line" points to the string) or multiple |
| 6118 | * lines ("line" is NULL, use reg_getline()). |
| 6119 | * |
| 6120 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6121 | */ |
| 6122 | static long |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6123 | nfa_regexec_both(line, startcol) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6124 | char_u *line; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6125 | colnr_T startcol; /* column to start looking for match */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6126 | { |
| 6127 | nfa_regprog_T *prog; |
| 6128 | long retval = 0L; |
| 6129 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6130 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6131 | |
| 6132 | if (REG_MULTI) |
| 6133 | { |
| 6134 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 6135 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 6136 | reg_startpos = reg_mmatch->startpos; |
| 6137 | reg_endpos = reg_mmatch->endpos; |
| 6138 | } |
| 6139 | else |
| 6140 | { |
| 6141 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 6142 | reg_startp = reg_match->startp; |
| 6143 | reg_endp = reg_match->endp; |
| 6144 | } |
| 6145 | |
| 6146 | /* Be paranoid... */ |
| 6147 | if (prog == NULL || line == NULL) |
| 6148 | { |
| 6149 | EMSG(_(e_null)); |
| 6150 | goto theend; |
| 6151 | } |
| 6152 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6153 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 6154 | if (prog->regflags & RF_ICASE) |
| 6155 | ireg_ic = TRUE; |
| 6156 | else if (prog->regflags & RF_NOICASE) |
| 6157 | ireg_ic = FALSE; |
| 6158 | |
| 6159 | #ifdef FEAT_MBYTE |
| 6160 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 6161 | if (prog->regflags & RF_ICOMBINE) |
| 6162 | ireg_icombine = TRUE; |
| 6163 | #endif |
| 6164 | |
| 6165 | regline = line; |
| 6166 | reglnum = 0; /* relative to line */ |
| 6167 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6168 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6169 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6170 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6171 | nfa_listid = 1; |
| 6172 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6173 | #ifdef DEBUG |
| 6174 | nfa_regengine.expr = prog->pattern; |
| 6175 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6176 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6177 | if (prog->reganch && col > 0) |
| 6178 | return 0L; |
| 6179 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6180 | need_clear_subexpr = TRUE; |
| 6181 | #ifdef FEAT_SYN_HL |
| 6182 | /* Clear the external match subpointers if necessary. */ |
| 6183 | if (prog->reghasz == REX_SET) |
| 6184 | { |
| 6185 | nfa_has_zsubexpr = TRUE; |
| 6186 | need_clear_zsubexpr = TRUE; |
| 6187 | } |
| 6188 | else |
| 6189 | nfa_has_zsubexpr = FALSE; |
| 6190 | #endif |
| 6191 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6192 | if (prog->regstart != NUL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6193 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6194 | /* Skip ahead until a character we know the match must start with. |
| 6195 | * When there is none there is no match. */ |
| 6196 | if (skip_to_start(prog->regstart, &col) == FAIL) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6197 | return 0L; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6198 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6199 | /* If match_text is set it contains the full text that must match. |
| 6200 | * Nothing else to try. Doesn't handle combining chars well. */ |
| 6201 | if (prog->match_text != NULL && !ireg_icombine) |
| 6202 | return find_match_text(col, prog->regstart, prog->match_text); |
| 6203 | } |
| 6204 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6205 | /* If the start column is past the maximum column: no need to try. */ |
| 6206 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 6207 | goto theend; |
| 6208 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6209 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6210 | for (i = 0; i < nstate; ++i) |
| 6211 | { |
| 6212 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6213 | prog->state[i].lastlist[0] = 0; |
| 6214 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6215 | } |
| 6216 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6217 | retval = nfa_regtry(prog, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6218 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6219 | #ifdef DEBUG |
| 6220 | nfa_regengine.expr = NULL; |
| 6221 | #endif |
| 6222 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6223 | theend: |
| 6224 | return retval; |
| 6225 | } |
| 6226 | |
| 6227 | /* |
| 6228 | * Compile a regular expression into internal code for the NFA matcher. |
| 6229 | * Returns the program in allocated space. Returns NULL for an error. |
| 6230 | */ |
| 6231 | static regprog_T * |
| 6232 | nfa_regcomp(expr, re_flags) |
| 6233 | char_u *expr; |
| 6234 | int re_flags; |
| 6235 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6236 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 6237 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6238 | int *postfix; |
| 6239 | |
| 6240 | if (expr == NULL) |
| 6241 | return NULL; |
| 6242 | |
| 6243 | #ifdef DEBUG |
| 6244 | nfa_regengine.expr = expr; |
| 6245 | #endif |
| 6246 | |
| 6247 | init_class_tab(); |
| 6248 | |
| 6249 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 6250 | return NULL; |
| 6251 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6252 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6253 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6254 | postfix = re2post(); |
| 6255 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6256 | { |
| 6257 | /* TODO: only give this error for debugging? */ |
| 6258 | if (post_ptr >= post_end) |
| 6259 | 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] | 6260 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6261 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6262 | |
| 6263 | /* |
| 6264 | * In order to build the NFA, we parse the input regexp twice: |
| 6265 | * 1. first pass to count size (so we can allocate space) |
| 6266 | * 2. second to emit code |
| 6267 | */ |
| 6268 | #ifdef ENABLE_LOG |
| 6269 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6270 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6271 | |
| 6272 | if (f != NULL) |
| 6273 | { |
| 6274 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 6275 | fclose(f); |
| 6276 | } |
| 6277 | } |
| 6278 | #endif |
| 6279 | |
| 6280 | /* |
| 6281 | * PASS 1 |
| 6282 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 6283 | */ |
| 6284 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6285 | |
| 6286 | /* Space for compiled regexp */ |
| 6287 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate; |
| 6288 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 6289 | if (prog == NULL) |
| 6290 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6291 | state_ptr = prog->state; |
| 6292 | |
| 6293 | /* |
| 6294 | * PASS 2 |
| 6295 | * Build the NFA |
| 6296 | */ |
| 6297 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 6298 | if (prog->start == NULL) |
| 6299 | goto fail; |
| 6300 | |
| 6301 | prog->regflags = regflags; |
| 6302 | prog->engine = &nfa_regengine; |
| 6303 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6304 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6305 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6306 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6307 | |
| 6308 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 6309 | prog->regstart = nfa_get_regstart(prog->start, 0); |
| 6310 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6311 | prog->match_text = nfa_get_match_text(prog->start); |
| 6312 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6313 | #ifdef ENABLE_LOG |
| 6314 | nfa_postfix_dump(expr, OK); |
| 6315 | nfa_dump(prog); |
| 6316 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6317 | #ifdef FEAT_SYN_HL |
| 6318 | /* Remember whether this pattern has any \z specials in it. */ |
| 6319 | prog->reghasz = re_has_z; |
| 6320 | #endif |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6321 | #ifdef DEBUG |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6322 | prog->pattern = vim_strsave(expr); |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6323 | nfa_regengine.expr = NULL; |
| 6324 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6325 | |
| 6326 | out: |
| 6327 | vim_free(post_start); |
| 6328 | post_start = post_ptr = post_end = NULL; |
| 6329 | state_ptr = NULL; |
| 6330 | return (regprog_T *)prog; |
| 6331 | |
| 6332 | fail: |
| 6333 | vim_free(prog); |
| 6334 | prog = NULL; |
| 6335 | #ifdef ENABLE_LOG |
| 6336 | nfa_postfix_dump(expr, FAIL); |
| 6337 | #endif |
| 6338 | #ifdef DEBUG |
| 6339 | nfa_regengine.expr = NULL; |
| 6340 | #endif |
| 6341 | goto out; |
| 6342 | } |
| 6343 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6344 | /* |
| 6345 | * Free a compiled regexp program, returned by nfa_regcomp(). |
| 6346 | */ |
| 6347 | static void |
| 6348 | nfa_regfree(prog) |
| 6349 | regprog_T *prog; |
| 6350 | { |
| 6351 | if (prog != NULL) |
| 6352 | { |
| 6353 | vim_free(((nfa_regprog_T *)prog)->match_text); |
| 6354 | #ifdef DEBUG |
| 6355 | vim_free(((nfa_regprog_T *)prog)->pattern); |
| 6356 | #endif |
| 6357 | vim_free(prog); |
| 6358 | } |
| 6359 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6360 | |
| 6361 | /* |
| 6362 | * Match a regexp against a string. |
| 6363 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 6364 | * Uses curbuf for line count and 'iskeyword'. |
| 6365 | * |
| 6366 | * Return TRUE if there is a match, FALSE if not. |
| 6367 | */ |
| 6368 | static int |
| 6369 | nfa_regexec(rmp, line, col) |
| 6370 | regmatch_T *rmp; |
| 6371 | char_u *line; /* string to match against */ |
| 6372 | colnr_T col; /* column to start looking for match */ |
| 6373 | { |
| 6374 | reg_match = rmp; |
| 6375 | reg_mmatch = NULL; |
| 6376 | reg_maxline = 0; |
| 6377 | reg_line_lbr = FALSE; |
| 6378 | reg_buf = curbuf; |
| 6379 | reg_win = NULL; |
| 6380 | ireg_ic = rmp->rm_ic; |
| 6381 | #ifdef FEAT_MBYTE |
| 6382 | ireg_icombine = FALSE; |
| 6383 | #endif |
| 6384 | ireg_maxcol = 0; |
| 6385 | return (nfa_regexec_both(line, col) != 0); |
| 6386 | } |
| 6387 | |
| 6388 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 6389 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 6390 | |
| 6391 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 6392 | |
| 6393 | /* |
| 6394 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 6395 | */ |
| 6396 | static int |
| 6397 | nfa_regexec_nl(rmp, line, col) |
| 6398 | regmatch_T *rmp; |
| 6399 | char_u *line; /* string to match against */ |
| 6400 | colnr_T col; /* column to start looking for match */ |
| 6401 | { |
| 6402 | reg_match = rmp; |
| 6403 | reg_mmatch = NULL; |
| 6404 | reg_maxline = 0; |
| 6405 | reg_line_lbr = TRUE; |
| 6406 | reg_buf = curbuf; |
| 6407 | reg_win = NULL; |
| 6408 | ireg_ic = rmp->rm_ic; |
| 6409 | #ifdef FEAT_MBYTE |
| 6410 | ireg_icombine = FALSE; |
| 6411 | #endif |
| 6412 | ireg_maxcol = 0; |
| 6413 | return (nfa_regexec_both(line, col) != 0); |
| 6414 | } |
| 6415 | #endif |
| 6416 | |
| 6417 | |
| 6418 | /* |
| 6419 | * Match a regexp against multiple lines. |
| 6420 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 6421 | * Uses curbuf for line count and 'iskeyword'. |
| 6422 | * |
| 6423 | * Return zero if there is no match. Return number of lines contained in the |
| 6424 | * match otherwise. |
| 6425 | * |
| 6426 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 6427 | * |
| 6428 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 6429 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 6430 | * |
| 6431 | * +-------------------------+ |
| 6432 | * |a | |
| 6433 | * |b | |
| 6434 | * |c | |
| 6435 | * | | |
| 6436 | * +-------------------------+ |
| 6437 | * |
| 6438 | * then nfa_regexec_multi() returns 3. while the original |
| 6439 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 6440 | * |
| 6441 | * FIXME if this behavior is not compatible. |
| 6442 | */ |
| 6443 | static long |
| 6444 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 6445 | regmmatch_T *rmp; |
| 6446 | win_T *win; /* window in which to search or NULL */ |
| 6447 | buf_T *buf; /* buffer in which to search */ |
| 6448 | linenr_T lnum; /* nr of line to start looking for match */ |
| 6449 | colnr_T col; /* column to start looking for match */ |
| 6450 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 6451 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6452 | reg_match = NULL; |
| 6453 | reg_mmatch = rmp; |
| 6454 | reg_buf = buf; |
| 6455 | reg_win = win; |
| 6456 | reg_firstlnum = lnum; |
| 6457 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 6458 | reg_line_lbr = FALSE; |
| 6459 | ireg_ic = rmp->rmm_ic; |
| 6460 | #ifdef FEAT_MBYTE |
| 6461 | ireg_icombine = FALSE; |
| 6462 | #endif |
| 6463 | ireg_maxcol = rmp->rmm_maxcol; |
| 6464 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 6465 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6466 | } |
| 6467 | |
| 6468 | #ifdef DEBUG |
| 6469 | # undef ENABLE_LOG |
| 6470 | #endif |