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 | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 67 | NFA_START_INVISIBLE_FIRST, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 68 | NFA_START_INVISIBLE_NEG, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 69 | NFA_START_INVISIBLE_NEG_FIRST, |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 70 | NFA_START_INVISIBLE_BEFORE, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 71 | NFA_START_INVISIBLE_BEFORE_FIRST, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 72 | NFA_START_INVISIBLE_BEFORE_NEG, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 73 | NFA_START_INVISIBLE_BEFORE_NEG_FIRST, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 74 | NFA_START_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 75 | NFA_END_INVISIBLE, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 76 | NFA_END_INVISIBLE_NEG, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 77 | NFA_END_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 78 | NFA_COMPOSING, /* Next nodes in NFA are part of the |
| 79 | composing multibyte char */ |
| 80 | NFA_END_COMPOSING, /* End of a composing char in the NFA */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 81 | NFA_OPT_CHARS, /* \%[abc] */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 82 | |
| 83 | /* The following are used only in the postfix form, not in the NFA */ |
| 84 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 85 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 86 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 87 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 88 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 89 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 90 | NFA_BACKREF1, /* \1 */ |
| 91 | NFA_BACKREF2, /* \2 */ |
| 92 | NFA_BACKREF3, /* \3 */ |
| 93 | NFA_BACKREF4, /* \4 */ |
| 94 | NFA_BACKREF5, /* \5 */ |
| 95 | NFA_BACKREF6, /* \6 */ |
| 96 | NFA_BACKREF7, /* \7 */ |
| 97 | NFA_BACKREF8, /* \8 */ |
| 98 | NFA_BACKREF9, /* \9 */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 99 | #ifdef FEAT_SYN_HL |
| 100 | NFA_ZREF1, /* \z1 */ |
| 101 | NFA_ZREF2, /* \z2 */ |
| 102 | NFA_ZREF3, /* \z3 */ |
| 103 | NFA_ZREF4, /* \z4 */ |
| 104 | NFA_ZREF5, /* \z5 */ |
| 105 | NFA_ZREF6, /* \z6 */ |
| 106 | NFA_ZREF7, /* \z7 */ |
| 107 | NFA_ZREF8, /* \z8 */ |
| 108 | NFA_ZREF9, /* \z9 */ |
| 109 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 110 | NFA_SKIP, /* Skip characters */ |
| 111 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 112 | NFA_MOPEN, |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 113 | NFA_MOPEN1, |
| 114 | NFA_MOPEN2, |
| 115 | NFA_MOPEN3, |
| 116 | NFA_MOPEN4, |
| 117 | NFA_MOPEN5, |
| 118 | NFA_MOPEN6, |
| 119 | NFA_MOPEN7, |
| 120 | NFA_MOPEN8, |
| 121 | NFA_MOPEN9, |
| 122 | |
| 123 | NFA_MCLOSE, |
| 124 | NFA_MCLOSE1, |
| 125 | NFA_MCLOSE2, |
| 126 | NFA_MCLOSE3, |
| 127 | NFA_MCLOSE4, |
| 128 | NFA_MCLOSE5, |
| 129 | NFA_MCLOSE6, |
| 130 | NFA_MCLOSE7, |
| 131 | NFA_MCLOSE8, |
| 132 | NFA_MCLOSE9, |
| 133 | |
| 134 | #ifdef FEAT_SYN_HL |
| 135 | NFA_ZOPEN, |
| 136 | NFA_ZOPEN1, |
| 137 | NFA_ZOPEN2, |
| 138 | NFA_ZOPEN3, |
| 139 | NFA_ZOPEN4, |
| 140 | NFA_ZOPEN5, |
| 141 | NFA_ZOPEN6, |
| 142 | NFA_ZOPEN7, |
| 143 | NFA_ZOPEN8, |
| 144 | NFA_ZOPEN9, |
| 145 | |
| 146 | NFA_ZCLOSE, |
| 147 | NFA_ZCLOSE1, |
| 148 | NFA_ZCLOSE2, |
| 149 | NFA_ZCLOSE3, |
| 150 | NFA_ZCLOSE4, |
| 151 | NFA_ZCLOSE5, |
| 152 | NFA_ZCLOSE6, |
| 153 | NFA_ZCLOSE7, |
| 154 | NFA_ZCLOSE8, |
| 155 | NFA_ZCLOSE9, |
| 156 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 157 | |
| 158 | /* NFA_FIRST_NL */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 159 | NFA_ANY, /* Match any one character. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 160 | NFA_IDENT, /* Match identifier char */ |
| 161 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 162 | NFA_KWORD, /* Match keyword char */ |
| 163 | NFA_SKWORD, /* Match word char but no digit */ |
| 164 | NFA_FNAME, /* Match file name char */ |
| 165 | NFA_SFNAME, /* Match file name char but no digit */ |
| 166 | NFA_PRINT, /* Match printable char */ |
| 167 | NFA_SPRINT, /* Match printable char but no digit */ |
| 168 | NFA_WHITE, /* Match whitespace char */ |
| 169 | NFA_NWHITE, /* Match non-whitespace char */ |
| 170 | NFA_DIGIT, /* Match digit char */ |
| 171 | NFA_NDIGIT, /* Match non-digit char */ |
| 172 | NFA_HEX, /* Match hex char */ |
| 173 | NFA_NHEX, /* Match non-hex char */ |
| 174 | NFA_OCTAL, /* Match octal char */ |
| 175 | NFA_NOCTAL, /* Match non-octal char */ |
| 176 | NFA_WORD, /* Match word char */ |
| 177 | NFA_NWORD, /* Match non-word char */ |
| 178 | NFA_HEAD, /* Match head char */ |
| 179 | NFA_NHEAD, /* Match non-head char */ |
| 180 | NFA_ALPHA, /* Match alpha char */ |
| 181 | NFA_NALPHA, /* Match non-alpha char */ |
| 182 | NFA_LOWER, /* Match lowercase char */ |
| 183 | NFA_NLOWER, /* Match non-lowercase char */ |
| 184 | NFA_UPPER, /* Match uppercase char */ |
| 185 | NFA_NUPPER, /* Match non-uppercase char */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 186 | |
| 187 | NFA_CURSOR, /* Match cursor pos */ |
| 188 | NFA_LNUM, /* Match line number */ |
| 189 | NFA_LNUM_GT, /* Match > line number */ |
| 190 | NFA_LNUM_LT, /* Match < line number */ |
| 191 | NFA_COL, /* Match cursor column */ |
| 192 | NFA_COL_GT, /* Match > cursor column */ |
| 193 | NFA_COL_LT, /* Match < cursor column */ |
| 194 | NFA_VCOL, /* Match cursor virtual column */ |
| 195 | NFA_VCOL_GT, /* Match > cursor virtual column */ |
| 196 | NFA_VCOL_LT, /* Match < cursor virtual column */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 197 | NFA_MARK, /* Match mark */ |
| 198 | NFA_MARK_GT, /* Match > mark */ |
| 199 | NFA_MARK_LT, /* Match < mark */ |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 200 | NFA_VISUAL, /* Match Visual area */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 201 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 202 | NFA_FIRST_NL = NFA_ANY + ADD_NL, |
| 203 | NFA_LAST_NL = NFA_NUPPER + ADD_NL, |
| 204 | |
| 205 | /* Character classes [:alnum:] etc */ |
| 206 | NFA_CLASS_ALNUM, |
| 207 | NFA_CLASS_ALPHA, |
| 208 | NFA_CLASS_BLANK, |
| 209 | NFA_CLASS_CNTRL, |
| 210 | NFA_CLASS_DIGIT, |
| 211 | NFA_CLASS_GRAPH, |
| 212 | NFA_CLASS_LOWER, |
| 213 | NFA_CLASS_PRINT, |
| 214 | NFA_CLASS_PUNCT, |
| 215 | NFA_CLASS_SPACE, |
| 216 | NFA_CLASS_UPPER, |
| 217 | NFA_CLASS_XDIGIT, |
| 218 | NFA_CLASS_TAB, |
| 219 | NFA_CLASS_RETURN, |
| 220 | NFA_CLASS_BACKSPACE, |
| 221 | NFA_CLASS_ESCAPE |
| 222 | }; |
| 223 | |
| 224 | /* Keep in sync with classchars. */ |
| 225 | static int nfa_classcodes[] = { |
| 226 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 227 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 228 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 229 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 230 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 231 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 232 | NFA_UPPER, NFA_NUPPER |
| 233 | }; |
| 234 | |
| 235 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
| 236 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 237 | /* NFA regexp \ze operator encountered. */ |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 238 | static int nfa_has_zend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 239 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 240 | /* NFA regexp \1 .. \9 encountered. */ |
| 241 | static int nfa_has_backref; |
| 242 | |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 243 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 244 | /* NFA regexp has \z( ), set zsubexpr. */ |
| 245 | static int nfa_has_zsubexpr; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 246 | #endif |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 247 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 248 | /* Number of sub expressions actually being used during execution. 1 if only |
| 249 | * the whole match (subexpr 0) is used. */ |
| 250 | static int nfa_nsubexpr; |
| 251 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 252 | static int *post_start; /* holds the postfix form of r.e. */ |
| 253 | static int *post_end; |
| 254 | static int *post_ptr; |
| 255 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 256 | static int nstate; /* Number of states in the NFA. Also used when |
| 257 | * executing. */ |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 258 | static int istate; /* Index in the state vector, used in alloc_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 259 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 260 | /* If not NULL match must end at this position */ |
| 261 | static save_se_T *nfa_endp = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 262 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 263 | /* listid is global, so that it increases on recursive calls to |
| 264 | * nfa_regmatch(), which means we don't have to clear the lastlist field of |
| 265 | * all the states. */ |
| 266 | static int nfa_listid; |
| 267 | static int nfa_alt_listid; |
| 268 | |
| 269 | /* 0 for first call to nfa_regmatch(), 1 for recursive call. */ |
| 270 | static int nfa_ll_index = 0; |
| 271 | |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 272 | static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 273 | static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth)); |
| 274 | static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 275 | static char_u *nfa_get_match_text __ARGS((nfa_state_T *start)); |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 276 | static int realloc_post_list __ARGS((void)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 277 | 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] | 278 | static int nfa_emit_equi_class __ARGS((int c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 279 | static int nfa_regatom __ARGS((void)); |
| 280 | static int nfa_regpiece __ARGS((void)); |
| 281 | static int nfa_regconcat __ARGS((void)); |
| 282 | static int nfa_regbranch __ARGS((void)); |
| 283 | static int nfa_reg __ARGS((int paren)); |
| 284 | #ifdef DEBUG |
| 285 | static void nfa_set_code __ARGS((int c)); |
| 286 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 287 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 288 | 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] | 289 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 290 | #endif |
| 291 | static int *re2post __ARGS((void)); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 292 | 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] | 293 | static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size)); |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 294 | static void nfa_postprocess __ARGS((nfa_regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 295 | static int check_char_class __ARGS((int class, int c)); |
| 296 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 297 | static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list)); |
| 298 | static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 299 | 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] | 300 | static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 301 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 302 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 303 | static void nfa_regfree __ARGS((regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 304 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 305 | static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 306 | static int match_follows __ARGS((nfa_state_T *startstate, int depth)); |
| 307 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 308 | |
| 309 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 310 | #define EMIT(c) do { \ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 311 | if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 312 | return FAIL; \ |
| 313 | *post_ptr++ = c; \ |
| 314 | } while (0) |
| 315 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 316 | /* |
| 317 | * Initialize internal variables before NFA compilation. |
| 318 | * Return OK on success, FAIL otherwise. |
| 319 | */ |
| 320 | static int |
| 321 | nfa_regcomp_start(expr, re_flags) |
| 322 | char_u *expr; |
| 323 | int re_flags; /* see vim_regcomp() */ |
| 324 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 325 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 326 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 327 | |
| 328 | nstate = 0; |
| 329 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 330 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 331 | nstate_max = (int)(STRLEN(expr) + 1) * 25; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 332 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 333 | /* 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] | 334 | * When it is still not enough realloc_post_list() will be used. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 335 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 336 | |
| 337 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 338 | postfix_size = sizeof(int) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 339 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 340 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 341 | if (post_start == NULL) |
| 342 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 343 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 344 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 345 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 346 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 347 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 348 | /* shared with BT engine */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 349 | regcomp_start(expr, re_flags); |
| 350 | |
| 351 | return OK; |
| 352 | } |
| 353 | |
| 354 | /* |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 355 | * Figure out if the NFA state list starts with an anchor, must match at start |
| 356 | * of the line. |
| 357 | */ |
| 358 | static int |
| 359 | nfa_get_reganch(start, depth) |
| 360 | nfa_state_T *start; |
| 361 | int depth; |
| 362 | { |
| 363 | nfa_state_T *p = start; |
| 364 | |
| 365 | if (depth > 4) |
| 366 | return 0; |
| 367 | |
| 368 | while (p != NULL) |
| 369 | { |
| 370 | switch (p->c) |
| 371 | { |
| 372 | case NFA_BOL: |
| 373 | case NFA_BOF: |
| 374 | return 1; /* yes! */ |
| 375 | |
| 376 | case NFA_ZSTART: |
| 377 | case NFA_ZEND: |
| 378 | case NFA_CURSOR: |
| 379 | case NFA_VISUAL: |
| 380 | |
| 381 | case NFA_MOPEN: |
| 382 | case NFA_MOPEN1: |
| 383 | case NFA_MOPEN2: |
| 384 | case NFA_MOPEN3: |
| 385 | case NFA_MOPEN4: |
| 386 | case NFA_MOPEN5: |
| 387 | case NFA_MOPEN6: |
| 388 | case NFA_MOPEN7: |
| 389 | case NFA_MOPEN8: |
| 390 | case NFA_MOPEN9: |
| 391 | case NFA_NOPEN: |
| 392 | #ifdef FEAT_SYN_HL |
| 393 | case NFA_ZOPEN: |
| 394 | case NFA_ZOPEN1: |
| 395 | case NFA_ZOPEN2: |
| 396 | case NFA_ZOPEN3: |
| 397 | case NFA_ZOPEN4: |
| 398 | case NFA_ZOPEN5: |
| 399 | case NFA_ZOPEN6: |
| 400 | case NFA_ZOPEN7: |
| 401 | case NFA_ZOPEN8: |
| 402 | case NFA_ZOPEN9: |
| 403 | #endif |
| 404 | p = p->out; |
| 405 | break; |
| 406 | |
| 407 | case NFA_SPLIT: |
| 408 | return nfa_get_reganch(p->out, depth + 1) |
| 409 | && nfa_get_reganch(p->out1, depth + 1); |
| 410 | |
| 411 | default: |
| 412 | return 0; /* noooo */ |
| 413 | } |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * Figure out if the NFA state list starts with a character which must match |
| 420 | * at start of the match. |
| 421 | */ |
| 422 | static int |
| 423 | nfa_get_regstart(start, depth) |
| 424 | nfa_state_T *start; |
| 425 | int depth; |
| 426 | { |
| 427 | nfa_state_T *p = start; |
| 428 | |
| 429 | if (depth > 4) |
| 430 | return 0; |
| 431 | |
| 432 | while (p != NULL) |
| 433 | { |
| 434 | switch (p->c) |
| 435 | { |
| 436 | /* all kinds of zero-width matches */ |
| 437 | case NFA_BOL: |
| 438 | case NFA_BOF: |
| 439 | case NFA_BOW: |
| 440 | case NFA_EOW: |
| 441 | case NFA_ZSTART: |
| 442 | case NFA_ZEND: |
| 443 | case NFA_CURSOR: |
| 444 | case NFA_VISUAL: |
| 445 | case NFA_LNUM: |
| 446 | case NFA_LNUM_GT: |
| 447 | case NFA_LNUM_LT: |
| 448 | case NFA_COL: |
| 449 | case NFA_COL_GT: |
| 450 | case NFA_COL_LT: |
| 451 | case NFA_VCOL: |
| 452 | case NFA_VCOL_GT: |
| 453 | case NFA_VCOL_LT: |
| 454 | case NFA_MARK: |
| 455 | case NFA_MARK_GT: |
| 456 | case NFA_MARK_LT: |
| 457 | |
| 458 | case NFA_MOPEN: |
| 459 | case NFA_MOPEN1: |
| 460 | case NFA_MOPEN2: |
| 461 | case NFA_MOPEN3: |
| 462 | case NFA_MOPEN4: |
| 463 | case NFA_MOPEN5: |
| 464 | case NFA_MOPEN6: |
| 465 | case NFA_MOPEN7: |
| 466 | case NFA_MOPEN8: |
| 467 | case NFA_MOPEN9: |
| 468 | case NFA_NOPEN: |
| 469 | #ifdef FEAT_SYN_HL |
| 470 | case NFA_ZOPEN: |
| 471 | case NFA_ZOPEN1: |
| 472 | case NFA_ZOPEN2: |
| 473 | case NFA_ZOPEN3: |
| 474 | case NFA_ZOPEN4: |
| 475 | case NFA_ZOPEN5: |
| 476 | case NFA_ZOPEN6: |
| 477 | case NFA_ZOPEN7: |
| 478 | case NFA_ZOPEN8: |
| 479 | case NFA_ZOPEN9: |
| 480 | #endif |
| 481 | p = p->out; |
| 482 | break; |
| 483 | |
| 484 | case NFA_SPLIT: |
| 485 | { |
| 486 | int c1 = nfa_get_regstart(p->out, depth + 1); |
| 487 | int c2 = nfa_get_regstart(p->out1, depth + 1); |
| 488 | |
| 489 | if (c1 == c2) |
| 490 | return c1; /* yes! */ |
| 491 | return 0; |
| 492 | } |
| 493 | |
| 494 | default: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 495 | if (p->c > 0) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 496 | return p->c; /* yes! */ |
| 497 | return 0; |
| 498 | } |
| 499 | } |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 504 | * 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] | 505 | * else. If so return a string in allocated memory with what must match after |
| 506 | * regstart. Otherwise return NULL. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 507 | */ |
| 508 | static char_u * |
| 509 | nfa_get_match_text(start) |
| 510 | nfa_state_T *start; |
| 511 | { |
| 512 | nfa_state_T *p = start; |
| 513 | int len = 0; |
| 514 | char_u *ret; |
| 515 | char_u *s; |
| 516 | |
| 517 | if (p->c != NFA_MOPEN) |
| 518 | return NULL; /* just in case */ |
| 519 | p = p->out; |
| 520 | while (p->c > 0) |
| 521 | { |
| 522 | len += MB_CHAR2LEN(p->c); |
| 523 | p = p->out; |
| 524 | } |
| 525 | if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH) |
| 526 | return NULL; |
| 527 | |
| 528 | ret = alloc(len); |
| 529 | if (ret != NULL) |
| 530 | { |
| 531 | len = 0; |
| 532 | p = start->out->out; /* skip first char, it goes into regstart */ |
| 533 | s = ret; |
| 534 | while (p->c > 0) |
| 535 | { |
| 536 | #ifdef FEAT_MBYTE |
| 537 | if (has_mbyte) |
| 538 | s += (*mb_char2bytes)(p->c, s); |
| 539 | else |
| 540 | #endif |
| 541 | *s++ = p->c; |
| 542 | p = p->out; |
| 543 | } |
| 544 | *s = NUL; |
| 545 | } |
| 546 | return ret; |
| 547 | } |
| 548 | |
| 549 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 550 | * Allocate more space for post_start. Called when |
| 551 | * running above the estimated number of states. |
| 552 | */ |
| 553 | static int |
| 554 | realloc_post_list() |
| 555 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 556 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 557 | int new_max = nstate_max + 1000; |
| 558 | int *new_start; |
| 559 | int *old_start; |
| 560 | |
| 561 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 562 | if (new_start == NULL) |
| 563 | return FAIL; |
| 564 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 565 | old_start = post_start; |
| 566 | post_start = new_start; |
| 567 | post_ptr = new_start + (post_ptr - old_start); |
| 568 | post_end = post_start + new_max; |
| 569 | vim_free(old_start); |
| 570 | return OK; |
| 571 | } |
| 572 | |
| 573 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 574 | * Search between "start" and "end" and try to recognize a |
| 575 | * character class in expanded form. For example [0-9]. |
| 576 | * On success, return the id the character class to be emitted. |
| 577 | * On failure, return 0 (=FAIL) |
| 578 | * Start points to the first char of the range, while end should point |
| 579 | * to the closing brace. |
| 580 | */ |
| 581 | static int |
| 582 | nfa_recognize_char_class(start, end, extra_newl) |
| 583 | char_u *start; |
| 584 | char_u *end; |
| 585 | int extra_newl; |
| 586 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 587 | # define CLASS_not 0x80 |
| 588 | # define CLASS_af 0x40 |
| 589 | # define CLASS_AF 0x20 |
| 590 | # define CLASS_az 0x10 |
| 591 | # define CLASS_AZ 0x08 |
| 592 | # define CLASS_o7 0x04 |
| 593 | # define CLASS_o9 0x02 |
| 594 | # define CLASS_underscore 0x01 |
| 595 | |
| 596 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 597 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 598 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 599 | |
| 600 | if (extra_newl == TRUE) |
| 601 | newl = TRUE; |
| 602 | |
| 603 | if (*end != ']') |
| 604 | return FAIL; |
| 605 | p = start; |
| 606 | if (*p == '^') |
| 607 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 608 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 609 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | while (p < end) |
| 613 | { |
| 614 | if (p + 2 < end && *(p + 1) == '-') |
| 615 | { |
| 616 | switch (*p) |
| 617 | { |
| 618 | case '0': |
| 619 | if (*(p + 2) == '9') |
| 620 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 621 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 622 | break; |
| 623 | } |
| 624 | else |
| 625 | if (*(p + 2) == '7') |
| 626 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 627 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 628 | break; |
| 629 | } |
| 630 | case 'a': |
| 631 | if (*(p + 2) == 'z') |
| 632 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 633 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | else |
| 637 | if (*(p + 2) == 'f') |
| 638 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 639 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | case 'A': |
| 643 | if (*(p + 2) == 'Z') |
| 644 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 645 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 646 | break; |
| 647 | } |
| 648 | else |
| 649 | if (*(p + 2) == 'F') |
| 650 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 651 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | /* FALLTHROUGH */ |
| 655 | default: |
| 656 | return FAIL; |
| 657 | } |
| 658 | p += 3; |
| 659 | } |
| 660 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 661 | { |
| 662 | newl = TRUE; |
| 663 | p += 2; |
| 664 | } |
| 665 | else if (*p == '_') |
| 666 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 667 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 668 | p ++; |
| 669 | } |
| 670 | else if (*p == '\n') |
| 671 | { |
| 672 | newl = TRUE; |
| 673 | p ++; |
| 674 | } |
| 675 | else |
| 676 | return FAIL; |
| 677 | } /* while (p < end) */ |
| 678 | |
| 679 | if (p != end) |
| 680 | return FAIL; |
| 681 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 682 | if (newl == TRUE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 683 | extra_newl = ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 684 | |
| 685 | switch (config) |
| 686 | { |
| 687 | case CLASS_o9: |
| 688 | return extra_newl + NFA_DIGIT; |
| 689 | case CLASS_not | CLASS_o9: |
| 690 | return extra_newl + NFA_NDIGIT; |
| 691 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 692 | return extra_newl + NFA_HEX; |
| 693 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 694 | return extra_newl + NFA_NHEX; |
| 695 | case CLASS_o7: |
| 696 | return extra_newl + NFA_OCTAL; |
| 697 | case CLASS_not | CLASS_o7: |
| 698 | return extra_newl + NFA_NOCTAL; |
| 699 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 700 | return extra_newl + NFA_WORD; |
| 701 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 702 | return extra_newl + NFA_NWORD; |
| 703 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 704 | return extra_newl + NFA_HEAD; |
| 705 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 706 | return extra_newl + NFA_NHEAD; |
| 707 | case CLASS_az | CLASS_AZ: |
| 708 | return extra_newl + NFA_ALPHA; |
| 709 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 710 | return extra_newl + NFA_NALPHA; |
| 711 | case CLASS_az: |
| 712 | return extra_newl + NFA_LOWER; |
| 713 | case CLASS_not | CLASS_az: |
| 714 | return extra_newl + NFA_NLOWER; |
| 715 | case CLASS_AZ: |
| 716 | return extra_newl + NFA_UPPER; |
| 717 | case CLASS_not | CLASS_AZ: |
| 718 | return extra_newl + NFA_NUPPER; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 719 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 720 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* |
| 724 | * Produce the bytes for equivalence class "c". |
| 725 | * Currently only handles latin1, latin9 and utf-8. |
| 726 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 727 | * equivalent to 'a OR b OR c' |
| 728 | * |
| 729 | * NOTE! When changing this function, also update reg_equi_class() |
| 730 | */ |
| 731 | static int |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 732 | nfa_emit_equi_class(c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 733 | int c; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 734 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 735 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 736 | |
| 737 | #ifdef FEAT_MBYTE |
| 738 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 739 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 740 | #endif |
| 741 | { |
| 742 | switch (c) |
| 743 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 744 | case 'A': case 0300: case 0301: case 0302: |
| 745 | case 0303: case 0304: case 0305: |
| 746 | EMIT2('A'); EMIT2(0300); EMIT2(0301); |
| 747 | EMIT2(0302); EMIT2(0303); EMIT2(0304); |
| 748 | EMIT2(0305); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 749 | return OK; |
| 750 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 751 | case 'C': case 0307: |
| 752 | EMIT2('C'); EMIT2(0307); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 753 | return OK; |
| 754 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 755 | case 'E': case 0310: case 0311: case 0312: case 0313: |
| 756 | EMIT2('E'); EMIT2(0310); EMIT2(0311); |
| 757 | EMIT2(0312); EMIT2(0313); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 758 | return OK; |
| 759 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 760 | case 'I': case 0314: case 0315: case 0316: case 0317: |
| 761 | EMIT2('I'); EMIT2(0314); EMIT2(0315); |
| 762 | EMIT2(0316); EMIT2(0317); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 763 | return OK; |
| 764 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 765 | case 'N': case 0321: |
| 766 | EMIT2('N'); EMIT2(0321); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 767 | return OK; |
| 768 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 769 | case 'O': case 0322: case 0323: case 0324: case 0325: |
| 770 | case 0326: |
| 771 | EMIT2('O'); EMIT2(0322); EMIT2(0323); |
| 772 | EMIT2(0324); EMIT2(0325); EMIT2(0326); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 773 | return OK; |
| 774 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 775 | case 'U': case 0331: case 0332: case 0333: case 0334: |
| 776 | EMIT2('U'); EMIT2(0331); EMIT2(0332); |
| 777 | EMIT2(0333); EMIT2(0334); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 778 | return OK; |
| 779 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 780 | case 'Y': case 0335: |
| 781 | EMIT2('Y'); EMIT2(0335); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 782 | return OK; |
| 783 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 784 | case 'a': case 0340: case 0341: case 0342: |
| 785 | case 0343: case 0344: case 0345: |
| 786 | EMIT2('a'); EMIT2(0340); EMIT2(0341); |
| 787 | EMIT2(0342); EMIT2(0343); EMIT2(0344); |
| 788 | EMIT2(0345); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 789 | return OK; |
| 790 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 791 | case 'c': case 0347: |
| 792 | EMIT2('c'); EMIT2(0347); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 793 | return OK; |
| 794 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 795 | case 'e': case 0350: case 0351: case 0352: case 0353: |
| 796 | EMIT2('e'); EMIT2(0350); EMIT2(0351); |
| 797 | EMIT2(0352); EMIT2(0353); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 798 | return OK; |
| 799 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 800 | case 'i': case 0354: case 0355: case 0356: case 0357: |
| 801 | EMIT2('i'); EMIT2(0354); EMIT2(0355); |
| 802 | EMIT2(0356); EMIT2(0357); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 803 | return OK; |
| 804 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 805 | case 'n': case 0361: |
| 806 | EMIT2('n'); EMIT2(0361); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 807 | return OK; |
| 808 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 809 | case 'o': case 0362: case 0363: case 0364: case 0365: |
| 810 | case 0366: |
| 811 | EMIT2('o'); EMIT2(0362); EMIT2(0363); |
| 812 | EMIT2(0364); EMIT2(0365); EMIT2(0366); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 813 | return OK; |
| 814 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 815 | case 'u': case 0371: case 0372: case 0373: case 0374: |
| 816 | EMIT2('u'); EMIT2(0371); EMIT2(0372); |
| 817 | EMIT2(0373); EMIT2(0374); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 818 | return OK; |
| 819 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 820 | case 'y': case 0375: case 0377: |
| 821 | EMIT2('y'); EMIT2(0375); EMIT2(0377); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 822 | return OK; |
| 823 | |
| 824 | default: |
| 825 | return FAIL; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | EMIT(c); |
| 830 | return OK; |
| 831 | #undef EMIT2 |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Code to parse regular expression. |
| 836 | * |
| 837 | * We try to reuse parsing functions in regexp.c to |
| 838 | * minimize surprise and keep the syntax consistent. |
| 839 | */ |
| 840 | |
| 841 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 842 | * Parse the lowest level. |
| 843 | * |
| 844 | * An atom can be one of a long list of items. Many atoms match one character |
| 845 | * in the text. It is often an ordinary character or a character class. |
| 846 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 847 | * is only for syntax highlighting. |
| 848 | * |
| 849 | * atom ::= ordinary-atom |
| 850 | * or \( pattern \) |
| 851 | * or \%( pattern \) |
| 852 | * or \z( pattern \) |
| 853 | */ |
| 854 | static int |
| 855 | nfa_regatom() |
| 856 | { |
| 857 | int c; |
| 858 | int charclass; |
| 859 | int equiclass; |
| 860 | int collclass; |
| 861 | int got_coll_char; |
| 862 | char_u *p; |
| 863 | char_u *endp; |
| 864 | #ifdef FEAT_MBYTE |
| 865 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 866 | #endif |
| 867 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 868 | int emit_range; |
| 869 | int negated; |
| 870 | int result; |
| 871 | int startc = -1; |
| 872 | int endc = -1; |
| 873 | int oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 874 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 875 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 876 | switch (c) |
| 877 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 878 | case NUL: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 879 | EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); |
| 880 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 881 | case Magic('^'): |
| 882 | EMIT(NFA_BOL); |
| 883 | break; |
| 884 | |
| 885 | case Magic('$'): |
| 886 | EMIT(NFA_EOL); |
| 887 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 888 | had_eol = TRUE; |
| 889 | #endif |
| 890 | break; |
| 891 | |
| 892 | case Magic('<'): |
| 893 | EMIT(NFA_BOW); |
| 894 | break; |
| 895 | |
| 896 | case Magic('>'): |
| 897 | EMIT(NFA_EOW); |
| 898 | break; |
| 899 | |
| 900 | case Magic('_'): |
| 901 | c = no_Magic(getchr()); |
| 902 | if (c == '^') /* "\_^" is start-of-line */ |
| 903 | { |
| 904 | EMIT(NFA_BOL); |
| 905 | break; |
| 906 | } |
| 907 | if (c == '$') /* "\_$" is end-of-line */ |
| 908 | { |
| 909 | EMIT(NFA_EOL); |
| 910 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 911 | had_eol = TRUE; |
| 912 | #endif |
| 913 | break; |
| 914 | } |
| 915 | |
| 916 | extra = ADD_NL; |
| 917 | |
| 918 | /* "\_[" is collection plus newline */ |
| 919 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 920 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 921 | |
| 922 | /* "\_x" is character class plus newline */ |
| 923 | /*FALLTHROUGH*/ |
| 924 | |
| 925 | /* |
| 926 | * Character classes. |
| 927 | */ |
| 928 | case Magic('.'): |
| 929 | case Magic('i'): |
| 930 | case Magic('I'): |
| 931 | case Magic('k'): |
| 932 | case Magic('K'): |
| 933 | case Magic('f'): |
| 934 | case Magic('F'): |
| 935 | case Magic('p'): |
| 936 | case Magic('P'): |
| 937 | case Magic('s'): |
| 938 | case Magic('S'): |
| 939 | case Magic('d'): |
| 940 | case Magic('D'): |
| 941 | case Magic('x'): |
| 942 | case Magic('X'): |
| 943 | case Magic('o'): |
| 944 | case Magic('O'): |
| 945 | case Magic('w'): |
| 946 | case Magic('W'): |
| 947 | case Magic('h'): |
| 948 | case Magic('H'): |
| 949 | case Magic('a'): |
| 950 | case Magic('A'): |
| 951 | case Magic('l'): |
| 952 | case Magic('L'): |
| 953 | case Magic('u'): |
| 954 | case Magic('U'): |
| 955 | p = vim_strchr(classchars, no_Magic(c)); |
| 956 | if (p == NULL) |
| 957 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 958 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 959 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 960 | } |
| 961 | #ifdef FEAT_MBYTE |
| 962 | /* When '.' is followed by a composing char ignore the dot, so that |
| 963 | * the composing char is matched here. */ |
| 964 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 965 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 966 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 967 | c = getchr(); |
| 968 | goto nfa_do_multibyte; |
| 969 | } |
| 970 | #endif |
| 971 | EMIT(nfa_classcodes[p - classchars]); |
| 972 | if (extra == ADD_NL) |
| 973 | { |
| 974 | EMIT(NFA_NEWL); |
| 975 | EMIT(NFA_OR); |
| 976 | regflags |= RF_HASNL; |
| 977 | } |
| 978 | break; |
| 979 | |
| 980 | case Magic('n'): |
| 981 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 982 | /* In a string "\n" matches a newline character. */ |
| 983 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 984 | else |
| 985 | { |
| 986 | /* In buffer text "\n" matches the end of a line. */ |
| 987 | EMIT(NFA_NEWL); |
| 988 | regflags |= RF_HASNL; |
| 989 | } |
| 990 | break; |
| 991 | |
| 992 | case Magic('('): |
| 993 | if (nfa_reg(REG_PAREN) == FAIL) |
| 994 | return FAIL; /* cascaded error */ |
| 995 | break; |
| 996 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 997 | case Magic('|'): |
| 998 | case Magic('&'): |
| 999 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1000 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1001 | return FAIL; |
| 1002 | |
| 1003 | case Magic('='): |
| 1004 | case Magic('?'): |
| 1005 | case Magic('+'): |
| 1006 | case Magic('@'): |
| 1007 | case Magic('*'): |
| 1008 | case Magic('{'): |
| 1009 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1010 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1011 | return FAIL; |
| 1012 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 1013 | case Magic('~'): |
| 1014 | { |
| 1015 | char_u *lp; |
| 1016 | |
| 1017 | /* Previous substitute pattern. |
| 1018 | * Generated as "\%(pattern\)". */ |
| 1019 | if (reg_prev_sub == NULL) |
| 1020 | { |
| 1021 | EMSG(_(e_nopresub)); |
| 1022 | return FAIL; |
| 1023 | } |
| 1024 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 1025 | { |
| 1026 | EMIT(PTR2CHAR(lp)); |
| 1027 | if (lp != reg_prev_sub) |
| 1028 | EMIT(NFA_CONCAT); |
| 1029 | } |
| 1030 | EMIT(NFA_NOPEN); |
| 1031 | break; |
| 1032 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1033 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 1034 | case Magic('1'): |
| 1035 | case Magic('2'): |
| 1036 | case Magic('3'): |
| 1037 | case Magic('4'): |
| 1038 | case Magic('5'): |
| 1039 | case Magic('6'): |
| 1040 | case Magic('7'): |
| 1041 | case Magic('8'): |
| 1042 | case Magic('9'): |
| 1043 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 1044 | nfa_has_backref = TRUE; |
| 1045 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1046 | |
| 1047 | case Magic('z'): |
| 1048 | c = no_Magic(getchr()); |
| 1049 | switch (c) |
| 1050 | { |
| 1051 | case 's': |
| 1052 | EMIT(NFA_ZSTART); |
| 1053 | break; |
| 1054 | case 'e': |
| 1055 | EMIT(NFA_ZEND); |
| 1056 | nfa_has_zend = TRUE; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1057 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1058 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1059 | case '1': |
| 1060 | case '2': |
| 1061 | case '3': |
| 1062 | case '4': |
| 1063 | case '5': |
| 1064 | case '6': |
| 1065 | case '7': |
| 1066 | case '8': |
| 1067 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1068 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1069 | if (reg_do_extmatch != REX_USE) |
| 1070 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1071 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1072 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1073 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1074 | re_has_z = REX_USE; |
| 1075 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1076 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1077 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1078 | if (reg_do_extmatch != REX_SET) |
| 1079 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1080 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1081 | return FAIL; /* cascaded error */ |
| 1082 | re_has_z = REX_SET; |
| 1083 | break; |
| 1084 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1085 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1086 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1087 | no_Magic(c)); |
| 1088 | return FAIL; |
| 1089 | } |
| 1090 | break; |
| 1091 | |
| 1092 | case Magic('%'): |
| 1093 | c = no_Magic(getchr()); |
| 1094 | switch (c) |
| 1095 | { |
| 1096 | /* () without a back reference */ |
| 1097 | case '(': |
| 1098 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1099 | return FAIL; |
| 1100 | EMIT(NFA_NOPEN); |
| 1101 | break; |
| 1102 | |
| 1103 | case 'd': /* %d123 decimal */ |
| 1104 | case 'o': /* %o123 octal */ |
| 1105 | case 'x': /* %xab hex 2 */ |
| 1106 | case 'u': /* %uabcd hex 4 */ |
| 1107 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1108 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1109 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1110 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1111 | switch (c) |
| 1112 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1113 | case 'd': nr = getdecchrs(); break; |
| 1114 | case 'o': nr = getoctchrs(); break; |
| 1115 | case 'x': nr = gethexchrs(2); break; |
| 1116 | case 'u': nr = gethexchrs(4); break; |
| 1117 | case 'U': nr = gethexchrs(8); break; |
| 1118 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1119 | } |
| 1120 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1121 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1122 | EMSG2_RET_FAIL( |
| 1123 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1124 | reg_magic == MAGIC_ALL); |
| 1125 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1126 | EMIT(nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1127 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1128 | break; |
| 1129 | |
| 1130 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1131 | * pattern -- regardless of whether or not it makes sense. */ |
| 1132 | case '^': |
| 1133 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1134 | break; |
| 1135 | |
| 1136 | case '$': |
| 1137 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1138 | break; |
| 1139 | |
| 1140 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1141 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1142 | break; |
| 1143 | |
| 1144 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1145 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1146 | break; |
| 1147 | |
| 1148 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1149 | { |
| 1150 | int n; |
| 1151 | |
| 1152 | /* \%[abc] */ |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1153 | for (n = 0; (c = peekchr()) != ']'; ++n) |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1154 | { |
| 1155 | if (c == NUL) |
| 1156 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1157 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1158 | /* recursive call! */ |
| 1159 | if (nfa_regatom() == FAIL) |
| 1160 | return FAIL; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1161 | } |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1162 | getchr(); /* get the ] */ |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1163 | if (n == 0) |
| 1164 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1165 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1166 | EMIT(NFA_OPT_CHARS); |
| 1167 | EMIT(n); |
| 1168 | break; |
| 1169 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1170 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1171 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1172 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1173 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1174 | int cmp = c; |
| 1175 | |
| 1176 | if (c == '<' || c == '>') |
| 1177 | c = getchr(); |
| 1178 | while (VIM_ISDIGIT(c)) |
| 1179 | { |
| 1180 | n = n * 10 + (c - '0'); |
| 1181 | c = getchr(); |
| 1182 | } |
| 1183 | if (c == 'l' || c == 'c' || c == 'v') |
| 1184 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1185 | if (c == 'l') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1186 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1187 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1188 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1189 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1190 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1191 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1192 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1193 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1194 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1195 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1196 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1197 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1198 | break; |
| 1199 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1200 | else if (c == '\'' && n == 0) |
| 1201 | { |
| 1202 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1203 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1204 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1205 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1206 | break; |
| 1207 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1208 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1209 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1210 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1211 | return FAIL; |
| 1212 | } |
| 1213 | break; |
| 1214 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1215 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1216 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1217 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1218 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1219 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1220 | * Each character is produced as a regular state, using |
| 1221 | * NFA_CONCAT to bind them together. |
| 1222 | * Besides normal characters there can be: |
| 1223 | * - character classes NFA_CLASS_* |
| 1224 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1225 | */ |
| 1226 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1227 | p = regparse; |
| 1228 | endp = skip_anyof(p); |
| 1229 | if (*endp == ']') |
| 1230 | { |
| 1231 | /* |
| 1232 | * Try to reverse engineer character classes. For example, |
| 1233 | * recognize that [0-9] stands for \d and [A-Za-z_] with \h, |
| 1234 | * and perform the necessary substitutions in the NFA. |
| 1235 | */ |
| 1236 | result = nfa_recognize_char_class(regparse, endp, |
| 1237 | extra == ADD_NL); |
| 1238 | if (result != FAIL) |
| 1239 | { |
| 1240 | if (result >= NFA_DIGIT && result <= NFA_NUPPER) |
| 1241 | EMIT(result); |
| 1242 | else /* must be char class + newline */ |
| 1243 | { |
| 1244 | EMIT(result - ADD_NL); |
| 1245 | EMIT(NFA_NEWL); |
| 1246 | EMIT(NFA_OR); |
| 1247 | } |
| 1248 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1249 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1250 | return OK; |
| 1251 | } |
| 1252 | /* |
| 1253 | * Failed to recognize a character class. Use the simple |
| 1254 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1255 | */ |
| 1256 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1257 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1258 | if (*regparse == '^') /* negated range */ |
| 1259 | { |
| 1260 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1261 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1262 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1263 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1264 | else |
| 1265 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1266 | if (*regparse == '-') |
| 1267 | { |
| 1268 | startc = '-'; |
| 1269 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1270 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1271 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1272 | } |
| 1273 | /* Emit the OR branches for each character in the [] */ |
| 1274 | emit_range = FALSE; |
| 1275 | while (regparse < endp) |
| 1276 | { |
| 1277 | oldstartc = startc; |
| 1278 | startc = -1; |
| 1279 | got_coll_char = FALSE; |
| 1280 | if (*regparse == '[') |
| 1281 | { |
| 1282 | /* Check for [: :], [= =], [. .] */ |
| 1283 | equiclass = collclass = 0; |
| 1284 | charclass = get_char_class(®parse); |
| 1285 | if (charclass == CLASS_NONE) |
| 1286 | { |
| 1287 | equiclass = get_equi_class(®parse); |
| 1288 | if (equiclass == 0) |
| 1289 | collclass = get_coll_element(®parse); |
| 1290 | } |
| 1291 | |
| 1292 | /* Character class like [:alpha:] */ |
| 1293 | if (charclass != CLASS_NONE) |
| 1294 | { |
| 1295 | switch (charclass) |
| 1296 | { |
| 1297 | case CLASS_ALNUM: |
| 1298 | EMIT(NFA_CLASS_ALNUM); |
| 1299 | break; |
| 1300 | case CLASS_ALPHA: |
| 1301 | EMIT(NFA_CLASS_ALPHA); |
| 1302 | break; |
| 1303 | case CLASS_BLANK: |
| 1304 | EMIT(NFA_CLASS_BLANK); |
| 1305 | break; |
| 1306 | case CLASS_CNTRL: |
| 1307 | EMIT(NFA_CLASS_CNTRL); |
| 1308 | break; |
| 1309 | case CLASS_DIGIT: |
| 1310 | EMIT(NFA_CLASS_DIGIT); |
| 1311 | break; |
| 1312 | case CLASS_GRAPH: |
| 1313 | EMIT(NFA_CLASS_GRAPH); |
| 1314 | break; |
| 1315 | case CLASS_LOWER: |
| 1316 | EMIT(NFA_CLASS_LOWER); |
| 1317 | break; |
| 1318 | case CLASS_PRINT: |
| 1319 | EMIT(NFA_CLASS_PRINT); |
| 1320 | break; |
| 1321 | case CLASS_PUNCT: |
| 1322 | EMIT(NFA_CLASS_PUNCT); |
| 1323 | break; |
| 1324 | case CLASS_SPACE: |
| 1325 | EMIT(NFA_CLASS_SPACE); |
| 1326 | break; |
| 1327 | case CLASS_UPPER: |
| 1328 | EMIT(NFA_CLASS_UPPER); |
| 1329 | break; |
| 1330 | case CLASS_XDIGIT: |
| 1331 | EMIT(NFA_CLASS_XDIGIT); |
| 1332 | break; |
| 1333 | case CLASS_TAB: |
| 1334 | EMIT(NFA_CLASS_TAB); |
| 1335 | break; |
| 1336 | case CLASS_RETURN: |
| 1337 | EMIT(NFA_CLASS_RETURN); |
| 1338 | break; |
| 1339 | case CLASS_BACKSPACE: |
| 1340 | EMIT(NFA_CLASS_BACKSPACE); |
| 1341 | break; |
| 1342 | case CLASS_ESCAPE: |
| 1343 | EMIT(NFA_CLASS_ESCAPE); |
| 1344 | break; |
| 1345 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1346 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1347 | continue; |
| 1348 | } |
| 1349 | /* Try equivalence class [=a=] and the like */ |
| 1350 | if (equiclass != 0) |
| 1351 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1352 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1353 | if (result == FAIL) |
| 1354 | { |
| 1355 | /* should never happen */ |
| 1356 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1357 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1358 | continue; |
| 1359 | } |
| 1360 | /* Try collating class like [. .] */ |
| 1361 | if (collclass != 0) |
| 1362 | { |
| 1363 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1364 | /* Will emit the proper atom at the end of the |
| 1365 | * while loop. */ |
| 1366 | } |
| 1367 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1368 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1369 | * start character. */ |
| 1370 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1371 | { |
| 1372 | emit_range = TRUE; |
| 1373 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1374 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1375 | continue; /* reading the end of the range */ |
| 1376 | } |
| 1377 | |
| 1378 | /* Now handle simple and escaped characters. |
| 1379 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1380 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1381 | * 'cpoptions' is not included. |
| 1382 | * Posix doesn't recognize backslash at all. |
| 1383 | */ |
| 1384 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1385 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1386 | && regparse + 1 <= endp |
| 1387 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1388 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1389 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1390 | != NULL) |
| 1391 | ) |
| 1392 | ) |
| 1393 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1394 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1395 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1396 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1397 | startc = reg_string ? NL : NFA_NEWL; |
| 1398 | else |
| 1399 | if (*regparse == 'd' |
| 1400 | || *regparse == 'o' |
| 1401 | || *regparse == 'x' |
| 1402 | || *regparse == 'u' |
| 1403 | || *regparse == 'U' |
| 1404 | ) |
| 1405 | { |
| 1406 | /* TODO(RE) This needs more testing */ |
| 1407 | startc = coll_get_char(); |
| 1408 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1409 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1410 | } |
| 1411 | else |
| 1412 | { |
| 1413 | /* \r,\t,\e,\b */ |
| 1414 | startc = backslash_trans(*regparse); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /* Normal printable char */ |
| 1419 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1420 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1421 | |
| 1422 | /* Previous char was '-', so this char is end of range. */ |
| 1423 | if (emit_range) |
| 1424 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1425 | endc = startc; |
| 1426 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1427 | if (startc > endc) |
| 1428 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1429 | |
| 1430 | if (endc > startc + 2) |
| 1431 | { |
| 1432 | /* Emit a range instead of the sequence of |
| 1433 | * individual characters. */ |
| 1434 | if (startc == 0) |
| 1435 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1436 | EMIT(1); |
| 1437 | else |
| 1438 | --post_ptr; /* remove NFA_CONCAT */ |
| 1439 | EMIT(endc); |
| 1440 | EMIT(NFA_RANGE); |
| 1441 | EMIT(NFA_CONCAT); |
| 1442 | } |
| 1443 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1444 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1445 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1446 | || (*mb_char2len)(endc) > 1)) |
| 1447 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1448 | /* Emit the characters in the range. |
| 1449 | * "startc" was already emitted, so skip it. |
| 1450 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1451 | for (c = startc + 1; c <= endc; c++) |
| 1452 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1453 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1454 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1455 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1456 | } |
| 1457 | else |
| 1458 | #endif |
| 1459 | { |
| 1460 | #ifdef EBCDIC |
| 1461 | int alpha_only = FALSE; |
| 1462 | |
| 1463 | /* for alphabetical range skip the gaps |
| 1464 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1465 | if (isalpha(startc) && isalpha(endc)) |
| 1466 | alpha_only = TRUE; |
| 1467 | #endif |
| 1468 | /* Emit the range. "startc" was already emitted, so |
| 1469 | * skip it. */ |
| 1470 | for (c = startc + 1; c <= endc; c++) |
| 1471 | #ifdef EBCDIC |
| 1472 | if (!alpha_only || isalpha(startc)) |
| 1473 | #endif |
| 1474 | { |
| 1475 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1476 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1477 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1478 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1479 | emit_range = FALSE; |
| 1480 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1481 | } |
| 1482 | else |
| 1483 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1484 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1485 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1486 | * Normally, simply emit startc. But if we get char |
| 1487 | * code=0 from a collating char, then replace it with |
| 1488 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1489 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1490 | * the backtracking engine. */ |
| 1491 | if (startc == NFA_NEWL) |
| 1492 | { |
| 1493 | /* Line break can't be matched as part of the |
| 1494 | * collection, add an OR below. But not for negated |
| 1495 | * range. */ |
| 1496 | if (!negated) |
| 1497 | extra = ADD_NL; |
| 1498 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1499 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1500 | { |
| 1501 | if (got_coll_char == TRUE && startc == 0) |
| 1502 | EMIT(0x0a); |
| 1503 | else |
| 1504 | EMIT(startc); |
| 1505 | EMIT(NFA_CONCAT); |
| 1506 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1507 | } |
| 1508 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1509 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1510 | } /* while (p < endp) */ |
| 1511 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1512 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1513 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1514 | { |
| 1515 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1516 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1517 | } |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1518 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1519 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1520 | /* skip the trailing ] */ |
| 1521 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1522 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1523 | |
| 1524 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1525 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1526 | EMIT(NFA_END_NEG_COLL); |
| 1527 | else |
| 1528 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1529 | |
| 1530 | /* \_[] also matches \n but it's not negated */ |
| 1531 | if (extra == ADD_NL) |
| 1532 | { |
| 1533 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1534 | EMIT(NFA_OR); |
| 1535 | } |
| 1536 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1537 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1538 | } /* if exists closing ] */ |
| 1539 | |
| 1540 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1541 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1542 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1543 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1544 | default: |
| 1545 | { |
| 1546 | #ifdef FEAT_MBYTE |
| 1547 | int plen; |
| 1548 | |
| 1549 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1550 | /* plen is length of current char with composing chars */ |
| 1551 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1552 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1553 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1554 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1555 | int i = 0; |
| 1556 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1557 | /* A base character plus composing characters, or just one |
| 1558 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1559 | * This requires creating a separate atom as if enclosing |
| 1560 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1561 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1562 | * building the postfix form, not the NFA itself; |
| 1563 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1564 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1565 | for (;;) |
| 1566 | { |
| 1567 | EMIT(c); |
| 1568 | if (i > 0) |
| 1569 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1570 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1571 | break; |
| 1572 | c = utf_ptr2char(old_regparse + i); |
| 1573 | } |
| 1574 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1575 | regparse = old_regparse + plen; |
| 1576 | } |
| 1577 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1578 | #endif |
| 1579 | { |
| 1580 | c = no_Magic(c); |
| 1581 | EMIT(c); |
| 1582 | } |
| 1583 | return OK; |
| 1584 | } |
| 1585 | } |
| 1586 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1587 | return OK; |
| 1588 | } |
| 1589 | |
| 1590 | /* |
| 1591 | * Parse something followed by possible [*+=]. |
| 1592 | * |
| 1593 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1594 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1595 | * characters: "", "a", "aa", etc. |
| 1596 | * |
| 1597 | * piece ::= atom |
| 1598 | * or atom multi |
| 1599 | */ |
| 1600 | static int |
| 1601 | nfa_regpiece() |
| 1602 | { |
| 1603 | int i; |
| 1604 | int op; |
| 1605 | int ret; |
| 1606 | long minval, maxval; |
| 1607 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1608 | parse_state_T old_state; |
| 1609 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1610 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1611 | int old_post_pos; |
| 1612 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1613 | int quest; |
| 1614 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1615 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1616 | * next. */ |
| 1617 | save_parse_state(&old_state); |
| 1618 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1619 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1620 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1621 | |
| 1622 | ret = nfa_regatom(); |
| 1623 | if (ret == FAIL) |
| 1624 | return FAIL; /* cascaded error */ |
| 1625 | |
| 1626 | op = peekchr(); |
| 1627 | if (re_multi_type(op) == NOT_MULTI) |
| 1628 | return OK; |
| 1629 | |
| 1630 | skipchr(); |
| 1631 | switch (op) |
| 1632 | { |
| 1633 | case Magic('*'): |
| 1634 | EMIT(NFA_STAR); |
| 1635 | break; |
| 1636 | |
| 1637 | case Magic('+'): |
| 1638 | /* |
| 1639 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1640 | * first and only submatch would be "aaa". But the backtracking |
| 1641 | * engine interprets the plus as "try matching one more time", and |
| 1642 | * a* matches a second time at the end of the input, the empty |
| 1643 | * string. |
| 1644 | * The submatch will the empty string. |
| 1645 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1646 | * In order to be consistent with the old engine, we replace |
| 1647 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1648 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1649 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1650 | curchr = -1; |
| 1651 | if (nfa_regatom() == FAIL) |
| 1652 | return FAIL; |
| 1653 | EMIT(NFA_STAR); |
| 1654 | EMIT(NFA_CONCAT); |
| 1655 | skipchr(); /* skip the \+ */ |
| 1656 | break; |
| 1657 | |
| 1658 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1659 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1660 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1661 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1662 | switch(op) |
| 1663 | { |
| 1664 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1665 | /* \@= */ |
| 1666 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1667 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1668 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1669 | /* \@! */ |
| 1670 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1671 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1672 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1673 | op = no_Magic(getchr()); |
| 1674 | if (op == '=') |
| 1675 | /* \@<= */ |
| 1676 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1677 | else if (op == '!') |
| 1678 | /* \@<! */ |
| 1679 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1680 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1681 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1682 | /* \@> */ |
| 1683 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1684 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1685 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1686 | if (i == 0) |
| 1687 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1688 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1689 | return FAIL; |
| 1690 | } |
| 1691 | EMIT(i); |
| 1692 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1693 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1694 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1695 | break; |
| 1696 | |
| 1697 | case Magic('?'): |
| 1698 | case Magic('='): |
| 1699 | EMIT(NFA_QUEST); |
| 1700 | break; |
| 1701 | |
| 1702 | case Magic('{'): |
| 1703 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1704 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1705 | * version of '?' |
| 1706 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1707 | * parenthesis have the same id |
| 1708 | */ |
| 1709 | |
| 1710 | greedy = TRUE; |
| 1711 | c2 = peekchr(); |
| 1712 | if (c2 == '-' || c2 == Magic('-')) |
| 1713 | { |
| 1714 | skipchr(); |
| 1715 | greedy = FALSE; |
| 1716 | } |
| 1717 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1718 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 1719 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1720 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 1721 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1722 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1723 | { |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1724 | if (greedy) |
| 1725 | /* \{}, \{0,} */ |
| 1726 | EMIT(NFA_STAR); |
| 1727 | else |
| 1728 | /* \{-}, \{-0,} */ |
| 1729 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1730 | break; |
| 1731 | } |
| 1732 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1733 | /* Special case: x{0} or x{-0} */ |
| 1734 | if (maxval == 0) |
| 1735 | { |
| 1736 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1737 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1738 | /* NFA_SKIP_CHAR has 0-length and works everywhere */ |
| 1739 | EMIT(NFA_SKIP_CHAR); |
| 1740 | return OK; |
| 1741 | } |
| 1742 | |
| 1743 | /* Ignore previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1744 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1745 | /* Save parse state after the repeated atom and the \{} */ |
| 1746 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1747 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1748 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 1749 | for (i = 0; i < maxval; i++) |
| 1750 | { |
| 1751 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1752 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1753 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1754 | if (nfa_regatom() == FAIL) |
| 1755 | return FAIL; |
| 1756 | /* after "minval" times, atoms are optional */ |
| 1757 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1758 | { |
| 1759 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1760 | { |
| 1761 | if (greedy) |
| 1762 | EMIT(NFA_STAR); |
| 1763 | else |
| 1764 | EMIT(NFA_STAR_NONGREEDY); |
| 1765 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1766 | else |
| 1767 | EMIT(quest); |
| 1768 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1769 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1770 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1771 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 1772 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1773 | } |
| 1774 | |
| 1775 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1776 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1777 | curchr = -1; |
| 1778 | |
| 1779 | break; |
| 1780 | |
| 1781 | |
| 1782 | default: |
| 1783 | break; |
| 1784 | } /* end switch */ |
| 1785 | |
| 1786 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1787 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1788 | 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] | 1789 | |
| 1790 | return OK; |
| 1791 | } |
| 1792 | |
| 1793 | /* |
| 1794 | * Parse one or more pieces, concatenated. It matches a match for the |
| 1795 | * first piece, followed by a match for the second piece, etc. Example: |
| 1796 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 1797 | * |
| 1798 | * concat ::= piece |
| 1799 | * or piece piece |
| 1800 | * or piece piece piece |
| 1801 | * etc. |
| 1802 | */ |
| 1803 | static int |
| 1804 | nfa_regconcat() |
| 1805 | { |
| 1806 | int cont = TRUE; |
| 1807 | int first = TRUE; |
| 1808 | |
| 1809 | while (cont) |
| 1810 | { |
| 1811 | switch (peekchr()) |
| 1812 | { |
| 1813 | case NUL: |
| 1814 | case Magic('|'): |
| 1815 | case Magic('&'): |
| 1816 | case Magic(')'): |
| 1817 | cont = FALSE; |
| 1818 | break; |
| 1819 | |
| 1820 | case Magic('Z'): |
| 1821 | #ifdef FEAT_MBYTE |
| 1822 | regflags |= RF_ICOMBINE; |
| 1823 | #endif |
| 1824 | skipchr_keepstart(); |
| 1825 | break; |
| 1826 | case Magic('c'): |
| 1827 | regflags |= RF_ICASE; |
| 1828 | skipchr_keepstart(); |
| 1829 | break; |
| 1830 | case Magic('C'): |
| 1831 | regflags |= RF_NOICASE; |
| 1832 | skipchr_keepstart(); |
| 1833 | break; |
| 1834 | case Magic('v'): |
| 1835 | reg_magic = MAGIC_ALL; |
| 1836 | skipchr_keepstart(); |
| 1837 | curchr = -1; |
| 1838 | break; |
| 1839 | case Magic('m'): |
| 1840 | reg_magic = MAGIC_ON; |
| 1841 | skipchr_keepstart(); |
| 1842 | curchr = -1; |
| 1843 | break; |
| 1844 | case Magic('M'): |
| 1845 | reg_magic = MAGIC_OFF; |
| 1846 | skipchr_keepstart(); |
| 1847 | curchr = -1; |
| 1848 | break; |
| 1849 | case Magic('V'): |
| 1850 | reg_magic = MAGIC_NONE; |
| 1851 | skipchr_keepstart(); |
| 1852 | curchr = -1; |
| 1853 | break; |
| 1854 | |
| 1855 | default: |
| 1856 | if (nfa_regpiece() == FAIL) |
| 1857 | return FAIL; |
| 1858 | if (first == FALSE) |
| 1859 | EMIT(NFA_CONCAT); |
| 1860 | else |
| 1861 | first = FALSE; |
| 1862 | break; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | return OK; |
| 1867 | } |
| 1868 | |
| 1869 | /* |
| 1870 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 1871 | * last concat, but only if all the preceding concats also match at the same |
| 1872 | * position. Examples: |
| 1873 | * "foobeep\&..." matches "foo" in "foobeep". |
| 1874 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 1875 | * |
| 1876 | * branch ::= concat |
| 1877 | * or concat \& concat |
| 1878 | * or concat \& concat \& concat |
| 1879 | * etc. |
| 1880 | */ |
| 1881 | static int |
| 1882 | nfa_regbranch() |
| 1883 | { |
| 1884 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1885 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1886 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1887 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1888 | |
| 1889 | /* First branch, possibly the only one */ |
| 1890 | if (nfa_regconcat() == FAIL) |
| 1891 | return FAIL; |
| 1892 | |
| 1893 | ch = peekchr(); |
| 1894 | /* Try next concats */ |
| 1895 | while (ch == Magic('&')) |
| 1896 | { |
| 1897 | skipchr(); |
| 1898 | EMIT(NFA_NOPEN); |
| 1899 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1900 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1901 | if (nfa_regconcat() == FAIL) |
| 1902 | return FAIL; |
| 1903 | /* 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] | 1904 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1905 | EMIT(NFA_SKIP_CHAR); |
| 1906 | EMIT(NFA_CONCAT); |
| 1907 | ch = peekchr(); |
| 1908 | } |
| 1909 | |
| 1910 | /* Even if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1911 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1912 | EMIT(NFA_SKIP_CHAR); |
| 1913 | |
| 1914 | return OK; |
| 1915 | } |
| 1916 | |
| 1917 | /* |
| 1918 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 1919 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 1920 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 1921 | * is used. |
| 1922 | * |
| 1923 | * pattern ::= branch |
| 1924 | * or branch \| branch |
| 1925 | * or branch \| branch \| branch |
| 1926 | * etc. |
| 1927 | */ |
| 1928 | static int |
| 1929 | nfa_reg(paren) |
| 1930 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 1931 | { |
| 1932 | int parno = 0; |
| 1933 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1934 | if (paren == REG_PAREN) |
| 1935 | { |
| 1936 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1937 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1938 | parno = regnpar++; |
| 1939 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1940 | #ifdef FEAT_SYN_HL |
| 1941 | else if (paren == REG_ZPAREN) |
| 1942 | { |
| 1943 | /* Make a ZOPEN node. */ |
| 1944 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1945 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1946 | parno = regnzpar++; |
| 1947 | } |
| 1948 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1949 | |
| 1950 | if (nfa_regbranch() == FAIL) |
| 1951 | return FAIL; /* cascaded error */ |
| 1952 | |
| 1953 | while (peekchr() == Magic('|')) |
| 1954 | { |
| 1955 | skipchr(); |
| 1956 | if (nfa_regbranch() == FAIL) |
| 1957 | return FAIL; /* cascaded error */ |
| 1958 | EMIT(NFA_OR); |
| 1959 | } |
| 1960 | |
| 1961 | /* Check for proper termination. */ |
| 1962 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 1963 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1964 | if (paren == REG_NPAREN) |
| 1965 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 1966 | else |
| 1967 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 1968 | } |
| 1969 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 1970 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1971 | if (peekchr() == Magic(')')) |
| 1972 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 1973 | else |
| 1974 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 1975 | } |
| 1976 | /* |
| 1977 | * Here we set the flag allowing back references to this set of |
| 1978 | * parentheses. |
| 1979 | */ |
| 1980 | if (paren == REG_PAREN) |
| 1981 | { |
| 1982 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 1983 | EMIT(NFA_MOPEN + parno); |
| 1984 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1985 | #ifdef FEAT_SYN_HL |
| 1986 | else if (paren == REG_ZPAREN) |
| 1987 | EMIT(NFA_ZOPEN + parno); |
| 1988 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1989 | |
| 1990 | return OK; |
| 1991 | } |
| 1992 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1993 | #ifdef DEBUG |
| 1994 | static char_u code[50]; |
| 1995 | |
| 1996 | static void |
| 1997 | nfa_set_code(c) |
| 1998 | int c; |
| 1999 | { |
| 2000 | int addnl = FALSE; |
| 2001 | |
| 2002 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 2003 | { |
| 2004 | addnl = TRUE; |
| 2005 | c -= ADD_NL; |
| 2006 | } |
| 2007 | |
| 2008 | STRCPY(code, ""); |
| 2009 | switch (c) |
| 2010 | { |
| 2011 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 2012 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 2013 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 2014 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 2015 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 2016 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 2017 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2018 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 2019 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 2020 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 2021 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 2022 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 2023 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 2024 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 2025 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 2026 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2027 | #ifdef FEAT_SYN_HL |
| 2028 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 2029 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 2030 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 2031 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 2032 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 2033 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 2034 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 2035 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 2036 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 2037 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2038 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 2039 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2040 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2041 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2042 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 2043 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2044 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2045 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 2046 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 2047 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2048 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 2049 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 2050 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2051 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 2052 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2053 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2054 | case NFA_START_INVISIBLE_FIRST: |
| 2055 | STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2056 | case NFA_START_INVISIBLE_NEG: |
| 2057 | STRCPY(code, "NFA_START_INVISIBLE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2058 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 2059 | STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2060 | case NFA_START_INVISIBLE_BEFORE: |
| 2061 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2062 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 2063 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2064 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2065 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2066 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 2067 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2068 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2069 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2070 | case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2071 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2072 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2073 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2074 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2075 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2076 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2077 | case NFA_MOPEN: |
| 2078 | case NFA_MOPEN1: |
| 2079 | case NFA_MOPEN2: |
| 2080 | case NFA_MOPEN3: |
| 2081 | case NFA_MOPEN4: |
| 2082 | case NFA_MOPEN5: |
| 2083 | case NFA_MOPEN6: |
| 2084 | case NFA_MOPEN7: |
| 2085 | case NFA_MOPEN8: |
| 2086 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2087 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2088 | code[10] = c - NFA_MOPEN + '0'; |
| 2089 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2090 | case NFA_MCLOSE: |
| 2091 | case NFA_MCLOSE1: |
| 2092 | case NFA_MCLOSE2: |
| 2093 | case NFA_MCLOSE3: |
| 2094 | case NFA_MCLOSE4: |
| 2095 | case NFA_MCLOSE5: |
| 2096 | case NFA_MCLOSE6: |
| 2097 | case NFA_MCLOSE7: |
| 2098 | case NFA_MCLOSE8: |
| 2099 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2100 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2101 | code[11] = c - NFA_MCLOSE + '0'; |
| 2102 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2103 | #ifdef FEAT_SYN_HL |
| 2104 | case NFA_ZOPEN: |
| 2105 | case NFA_ZOPEN1: |
| 2106 | case NFA_ZOPEN2: |
| 2107 | case NFA_ZOPEN3: |
| 2108 | case NFA_ZOPEN4: |
| 2109 | case NFA_ZOPEN5: |
| 2110 | case NFA_ZOPEN6: |
| 2111 | case NFA_ZOPEN7: |
| 2112 | case NFA_ZOPEN8: |
| 2113 | case NFA_ZOPEN9: |
| 2114 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2115 | code[10] = c - NFA_ZOPEN + '0'; |
| 2116 | break; |
| 2117 | case NFA_ZCLOSE: |
| 2118 | case NFA_ZCLOSE1: |
| 2119 | case NFA_ZCLOSE2: |
| 2120 | case NFA_ZCLOSE3: |
| 2121 | case NFA_ZCLOSE4: |
| 2122 | case NFA_ZCLOSE5: |
| 2123 | case NFA_ZCLOSE6: |
| 2124 | case NFA_ZCLOSE7: |
| 2125 | case NFA_ZCLOSE8: |
| 2126 | case NFA_ZCLOSE9: |
| 2127 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2128 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2129 | break; |
| 2130 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2131 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2132 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2133 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2134 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2135 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2136 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2137 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2138 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2139 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2140 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2141 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2142 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2143 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2144 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2145 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2146 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2147 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2148 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2149 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2150 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
| 2151 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2152 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2153 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2154 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2155 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2156 | case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; |
| 2157 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2158 | |
| 2159 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2160 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2161 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2162 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2163 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2164 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2165 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2166 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2167 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2168 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2169 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2170 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2171 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2172 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2173 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2174 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2175 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2176 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2177 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2178 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2179 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2180 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2181 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2182 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2183 | |
| 2184 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2185 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2186 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2187 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2188 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2189 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2190 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2191 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2192 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2193 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2194 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2195 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2196 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2197 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2198 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2199 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2200 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2201 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2202 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2203 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2204 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2205 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2206 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2207 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2208 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2209 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2210 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
| 2211 | |
| 2212 | default: |
| 2213 | STRCPY(code, "CHAR(x)"); |
| 2214 | code[5] = c; |
| 2215 | } |
| 2216 | |
| 2217 | if (addnl == TRUE) |
| 2218 | STRCAT(code, " + NEWLINE "); |
| 2219 | |
| 2220 | } |
| 2221 | |
| 2222 | #ifdef ENABLE_LOG |
| 2223 | static FILE *log_fd; |
| 2224 | |
| 2225 | /* |
| 2226 | * Print the postfix notation of the current regexp. |
| 2227 | */ |
| 2228 | static void |
| 2229 | nfa_postfix_dump(expr, retval) |
| 2230 | char_u *expr; |
| 2231 | int retval; |
| 2232 | { |
| 2233 | int *p; |
| 2234 | FILE *f; |
| 2235 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2236 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2237 | if (f != NULL) |
| 2238 | { |
| 2239 | fprintf(f, "\n-------------------------\n"); |
| 2240 | if (retval == FAIL) |
| 2241 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2242 | else if (retval == OK) |
| 2243 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2244 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2245 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2246 | { |
| 2247 | nfa_set_code(*p); |
| 2248 | fprintf(f, "%s, ", code); |
| 2249 | } |
| 2250 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2251 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2252 | fprintf(f, "%d ", *p); |
| 2253 | fprintf(f, "\n\n"); |
| 2254 | fclose(f); |
| 2255 | } |
| 2256 | } |
| 2257 | |
| 2258 | /* |
| 2259 | * Print the NFA starting with a root node "state". |
| 2260 | */ |
| 2261 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2262 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2263 | FILE *debugf; |
| 2264 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2265 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2266 | garray_T indent; |
| 2267 | |
| 2268 | ga_init2(&indent, 1, 64); |
| 2269 | ga_append(&indent, '\0'); |
| 2270 | nfa_print_state2(debugf, state, &indent); |
| 2271 | ga_clear(&indent); |
| 2272 | } |
| 2273 | |
| 2274 | static void |
| 2275 | nfa_print_state2(debugf, state, indent) |
| 2276 | FILE *debugf; |
| 2277 | nfa_state_T *state; |
| 2278 | garray_T *indent; |
| 2279 | { |
| 2280 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2281 | |
| 2282 | if (state == NULL) |
| 2283 | return; |
| 2284 | |
| 2285 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2286 | |
| 2287 | /* Output indent */ |
| 2288 | p = (char_u *)indent->ga_data; |
| 2289 | if (indent->ga_len >= 3) |
| 2290 | { |
| 2291 | int last = indent->ga_len - 3; |
| 2292 | char_u save[2]; |
| 2293 | |
| 2294 | STRNCPY(save, &p[last], 2); |
| 2295 | STRNCPY(&p[last], "+-", 2); |
| 2296 | fprintf(debugf, " %s", p); |
| 2297 | STRNCPY(&p[last], save, 2); |
| 2298 | } |
| 2299 | else |
| 2300 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2301 | |
| 2302 | nfa_set_code(state->c); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2303 | fprintf(debugf, "%s (%d) (id=%d) val=%d\n", |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2304 | code, |
| 2305 | state->c, |
| 2306 | abs(state->id), |
| 2307 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2308 | if (state->id < 0) |
| 2309 | return; |
| 2310 | |
| 2311 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2312 | |
| 2313 | /* grow indent for state->out */ |
| 2314 | indent->ga_len -= 1; |
| 2315 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2316 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2317 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2318 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2319 | ga_append(indent, '\0'); |
| 2320 | |
| 2321 | nfa_print_state2(debugf, state->out, indent); |
| 2322 | |
| 2323 | /* replace last part of indent for state->out1 */ |
| 2324 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2325 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2326 | ga_append(indent, '\0'); |
| 2327 | |
| 2328 | nfa_print_state2(debugf, state->out1, indent); |
| 2329 | |
| 2330 | /* shrink indent */ |
| 2331 | indent->ga_len -= 3; |
| 2332 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | /* |
| 2336 | * Print the NFA state machine. |
| 2337 | */ |
| 2338 | static void |
| 2339 | nfa_dump(prog) |
| 2340 | nfa_regprog_T *prog; |
| 2341 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2342 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2343 | |
| 2344 | if (debugf != NULL) |
| 2345 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2346 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2347 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 2348 | if (prog->reganch) |
| 2349 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2350 | if (prog->regstart != NUL) |
| 2351 | fprintf(debugf, "regstart: %c (decimal: %d)\n", |
| 2352 | prog->regstart, prog->regstart); |
| 2353 | if (prog->match_text != NULL) |
| 2354 | fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2355 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2356 | fclose(debugf); |
| 2357 | } |
| 2358 | } |
| 2359 | #endif /* ENABLE_LOG */ |
| 2360 | #endif /* DEBUG */ |
| 2361 | |
| 2362 | /* |
| 2363 | * Parse r.e. @expr and convert it into postfix form. |
| 2364 | * Return the postfix string on success, NULL otherwise. |
| 2365 | */ |
| 2366 | static int * |
| 2367 | re2post() |
| 2368 | { |
| 2369 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2370 | return NULL; |
| 2371 | EMIT(NFA_MOPEN); |
| 2372 | return post_start; |
| 2373 | } |
| 2374 | |
| 2375 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2376 | |
| 2377 | /* |
| 2378 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2379 | * if c == MATCH, no arrows out; matching state. |
| 2380 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2381 | * If c < 256, labeled arrow with character c to out. |
| 2382 | */ |
| 2383 | |
| 2384 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2385 | |
| 2386 | /* |
| 2387 | * Allocate and initialize nfa_state_T. |
| 2388 | */ |
| 2389 | static nfa_state_T * |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2390 | alloc_state(c, out, out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2391 | int c; |
| 2392 | nfa_state_T *out; |
| 2393 | nfa_state_T *out1; |
| 2394 | { |
| 2395 | nfa_state_T *s; |
| 2396 | |
| 2397 | if (istate >= nstate) |
| 2398 | return NULL; |
| 2399 | |
| 2400 | s = &state_ptr[istate++]; |
| 2401 | |
| 2402 | s->c = c; |
| 2403 | s->out = out; |
| 2404 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2405 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2406 | |
| 2407 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2408 | s->lastlist[0] = 0; |
| 2409 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2410 | |
| 2411 | return s; |
| 2412 | } |
| 2413 | |
| 2414 | /* |
| 2415 | * A partially built NFA without the matching state filled in. |
| 2416 | * Frag_T.start points at the start state. |
| 2417 | * Frag_T.out is a list of places that need to be set to the |
| 2418 | * next state for this fragment. |
| 2419 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2420 | |
| 2421 | /* Since the out pointers in the list are always |
| 2422 | * uninitialized, we use the pointers themselves |
| 2423 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2424 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2425 | union Ptrlist |
| 2426 | { |
| 2427 | Ptrlist *next; |
| 2428 | nfa_state_T *s; |
| 2429 | }; |
| 2430 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2431 | struct Frag |
| 2432 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2433 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2434 | Ptrlist *out; |
| 2435 | }; |
| 2436 | typedef struct Frag Frag_T; |
| 2437 | |
| 2438 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2439 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2440 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2441 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2442 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2443 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2444 | |
| 2445 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2446 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2447 | */ |
| 2448 | static Frag_T |
| 2449 | frag(start, out) |
| 2450 | nfa_state_T *start; |
| 2451 | Ptrlist *out; |
| 2452 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2453 | Frag_T n; |
| 2454 | |
| 2455 | n.start = start; |
| 2456 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2457 | return n; |
| 2458 | } |
| 2459 | |
| 2460 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2461 | * Create singleton list containing just outp. |
| 2462 | */ |
| 2463 | static Ptrlist * |
| 2464 | list1(outp) |
| 2465 | nfa_state_T **outp; |
| 2466 | { |
| 2467 | Ptrlist *l; |
| 2468 | |
| 2469 | l = (Ptrlist *)outp; |
| 2470 | l->next = NULL; |
| 2471 | return l; |
| 2472 | } |
| 2473 | |
| 2474 | /* |
| 2475 | * Patch the list of states at out to point to start. |
| 2476 | */ |
| 2477 | static void |
| 2478 | patch(l, s) |
| 2479 | Ptrlist *l; |
| 2480 | nfa_state_T *s; |
| 2481 | { |
| 2482 | Ptrlist *next; |
| 2483 | |
| 2484 | for (; l; l = next) |
| 2485 | { |
| 2486 | next = l->next; |
| 2487 | l->s = s; |
| 2488 | } |
| 2489 | } |
| 2490 | |
| 2491 | |
| 2492 | /* |
| 2493 | * Join the two lists l1 and l2, returning the combination. |
| 2494 | */ |
| 2495 | static Ptrlist * |
| 2496 | append(l1, l2) |
| 2497 | Ptrlist *l1; |
| 2498 | Ptrlist *l2; |
| 2499 | { |
| 2500 | Ptrlist *oldl1; |
| 2501 | |
| 2502 | oldl1 = l1; |
| 2503 | while (l1->next) |
| 2504 | l1 = l1->next; |
| 2505 | l1->next = l2; |
| 2506 | return oldl1; |
| 2507 | } |
| 2508 | |
| 2509 | /* |
| 2510 | * Stack used for transforming postfix form into NFA. |
| 2511 | */ |
| 2512 | static Frag_T empty; |
| 2513 | |
| 2514 | static void |
| 2515 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2516 | int *postfix UNUSED; |
| 2517 | int *end UNUSED; |
| 2518 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2519 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2520 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2521 | FILE *df; |
| 2522 | int *p2; |
| 2523 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2524 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2525 | if (df) |
| 2526 | { |
| 2527 | fprintf(df, "Error popping the stack!\n"); |
| 2528 | #ifdef DEBUG |
| 2529 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2530 | #endif |
| 2531 | fprintf(df, "Postfix form is: "); |
| 2532 | #ifdef DEBUG |
| 2533 | for (p2 = postfix; p2 < end; p2++) |
| 2534 | { |
| 2535 | nfa_set_code(*p2); |
| 2536 | fprintf(df, "%s, ", code); |
| 2537 | } |
| 2538 | nfa_set_code(*p); |
| 2539 | fprintf(df, "\nCurrent position is: "); |
| 2540 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2541 | { |
| 2542 | nfa_set_code(*p2); |
| 2543 | fprintf(df, "%s, ", code); |
| 2544 | } |
| 2545 | #else |
| 2546 | for (p2 = postfix; p2 < end; p2++) |
| 2547 | { |
| 2548 | fprintf(df, "%d, ", *p2); |
| 2549 | } |
| 2550 | fprintf(df, "\nCurrent position is: "); |
| 2551 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2552 | { |
| 2553 | fprintf(df, "%d, ", *p2); |
| 2554 | } |
| 2555 | #endif |
| 2556 | fprintf(df, "\n--------------------------\n"); |
| 2557 | fclose(df); |
| 2558 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2559 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2560 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2561 | } |
| 2562 | |
| 2563 | /* |
| 2564 | * Push an item onto the stack. |
| 2565 | */ |
| 2566 | static void |
| 2567 | st_push(s, p, stack_end) |
| 2568 | Frag_T s; |
| 2569 | Frag_T **p; |
| 2570 | Frag_T *stack_end; |
| 2571 | { |
| 2572 | Frag_T *stackp = *p; |
| 2573 | |
| 2574 | if (stackp >= stack_end) |
| 2575 | return; |
| 2576 | *stackp = s; |
| 2577 | *p = *p + 1; |
| 2578 | } |
| 2579 | |
| 2580 | /* |
| 2581 | * Pop an item from the stack. |
| 2582 | */ |
| 2583 | static Frag_T |
| 2584 | st_pop(p, stack) |
| 2585 | Frag_T **p; |
| 2586 | Frag_T *stack; |
| 2587 | { |
| 2588 | Frag_T *stackp; |
| 2589 | |
| 2590 | *p = *p - 1; |
| 2591 | stackp = *p; |
| 2592 | if (stackp < stack) |
| 2593 | return empty; |
| 2594 | return **p; |
| 2595 | } |
| 2596 | |
| 2597 | /* |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2598 | * Estimate the maximum byte length of anything matching "state". |
| 2599 | * When unknown or unlimited return -1. |
| 2600 | */ |
| 2601 | static int |
| 2602 | nfa_max_width(startstate, depth) |
| 2603 | nfa_state_T *startstate; |
| 2604 | int depth; |
| 2605 | { |
| 2606 | int l, r; |
| 2607 | nfa_state_T *state = startstate; |
| 2608 | int len = 0; |
| 2609 | |
| 2610 | /* detect looping in a NFA_SPLIT */ |
| 2611 | if (depth > 4) |
| 2612 | return -1; |
| 2613 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 2614 | while (state != NULL) |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2615 | { |
| 2616 | switch (state->c) |
| 2617 | { |
| 2618 | case NFA_END_INVISIBLE: |
| 2619 | case NFA_END_INVISIBLE_NEG: |
| 2620 | /* the end, return what we have */ |
| 2621 | return len; |
| 2622 | |
| 2623 | case NFA_SPLIT: |
| 2624 | /* two alternatives, use the maximum */ |
| 2625 | l = nfa_max_width(state->out, depth + 1); |
| 2626 | r = nfa_max_width(state->out1, depth + 1); |
| 2627 | if (l < 0 || r < 0) |
| 2628 | return -1; |
| 2629 | return len + (l > r ? l : r); |
| 2630 | |
| 2631 | case NFA_ANY: |
| 2632 | case NFA_START_COLL: |
| 2633 | case NFA_START_NEG_COLL: |
| 2634 | /* matches some character, including composing chars */ |
| 2635 | #ifdef FEAT_MBYTE |
| 2636 | if (enc_utf8) |
| 2637 | len += MB_MAXBYTES; |
| 2638 | else if (has_mbyte) |
| 2639 | len += 2; |
| 2640 | else |
| 2641 | #endif |
| 2642 | ++len; |
| 2643 | if (state->c != NFA_ANY) |
| 2644 | { |
| 2645 | /* skip over the characters */ |
| 2646 | state = state->out1->out; |
| 2647 | continue; |
| 2648 | } |
| 2649 | break; |
| 2650 | |
| 2651 | case NFA_DIGIT: |
| 2652 | case NFA_WHITE: |
| 2653 | case NFA_HEX: |
| 2654 | case NFA_OCTAL: |
| 2655 | /* ascii */ |
| 2656 | ++len; |
| 2657 | break; |
| 2658 | |
| 2659 | case NFA_IDENT: |
| 2660 | case NFA_SIDENT: |
| 2661 | case NFA_KWORD: |
| 2662 | case NFA_SKWORD: |
| 2663 | case NFA_FNAME: |
| 2664 | case NFA_SFNAME: |
| 2665 | case NFA_PRINT: |
| 2666 | case NFA_SPRINT: |
| 2667 | case NFA_NWHITE: |
| 2668 | case NFA_NDIGIT: |
| 2669 | case NFA_NHEX: |
| 2670 | case NFA_NOCTAL: |
| 2671 | case NFA_WORD: |
| 2672 | case NFA_NWORD: |
| 2673 | case NFA_HEAD: |
| 2674 | case NFA_NHEAD: |
| 2675 | case NFA_ALPHA: |
| 2676 | case NFA_NALPHA: |
| 2677 | case NFA_LOWER: |
| 2678 | case NFA_NLOWER: |
| 2679 | case NFA_UPPER: |
| 2680 | case NFA_NUPPER: |
| 2681 | /* possibly non-ascii */ |
| 2682 | #ifdef FEAT_MBYTE |
| 2683 | if (has_mbyte) |
| 2684 | len += 3; |
| 2685 | else |
| 2686 | #endif |
| 2687 | ++len; |
| 2688 | break; |
| 2689 | |
| 2690 | case NFA_START_INVISIBLE: |
| 2691 | case NFA_START_INVISIBLE_NEG: |
| 2692 | case NFA_START_INVISIBLE_BEFORE: |
| 2693 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2694 | /* zero-width, out1 points to the END state */ |
| 2695 | state = state->out1->out; |
| 2696 | continue; |
| 2697 | |
| 2698 | case NFA_BACKREF1: |
| 2699 | case NFA_BACKREF2: |
| 2700 | case NFA_BACKREF3: |
| 2701 | case NFA_BACKREF4: |
| 2702 | case NFA_BACKREF5: |
| 2703 | case NFA_BACKREF6: |
| 2704 | case NFA_BACKREF7: |
| 2705 | case NFA_BACKREF8: |
| 2706 | case NFA_BACKREF9: |
| 2707 | #ifdef FEAT_SYN_HL |
| 2708 | case NFA_ZREF1: |
| 2709 | case NFA_ZREF2: |
| 2710 | case NFA_ZREF3: |
| 2711 | case NFA_ZREF4: |
| 2712 | case NFA_ZREF5: |
| 2713 | case NFA_ZREF6: |
| 2714 | case NFA_ZREF7: |
| 2715 | case NFA_ZREF8: |
| 2716 | case NFA_ZREF9: |
| 2717 | #endif |
| 2718 | case NFA_NEWL: |
| 2719 | case NFA_SKIP: |
| 2720 | /* unknown width */ |
| 2721 | return -1; |
| 2722 | |
| 2723 | case NFA_BOL: |
| 2724 | case NFA_EOL: |
| 2725 | case NFA_BOF: |
| 2726 | case NFA_EOF: |
| 2727 | case NFA_BOW: |
| 2728 | case NFA_EOW: |
| 2729 | case NFA_MOPEN: |
| 2730 | case NFA_MOPEN1: |
| 2731 | case NFA_MOPEN2: |
| 2732 | case NFA_MOPEN3: |
| 2733 | case NFA_MOPEN4: |
| 2734 | case NFA_MOPEN5: |
| 2735 | case NFA_MOPEN6: |
| 2736 | case NFA_MOPEN7: |
| 2737 | case NFA_MOPEN8: |
| 2738 | case NFA_MOPEN9: |
| 2739 | #ifdef FEAT_SYN_HL |
| 2740 | case NFA_ZOPEN: |
| 2741 | case NFA_ZOPEN1: |
| 2742 | case NFA_ZOPEN2: |
| 2743 | case NFA_ZOPEN3: |
| 2744 | case NFA_ZOPEN4: |
| 2745 | case NFA_ZOPEN5: |
| 2746 | case NFA_ZOPEN6: |
| 2747 | case NFA_ZOPEN7: |
| 2748 | case NFA_ZOPEN8: |
| 2749 | case NFA_ZOPEN9: |
| 2750 | case NFA_ZCLOSE: |
| 2751 | case NFA_ZCLOSE1: |
| 2752 | case NFA_ZCLOSE2: |
| 2753 | case NFA_ZCLOSE3: |
| 2754 | case NFA_ZCLOSE4: |
| 2755 | case NFA_ZCLOSE5: |
| 2756 | case NFA_ZCLOSE6: |
| 2757 | case NFA_ZCLOSE7: |
| 2758 | case NFA_ZCLOSE8: |
| 2759 | case NFA_ZCLOSE9: |
| 2760 | #endif |
| 2761 | case NFA_MCLOSE: |
| 2762 | case NFA_MCLOSE1: |
| 2763 | case NFA_MCLOSE2: |
| 2764 | case NFA_MCLOSE3: |
| 2765 | case NFA_MCLOSE4: |
| 2766 | case NFA_MCLOSE5: |
| 2767 | case NFA_MCLOSE6: |
| 2768 | case NFA_MCLOSE7: |
| 2769 | case NFA_MCLOSE8: |
| 2770 | case NFA_MCLOSE9: |
| 2771 | case NFA_NOPEN: |
| 2772 | case NFA_NCLOSE: |
| 2773 | |
| 2774 | case NFA_LNUM_GT: |
| 2775 | case NFA_LNUM_LT: |
| 2776 | case NFA_COL_GT: |
| 2777 | case NFA_COL_LT: |
| 2778 | case NFA_VCOL_GT: |
| 2779 | case NFA_VCOL_LT: |
| 2780 | case NFA_MARK_GT: |
| 2781 | case NFA_MARK_LT: |
| 2782 | case NFA_VISUAL: |
| 2783 | case NFA_LNUM: |
| 2784 | case NFA_CURSOR: |
| 2785 | case NFA_COL: |
| 2786 | case NFA_VCOL: |
| 2787 | case NFA_MARK: |
| 2788 | |
| 2789 | case NFA_ZSTART: |
| 2790 | case NFA_ZEND: |
| 2791 | case NFA_OPT_CHARS: |
| 2792 | case NFA_SKIP_CHAR: |
| 2793 | case NFA_START_PATTERN: |
| 2794 | case NFA_END_PATTERN: |
| 2795 | case NFA_COMPOSING: |
| 2796 | case NFA_END_COMPOSING: |
| 2797 | /* zero-width */ |
| 2798 | break; |
| 2799 | |
| 2800 | default: |
| 2801 | if (state->c < 0) |
| 2802 | /* don't know what this is */ |
| 2803 | return -1; |
| 2804 | /* normal character */ |
| 2805 | len += MB_CHAR2LEN(state->c); |
| 2806 | break; |
| 2807 | } |
| 2808 | |
| 2809 | /* normal way to continue */ |
| 2810 | state = state->out; |
| 2811 | } |
| 2812 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 2813 | /* unrecognized, "cannot happen" */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2814 | return -1; |
| 2815 | } |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 2816 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2817 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2818 | * Convert a postfix form into its equivalent NFA. |
| 2819 | * Return the NFA start state on success, NULL otherwise. |
| 2820 | */ |
| 2821 | static nfa_state_T * |
| 2822 | post2nfa(postfix, end, nfa_calc_size) |
| 2823 | int *postfix; |
| 2824 | int *end; |
| 2825 | int nfa_calc_size; |
| 2826 | { |
| 2827 | int *p; |
| 2828 | int mopen; |
| 2829 | int mclose; |
| 2830 | Frag_T *stack = NULL; |
| 2831 | Frag_T *stackp = NULL; |
| 2832 | Frag_T *stack_end = NULL; |
| 2833 | Frag_T e1; |
| 2834 | Frag_T e2; |
| 2835 | Frag_T e; |
| 2836 | nfa_state_T *s; |
| 2837 | nfa_state_T *s1; |
| 2838 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2839 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2840 | |
| 2841 | if (postfix == NULL) |
| 2842 | return NULL; |
| 2843 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2844 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2845 | #define POP() st_pop(&stackp, stack); \ |
| 2846 | if (stackp < stack) \ |
| 2847 | { \ |
| 2848 | st_error(postfix, end, p); \ |
| 2849 | return NULL; \ |
| 2850 | } |
| 2851 | |
| 2852 | if (nfa_calc_size == FALSE) |
| 2853 | { |
| 2854 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2855 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2856 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2857 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | for (p = postfix; p < end; ++p) |
| 2861 | { |
| 2862 | switch (*p) |
| 2863 | { |
| 2864 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2865 | /* Concatenation. |
| 2866 | * Pay attention: this operator does not exist in the r.e. itself |
| 2867 | * (it is implicit, really). It is added when r.e. is translated |
| 2868 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2869 | if (nfa_calc_size == TRUE) |
| 2870 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2871 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2872 | break; |
| 2873 | } |
| 2874 | e2 = POP(); |
| 2875 | e1 = POP(); |
| 2876 | patch(e1.out, e2.start); |
| 2877 | PUSH(frag(e1.start, e2.out)); |
| 2878 | break; |
| 2879 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2880 | case NFA_OR: |
| 2881 | /* Alternation */ |
| 2882 | if (nfa_calc_size == TRUE) |
| 2883 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2884 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2885 | break; |
| 2886 | } |
| 2887 | e2 = POP(); |
| 2888 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2889 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2890 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2891 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2892 | PUSH(frag(s, append(e1.out, e2.out))); |
| 2893 | break; |
| 2894 | |
| 2895 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2896 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2897 | if (nfa_calc_size == TRUE) |
| 2898 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2899 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2900 | break; |
| 2901 | } |
| 2902 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2903 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2904 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2905 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2906 | patch(e.out, s); |
| 2907 | PUSH(frag(s, list1(&s->out1))); |
| 2908 | break; |
| 2909 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2910 | case NFA_STAR_NONGREEDY: |
| 2911 | /* Zero or more, prefer zero */ |
| 2912 | if (nfa_calc_size == TRUE) |
| 2913 | { |
| 2914 | nstate++; |
| 2915 | break; |
| 2916 | } |
| 2917 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2918 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2919 | if (s == NULL) |
| 2920 | goto theend; |
| 2921 | patch(e.out, s); |
| 2922 | PUSH(frag(s, list1(&s->out))); |
| 2923 | break; |
| 2924 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2925 | case NFA_QUEST: |
| 2926 | /* one or zero atoms=> greedy match */ |
| 2927 | if (nfa_calc_size == TRUE) |
| 2928 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2929 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2930 | break; |
| 2931 | } |
| 2932 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2933 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2934 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2935 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2936 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 2937 | break; |
| 2938 | |
| 2939 | case NFA_QUEST_NONGREEDY: |
| 2940 | /* zero or one atoms => non-greedy match */ |
| 2941 | if (nfa_calc_size == TRUE) |
| 2942 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2943 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2944 | break; |
| 2945 | } |
| 2946 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2947 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2948 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2949 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2950 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 2951 | break; |
| 2952 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2953 | case NFA_END_COLL: |
| 2954 | case NFA_END_NEG_COLL: |
| 2955 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 2956 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 2957 | * add the output to the start. */ |
| 2958 | if (nfa_calc_size == TRUE) |
| 2959 | { |
| 2960 | nstate++; |
| 2961 | break; |
| 2962 | } |
| 2963 | e = POP(); |
| 2964 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 2965 | if (s == NULL) |
| 2966 | goto theend; |
| 2967 | patch(e.out, s); |
| 2968 | e.start->out1 = s; |
| 2969 | PUSH(frag(e.start, list1(&s->out))); |
| 2970 | break; |
| 2971 | |
| 2972 | case NFA_RANGE: |
| 2973 | /* Before this are two characters, the low and high end of a |
| 2974 | * range. Turn them into two states with MIN and MAX. */ |
| 2975 | if (nfa_calc_size == TRUE) |
| 2976 | { |
| 2977 | /* nstate += 0; */ |
| 2978 | break; |
| 2979 | } |
| 2980 | e2 = POP(); |
| 2981 | e1 = POP(); |
| 2982 | e2.start->val = e2.start->c; |
| 2983 | e2.start->c = NFA_RANGE_MAX; |
| 2984 | e1.start->val = e1.start->c; |
| 2985 | e1.start->c = NFA_RANGE_MIN; |
| 2986 | patch(e1.out, e2.start); |
| 2987 | PUSH(frag(e1.start, e2.out)); |
| 2988 | break; |
| 2989 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2990 | case NFA_SKIP_CHAR: |
| 2991 | /* Symbol of 0-length, Used in a repetition |
| 2992 | * with max/min count of 0 */ |
| 2993 | if (nfa_calc_size == TRUE) |
| 2994 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2995 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2996 | break; |
| 2997 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2998 | s = alloc_state(NFA_SKIP_CHAR, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2999 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3000 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3001 | PUSH(frag(s, list1(&s->out))); |
| 3002 | break; |
| 3003 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3004 | case NFA_OPT_CHARS: |
| 3005 | { |
| 3006 | int n; |
| 3007 | |
| 3008 | /* \%[abc] */ |
| 3009 | n = *++p; /* get number of characters */ |
| 3010 | if (nfa_calc_size == TRUE) |
| 3011 | { |
| 3012 | nstate += n; |
| 3013 | break; |
| 3014 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 3015 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3016 | e1.out = NULL; /* stores list with out1's */ |
| 3017 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 3018 | while (n-- > 0) |
| 3019 | { |
| 3020 | e = POP(); /* get character */ |
| 3021 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 3022 | if (s == NULL) |
| 3023 | goto theend; |
| 3024 | if (e1.out == NULL) |
| 3025 | e1 = e; |
| 3026 | patch(e.out, s1); |
| 3027 | append(e1.out, list1(&s->out1)); |
| 3028 | s1 = s; |
| 3029 | } |
| 3030 | PUSH(frag(s, e1.out)); |
| 3031 | break; |
| 3032 | } |
| 3033 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3034 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3035 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3036 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3037 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3038 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3039 | { |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3040 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 3041 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3042 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3043 | int start_state; |
| 3044 | int end_state; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3045 | int n = 0; |
| 3046 | nfa_state_T *zend; |
| 3047 | nfa_state_T *skip; |
| 3048 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3049 | switch (*p) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3050 | { |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3051 | case NFA_PREV_ATOM_NO_WIDTH: |
| 3052 | start_state = NFA_START_INVISIBLE; |
| 3053 | end_state = NFA_END_INVISIBLE; |
| 3054 | break; |
| 3055 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 3056 | start_state = NFA_START_INVISIBLE_NEG; |
| 3057 | end_state = NFA_END_INVISIBLE_NEG; |
| 3058 | break; |
| 3059 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3060 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 3061 | end_state = NFA_END_INVISIBLE; |
| 3062 | break; |
| 3063 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 3064 | start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
| 3065 | end_state = NFA_END_INVISIBLE_NEG; |
| 3066 | break; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 3067 | default: /* NFA_PREV_ATOM_LIKE_PATTERN: */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3068 | start_state = NFA_START_PATTERN; |
| 3069 | end_state = NFA_END_PATTERN; |
| 3070 | break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3071 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3072 | |
| 3073 | if (before) |
| 3074 | n = *++p; /* get the count */ |
| 3075 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3076 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3077 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3078 | * The \@<= operator: match for the preceding atom. |
| 3079 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3080 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3081 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3082 | |
| 3083 | if (nfa_calc_size == TRUE) |
| 3084 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3085 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3086 | break; |
| 3087 | } |
| 3088 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3089 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3090 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3091 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3092 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3093 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3094 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3095 | goto theend; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3096 | if (pattern) |
| 3097 | { |
| 3098 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 3099 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 3100 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 3101 | s1->out= skip; |
| 3102 | patch(e.out, zend); |
| 3103 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3104 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3105 | else |
| 3106 | { |
| 3107 | patch(e.out, s1); |
| 3108 | PUSH(frag(s, list1(&s1->out))); |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3109 | if (before) |
| 3110 | { |
| 3111 | if (n <= 0) |
| 3112 | /* See if we can guess the maximum width, it avoids a |
| 3113 | * lot of pointless tries. */ |
| 3114 | n = nfa_max_width(e.start, 0); |
| 3115 | s->val = n; /* store the count */ |
| 3116 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3117 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3118 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3119 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3120 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3121 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3122 | case NFA_COMPOSING: /* char with composing char */ |
| 3123 | #if 0 |
| 3124 | /* TODO */ |
| 3125 | if (regflags & RF_ICOMBINE) |
| 3126 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3127 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3128 | } |
| 3129 | #endif |
| 3130 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3131 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3132 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3133 | case NFA_MOPEN: /* \( \) Submatch */ |
| 3134 | case NFA_MOPEN1: |
| 3135 | case NFA_MOPEN2: |
| 3136 | case NFA_MOPEN3: |
| 3137 | case NFA_MOPEN4: |
| 3138 | case NFA_MOPEN5: |
| 3139 | case NFA_MOPEN6: |
| 3140 | case NFA_MOPEN7: |
| 3141 | case NFA_MOPEN8: |
| 3142 | case NFA_MOPEN9: |
| 3143 | #ifdef FEAT_SYN_HL |
| 3144 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 3145 | case NFA_ZOPEN1: |
| 3146 | case NFA_ZOPEN2: |
| 3147 | case NFA_ZOPEN3: |
| 3148 | case NFA_ZOPEN4: |
| 3149 | case NFA_ZOPEN5: |
| 3150 | case NFA_ZOPEN6: |
| 3151 | case NFA_ZOPEN7: |
| 3152 | case NFA_ZOPEN8: |
| 3153 | case NFA_ZOPEN9: |
| 3154 | #endif |
| 3155 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3156 | if (nfa_calc_size == TRUE) |
| 3157 | { |
| 3158 | nstate += 2; |
| 3159 | break; |
| 3160 | } |
| 3161 | |
| 3162 | mopen = *p; |
| 3163 | switch (*p) |
| 3164 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3165 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 3166 | #ifdef FEAT_SYN_HL |
| 3167 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 3168 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 3169 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 3170 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 3171 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 3172 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 3173 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 3174 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 3175 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 3176 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 3177 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3178 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3179 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3180 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3181 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3182 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3183 | mclose = *p + NSUBEXP; |
| 3184 | break; |
| 3185 | } |
| 3186 | |
| 3187 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 3188 | * the empty regexp "". In this case, the NFA will be |
| 3189 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 3190 | * empty groups of parenthesis, and empty mbyte chars */ |
| 3191 | if (stackp == stack) |
| 3192 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3193 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3194 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3195 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3196 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3197 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3198 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3199 | patch(list1(&s->out), s1); |
| 3200 | PUSH(frag(s, list1(&s1->out))); |
| 3201 | break; |
| 3202 | } |
| 3203 | |
| 3204 | /* At least one node was emitted before NFA_MOPEN, so |
| 3205 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 3206 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3207 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3208 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3209 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3210 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3211 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3212 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3213 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3214 | patch(e.out, s1); |
| 3215 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3216 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3217 | if (mopen == NFA_COMPOSING) |
| 3218 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3219 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3220 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3221 | |
| 3222 | PUSH(frag(s, list1(&s1->out))); |
| 3223 | break; |
| 3224 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3225 | case NFA_BACKREF1: |
| 3226 | case NFA_BACKREF2: |
| 3227 | case NFA_BACKREF3: |
| 3228 | case NFA_BACKREF4: |
| 3229 | case NFA_BACKREF5: |
| 3230 | case NFA_BACKREF6: |
| 3231 | case NFA_BACKREF7: |
| 3232 | case NFA_BACKREF8: |
| 3233 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3234 | #ifdef FEAT_SYN_HL |
| 3235 | case NFA_ZREF1: |
| 3236 | case NFA_ZREF2: |
| 3237 | case NFA_ZREF3: |
| 3238 | case NFA_ZREF4: |
| 3239 | case NFA_ZREF5: |
| 3240 | case NFA_ZREF6: |
| 3241 | case NFA_ZREF7: |
| 3242 | case NFA_ZREF8: |
| 3243 | case NFA_ZREF9: |
| 3244 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3245 | if (nfa_calc_size == TRUE) |
| 3246 | { |
| 3247 | nstate += 2; |
| 3248 | break; |
| 3249 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3250 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3251 | if (s == NULL) |
| 3252 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3253 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3254 | if (s1 == NULL) |
| 3255 | goto theend; |
| 3256 | patch(list1(&s->out), s1); |
| 3257 | PUSH(frag(s, list1(&s1->out))); |
| 3258 | break; |
| 3259 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3260 | case NFA_LNUM: |
| 3261 | case NFA_LNUM_GT: |
| 3262 | case NFA_LNUM_LT: |
| 3263 | case NFA_VCOL: |
| 3264 | case NFA_VCOL_GT: |
| 3265 | case NFA_VCOL_LT: |
| 3266 | case NFA_COL: |
| 3267 | case NFA_COL_GT: |
| 3268 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 3269 | case NFA_MARK: |
| 3270 | case NFA_MARK_GT: |
| 3271 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3272 | { |
| 3273 | int n = *++p; /* lnum, col or mark name */ |
| 3274 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3275 | if (nfa_calc_size == TRUE) |
| 3276 | { |
| 3277 | nstate += 1; |
| 3278 | break; |
| 3279 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3280 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3281 | if (s == NULL) |
| 3282 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3283 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3284 | PUSH(frag(s, list1(&s->out))); |
| 3285 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3286 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3287 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3288 | case NFA_ZSTART: |
| 3289 | case NFA_ZEND: |
| 3290 | default: |
| 3291 | /* Operands */ |
| 3292 | if (nfa_calc_size == TRUE) |
| 3293 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3294 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3295 | break; |
| 3296 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3297 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3298 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3299 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3300 | PUSH(frag(s, list1(&s->out))); |
| 3301 | break; |
| 3302 | |
| 3303 | } /* switch(*p) */ |
| 3304 | |
| 3305 | } /* for(p = postfix; *p; ++p) */ |
| 3306 | |
| 3307 | if (nfa_calc_size == TRUE) |
| 3308 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3309 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3310 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3311 | } |
| 3312 | |
| 3313 | e = POP(); |
| 3314 | if (stackp != stack) |
| 3315 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 3316 | |
| 3317 | if (istate >= nstate) |
| 3318 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 3319 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3320 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3321 | matchstate->c = NFA_MATCH; |
| 3322 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3323 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3324 | |
| 3325 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3326 | ret = e.start; |
| 3327 | |
| 3328 | theend: |
| 3329 | vim_free(stack); |
| 3330 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3331 | |
| 3332 | #undef POP1 |
| 3333 | #undef PUSH1 |
| 3334 | #undef POP2 |
| 3335 | #undef PUSH2 |
| 3336 | #undef POP |
| 3337 | #undef PUSH |
| 3338 | } |
| 3339 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3340 | /* |
| 3341 | * After building the NFA program, inspect it to add optimization hints. |
| 3342 | */ |
| 3343 | static void |
| 3344 | nfa_postprocess(prog) |
| 3345 | nfa_regprog_T *prog; |
| 3346 | { |
| 3347 | int i; |
| 3348 | int c; |
| 3349 | |
| 3350 | for (i = 0; i < prog->nstate; ++i) |
| 3351 | { |
| 3352 | c = prog->state[i].c; |
| 3353 | if (c == NFA_START_INVISIBLE |
| 3354 | || c == NFA_START_INVISIBLE_NEG |
| 3355 | || c == NFA_START_INVISIBLE_BEFORE |
| 3356 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3357 | { |
| 3358 | int directly; |
| 3359 | |
| 3360 | /* Do it directly when what follows is possibly the end of the |
| 3361 | * match. */ |
| 3362 | if (match_follows(prog->state[i].out1->out, 0)) |
| 3363 | directly = TRUE; |
| 3364 | else |
| 3365 | { |
| 3366 | int ch_invisible = failure_chance(prog->state[i].out, 0); |
| 3367 | int ch_follows = failure_chance(prog->state[i].out1->out, 0); |
| 3368 | |
| 3369 | /* Postpone when the invisible match is expensive or has a |
| 3370 | * lower chance of failing. */ |
| 3371 | if (c == NFA_START_INVISIBLE_BEFORE |
| 3372 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3373 | { |
| 3374 | /* "before" matches are very expensive when |
| 3375 | * unbounded, always prefer what follows then, |
| 3376 | * unless what follows will always match. |
| 3377 | * Otherwise strongly prefer what follows. */ |
| 3378 | if (prog->state[i].val <= 0 && ch_follows > 0) |
| 3379 | directly = FALSE; |
| 3380 | else |
| 3381 | directly = ch_follows * 10 < ch_invisible; |
| 3382 | } |
| 3383 | else |
| 3384 | { |
| 3385 | /* normal invisible, first do the one with the |
| 3386 | * highest failure chance */ |
| 3387 | directly = ch_follows < ch_invisible; |
| 3388 | } |
| 3389 | } |
| 3390 | if (directly) |
| 3391 | /* switch to the _FIRST state */ |
| 3392 | ++prog->state[i].c; |
| 3393 | } |
| 3394 | } |
| 3395 | } |
| 3396 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3397 | /**************************************************************** |
| 3398 | * NFA execution code. |
| 3399 | ****************************************************************/ |
| 3400 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3401 | typedef struct |
| 3402 | { |
| 3403 | int in_use; /* number of subexpr with useful info */ |
| 3404 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3405 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3406 | union |
| 3407 | { |
| 3408 | struct multipos |
| 3409 | { |
| 3410 | lpos_T start; |
| 3411 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3412 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3413 | struct linepos |
| 3414 | { |
| 3415 | char_u *start; |
| 3416 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3417 | } line[NSUBEXP]; |
| 3418 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3419 | } regsub_T; |
| 3420 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3421 | typedef struct |
| 3422 | { |
| 3423 | regsub_T norm; /* \( .. \) matches */ |
| 3424 | #ifdef FEAT_SYN_HL |
| 3425 | regsub_T synt; /* \z( .. \) matches */ |
| 3426 | #endif |
| 3427 | } regsubs_T; |
| 3428 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3429 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3430 | typedef struct nfa_pim_S nfa_pim_T; |
| 3431 | struct nfa_pim_S |
| 3432 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3433 | int result; /* NFA_PIM_*, see below */ |
| 3434 | nfa_state_T *state; /* the invisible match start state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3435 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3436 | union |
| 3437 | { |
| 3438 | lpos_T pos; |
| 3439 | char_u *ptr; |
| 3440 | } end; /* where the match must end */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3441 | }; |
| 3442 | |
| 3443 | /* Values for done in nfa_pim_T. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3444 | #define NFA_PIM_UNUSED 0 /* pim not used */ |
| 3445 | #define NFA_PIM_TODO 1 /* pim not done yet */ |
| 3446 | #define NFA_PIM_MATCH 2 /* pim executed, matches */ |
| 3447 | #define NFA_PIM_NOMATCH 3 /* pim executed, no match */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3448 | |
| 3449 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3450 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3451 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3452 | { |
| 3453 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3454 | int count; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3455 | nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed |
| 3456 | * invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3457 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3458 | } nfa_thread_T; |
| 3459 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3460 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3461 | typedef struct |
| 3462 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3463 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3464 | int n; /* nr of states currently in "t" */ |
| 3465 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3466 | int id; /* ID of the list */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3467 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3468 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3469 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3470 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 3471 | static void log_subexpr __ARGS((regsub_T *sub)); |
| 3472 | |
| 3473 | static void |
| 3474 | log_subsexpr(subs) |
| 3475 | regsubs_T *subs; |
| 3476 | { |
| 3477 | log_subexpr(&subs->norm); |
| 3478 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3479 | if (nfa_has_zsubexpr) |
| 3480 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3481 | # endif |
| 3482 | } |
| 3483 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3484 | static void |
| 3485 | log_subexpr(sub) |
| 3486 | regsub_T *sub; |
| 3487 | { |
| 3488 | int j; |
| 3489 | |
| 3490 | for (j = 0; j < sub->in_use; j++) |
| 3491 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3492 | 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] | 3493 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3494 | sub->list.multi[j].start.col, |
| 3495 | (int)sub->list.multi[j].start.lnum, |
| 3496 | sub->list.multi[j].end.col, |
| 3497 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3498 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3499 | { |
| 3500 | char *s = (char *)sub->list.line[j].start; |
| 3501 | char *e = (char *)sub->list.line[j].end; |
| 3502 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3503 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3504 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3505 | s == NULL ? "NULL" : s, |
| 3506 | e == NULL ? "NULL" : e); |
| 3507 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3508 | } |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3509 | |
| 3510 | static char * |
| 3511 | pim_info(nfa_pim_T *pim) |
| 3512 | { |
| 3513 | static char buf[30]; |
| 3514 | |
| 3515 | if (pim == NULL || pim->result == NFA_PIM_UNUSED) |
| 3516 | buf[0] = NUL; |
| 3517 | else |
| 3518 | { |
| 3519 | sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col |
| 3520 | : (int)(pim->end.ptr - reginput)); |
| 3521 | } |
| 3522 | return buf; |
| 3523 | } |
| 3524 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3525 | #endif |
| 3526 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3527 | /* Used during execution: whether a match has been found. */ |
| 3528 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3529 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3530 | static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3531 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3532 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3533 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3534 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3535 | static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs)); |
| 3536 | static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs)); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3537 | static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int off)); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3538 | 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] | 3539 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3540 | /* |
| 3541 | * Copy postponed invisible match info from "from" to "to". |
| 3542 | */ |
| 3543 | static void |
| 3544 | copy_pim(to, from) |
| 3545 | nfa_pim_T *to; |
| 3546 | nfa_pim_T *from; |
| 3547 | { |
| 3548 | to->result = from->result; |
| 3549 | to->state = from->state; |
| 3550 | copy_sub(&to->subs.norm, &from->subs.norm); |
| 3551 | #ifdef FEAT_SYN_HL |
| 3552 | if (nfa_has_zsubexpr) |
| 3553 | copy_sub(&to->subs.synt, &from->subs.synt); |
| 3554 | #endif |
| 3555 | to->end = from->end; |
| 3556 | } |
| 3557 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3558 | static void |
| 3559 | clear_sub(sub) |
| 3560 | regsub_T *sub; |
| 3561 | { |
| 3562 | if (REG_MULTI) |
| 3563 | /* Use 0xff to set lnum to -1 */ |
| 3564 | vim_memset(sub->list.multi, 0xff, |
| 3565 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3566 | else |
| 3567 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3568 | sub->in_use = 0; |
| 3569 | } |
| 3570 | |
| 3571 | /* |
| 3572 | * Copy the submatches from "from" to "to". |
| 3573 | */ |
| 3574 | static void |
| 3575 | copy_sub(to, from) |
| 3576 | regsub_T *to; |
| 3577 | regsub_T *from; |
| 3578 | { |
| 3579 | to->in_use = from->in_use; |
| 3580 | if (from->in_use > 0) |
| 3581 | { |
| 3582 | /* Copy the match start and end positions. */ |
| 3583 | if (REG_MULTI) |
| 3584 | mch_memmove(&to->list.multi[0], |
| 3585 | &from->list.multi[0], |
| 3586 | sizeof(struct multipos) * from->in_use); |
| 3587 | else |
| 3588 | mch_memmove(&to->list.line[0], |
| 3589 | &from->list.line[0], |
| 3590 | sizeof(struct linepos) * from->in_use); |
| 3591 | } |
| 3592 | } |
| 3593 | |
| 3594 | /* |
| 3595 | * Like copy_sub() but exclude the main match. |
| 3596 | */ |
| 3597 | static void |
| 3598 | copy_sub_off(to, from) |
| 3599 | regsub_T *to; |
| 3600 | regsub_T *from; |
| 3601 | { |
| 3602 | if (to->in_use < from->in_use) |
| 3603 | to->in_use = from->in_use; |
| 3604 | if (from->in_use > 1) |
| 3605 | { |
| 3606 | /* Copy the match start and end positions. */ |
| 3607 | if (REG_MULTI) |
| 3608 | mch_memmove(&to->list.multi[1], |
| 3609 | &from->list.multi[1], |
| 3610 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3611 | else |
| 3612 | mch_memmove(&to->list.line[1], |
| 3613 | &from->list.line[1], |
| 3614 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3615 | } |
| 3616 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3617 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3618 | /* |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3619 | * Return TRUE if "sub1" and "sub2" have the same start positions. |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3620 | */ |
| 3621 | static int |
| 3622 | sub_equal(sub1, sub2) |
| 3623 | regsub_T *sub1; |
| 3624 | regsub_T *sub2; |
| 3625 | { |
| 3626 | int i; |
| 3627 | int todo; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3628 | linenr_T s1; |
| 3629 | linenr_T s2; |
| 3630 | char_u *sp1; |
| 3631 | char_u *sp2; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3632 | |
| 3633 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3634 | if (REG_MULTI) |
| 3635 | { |
| 3636 | for (i = 0; i < todo; ++i) |
| 3637 | { |
| 3638 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3639 | s1 = sub1->list.multi[i].start.lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3640 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3641 | s1 = 0; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3642 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3643 | s2 = sub2->list.multi[i].start.lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3644 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3645 | s2 = 0; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3646 | if (s1 != s2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3647 | return FALSE; |
| 3648 | if (s1 != 0 && sub1->list.multi[i].start.col |
| 3649 | != sub2->list.multi[i].start.col) |
| 3650 | return FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3651 | } |
| 3652 | } |
| 3653 | else |
| 3654 | { |
| 3655 | for (i = 0; i < todo; ++i) |
| 3656 | { |
| 3657 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3658 | sp1 = sub1->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3659 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3660 | sp1 = NULL; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3661 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3662 | sp2 = sub2->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3663 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3664 | sp2 = NULL; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3665 | if (sp1 != sp2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3666 | return FALSE; |
| 3667 | } |
| 3668 | } |
| 3669 | |
| 3670 | return TRUE; |
| 3671 | } |
| 3672 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3673 | #ifdef ENABLE_LOG |
| 3674 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3675 | report_state(char *action, |
| 3676 | regsub_T *sub, |
| 3677 | nfa_state_T *state, |
| 3678 | int lid, |
| 3679 | nfa_pim_T *pim) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3680 | { |
| 3681 | int col; |
| 3682 | |
| 3683 | if (sub->in_use <= 0) |
| 3684 | col = -1; |
| 3685 | else if (REG_MULTI) |
| 3686 | col = sub->list.multi[0].start.col; |
| 3687 | else |
| 3688 | col = (int)(sub->list.line[0].start - regline); |
| 3689 | nfa_set_code(state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3690 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n", |
| 3691 | action, abs(state->id), lid, state->c, code, col, |
| 3692 | pim_info(pim)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3693 | } |
| 3694 | #endif |
| 3695 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3696 | /* |
| 3697 | * Return TRUE if the same state is already in list "l" with the same |
| 3698 | * positions as "subs". |
| 3699 | */ |
| 3700 | static int |
| 3701 | has_state_with_pos(l, state, subs) |
| 3702 | nfa_list_T *l; /* runtime state list */ |
| 3703 | nfa_state_T *state; /* state to update */ |
| 3704 | regsubs_T *subs; /* pointers to subexpressions */ |
| 3705 | { |
| 3706 | nfa_thread_T *thread; |
| 3707 | int i; |
| 3708 | |
| 3709 | for (i = 0; i < l->n; ++i) |
| 3710 | { |
| 3711 | thread = &l->t[i]; |
| 3712 | if (thread->state->id == state->id |
| 3713 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 3714 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3715 | && (!nfa_has_zsubexpr |
| 3716 | || sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3717 | #endif |
| 3718 | ) |
| 3719 | return TRUE; |
| 3720 | } |
| 3721 | return FALSE; |
| 3722 | } |
| 3723 | |
| 3724 | /* |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3725 | * Return TRUE if "state" leads to a NFA_MATCH without advancing the input. |
| 3726 | */ |
| 3727 | static int |
| 3728 | match_follows(startstate, depth) |
| 3729 | nfa_state_T *startstate; |
| 3730 | int depth; |
| 3731 | { |
| 3732 | nfa_state_T *state = startstate; |
| 3733 | |
| 3734 | /* avoid too much recursion */ |
| 3735 | if (depth > 10) |
| 3736 | return FALSE; |
| 3737 | |
| 3738 | for (;;) |
| 3739 | { |
| 3740 | switch (state->c) |
| 3741 | { |
| 3742 | case NFA_MATCH: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3743 | case NFA_MCLOSE: |
| 3744 | case NFA_END_INVISIBLE: |
| 3745 | case NFA_END_INVISIBLE_NEG: |
| 3746 | case NFA_END_PATTERN: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3747 | return TRUE; |
| 3748 | |
| 3749 | case NFA_SPLIT: |
| 3750 | return match_follows(state->out, depth + 1) |
| 3751 | || match_follows(state->out1, depth + 1); |
| 3752 | |
| 3753 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3754 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3755 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3756 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3757 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3758 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3759 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3760 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3761 | case NFA_COMPOSING: |
| 3762 | /* skip ahead to next state */ |
| 3763 | state = state->out1->out; |
| 3764 | break; |
| 3765 | |
| 3766 | case NFA_ANY: |
| 3767 | case NFA_IDENT: |
| 3768 | case NFA_SIDENT: |
| 3769 | case NFA_KWORD: |
| 3770 | case NFA_SKWORD: |
| 3771 | case NFA_FNAME: |
| 3772 | case NFA_SFNAME: |
| 3773 | case NFA_PRINT: |
| 3774 | case NFA_SPRINT: |
| 3775 | case NFA_WHITE: |
| 3776 | case NFA_NWHITE: |
| 3777 | case NFA_DIGIT: |
| 3778 | case NFA_NDIGIT: |
| 3779 | case NFA_HEX: |
| 3780 | case NFA_NHEX: |
| 3781 | case NFA_OCTAL: |
| 3782 | case NFA_NOCTAL: |
| 3783 | case NFA_WORD: |
| 3784 | case NFA_NWORD: |
| 3785 | case NFA_HEAD: |
| 3786 | case NFA_NHEAD: |
| 3787 | case NFA_ALPHA: |
| 3788 | case NFA_NALPHA: |
| 3789 | case NFA_LOWER: |
| 3790 | case NFA_NLOWER: |
| 3791 | case NFA_UPPER: |
| 3792 | case NFA_NUPPER: |
| 3793 | case NFA_START_COLL: |
| 3794 | case NFA_START_NEG_COLL: |
| 3795 | case NFA_NEWL: |
| 3796 | /* state will advance input */ |
| 3797 | return FALSE; |
| 3798 | |
| 3799 | default: |
| 3800 | if (state->c > 0) |
| 3801 | /* state will advance input */ |
| 3802 | return FALSE; |
| 3803 | |
| 3804 | /* Others: zero-width or possibly zero-width, might still find |
| 3805 | * a match at the same position, keep looking. */ |
| 3806 | break; |
| 3807 | } |
| 3808 | state = state->out; |
| 3809 | } |
| 3810 | return FALSE; |
| 3811 | } |
| 3812 | |
| 3813 | |
| 3814 | /* |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3815 | * Return TRUE if "state" is already in list "l". |
| 3816 | */ |
| 3817 | static int |
| 3818 | state_in_list(l, state, subs) |
| 3819 | nfa_list_T *l; /* runtime state list */ |
| 3820 | nfa_state_T *state; /* state to update */ |
| 3821 | regsubs_T *subs; /* pointers to subexpressions */ |
| 3822 | { |
| 3823 | if (state->lastlist[nfa_ll_index] == l->id) |
| 3824 | { |
| 3825 | if (!nfa_has_backref || has_state_with_pos(l, state, subs)) |
| 3826 | return TRUE; |
| 3827 | } |
| 3828 | return FALSE; |
| 3829 | } |
| 3830 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3831 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3832 | addstate(l, state, subs, pim, off) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3833 | nfa_list_T *l; /* runtime state list */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3834 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3835 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3836 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3837 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3838 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3839 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3840 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3841 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3842 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3843 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3844 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3845 | regsub_T *sub; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3846 | #ifdef ENABLE_LOG |
| 3847 | int did_print = FALSE; |
| 3848 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3849 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3850 | switch (state->c) |
| 3851 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3852 | case NFA_NCLOSE: |
| 3853 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3854 | case NFA_MCLOSE1: |
| 3855 | case NFA_MCLOSE2: |
| 3856 | case NFA_MCLOSE3: |
| 3857 | case NFA_MCLOSE4: |
| 3858 | case NFA_MCLOSE5: |
| 3859 | case NFA_MCLOSE6: |
| 3860 | case NFA_MCLOSE7: |
| 3861 | case NFA_MCLOSE8: |
| 3862 | case NFA_MCLOSE9: |
| 3863 | #ifdef FEAT_SYN_HL |
| 3864 | case NFA_ZCLOSE: |
| 3865 | case NFA_ZCLOSE1: |
| 3866 | case NFA_ZCLOSE2: |
| 3867 | case NFA_ZCLOSE3: |
| 3868 | case NFA_ZCLOSE4: |
| 3869 | case NFA_ZCLOSE5: |
| 3870 | case NFA_ZCLOSE6: |
| 3871 | case NFA_ZCLOSE7: |
| 3872 | case NFA_ZCLOSE8: |
| 3873 | case NFA_ZCLOSE9: |
| 3874 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3875 | case NFA_ZEND: |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 3876 | case NFA_SPLIT: |
| 3877 | case NFA_NOPEN: |
| 3878 | case NFA_SKIP_CHAR: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3879 | /* These nodes are not added themselves but their "out" and/or |
| 3880 | * "out1" may be added below. */ |
| 3881 | break; |
| 3882 | |
| 3883 | case NFA_MOPEN: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3884 | case NFA_MOPEN1: |
| 3885 | case NFA_MOPEN2: |
| 3886 | case NFA_MOPEN3: |
| 3887 | case NFA_MOPEN4: |
| 3888 | case NFA_MOPEN5: |
| 3889 | case NFA_MOPEN6: |
| 3890 | case NFA_MOPEN7: |
| 3891 | case NFA_MOPEN8: |
| 3892 | case NFA_MOPEN9: |
| 3893 | #ifdef FEAT_SYN_HL |
| 3894 | case NFA_ZOPEN: |
| 3895 | case NFA_ZOPEN1: |
| 3896 | case NFA_ZOPEN2: |
| 3897 | case NFA_ZOPEN3: |
| 3898 | case NFA_ZOPEN4: |
| 3899 | case NFA_ZOPEN5: |
| 3900 | case NFA_ZOPEN6: |
| 3901 | case NFA_ZOPEN7: |
| 3902 | case NFA_ZOPEN8: |
| 3903 | case NFA_ZOPEN9: |
| 3904 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3905 | case NFA_ZSTART: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3906 | /* These nodes do not need to be added, but we need to bail out |
| 3907 | * when it was tried to be added to this list before. */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3908 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3909 | goto skip_add; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3910 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3911 | break; |
| 3912 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3913 | case NFA_BOL: |
| 3914 | case NFA_BOF: |
| 3915 | /* "^" won't match past end-of-line, don't bother trying. |
Bram Moolenaar | b62bcd1 | 2013-06-13 20:19:40 +0200 | [diff] [blame] | 3916 | * Except when at the end of the line, or when we are going to the |
| 3917 | * next line for a look-behind match. */ |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3918 | if (reginput > regline |
Bram Moolenaar | b62bcd1 | 2013-06-13 20:19:40 +0200 | [diff] [blame] | 3919 | && *reginput != NUL |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3920 | && (nfa_endp == NULL |
| 3921 | || !REG_MULTI |
| 3922 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 3923 | goto skip_add; |
| 3924 | /* FALLTHROUGH */ |
| 3925 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3926 | default: |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3927 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3928 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3929 | /* This state is already in the list, don't add it again, |
| 3930 | * unless it is an MOPEN that is used for a backreference. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3931 | if (!nfa_has_backref) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3932 | { |
| 3933 | skip_add: |
| 3934 | #ifdef ENABLE_LOG |
| 3935 | nfa_set_code(state->c); |
| 3936 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 3937 | abs(state->id), l->id, state->c, code); |
| 3938 | #endif |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3939 | return; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3940 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3941 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 3942 | /* Do not add the state again when it exists with the same |
| 3943 | * positions. */ |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3944 | if (has_state_with_pos(l, state, subs)) |
| 3945 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3946 | } |
| 3947 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 3948 | /* When there are backreferences the number of states may be (a |
| 3949 | * lot) bigger than anticipated. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3950 | if (nfa_has_backref && l->n == l->len) |
| 3951 | { |
| 3952 | int newlen = l->len * 3 / 2 + 50; |
| 3953 | |
| 3954 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 3955 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3956 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3957 | |
| 3958 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3959 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3960 | thread = &l->t[l->n++]; |
| 3961 | thread->state = state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3962 | if (pim == NULL) |
| 3963 | thread->pim.result = NFA_PIM_UNUSED; |
| 3964 | else |
| 3965 | copy_pim(&thread->pim, pim); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3966 | copy_sub(&thread->subs.norm, &subs->norm); |
| 3967 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3968 | if (nfa_has_zsubexpr) |
| 3969 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3970 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3971 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3972 | report_state("Adding", &thread->subs.norm, state, l->id, pim); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3973 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3974 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3978 | if (!did_print) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3979 | report_state("Processing", &subs->norm, state, l->id, pim); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3980 | #endif |
| 3981 | switch (state->c) |
| 3982 | { |
| 3983 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3984 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3985 | break; |
| 3986 | |
| 3987 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3988 | /* order matters here */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3989 | addstate(l, state->out, subs, pim, off); |
| 3990 | addstate(l, state->out1, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3991 | break; |
| 3992 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3993 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3994 | case NFA_NOPEN: |
| 3995 | case NFA_NCLOSE: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3996 | addstate(l, state->out, subs, pim, off); |
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_MOPEN: |
| 4000 | case NFA_MOPEN1: |
| 4001 | case NFA_MOPEN2: |
| 4002 | case NFA_MOPEN3: |
| 4003 | case NFA_MOPEN4: |
| 4004 | case NFA_MOPEN5: |
| 4005 | case NFA_MOPEN6: |
| 4006 | case NFA_MOPEN7: |
| 4007 | case NFA_MOPEN8: |
| 4008 | case NFA_MOPEN9: |
| 4009 | #ifdef FEAT_SYN_HL |
| 4010 | case NFA_ZOPEN: |
| 4011 | case NFA_ZOPEN1: |
| 4012 | case NFA_ZOPEN2: |
| 4013 | case NFA_ZOPEN3: |
| 4014 | case NFA_ZOPEN4: |
| 4015 | case NFA_ZOPEN5: |
| 4016 | case NFA_ZOPEN6: |
| 4017 | case NFA_ZOPEN7: |
| 4018 | case NFA_ZOPEN8: |
| 4019 | case NFA_ZOPEN9: |
| 4020 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4021 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4022 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4023 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4024 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4025 | sub = &subs->norm; |
| 4026 | } |
| 4027 | #ifdef FEAT_SYN_HL |
| 4028 | else if (state->c >= NFA_ZOPEN) |
| 4029 | { |
| 4030 | subidx = state->c - NFA_ZOPEN; |
| 4031 | sub = &subs->synt; |
| 4032 | } |
| 4033 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4034 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4035 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4036 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4037 | sub = &subs->norm; |
| 4038 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4039 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4040 | /* Set the position (with "off" added) in the subexpression. Save |
| 4041 | * and restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | 4b6ebe6 | 2013-05-30 17:49:24 +0200 | [diff] [blame] | 4042 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4043 | if (REG_MULTI) |
| 4044 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4045 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4046 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4047 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4048 | save_in_use = -1; |
| 4049 | } |
| 4050 | else |
| 4051 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4052 | save_in_use = sub->in_use; |
| 4053 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4054 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4055 | sub->list.multi[i].start.lnum = -1; |
| 4056 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4057 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4058 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4059 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4060 | if (off == -1) |
| 4061 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4062 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 4063 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4064 | } |
| 4065 | else |
| 4066 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4067 | sub->list.multi[subidx].start.lnum = reglnum; |
| 4068 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4069 | (colnr_T)(reginput - regline + off); |
| 4070 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4071 | } |
| 4072 | else |
| 4073 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4074 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4075 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4076 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4077 | save_in_use = -1; |
| 4078 | } |
| 4079 | else |
| 4080 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4081 | save_in_use = sub->in_use; |
| 4082 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4083 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4084 | sub->list.line[i].start = NULL; |
| 4085 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4086 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4087 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4088 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4089 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4090 | } |
| 4091 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4092 | addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4093 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4094 | if (save_in_use == -1) |
| 4095 | { |
| 4096 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4097 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4098 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4099 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4100 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4101 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4102 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4103 | break; |
| 4104 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4105 | case NFA_MCLOSE: |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4106 | if (nfa_has_zend) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4107 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 4108 | /* Do not overwrite the position set by \ze. If no \ze |
| 4109 | * encountered end will be set in nfa_regtry(). */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4110 | addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4111 | break; |
| 4112 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4113 | case NFA_MCLOSE1: |
| 4114 | case NFA_MCLOSE2: |
| 4115 | case NFA_MCLOSE3: |
| 4116 | case NFA_MCLOSE4: |
| 4117 | case NFA_MCLOSE5: |
| 4118 | case NFA_MCLOSE6: |
| 4119 | case NFA_MCLOSE7: |
| 4120 | case NFA_MCLOSE8: |
| 4121 | case NFA_MCLOSE9: |
| 4122 | #ifdef FEAT_SYN_HL |
| 4123 | case NFA_ZCLOSE: |
| 4124 | case NFA_ZCLOSE1: |
| 4125 | case NFA_ZCLOSE2: |
| 4126 | case NFA_ZCLOSE3: |
| 4127 | case NFA_ZCLOSE4: |
| 4128 | case NFA_ZCLOSE5: |
| 4129 | case NFA_ZCLOSE6: |
| 4130 | case NFA_ZCLOSE7: |
| 4131 | case NFA_ZCLOSE8: |
| 4132 | case NFA_ZCLOSE9: |
| 4133 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4134 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4135 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4136 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4137 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4138 | sub = &subs->norm; |
| 4139 | } |
| 4140 | #ifdef FEAT_SYN_HL |
| 4141 | else if (state->c >= NFA_ZCLOSE) |
| 4142 | { |
| 4143 | subidx = state->c - NFA_ZCLOSE; |
| 4144 | sub = &subs->synt; |
| 4145 | } |
| 4146 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4147 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4148 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4149 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4150 | sub = &subs->norm; |
| 4151 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4152 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4153 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 4154 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4155 | save_in_use = sub->in_use; |
| 4156 | if (sub->in_use <= subidx) |
| 4157 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4158 | if (REG_MULTI) |
| 4159 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4160 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4161 | if (off == -1) |
| 4162 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4163 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 4164 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4165 | } |
| 4166 | else |
| 4167 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4168 | sub->list.multi[subidx].end.lnum = reglnum; |
| 4169 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4170 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4171 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4172 | } |
| 4173 | else |
| 4174 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4175 | save_ptr = sub->list.line[subidx].end; |
| 4176 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4177 | } |
| 4178 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4179 | addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4180 | |
| 4181 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4182 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4183 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4184 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4185 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4186 | break; |
| 4187 | } |
| 4188 | } |
| 4189 | |
| 4190 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4191 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 4192 | * Used for zero-width matches, next state to use is the added one. |
| 4193 | * This makes sure the order of states to be tried does not change, which |
| 4194 | * matters for alternatives. |
| 4195 | */ |
| 4196 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4197 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4198 | nfa_list_T *l; /* runtime state list */ |
| 4199 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4200 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4201 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4202 | int *ip; |
| 4203 | { |
| 4204 | int tlen = l->n; |
| 4205 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4206 | int listidx = *ip; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4207 | |
| 4208 | /* first add the state(s) at the end, so that we know how many there are */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4209 | addstate(l, state, subs, pim, 0); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4210 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4211 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4212 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4213 | return; |
| 4214 | |
| 4215 | /* re-order to put the new state at the current position */ |
| 4216 | count = l->n - tlen; |
Bram Moolenaar | a50d02d | 2013-06-16 15:43:50 +0200 | [diff] [blame] | 4217 | if (count == 0) |
| 4218 | return; /* no state got added */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4219 | if (count == 1) |
| 4220 | { |
| 4221 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4222 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4223 | } |
| 4224 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4225 | { |
| 4226 | /* make space for new states, then move them from the |
| 4227 | * end to the current position */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4228 | mch_memmove(&(l->t[listidx + count]), |
| 4229 | &(l->t[listidx + 1]), |
| 4230 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 4231 | mch_memmove(&(l->t[listidx]), |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4232 | &(l->t[l->n - 1]), |
| 4233 | sizeof(nfa_thread_T) * count); |
| 4234 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4235 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4236 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4240 | * Check character class "class" against current character c. |
| 4241 | */ |
| 4242 | static int |
| 4243 | check_char_class(class, c) |
| 4244 | int class; |
| 4245 | int c; |
| 4246 | { |
| 4247 | switch (class) |
| 4248 | { |
| 4249 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4250 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4251 | return OK; |
| 4252 | break; |
| 4253 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4254 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4255 | return OK; |
| 4256 | break; |
| 4257 | case NFA_CLASS_BLANK: |
| 4258 | if (c == ' ' || c == '\t') |
| 4259 | return OK; |
| 4260 | break; |
| 4261 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4262 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4263 | return OK; |
| 4264 | break; |
| 4265 | case NFA_CLASS_DIGIT: |
| 4266 | if (VIM_ISDIGIT(c)) |
| 4267 | return OK; |
| 4268 | break; |
| 4269 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4270 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4271 | return OK; |
| 4272 | break; |
| 4273 | case NFA_CLASS_LOWER: |
| 4274 | if (MB_ISLOWER(c)) |
| 4275 | return OK; |
| 4276 | break; |
| 4277 | case NFA_CLASS_PRINT: |
| 4278 | if (vim_isprintc(c)) |
| 4279 | return OK; |
| 4280 | break; |
| 4281 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4282 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4283 | return OK; |
| 4284 | break; |
| 4285 | case NFA_CLASS_SPACE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4286 | if ((c >= 9 && c <= 13) || (c == ' ')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4287 | return OK; |
| 4288 | break; |
| 4289 | case NFA_CLASS_UPPER: |
| 4290 | if (MB_ISUPPER(c)) |
| 4291 | return OK; |
| 4292 | break; |
| 4293 | case NFA_CLASS_XDIGIT: |
| 4294 | if (vim_isxdigit(c)) |
| 4295 | return OK; |
| 4296 | break; |
| 4297 | case NFA_CLASS_TAB: |
| 4298 | if (c == '\t') |
| 4299 | return OK; |
| 4300 | break; |
| 4301 | case NFA_CLASS_RETURN: |
| 4302 | if (c == '\r') |
| 4303 | return OK; |
| 4304 | break; |
| 4305 | case NFA_CLASS_BACKSPACE: |
| 4306 | if (c == '\b') |
| 4307 | return OK; |
| 4308 | break; |
| 4309 | case NFA_CLASS_ESCAPE: |
| 4310 | if (c == '\033') |
| 4311 | return OK; |
| 4312 | break; |
| 4313 | |
| 4314 | default: |
| 4315 | /* should not be here :P */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 4316 | EMSGN("E877: (NFA regexp) Invalid character class: %ld", class); |
| 4317 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4318 | } |
| 4319 | return FAIL; |
| 4320 | } |
| 4321 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4322 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
| 4323 | |
| 4324 | /* |
| 4325 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4326 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4327 | */ |
| 4328 | static int |
| 4329 | match_backref(sub, subidx, bytelen) |
| 4330 | regsub_T *sub; /* pointers to subexpressions */ |
| 4331 | int subidx; |
| 4332 | int *bytelen; /* out: length of match in bytes */ |
| 4333 | { |
| 4334 | int len; |
| 4335 | |
| 4336 | if (sub->in_use <= subidx) |
| 4337 | { |
| 4338 | retempty: |
| 4339 | /* backref was not set, match an empty string */ |
| 4340 | *bytelen = 0; |
| 4341 | return TRUE; |
| 4342 | } |
| 4343 | |
| 4344 | if (REG_MULTI) |
| 4345 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4346 | if (sub->list.multi[subidx].start.lnum < 0 |
| 4347 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4348 | goto retempty; |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4349 | if (sub->list.multi[subidx].start.lnum == reglnum |
| 4350 | && sub->list.multi[subidx].end.lnum == reglnum) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4351 | { |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4352 | len = sub->list.multi[subidx].end.col |
| 4353 | - sub->list.multi[subidx].start.col; |
| 4354 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
| 4355 | reginput, &len) == 0) |
| 4356 | { |
| 4357 | *bytelen = len; |
| 4358 | return TRUE; |
| 4359 | } |
| 4360 | } |
| 4361 | else |
| 4362 | { |
| 4363 | if (match_with_backref( |
| 4364 | sub->list.multi[subidx].start.lnum, |
| 4365 | sub->list.multi[subidx].start.col, |
| 4366 | sub->list.multi[subidx].end.lnum, |
| 4367 | sub->list.multi[subidx].end.col, |
| 4368 | bytelen) == RA_MATCH) |
| 4369 | return TRUE; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4370 | } |
| 4371 | } |
| 4372 | else |
| 4373 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4374 | if (sub->list.line[subidx].start == NULL |
| 4375 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4376 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4377 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 4378 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4379 | { |
| 4380 | *bytelen = len; |
| 4381 | return TRUE; |
| 4382 | } |
| 4383 | } |
| 4384 | return FALSE; |
| 4385 | } |
| 4386 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4387 | #ifdef FEAT_SYN_HL |
| 4388 | |
| 4389 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 4390 | |
| 4391 | /* |
| 4392 | * Check for a match with \z subexpression "subidx". |
| 4393 | * Return TRUE if it matches. |
| 4394 | */ |
| 4395 | static int |
| 4396 | match_zref(subidx, bytelen) |
| 4397 | int subidx; |
| 4398 | int *bytelen; /* out: length of match in bytes */ |
| 4399 | { |
| 4400 | int len; |
| 4401 | |
| 4402 | cleanup_zsubexpr(); |
| 4403 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 4404 | { |
| 4405 | /* backref was not set, match an empty string */ |
| 4406 | *bytelen = 0; |
| 4407 | return TRUE; |
| 4408 | } |
| 4409 | |
| 4410 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 4411 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 4412 | { |
| 4413 | *bytelen = len; |
| 4414 | return TRUE; |
| 4415 | } |
| 4416 | return FALSE; |
| 4417 | } |
| 4418 | #endif |
| 4419 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4420 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4421 | * Save list IDs for all NFA states of "prog" into "list". |
| 4422 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4423 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4424 | */ |
| 4425 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4426 | nfa_save_listids(prog, list) |
| 4427 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4428 | int *list; |
| 4429 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4430 | int i; |
| 4431 | nfa_state_T *p; |
| 4432 | |
| 4433 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 4434 | p = &prog->state[0]; |
| 4435 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4436 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4437 | list[i] = p->lastlist[1]; |
| 4438 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4439 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4440 | } |
| 4441 | } |
| 4442 | |
| 4443 | /* |
| 4444 | * Restore list IDs from "list" to all NFA states. |
| 4445 | */ |
| 4446 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4447 | nfa_restore_listids(prog, list) |
| 4448 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4449 | int *list; |
| 4450 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4451 | int i; |
| 4452 | nfa_state_T *p; |
| 4453 | |
| 4454 | p = &prog->state[0]; |
| 4455 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4456 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4457 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4458 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4459 | } |
| 4460 | } |
| 4461 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4462 | static int |
| 4463 | nfa_re_num_cmp(val, op, pos) |
| 4464 | long_u val; |
| 4465 | int op; |
| 4466 | long_u pos; |
| 4467 | { |
| 4468 | if (op == 1) return pos > val; |
| 4469 | if (op == 2) return pos < val; |
| 4470 | return val == pos; |
| 4471 | } |
| 4472 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4473 | static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4474 | 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] | 4475 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4476 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4477 | * Recursively call nfa_regmatch() |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4478 | * "pim" is NULL or contains info about a Postponed Invisible Match (start |
| 4479 | * position). |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4480 | */ |
| 4481 | static int |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4482 | recursive_regmatch(state, pim, prog, submatch, m, listids) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4483 | nfa_state_T *state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4484 | nfa_pim_T *pim; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4485 | nfa_regprog_T *prog; |
| 4486 | regsubs_T *submatch; |
| 4487 | regsubs_T *m; |
| 4488 | int **listids; |
| 4489 | { |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4490 | int save_reginput_col = (int)(reginput - regline); |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4491 | int save_reglnum = reglnum; |
| 4492 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4493 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4494 | save_se_T *save_nfa_endp = nfa_endp; |
| 4495 | save_se_T endpos; |
| 4496 | save_se_T *endposp = NULL; |
| 4497 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4498 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4499 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4500 | if (pim != NULL) |
| 4501 | { |
| 4502 | /* start at the position where the postponed match was */ |
| 4503 | if (REG_MULTI) |
| 4504 | reginput = regline + pim->end.pos.col; |
| 4505 | else |
| 4506 | reginput = pim->end.ptr; |
| 4507 | } |
| 4508 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4509 | if (state->c == NFA_START_INVISIBLE_BEFORE |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4510 | || state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 4511 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG |
| 4512 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4513 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4514 | /* The recursive match must end at the current position. When "pim" is |
| 4515 | * not NULL it specifies the current position. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4516 | endposp = &endpos; |
| 4517 | if (REG_MULTI) |
| 4518 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4519 | if (pim == NULL) |
| 4520 | { |
| 4521 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 4522 | endpos.se_u.pos.lnum = reglnum; |
| 4523 | } |
| 4524 | else |
| 4525 | endpos.se_u.pos = pim->end.pos; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4526 | } |
| 4527 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4528 | { |
| 4529 | if (pim == NULL) |
| 4530 | endpos.se_u.ptr = reginput; |
| 4531 | else |
| 4532 | endpos.se_u.ptr = pim->end.ptr; |
| 4533 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4534 | |
| 4535 | /* Go back the specified number of bytes, or as far as the |
| 4536 | * start of the previous line, to try matching "\@<=" or |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4537 | * not matching "\@<!". This is very ineffecient, limit the number of |
| 4538 | * bytes if possible. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4539 | if (state->val <= 0) |
| 4540 | { |
| 4541 | if (REG_MULTI) |
| 4542 | { |
| 4543 | regline = reg_getline(--reglnum); |
| 4544 | if (regline == NULL) |
| 4545 | /* can't go before the first line */ |
| 4546 | regline = reg_getline(++reglnum); |
| 4547 | } |
| 4548 | reginput = regline; |
| 4549 | } |
| 4550 | else |
| 4551 | { |
| 4552 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 4553 | { |
| 4554 | /* Not enough bytes in this line, go to end of |
| 4555 | * previous line. */ |
| 4556 | regline = reg_getline(--reglnum); |
| 4557 | if (regline == NULL) |
| 4558 | { |
| 4559 | /* can't go before the first line */ |
| 4560 | regline = reg_getline(++reglnum); |
| 4561 | reginput = regline; |
| 4562 | } |
| 4563 | else |
| 4564 | reginput = regline + STRLEN(regline); |
| 4565 | } |
| 4566 | if ((int)(reginput - regline) >= state->val) |
| 4567 | { |
| 4568 | reginput -= state->val; |
| 4569 | #ifdef FEAT_MBYTE |
| 4570 | if (has_mbyte) |
| 4571 | reginput -= mb_head_off(regline, reginput); |
| 4572 | #endif |
| 4573 | } |
| 4574 | else |
| 4575 | reginput = regline; |
| 4576 | } |
| 4577 | } |
| 4578 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4579 | #ifdef ENABLE_LOG |
| 4580 | if (log_fd != stderr) |
| 4581 | fclose(log_fd); |
| 4582 | log_fd = NULL; |
| 4583 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4584 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 4585 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 4586 | if (nfa_ll_index == 1) |
| 4587 | { |
| 4588 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 4589 | * values and clear them. */ |
| 4590 | if (*listids == NULL) |
| 4591 | { |
| 4592 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 4593 | if (*listids == NULL) |
| 4594 | { |
| 4595 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 4596 | return 0; |
| 4597 | } |
| 4598 | } |
| 4599 | nfa_save_listids(prog, *listids); |
| 4600 | need_restore = TRUE; |
| 4601 | /* any value of nfa_listid will do */ |
| 4602 | } |
| 4603 | else |
| 4604 | { |
| 4605 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 4606 | * entry. Make sure nfa_listid is different from a previous recursive |
| 4607 | * call, because some states may still have this ID. */ |
| 4608 | ++nfa_ll_index; |
| 4609 | if (nfa_listid <= nfa_alt_listid) |
| 4610 | nfa_listid = nfa_alt_listid; |
| 4611 | } |
| 4612 | |
| 4613 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 4614 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4615 | nfa_endp = endposp; |
| 4616 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4617 | |
| 4618 | if (need_restore) |
| 4619 | nfa_restore_listids(prog, *listids); |
| 4620 | else |
| 4621 | { |
| 4622 | --nfa_ll_index; |
| 4623 | nfa_alt_listid = nfa_listid; |
| 4624 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4625 | |
| 4626 | /* restore position in input text */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4627 | reglnum = save_reglnum; |
Bram Moolenaar | 484d241 | 2013-06-13 19:47:07 +0200 | [diff] [blame] | 4628 | if (REG_MULTI) |
| 4629 | regline = reg_getline(reglnum); |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4630 | reginput = regline + save_reginput_col; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4631 | nfa_match = save_nfa_match; |
| 4632 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4633 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4634 | |
| 4635 | #ifdef ENABLE_LOG |
| 4636 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 4637 | if (log_fd != NULL) |
| 4638 | { |
| 4639 | fprintf(log_fd, "****************************\n"); |
| 4640 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 4641 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 4642 | fprintf(log_fd, "****************************\n"); |
| 4643 | } |
| 4644 | else |
| 4645 | { |
| 4646 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4647 | log_fd = stderr; |
| 4648 | } |
| 4649 | #endif |
| 4650 | |
| 4651 | return result; |
| 4652 | } |
| 4653 | |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 4654 | static int skip_to_start __ARGS((int c, colnr_T *colp)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4655 | 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] | 4656 | |
| 4657 | /* |
| 4658 | * Estimate the chance of a match with "state" failing. |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 4659 | * empty match: 0 |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4660 | * NFA_ANY: 1 |
| 4661 | * specific character: 99 |
| 4662 | */ |
| 4663 | static int |
| 4664 | failure_chance(state, depth) |
| 4665 | nfa_state_T *state; |
| 4666 | int depth; |
| 4667 | { |
| 4668 | int c = state->c; |
| 4669 | int l, r; |
| 4670 | |
| 4671 | /* detect looping */ |
| 4672 | if (depth > 4) |
| 4673 | return 1; |
| 4674 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4675 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4676 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4677 | case NFA_SPLIT: |
| 4678 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 4679 | /* avoid recursive stuff */ |
| 4680 | return 1; |
| 4681 | /* two alternatives, use the lowest failure chance */ |
| 4682 | l = failure_chance(state->out, depth + 1); |
| 4683 | r = failure_chance(state->out1, depth + 1); |
| 4684 | return l < r ? l : r; |
| 4685 | |
| 4686 | case NFA_ANY: |
| 4687 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4688 | return 1; |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 4689 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4690 | case NFA_MATCH: |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 4691 | case NFA_MCLOSE: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4692 | /* empty match works always */ |
| 4693 | return 0; |
| 4694 | |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 4695 | case NFA_START_INVISIBLE: |
| 4696 | case NFA_START_INVISIBLE_FIRST: |
| 4697 | case NFA_START_INVISIBLE_NEG: |
| 4698 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 4699 | case NFA_START_INVISIBLE_BEFORE: |
| 4700 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 4701 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 4702 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 4703 | case NFA_START_PATTERN: |
| 4704 | /* recursive regmatch is expensive, use low failure chance */ |
| 4705 | return 5; |
| 4706 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4707 | case NFA_BOL: |
| 4708 | case NFA_EOL: |
| 4709 | case NFA_BOF: |
| 4710 | case NFA_EOF: |
| 4711 | case NFA_NEWL: |
| 4712 | return 99; |
| 4713 | |
| 4714 | case NFA_BOW: |
| 4715 | case NFA_EOW: |
| 4716 | return 90; |
| 4717 | |
| 4718 | case NFA_MOPEN: |
| 4719 | case NFA_MOPEN1: |
| 4720 | case NFA_MOPEN2: |
| 4721 | case NFA_MOPEN3: |
| 4722 | case NFA_MOPEN4: |
| 4723 | case NFA_MOPEN5: |
| 4724 | case NFA_MOPEN6: |
| 4725 | case NFA_MOPEN7: |
| 4726 | case NFA_MOPEN8: |
| 4727 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4728 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4729 | case NFA_ZOPEN: |
| 4730 | case NFA_ZOPEN1: |
| 4731 | case NFA_ZOPEN2: |
| 4732 | case NFA_ZOPEN3: |
| 4733 | case NFA_ZOPEN4: |
| 4734 | case NFA_ZOPEN5: |
| 4735 | case NFA_ZOPEN6: |
| 4736 | case NFA_ZOPEN7: |
| 4737 | case NFA_ZOPEN8: |
| 4738 | case NFA_ZOPEN9: |
| 4739 | case NFA_ZCLOSE: |
| 4740 | case NFA_ZCLOSE1: |
| 4741 | case NFA_ZCLOSE2: |
| 4742 | case NFA_ZCLOSE3: |
| 4743 | case NFA_ZCLOSE4: |
| 4744 | case NFA_ZCLOSE5: |
| 4745 | case NFA_ZCLOSE6: |
| 4746 | case NFA_ZCLOSE7: |
| 4747 | case NFA_ZCLOSE8: |
| 4748 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4749 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4750 | case NFA_NOPEN: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4751 | case NFA_MCLOSE1: |
| 4752 | case NFA_MCLOSE2: |
| 4753 | case NFA_MCLOSE3: |
| 4754 | case NFA_MCLOSE4: |
| 4755 | case NFA_MCLOSE5: |
| 4756 | case NFA_MCLOSE6: |
| 4757 | case NFA_MCLOSE7: |
| 4758 | case NFA_MCLOSE8: |
| 4759 | case NFA_MCLOSE9: |
| 4760 | case NFA_NCLOSE: |
| 4761 | return failure_chance(state->out, depth + 1); |
| 4762 | |
| 4763 | case NFA_BACKREF1: |
| 4764 | case NFA_BACKREF2: |
| 4765 | case NFA_BACKREF3: |
| 4766 | case NFA_BACKREF4: |
| 4767 | case NFA_BACKREF5: |
| 4768 | case NFA_BACKREF6: |
| 4769 | case NFA_BACKREF7: |
| 4770 | case NFA_BACKREF8: |
| 4771 | case NFA_BACKREF9: |
| 4772 | #ifdef FEAT_SYN_HL |
| 4773 | case NFA_ZREF1: |
| 4774 | case NFA_ZREF2: |
| 4775 | case NFA_ZREF3: |
| 4776 | case NFA_ZREF4: |
| 4777 | case NFA_ZREF5: |
| 4778 | case NFA_ZREF6: |
| 4779 | case NFA_ZREF7: |
| 4780 | case NFA_ZREF8: |
| 4781 | case NFA_ZREF9: |
| 4782 | #endif |
| 4783 | /* backreferences don't match in many places */ |
| 4784 | return 94; |
| 4785 | |
| 4786 | case NFA_LNUM_GT: |
| 4787 | case NFA_LNUM_LT: |
| 4788 | case NFA_COL_GT: |
| 4789 | case NFA_COL_LT: |
| 4790 | case NFA_VCOL_GT: |
| 4791 | case NFA_VCOL_LT: |
| 4792 | case NFA_MARK_GT: |
| 4793 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4794 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4795 | /* before/after positions don't match very often */ |
| 4796 | return 85; |
| 4797 | |
| 4798 | case NFA_LNUM: |
| 4799 | return 90; |
| 4800 | |
| 4801 | case NFA_CURSOR: |
| 4802 | case NFA_COL: |
| 4803 | case NFA_VCOL: |
| 4804 | case NFA_MARK: |
| 4805 | /* specific positions rarely match */ |
| 4806 | return 98; |
| 4807 | |
| 4808 | case NFA_COMPOSING: |
| 4809 | return 95; |
| 4810 | |
| 4811 | default: |
| 4812 | if (c > 0) |
| 4813 | /* character match fails often */ |
| 4814 | return 95; |
| 4815 | } |
| 4816 | |
| 4817 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4818 | return 50; |
| 4819 | } |
| 4820 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4821 | /* |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 4822 | * Skip until the char "c" we know a match must start with. |
| 4823 | */ |
| 4824 | static int |
| 4825 | skip_to_start(c, colp) |
| 4826 | int c; |
| 4827 | colnr_T *colp; |
| 4828 | { |
| 4829 | char_u *s; |
| 4830 | |
| 4831 | /* Used often, do some work to avoid call overhead. */ |
| 4832 | if (!ireg_ic |
| 4833 | #ifdef FEAT_MBYTE |
| 4834 | && !has_mbyte |
| 4835 | #endif |
| 4836 | ) |
| 4837 | s = vim_strbyte(regline + *colp, c); |
| 4838 | else |
| 4839 | s = cstrchr(regline + *colp, c); |
| 4840 | if (s == NULL) |
| 4841 | return FAIL; |
| 4842 | *colp = (int)(s - regline); |
| 4843 | return OK; |
| 4844 | } |
| 4845 | |
| 4846 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4847 | * Check for a match with match_text. |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4848 | * Called after skip_to_start() has found regstart. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4849 | * Returns zero for no match, 1 for a match. |
| 4850 | */ |
| 4851 | static long |
| 4852 | find_match_text(startcol, regstart, match_text) |
| 4853 | colnr_T startcol; |
| 4854 | int regstart; |
| 4855 | char_u *match_text; |
| 4856 | { |
| 4857 | colnr_T col = startcol; |
| 4858 | int c1, c2; |
| 4859 | int len1, len2; |
| 4860 | int match; |
| 4861 | |
| 4862 | for (;;) |
| 4863 | { |
| 4864 | match = TRUE; |
| 4865 | len2 = MB_CHAR2LEN(regstart); /* skip regstart */ |
| 4866 | for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
| 4867 | { |
| 4868 | c1 = PTR2CHAR(match_text + len1); |
| 4869 | c2 = PTR2CHAR(regline + col + len2); |
| 4870 | if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
| 4871 | { |
| 4872 | match = FALSE; |
| 4873 | break; |
| 4874 | } |
| 4875 | len2 += MB_CHAR2LEN(c2); |
| 4876 | } |
| 4877 | if (match |
| 4878 | #ifdef FEAT_MBYTE |
| 4879 | /* check that no composing char follows */ |
| 4880 | && !(enc_utf8 |
| 4881 | && utf_iscomposing(PTR2CHAR(regline + col + len2))) |
| 4882 | #endif |
| 4883 | ) |
| 4884 | { |
| 4885 | cleanup_subexpr(); |
| 4886 | if (REG_MULTI) |
| 4887 | { |
| 4888 | reg_startpos[0].lnum = reglnum; |
| 4889 | reg_startpos[0].col = col; |
| 4890 | reg_endpos[0].lnum = reglnum; |
| 4891 | reg_endpos[0].col = col + len2; |
| 4892 | } |
| 4893 | else |
| 4894 | { |
| 4895 | reg_startp[0] = regline + col; |
| 4896 | reg_endp[0] = regline + col + len2; |
| 4897 | } |
| 4898 | return 1L; |
| 4899 | } |
| 4900 | |
| 4901 | /* Try finding regstart after the current match. */ |
| 4902 | col += MB_CHAR2LEN(regstart); /* skip regstart */ |
| 4903 | if (skip_to_start(regstart, &col) == FAIL) |
| 4904 | break; |
| 4905 | } |
| 4906 | return 0L; |
| 4907 | } |
| 4908 | |
| 4909 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4910 | * Main matching routine. |
| 4911 | * |
| 4912 | * Run NFA to determine whether it matches reginput. |
| 4913 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4914 | * 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] | 4915 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4916 | * Return TRUE if there is a match, FALSE otherwise. |
| 4917 | * Note: Caller must ensure that: start != NULL. |
| 4918 | */ |
| 4919 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4920 | nfa_regmatch(prog, start, submatch, m) |
| 4921 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4922 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4923 | regsubs_T *submatch; |
| 4924 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4925 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4926 | int result; |
| 4927 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4928 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4929 | int go_to_nextline = FALSE; |
| 4930 | nfa_thread_T *t; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4931 | nfa_list_T list[2]; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4932 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4933 | nfa_list_T *thislist; |
| 4934 | nfa_list_T *nextlist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4935 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4936 | nfa_state_T *add_state; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 4937 | int add_here; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4938 | int add_count; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 4939 | int add_off = 0; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4940 | int toplevel = start->c == NFA_MOPEN; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4941 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4942 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4943 | |
| 4944 | if (debug == NULL) |
| 4945 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4946 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4947 | return FALSE; |
| 4948 | } |
| 4949 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4950 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4951 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4952 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4953 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4954 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4955 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4956 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4957 | list[1].len = nstate + 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4958 | if (list[0].t == NULL || list[1].t == NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4959 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4960 | |
| 4961 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4962 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4963 | if (log_fd != NULL) |
| 4964 | { |
| 4965 | fprintf(log_fd, "**********************************\n"); |
| 4966 | nfa_set_code(start->c); |
| 4967 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 4968 | abs(start->id), code); |
| 4969 | fprintf(log_fd, "**********************************\n"); |
| 4970 | } |
| 4971 | else |
| 4972 | { |
| 4973 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4974 | log_fd = stderr; |
| 4975 | } |
| 4976 | #endif |
| 4977 | |
| 4978 | thislist = &list[0]; |
| 4979 | thislist->n = 0; |
| 4980 | nextlist = &list[1]; |
| 4981 | nextlist->n = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4982 | #ifdef ENABLE_LOG |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4983 | fprintf(log_fd, "(---) STARTSTATE first\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4984 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4985 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4986 | |
| 4987 | /* Inline optimized code for addstate(thislist, start, m, 0) if we know |
| 4988 | * it's the first MOPEN. */ |
| 4989 | if (toplevel) |
| 4990 | { |
| 4991 | if (REG_MULTI) |
| 4992 | { |
| 4993 | m->norm.list.multi[0].start.lnum = reglnum; |
| 4994 | m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline); |
| 4995 | } |
| 4996 | else |
| 4997 | m->norm.list.line[0].start = reginput; |
| 4998 | m->norm.in_use = 1; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4999 | addstate(thislist, start->out, m, NULL, 0); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5000 | } |
| 5001 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5002 | addstate(thislist, start, m, NULL, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5003 | |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5004 | #define ADD_STATE_IF_MATCH(state) \ |
| 5005 | if (result) { \ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5006 | add_state = state->out; \ |
| 5007 | add_off = clen; \ |
| 5008 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5009 | |
| 5010 | /* |
| 5011 | * Run for each character. |
| 5012 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5013 | for (;;) |
| 5014 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5015 | int curc; |
| 5016 | int clen; |
| 5017 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5018 | #ifdef FEAT_MBYTE |
| 5019 | if (has_mbyte) |
| 5020 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5021 | curc = (*mb_ptr2char)(reginput); |
| 5022 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5023 | } |
| 5024 | else |
| 5025 | #endif |
| 5026 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5027 | curc = *reginput; |
| 5028 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5029 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5030 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5031 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5032 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5033 | go_to_nextline = FALSE; |
| 5034 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5035 | |
| 5036 | /* swap lists */ |
| 5037 | thislist = &list[flag]; |
| 5038 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5039 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5040 | ++nfa_listid; |
| 5041 | thislist->id = nfa_listid; |
| 5042 | nextlist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5043 | |
| 5044 | #ifdef ENABLE_LOG |
| 5045 | fprintf(log_fd, "------------------------------------------\n"); |
| 5046 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5047 | 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] | 5048 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5049 | { |
| 5050 | int i; |
| 5051 | |
| 5052 | for (i = 0; i < thislist->n; i++) |
| 5053 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5054 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5055 | fprintf(log_fd, "\n"); |
| 5056 | #endif |
| 5057 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5058 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5059 | fprintf(debug, "\n-------------------\n"); |
| 5060 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5061 | /* |
| 5062 | * If the state lists are empty we can stop. |
| 5063 | */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5064 | if (thislist->n == 0) |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5065 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5066 | |
| 5067 | /* compute nextlist */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5068 | for (listidx = 0; listidx < thislist->n; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5069 | { |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5070 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5071 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5072 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5073 | nfa_set_code(t->state->c); |
| 5074 | fprintf(debug, "%s, ", code); |
| 5075 | #endif |
| 5076 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5077 | { |
| 5078 | int col; |
| 5079 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5080 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5081 | col = -1; |
| 5082 | else if (REG_MULTI) |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5083 | col = t->subs.norm.list.multi[0].start.col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5084 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5085 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5086 | nfa_set_code(t->state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5087 | fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n", |
| 5088 | abs(t->state->id), (int)t->state->c, code, col, |
| 5089 | pim_info(&t->pim)); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5090 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5091 | #endif |
| 5092 | |
| 5093 | /* |
| 5094 | * Handle the possible codes of the current state. |
| 5095 | * The most important is NFA_MATCH. |
| 5096 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5097 | add_state = NULL; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5098 | add_here = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5099 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5100 | switch (t->state->c) |
| 5101 | { |
| 5102 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5103 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5104 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5105 | copy_sub(&submatch->norm, &t->subs.norm); |
| 5106 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5107 | if (nfa_has_zsubexpr) |
| 5108 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5109 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5110 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5111 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5112 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5113 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5114 | * states at this position. When the list of states is going |
| 5115 | * to be empty quit without advancing, so that "reginput" is |
| 5116 | * correct. */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5117 | if (nextlist->n == 0) |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5118 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5119 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5120 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5121 | |
| 5122 | case NFA_END_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5123 | case NFA_END_INVISIBLE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5124 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5125 | /* |
| 5126 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5127 | * NFA_START_INVISIBLE_BEFORE node. |
| 5128 | * They surround a zero-width group, used with "\@=", "\&", |
| 5129 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5130 | * If we got here, it means that the current "invisible" group |
| 5131 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5132 | * nfa_regmatch(). For a look-behind match only when it ends |
| 5133 | * in the position in "nfa_endp". |
| 5134 | * Submatches are stored in *m, and used in the parent call. |
| 5135 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5136 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5137 | if (nfa_endp != NULL) |
| 5138 | { |
| 5139 | if (REG_MULTI) |
| 5140 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 5141 | (int)reglnum, |
| 5142 | (int)nfa_endp->se_u.pos.lnum, |
| 5143 | (int)(reginput - regline), |
| 5144 | nfa_endp->se_u.pos.col); |
| 5145 | else |
| 5146 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 5147 | (int)(reginput - regline), |
| 5148 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5149 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5150 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5151 | /* If "nfa_endp" is set it's only a match if it ends at |
| 5152 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5153 | if (nfa_endp != NULL && (REG_MULTI |
| 5154 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 5155 | || (int)(reginput - regline) |
| 5156 | != nfa_endp->se_u.pos.col) |
| 5157 | : reginput != nfa_endp->se_u.ptr)) |
| 5158 | break; |
| 5159 | |
| 5160 | /* do not set submatches for \@! */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5161 | if (t->state->c != NFA_END_INVISIBLE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5162 | { |
| 5163 | copy_sub(&m->norm, &t->subs.norm); |
| 5164 | #ifdef FEAT_SYN_HL |
| 5165 | if (nfa_has_zsubexpr) |
| 5166 | copy_sub(&m->synt, &t->subs.synt); |
| 5167 | #endif |
| 5168 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5169 | #ifdef ENABLE_LOG |
| 5170 | fprintf(log_fd, "Match found:\n"); |
| 5171 | log_subsexpr(m); |
| 5172 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5173 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5174 | break; |
| 5175 | |
| 5176 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5177 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5178 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5179 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5180 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5181 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5182 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5183 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5184 | { |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5185 | #ifdef ENABLE_LOG |
| 5186 | fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", |
| 5187 | failure_chance(t->state->out, 0), |
| 5188 | failure_chance(t->state->out1->out, 0)); |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5189 | #endif |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5190 | /* Do it directly if there already is a PIM or when |
| 5191 | * nfa_postprocess() detected it will work better. */ |
| 5192 | if (t->pim.result != NFA_PIM_UNUSED |
| 5193 | || t->state->c == NFA_START_INVISIBLE_FIRST |
| 5194 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5195 | || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 5196 | || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5197 | { |
| 5198 | /* |
| 5199 | * First try matching the invisible match, then what |
| 5200 | * follows. |
| 5201 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5202 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5203 | submatch, m, &listids); |
| 5204 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5205 | /* for \@! and \@<! it is a match when the result is |
| 5206 | * FALSE */ |
| 5207 | if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5208 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5209 | || t->state->c |
| 5210 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5211 | || t->state->c |
| 5212 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5213 | { |
| 5214 | /* Copy submatch info from the recursive call */ |
| 5215 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5216 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5217 | if (nfa_has_zsubexpr) |
| 5218 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5219 | #endif |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5220 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5221 | /* t->state->out1 is the corresponding |
| 5222 | * END_INVISIBLE node; Add its out to the current |
| 5223 | * list (zero-width match). */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5224 | add_here = TRUE; |
| 5225 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5226 | } |
| 5227 | } |
| 5228 | else |
| 5229 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5230 | nfa_pim_T pim; |
| 5231 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5232 | /* |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5233 | * First try matching what follows. Only if a match |
| 5234 | * is found verify the invisible match matches. Add a |
| 5235 | * nfa_pim_T to the following states, it contains info |
| 5236 | * about the invisible match. |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5237 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5238 | pim.state = t->state; |
| 5239 | pim.result = NFA_PIM_TODO; |
| 5240 | pim.subs.norm.in_use = 0; |
| 5241 | #ifdef FEAT_SYN_HL |
| 5242 | pim.subs.synt.in_use = 0; |
| 5243 | #endif |
| 5244 | if (REG_MULTI) |
| 5245 | { |
| 5246 | pim.end.pos.col = (int)(reginput - regline); |
| 5247 | pim.end.pos.lnum = reglnum; |
| 5248 | } |
| 5249 | else |
| 5250 | pim.end.ptr = reginput; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5251 | |
| 5252 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 5253 | * node; Add its out to the current list (zero-width |
| 5254 | * match). */ |
| 5255 | addstate_here(thislist, t->state->out1->out, &t->subs, |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5256 | &pim, &listidx); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5257 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5258 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5259 | break; |
| 5260 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5261 | case NFA_START_PATTERN: |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5262 | { |
| 5263 | nfa_state_T *skip = NULL; |
| 5264 | #ifdef ENABLE_LOG |
| 5265 | int skip_lid = 0; |
| 5266 | #endif |
| 5267 | |
| 5268 | /* There is no point in trying to match the pattern if the |
| 5269 | * output state is not going to be added to the list. */ |
| 5270 | if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
| 5271 | { |
| 5272 | skip = t->state->out1->out; |
| 5273 | #ifdef ENABLE_LOG |
| 5274 | skip_lid = nextlist->id; |
| 5275 | #endif |
| 5276 | } |
| 5277 | else if (state_in_list(nextlist, |
| 5278 | t->state->out1->out->out, &t->subs)) |
| 5279 | { |
| 5280 | skip = t->state->out1->out->out; |
| 5281 | #ifdef ENABLE_LOG |
| 5282 | skip_lid = nextlist->id; |
| 5283 | #endif |
| 5284 | } |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5285 | else if (state_in_list(thislist, |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5286 | t->state->out1->out->out, &t->subs)) |
| 5287 | { |
| 5288 | skip = t->state->out1->out->out; |
| 5289 | #ifdef ENABLE_LOG |
| 5290 | skip_lid = thislist->id; |
| 5291 | #endif |
| 5292 | } |
| 5293 | if (skip != NULL) |
| 5294 | { |
| 5295 | #ifdef ENABLE_LOG |
| 5296 | nfa_set_code(skip->c); |
| 5297 | fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
| 5298 | abs(skip->id), skip_lid, skip->c, code); |
| 5299 | #endif |
| 5300 | break; |
| 5301 | } |
| 5302 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5303 | /* First try matching the pattern. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5304 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5305 | submatch, m, &listids); |
| 5306 | if (result) |
| 5307 | { |
| 5308 | int bytelen; |
| 5309 | |
| 5310 | #ifdef ENABLE_LOG |
| 5311 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 5312 | log_subsexpr(m); |
| 5313 | #endif |
| 5314 | /* Copy submatch info from the recursive call */ |
| 5315 | copy_sub_off(&t->subs.norm, &m->norm); |
| 5316 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5317 | if (nfa_has_zsubexpr) |
| 5318 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5319 | #endif |
| 5320 | /* Now we need to skip over the matched text and then |
| 5321 | * continue with what follows. */ |
| 5322 | if (REG_MULTI) |
| 5323 | /* TODO: multi-line match */ |
| 5324 | bytelen = m->norm.list.multi[0].end.col |
| 5325 | - (int)(reginput - regline); |
| 5326 | else |
| 5327 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 5328 | |
| 5329 | #ifdef ENABLE_LOG |
| 5330 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 5331 | #endif |
| 5332 | if (bytelen == 0) |
| 5333 | { |
| 5334 | /* empty match, output of corresponding |
| 5335 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 5336 | * position */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5337 | add_here = TRUE; |
| 5338 | add_state = t->state->out1->out->out; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5339 | } |
| 5340 | else if (bytelen <= clen) |
| 5341 | { |
| 5342 | /* match current character, output of corresponding |
| 5343 | * NFA_END_PATTERN to be used at next position. */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5344 | add_state = t->state->out1->out->out; |
| 5345 | add_off = clen; |
| 5346 | } |
| 5347 | else |
| 5348 | { |
| 5349 | /* skip over the matched characters, set character |
| 5350 | * count in NFA_SKIP */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5351 | add_state = t->state->out1->out; |
| 5352 | add_off = bytelen; |
| 5353 | add_count = bytelen - clen; |
| 5354 | } |
| 5355 | } |
| 5356 | break; |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5357 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5358 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5359 | case NFA_BOL: |
| 5360 | if (reginput == regline) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5361 | { |
| 5362 | add_here = TRUE; |
| 5363 | add_state = t->state->out; |
| 5364 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5365 | break; |
| 5366 | |
| 5367 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5368 | if (curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5369 | { |
| 5370 | add_here = TRUE; |
| 5371 | add_state = t->state->out; |
| 5372 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5373 | break; |
| 5374 | |
| 5375 | case NFA_BOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5376 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5377 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5378 | if (curc == NUL) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5379 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5380 | #ifdef FEAT_MBYTE |
| 5381 | else if (has_mbyte) |
| 5382 | { |
| 5383 | int this_class; |
| 5384 | |
| 5385 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5386 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5387 | if (this_class <= 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5388 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5389 | else if (reg_prev_class() == this_class) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5390 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5391 | } |
| 5392 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5393 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5394 | || (reginput > regline |
| 5395 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5396 | result = FALSE; |
| 5397 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5398 | { |
| 5399 | add_here = TRUE; |
| 5400 | add_state = t->state->out; |
| 5401 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5402 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5403 | |
| 5404 | case NFA_EOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5405 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5406 | if (reginput == regline) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5407 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5408 | #ifdef FEAT_MBYTE |
| 5409 | else if (has_mbyte) |
| 5410 | { |
| 5411 | int this_class, prev_class; |
| 5412 | |
| 5413 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5414 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5415 | prev_class = reg_prev_class(); |
| 5416 | if (this_class == prev_class |
| 5417 | || prev_class == 0 || prev_class == 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5418 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5419 | } |
| 5420 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5421 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5422 | || (reginput[0] != NUL |
| 5423 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5424 | result = FALSE; |
| 5425 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5426 | { |
| 5427 | add_here = TRUE; |
| 5428 | add_state = t->state->out; |
| 5429 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5430 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5431 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5432 | case NFA_BOF: |
| 5433 | if (reglnum == 0 && reginput == regline |
| 5434 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5435 | { |
| 5436 | add_here = TRUE; |
| 5437 | add_state = t->state->out; |
| 5438 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5439 | break; |
| 5440 | |
| 5441 | case NFA_EOF: |
| 5442 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5443 | { |
| 5444 | add_here = TRUE; |
| 5445 | add_state = t->state->out; |
| 5446 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5447 | break; |
| 5448 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5449 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5450 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5451 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5452 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5453 | int len = 0; |
| 5454 | nfa_state_T *end; |
| 5455 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5456 | int cchars[MAX_MCO]; |
| 5457 | int ccount = 0; |
| 5458 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5459 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5460 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5461 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5462 | if (utf_iscomposing(sta->c)) |
| 5463 | { |
| 5464 | /* Only match composing character(s), ignore base |
| 5465 | * character. Used for ".{composing}" and "{composing}" |
| 5466 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5467 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5468 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 5469 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5470 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5471 | /* If \Z was present, then ignore composing characters. |
| 5472 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5473 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5474 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5475 | else |
| 5476 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5477 | while (sta->c != NFA_END_COMPOSING) |
| 5478 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5479 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5480 | |
| 5481 | /* Check base character matches first, unless ignored. */ |
| 5482 | else if (len > 0 || mc == sta->c) |
| 5483 | { |
| 5484 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5485 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5486 | len += mb_char2len(mc); |
| 5487 | sta = sta->out; |
| 5488 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5489 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5490 | /* We don't care about the order of composing characters. |
| 5491 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5492 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5493 | { |
| 5494 | mc = mb_ptr2char(reginput + len); |
| 5495 | cchars[ccount++] = mc; |
| 5496 | len += mb_char2len(mc); |
| 5497 | if (ccount == MAX_MCO) |
| 5498 | break; |
| 5499 | } |
| 5500 | |
| 5501 | /* Check that each composing char in the pattern matches a |
| 5502 | * composing char in the text. We do not check if all |
| 5503 | * composing chars are matched. */ |
| 5504 | result = OK; |
| 5505 | while (sta->c != NFA_END_COMPOSING) |
| 5506 | { |
| 5507 | for (j = 0; j < ccount; ++j) |
| 5508 | if (cchars[j] == sta->c) |
| 5509 | break; |
| 5510 | if (j == ccount) |
| 5511 | { |
| 5512 | result = FAIL; |
| 5513 | break; |
| 5514 | } |
| 5515 | sta = sta->out; |
| 5516 | } |
| 5517 | } |
| 5518 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 5519 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5520 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5521 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5522 | ADD_STATE_IF_MATCH(end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5523 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5524 | } |
| 5525 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5526 | |
| 5527 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5528 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 5529 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5530 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5531 | go_to_nextline = TRUE; |
| 5532 | /* Pass -1 for the offset, which means taking the position |
| 5533 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5534 | add_state = t->state->out; |
| 5535 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5536 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5537 | else if (curc == '\n' && reg_line_lbr) |
| 5538 | { |
| 5539 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5540 | add_state = t->state->out; |
| 5541 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5542 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5543 | break; |
| 5544 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5545 | case NFA_START_COLL: |
| 5546 | case NFA_START_NEG_COLL: |
| 5547 | { |
| 5548 | /* What follows is a list of characters, until NFA_END_COLL. |
| 5549 | * One of them must match or none of them must match. */ |
| 5550 | nfa_state_T *state; |
| 5551 | int result_if_matched; |
| 5552 | int c1, c2; |
| 5553 | |
| 5554 | /* Never match EOL. If it's part of the collection it is added |
| 5555 | * as a separate state with an OR. */ |
| 5556 | if (curc == NUL) |
| 5557 | break; |
| 5558 | |
| 5559 | state = t->state->out; |
| 5560 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 5561 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5562 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5563 | if (state->c == NFA_END_COLL) |
| 5564 | { |
| 5565 | result = !result_if_matched; |
| 5566 | break; |
| 5567 | } |
| 5568 | if (state->c == NFA_RANGE_MIN) |
| 5569 | { |
| 5570 | c1 = state->val; |
| 5571 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 5572 | c2 = state->val; |
| 5573 | #ifdef ENABLE_LOG |
| 5574 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 5575 | curc, c1, c2); |
| 5576 | #endif |
| 5577 | if (curc >= c1 && curc <= c2) |
| 5578 | { |
| 5579 | result = result_if_matched; |
| 5580 | break; |
| 5581 | } |
| 5582 | if (ireg_ic) |
| 5583 | { |
| 5584 | int curc_low = MB_TOLOWER(curc); |
| 5585 | int done = FALSE; |
| 5586 | |
| 5587 | for ( ; c1 <= c2; ++c1) |
| 5588 | if (MB_TOLOWER(c1) == curc_low) |
| 5589 | { |
| 5590 | result = result_if_matched; |
| 5591 | done = TRUE; |
| 5592 | break; |
| 5593 | } |
| 5594 | if (done) |
| 5595 | break; |
| 5596 | } |
| 5597 | } |
| 5598 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 5599 | : (curc == state->c |
| 5600 | || (ireg_ic && MB_TOLOWER(curc) |
| 5601 | == MB_TOLOWER(state->c)))) |
| 5602 | { |
| 5603 | result = result_if_matched; |
| 5604 | break; |
| 5605 | } |
| 5606 | state = state->out; |
| 5607 | } |
| 5608 | if (result) |
| 5609 | { |
| 5610 | /* next state is in out of the NFA_END_COLL, out1 of |
| 5611 | * START points to the END state */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5612 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5613 | add_off = clen; |
| 5614 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5615 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5616 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5617 | |
| 5618 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5619 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5620 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5621 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5622 | add_state = t->state->out; |
| 5623 | add_off = clen; |
| 5624 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5625 | break; |
| 5626 | |
| 5627 | /* |
| 5628 | * Character classes like \a for alpha, \d for digit etc. |
| 5629 | */ |
| 5630 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5631 | result = vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5632 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5633 | break; |
| 5634 | |
| 5635 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5636 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5637 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5638 | break; |
| 5639 | |
| 5640 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5641 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5642 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5643 | break; |
| 5644 | |
| 5645 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5646 | result = !VIM_ISDIGIT(curc) |
| 5647 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5648 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5649 | break; |
| 5650 | |
| 5651 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5652 | result = vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5653 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5654 | break; |
| 5655 | |
| 5656 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5657 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5658 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5659 | break; |
| 5660 | |
| 5661 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | 0805049 | 2013-05-21 12:43:56 +0200 | [diff] [blame] | 5662 | result = ptr2cells(reginput) == 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5663 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5664 | break; |
| 5665 | |
| 5666 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5667 | result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5668 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5669 | break; |
| 5670 | |
| 5671 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5672 | result = vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5673 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5674 | break; |
| 5675 | |
| 5676 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5677 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5678 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5679 | break; |
| 5680 | |
| 5681 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5682 | result = ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5683 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5684 | break; |
| 5685 | |
| 5686 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5687 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5688 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5689 | break; |
| 5690 | |
| 5691 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5692 | result = ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5693 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5694 | break; |
| 5695 | |
| 5696 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5697 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5698 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5699 | break; |
| 5700 | |
| 5701 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5702 | result = ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5703 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5704 | break; |
| 5705 | |
| 5706 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5707 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5708 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5709 | break; |
| 5710 | |
| 5711 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5712 | result = ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5713 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5714 | break; |
| 5715 | |
| 5716 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5717 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5718 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5719 | break; |
| 5720 | |
| 5721 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5722 | result = ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5723 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5724 | break; |
| 5725 | |
| 5726 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5727 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5728 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5729 | break; |
| 5730 | |
| 5731 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5732 | result = ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5733 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5734 | break; |
| 5735 | |
| 5736 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5737 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5738 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5739 | break; |
| 5740 | |
| 5741 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5742 | result = ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5743 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5744 | break; |
| 5745 | |
| 5746 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5747 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5748 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5749 | break; |
| 5750 | |
| 5751 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5752 | result = ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5753 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5754 | break; |
| 5755 | |
| 5756 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5757 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5758 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5759 | break; |
| 5760 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5761 | case NFA_BACKREF1: |
| 5762 | case NFA_BACKREF2: |
| 5763 | case NFA_BACKREF3: |
| 5764 | case NFA_BACKREF4: |
| 5765 | case NFA_BACKREF5: |
| 5766 | case NFA_BACKREF6: |
| 5767 | case NFA_BACKREF7: |
| 5768 | case NFA_BACKREF8: |
| 5769 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5770 | #ifdef FEAT_SYN_HL |
| 5771 | case NFA_ZREF1: |
| 5772 | case NFA_ZREF2: |
| 5773 | case NFA_ZREF3: |
| 5774 | case NFA_ZREF4: |
| 5775 | case NFA_ZREF5: |
| 5776 | case NFA_ZREF6: |
| 5777 | case NFA_ZREF7: |
| 5778 | case NFA_ZREF8: |
| 5779 | case NFA_ZREF9: |
| 5780 | #endif |
| 5781 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5782 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5783 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5784 | int bytelen; |
| 5785 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5786 | if (t->state->c <= NFA_BACKREF9) |
| 5787 | { |
| 5788 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 5789 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 5790 | } |
| 5791 | #ifdef FEAT_SYN_HL |
| 5792 | else |
| 5793 | { |
| 5794 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 5795 | result = match_zref(subidx, &bytelen); |
| 5796 | } |
| 5797 | #endif |
| 5798 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5799 | if (result) |
| 5800 | { |
| 5801 | if (bytelen == 0) |
| 5802 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 5803 | /* empty match always works, output of NFA_SKIP to be |
| 5804 | * used next */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5805 | add_here = TRUE; |
| 5806 | add_state = t->state->out->out; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5807 | } |
| 5808 | else if (bytelen <= clen) |
| 5809 | { |
| 5810 | /* match current character, jump ahead to out of |
| 5811 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5812 | add_state = t->state->out->out; |
| 5813 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5814 | } |
| 5815 | else |
| 5816 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 5817 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5818 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5819 | add_state = t->state->out; |
| 5820 | add_off = bytelen; |
| 5821 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5822 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5823 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5824 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5825 | } |
| 5826 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 5827 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5828 | if (t->count - clen <= 0) |
| 5829 | { |
| 5830 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5831 | add_state = t->state->out; |
| 5832 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5833 | } |
| 5834 | else |
| 5835 | { |
| 5836 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5837 | add_state = t->state; |
| 5838 | add_off = 0; |
| 5839 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5840 | } |
| 5841 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5842 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5843 | case NFA_LNUM: |
| 5844 | case NFA_LNUM_GT: |
| 5845 | case NFA_LNUM_LT: |
| 5846 | result = (REG_MULTI && |
| 5847 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 5848 | (long_u)(reglnum + reg_firstlnum))); |
| 5849 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5850 | { |
| 5851 | add_here = TRUE; |
| 5852 | add_state = t->state->out; |
| 5853 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5854 | break; |
| 5855 | |
| 5856 | case NFA_COL: |
| 5857 | case NFA_COL_GT: |
| 5858 | case NFA_COL_LT: |
| 5859 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 5860 | (long_u)(reginput - regline) + 1); |
| 5861 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5862 | { |
| 5863 | add_here = TRUE; |
| 5864 | add_state = t->state->out; |
| 5865 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5866 | break; |
| 5867 | |
| 5868 | case NFA_VCOL: |
| 5869 | case NFA_VCOL_GT: |
| 5870 | case NFA_VCOL_LT: |
| 5871 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 5872 | (long_u)win_linetabsize( |
| 5873 | reg_win == NULL ? curwin : reg_win, |
| 5874 | regline, (colnr_T)(reginput - regline)) + 1); |
| 5875 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5876 | { |
| 5877 | add_here = TRUE; |
| 5878 | add_state = t->state->out; |
| 5879 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5880 | break; |
| 5881 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 5882 | case NFA_MARK: |
| 5883 | case NFA_MARK_GT: |
| 5884 | case NFA_MARK_LT: |
| 5885 | { |
| 5886 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 5887 | |
| 5888 | /* Compare the mark position to the match position. */ |
| 5889 | result = (pos != NULL /* mark doesn't exist */ |
| 5890 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 5891 | && (pos->lnum == reglnum + reg_firstlnum |
| 5892 | ? (pos->col == (colnr_T)(reginput - regline) |
| 5893 | ? t->state->c == NFA_MARK |
| 5894 | : (pos->col < (colnr_T)(reginput - regline) |
| 5895 | ? t->state->c == NFA_MARK_GT |
| 5896 | : t->state->c == NFA_MARK_LT)) |
| 5897 | : (pos->lnum < reglnum + reg_firstlnum |
| 5898 | ? t->state->c == NFA_MARK_GT |
| 5899 | : t->state->c == NFA_MARK_LT))); |
| 5900 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5901 | { |
| 5902 | add_here = TRUE; |
| 5903 | add_state = t->state->out; |
| 5904 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 5905 | break; |
| 5906 | } |
| 5907 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5908 | case NFA_CURSOR: |
| 5909 | result = (reg_win != NULL |
| 5910 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 5911 | && ((colnr_T)(reginput - regline) |
| 5912 | == reg_win->w_cursor.col)); |
| 5913 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5914 | { |
| 5915 | add_here = TRUE; |
| 5916 | add_state = t->state->out; |
| 5917 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5918 | break; |
| 5919 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5920 | case NFA_VISUAL: |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5921 | #ifdef FEAT_VISUAL |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5922 | result = reg_match_visual(); |
| 5923 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5924 | { |
| 5925 | add_here = TRUE; |
| 5926 | add_state = t->state->out; |
| 5927 | } |
Bram Moolenaar | 78eae9a | 2013-06-05 11:02:05 +0200 | [diff] [blame] | 5928 | #endif |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5929 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5930 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5931 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5932 | { |
| 5933 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5934 | |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5935 | /* TODO: put this in #ifdef later */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5936 | if (c < 0) |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5937 | EMSGN("INTERNAL: Negative state char: %ld", c); |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5938 | result = (c == curc); |
| 5939 | |
| 5940 | if (!result && ireg_ic) |
| 5941 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5942 | #ifdef FEAT_MBYTE |
| 5943 | /* If there is a composing character which is not being |
| 5944 | * ignored there can be no match. Match with composing |
| 5945 | * character uses NFA_COMPOSING above. */ |
| 5946 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5947 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5948 | result = FALSE; |
| 5949 | #endif |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5950 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5951 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5952 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5953 | |
| 5954 | } /* switch (t->state->c) */ |
| 5955 | |
| 5956 | if (add_state != NULL) |
| 5957 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5958 | nfa_pim_T *pim; |
| 5959 | |
| 5960 | if (t->pim.result == NFA_PIM_UNUSED) |
| 5961 | pim = NULL; |
| 5962 | else |
| 5963 | pim = &t->pim; |
| 5964 | |
| 5965 | /* Handle the postponed invisible match if the match might end |
| 5966 | * without advancing and before the end of the line. */ |
| 5967 | if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5968 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5969 | if (pim->result == NFA_PIM_TODO) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5970 | { |
| 5971 | #ifdef ENABLE_LOG |
| 5972 | fprintf(log_fd, "\n"); |
| 5973 | fprintf(log_fd, "==================================\n"); |
| 5974 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 5975 | fprintf(log_fd, "\n"); |
| 5976 | #endif |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5977 | result = recursive_regmatch(pim->state, pim, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5978 | prog, submatch, m, &listids); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5979 | pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5980 | /* for \@! and \@<! it is a match when the result is |
| 5981 | * FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5982 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5983 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5984 | || pim->state->c |
| 5985 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5986 | || pim->state->c |
| 5987 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5988 | { |
| 5989 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5990 | copy_sub_off(&pim->subs.norm, &m->norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5991 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5992 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5993 | copy_sub_off(&pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5994 | #endif |
| 5995 | } |
| 5996 | } |
| 5997 | else |
| 5998 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5999 | result = (pim->result == NFA_PIM_MATCH); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6000 | #ifdef ENABLE_LOG |
| 6001 | fprintf(log_fd, "\n"); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6002 | fprintf(log_fd, "Using previous recursive nfa_regmatch() result, result == %d\n", pim->result); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6003 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 6004 | fprintf(log_fd, "\n"); |
| 6005 | #endif |
| 6006 | } |
| 6007 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6008 | /* for \@! and \@<! it is a match when result is FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6009 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6010 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6011 | || pim->state->c |
| 6012 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6013 | || pim->state->c |
| 6014 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6015 | { |
| 6016 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6017 | copy_sub_off(&t->subs.norm, &pim->subs.norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6018 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6019 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6020 | copy_sub_off(&t->subs.synt, &pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6021 | #endif |
| 6022 | } |
| 6023 | else |
| 6024 | /* look-behind match failed, don't add the state */ |
| 6025 | continue; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6026 | |
| 6027 | /* Postponed invisible match was handled, don't add it to |
| 6028 | * following states. */ |
| 6029 | pim = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6030 | } |
| 6031 | |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6032 | if (add_here) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6033 | addstate_here(thislist, add_state, &t->subs, pim, &listidx); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6034 | else |
| 6035 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6036 | addstate(nextlist, add_state, &t->subs, pim, add_off); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6037 | if (add_count > 0) |
| 6038 | nextlist->t[nextlist->n - 1].count = add_count; |
| 6039 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6040 | } |
| 6041 | |
| 6042 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 6043 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6044 | /* Look for the start of a match in the current position by adding the |
| 6045 | * start state to the list of states. |
| 6046 | * The first found match is the leftmost one, thus the order of states |
| 6047 | * matters! |
| 6048 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 6049 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6050 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6051 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6052 | if (nfa_match == FALSE |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6053 | && ((toplevel |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6054 | && reglnum == 0 |
| 6055 | && clen != 0 |
| 6056 | && (ireg_maxcol == 0 |
| 6057 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6058 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6059 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6060 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 6061 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6062 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6063 | < nfa_endp->se_u.pos.col)) |
| 6064 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6065 | { |
| 6066 | #ifdef ENABLE_LOG |
| 6067 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 6068 | #endif |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6069 | /* Inline optimized code for addstate() if we know the state is |
| 6070 | * the first MOPEN. */ |
| 6071 | if (toplevel) |
| 6072 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6073 | int add = TRUE; |
| 6074 | int c; |
| 6075 | |
| 6076 | if (prog->regstart != NUL && clen != 0) |
| 6077 | { |
| 6078 | if (nextlist->n == 0) |
| 6079 | { |
| 6080 | colnr_T col = (colnr_T)(reginput - regline) + clen; |
| 6081 | |
| 6082 | /* Nextlist is empty, we can skip ahead to the |
| 6083 | * character that must appear at the start. */ |
| 6084 | if (skip_to_start(prog->regstart, &col) == FAIL) |
| 6085 | break; |
| 6086 | #ifdef ENABLE_LOG |
| 6087 | fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
| 6088 | col - ((colnr_T)(reginput - regline) + clen)); |
| 6089 | #endif |
| 6090 | reginput = regline + col - clen; |
| 6091 | } |
| 6092 | else |
| 6093 | { |
| 6094 | /* Checking if the required start character matches is |
| 6095 | * cheaper than adding a state that won't match. */ |
| 6096 | c = PTR2CHAR(reginput + clen); |
| 6097 | if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c) |
| 6098 | != MB_TOLOWER(prog->regstart))) |
| 6099 | { |
| 6100 | #ifdef ENABLE_LOG |
| 6101 | fprintf(log_fd, " Skipping start state, regstart does not match\n"); |
| 6102 | #endif |
| 6103 | add = FALSE; |
| 6104 | } |
| 6105 | } |
| 6106 | } |
| 6107 | |
| 6108 | if (add) |
| 6109 | { |
| 6110 | if (REG_MULTI) |
| 6111 | m->norm.list.multi[0].start.col = |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6112 | (colnr_T)(reginput - regline) + clen; |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6113 | else |
| 6114 | m->norm.list.line[0].start = reginput + clen; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6115 | addstate(nextlist, start->out, m, NULL, clen); |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6116 | } |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6117 | } |
| 6118 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6119 | addstate(nextlist, start, m, NULL, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6120 | } |
| 6121 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6122 | #ifdef ENABLE_LOG |
| 6123 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6124 | { |
| 6125 | int i; |
| 6126 | |
| 6127 | for (i = 0; i < thislist->n; i++) |
| 6128 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 6129 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6130 | fprintf(log_fd, "\n"); |
| 6131 | #endif |
| 6132 | |
| 6133 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6134 | /* Advance to the next character, or advance to the next line, or |
| 6135 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6136 | if (clen != 0) |
| 6137 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6138 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 6139 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6140 | reg_nextline(); |
| 6141 | else |
| 6142 | break; |
| 6143 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6144 | |
| 6145 | #ifdef ENABLE_LOG |
| 6146 | if (log_fd != stderr) |
| 6147 | fclose(log_fd); |
| 6148 | log_fd = NULL; |
| 6149 | #endif |
| 6150 | |
| 6151 | theend: |
| 6152 | /* Free memory */ |
| 6153 | vim_free(list[0].t); |
| 6154 | vim_free(list[1].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6155 | vim_free(listids); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6156 | #undef ADD_STATE_IF_MATCH |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 6157 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6158 | fclose(debug); |
| 6159 | #endif |
| 6160 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6161 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6162 | } |
| 6163 | |
| 6164 | /* |
| 6165 | * Try match of "prog" with at regline["col"]. |
| 6166 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6167 | */ |
| 6168 | static long |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6169 | nfa_regtry(prog, col) |
| 6170 | nfa_regprog_T *prog; |
| 6171 | colnr_T col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6172 | { |
| 6173 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6174 | regsubs_T subs, m; |
| 6175 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6176 | #ifdef ENABLE_LOG |
| 6177 | FILE *f; |
| 6178 | #endif |
| 6179 | |
| 6180 | reginput = regline + col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6181 | |
| 6182 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6183 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6184 | if (f != NULL) |
| 6185 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6186 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6187 | #ifdef DEBUG |
| 6188 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 6189 | #endif |
| 6190 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6191 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 6192 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6193 | fprintf(f, "\n\n"); |
| 6194 | fclose(f); |
| 6195 | } |
| 6196 | else |
| 6197 | EMSG(_("Could not open temporary log file for writing ")); |
| 6198 | #endif |
| 6199 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6200 | clear_sub(&subs.norm); |
| 6201 | clear_sub(&m.norm); |
| 6202 | #ifdef FEAT_SYN_HL |
| 6203 | clear_sub(&subs.synt); |
| 6204 | clear_sub(&m.synt); |
| 6205 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6206 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 6207 | if (nfa_regmatch(prog, start, &subs, &m) == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6208 | return 0; |
| 6209 | |
| 6210 | cleanup_subexpr(); |
| 6211 | if (REG_MULTI) |
| 6212 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6213 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6214 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6215 | reg_startpos[i] = subs.norm.list.multi[i].start; |
| 6216 | reg_endpos[i] = subs.norm.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6217 | } |
| 6218 | |
| 6219 | if (reg_startpos[0].lnum < 0) |
| 6220 | { |
| 6221 | reg_startpos[0].lnum = 0; |
| 6222 | reg_startpos[0].col = col; |
| 6223 | } |
| 6224 | if (reg_endpos[0].lnum < 0) |
| 6225 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 6226 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6227 | reg_endpos[0].lnum = reglnum; |
| 6228 | reg_endpos[0].col = (int)(reginput - regline); |
| 6229 | } |
| 6230 | else |
| 6231 | /* Use line number of "\ze". */ |
| 6232 | reglnum = reg_endpos[0].lnum; |
| 6233 | } |
| 6234 | else |
| 6235 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6236 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6237 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6238 | reg_startp[i] = subs.norm.list.line[i].start; |
| 6239 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6240 | } |
| 6241 | |
| 6242 | if (reg_startp[0] == NULL) |
| 6243 | reg_startp[0] = regline + col; |
| 6244 | if (reg_endp[0] == NULL) |
| 6245 | reg_endp[0] = reginput; |
| 6246 | } |
| 6247 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6248 | #ifdef FEAT_SYN_HL |
| 6249 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 6250 | unref_extmatch(re_extmatch_out); |
| 6251 | re_extmatch_out = NULL; |
| 6252 | |
| 6253 | if (prog->reghasz == REX_SET) |
| 6254 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6255 | cleanup_zsubexpr(); |
| 6256 | re_extmatch_out = make_extmatch(); |
| 6257 | for (i = 0; i < subs.synt.in_use; i++) |
| 6258 | { |
| 6259 | if (REG_MULTI) |
| 6260 | { |
| 6261 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 6262 | |
| 6263 | /* Only accept single line matches. */ |
| 6264 | if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum) |
| 6265 | re_extmatch_out->matches[i] = |
| 6266 | vim_strnsave(reg_getline(mpos->start.lnum) |
| 6267 | + mpos->start.col, |
| 6268 | mpos->end.col - mpos->start.col); |
| 6269 | } |
| 6270 | else |
| 6271 | { |
| 6272 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 6273 | |
| 6274 | if (lpos->start != NULL && lpos->end != NULL) |
| 6275 | re_extmatch_out->matches[i] = |
| 6276 | vim_strnsave(lpos->start, |
| 6277 | (int)(lpos->end - lpos->start)); |
| 6278 | } |
| 6279 | } |
| 6280 | } |
| 6281 | #endif |
| 6282 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6283 | return 1 + reglnum; |
| 6284 | } |
| 6285 | |
| 6286 | /* |
| 6287 | * Match a regexp against a string ("line" points to the string) or multiple |
| 6288 | * lines ("line" is NULL, use reg_getline()). |
| 6289 | * |
| 6290 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6291 | */ |
| 6292 | static long |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6293 | nfa_regexec_both(line, startcol) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6294 | char_u *line; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6295 | colnr_T startcol; /* column to start looking for match */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6296 | { |
| 6297 | nfa_regprog_T *prog; |
| 6298 | long retval = 0L; |
| 6299 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6300 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6301 | |
| 6302 | if (REG_MULTI) |
| 6303 | { |
| 6304 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 6305 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 6306 | reg_startpos = reg_mmatch->startpos; |
| 6307 | reg_endpos = reg_mmatch->endpos; |
| 6308 | } |
| 6309 | else |
| 6310 | { |
| 6311 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 6312 | reg_startp = reg_match->startp; |
| 6313 | reg_endp = reg_match->endp; |
| 6314 | } |
| 6315 | |
| 6316 | /* Be paranoid... */ |
| 6317 | if (prog == NULL || line == NULL) |
| 6318 | { |
| 6319 | EMSG(_(e_null)); |
| 6320 | goto theend; |
| 6321 | } |
| 6322 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6323 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 6324 | if (prog->regflags & RF_ICASE) |
| 6325 | ireg_ic = TRUE; |
| 6326 | else if (prog->regflags & RF_NOICASE) |
| 6327 | ireg_ic = FALSE; |
| 6328 | |
| 6329 | #ifdef FEAT_MBYTE |
| 6330 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 6331 | if (prog->regflags & RF_ICOMBINE) |
| 6332 | ireg_icombine = TRUE; |
| 6333 | #endif |
| 6334 | |
| 6335 | regline = line; |
| 6336 | reglnum = 0; /* relative to line */ |
| 6337 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6338 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6339 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6340 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6341 | nfa_listid = 1; |
| 6342 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6343 | #ifdef DEBUG |
| 6344 | nfa_regengine.expr = prog->pattern; |
| 6345 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6346 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6347 | if (prog->reganch && col > 0) |
| 6348 | return 0L; |
| 6349 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6350 | need_clear_subexpr = TRUE; |
| 6351 | #ifdef FEAT_SYN_HL |
| 6352 | /* Clear the external match subpointers if necessary. */ |
| 6353 | if (prog->reghasz == REX_SET) |
| 6354 | { |
| 6355 | nfa_has_zsubexpr = TRUE; |
| 6356 | need_clear_zsubexpr = TRUE; |
| 6357 | } |
| 6358 | else |
| 6359 | nfa_has_zsubexpr = FALSE; |
| 6360 | #endif |
| 6361 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6362 | if (prog->regstart != NUL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6363 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6364 | /* Skip ahead until a character we know the match must start with. |
| 6365 | * When there is none there is no match. */ |
| 6366 | if (skip_to_start(prog->regstart, &col) == FAIL) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6367 | return 0L; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6368 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6369 | /* If match_text is set it contains the full text that must match. |
| 6370 | * Nothing else to try. Doesn't handle combining chars well. */ |
Bram Moolenaar | a940aa6 | 2013-06-08 23:30:04 +0200 | [diff] [blame] | 6371 | if (prog->match_text != NULL |
| 6372 | #ifdef FEAT_MBYTE |
| 6373 | && !ireg_icombine |
| 6374 | #endif |
| 6375 | ) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6376 | return find_match_text(col, prog->regstart, prog->match_text); |
| 6377 | } |
| 6378 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6379 | /* If the start column is past the maximum column: no need to try. */ |
| 6380 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 6381 | goto theend; |
| 6382 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6383 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6384 | for (i = 0; i < nstate; ++i) |
| 6385 | { |
| 6386 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6387 | prog->state[i].lastlist[0] = 0; |
| 6388 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6389 | } |
| 6390 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6391 | retval = nfa_regtry(prog, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6392 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6393 | #ifdef DEBUG |
| 6394 | nfa_regengine.expr = NULL; |
| 6395 | #endif |
| 6396 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6397 | theend: |
| 6398 | return retval; |
| 6399 | } |
| 6400 | |
| 6401 | /* |
| 6402 | * Compile a regular expression into internal code for the NFA matcher. |
| 6403 | * Returns the program in allocated space. Returns NULL for an error. |
| 6404 | */ |
| 6405 | static regprog_T * |
| 6406 | nfa_regcomp(expr, re_flags) |
| 6407 | char_u *expr; |
| 6408 | int re_flags; |
| 6409 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6410 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 6411 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6412 | int *postfix; |
| 6413 | |
| 6414 | if (expr == NULL) |
| 6415 | return NULL; |
| 6416 | |
| 6417 | #ifdef DEBUG |
| 6418 | nfa_regengine.expr = expr; |
| 6419 | #endif |
| 6420 | |
| 6421 | init_class_tab(); |
| 6422 | |
| 6423 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 6424 | return NULL; |
| 6425 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6426 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6427 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6428 | postfix = re2post(); |
| 6429 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6430 | { |
| 6431 | /* TODO: only give this error for debugging? */ |
| 6432 | if (post_ptr >= post_end) |
| 6433 | 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] | 6434 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6435 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6436 | |
| 6437 | /* |
| 6438 | * In order to build the NFA, we parse the input regexp twice: |
| 6439 | * 1. first pass to count size (so we can allocate space) |
| 6440 | * 2. second to emit code |
| 6441 | */ |
| 6442 | #ifdef ENABLE_LOG |
| 6443 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6444 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6445 | |
| 6446 | if (f != NULL) |
| 6447 | { |
| 6448 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 6449 | fclose(f); |
| 6450 | } |
| 6451 | } |
| 6452 | #endif |
| 6453 | |
| 6454 | /* |
| 6455 | * PASS 1 |
| 6456 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 6457 | */ |
| 6458 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6459 | |
Bram Moolenaar | 16619a2 | 2013-06-11 18:42:36 +0200 | [diff] [blame] | 6460 | /* allocate the regprog with space for the compiled regexp */ |
| 6461 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6462 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 6463 | if (prog == NULL) |
| 6464 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6465 | state_ptr = prog->state; |
| 6466 | |
| 6467 | /* |
| 6468 | * PASS 2 |
| 6469 | * Build the NFA |
| 6470 | */ |
| 6471 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 6472 | if (prog->start == NULL) |
| 6473 | goto fail; |
| 6474 | |
| 6475 | prog->regflags = regflags; |
| 6476 | prog->engine = &nfa_regengine; |
| 6477 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6478 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6479 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6480 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6481 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6482 | nfa_postprocess(prog); |
| 6483 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6484 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 6485 | prog->regstart = nfa_get_regstart(prog->start, 0); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6486 | prog->match_text = nfa_get_match_text(prog->start); |
| 6487 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6488 | #ifdef ENABLE_LOG |
| 6489 | nfa_postfix_dump(expr, OK); |
| 6490 | nfa_dump(prog); |
| 6491 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6492 | #ifdef FEAT_SYN_HL |
| 6493 | /* Remember whether this pattern has any \z specials in it. */ |
| 6494 | prog->reghasz = re_has_z; |
| 6495 | #endif |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6496 | #ifdef DEBUG |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6497 | prog->pattern = vim_strsave(expr); |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6498 | nfa_regengine.expr = NULL; |
| 6499 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6500 | |
| 6501 | out: |
| 6502 | vim_free(post_start); |
| 6503 | post_start = post_ptr = post_end = NULL; |
| 6504 | state_ptr = NULL; |
| 6505 | return (regprog_T *)prog; |
| 6506 | |
| 6507 | fail: |
| 6508 | vim_free(prog); |
| 6509 | prog = NULL; |
| 6510 | #ifdef ENABLE_LOG |
| 6511 | nfa_postfix_dump(expr, FAIL); |
| 6512 | #endif |
| 6513 | #ifdef DEBUG |
| 6514 | nfa_regengine.expr = NULL; |
| 6515 | #endif |
| 6516 | goto out; |
| 6517 | } |
| 6518 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6519 | /* |
| 6520 | * Free a compiled regexp program, returned by nfa_regcomp(). |
| 6521 | */ |
| 6522 | static void |
| 6523 | nfa_regfree(prog) |
| 6524 | regprog_T *prog; |
| 6525 | { |
| 6526 | if (prog != NULL) |
| 6527 | { |
| 6528 | vim_free(((nfa_regprog_T *)prog)->match_text); |
| 6529 | #ifdef DEBUG |
| 6530 | vim_free(((nfa_regprog_T *)prog)->pattern); |
| 6531 | #endif |
| 6532 | vim_free(prog); |
| 6533 | } |
| 6534 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6535 | |
| 6536 | /* |
| 6537 | * Match a regexp against a string. |
| 6538 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 6539 | * Uses curbuf for line count and 'iskeyword'. |
| 6540 | * |
| 6541 | * Return TRUE if there is a match, FALSE if not. |
| 6542 | */ |
| 6543 | static int |
| 6544 | nfa_regexec(rmp, line, col) |
| 6545 | regmatch_T *rmp; |
| 6546 | char_u *line; /* string to match against */ |
| 6547 | colnr_T col; /* column to start looking for match */ |
| 6548 | { |
| 6549 | reg_match = rmp; |
| 6550 | reg_mmatch = NULL; |
| 6551 | reg_maxline = 0; |
| 6552 | reg_line_lbr = FALSE; |
| 6553 | reg_buf = curbuf; |
| 6554 | reg_win = NULL; |
| 6555 | ireg_ic = rmp->rm_ic; |
| 6556 | #ifdef FEAT_MBYTE |
| 6557 | ireg_icombine = FALSE; |
| 6558 | #endif |
| 6559 | ireg_maxcol = 0; |
| 6560 | return (nfa_regexec_both(line, col) != 0); |
| 6561 | } |
| 6562 | |
| 6563 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 6564 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 6565 | |
| 6566 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 6567 | |
| 6568 | /* |
| 6569 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 6570 | */ |
| 6571 | static int |
| 6572 | nfa_regexec_nl(rmp, line, col) |
| 6573 | regmatch_T *rmp; |
| 6574 | char_u *line; /* string to match against */ |
| 6575 | colnr_T col; /* column to start looking for match */ |
| 6576 | { |
| 6577 | reg_match = rmp; |
| 6578 | reg_mmatch = NULL; |
| 6579 | reg_maxline = 0; |
| 6580 | reg_line_lbr = TRUE; |
| 6581 | reg_buf = curbuf; |
| 6582 | reg_win = NULL; |
| 6583 | ireg_ic = rmp->rm_ic; |
| 6584 | #ifdef FEAT_MBYTE |
| 6585 | ireg_icombine = FALSE; |
| 6586 | #endif |
| 6587 | ireg_maxcol = 0; |
| 6588 | return (nfa_regexec_both(line, col) != 0); |
| 6589 | } |
| 6590 | #endif |
| 6591 | |
| 6592 | |
| 6593 | /* |
| 6594 | * Match a regexp against multiple lines. |
| 6595 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 6596 | * Uses curbuf for line count and 'iskeyword'. |
| 6597 | * |
| 6598 | * Return zero if there is no match. Return number of lines contained in the |
| 6599 | * match otherwise. |
| 6600 | * |
| 6601 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 6602 | * |
| 6603 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 6604 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 6605 | * |
| 6606 | * +-------------------------+ |
| 6607 | * |a | |
| 6608 | * |b | |
| 6609 | * |c | |
| 6610 | * | | |
| 6611 | * +-------------------------+ |
| 6612 | * |
| 6613 | * then nfa_regexec_multi() returns 3. while the original |
| 6614 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 6615 | * |
| 6616 | * FIXME if this behavior is not compatible. |
| 6617 | */ |
| 6618 | static long |
| 6619 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 6620 | regmmatch_T *rmp; |
| 6621 | win_T *win; /* window in which to search or NULL */ |
| 6622 | buf_T *buf; /* buffer in which to search */ |
| 6623 | linenr_T lnum; /* nr of line to start looking for match */ |
| 6624 | colnr_T col; /* column to start looking for match */ |
| 6625 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 6626 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6627 | reg_match = NULL; |
| 6628 | reg_mmatch = rmp; |
| 6629 | reg_buf = buf; |
| 6630 | reg_win = win; |
| 6631 | reg_firstlnum = lnum; |
| 6632 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 6633 | reg_line_lbr = FALSE; |
| 6634 | ireg_ic = rmp->rmm_ic; |
| 6635 | #ifdef FEAT_MBYTE |
| 6636 | ireg_icombine = FALSE; |
| 6637 | #endif |
| 6638 | ireg_maxcol = rmp->rmm_maxcol; |
| 6639 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 6640 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6641 | } |
| 6642 | |
| 6643 | #ifdef DEBUG |
| 6644 | # undef ENABLE_LOG |
| 6645 | #endif |