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 | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 276 | 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] | 277 | static int nfa_emit_equi_class __ARGS((int c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 278 | static int nfa_regatom __ARGS((void)); |
| 279 | static int nfa_regpiece __ARGS((void)); |
| 280 | static int nfa_regconcat __ARGS((void)); |
| 281 | static int nfa_regbranch __ARGS((void)); |
| 282 | static int nfa_reg __ARGS((int paren)); |
| 283 | #ifdef DEBUG |
| 284 | static void nfa_set_code __ARGS((int c)); |
| 285 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 286 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 287 | 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] | 288 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 289 | #endif |
| 290 | static int *re2post __ARGS((void)); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 291 | 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] | 292 | 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] | 293 | static void nfa_postprocess __ARGS((nfa_regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 294 | static int check_char_class __ARGS((int class, int c)); |
| 295 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 296 | static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list)); |
| 297 | static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 298 | 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] | 299 | static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 300 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 301 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 302 | static void nfa_regfree __ARGS((regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 303 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 304 | 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] | 305 | static int match_follows __ARGS((nfa_state_T *startstate, int depth)); |
| 306 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 307 | |
| 308 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 309 | #define EMIT(c) do { \ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 310 | if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 311 | return FAIL; \ |
| 312 | *post_ptr++ = c; \ |
| 313 | } while (0) |
| 314 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 315 | /* |
| 316 | * Initialize internal variables before NFA compilation. |
| 317 | * Return OK on success, FAIL otherwise. |
| 318 | */ |
| 319 | static int |
| 320 | nfa_regcomp_start(expr, re_flags) |
| 321 | char_u *expr; |
| 322 | int re_flags; /* see vim_regcomp() */ |
| 323 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 324 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 325 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 326 | |
| 327 | nstate = 0; |
| 328 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 329 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 330 | nstate_max = (int)(STRLEN(expr) + 1) * 25; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 331 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 332 | /* 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] | 333 | * When it is still not enough realloc_post_list() will be used. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 334 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 335 | |
| 336 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 337 | postfix_size = sizeof(int) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 338 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 339 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 340 | if (post_start == NULL) |
| 341 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 342 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 343 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 344 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 345 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 346 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 347 | /* shared with BT engine */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 348 | regcomp_start(expr, re_flags); |
| 349 | |
| 350 | return OK; |
| 351 | } |
| 352 | |
| 353 | /* |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 354 | * Figure out if the NFA state list starts with an anchor, must match at start |
| 355 | * of the line. |
| 356 | */ |
| 357 | static int |
| 358 | nfa_get_reganch(start, depth) |
| 359 | nfa_state_T *start; |
| 360 | int depth; |
| 361 | { |
| 362 | nfa_state_T *p = start; |
| 363 | |
| 364 | if (depth > 4) |
| 365 | return 0; |
| 366 | |
| 367 | while (p != NULL) |
| 368 | { |
| 369 | switch (p->c) |
| 370 | { |
| 371 | case NFA_BOL: |
| 372 | case NFA_BOF: |
| 373 | return 1; /* yes! */ |
| 374 | |
| 375 | case NFA_ZSTART: |
| 376 | case NFA_ZEND: |
| 377 | case NFA_CURSOR: |
| 378 | case NFA_VISUAL: |
| 379 | |
| 380 | case NFA_MOPEN: |
| 381 | case NFA_MOPEN1: |
| 382 | case NFA_MOPEN2: |
| 383 | case NFA_MOPEN3: |
| 384 | case NFA_MOPEN4: |
| 385 | case NFA_MOPEN5: |
| 386 | case NFA_MOPEN6: |
| 387 | case NFA_MOPEN7: |
| 388 | case NFA_MOPEN8: |
| 389 | case NFA_MOPEN9: |
| 390 | case NFA_NOPEN: |
| 391 | #ifdef FEAT_SYN_HL |
| 392 | case NFA_ZOPEN: |
| 393 | case NFA_ZOPEN1: |
| 394 | case NFA_ZOPEN2: |
| 395 | case NFA_ZOPEN3: |
| 396 | case NFA_ZOPEN4: |
| 397 | case NFA_ZOPEN5: |
| 398 | case NFA_ZOPEN6: |
| 399 | case NFA_ZOPEN7: |
| 400 | case NFA_ZOPEN8: |
| 401 | case NFA_ZOPEN9: |
| 402 | #endif |
| 403 | p = p->out; |
| 404 | break; |
| 405 | |
| 406 | case NFA_SPLIT: |
| 407 | return nfa_get_reganch(p->out, depth + 1) |
| 408 | && nfa_get_reganch(p->out1, depth + 1); |
| 409 | |
| 410 | default: |
| 411 | return 0; /* noooo */ |
| 412 | } |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | /* |
| 418 | * Figure out if the NFA state list starts with a character which must match |
| 419 | * at start of the match. |
| 420 | */ |
| 421 | static int |
| 422 | nfa_get_regstart(start, depth) |
| 423 | nfa_state_T *start; |
| 424 | int depth; |
| 425 | { |
| 426 | nfa_state_T *p = start; |
| 427 | |
| 428 | if (depth > 4) |
| 429 | return 0; |
| 430 | |
| 431 | while (p != NULL) |
| 432 | { |
| 433 | switch (p->c) |
| 434 | { |
| 435 | /* all kinds of zero-width matches */ |
| 436 | case NFA_BOL: |
| 437 | case NFA_BOF: |
| 438 | case NFA_BOW: |
| 439 | case NFA_EOW: |
| 440 | case NFA_ZSTART: |
| 441 | case NFA_ZEND: |
| 442 | case NFA_CURSOR: |
| 443 | case NFA_VISUAL: |
| 444 | case NFA_LNUM: |
| 445 | case NFA_LNUM_GT: |
| 446 | case NFA_LNUM_LT: |
| 447 | case NFA_COL: |
| 448 | case NFA_COL_GT: |
| 449 | case NFA_COL_LT: |
| 450 | case NFA_VCOL: |
| 451 | case NFA_VCOL_GT: |
| 452 | case NFA_VCOL_LT: |
| 453 | case NFA_MARK: |
| 454 | case NFA_MARK_GT: |
| 455 | case NFA_MARK_LT: |
| 456 | |
| 457 | case NFA_MOPEN: |
| 458 | case NFA_MOPEN1: |
| 459 | case NFA_MOPEN2: |
| 460 | case NFA_MOPEN3: |
| 461 | case NFA_MOPEN4: |
| 462 | case NFA_MOPEN5: |
| 463 | case NFA_MOPEN6: |
| 464 | case NFA_MOPEN7: |
| 465 | case NFA_MOPEN8: |
| 466 | case NFA_MOPEN9: |
| 467 | case NFA_NOPEN: |
| 468 | #ifdef FEAT_SYN_HL |
| 469 | case NFA_ZOPEN: |
| 470 | case NFA_ZOPEN1: |
| 471 | case NFA_ZOPEN2: |
| 472 | case NFA_ZOPEN3: |
| 473 | case NFA_ZOPEN4: |
| 474 | case NFA_ZOPEN5: |
| 475 | case NFA_ZOPEN6: |
| 476 | case NFA_ZOPEN7: |
| 477 | case NFA_ZOPEN8: |
| 478 | case NFA_ZOPEN9: |
| 479 | #endif |
| 480 | p = p->out; |
| 481 | break; |
| 482 | |
| 483 | case NFA_SPLIT: |
| 484 | { |
| 485 | int c1 = nfa_get_regstart(p->out, depth + 1); |
| 486 | int c2 = nfa_get_regstart(p->out1, depth + 1); |
| 487 | |
| 488 | if (c1 == c2) |
| 489 | return c1; /* yes! */ |
| 490 | return 0; |
| 491 | } |
| 492 | |
| 493 | default: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 494 | if (p->c > 0) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 495 | return p->c; /* yes! */ |
| 496 | return 0; |
| 497 | } |
| 498 | } |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 503 | * 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] | 504 | * else. If so return a string in allocated memory with what must match after |
| 505 | * regstart. Otherwise return NULL. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 506 | */ |
| 507 | static char_u * |
| 508 | nfa_get_match_text(start) |
| 509 | nfa_state_T *start; |
| 510 | { |
| 511 | nfa_state_T *p = start; |
| 512 | int len = 0; |
| 513 | char_u *ret; |
| 514 | char_u *s; |
| 515 | |
| 516 | if (p->c != NFA_MOPEN) |
| 517 | return NULL; /* just in case */ |
| 518 | p = p->out; |
| 519 | while (p->c > 0) |
| 520 | { |
| 521 | len += MB_CHAR2LEN(p->c); |
| 522 | p = p->out; |
| 523 | } |
| 524 | if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH) |
| 525 | return NULL; |
| 526 | |
| 527 | ret = alloc(len); |
| 528 | if (ret != NULL) |
| 529 | { |
| 530 | len = 0; |
| 531 | p = start->out->out; /* skip first char, it goes into regstart */ |
| 532 | s = ret; |
| 533 | while (p->c > 0) |
| 534 | { |
| 535 | #ifdef FEAT_MBYTE |
| 536 | if (has_mbyte) |
| 537 | s += (*mb_char2bytes)(p->c, s); |
| 538 | else |
| 539 | #endif |
| 540 | *s++ = p->c; |
| 541 | p = p->out; |
| 542 | } |
| 543 | *s = NUL; |
| 544 | } |
| 545 | return ret; |
| 546 | } |
| 547 | |
| 548 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 549 | * Allocate more space for post_start. Called when |
| 550 | * running above the estimated number of states. |
| 551 | */ |
| 552 | static int |
| 553 | realloc_post_list() |
| 554 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 555 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 556 | int new_max = nstate_max + 1000; |
| 557 | int *new_start; |
| 558 | int *old_start; |
| 559 | |
| 560 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 561 | if (new_start == NULL) |
| 562 | return FAIL; |
| 563 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 564 | old_start = post_start; |
| 565 | post_start = new_start; |
| 566 | post_ptr = new_start + (post_ptr - old_start); |
| 567 | post_end = post_start + new_max; |
| 568 | vim_free(old_start); |
| 569 | return OK; |
| 570 | } |
| 571 | |
| 572 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 573 | * Search between "start" and "end" and try to recognize a |
| 574 | * character class in expanded form. For example [0-9]. |
| 575 | * On success, return the id the character class to be emitted. |
| 576 | * On failure, return 0 (=FAIL) |
| 577 | * Start points to the first char of the range, while end should point |
| 578 | * to the closing brace. |
| 579 | */ |
| 580 | static int |
| 581 | nfa_recognize_char_class(start, end, extra_newl) |
| 582 | char_u *start; |
| 583 | char_u *end; |
| 584 | int extra_newl; |
| 585 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 586 | # define CLASS_not 0x80 |
| 587 | # define CLASS_af 0x40 |
| 588 | # define CLASS_AF 0x20 |
| 589 | # define CLASS_az 0x10 |
| 590 | # define CLASS_AZ 0x08 |
| 591 | # define CLASS_o7 0x04 |
| 592 | # define CLASS_o9 0x02 |
| 593 | # define CLASS_underscore 0x01 |
| 594 | |
| 595 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 596 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 597 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 598 | |
| 599 | if (extra_newl == TRUE) |
| 600 | newl = TRUE; |
| 601 | |
| 602 | if (*end != ']') |
| 603 | return FAIL; |
| 604 | p = start; |
| 605 | if (*p == '^') |
| 606 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 607 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 608 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | while (p < end) |
| 612 | { |
| 613 | if (p + 2 < end && *(p + 1) == '-') |
| 614 | { |
| 615 | switch (*p) |
| 616 | { |
| 617 | case '0': |
| 618 | if (*(p + 2) == '9') |
| 619 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 620 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 621 | break; |
| 622 | } |
| 623 | else |
| 624 | if (*(p + 2) == '7') |
| 625 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 626 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 627 | break; |
| 628 | } |
| 629 | case 'a': |
| 630 | if (*(p + 2) == 'z') |
| 631 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 632 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 633 | break; |
| 634 | } |
| 635 | else |
| 636 | if (*(p + 2) == 'f') |
| 637 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 638 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | case 'A': |
| 642 | if (*(p + 2) == 'Z') |
| 643 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 644 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 645 | break; |
| 646 | } |
| 647 | else |
| 648 | if (*(p + 2) == 'F') |
| 649 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 650 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 651 | break; |
| 652 | } |
| 653 | /* FALLTHROUGH */ |
| 654 | default: |
| 655 | return FAIL; |
| 656 | } |
| 657 | p += 3; |
| 658 | } |
| 659 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 660 | { |
| 661 | newl = TRUE; |
| 662 | p += 2; |
| 663 | } |
| 664 | else if (*p == '_') |
| 665 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 666 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 667 | p ++; |
| 668 | } |
| 669 | else if (*p == '\n') |
| 670 | { |
| 671 | newl = TRUE; |
| 672 | p ++; |
| 673 | } |
| 674 | else |
| 675 | return FAIL; |
| 676 | } /* while (p < end) */ |
| 677 | |
| 678 | if (p != end) |
| 679 | return FAIL; |
| 680 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 681 | if (newl == TRUE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 682 | extra_newl = ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 683 | |
| 684 | switch (config) |
| 685 | { |
| 686 | case CLASS_o9: |
| 687 | return extra_newl + NFA_DIGIT; |
| 688 | case CLASS_not | CLASS_o9: |
| 689 | return extra_newl + NFA_NDIGIT; |
| 690 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 691 | return extra_newl + NFA_HEX; |
| 692 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 693 | return extra_newl + NFA_NHEX; |
| 694 | case CLASS_o7: |
| 695 | return extra_newl + NFA_OCTAL; |
| 696 | case CLASS_not | CLASS_o7: |
| 697 | return extra_newl + NFA_NOCTAL; |
| 698 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 699 | return extra_newl + NFA_WORD; |
| 700 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 701 | return extra_newl + NFA_NWORD; |
| 702 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 703 | return extra_newl + NFA_HEAD; |
| 704 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 705 | return extra_newl + NFA_NHEAD; |
| 706 | case CLASS_az | CLASS_AZ: |
| 707 | return extra_newl + NFA_ALPHA; |
| 708 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 709 | return extra_newl + NFA_NALPHA; |
| 710 | case CLASS_az: |
| 711 | return extra_newl + NFA_LOWER; |
| 712 | case CLASS_not | CLASS_az: |
| 713 | return extra_newl + NFA_NLOWER; |
| 714 | case CLASS_AZ: |
| 715 | return extra_newl + NFA_UPPER; |
| 716 | case CLASS_not | CLASS_AZ: |
| 717 | return extra_newl + NFA_NUPPER; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 718 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 719 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /* |
| 723 | * Produce the bytes for equivalence class "c". |
| 724 | * Currently only handles latin1, latin9 and utf-8. |
| 725 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 726 | * equivalent to 'a OR b OR c' |
| 727 | * |
| 728 | * NOTE! When changing this function, also update reg_equi_class() |
| 729 | */ |
| 730 | static int |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 731 | nfa_emit_equi_class(c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 732 | int c; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 733 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 734 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 735 | |
| 736 | #ifdef FEAT_MBYTE |
| 737 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 738 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 739 | #endif |
| 740 | { |
| 741 | switch (c) |
| 742 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 743 | case 'A': case 0300: case 0301: case 0302: |
| 744 | case 0303: case 0304: case 0305: |
| 745 | EMIT2('A'); EMIT2(0300); EMIT2(0301); |
| 746 | EMIT2(0302); EMIT2(0303); EMIT2(0304); |
| 747 | EMIT2(0305); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 748 | return OK; |
| 749 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 750 | case 'C': case 0307: |
| 751 | EMIT2('C'); EMIT2(0307); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 752 | return OK; |
| 753 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 754 | case 'E': case 0310: case 0311: case 0312: case 0313: |
| 755 | EMIT2('E'); EMIT2(0310); EMIT2(0311); |
| 756 | EMIT2(0312); EMIT2(0313); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 757 | return OK; |
| 758 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 759 | case 'I': case 0314: case 0315: case 0316: case 0317: |
| 760 | EMIT2('I'); EMIT2(0314); EMIT2(0315); |
| 761 | EMIT2(0316); EMIT2(0317); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 762 | return OK; |
| 763 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 764 | case 'N': case 0321: |
| 765 | EMIT2('N'); EMIT2(0321); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 766 | return OK; |
| 767 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 768 | case 'O': case 0322: case 0323: case 0324: case 0325: |
| 769 | case 0326: |
| 770 | EMIT2('O'); EMIT2(0322); EMIT2(0323); |
| 771 | EMIT2(0324); EMIT2(0325); EMIT2(0326); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 772 | return OK; |
| 773 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 774 | case 'U': case 0331: case 0332: case 0333: case 0334: |
| 775 | EMIT2('U'); EMIT2(0331); EMIT2(0332); |
| 776 | EMIT2(0333); EMIT2(0334); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 777 | return OK; |
| 778 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 779 | case 'Y': case 0335: |
| 780 | EMIT2('Y'); EMIT2(0335); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 781 | return OK; |
| 782 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 783 | case 'a': case 0340: case 0341: case 0342: |
| 784 | case 0343: case 0344: case 0345: |
| 785 | EMIT2('a'); EMIT2(0340); EMIT2(0341); |
| 786 | EMIT2(0342); EMIT2(0343); EMIT2(0344); |
| 787 | EMIT2(0345); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 788 | return OK; |
| 789 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 790 | case 'c': case 0347: |
| 791 | EMIT2('c'); EMIT2(0347); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 792 | return OK; |
| 793 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 794 | case 'e': case 0350: case 0351: case 0352: case 0353: |
| 795 | EMIT2('e'); EMIT2(0350); EMIT2(0351); |
| 796 | EMIT2(0352); EMIT2(0353); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 797 | return OK; |
| 798 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 799 | case 'i': case 0354: case 0355: case 0356: case 0357: |
| 800 | EMIT2('i'); EMIT2(0354); EMIT2(0355); |
| 801 | EMIT2(0356); EMIT2(0357); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 802 | return OK; |
| 803 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 804 | case 'n': case 0361: |
| 805 | EMIT2('n'); EMIT2(0361); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 806 | return OK; |
| 807 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 808 | case 'o': case 0362: case 0363: case 0364: case 0365: |
| 809 | case 0366: |
| 810 | EMIT2('o'); EMIT2(0362); EMIT2(0363); |
| 811 | EMIT2(0364); EMIT2(0365); EMIT2(0366); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 812 | return OK; |
| 813 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 814 | case 'u': case 0371: case 0372: case 0373: case 0374: |
| 815 | EMIT2('u'); EMIT2(0371); EMIT2(0372); |
| 816 | EMIT2(0373); EMIT2(0374); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 817 | return OK; |
| 818 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 819 | case 'y': case 0375: case 0377: |
| 820 | EMIT2('y'); EMIT2(0375); EMIT2(0377); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 821 | return OK; |
| 822 | |
| 823 | default: |
| 824 | return FAIL; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | EMIT(c); |
| 829 | return OK; |
| 830 | #undef EMIT2 |
| 831 | } |
| 832 | |
| 833 | /* |
| 834 | * Code to parse regular expression. |
| 835 | * |
| 836 | * We try to reuse parsing functions in regexp.c to |
| 837 | * minimize surprise and keep the syntax consistent. |
| 838 | */ |
| 839 | |
| 840 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 841 | * Parse the lowest level. |
| 842 | * |
| 843 | * An atom can be one of a long list of items. Many atoms match one character |
| 844 | * in the text. It is often an ordinary character or a character class. |
| 845 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 846 | * is only for syntax highlighting. |
| 847 | * |
| 848 | * atom ::= ordinary-atom |
| 849 | * or \( pattern \) |
| 850 | * or \%( pattern \) |
| 851 | * or \z( pattern \) |
| 852 | */ |
| 853 | static int |
| 854 | nfa_regatom() |
| 855 | { |
| 856 | int c; |
| 857 | int charclass; |
| 858 | int equiclass; |
| 859 | int collclass; |
| 860 | int got_coll_char; |
| 861 | char_u *p; |
| 862 | char_u *endp; |
| 863 | #ifdef FEAT_MBYTE |
| 864 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 865 | #endif |
| 866 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 867 | int emit_range; |
| 868 | int negated; |
| 869 | int result; |
| 870 | int startc = -1; |
| 871 | int endc = -1; |
| 872 | int oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 873 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 874 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 875 | switch (c) |
| 876 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 877 | case NUL: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 878 | EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); |
| 879 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 880 | case Magic('^'): |
| 881 | EMIT(NFA_BOL); |
| 882 | break; |
| 883 | |
| 884 | case Magic('$'): |
| 885 | EMIT(NFA_EOL); |
| 886 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 887 | had_eol = TRUE; |
| 888 | #endif |
| 889 | break; |
| 890 | |
| 891 | case Magic('<'): |
| 892 | EMIT(NFA_BOW); |
| 893 | break; |
| 894 | |
| 895 | case Magic('>'): |
| 896 | EMIT(NFA_EOW); |
| 897 | break; |
| 898 | |
| 899 | case Magic('_'): |
| 900 | c = no_Magic(getchr()); |
| 901 | if (c == '^') /* "\_^" is start-of-line */ |
| 902 | { |
| 903 | EMIT(NFA_BOL); |
| 904 | break; |
| 905 | } |
| 906 | if (c == '$') /* "\_$" is end-of-line */ |
| 907 | { |
| 908 | EMIT(NFA_EOL); |
| 909 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 910 | had_eol = TRUE; |
| 911 | #endif |
| 912 | break; |
| 913 | } |
| 914 | |
| 915 | extra = ADD_NL; |
| 916 | |
| 917 | /* "\_[" is collection plus newline */ |
| 918 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 919 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 920 | |
| 921 | /* "\_x" is character class plus newline */ |
| 922 | /*FALLTHROUGH*/ |
| 923 | |
| 924 | /* |
| 925 | * Character classes. |
| 926 | */ |
| 927 | case Magic('.'): |
| 928 | case Magic('i'): |
| 929 | case Magic('I'): |
| 930 | case Magic('k'): |
| 931 | case Magic('K'): |
| 932 | case Magic('f'): |
| 933 | case Magic('F'): |
| 934 | case Magic('p'): |
| 935 | case Magic('P'): |
| 936 | case Magic('s'): |
| 937 | case Magic('S'): |
| 938 | case Magic('d'): |
| 939 | case Magic('D'): |
| 940 | case Magic('x'): |
| 941 | case Magic('X'): |
| 942 | case Magic('o'): |
| 943 | case Magic('O'): |
| 944 | case Magic('w'): |
| 945 | case Magic('W'): |
| 946 | case Magic('h'): |
| 947 | case Magic('H'): |
| 948 | case Magic('a'): |
| 949 | case Magic('A'): |
| 950 | case Magic('l'): |
| 951 | case Magic('L'): |
| 952 | case Magic('u'): |
| 953 | case Magic('U'): |
| 954 | p = vim_strchr(classchars, no_Magic(c)); |
| 955 | if (p == NULL) |
| 956 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 957 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 958 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 959 | } |
| 960 | #ifdef FEAT_MBYTE |
| 961 | /* When '.' is followed by a composing char ignore the dot, so that |
| 962 | * the composing char is matched here. */ |
| 963 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 964 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 965 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 966 | c = getchr(); |
| 967 | goto nfa_do_multibyte; |
| 968 | } |
| 969 | #endif |
| 970 | EMIT(nfa_classcodes[p - classchars]); |
| 971 | if (extra == ADD_NL) |
| 972 | { |
| 973 | EMIT(NFA_NEWL); |
| 974 | EMIT(NFA_OR); |
| 975 | regflags |= RF_HASNL; |
| 976 | } |
| 977 | break; |
| 978 | |
| 979 | case Magic('n'): |
| 980 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 981 | /* In a string "\n" matches a newline character. */ |
| 982 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 983 | else |
| 984 | { |
| 985 | /* In buffer text "\n" matches the end of a line. */ |
| 986 | EMIT(NFA_NEWL); |
| 987 | regflags |= RF_HASNL; |
| 988 | } |
| 989 | break; |
| 990 | |
| 991 | case Magic('('): |
| 992 | if (nfa_reg(REG_PAREN) == FAIL) |
| 993 | return FAIL; /* cascaded error */ |
| 994 | break; |
| 995 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 996 | case Magic('|'): |
| 997 | case Magic('&'): |
| 998 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 999 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1000 | return FAIL; |
| 1001 | |
| 1002 | case Magic('='): |
| 1003 | case Magic('?'): |
| 1004 | case Magic('+'): |
| 1005 | case Magic('@'): |
| 1006 | case Magic('*'): |
| 1007 | case Magic('{'): |
| 1008 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1009 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1010 | return FAIL; |
| 1011 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 1012 | case Magic('~'): |
| 1013 | { |
| 1014 | char_u *lp; |
| 1015 | |
| 1016 | /* Previous substitute pattern. |
| 1017 | * Generated as "\%(pattern\)". */ |
| 1018 | if (reg_prev_sub == NULL) |
| 1019 | { |
| 1020 | EMSG(_(e_nopresub)); |
| 1021 | return FAIL; |
| 1022 | } |
| 1023 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 1024 | { |
| 1025 | EMIT(PTR2CHAR(lp)); |
| 1026 | if (lp != reg_prev_sub) |
| 1027 | EMIT(NFA_CONCAT); |
| 1028 | } |
| 1029 | EMIT(NFA_NOPEN); |
| 1030 | break; |
| 1031 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1032 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 1033 | case Magic('1'): |
| 1034 | case Magic('2'): |
| 1035 | case Magic('3'): |
| 1036 | case Magic('4'): |
| 1037 | case Magic('5'): |
| 1038 | case Magic('6'): |
| 1039 | case Magic('7'): |
| 1040 | case Magic('8'): |
| 1041 | case Magic('9'): |
| 1042 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 1043 | nfa_has_backref = TRUE; |
| 1044 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1045 | |
| 1046 | case Magic('z'): |
| 1047 | c = no_Magic(getchr()); |
| 1048 | switch (c) |
| 1049 | { |
| 1050 | case 's': |
| 1051 | EMIT(NFA_ZSTART); |
| 1052 | break; |
| 1053 | case 'e': |
| 1054 | EMIT(NFA_ZEND); |
| 1055 | nfa_has_zend = TRUE; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1056 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1057 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1058 | case '1': |
| 1059 | case '2': |
| 1060 | case '3': |
| 1061 | case '4': |
| 1062 | case '5': |
| 1063 | case '6': |
| 1064 | case '7': |
| 1065 | case '8': |
| 1066 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1067 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1068 | if (reg_do_extmatch != REX_USE) |
| 1069 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1070 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1071 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1072 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1073 | re_has_z = REX_USE; |
| 1074 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1075 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1076 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1077 | if (reg_do_extmatch != REX_SET) |
| 1078 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1079 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1080 | return FAIL; /* cascaded error */ |
| 1081 | re_has_z = REX_SET; |
| 1082 | break; |
| 1083 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1084 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1085 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1086 | no_Magic(c)); |
| 1087 | return FAIL; |
| 1088 | } |
| 1089 | break; |
| 1090 | |
| 1091 | case Magic('%'): |
| 1092 | c = no_Magic(getchr()); |
| 1093 | switch (c) |
| 1094 | { |
| 1095 | /* () without a back reference */ |
| 1096 | case '(': |
| 1097 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1098 | return FAIL; |
| 1099 | EMIT(NFA_NOPEN); |
| 1100 | break; |
| 1101 | |
| 1102 | case 'd': /* %d123 decimal */ |
| 1103 | case 'o': /* %o123 octal */ |
| 1104 | case 'x': /* %xab hex 2 */ |
| 1105 | case 'u': /* %uabcd hex 4 */ |
| 1106 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1107 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1108 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1109 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1110 | switch (c) |
| 1111 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1112 | case 'd': nr = getdecchrs(); break; |
| 1113 | case 'o': nr = getoctchrs(); break; |
| 1114 | case 'x': nr = gethexchrs(2); break; |
| 1115 | case 'u': nr = gethexchrs(4); break; |
| 1116 | case 'U': nr = gethexchrs(8); break; |
| 1117 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1118 | } |
| 1119 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1120 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1121 | EMSG2_RET_FAIL( |
| 1122 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1123 | reg_magic == MAGIC_ALL); |
| 1124 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1125 | EMIT(nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1126 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1127 | break; |
| 1128 | |
| 1129 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1130 | * pattern -- regardless of whether or not it makes sense. */ |
| 1131 | case '^': |
| 1132 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1133 | break; |
| 1134 | |
| 1135 | case '$': |
| 1136 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1137 | break; |
| 1138 | |
| 1139 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1140 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1141 | break; |
| 1142 | |
| 1143 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1144 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1145 | break; |
| 1146 | |
| 1147 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1148 | { |
| 1149 | int n; |
| 1150 | |
| 1151 | /* \%[abc] */ |
| 1152 | for (n = 0; (c = getchr()) != ']'; ++n) |
| 1153 | { |
| 1154 | if (c == NUL) |
| 1155 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1156 | reg_magic == MAGIC_ALL); |
| 1157 | EMIT(c); |
| 1158 | } |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1159 | if (n == 0) |
| 1160 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1161 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1162 | EMIT(NFA_OPT_CHARS); |
| 1163 | EMIT(n); |
| 1164 | break; |
| 1165 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1166 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1167 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1168 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1169 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1170 | int cmp = c; |
| 1171 | |
| 1172 | if (c == '<' || c == '>') |
| 1173 | c = getchr(); |
| 1174 | while (VIM_ISDIGIT(c)) |
| 1175 | { |
| 1176 | n = n * 10 + (c - '0'); |
| 1177 | c = getchr(); |
| 1178 | } |
| 1179 | if (c == 'l' || c == 'c' || c == 'v') |
| 1180 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1181 | if (c == 'l') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1182 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1183 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1184 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1185 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1186 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1187 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1188 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1189 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1190 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1191 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1192 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1193 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1194 | break; |
| 1195 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1196 | else if (c == '\'' && n == 0) |
| 1197 | { |
| 1198 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1199 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1200 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1201 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1202 | break; |
| 1203 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1204 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1205 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1206 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1207 | return FAIL; |
| 1208 | } |
| 1209 | break; |
| 1210 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1211 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1212 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1213 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1214 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1215 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1216 | * Each character is produced as a regular state, using |
| 1217 | * NFA_CONCAT to bind them together. |
| 1218 | * Besides normal characters there can be: |
| 1219 | * - character classes NFA_CLASS_* |
| 1220 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1221 | */ |
| 1222 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1223 | p = regparse; |
| 1224 | endp = skip_anyof(p); |
| 1225 | if (*endp == ']') |
| 1226 | { |
| 1227 | /* |
| 1228 | * Try to reverse engineer character classes. For example, |
| 1229 | * recognize that [0-9] stands for \d and [A-Za-z_] with \h, |
| 1230 | * and perform the necessary substitutions in the NFA. |
| 1231 | */ |
| 1232 | result = nfa_recognize_char_class(regparse, endp, |
| 1233 | extra == ADD_NL); |
| 1234 | if (result != FAIL) |
| 1235 | { |
| 1236 | if (result >= NFA_DIGIT && result <= NFA_NUPPER) |
| 1237 | EMIT(result); |
| 1238 | else /* must be char class + newline */ |
| 1239 | { |
| 1240 | EMIT(result - ADD_NL); |
| 1241 | EMIT(NFA_NEWL); |
| 1242 | EMIT(NFA_OR); |
| 1243 | } |
| 1244 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1245 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1246 | return OK; |
| 1247 | } |
| 1248 | /* |
| 1249 | * Failed to recognize a character class. Use the simple |
| 1250 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1251 | */ |
| 1252 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1253 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1254 | if (*regparse == '^') /* negated range */ |
| 1255 | { |
| 1256 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1257 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1258 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1259 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1260 | else |
| 1261 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1262 | if (*regparse == '-') |
| 1263 | { |
| 1264 | startc = '-'; |
| 1265 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1266 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1267 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1268 | } |
| 1269 | /* Emit the OR branches for each character in the [] */ |
| 1270 | emit_range = FALSE; |
| 1271 | while (regparse < endp) |
| 1272 | { |
| 1273 | oldstartc = startc; |
| 1274 | startc = -1; |
| 1275 | got_coll_char = FALSE; |
| 1276 | if (*regparse == '[') |
| 1277 | { |
| 1278 | /* Check for [: :], [= =], [. .] */ |
| 1279 | equiclass = collclass = 0; |
| 1280 | charclass = get_char_class(®parse); |
| 1281 | if (charclass == CLASS_NONE) |
| 1282 | { |
| 1283 | equiclass = get_equi_class(®parse); |
| 1284 | if (equiclass == 0) |
| 1285 | collclass = get_coll_element(®parse); |
| 1286 | } |
| 1287 | |
| 1288 | /* Character class like [:alpha:] */ |
| 1289 | if (charclass != CLASS_NONE) |
| 1290 | { |
| 1291 | switch (charclass) |
| 1292 | { |
| 1293 | case CLASS_ALNUM: |
| 1294 | EMIT(NFA_CLASS_ALNUM); |
| 1295 | break; |
| 1296 | case CLASS_ALPHA: |
| 1297 | EMIT(NFA_CLASS_ALPHA); |
| 1298 | break; |
| 1299 | case CLASS_BLANK: |
| 1300 | EMIT(NFA_CLASS_BLANK); |
| 1301 | break; |
| 1302 | case CLASS_CNTRL: |
| 1303 | EMIT(NFA_CLASS_CNTRL); |
| 1304 | break; |
| 1305 | case CLASS_DIGIT: |
| 1306 | EMIT(NFA_CLASS_DIGIT); |
| 1307 | break; |
| 1308 | case CLASS_GRAPH: |
| 1309 | EMIT(NFA_CLASS_GRAPH); |
| 1310 | break; |
| 1311 | case CLASS_LOWER: |
| 1312 | EMIT(NFA_CLASS_LOWER); |
| 1313 | break; |
| 1314 | case CLASS_PRINT: |
| 1315 | EMIT(NFA_CLASS_PRINT); |
| 1316 | break; |
| 1317 | case CLASS_PUNCT: |
| 1318 | EMIT(NFA_CLASS_PUNCT); |
| 1319 | break; |
| 1320 | case CLASS_SPACE: |
| 1321 | EMIT(NFA_CLASS_SPACE); |
| 1322 | break; |
| 1323 | case CLASS_UPPER: |
| 1324 | EMIT(NFA_CLASS_UPPER); |
| 1325 | break; |
| 1326 | case CLASS_XDIGIT: |
| 1327 | EMIT(NFA_CLASS_XDIGIT); |
| 1328 | break; |
| 1329 | case CLASS_TAB: |
| 1330 | EMIT(NFA_CLASS_TAB); |
| 1331 | break; |
| 1332 | case CLASS_RETURN: |
| 1333 | EMIT(NFA_CLASS_RETURN); |
| 1334 | break; |
| 1335 | case CLASS_BACKSPACE: |
| 1336 | EMIT(NFA_CLASS_BACKSPACE); |
| 1337 | break; |
| 1338 | case CLASS_ESCAPE: |
| 1339 | EMIT(NFA_CLASS_ESCAPE); |
| 1340 | break; |
| 1341 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1342 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1343 | continue; |
| 1344 | } |
| 1345 | /* Try equivalence class [=a=] and the like */ |
| 1346 | if (equiclass != 0) |
| 1347 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1348 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1349 | if (result == FAIL) |
| 1350 | { |
| 1351 | /* should never happen */ |
| 1352 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1353 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1354 | continue; |
| 1355 | } |
| 1356 | /* Try collating class like [. .] */ |
| 1357 | if (collclass != 0) |
| 1358 | { |
| 1359 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1360 | /* Will emit the proper atom at the end of the |
| 1361 | * while loop. */ |
| 1362 | } |
| 1363 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1364 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1365 | * start character. */ |
| 1366 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1367 | { |
| 1368 | emit_range = TRUE; |
| 1369 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1370 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1371 | continue; /* reading the end of the range */ |
| 1372 | } |
| 1373 | |
| 1374 | /* Now handle simple and escaped characters. |
| 1375 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1376 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1377 | * 'cpoptions' is not included. |
| 1378 | * Posix doesn't recognize backslash at all. |
| 1379 | */ |
| 1380 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1381 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1382 | && regparse + 1 <= endp |
| 1383 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1384 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1385 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1386 | != NULL) |
| 1387 | ) |
| 1388 | ) |
| 1389 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1390 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1391 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1392 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1393 | startc = reg_string ? NL : NFA_NEWL; |
| 1394 | else |
| 1395 | if (*regparse == 'd' |
| 1396 | || *regparse == 'o' |
| 1397 | || *regparse == 'x' |
| 1398 | || *regparse == 'u' |
| 1399 | || *regparse == 'U' |
| 1400 | ) |
| 1401 | { |
| 1402 | /* TODO(RE) This needs more testing */ |
| 1403 | startc = coll_get_char(); |
| 1404 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1405 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1406 | } |
| 1407 | else |
| 1408 | { |
| 1409 | /* \r,\t,\e,\b */ |
| 1410 | startc = backslash_trans(*regparse); |
| 1411 | } |
| 1412 | } |
| 1413 | |
| 1414 | /* Normal printable char */ |
| 1415 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1416 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1417 | |
| 1418 | /* Previous char was '-', so this char is end of range. */ |
| 1419 | if (emit_range) |
| 1420 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1421 | endc = startc; |
| 1422 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1423 | if (startc > endc) |
| 1424 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1425 | |
| 1426 | if (endc > startc + 2) |
| 1427 | { |
| 1428 | /* Emit a range instead of the sequence of |
| 1429 | * individual characters. */ |
| 1430 | if (startc == 0) |
| 1431 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1432 | EMIT(1); |
| 1433 | else |
| 1434 | --post_ptr; /* remove NFA_CONCAT */ |
| 1435 | EMIT(endc); |
| 1436 | EMIT(NFA_RANGE); |
| 1437 | EMIT(NFA_CONCAT); |
| 1438 | } |
| 1439 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1440 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1441 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1442 | || (*mb_char2len)(endc) > 1)) |
| 1443 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1444 | /* Emit the characters in the range. |
| 1445 | * "startc" was already emitted, so skip it. |
| 1446 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1447 | for (c = startc + 1; c <= endc; c++) |
| 1448 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1449 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1450 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1451 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1452 | } |
| 1453 | else |
| 1454 | #endif |
| 1455 | { |
| 1456 | #ifdef EBCDIC |
| 1457 | int alpha_only = FALSE; |
| 1458 | |
| 1459 | /* for alphabetical range skip the gaps |
| 1460 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1461 | if (isalpha(startc) && isalpha(endc)) |
| 1462 | alpha_only = TRUE; |
| 1463 | #endif |
| 1464 | /* Emit the range. "startc" was already emitted, so |
| 1465 | * skip it. */ |
| 1466 | for (c = startc + 1; c <= endc; c++) |
| 1467 | #ifdef EBCDIC |
| 1468 | if (!alpha_only || isalpha(startc)) |
| 1469 | #endif |
| 1470 | { |
| 1471 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1472 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1473 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1474 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1475 | emit_range = FALSE; |
| 1476 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1477 | } |
| 1478 | else |
| 1479 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1480 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1481 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1482 | * Normally, simply emit startc. But if we get char |
| 1483 | * code=0 from a collating char, then replace it with |
| 1484 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1485 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1486 | * the backtracking engine. */ |
| 1487 | if (startc == NFA_NEWL) |
| 1488 | { |
| 1489 | /* Line break can't be matched as part of the |
| 1490 | * collection, add an OR below. But not for negated |
| 1491 | * range. */ |
| 1492 | if (!negated) |
| 1493 | extra = ADD_NL; |
| 1494 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1495 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1496 | { |
| 1497 | if (got_coll_char == TRUE && startc == 0) |
| 1498 | EMIT(0x0a); |
| 1499 | else |
| 1500 | EMIT(startc); |
| 1501 | EMIT(NFA_CONCAT); |
| 1502 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1503 | } |
| 1504 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1505 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1506 | } /* while (p < endp) */ |
| 1507 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1508 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1509 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1510 | { |
| 1511 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1512 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1513 | } |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1514 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1515 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1516 | /* skip the trailing ] */ |
| 1517 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1518 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1519 | |
| 1520 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1521 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1522 | EMIT(NFA_END_NEG_COLL); |
| 1523 | else |
| 1524 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1525 | |
| 1526 | /* \_[] also matches \n but it's not negated */ |
| 1527 | if (extra == ADD_NL) |
| 1528 | { |
| 1529 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1530 | EMIT(NFA_OR); |
| 1531 | } |
| 1532 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1533 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1534 | } /* if exists closing ] */ |
| 1535 | |
| 1536 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1537 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1538 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1539 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1540 | default: |
| 1541 | { |
| 1542 | #ifdef FEAT_MBYTE |
| 1543 | int plen; |
| 1544 | |
| 1545 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1546 | /* plen is length of current char with composing chars */ |
| 1547 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1548 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1549 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1550 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1551 | int i = 0; |
| 1552 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1553 | /* A base character plus composing characters, or just one |
| 1554 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1555 | * This requires creating a separate atom as if enclosing |
| 1556 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1557 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1558 | * building the postfix form, not the NFA itself; |
| 1559 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1560 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1561 | for (;;) |
| 1562 | { |
| 1563 | EMIT(c); |
| 1564 | if (i > 0) |
| 1565 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1566 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1567 | break; |
| 1568 | c = utf_ptr2char(old_regparse + i); |
| 1569 | } |
| 1570 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1571 | regparse = old_regparse + plen; |
| 1572 | } |
| 1573 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1574 | #endif |
| 1575 | { |
| 1576 | c = no_Magic(c); |
| 1577 | EMIT(c); |
| 1578 | } |
| 1579 | return OK; |
| 1580 | } |
| 1581 | } |
| 1582 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1583 | return OK; |
| 1584 | } |
| 1585 | |
| 1586 | /* |
| 1587 | * Parse something followed by possible [*+=]. |
| 1588 | * |
| 1589 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1590 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1591 | * characters: "", "a", "aa", etc. |
| 1592 | * |
| 1593 | * piece ::= atom |
| 1594 | * or atom multi |
| 1595 | */ |
| 1596 | static int |
| 1597 | nfa_regpiece() |
| 1598 | { |
| 1599 | int i; |
| 1600 | int op; |
| 1601 | int ret; |
| 1602 | long minval, maxval; |
| 1603 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1604 | parse_state_T old_state; |
| 1605 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1606 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1607 | int old_post_pos; |
| 1608 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1609 | int quest; |
| 1610 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1611 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1612 | * next. */ |
| 1613 | save_parse_state(&old_state); |
| 1614 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1615 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1616 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1617 | |
| 1618 | ret = nfa_regatom(); |
| 1619 | if (ret == FAIL) |
| 1620 | return FAIL; /* cascaded error */ |
| 1621 | |
| 1622 | op = peekchr(); |
| 1623 | if (re_multi_type(op) == NOT_MULTI) |
| 1624 | return OK; |
| 1625 | |
| 1626 | skipchr(); |
| 1627 | switch (op) |
| 1628 | { |
| 1629 | case Magic('*'): |
| 1630 | EMIT(NFA_STAR); |
| 1631 | break; |
| 1632 | |
| 1633 | case Magic('+'): |
| 1634 | /* |
| 1635 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1636 | * first and only submatch would be "aaa". But the backtracking |
| 1637 | * engine interprets the plus as "try matching one more time", and |
| 1638 | * a* matches a second time at the end of the input, the empty |
| 1639 | * string. |
| 1640 | * The submatch will the empty string. |
| 1641 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1642 | * In order to be consistent with the old engine, we replace |
| 1643 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1644 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1645 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1646 | curchr = -1; |
| 1647 | if (nfa_regatom() == FAIL) |
| 1648 | return FAIL; |
| 1649 | EMIT(NFA_STAR); |
| 1650 | EMIT(NFA_CONCAT); |
| 1651 | skipchr(); /* skip the \+ */ |
| 1652 | break; |
| 1653 | |
| 1654 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1655 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1656 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1657 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1658 | switch(op) |
| 1659 | { |
| 1660 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1661 | /* \@= */ |
| 1662 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1663 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1664 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1665 | /* \@! */ |
| 1666 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1667 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1668 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1669 | op = no_Magic(getchr()); |
| 1670 | if (op == '=') |
| 1671 | /* \@<= */ |
| 1672 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1673 | else if (op == '!') |
| 1674 | /* \@<! */ |
| 1675 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1676 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1677 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1678 | /* \@> */ |
| 1679 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1680 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1681 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1682 | if (i == 0) |
| 1683 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1684 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1685 | return FAIL; |
| 1686 | } |
| 1687 | EMIT(i); |
| 1688 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1689 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1690 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1691 | break; |
| 1692 | |
| 1693 | case Magic('?'): |
| 1694 | case Magic('='): |
| 1695 | EMIT(NFA_QUEST); |
| 1696 | break; |
| 1697 | |
| 1698 | case Magic('{'): |
| 1699 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1700 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1701 | * version of '?' |
| 1702 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1703 | * parenthesis have the same id |
| 1704 | */ |
| 1705 | |
| 1706 | greedy = TRUE; |
| 1707 | c2 = peekchr(); |
| 1708 | if (c2 == '-' || c2 == Magic('-')) |
| 1709 | { |
| 1710 | skipchr(); |
| 1711 | greedy = FALSE; |
| 1712 | } |
| 1713 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1714 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 1715 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1716 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 1717 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1718 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1719 | { |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1720 | if (greedy) |
| 1721 | /* \{}, \{0,} */ |
| 1722 | EMIT(NFA_STAR); |
| 1723 | else |
| 1724 | /* \{-}, \{-0,} */ |
| 1725 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1726 | break; |
| 1727 | } |
| 1728 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1729 | /* Special case: x{0} or x{-0} */ |
| 1730 | if (maxval == 0) |
| 1731 | { |
| 1732 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1733 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1734 | /* NFA_SKIP_CHAR has 0-length and works everywhere */ |
| 1735 | EMIT(NFA_SKIP_CHAR); |
| 1736 | return OK; |
| 1737 | } |
| 1738 | |
| 1739 | /* Ignore previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1740 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1741 | /* Save parse state after the repeated atom and the \{} */ |
| 1742 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1743 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1744 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 1745 | for (i = 0; i < maxval; i++) |
| 1746 | { |
| 1747 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1748 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1749 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1750 | if (nfa_regatom() == FAIL) |
| 1751 | return FAIL; |
| 1752 | /* after "minval" times, atoms are optional */ |
| 1753 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1754 | { |
| 1755 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1756 | { |
| 1757 | if (greedy) |
| 1758 | EMIT(NFA_STAR); |
| 1759 | else |
| 1760 | EMIT(NFA_STAR_NONGREEDY); |
| 1761 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1762 | else |
| 1763 | EMIT(quest); |
| 1764 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1765 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1766 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1767 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 1768 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1769 | } |
| 1770 | |
| 1771 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1772 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1773 | curchr = -1; |
| 1774 | |
| 1775 | break; |
| 1776 | |
| 1777 | |
| 1778 | default: |
| 1779 | break; |
| 1780 | } /* end switch */ |
| 1781 | |
| 1782 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1783 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1784 | 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] | 1785 | |
| 1786 | return OK; |
| 1787 | } |
| 1788 | |
| 1789 | /* |
| 1790 | * Parse one or more pieces, concatenated. It matches a match for the |
| 1791 | * first piece, followed by a match for the second piece, etc. Example: |
| 1792 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 1793 | * |
| 1794 | * concat ::= piece |
| 1795 | * or piece piece |
| 1796 | * or piece piece piece |
| 1797 | * etc. |
| 1798 | */ |
| 1799 | static int |
| 1800 | nfa_regconcat() |
| 1801 | { |
| 1802 | int cont = TRUE; |
| 1803 | int first = TRUE; |
| 1804 | |
| 1805 | while (cont) |
| 1806 | { |
| 1807 | switch (peekchr()) |
| 1808 | { |
| 1809 | case NUL: |
| 1810 | case Magic('|'): |
| 1811 | case Magic('&'): |
| 1812 | case Magic(')'): |
| 1813 | cont = FALSE; |
| 1814 | break; |
| 1815 | |
| 1816 | case Magic('Z'): |
| 1817 | #ifdef FEAT_MBYTE |
| 1818 | regflags |= RF_ICOMBINE; |
| 1819 | #endif |
| 1820 | skipchr_keepstart(); |
| 1821 | break; |
| 1822 | case Magic('c'): |
| 1823 | regflags |= RF_ICASE; |
| 1824 | skipchr_keepstart(); |
| 1825 | break; |
| 1826 | case Magic('C'): |
| 1827 | regflags |= RF_NOICASE; |
| 1828 | skipchr_keepstart(); |
| 1829 | break; |
| 1830 | case Magic('v'): |
| 1831 | reg_magic = MAGIC_ALL; |
| 1832 | skipchr_keepstart(); |
| 1833 | curchr = -1; |
| 1834 | break; |
| 1835 | case Magic('m'): |
| 1836 | reg_magic = MAGIC_ON; |
| 1837 | skipchr_keepstart(); |
| 1838 | curchr = -1; |
| 1839 | break; |
| 1840 | case Magic('M'): |
| 1841 | reg_magic = MAGIC_OFF; |
| 1842 | skipchr_keepstart(); |
| 1843 | curchr = -1; |
| 1844 | break; |
| 1845 | case Magic('V'): |
| 1846 | reg_magic = MAGIC_NONE; |
| 1847 | skipchr_keepstart(); |
| 1848 | curchr = -1; |
| 1849 | break; |
| 1850 | |
| 1851 | default: |
| 1852 | if (nfa_regpiece() == FAIL) |
| 1853 | return FAIL; |
| 1854 | if (first == FALSE) |
| 1855 | EMIT(NFA_CONCAT); |
| 1856 | else |
| 1857 | first = FALSE; |
| 1858 | break; |
| 1859 | } |
| 1860 | } |
| 1861 | |
| 1862 | return OK; |
| 1863 | } |
| 1864 | |
| 1865 | /* |
| 1866 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 1867 | * last concat, but only if all the preceding concats also match at the same |
| 1868 | * position. Examples: |
| 1869 | * "foobeep\&..." matches "foo" in "foobeep". |
| 1870 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 1871 | * |
| 1872 | * branch ::= concat |
| 1873 | * or concat \& concat |
| 1874 | * or concat \& concat \& concat |
| 1875 | * etc. |
| 1876 | */ |
| 1877 | static int |
| 1878 | nfa_regbranch() |
| 1879 | { |
| 1880 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1881 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1882 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1883 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1884 | |
| 1885 | /* First branch, possibly the only one */ |
| 1886 | if (nfa_regconcat() == FAIL) |
| 1887 | return FAIL; |
| 1888 | |
| 1889 | ch = peekchr(); |
| 1890 | /* Try next concats */ |
| 1891 | while (ch == Magic('&')) |
| 1892 | { |
| 1893 | skipchr(); |
| 1894 | EMIT(NFA_NOPEN); |
| 1895 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1896 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1897 | if (nfa_regconcat() == FAIL) |
| 1898 | return FAIL; |
| 1899 | /* 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] | 1900 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1901 | EMIT(NFA_SKIP_CHAR); |
| 1902 | EMIT(NFA_CONCAT); |
| 1903 | ch = peekchr(); |
| 1904 | } |
| 1905 | |
| 1906 | /* Even if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1907 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1908 | EMIT(NFA_SKIP_CHAR); |
| 1909 | |
| 1910 | return OK; |
| 1911 | } |
| 1912 | |
| 1913 | /* |
| 1914 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 1915 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 1916 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 1917 | * is used. |
| 1918 | * |
| 1919 | * pattern ::= branch |
| 1920 | * or branch \| branch |
| 1921 | * or branch \| branch \| branch |
| 1922 | * etc. |
| 1923 | */ |
| 1924 | static int |
| 1925 | nfa_reg(paren) |
| 1926 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 1927 | { |
| 1928 | int parno = 0; |
| 1929 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1930 | if (paren == REG_PAREN) |
| 1931 | { |
| 1932 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1933 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1934 | parno = regnpar++; |
| 1935 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1936 | #ifdef FEAT_SYN_HL |
| 1937 | else if (paren == REG_ZPAREN) |
| 1938 | { |
| 1939 | /* Make a ZOPEN node. */ |
| 1940 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1941 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1942 | parno = regnzpar++; |
| 1943 | } |
| 1944 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1945 | |
| 1946 | if (nfa_regbranch() == FAIL) |
| 1947 | return FAIL; /* cascaded error */ |
| 1948 | |
| 1949 | while (peekchr() == Magic('|')) |
| 1950 | { |
| 1951 | skipchr(); |
| 1952 | if (nfa_regbranch() == FAIL) |
| 1953 | return FAIL; /* cascaded error */ |
| 1954 | EMIT(NFA_OR); |
| 1955 | } |
| 1956 | |
| 1957 | /* Check for proper termination. */ |
| 1958 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 1959 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1960 | if (paren == REG_NPAREN) |
| 1961 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 1962 | else |
| 1963 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 1964 | } |
| 1965 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 1966 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1967 | if (peekchr() == Magic(')')) |
| 1968 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 1969 | else |
| 1970 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 1971 | } |
| 1972 | /* |
| 1973 | * Here we set the flag allowing back references to this set of |
| 1974 | * parentheses. |
| 1975 | */ |
| 1976 | if (paren == REG_PAREN) |
| 1977 | { |
| 1978 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 1979 | EMIT(NFA_MOPEN + parno); |
| 1980 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1981 | #ifdef FEAT_SYN_HL |
| 1982 | else if (paren == REG_ZPAREN) |
| 1983 | EMIT(NFA_ZOPEN + parno); |
| 1984 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1985 | |
| 1986 | return OK; |
| 1987 | } |
| 1988 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1989 | #ifdef DEBUG |
| 1990 | static char_u code[50]; |
| 1991 | |
| 1992 | static void |
| 1993 | nfa_set_code(c) |
| 1994 | int c; |
| 1995 | { |
| 1996 | int addnl = FALSE; |
| 1997 | |
| 1998 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 1999 | { |
| 2000 | addnl = TRUE; |
| 2001 | c -= ADD_NL; |
| 2002 | } |
| 2003 | |
| 2004 | STRCPY(code, ""); |
| 2005 | switch (c) |
| 2006 | { |
| 2007 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 2008 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 2009 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 2010 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 2011 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 2012 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 2013 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2014 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 2015 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 2016 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 2017 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 2018 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 2019 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 2020 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 2021 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 2022 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2023 | #ifdef FEAT_SYN_HL |
| 2024 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 2025 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 2026 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 2027 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 2028 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 2029 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 2030 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 2031 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 2032 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 2033 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2034 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 2035 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2036 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2037 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2038 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 2039 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2040 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2041 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 2042 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 2043 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2044 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 2045 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 2046 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2047 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 2048 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2049 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2050 | case NFA_START_INVISIBLE_FIRST: |
| 2051 | STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2052 | case NFA_START_INVISIBLE_NEG: |
| 2053 | STRCPY(code, "NFA_START_INVISIBLE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2054 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 2055 | STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2056 | case NFA_START_INVISIBLE_BEFORE: |
| 2057 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2058 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 2059 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2060 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2061 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2062 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 2063 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2064 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2065 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2066 | case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2067 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2068 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2069 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2070 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2071 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2072 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2073 | case NFA_MOPEN: |
| 2074 | case NFA_MOPEN1: |
| 2075 | case NFA_MOPEN2: |
| 2076 | case NFA_MOPEN3: |
| 2077 | case NFA_MOPEN4: |
| 2078 | case NFA_MOPEN5: |
| 2079 | case NFA_MOPEN6: |
| 2080 | case NFA_MOPEN7: |
| 2081 | case NFA_MOPEN8: |
| 2082 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2083 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2084 | code[10] = c - NFA_MOPEN + '0'; |
| 2085 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2086 | case NFA_MCLOSE: |
| 2087 | case NFA_MCLOSE1: |
| 2088 | case NFA_MCLOSE2: |
| 2089 | case NFA_MCLOSE3: |
| 2090 | case NFA_MCLOSE4: |
| 2091 | case NFA_MCLOSE5: |
| 2092 | case NFA_MCLOSE6: |
| 2093 | case NFA_MCLOSE7: |
| 2094 | case NFA_MCLOSE8: |
| 2095 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2096 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2097 | code[11] = c - NFA_MCLOSE + '0'; |
| 2098 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2099 | #ifdef FEAT_SYN_HL |
| 2100 | case NFA_ZOPEN: |
| 2101 | case NFA_ZOPEN1: |
| 2102 | case NFA_ZOPEN2: |
| 2103 | case NFA_ZOPEN3: |
| 2104 | case NFA_ZOPEN4: |
| 2105 | case NFA_ZOPEN5: |
| 2106 | case NFA_ZOPEN6: |
| 2107 | case NFA_ZOPEN7: |
| 2108 | case NFA_ZOPEN8: |
| 2109 | case NFA_ZOPEN9: |
| 2110 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2111 | code[10] = c - NFA_ZOPEN + '0'; |
| 2112 | break; |
| 2113 | case NFA_ZCLOSE: |
| 2114 | case NFA_ZCLOSE1: |
| 2115 | case NFA_ZCLOSE2: |
| 2116 | case NFA_ZCLOSE3: |
| 2117 | case NFA_ZCLOSE4: |
| 2118 | case NFA_ZCLOSE5: |
| 2119 | case NFA_ZCLOSE6: |
| 2120 | case NFA_ZCLOSE7: |
| 2121 | case NFA_ZCLOSE8: |
| 2122 | case NFA_ZCLOSE9: |
| 2123 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2124 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2125 | break; |
| 2126 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2127 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2128 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2129 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2130 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2131 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2132 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2133 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2134 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2135 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2136 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2137 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2138 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2139 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2140 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2141 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2142 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2143 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2144 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2145 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2146 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
| 2147 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2148 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2149 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2150 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2151 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2152 | case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; |
| 2153 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2154 | |
| 2155 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2156 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2157 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2158 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2159 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2160 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2161 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2162 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2163 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2164 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2165 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2166 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2167 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2168 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2169 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2170 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2171 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2172 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2173 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2174 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2175 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2176 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2177 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2178 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2179 | |
| 2180 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2181 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2182 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2183 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2184 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2185 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2186 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2187 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2188 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2189 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2190 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2191 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2192 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2193 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2194 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2195 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2196 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2197 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2198 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2199 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2200 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2201 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2202 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2203 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2204 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2205 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2206 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
| 2207 | |
| 2208 | default: |
| 2209 | STRCPY(code, "CHAR(x)"); |
| 2210 | code[5] = c; |
| 2211 | } |
| 2212 | |
| 2213 | if (addnl == TRUE) |
| 2214 | STRCAT(code, " + NEWLINE "); |
| 2215 | |
| 2216 | } |
| 2217 | |
| 2218 | #ifdef ENABLE_LOG |
| 2219 | static FILE *log_fd; |
| 2220 | |
| 2221 | /* |
| 2222 | * Print the postfix notation of the current regexp. |
| 2223 | */ |
| 2224 | static void |
| 2225 | nfa_postfix_dump(expr, retval) |
| 2226 | char_u *expr; |
| 2227 | int retval; |
| 2228 | { |
| 2229 | int *p; |
| 2230 | FILE *f; |
| 2231 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2232 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2233 | if (f != NULL) |
| 2234 | { |
| 2235 | fprintf(f, "\n-------------------------\n"); |
| 2236 | if (retval == FAIL) |
| 2237 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2238 | else if (retval == OK) |
| 2239 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2240 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2241 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2242 | { |
| 2243 | nfa_set_code(*p); |
| 2244 | fprintf(f, "%s, ", code); |
| 2245 | } |
| 2246 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 2247 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2248 | fprintf(f, "%d ", *p); |
| 2249 | fprintf(f, "\n\n"); |
| 2250 | fclose(f); |
| 2251 | } |
| 2252 | } |
| 2253 | |
| 2254 | /* |
| 2255 | * Print the NFA starting with a root node "state". |
| 2256 | */ |
| 2257 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2258 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2259 | FILE *debugf; |
| 2260 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2261 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2262 | garray_T indent; |
| 2263 | |
| 2264 | ga_init2(&indent, 1, 64); |
| 2265 | ga_append(&indent, '\0'); |
| 2266 | nfa_print_state2(debugf, state, &indent); |
| 2267 | ga_clear(&indent); |
| 2268 | } |
| 2269 | |
| 2270 | static void |
| 2271 | nfa_print_state2(debugf, state, indent) |
| 2272 | FILE *debugf; |
| 2273 | nfa_state_T *state; |
| 2274 | garray_T *indent; |
| 2275 | { |
| 2276 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2277 | |
| 2278 | if (state == NULL) |
| 2279 | return; |
| 2280 | |
| 2281 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2282 | |
| 2283 | /* Output indent */ |
| 2284 | p = (char_u *)indent->ga_data; |
| 2285 | if (indent->ga_len >= 3) |
| 2286 | { |
| 2287 | int last = indent->ga_len - 3; |
| 2288 | char_u save[2]; |
| 2289 | |
| 2290 | STRNCPY(save, &p[last], 2); |
| 2291 | STRNCPY(&p[last], "+-", 2); |
| 2292 | fprintf(debugf, " %s", p); |
| 2293 | STRNCPY(&p[last], save, 2); |
| 2294 | } |
| 2295 | else |
| 2296 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2297 | |
| 2298 | nfa_set_code(state->c); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2299 | fprintf(debugf, "%s (%d) (id=%d) val=%d\n", |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2300 | code, |
| 2301 | state->c, |
| 2302 | abs(state->id), |
| 2303 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2304 | if (state->id < 0) |
| 2305 | return; |
| 2306 | |
| 2307 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2308 | |
| 2309 | /* grow indent for state->out */ |
| 2310 | indent->ga_len -= 1; |
| 2311 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2312 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2313 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2314 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2315 | ga_append(indent, '\0'); |
| 2316 | |
| 2317 | nfa_print_state2(debugf, state->out, indent); |
| 2318 | |
| 2319 | /* replace last part of indent for state->out1 */ |
| 2320 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2321 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2322 | ga_append(indent, '\0'); |
| 2323 | |
| 2324 | nfa_print_state2(debugf, state->out1, indent); |
| 2325 | |
| 2326 | /* shrink indent */ |
| 2327 | indent->ga_len -= 3; |
| 2328 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2329 | } |
| 2330 | |
| 2331 | /* |
| 2332 | * Print the NFA state machine. |
| 2333 | */ |
| 2334 | static void |
| 2335 | nfa_dump(prog) |
| 2336 | nfa_regprog_T *prog; |
| 2337 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2338 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2339 | |
| 2340 | if (debugf != NULL) |
| 2341 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2342 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2343 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 2344 | if (prog->reganch) |
| 2345 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2346 | if (prog->regstart != NUL) |
| 2347 | fprintf(debugf, "regstart: %c (decimal: %d)\n", |
| 2348 | prog->regstart, prog->regstart); |
| 2349 | if (prog->match_text != NULL) |
| 2350 | fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2351 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2352 | fclose(debugf); |
| 2353 | } |
| 2354 | } |
| 2355 | #endif /* ENABLE_LOG */ |
| 2356 | #endif /* DEBUG */ |
| 2357 | |
| 2358 | /* |
| 2359 | * Parse r.e. @expr and convert it into postfix form. |
| 2360 | * Return the postfix string on success, NULL otherwise. |
| 2361 | */ |
| 2362 | static int * |
| 2363 | re2post() |
| 2364 | { |
| 2365 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2366 | return NULL; |
| 2367 | EMIT(NFA_MOPEN); |
| 2368 | return post_start; |
| 2369 | } |
| 2370 | |
| 2371 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2372 | |
| 2373 | /* |
| 2374 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2375 | * if c == MATCH, no arrows out; matching state. |
| 2376 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2377 | * If c < 256, labeled arrow with character c to out. |
| 2378 | */ |
| 2379 | |
| 2380 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2381 | |
| 2382 | /* |
| 2383 | * Allocate and initialize nfa_state_T. |
| 2384 | */ |
| 2385 | static nfa_state_T * |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2386 | alloc_state(c, out, out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2387 | int c; |
| 2388 | nfa_state_T *out; |
| 2389 | nfa_state_T *out1; |
| 2390 | { |
| 2391 | nfa_state_T *s; |
| 2392 | |
| 2393 | if (istate >= nstate) |
| 2394 | return NULL; |
| 2395 | |
| 2396 | s = &state_ptr[istate++]; |
| 2397 | |
| 2398 | s->c = c; |
| 2399 | s->out = out; |
| 2400 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2401 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2402 | |
| 2403 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2404 | s->lastlist[0] = 0; |
| 2405 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2406 | |
| 2407 | return s; |
| 2408 | } |
| 2409 | |
| 2410 | /* |
| 2411 | * A partially built NFA without the matching state filled in. |
| 2412 | * Frag_T.start points at the start state. |
| 2413 | * Frag_T.out is a list of places that need to be set to the |
| 2414 | * next state for this fragment. |
| 2415 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2416 | |
| 2417 | /* Since the out pointers in the list are always |
| 2418 | * uninitialized, we use the pointers themselves |
| 2419 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2420 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2421 | union Ptrlist |
| 2422 | { |
| 2423 | Ptrlist *next; |
| 2424 | nfa_state_T *s; |
| 2425 | }; |
| 2426 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2427 | struct Frag |
| 2428 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2429 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2430 | Ptrlist *out; |
| 2431 | }; |
| 2432 | typedef struct Frag Frag_T; |
| 2433 | |
| 2434 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2435 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2436 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2437 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2438 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2439 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2440 | |
| 2441 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2442 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2443 | */ |
| 2444 | static Frag_T |
| 2445 | frag(start, out) |
| 2446 | nfa_state_T *start; |
| 2447 | Ptrlist *out; |
| 2448 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2449 | Frag_T n; |
| 2450 | |
| 2451 | n.start = start; |
| 2452 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2453 | return n; |
| 2454 | } |
| 2455 | |
| 2456 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2457 | * Create singleton list containing just outp. |
| 2458 | */ |
| 2459 | static Ptrlist * |
| 2460 | list1(outp) |
| 2461 | nfa_state_T **outp; |
| 2462 | { |
| 2463 | Ptrlist *l; |
| 2464 | |
| 2465 | l = (Ptrlist *)outp; |
| 2466 | l->next = NULL; |
| 2467 | return l; |
| 2468 | } |
| 2469 | |
| 2470 | /* |
| 2471 | * Patch the list of states at out to point to start. |
| 2472 | */ |
| 2473 | static void |
| 2474 | patch(l, s) |
| 2475 | Ptrlist *l; |
| 2476 | nfa_state_T *s; |
| 2477 | { |
| 2478 | Ptrlist *next; |
| 2479 | |
| 2480 | for (; l; l = next) |
| 2481 | { |
| 2482 | next = l->next; |
| 2483 | l->s = s; |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | |
| 2488 | /* |
| 2489 | * Join the two lists l1 and l2, returning the combination. |
| 2490 | */ |
| 2491 | static Ptrlist * |
| 2492 | append(l1, l2) |
| 2493 | Ptrlist *l1; |
| 2494 | Ptrlist *l2; |
| 2495 | { |
| 2496 | Ptrlist *oldl1; |
| 2497 | |
| 2498 | oldl1 = l1; |
| 2499 | while (l1->next) |
| 2500 | l1 = l1->next; |
| 2501 | l1->next = l2; |
| 2502 | return oldl1; |
| 2503 | } |
| 2504 | |
| 2505 | /* |
| 2506 | * Stack used for transforming postfix form into NFA. |
| 2507 | */ |
| 2508 | static Frag_T empty; |
| 2509 | |
| 2510 | static void |
| 2511 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2512 | int *postfix UNUSED; |
| 2513 | int *end UNUSED; |
| 2514 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2515 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2516 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2517 | FILE *df; |
| 2518 | int *p2; |
| 2519 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2520 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2521 | if (df) |
| 2522 | { |
| 2523 | fprintf(df, "Error popping the stack!\n"); |
| 2524 | #ifdef DEBUG |
| 2525 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2526 | #endif |
| 2527 | fprintf(df, "Postfix form is: "); |
| 2528 | #ifdef DEBUG |
| 2529 | for (p2 = postfix; p2 < end; p2++) |
| 2530 | { |
| 2531 | nfa_set_code(*p2); |
| 2532 | fprintf(df, "%s, ", code); |
| 2533 | } |
| 2534 | nfa_set_code(*p); |
| 2535 | fprintf(df, "\nCurrent position is: "); |
| 2536 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2537 | { |
| 2538 | nfa_set_code(*p2); |
| 2539 | fprintf(df, "%s, ", code); |
| 2540 | } |
| 2541 | #else |
| 2542 | for (p2 = postfix; p2 < end; p2++) |
| 2543 | { |
| 2544 | fprintf(df, "%d, ", *p2); |
| 2545 | } |
| 2546 | fprintf(df, "\nCurrent position is: "); |
| 2547 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2548 | { |
| 2549 | fprintf(df, "%d, ", *p2); |
| 2550 | } |
| 2551 | #endif |
| 2552 | fprintf(df, "\n--------------------------\n"); |
| 2553 | fclose(df); |
| 2554 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2555 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2556 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2557 | } |
| 2558 | |
| 2559 | /* |
| 2560 | * Push an item onto the stack. |
| 2561 | */ |
| 2562 | static void |
| 2563 | st_push(s, p, stack_end) |
| 2564 | Frag_T s; |
| 2565 | Frag_T **p; |
| 2566 | Frag_T *stack_end; |
| 2567 | { |
| 2568 | Frag_T *stackp = *p; |
| 2569 | |
| 2570 | if (stackp >= stack_end) |
| 2571 | return; |
| 2572 | *stackp = s; |
| 2573 | *p = *p + 1; |
| 2574 | } |
| 2575 | |
| 2576 | /* |
| 2577 | * Pop an item from the stack. |
| 2578 | */ |
| 2579 | static Frag_T |
| 2580 | st_pop(p, stack) |
| 2581 | Frag_T **p; |
| 2582 | Frag_T *stack; |
| 2583 | { |
| 2584 | Frag_T *stackp; |
| 2585 | |
| 2586 | *p = *p - 1; |
| 2587 | stackp = *p; |
| 2588 | if (stackp < stack) |
| 2589 | return empty; |
| 2590 | return **p; |
| 2591 | } |
| 2592 | |
| 2593 | /* |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2594 | * Estimate the maximum byte length of anything matching "state". |
| 2595 | * When unknown or unlimited return -1. |
| 2596 | */ |
| 2597 | static int |
| 2598 | nfa_max_width(startstate, depth) |
| 2599 | nfa_state_T *startstate; |
| 2600 | int depth; |
| 2601 | { |
| 2602 | int l, r; |
| 2603 | nfa_state_T *state = startstate; |
| 2604 | int len = 0; |
| 2605 | |
| 2606 | /* detect looping in a NFA_SPLIT */ |
| 2607 | if (depth > 4) |
| 2608 | return -1; |
| 2609 | |
| 2610 | for (;;) |
| 2611 | { |
| 2612 | switch (state->c) |
| 2613 | { |
| 2614 | case NFA_END_INVISIBLE: |
| 2615 | case NFA_END_INVISIBLE_NEG: |
| 2616 | /* the end, return what we have */ |
| 2617 | return len; |
| 2618 | |
| 2619 | case NFA_SPLIT: |
| 2620 | /* two alternatives, use the maximum */ |
| 2621 | l = nfa_max_width(state->out, depth + 1); |
| 2622 | r = nfa_max_width(state->out1, depth + 1); |
| 2623 | if (l < 0 || r < 0) |
| 2624 | return -1; |
| 2625 | return len + (l > r ? l : r); |
| 2626 | |
| 2627 | case NFA_ANY: |
| 2628 | case NFA_START_COLL: |
| 2629 | case NFA_START_NEG_COLL: |
| 2630 | /* matches some character, including composing chars */ |
| 2631 | #ifdef FEAT_MBYTE |
| 2632 | if (enc_utf8) |
| 2633 | len += MB_MAXBYTES; |
| 2634 | else if (has_mbyte) |
| 2635 | len += 2; |
| 2636 | else |
| 2637 | #endif |
| 2638 | ++len; |
| 2639 | if (state->c != NFA_ANY) |
| 2640 | { |
| 2641 | /* skip over the characters */ |
| 2642 | state = state->out1->out; |
| 2643 | continue; |
| 2644 | } |
| 2645 | break; |
| 2646 | |
| 2647 | case NFA_DIGIT: |
| 2648 | case NFA_WHITE: |
| 2649 | case NFA_HEX: |
| 2650 | case NFA_OCTAL: |
| 2651 | /* ascii */ |
| 2652 | ++len; |
| 2653 | break; |
| 2654 | |
| 2655 | case NFA_IDENT: |
| 2656 | case NFA_SIDENT: |
| 2657 | case NFA_KWORD: |
| 2658 | case NFA_SKWORD: |
| 2659 | case NFA_FNAME: |
| 2660 | case NFA_SFNAME: |
| 2661 | case NFA_PRINT: |
| 2662 | case NFA_SPRINT: |
| 2663 | case NFA_NWHITE: |
| 2664 | case NFA_NDIGIT: |
| 2665 | case NFA_NHEX: |
| 2666 | case NFA_NOCTAL: |
| 2667 | case NFA_WORD: |
| 2668 | case NFA_NWORD: |
| 2669 | case NFA_HEAD: |
| 2670 | case NFA_NHEAD: |
| 2671 | case NFA_ALPHA: |
| 2672 | case NFA_NALPHA: |
| 2673 | case NFA_LOWER: |
| 2674 | case NFA_NLOWER: |
| 2675 | case NFA_UPPER: |
| 2676 | case NFA_NUPPER: |
| 2677 | /* possibly non-ascii */ |
| 2678 | #ifdef FEAT_MBYTE |
| 2679 | if (has_mbyte) |
| 2680 | len += 3; |
| 2681 | else |
| 2682 | #endif |
| 2683 | ++len; |
| 2684 | break; |
| 2685 | |
| 2686 | case NFA_START_INVISIBLE: |
| 2687 | case NFA_START_INVISIBLE_NEG: |
| 2688 | case NFA_START_INVISIBLE_BEFORE: |
| 2689 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2690 | /* zero-width, out1 points to the END state */ |
| 2691 | state = state->out1->out; |
| 2692 | continue; |
| 2693 | |
| 2694 | case NFA_BACKREF1: |
| 2695 | case NFA_BACKREF2: |
| 2696 | case NFA_BACKREF3: |
| 2697 | case NFA_BACKREF4: |
| 2698 | case NFA_BACKREF5: |
| 2699 | case NFA_BACKREF6: |
| 2700 | case NFA_BACKREF7: |
| 2701 | case NFA_BACKREF8: |
| 2702 | case NFA_BACKREF9: |
| 2703 | #ifdef FEAT_SYN_HL |
| 2704 | case NFA_ZREF1: |
| 2705 | case NFA_ZREF2: |
| 2706 | case NFA_ZREF3: |
| 2707 | case NFA_ZREF4: |
| 2708 | case NFA_ZREF5: |
| 2709 | case NFA_ZREF6: |
| 2710 | case NFA_ZREF7: |
| 2711 | case NFA_ZREF8: |
| 2712 | case NFA_ZREF9: |
| 2713 | #endif |
| 2714 | case NFA_NEWL: |
| 2715 | case NFA_SKIP: |
| 2716 | /* unknown width */ |
| 2717 | return -1; |
| 2718 | |
| 2719 | case NFA_BOL: |
| 2720 | case NFA_EOL: |
| 2721 | case NFA_BOF: |
| 2722 | case NFA_EOF: |
| 2723 | case NFA_BOW: |
| 2724 | case NFA_EOW: |
| 2725 | case NFA_MOPEN: |
| 2726 | case NFA_MOPEN1: |
| 2727 | case NFA_MOPEN2: |
| 2728 | case NFA_MOPEN3: |
| 2729 | case NFA_MOPEN4: |
| 2730 | case NFA_MOPEN5: |
| 2731 | case NFA_MOPEN6: |
| 2732 | case NFA_MOPEN7: |
| 2733 | case NFA_MOPEN8: |
| 2734 | case NFA_MOPEN9: |
| 2735 | #ifdef FEAT_SYN_HL |
| 2736 | case NFA_ZOPEN: |
| 2737 | case NFA_ZOPEN1: |
| 2738 | case NFA_ZOPEN2: |
| 2739 | case NFA_ZOPEN3: |
| 2740 | case NFA_ZOPEN4: |
| 2741 | case NFA_ZOPEN5: |
| 2742 | case NFA_ZOPEN6: |
| 2743 | case NFA_ZOPEN7: |
| 2744 | case NFA_ZOPEN8: |
| 2745 | case NFA_ZOPEN9: |
| 2746 | case NFA_ZCLOSE: |
| 2747 | case NFA_ZCLOSE1: |
| 2748 | case NFA_ZCLOSE2: |
| 2749 | case NFA_ZCLOSE3: |
| 2750 | case NFA_ZCLOSE4: |
| 2751 | case NFA_ZCLOSE5: |
| 2752 | case NFA_ZCLOSE6: |
| 2753 | case NFA_ZCLOSE7: |
| 2754 | case NFA_ZCLOSE8: |
| 2755 | case NFA_ZCLOSE9: |
| 2756 | #endif |
| 2757 | case NFA_MCLOSE: |
| 2758 | case NFA_MCLOSE1: |
| 2759 | case NFA_MCLOSE2: |
| 2760 | case NFA_MCLOSE3: |
| 2761 | case NFA_MCLOSE4: |
| 2762 | case NFA_MCLOSE5: |
| 2763 | case NFA_MCLOSE6: |
| 2764 | case NFA_MCLOSE7: |
| 2765 | case NFA_MCLOSE8: |
| 2766 | case NFA_MCLOSE9: |
| 2767 | case NFA_NOPEN: |
| 2768 | case NFA_NCLOSE: |
| 2769 | |
| 2770 | case NFA_LNUM_GT: |
| 2771 | case NFA_LNUM_LT: |
| 2772 | case NFA_COL_GT: |
| 2773 | case NFA_COL_LT: |
| 2774 | case NFA_VCOL_GT: |
| 2775 | case NFA_VCOL_LT: |
| 2776 | case NFA_MARK_GT: |
| 2777 | case NFA_MARK_LT: |
| 2778 | case NFA_VISUAL: |
| 2779 | case NFA_LNUM: |
| 2780 | case NFA_CURSOR: |
| 2781 | case NFA_COL: |
| 2782 | case NFA_VCOL: |
| 2783 | case NFA_MARK: |
| 2784 | |
| 2785 | case NFA_ZSTART: |
| 2786 | case NFA_ZEND: |
| 2787 | case NFA_OPT_CHARS: |
| 2788 | case NFA_SKIP_CHAR: |
| 2789 | case NFA_START_PATTERN: |
| 2790 | case NFA_END_PATTERN: |
| 2791 | case NFA_COMPOSING: |
| 2792 | case NFA_END_COMPOSING: |
| 2793 | /* zero-width */ |
| 2794 | break; |
| 2795 | |
| 2796 | default: |
| 2797 | if (state->c < 0) |
| 2798 | /* don't know what this is */ |
| 2799 | return -1; |
| 2800 | /* normal character */ |
| 2801 | len += MB_CHAR2LEN(state->c); |
| 2802 | break; |
| 2803 | } |
| 2804 | |
| 2805 | /* normal way to continue */ |
| 2806 | state = state->out; |
| 2807 | } |
| 2808 | |
| 2809 | /* unrecognized */ |
| 2810 | return -1; |
| 2811 | } |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 2812 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2813 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2814 | * Convert a postfix form into its equivalent NFA. |
| 2815 | * Return the NFA start state on success, NULL otherwise. |
| 2816 | */ |
| 2817 | static nfa_state_T * |
| 2818 | post2nfa(postfix, end, nfa_calc_size) |
| 2819 | int *postfix; |
| 2820 | int *end; |
| 2821 | int nfa_calc_size; |
| 2822 | { |
| 2823 | int *p; |
| 2824 | int mopen; |
| 2825 | int mclose; |
| 2826 | Frag_T *stack = NULL; |
| 2827 | Frag_T *stackp = NULL; |
| 2828 | Frag_T *stack_end = NULL; |
| 2829 | Frag_T e1; |
| 2830 | Frag_T e2; |
| 2831 | Frag_T e; |
| 2832 | nfa_state_T *s; |
| 2833 | nfa_state_T *s1; |
| 2834 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2835 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2836 | |
| 2837 | if (postfix == NULL) |
| 2838 | return NULL; |
| 2839 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2840 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2841 | #define POP() st_pop(&stackp, stack); \ |
| 2842 | if (stackp < stack) \ |
| 2843 | { \ |
| 2844 | st_error(postfix, end, p); \ |
| 2845 | return NULL; \ |
| 2846 | } |
| 2847 | |
| 2848 | if (nfa_calc_size == FALSE) |
| 2849 | { |
| 2850 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2851 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2852 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2853 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2854 | } |
| 2855 | |
| 2856 | for (p = postfix; p < end; ++p) |
| 2857 | { |
| 2858 | switch (*p) |
| 2859 | { |
| 2860 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2861 | /* Concatenation. |
| 2862 | * Pay attention: this operator does not exist in the r.e. itself |
| 2863 | * (it is implicit, really). It is added when r.e. is translated |
| 2864 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2865 | if (nfa_calc_size == TRUE) |
| 2866 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2867 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2868 | break; |
| 2869 | } |
| 2870 | e2 = POP(); |
| 2871 | e1 = POP(); |
| 2872 | patch(e1.out, e2.start); |
| 2873 | PUSH(frag(e1.start, e2.out)); |
| 2874 | break; |
| 2875 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2876 | case NFA_OR: |
| 2877 | /* Alternation */ |
| 2878 | if (nfa_calc_size == TRUE) |
| 2879 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2880 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2881 | break; |
| 2882 | } |
| 2883 | e2 = POP(); |
| 2884 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2885 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2886 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2887 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2888 | PUSH(frag(s, append(e1.out, e2.out))); |
| 2889 | break; |
| 2890 | |
| 2891 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2892 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2893 | if (nfa_calc_size == TRUE) |
| 2894 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2895 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2896 | break; |
| 2897 | } |
| 2898 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2899 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2900 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2901 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2902 | patch(e.out, s); |
| 2903 | PUSH(frag(s, list1(&s->out1))); |
| 2904 | break; |
| 2905 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2906 | case NFA_STAR_NONGREEDY: |
| 2907 | /* Zero or more, prefer zero */ |
| 2908 | if (nfa_calc_size == TRUE) |
| 2909 | { |
| 2910 | nstate++; |
| 2911 | break; |
| 2912 | } |
| 2913 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2914 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2915 | if (s == NULL) |
| 2916 | goto theend; |
| 2917 | patch(e.out, s); |
| 2918 | PUSH(frag(s, list1(&s->out))); |
| 2919 | break; |
| 2920 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2921 | case NFA_QUEST: |
| 2922 | /* one or zero atoms=> greedy match */ |
| 2923 | if (nfa_calc_size == TRUE) |
| 2924 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2925 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2926 | break; |
| 2927 | } |
| 2928 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2929 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2930 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2931 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2932 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 2933 | break; |
| 2934 | |
| 2935 | case NFA_QUEST_NONGREEDY: |
| 2936 | /* zero or one atoms => non-greedy match */ |
| 2937 | if (nfa_calc_size == TRUE) |
| 2938 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2939 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2940 | break; |
| 2941 | } |
| 2942 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2943 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2944 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2945 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2946 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 2947 | break; |
| 2948 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2949 | case NFA_END_COLL: |
| 2950 | case NFA_END_NEG_COLL: |
| 2951 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 2952 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 2953 | * add the output to the start. */ |
| 2954 | if (nfa_calc_size == TRUE) |
| 2955 | { |
| 2956 | nstate++; |
| 2957 | break; |
| 2958 | } |
| 2959 | e = POP(); |
| 2960 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 2961 | if (s == NULL) |
| 2962 | goto theend; |
| 2963 | patch(e.out, s); |
| 2964 | e.start->out1 = s; |
| 2965 | PUSH(frag(e.start, list1(&s->out))); |
| 2966 | break; |
| 2967 | |
| 2968 | case NFA_RANGE: |
| 2969 | /* Before this are two characters, the low and high end of a |
| 2970 | * range. Turn them into two states with MIN and MAX. */ |
| 2971 | if (nfa_calc_size == TRUE) |
| 2972 | { |
| 2973 | /* nstate += 0; */ |
| 2974 | break; |
| 2975 | } |
| 2976 | e2 = POP(); |
| 2977 | e1 = POP(); |
| 2978 | e2.start->val = e2.start->c; |
| 2979 | e2.start->c = NFA_RANGE_MAX; |
| 2980 | e1.start->val = e1.start->c; |
| 2981 | e1.start->c = NFA_RANGE_MIN; |
| 2982 | patch(e1.out, e2.start); |
| 2983 | PUSH(frag(e1.start, e2.out)); |
| 2984 | break; |
| 2985 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2986 | case NFA_SKIP_CHAR: |
| 2987 | /* Symbol of 0-length, Used in a repetition |
| 2988 | * with max/min count of 0 */ |
| 2989 | if (nfa_calc_size == TRUE) |
| 2990 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2991 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2992 | break; |
| 2993 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2994 | s = alloc_state(NFA_SKIP_CHAR, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2995 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2996 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2997 | PUSH(frag(s, list1(&s->out))); |
| 2998 | break; |
| 2999 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3000 | case NFA_OPT_CHARS: |
| 3001 | { |
| 3002 | int n; |
| 3003 | |
| 3004 | /* \%[abc] */ |
| 3005 | n = *++p; /* get number of characters */ |
| 3006 | if (nfa_calc_size == TRUE) |
| 3007 | { |
| 3008 | nstate += n; |
| 3009 | break; |
| 3010 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 3011 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3012 | e1.out = NULL; /* stores list with out1's */ |
| 3013 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 3014 | while (n-- > 0) |
| 3015 | { |
| 3016 | e = POP(); /* get character */ |
| 3017 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 3018 | if (s == NULL) |
| 3019 | goto theend; |
| 3020 | if (e1.out == NULL) |
| 3021 | e1 = e; |
| 3022 | patch(e.out, s1); |
| 3023 | append(e1.out, list1(&s->out1)); |
| 3024 | s1 = s; |
| 3025 | } |
| 3026 | PUSH(frag(s, e1.out)); |
| 3027 | break; |
| 3028 | } |
| 3029 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3030 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3031 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3032 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3033 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3034 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3035 | { |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3036 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 3037 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3038 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3039 | int start_state; |
| 3040 | int end_state; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3041 | int n = 0; |
| 3042 | nfa_state_T *zend; |
| 3043 | nfa_state_T *skip; |
| 3044 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3045 | switch (*p) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3046 | { |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3047 | case NFA_PREV_ATOM_NO_WIDTH: |
| 3048 | start_state = NFA_START_INVISIBLE; |
| 3049 | end_state = NFA_END_INVISIBLE; |
| 3050 | break; |
| 3051 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 3052 | start_state = NFA_START_INVISIBLE_NEG; |
| 3053 | end_state = NFA_END_INVISIBLE_NEG; |
| 3054 | break; |
| 3055 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3056 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 3057 | end_state = NFA_END_INVISIBLE; |
| 3058 | break; |
| 3059 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 3060 | start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
| 3061 | end_state = NFA_END_INVISIBLE_NEG; |
| 3062 | break; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 3063 | default: /* NFA_PREV_ATOM_LIKE_PATTERN: */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3064 | start_state = NFA_START_PATTERN; |
| 3065 | end_state = NFA_END_PATTERN; |
| 3066 | break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3067 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3068 | |
| 3069 | if (before) |
| 3070 | n = *++p; /* get the count */ |
| 3071 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3072 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3073 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3074 | * The \@<= operator: match for the preceding atom. |
| 3075 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3076 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3077 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3078 | |
| 3079 | if (nfa_calc_size == TRUE) |
| 3080 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3081 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3082 | break; |
| 3083 | } |
| 3084 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3085 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3086 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3087 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3088 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3089 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3090 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3091 | goto theend; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3092 | if (pattern) |
| 3093 | { |
| 3094 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 3095 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 3096 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 3097 | s1->out= skip; |
| 3098 | patch(e.out, zend); |
| 3099 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3100 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3101 | else |
| 3102 | { |
| 3103 | patch(e.out, s1); |
| 3104 | PUSH(frag(s, list1(&s1->out))); |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3105 | if (before) |
| 3106 | { |
| 3107 | if (n <= 0) |
| 3108 | /* See if we can guess the maximum width, it avoids a |
| 3109 | * lot of pointless tries. */ |
| 3110 | n = nfa_max_width(e.start, 0); |
| 3111 | s->val = n; /* store the count */ |
| 3112 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3113 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3114 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3115 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3116 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3117 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3118 | case NFA_COMPOSING: /* char with composing char */ |
| 3119 | #if 0 |
| 3120 | /* TODO */ |
| 3121 | if (regflags & RF_ICOMBINE) |
| 3122 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3123 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3124 | } |
| 3125 | #endif |
| 3126 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3127 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3128 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3129 | case NFA_MOPEN: /* \( \) Submatch */ |
| 3130 | case NFA_MOPEN1: |
| 3131 | case NFA_MOPEN2: |
| 3132 | case NFA_MOPEN3: |
| 3133 | case NFA_MOPEN4: |
| 3134 | case NFA_MOPEN5: |
| 3135 | case NFA_MOPEN6: |
| 3136 | case NFA_MOPEN7: |
| 3137 | case NFA_MOPEN8: |
| 3138 | case NFA_MOPEN9: |
| 3139 | #ifdef FEAT_SYN_HL |
| 3140 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 3141 | case NFA_ZOPEN1: |
| 3142 | case NFA_ZOPEN2: |
| 3143 | case NFA_ZOPEN3: |
| 3144 | case NFA_ZOPEN4: |
| 3145 | case NFA_ZOPEN5: |
| 3146 | case NFA_ZOPEN6: |
| 3147 | case NFA_ZOPEN7: |
| 3148 | case NFA_ZOPEN8: |
| 3149 | case NFA_ZOPEN9: |
| 3150 | #endif |
| 3151 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3152 | if (nfa_calc_size == TRUE) |
| 3153 | { |
| 3154 | nstate += 2; |
| 3155 | break; |
| 3156 | } |
| 3157 | |
| 3158 | mopen = *p; |
| 3159 | switch (*p) |
| 3160 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3161 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 3162 | #ifdef FEAT_SYN_HL |
| 3163 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 3164 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 3165 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 3166 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 3167 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 3168 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 3169 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 3170 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 3171 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 3172 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 3173 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3174 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3175 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3176 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3177 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3178 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3179 | mclose = *p + NSUBEXP; |
| 3180 | break; |
| 3181 | } |
| 3182 | |
| 3183 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 3184 | * the empty regexp "". In this case, the NFA will be |
| 3185 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 3186 | * empty groups of parenthesis, and empty mbyte chars */ |
| 3187 | if (stackp == stack) |
| 3188 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3189 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3190 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3191 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3192 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3193 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3194 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3195 | patch(list1(&s->out), s1); |
| 3196 | PUSH(frag(s, list1(&s1->out))); |
| 3197 | break; |
| 3198 | } |
| 3199 | |
| 3200 | /* At least one node was emitted before NFA_MOPEN, so |
| 3201 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 3202 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3203 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3204 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3205 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3206 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3207 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3208 | if (s1 == 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 | patch(e.out, s1); |
| 3211 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3212 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3213 | if (mopen == NFA_COMPOSING) |
| 3214 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3215 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3216 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3217 | |
| 3218 | PUSH(frag(s, list1(&s1->out))); |
| 3219 | break; |
| 3220 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3221 | case NFA_BACKREF1: |
| 3222 | case NFA_BACKREF2: |
| 3223 | case NFA_BACKREF3: |
| 3224 | case NFA_BACKREF4: |
| 3225 | case NFA_BACKREF5: |
| 3226 | case NFA_BACKREF6: |
| 3227 | case NFA_BACKREF7: |
| 3228 | case NFA_BACKREF8: |
| 3229 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3230 | #ifdef FEAT_SYN_HL |
| 3231 | case NFA_ZREF1: |
| 3232 | case NFA_ZREF2: |
| 3233 | case NFA_ZREF3: |
| 3234 | case NFA_ZREF4: |
| 3235 | case NFA_ZREF5: |
| 3236 | case NFA_ZREF6: |
| 3237 | case NFA_ZREF7: |
| 3238 | case NFA_ZREF8: |
| 3239 | case NFA_ZREF9: |
| 3240 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3241 | if (nfa_calc_size == TRUE) |
| 3242 | { |
| 3243 | nstate += 2; |
| 3244 | break; |
| 3245 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3246 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3247 | if (s == NULL) |
| 3248 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3249 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3250 | if (s1 == NULL) |
| 3251 | goto theend; |
| 3252 | patch(list1(&s->out), s1); |
| 3253 | PUSH(frag(s, list1(&s1->out))); |
| 3254 | break; |
| 3255 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3256 | case NFA_LNUM: |
| 3257 | case NFA_LNUM_GT: |
| 3258 | case NFA_LNUM_LT: |
| 3259 | case NFA_VCOL: |
| 3260 | case NFA_VCOL_GT: |
| 3261 | case NFA_VCOL_LT: |
| 3262 | case NFA_COL: |
| 3263 | case NFA_COL_GT: |
| 3264 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 3265 | case NFA_MARK: |
| 3266 | case NFA_MARK_GT: |
| 3267 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3268 | { |
| 3269 | int n = *++p; /* lnum, col or mark name */ |
| 3270 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3271 | if (nfa_calc_size == TRUE) |
| 3272 | { |
| 3273 | nstate += 1; |
| 3274 | break; |
| 3275 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3276 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3277 | if (s == NULL) |
| 3278 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3279 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3280 | PUSH(frag(s, list1(&s->out))); |
| 3281 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3282 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3283 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3284 | case NFA_ZSTART: |
| 3285 | case NFA_ZEND: |
| 3286 | default: |
| 3287 | /* Operands */ |
| 3288 | if (nfa_calc_size == TRUE) |
| 3289 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3290 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3291 | break; |
| 3292 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3293 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3294 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3295 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3296 | PUSH(frag(s, list1(&s->out))); |
| 3297 | break; |
| 3298 | |
| 3299 | } /* switch(*p) */ |
| 3300 | |
| 3301 | } /* for(p = postfix; *p; ++p) */ |
| 3302 | |
| 3303 | if (nfa_calc_size == TRUE) |
| 3304 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3305 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3306 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3307 | } |
| 3308 | |
| 3309 | e = POP(); |
| 3310 | if (stackp != stack) |
| 3311 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 3312 | |
| 3313 | if (istate >= nstate) |
| 3314 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 3315 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3316 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3317 | matchstate->c = NFA_MATCH; |
| 3318 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3319 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3320 | |
| 3321 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3322 | ret = e.start; |
| 3323 | |
| 3324 | theend: |
| 3325 | vim_free(stack); |
| 3326 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3327 | |
| 3328 | #undef POP1 |
| 3329 | #undef PUSH1 |
| 3330 | #undef POP2 |
| 3331 | #undef PUSH2 |
| 3332 | #undef POP |
| 3333 | #undef PUSH |
| 3334 | } |
| 3335 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3336 | /* |
| 3337 | * After building the NFA program, inspect it to add optimization hints. |
| 3338 | */ |
| 3339 | static void |
| 3340 | nfa_postprocess(prog) |
| 3341 | nfa_regprog_T *prog; |
| 3342 | { |
| 3343 | int i; |
| 3344 | int c; |
| 3345 | |
| 3346 | for (i = 0; i < prog->nstate; ++i) |
| 3347 | { |
| 3348 | c = prog->state[i].c; |
| 3349 | if (c == NFA_START_INVISIBLE |
| 3350 | || c == NFA_START_INVISIBLE_NEG |
| 3351 | || c == NFA_START_INVISIBLE_BEFORE |
| 3352 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3353 | { |
| 3354 | int directly; |
| 3355 | |
| 3356 | /* Do it directly when what follows is possibly the end of the |
| 3357 | * match. */ |
| 3358 | if (match_follows(prog->state[i].out1->out, 0)) |
| 3359 | directly = TRUE; |
| 3360 | else |
| 3361 | { |
| 3362 | int ch_invisible = failure_chance(prog->state[i].out, 0); |
| 3363 | int ch_follows = failure_chance(prog->state[i].out1->out, 0); |
| 3364 | |
| 3365 | /* Postpone when the invisible match is expensive or has a |
| 3366 | * lower chance of failing. */ |
| 3367 | if (c == NFA_START_INVISIBLE_BEFORE |
| 3368 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3369 | { |
| 3370 | /* "before" matches are very expensive when |
| 3371 | * unbounded, always prefer what follows then, |
| 3372 | * unless what follows will always match. |
| 3373 | * Otherwise strongly prefer what follows. */ |
| 3374 | if (prog->state[i].val <= 0 && ch_follows > 0) |
| 3375 | directly = FALSE; |
| 3376 | else |
| 3377 | directly = ch_follows * 10 < ch_invisible; |
| 3378 | } |
| 3379 | else |
| 3380 | { |
| 3381 | /* normal invisible, first do the one with the |
| 3382 | * highest failure chance */ |
| 3383 | directly = ch_follows < ch_invisible; |
| 3384 | } |
| 3385 | } |
| 3386 | if (directly) |
| 3387 | /* switch to the _FIRST state */ |
| 3388 | ++prog->state[i].c; |
| 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3393 | /**************************************************************** |
| 3394 | * NFA execution code. |
| 3395 | ****************************************************************/ |
| 3396 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3397 | typedef struct |
| 3398 | { |
| 3399 | int in_use; /* number of subexpr with useful info */ |
| 3400 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3401 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3402 | union |
| 3403 | { |
| 3404 | struct multipos |
| 3405 | { |
| 3406 | lpos_T start; |
| 3407 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3408 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3409 | struct linepos |
| 3410 | { |
| 3411 | char_u *start; |
| 3412 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3413 | } line[NSUBEXP]; |
| 3414 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3415 | } regsub_T; |
| 3416 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3417 | typedef struct |
| 3418 | { |
| 3419 | regsub_T norm; /* \( .. \) matches */ |
| 3420 | #ifdef FEAT_SYN_HL |
| 3421 | regsub_T synt; /* \z( .. \) matches */ |
| 3422 | #endif |
| 3423 | } regsubs_T; |
| 3424 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3425 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3426 | typedef struct nfa_pim_S nfa_pim_T; |
| 3427 | struct nfa_pim_S |
| 3428 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3429 | int result; /* NFA_PIM_*, see below */ |
| 3430 | nfa_state_T *state; /* the invisible match start state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3431 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3432 | union |
| 3433 | { |
| 3434 | lpos_T pos; |
| 3435 | char_u *ptr; |
| 3436 | } end; /* where the match must end */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3437 | }; |
| 3438 | |
| 3439 | /* Values for done in nfa_pim_T. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3440 | #define NFA_PIM_UNUSED 0 /* pim not used */ |
| 3441 | #define NFA_PIM_TODO 1 /* pim not done yet */ |
| 3442 | #define NFA_PIM_MATCH 2 /* pim executed, matches */ |
| 3443 | #define NFA_PIM_NOMATCH 3 /* pim executed, no match */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3444 | |
| 3445 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3446 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3447 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3448 | { |
| 3449 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3450 | int count; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3451 | nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed |
| 3452 | * invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3453 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3454 | } nfa_thread_T; |
| 3455 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3456 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3457 | typedef struct |
| 3458 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3459 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3460 | int n; /* nr of states currently in "t" */ |
| 3461 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3462 | int id; /* ID of the list */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3463 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3464 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3465 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3466 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 3467 | static void log_subexpr __ARGS((regsub_T *sub)); |
| 3468 | |
| 3469 | static void |
| 3470 | log_subsexpr(subs) |
| 3471 | regsubs_T *subs; |
| 3472 | { |
| 3473 | log_subexpr(&subs->norm); |
| 3474 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3475 | if (nfa_has_zsubexpr) |
| 3476 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3477 | # endif |
| 3478 | } |
| 3479 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3480 | static void |
| 3481 | log_subexpr(sub) |
| 3482 | regsub_T *sub; |
| 3483 | { |
| 3484 | int j; |
| 3485 | |
| 3486 | for (j = 0; j < sub->in_use; j++) |
| 3487 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3488 | 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] | 3489 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3490 | sub->list.multi[j].start.col, |
| 3491 | (int)sub->list.multi[j].start.lnum, |
| 3492 | sub->list.multi[j].end.col, |
| 3493 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3494 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3495 | { |
| 3496 | char *s = (char *)sub->list.line[j].start; |
| 3497 | char *e = (char *)sub->list.line[j].end; |
| 3498 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3499 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3500 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3501 | s == NULL ? "NULL" : s, |
| 3502 | e == NULL ? "NULL" : e); |
| 3503 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3504 | } |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3505 | |
| 3506 | static char * |
| 3507 | pim_info(nfa_pim_T *pim) |
| 3508 | { |
| 3509 | static char buf[30]; |
| 3510 | |
| 3511 | if (pim == NULL || pim->result == NFA_PIM_UNUSED) |
| 3512 | buf[0] = NUL; |
| 3513 | else |
| 3514 | { |
| 3515 | sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col |
| 3516 | : (int)(pim->end.ptr - reginput)); |
| 3517 | } |
| 3518 | return buf; |
| 3519 | } |
| 3520 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3521 | #endif |
| 3522 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3523 | /* Used during execution: whether a match has been found. */ |
| 3524 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3525 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3526 | 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] | 3527 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3528 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3529 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3530 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3531 | static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs)); |
| 3532 | 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] | 3533 | 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] | 3534 | 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] | 3535 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3536 | /* |
| 3537 | * Copy postponed invisible match info from "from" to "to". |
| 3538 | */ |
| 3539 | static void |
| 3540 | copy_pim(to, from) |
| 3541 | nfa_pim_T *to; |
| 3542 | nfa_pim_T *from; |
| 3543 | { |
| 3544 | to->result = from->result; |
| 3545 | to->state = from->state; |
| 3546 | copy_sub(&to->subs.norm, &from->subs.norm); |
| 3547 | #ifdef FEAT_SYN_HL |
| 3548 | if (nfa_has_zsubexpr) |
| 3549 | copy_sub(&to->subs.synt, &from->subs.synt); |
| 3550 | #endif |
| 3551 | to->end = from->end; |
| 3552 | } |
| 3553 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3554 | static void |
| 3555 | clear_sub(sub) |
| 3556 | regsub_T *sub; |
| 3557 | { |
| 3558 | if (REG_MULTI) |
| 3559 | /* Use 0xff to set lnum to -1 */ |
| 3560 | vim_memset(sub->list.multi, 0xff, |
| 3561 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3562 | else |
| 3563 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3564 | sub->in_use = 0; |
| 3565 | } |
| 3566 | |
| 3567 | /* |
| 3568 | * Copy the submatches from "from" to "to". |
| 3569 | */ |
| 3570 | static void |
| 3571 | copy_sub(to, from) |
| 3572 | regsub_T *to; |
| 3573 | regsub_T *from; |
| 3574 | { |
| 3575 | to->in_use = from->in_use; |
| 3576 | if (from->in_use > 0) |
| 3577 | { |
| 3578 | /* Copy the match start and end positions. */ |
| 3579 | if (REG_MULTI) |
| 3580 | mch_memmove(&to->list.multi[0], |
| 3581 | &from->list.multi[0], |
| 3582 | sizeof(struct multipos) * from->in_use); |
| 3583 | else |
| 3584 | mch_memmove(&to->list.line[0], |
| 3585 | &from->list.line[0], |
| 3586 | sizeof(struct linepos) * from->in_use); |
| 3587 | } |
| 3588 | } |
| 3589 | |
| 3590 | /* |
| 3591 | * Like copy_sub() but exclude the main match. |
| 3592 | */ |
| 3593 | static void |
| 3594 | copy_sub_off(to, from) |
| 3595 | regsub_T *to; |
| 3596 | regsub_T *from; |
| 3597 | { |
| 3598 | if (to->in_use < from->in_use) |
| 3599 | to->in_use = from->in_use; |
| 3600 | if (from->in_use > 1) |
| 3601 | { |
| 3602 | /* Copy the match start and end positions. */ |
| 3603 | if (REG_MULTI) |
| 3604 | mch_memmove(&to->list.multi[1], |
| 3605 | &from->list.multi[1], |
| 3606 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3607 | else |
| 3608 | mch_memmove(&to->list.line[1], |
| 3609 | &from->list.line[1], |
| 3610 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3611 | } |
| 3612 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3613 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3614 | /* |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3615 | * Return TRUE if "sub1" and "sub2" have the same start positions. |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3616 | */ |
| 3617 | static int |
| 3618 | sub_equal(sub1, sub2) |
| 3619 | regsub_T *sub1; |
| 3620 | regsub_T *sub2; |
| 3621 | { |
| 3622 | int i; |
| 3623 | int todo; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3624 | linenr_T s1; |
| 3625 | linenr_T s2; |
| 3626 | char_u *sp1; |
| 3627 | char_u *sp2; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3628 | |
| 3629 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3630 | if (REG_MULTI) |
| 3631 | { |
| 3632 | for (i = 0; i < todo; ++i) |
| 3633 | { |
| 3634 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3635 | s1 = sub1->list.multi[i].start.lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3636 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3637 | s1 = 0; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3638 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3639 | s2 = sub2->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 | s2 = 0; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3642 | if (s1 != s2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3643 | return FALSE; |
| 3644 | if (s1 != 0 && sub1->list.multi[i].start.col |
| 3645 | != sub2->list.multi[i].start.col) |
| 3646 | return FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3647 | } |
| 3648 | } |
| 3649 | else |
| 3650 | { |
| 3651 | for (i = 0; i < todo; ++i) |
| 3652 | { |
| 3653 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3654 | sp1 = sub1->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3655 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3656 | sp1 = NULL; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3657 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3658 | sp2 = sub2->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 | sp2 = NULL; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3661 | if (sp1 != sp2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3662 | return FALSE; |
| 3663 | } |
| 3664 | } |
| 3665 | |
| 3666 | return TRUE; |
| 3667 | } |
| 3668 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3669 | #ifdef ENABLE_LOG |
| 3670 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3671 | report_state(char *action, |
| 3672 | regsub_T *sub, |
| 3673 | nfa_state_T *state, |
| 3674 | int lid, |
| 3675 | nfa_pim_T *pim) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3676 | { |
| 3677 | int col; |
| 3678 | |
| 3679 | if (sub->in_use <= 0) |
| 3680 | col = -1; |
| 3681 | else if (REG_MULTI) |
| 3682 | col = sub->list.multi[0].start.col; |
| 3683 | else |
| 3684 | col = (int)(sub->list.line[0].start - regline); |
| 3685 | nfa_set_code(state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3686 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n", |
| 3687 | action, abs(state->id), lid, state->c, code, col, |
| 3688 | pim_info(pim)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3689 | } |
| 3690 | #endif |
| 3691 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3692 | /* |
| 3693 | * Return TRUE if the same state is already in list "l" with the same |
| 3694 | * positions as "subs". |
| 3695 | */ |
| 3696 | static int |
| 3697 | has_state_with_pos(l, state, subs) |
| 3698 | nfa_list_T *l; /* runtime state list */ |
| 3699 | nfa_state_T *state; /* state to update */ |
| 3700 | regsubs_T *subs; /* pointers to subexpressions */ |
| 3701 | { |
| 3702 | nfa_thread_T *thread; |
| 3703 | int i; |
| 3704 | |
| 3705 | for (i = 0; i < l->n; ++i) |
| 3706 | { |
| 3707 | thread = &l->t[i]; |
| 3708 | if (thread->state->id == state->id |
| 3709 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 3710 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3711 | && (!nfa_has_zsubexpr |
| 3712 | || sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3713 | #endif |
| 3714 | ) |
| 3715 | return TRUE; |
| 3716 | } |
| 3717 | return FALSE; |
| 3718 | } |
| 3719 | |
| 3720 | /* |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3721 | * Return TRUE if "state" leads to a NFA_MATCH without advancing the input. |
| 3722 | */ |
| 3723 | static int |
| 3724 | match_follows(startstate, depth) |
| 3725 | nfa_state_T *startstate; |
| 3726 | int depth; |
| 3727 | { |
| 3728 | nfa_state_T *state = startstate; |
| 3729 | |
| 3730 | /* avoid too much recursion */ |
| 3731 | if (depth > 10) |
| 3732 | return FALSE; |
| 3733 | |
| 3734 | for (;;) |
| 3735 | { |
| 3736 | switch (state->c) |
| 3737 | { |
| 3738 | case NFA_MATCH: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3739 | case NFA_MCLOSE: |
| 3740 | case NFA_END_INVISIBLE: |
| 3741 | case NFA_END_INVISIBLE_NEG: |
| 3742 | case NFA_END_PATTERN: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3743 | return TRUE; |
| 3744 | |
| 3745 | case NFA_SPLIT: |
| 3746 | return match_follows(state->out, depth + 1) |
| 3747 | || match_follows(state->out1, depth + 1); |
| 3748 | |
| 3749 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3750 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3751 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3752 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3753 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3754 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3755 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3756 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3757 | case NFA_COMPOSING: |
| 3758 | /* skip ahead to next state */ |
| 3759 | state = state->out1->out; |
| 3760 | break; |
| 3761 | |
| 3762 | case NFA_ANY: |
| 3763 | case NFA_IDENT: |
| 3764 | case NFA_SIDENT: |
| 3765 | case NFA_KWORD: |
| 3766 | case NFA_SKWORD: |
| 3767 | case NFA_FNAME: |
| 3768 | case NFA_SFNAME: |
| 3769 | case NFA_PRINT: |
| 3770 | case NFA_SPRINT: |
| 3771 | case NFA_WHITE: |
| 3772 | case NFA_NWHITE: |
| 3773 | case NFA_DIGIT: |
| 3774 | case NFA_NDIGIT: |
| 3775 | case NFA_HEX: |
| 3776 | case NFA_NHEX: |
| 3777 | case NFA_OCTAL: |
| 3778 | case NFA_NOCTAL: |
| 3779 | case NFA_WORD: |
| 3780 | case NFA_NWORD: |
| 3781 | case NFA_HEAD: |
| 3782 | case NFA_NHEAD: |
| 3783 | case NFA_ALPHA: |
| 3784 | case NFA_NALPHA: |
| 3785 | case NFA_LOWER: |
| 3786 | case NFA_NLOWER: |
| 3787 | case NFA_UPPER: |
| 3788 | case NFA_NUPPER: |
| 3789 | case NFA_START_COLL: |
| 3790 | case NFA_START_NEG_COLL: |
| 3791 | case NFA_NEWL: |
| 3792 | /* state will advance input */ |
| 3793 | return FALSE; |
| 3794 | |
| 3795 | default: |
| 3796 | if (state->c > 0) |
| 3797 | /* state will advance input */ |
| 3798 | return FALSE; |
| 3799 | |
| 3800 | /* Others: zero-width or possibly zero-width, might still find |
| 3801 | * a match at the same position, keep looking. */ |
| 3802 | break; |
| 3803 | } |
| 3804 | state = state->out; |
| 3805 | } |
| 3806 | return FALSE; |
| 3807 | } |
| 3808 | |
| 3809 | |
| 3810 | /* |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3811 | * Return TRUE if "state" is already in list "l". |
| 3812 | */ |
| 3813 | static int |
| 3814 | state_in_list(l, state, subs) |
| 3815 | nfa_list_T *l; /* runtime state list */ |
| 3816 | nfa_state_T *state; /* state to update */ |
| 3817 | regsubs_T *subs; /* pointers to subexpressions */ |
| 3818 | { |
| 3819 | if (state->lastlist[nfa_ll_index] == l->id) |
| 3820 | { |
| 3821 | if (!nfa_has_backref || has_state_with_pos(l, state, subs)) |
| 3822 | return TRUE; |
| 3823 | } |
| 3824 | return FALSE; |
| 3825 | } |
| 3826 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3827 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3828 | addstate(l, state, subs, pim, off) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3829 | nfa_list_T *l; /* runtime state list */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3830 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3831 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3832 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3833 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3834 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3835 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3836 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3837 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3838 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3839 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3840 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3841 | regsub_T *sub; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3842 | #ifdef ENABLE_LOG |
| 3843 | int did_print = FALSE; |
| 3844 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3845 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3846 | switch (state->c) |
| 3847 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3848 | case NFA_NCLOSE: |
| 3849 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3850 | case NFA_MCLOSE1: |
| 3851 | case NFA_MCLOSE2: |
| 3852 | case NFA_MCLOSE3: |
| 3853 | case NFA_MCLOSE4: |
| 3854 | case NFA_MCLOSE5: |
| 3855 | case NFA_MCLOSE6: |
| 3856 | case NFA_MCLOSE7: |
| 3857 | case NFA_MCLOSE8: |
| 3858 | case NFA_MCLOSE9: |
| 3859 | #ifdef FEAT_SYN_HL |
| 3860 | case NFA_ZCLOSE: |
| 3861 | case NFA_ZCLOSE1: |
| 3862 | case NFA_ZCLOSE2: |
| 3863 | case NFA_ZCLOSE3: |
| 3864 | case NFA_ZCLOSE4: |
| 3865 | case NFA_ZCLOSE5: |
| 3866 | case NFA_ZCLOSE6: |
| 3867 | case NFA_ZCLOSE7: |
| 3868 | case NFA_ZCLOSE8: |
| 3869 | case NFA_ZCLOSE9: |
| 3870 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3871 | case NFA_ZEND: |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 3872 | case NFA_SPLIT: |
| 3873 | case NFA_NOPEN: |
| 3874 | case NFA_SKIP_CHAR: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3875 | /* These nodes are not added themselves but their "out" and/or |
| 3876 | * "out1" may be added below. */ |
| 3877 | break; |
| 3878 | |
| 3879 | case NFA_MOPEN: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3880 | case NFA_MOPEN1: |
| 3881 | case NFA_MOPEN2: |
| 3882 | case NFA_MOPEN3: |
| 3883 | case NFA_MOPEN4: |
| 3884 | case NFA_MOPEN5: |
| 3885 | case NFA_MOPEN6: |
| 3886 | case NFA_MOPEN7: |
| 3887 | case NFA_MOPEN8: |
| 3888 | case NFA_MOPEN9: |
| 3889 | #ifdef FEAT_SYN_HL |
| 3890 | case NFA_ZOPEN: |
| 3891 | case NFA_ZOPEN1: |
| 3892 | case NFA_ZOPEN2: |
| 3893 | case NFA_ZOPEN3: |
| 3894 | case NFA_ZOPEN4: |
| 3895 | case NFA_ZOPEN5: |
| 3896 | case NFA_ZOPEN6: |
| 3897 | case NFA_ZOPEN7: |
| 3898 | case NFA_ZOPEN8: |
| 3899 | case NFA_ZOPEN9: |
| 3900 | #endif |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 3901 | case NFA_ZSTART: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3902 | /* These nodes do not need to be added, but we need to bail out |
| 3903 | * when it was tried to be added to this list before. */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3904 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3905 | goto skip_add; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3906 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3907 | break; |
| 3908 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3909 | case NFA_BOL: |
| 3910 | case NFA_BOF: |
| 3911 | /* "^" won't match past end-of-line, don't bother trying. |
Bram Moolenaar | b62bcd1 | 2013-06-13 20:19:40 +0200 | [diff] [blame] | 3912 | * Except when at the end of the line, or when we are going to the |
| 3913 | * next line for a look-behind match. */ |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3914 | if (reginput > regline |
Bram Moolenaar | b62bcd1 | 2013-06-13 20:19:40 +0200 | [diff] [blame] | 3915 | && *reginput != NUL |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 3916 | && (nfa_endp == NULL |
| 3917 | || !REG_MULTI |
| 3918 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 3919 | goto skip_add; |
| 3920 | /* FALLTHROUGH */ |
| 3921 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3922 | default: |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3923 | if (state->lastlist[nfa_ll_index] == l->id) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3924 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3925 | /* This state is already in the list, don't add it again, |
| 3926 | * unless it is an MOPEN that is used for a backreference. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3927 | if (!nfa_has_backref) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3928 | { |
| 3929 | skip_add: |
| 3930 | #ifdef ENABLE_LOG |
| 3931 | nfa_set_code(state->c); |
| 3932 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 3933 | abs(state->id), l->id, state->c, code); |
| 3934 | #endif |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3935 | return; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3936 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3937 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 3938 | /* Do not add the state again when it exists with the same |
| 3939 | * positions. */ |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3940 | if (has_state_with_pos(l, state, subs)) |
| 3941 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3942 | } |
| 3943 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 3944 | /* When there are backreferences the number of states may be (a |
| 3945 | * lot) bigger than anticipated. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3946 | if (nfa_has_backref && l->n == l->len) |
| 3947 | { |
| 3948 | int newlen = l->len * 3 / 2 + 50; |
| 3949 | |
| 3950 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 3951 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3952 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3953 | |
| 3954 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 3955 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3956 | thread = &l->t[l->n++]; |
| 3957 | thread->state = state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3958 | if (pim == NULL) |
| 3959 | thread->pim.result = NFA_PIM_UNUSED; |
| 3960 | else |
| 3961 | copy_pim(&thread->pim, pim); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3962 | copy_sub(&thread->subs.norm, &subs->norm); |
| 3963 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3964 | if (nfa_has_zsubexpr) |
| 3965 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3966 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3967 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3968 | report_state("Adding", &thread->subs.norm, state, l->id, pim); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3969 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3970 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3971 | } |
| 3972 | |
| 3973 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3974 | if (!did_print) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3975 | report_state("Processing", &subs->norm, state, l->id, pim); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3976 | #endif |
| 3977 | switch (state->c) |
| 3978 | { |
| 3979 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3980 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3981 | break; |
| 3982 | |
| 3983 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3984 | /* order matters here */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3985 | addstate(l, state->out, subs, pim, off); |
| 3986 | addstate(l, state->out1, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3987 | break; |
| 3988 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3989 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3990 | case NFA_NOPEN: |
| 3991 | case NFA_NCLOSE: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3992 | addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3993 | break; |
| 3994 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3995 | case NFA_MOPEN: |
| 3996 | case NFA_MOPEN1: |
| 3997 | case NFA_MOPEN2: |
| 3998 | case NFA_MOPEN3: |
| 3999 | case NFA_MOPEN4: |
| 4000 | case NFA_MOPEN5: |
| 4001 | case NFA_MOPEN6: |
| 4002 | case NFA_MOPEN7: |
| 4003 | case NFA_MOPEN8: |
| 4004 | case NFA_MOPEN9: |
| 4005 | #ifdef FEAT_SYN_HL |
| 4006 | case NFA_ZOPEN: |
| 4007 | case NFA_ZOPEN1: |
| 4008 | case NFA_ZOPEN2: |
| 4009 | case NFA_ZOPEN3: |
| 4010 | case NFA_ZOPEN4: |
| 4011 | case NFA_ZOPEN5: |
| 4012 | case NFA_ZOPEN6: |
| 4013 | case NFA_ZOPEN7: |
| 4014 | case NFA_ZOPEN8: |
| 4015 | case NFA_ZOPEN9: |
| 4016 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4017 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4018 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4019 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4020 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4021 | sub = &subs->norm; |
| 4022 | } |
| 4023 | #ifdef FEAT_SYN_HL |
| 4024 | else if (state->c >= NFA_ZOPEN) |
| 4025 | { |
| 4026 | subidx = state->c - NFA_ZOPEN; |
| 4027 | sub = &subs->synt; |
| 4028 | } |
| 4029 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4030 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4031 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4032 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4033 | sub = &subs->norm; |
| 4034 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4035 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4036 | /* Set the position (with "off" added) in the subexpression. Save |
| 4037 | * and restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | 4b6ebe6 | 2013-05-30 17:49:24 +0200 | [diff] [blame] | 4038 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4039 | if (REG_MULTI) |
| 4040 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4041 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4042 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4043 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4044 | save_in_use = -1; |
| 4045 | } |
| 4046 | else |
| 4047 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4048 | save_in_use = sub->in_use; |
| 4049 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4050 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4051 | sub->list.multi[i].start.lnum = -1; |
| 4052 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4053 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4054 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4055 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4056 | if (off == -1) |
| 4057 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4058 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 4059 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4060 | } |
| 4061 | else |
| 4062 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4063 | sub->list.multi[subidx].start.lnum = reglnum; |
| 4064 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4065 | (colnr_T)(reginput - regline + off); |
| 4066 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4067 | } |
| 4068 | else |
| 4069 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4070 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4071 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4072 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4073 | save_in_use = -1; |
| 4074 | } |
| 4075 | else |
| 4076 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4077 | save_in_use = sub->in_use; |
| 4078 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4079 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4080 | sub->list.line[i].start = NULL; |
| 4081 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4082 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4083 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4084 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4085 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4086 | } |
| 4087 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4088 | addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4089 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4090 | if (save_in_use == -1) |
| 4091 | { |
| 4092 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4093 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4094 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4095 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4096 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4097 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4098 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4099 | break; |
| 4100 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4101 | case NFA_MCLOSE: |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4102 | if (nfa_has_zend) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4103 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 4104 | /* Do not overwrite the position set by \ze. If no \ze |
| 4105 | * encountered end will be set in nfa_regtry(). */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4106 | addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4107 | break; |
| 4108 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4109 | case NFA_MCLOSE1: |
| 4110 | case NFA_MCLOSE2: |
| 4111 | case NFA_MCLOSE3: |
| 4112 | case NFA_MCLOSE4: |
| 4113 | case NFA_MCLOSE5: |
| 4114 | case NFA_MCLOSE6: |
| 4115 | case NFA_MCLOSE7: |
| 4116 | case NFA_MCLOSE8: |
| 4117 | case NFA_MCLOSE9: |
| 4118 | #ifdef FEAT_SYN_HL |
| 4119 | case NFA_ZCLOSE: |
| 4120 | case NFA_ZCLOSE1: |
| 4121 | case NFA_ZCLOSE2: |
| 4122 | case NFA_ZCLOSE3: |
| 4123 | case NFA_ZCLOSE4: |
| 4124 | case NFA_ZCLOSE5: |
| 4125 | case NFA_ZCLOSE6: |
| 4126 | case NFA_ZCLOSE7: |
| 4127 | case NFA_ZCLOSE8: |
| 4128 | case NFA_ZCLOSE9: |
| 4129 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4130 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4131 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4132 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4133 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4134 | sub = &subs->norm; |
| 4135 | } |
| 4136 | #ifdef FEAT_SYN_HL |
| 4137 | else if (state->c >= NFA_ZCLOSE) |
| 4138 | { |
| 4139 | subidx = state->c - NFA_ZCLOSE; |
| 4140 | sub = &subs->synt; |
| 4141 | } |
| 4142 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4143 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4144 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4145 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4146 | sub = &subs->norm; |
| 4147 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4148 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4149 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 4150 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4151 | save_in_use = sub->in_use; |
| 4152 | if (sub->in_use <= subidx) |
| 4153 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4154 | if (REG_MULTI) |
| 4155 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4156 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4157 | if (off == -1) |
| 4158 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4159 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 4160 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4161 | } |
| 4162 | else |
| 4163 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4164 | sub->list.multi[subidx].end.lnum = reglnum; |
| 4165 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4166 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4167 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4168 | } |
| 4169 | else |
| 4170 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4171 | save_ptr = sub->list.line[subidx].end; |
| 4172 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4173 | } |
| 4174 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4175 | addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4176 | |
| 4177 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4178 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4179 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4180 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4181 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4182 | break; |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4187 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 4188 | * Used for zero-width matches, next state to use is the added one. |
| 4189 | * This makes sure the order of states to be tried does not change, which |
| 4190 | * matters for alternatives. |
| 4191 | */ |
| 4192 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4193 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4194 | nfa_list_T *l; /* runtime state list */ |
| 4195 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4196 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4197 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4198 | int *ip; |
| 4199 | { |
| 4200 | int tlen = l->n; |
| 4201 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4202 | int listidx = *ip; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4203 | |
| 4204 | /* 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] | 4205 | addstate(l, state, subs, pim, 0); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4206 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4207 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4208 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4209 | return; |
| 4210 | |
| 4211 | /* re-order to put the new state at the current position */ |
| 4212 | count = l->n - tlen; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4213 | if (count == 1) |
| 4214 | { |
| 4215 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4216 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4217 | } |
| 4218 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4219 | { |
| 4220 | /* make space for new states, then move them from the |
| 4221 | * end to the current position */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4222 | mch_memmove(&(l->t[listidx + count]), |
| 4223 | &(l->t[listidx + 1]), |
| 4224 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 4225 | mch_memmove(&(l->t[listidx]), |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4226 | &(l->t[l->n - 1]), |
| 4227 | sizeof(nfa_thread_T) * count); |
| 4228 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4229 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4230 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4234 | * Check character class "class" against current character c. |
| 4235 | */ |
| 4236 | static int |
| 4237 | check_char_class(class, c) |
| 4238 | int class; |
| 4239 | int c; |
| 4240 | { |
| 4241 | switch (class) |
| 4242 | { |
| 4243 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4244 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4245 | return OK; |
| 4246 | break; |
| 4247 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4248 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4249 | return OK; |
| 4250 | break; |
| 4251 | case NFA_CLASS_BLANK: |
| 4252 | if (c == ' ' || c == '\t') |
| 4253 | return OK; |
| 4254 | break; |
| 4255 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4256 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4257 | return OK; |
| 4258 | break; |
| 4259 | case NFA_CLASS_DIGIT: |
| 4260 | if (VIM_ISDIGIT(c)) |
| 4261 | return OK; |
| 4262 | break; |
| 4263 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4264 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4265 | return OK; |
| 4266 | break; |
| 4267 | case NFA_CLASS_LOWER: |
| 4268 | if (MB_ISLOWER(c)) |
| 4269 | return OK; |
| 4270 | break; |
| 4271 | case NFA_CLASS_PRINT: |
| 4272 | if (vim_isprintc(c)) |
| 4273 | return OK; |
| 4274 | break; |
| 4275 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4276 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4277 | return OK; |
| 4278 | break; |
| 4279 | case NFA_CLASS_SPACE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4280 | if ((c >= 9 && c <= 13) || (c == ' ')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4281 | return OK; |
| 4282 | break; |
| 4283 | case NFA_CLASS_UPPER: |
| 4284 | if (MB_ISUPPER(c)) |
| 4285 | return OK; |
| 4286 | break; |
| 4287 | case NFA_CLASS_XDIGIT: |
| 4288 | if (vim_isxdigit(c)) |
| 4289 | return OK; |
| 4290 | break; |
| 4291 | case NFA_CLASS_TAB: |
| 4292 | if (c == '\t') |
| 4293 | return OK; |
| 4294 | break; |
| 4295 | case NFA_CLASS_RETURN: |
| 4296 | if (c == '\r') |
| 4297 | return OK; |
| 4298 | break; |
| 4299 | case NFA_CLASS_BACKSPACE: |
| 4300 | if (c == '\b') |
| 4301 | return OK; |
| 4302 | break; |
| 4303 | case NFA_CLASS_ESCAPE: |
| 4304 | if (c == '\033') |
| 4305 | return OK; |
| 4306 | break; |
| 4307 | |
| 4308 | default: |
| 4309 | /* should not be here :P */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 4310 | EMSGN("E877: (NFA regexp) Invalid character class: %ld", class); |
| 4311 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4312 | } |
| 4313 | return FAIL; |
| 4314 | } |
| 4315 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4316 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
| 4317 | |
| 4318 | /* |
| 4319 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4320 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4321 | */ |
| 4322 | static int |
| 4323 | match_backref(sub, subidx, bytelen) |
| 4324 | regsub_T *sub; /* pointers to subexpressions */ |
| 4325 | int subidx; |
| 4326 | int *bytelen; /* out: length of match in bytes */ |
| 4327 | { |
| 4328 | int len; |
| 4329 | |
| 4330 | if (sub->in_use <= subidx) |
| 4331 | { |
| 4332 | retempty: |
| 4333 | /* backref was not set, match an empty string */ |
| 4334 | *bytelen = 0; |
| 4335 | return TRUE; |
| 4336 | } |
| 4337 | |
| 4338 | if (REG_MULTI) |
| 4339 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4340 | if (sub->list.multi[subidx].start.lnum < 0 |
| 4341 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4342 | goto retempty; |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4343 | if (sub->list.multi[subidx].start.lnum == reglnum |
| 4344 | && sub->list.multi[subidx].end.lnum == reglnum) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4345 | { |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4346 | len = sub->list.multi[subidx].end.col |
| 4347 | - sub->list.multi[subidx].start.col; |
| 4348 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
| 4349 | reginput, &len) == 0) |
| 4350 | { |
| 4351 | *bytelen = len; |
| 4352 | return TRUE; |
| 4353 | } |
| 4354 | } |
| 4355 | else |
| 4356 | { |
| 4357 | if (match_with_backref( |
| 4358 | sub->list.multi[subidx].start.lnum, |
| 4359 | sub->list.multi[subidx].start.col, |
| 4360 | sub->list.multi[subidx].end.lnum, |
| 4361 | sub->list.multi[subidx].end.col, |
| 4362 | bytelen) == RA_MATCH) |
| 4363 | return TRUE; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4364 | } |
| 4365 | } |
| 4366 | else |
| 4367 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4368 | if (sub->list.line[subidx].start == NULL |
| 4369 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4370 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4371 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 4372 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4373 | { |
| 4374 | *bytelen = len; |
| 4375 | return TRUE; |
| 4376 | } |
| 4377 | } |
| 4378 | return FALSE; |
| 4379 | } |
| 4380 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4381 | #ifdef FEAT_SYN_HL |
| 4382 | |
| 4383 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 4384 | |
| 4385 | /* |
| 4386 | * Check for a match with \z subexpression "subidx". |
| 4387 | * Return TRUE if it matches. |
| 4388 | */ |
| 4389 | static int |
| 4390 | match_zref(subidx, bytelen) |
| 4391 | int subidx; |
| 4392 | int *bytelen; /* out: length of match in bytes */ |
| 4393 | { |
| 4394 | int len; |
| 4395 | |
| 4396 | cleanup_zsubexpr(); |
| 4397 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 4398 | { |
| 4399 | /* backref was not set, match an empty string */ |
| 4400 | *bytelen = 0; |
| 4401 | return TRUE; |
| 4402 | } |
| 4403 | |
| 4404 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 4405 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 4406 | { |
| 4407 | *bytelen = len; |
| 4408 | return TRUE; |
| 4409 | } |
| 4410 | return FALSE; |
| 4411 | } |
| 4412 | #endif |
| 4413 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4414 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4415 | * Save list IDs for all NFA states of "prog" into "list". |
| 4416 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4417 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4418 | */ |
| 4419 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4420 | nfa_save_listids(prog, list) |
| 4421 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4422 | int *list; |
| 4423 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4424 | int i; |
| 4425 | nfa_state_T *p; |
| 4426 | |
| 4427 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 4428 | p = &prog->state[0]; |
| 4429 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4430 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4431 | list[i] = p->lastlist[1]; |
| 4432 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4433 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4434 | } |
| 4435 | } |
| 4436 | |
| 4437 | /* |
| 4438 | * Restore list IDs from "list" to all NFA states. |
| 4439 | */ |
| 4440 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4441 | nfa_restore_listids(prog, list) |
| 4442 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4443 | int *list; |
| 4444 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4445 | int i; |
| 4446 | nfa_state_T *p; |
| 4447 | |
| 4448 | p = &prog->state[0]; |
| 4449 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4450 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4451 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4452 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4453 | } |
| 4454 | } |
| 4455 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4456 | static int |
| 4457 | nfa_re_num_cmp(val, op, pos) |
| 4458 | long_u val; |
| 4459 | int op; |
| 4460 | long_u pos; |
| 4461 | { |
| 4462 | if (op == 1) return pos > val; |
| 4463 | if (op == 2) return pos < val; |
| 4464 | return val == pos; |
| 4465 | } |
| 4466 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4467 | 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] | 4468 | 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] | 4469 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4470 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4471 | * Recursively call nfa_regmatch() |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4472 | * "pim" is NULL or contains info about a Postponed Invisible Match (start |
| 4473 | * position). |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4474 | */ |
| 4475 | static int |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4476 | recursive_regmatch(state, pim, prog, submatch, m, listids) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4477 | nfa_state_T *state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4478 | nfa_pim_T *pim; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4479 | nfa_regprog_T *prog; |
| 4480 | regsubs_T *submatch; |
| 4481 | regsubs_T *m; |
| 4482 | int **listids; |
| 4483 | { |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4484 | int save_reginput_col = (int)(reginput - regline); |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4485 | int save_reglnum = reglnum; |
| 4486 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4487 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4488 | save_se_T *save_nfa_endp = nfa_endp; |
| 4489 | save_se_T endpos; |
| 4490 | save_se_T *endposp = NULL; |
| 4491 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4492 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4493 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4494 | if (pim != NULL) |
| 4495 | { |
| 4496 | /* start at the position where the postponed match was */ |
| 4497 | if (REG_MULTI) |
| 4498 | reginput = regline + pim->end.pos.col; |
| 4499 | else |
| 4500 | reginput = pim->end.ptr; |
| 4501 | } |
| 4502 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4503 | if (state->c == NFA_START_INVISIBLE_BEFORE |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4504 | || state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 4505 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG |
| 4506 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4507 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4508 | /* The recursive match must end at the current position. When "pim" is |
| 4509 | * not NULL it specifies the current position. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4510 | endposp = &endpos; |
| 4511 | if (REG_MULTI) |
| 4512 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4513 | if (pim == NULL) |
| 4514 | { |
| 4515 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 4516 | endpos.se_u.pos.lnum = reglnum; |
| 4517 | } |
| 4518 | else |
| 4519 | endpos.se_u.pos = pim->end.pos; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4520 | } |
| 4521 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4522 | { |
| 4523 | if (pim == NULL) |
| 4524 | endpos.se_u.ptr = reginput; |
| 4525 | else |
| 4526 | endpos.se_u.ptr = pim->end.ptr; |
| 4527 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4528 | |
| 4529 | /* Go back the specified number of bytes, or as far as the |
| 4530 | * start of the previous line, to try matching "\@<=" or |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4531 | * not matching "\@<!". This is very ineffecient, limit the number of |
| 4532 | * bytes if possible. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4533 | if (state->val <= 0) |
| 4534 | { |
| 4535 | if (REG_MULTI) |
| 4536 | { |
| 4537 | regline = reg_getline(--reglnum); |
| 4538 | if (regline == NULL) |
| 4539 | /* can't go before the first line */ |
| 4540 | regline = reg_getline(++reglnum); |
| 4541 | } |
| 4542 | reginput = regline; |
| 4543 | } |
| 4544 | else |
| 4545 | { |
| 4546 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 4547 | { |
| 4548 | /* Not enough bytes in this line, go to end of |
| 4549 | * previous line. */ |
| 4550 | regline = reg_getline(--reglnum); |
| 4551 | if (regline == NULL) |
| 4552 | { |
| 4553 | /* can't go before the first line */ |
| 4554 | regline = reg_getline(++reglnum); |
| 4555 | reginput = regline; |
| 4556 | } |
| 4557 | else |
| 4558 | reginput = regline + STRLEN(regline); |
| 4559 | } |
| 4560 | if ((int)(reginput - regline) >= state->val) |
| 4561 | { |
| 4562 | reginput -= state->val; |
| 4563 | #ifdef FEAT_MBYTE |
| 4564 | if (has_mbyte) |
| 4565 | reginput -= mb_head_off(regline, reginput); |
| 4566 | #endif |
| 4567 | } |
| 4568 | else |
| 4569 | reginput = regline; |
| 4570 | } |
| 4571 | } |
| 4572 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4573 | #ifdef ENABLE_LOG |
| 4574 | if (log_fd != stderr) |
| 4575 | fclose(log_fd); |
| 4576 | log_fd = NULL; |
| 4577 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4578 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 4579 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 4580 | if (nfa_ll_index == 1) |
| 4581 | { |
| 4582 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 4583 | * values and clear them. */ |
| 4584 | if (*listids == NULL) |
| 4585 | { |
| 4586 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 4587 | if (*listids == NULL) |
| 4588 | { |
| 4589 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 4590 | return 0; |
| 4591 | } |
| 4592 | } |
| 4593 | nfa_save_listids(prog, *listids); |
| 4594 | need_restore = TRUE; |
| 4595 | /* any value of nfa_listid will do */ |
| 4596 | } |
| 4597 | else |
| 4598 | { |
| 4599 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 4600 | * entry. Make sure nfa_listid is different from a previous recursive |
| 4601 | * call, because some states may still have this ID. */ |
| 4602 | ++nfa_ll_index; |
| 4603 | if (nfa_listid <= nfa_alt_listid) |
| 4604 | nfa_listid = nfa_alt_listid; |
| 4605 | } |
| 4606 | |
| 4607 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 4608 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4609 | nfa_endp = endposp; |
| 4610 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4611 | |
| 4612 | if (need_restore) |
| 4613 | nfa_restore_listids(prog, *listids); |
| 4614 | else |
| 4615 | { |
| 4616 | --nfa_ll_index; |
| 4617 | nfa_alt_listid = nfa_listid; |
| 4618 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4619 | |
| 4620 | /* restore position in input text */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4621 | reglnum = save_reglnum; |
Bram Moolenaar | 484d241 | 2013-06-13 19:47:07 +0200 | [diff] [blame] | 4622 | if (REG_MULTI) |
| 4623 | regline = reg_getline(reglnum); |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4624 | reginput = regline + save_reginput_col; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4625 | nfa_match = save_nfa_match; |
| 4626 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4627 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4628 | |
| 4629 | #ifdef ENABLE_LOG |
| 4630 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 4631 | if (log_fd != NULL) |
| 4632 | { |
| 4633 | fprintf(log_fd, "****************************\n"); |
| 4634 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 4635 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 4636 | fprintf(log_fd, "****************************\n"); |
| 4637 | } |
| 4638 | else |
| 4639 | { |
| 4640 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4641 | log_fd = stderr; |
| 4642 | } |
| 4643 | #endif |
| 4644 | |
| 4645 | return result; |
| 4646 | } |
| 4647 | |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 4648 | static int skip_to_start __ARGS((int c, colnr_T *colp)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4649 | 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] | 4650 | |
| 4651 | /* |
| 4652 | * Estimate the chance of a match with "state" failing. |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 4653 | * empty match: 0 |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4654 | * NFA_ANY: 1 |
| 4655 | * specific character: 99 |
| 4656 | */ |
| 4657 | static int |
| 4658 | failure_chance(state, depth) |
| 4659 | nfa_state_T *state; |
| 4660 | int depth; |
| 4661 | { |
| 4662 | int c = state->c; |
| 4663 | int l, r; |
| 4664 | |
| 4665 | /* detect looping */ |
| 4666 | if (depth > 4) |
| 4667 | return 1; |
| 4668 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4669 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4670 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4671 | case NFA_SPLIT: |
| 4672 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 4673 | /* avoid recursive stuff */ |
| 4674 | return 1; |
| 4675 | /* two alternatives, use the lowest failure chance */ |
| 4676 | l = failure_chance(state->out, depth + 1); |
| 4677 | r = failure_chance(state->out1, depth + 1); |
| 4678 | return l < r ? l : r; |
| 4679 | |
| 4680 | case NFA_ANY: |
| 4681 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4682 | return 1; |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 4683 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4684 | case NFA_MATCH: |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 4685 | case NFA_MCLOSE: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4686 | /* empty match works always */ |
| 4687 | return 0; |
| 4688 | |
| 4689 | case NFA_BOL: |
| 4690 | case NFA_EOL: |
| 4691 | case NFA_BOF: |
| 4692 | case NFA_EOF: |
| 4693 | case NFA_NEWL: |
| 4694 | return 99; |
| 4695 | |
| 4696 | case NFA_BOW: |
| 4697 | case NFA_EOW: |
| 4698 | return 90; |
| 4699 | |
| 4700 | case NFA_MOPEN: |
| 4701 | case NFA_MOPEN1: |
| 4702 | case NFA_MOPEN2: |
| 4703 | case NFA_MOPEN3: |
| 4704 | case NFA_MOPEN4: |
| 4705 | case NFA_MOPEN5: |
| 4706 | case NFA_MOPEN6: |
| 4707 | case NFA_MOPEN7: |
| 4708 | case NFA_MOPEN8: |
| 4709 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4710 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4711 | case NFA_ZOPEN: |
| 4712 | case NFA_ZOPEN1: |
| 4713 | case NFA_ZOPEN2: |
| 4714 | case NFA_ZOPEN3: |
| 4715 | case NFA_ZOPEN4: |
| 4716 | case NFA_ZOPEN5: |
| 4717 | case NFA_ZOPEN6: |
| 4718 | case NFA_ZOPEN7: |
| 4719 | case NFA_ZOPEN8: |
| 4720 | case NFA_ZOPEN9: |
| 4721 | case NFA_ZCLOSE: |
| 4722 | case NFA_ZCLOSE1: |
| 4723 | case NFA_ZCLOSE2: |
| 4724 | case NFA_ZCLOSE3: |
| 4725 | case NFA_ZCLOSE4: |
| 4726 | case NFA_ZCLOSE5: |
| 4727 | case NFA_ZCLOSE6: |
| 4728 | case NFA_ZCLOSE7: |
| 4729 | case NFA_ZCLOSE8: |
| 4730 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 4731 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4732 | case NFA_NOPEN: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4733 | case NFA_MCLOSE1: |
| 4734 | case NFA_MCLOSE2: |
| 4735 | case NFA_MCLOSE3: |
| 4736 | case NFA_MCLOSE4: |
| 4737 | case NFA_MCLOSE5: |
| 4738 | case NFA_MCLOSE6: |
| 4739 | case NFA_MCLOSE7: |
| 4740 | case NFA_MCLOSE8: |
| 4741 | case NFA_MCLOSE9: |
| 4742 | case NFA_NCLOSE: |
| 4743 | return failure_chance(state->out, depth + 1); |
| 4744 | |
| 4745 | case NFA_BACKREF1: |
| 4746 | case NFA_BACKREF2: |
| 4747 | case NFA_BACKREF3: |
| 4748 | case NFA_BACKREF4: |
| 4749 | case NFA_BACKREF5: |
| 4750 | case NFA_BACKREF6: |
| 4751 | case NFA_BACKREF7: |
| 4752 | case NFA_BACKREF8: |
| 4753 | case NFA_BACKREF9: |
| 4754 | #ifdef FEAT_SYN_HL |
| 4755 | case NFA_ZREF1: |
| 4756 | case NFA_ZREF2: |
| 4757 | case NFA_ZREF3: |
| 4758 | case NFA_ZREF4: |
| 4759 | case NFA_ZREF5: |
| 4760 | case NFA_ZREF6: |
| 4761 | case NFA_ZREF7: |
| 4762 | case NFA_ZREF8: |
| 4763 | case NFA_ZREF9: |
| 4764 | #endif |
| 4765 | /* backreferences don't match in many places */ |
| 4766 | return 94; |
| 4767 | |
| 4768 | case NFA_LNUM_GT: |
| 4769 | case NFA_LNUM_LT: |
| 4770 | case NFA_COL_GT: |
| 4771 | case NFA_COL_LT: |
| 4772 | case NFA_VCOL_GT: |
| 4773 | case NFA_VCOL_LT: |
| 4774 | case NFA_MARK_GT: |
| 4775 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4776 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 4777 | /* before/after positions don't match very often */ |
| 4778 | return 85; |
| 4779 | |
| 4780 | case NFA_LNUM: |
| 4781 | return 90; |
| 4782 | |
| 4783 | case NFA_CURSOR: |
| 4784 | case NFA_COL: |
| 4785 | case NFA_VCOL: |
| 4786 | case NFA_MARK: |
| 4787 | /* specific positions rarely match */ |
| 4788 | return 98; |
| 4789 | |
| 4790 | case NFA_COMPOSING: |
| 4791 | return 95; |
| 4792 | |
| 4793 | default: |
| 4794 | if (c > 0) |
| 4795 | /* character match fails often */ |
| 4796 | return 95; |
| 4797 | } |
| 4798 | |
| 4799 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4800 | return 50; |
| 4801 | } |
| 4802 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4803 | /* |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 4804 | * Skip until the char "c" we know a match must start with. |
| 4805 | */ |
| 4806 | static int |
| 4807 | skip_to_start(c, colp) |
| 4808 | int c; |
| 4809 | colnr_T *colp; |
| 4810 | { |
| 4811 | char_u *s; |
| 4812 | |
| 4813 | /* Used often, do some work to avoid call overhead. */ |
| 4814 | if (!ireg_ic |
| 4815 | #ifdef FEAT_MBYTE |
| 4816 | && !has_mbyte |
| 4817 | #endif |
| 4818 | ) |
| 4819 | s = vim_strbyte(regline + *colp, c); |
| 4820 | else |
| 4821 | s = cstrchr(regline + *colp, c); |
| 4822 | if (s == NULL) |
| 4823 | return FAIL; |
| 4824 | *colp = (int)(s - regline); |
| 4825 | return OK; |
| 4826 | } |
| 4827 | |
| 4828 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4829 | * Check for a match with match_text. |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4830 | * Called after skip_to_start() has found regstart. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4831 | * Returns zero for no match, 1 for a match. |
| 4832 | */ |
| 4833 | static long |
| 4834 | find_match_text(startcol, regstart, match_text) |
| 4835 | colnr_T startcol; |
| 4836 | int regstart; |
| 4837 | char_u *match_text; |
| 4838 | { |
| 4839 | colnr_T col = startcol; |
| 4840 | int c1, c2; |
| 4841 | int len1, len2; |
| 4842 | int match; |
| 4843 | |
| 4844 | for (;;) |
| 4845 | { |
| 4846 | match = TRUE; |
| 4847 | len2 = MB_CHAR2LEN(regstart); /* skip regstart */ |
| 4848 | for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
| 4849 | { |
| 4850 | c1 = PTR2CHAR(match_text + len1); |
| 4851 | c2 = PTR2CHAR(regline + col + len2); |
| 4852 | if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
| 4853 | { |
| 4854 | match = FALSE; |
| 4855 | break; |
| 4856 | } |
| 4857 | len2 += MB_CHAR2LEN(c2); |
| 4858 | } |
| 4859 | if (match |
| 4860 | #ifdef FEAT_MBYTE |
| 4861 | /* check that no composing char follows */ |
| 4862 | && !(enc_utf8 |
| 4863 | && utf_iscomposing(PTR2CHAR(regline + col + len2))) |
| 4864 | #endif |
| 4865 | ) |
| 4866 | { |
| 4867 | cleanup_subexpr(); |
| 4868 | if (REG_MULTI) |
| 4869 | { |
| 4870 | reg_startpos[0].lnum = reglnum; |
| 4871 | reg_startpos[0].col = col; |
| 4872 | reg_endpos[0].lnum = reglnum; |
| 4873 | reg_endpos[0].col = col + len2; |
| 4874 | } |
| 4875 | else |
| 4876 | { |
| 4877 | reg_startp[0] = regline + col; |
| 4878 | reg_endp[0] = regline + col + len2; |
| 4879 | } |
| 4880 | return 1L; |
| 4881 | } |
| 4882 | |
| 4883 | /* Try finding regstart after the current match. */ |
| 4884 | col += MB_CHAR2LEN(regstart); /* skip regstart */ |
| 4885 | if (skip_to_start(regstart, &col) == FAIL) |
| 4886 | break; |
| 4887 | } |
| 4888 | return 0L; |
| 4889 | } |
| 4890 | |
| 4891 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4892 | * Main matching routine. |
| 4893 | * |
| 4894 | * Run NFA to determine whether it matches reginput. |
| 4895 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4896 | * 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] | 4897 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4898 | * Return TRUE if there is a match, FALSE otherwise. |
| 4899 | * Note: Caller must ensure that: start != NULL. |
| 4900 | */ |
| 4901 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4902 | nfa_regmatch(prog, start, submatch, m) |
| 4903 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4904 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4905 | regsubs_T *submatch; |
| 4906 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4907 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4908 | int result; |
| 4909 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4910 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4911 | int go_to_nextline = FALSE; |
| 4912 | nfa_thread_T *t; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4913 | nfa_list_T list[2]; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4914 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4915 | nfa_list_T *thislist; |
| 4916 | nfa_list_T *nextlist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4917 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4918 | nfa_state_T *add_state; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 4919 | int add_here; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4920 | int add_count; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 4921 | int add_off = 0; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4922 | int toplevel = start->c == NFA_MOPEN; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4923 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4924 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4925 | |
| 4926 | if (debug == NULL) |
| 4927 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4928 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4929 | return FALSE; |
| 4930 | } |
| 4931 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4932 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4933 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4934 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4935 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4936 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4937 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 4938 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4939 | list[1].len = nstate + 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4940 | if (list[0].t == NULL || list[1].t == NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4941 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4942 | |
| 4943 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4944 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4945 | if (log_fd != NULL) |
| 4946 | { |
| 4947 | fprintf(log_fd, "**********************************\n"); |
| 4948 | nfa_set_code(start->c); |
| 4949 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 4950 | abs(start->id), code); |
| 4951 | fprintf(log_fd, "**********************************\n"); |
| 4952 | } |
| 4953 | else |
| 4954 | { |
| 4955 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 4956 | log_fd = stderr; |
| 4957 | } |
| 4958 | #endif |
| 4959 | |
| 4960 | thislist = &list[0]; |
| 4961 | thislist->n = 0; |
| 4962 | nextlist = &list[1]; |
| 4963 | nextlist->n = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4964 | #ifdef ENABLE_LOG |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4965 | fprintf(log_fd, "(---) STARTSTATE first\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4966 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4967 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4968 | |
| 4969 | /* Inline optimized code for addstate(thislist, start, m, 0) if we know |
| 4970 | * it's the first MOPEN. */ |
| 4971 | if (toplevel) |
| 4972 | { |
| 4973 | if (REG_MULTI) |
| 4974 | { |
| 4975 | m->norm.list.multi[0].start.lnum = reglnum; |
| 4976 | m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline); |
| 4977 | } |
| 4978 | else |
| 4979 | m->norm.list.line[0].start = reginput; |
| 4980 | m->norm.in_use = 1; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4981 | addstate(thislist, start->out, m, NULL, 0); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 4982 | } |
| 4983 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4984 | addstate(thislist, start, m, NULL, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4985 | |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 4986 | #define ADD_STATE_IF_MATCH(state) \ |
| 4987 | if (result) { \ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4988 | add_state = state->out; \ |
| 4989 | add_off = clen; \ |
| 4990 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4991 | |
| 4992 | /* |
| 4993 | * Run for each character. |
| 4994 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4995 | for (;;) |
| 4996 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4997 | int curc; |
| 4998 | int clen; |
| 4999 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5000 | #ifdef FEAT_MBYTE |
| 5001 | if (has_mbyte) |
| 5002 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5003 | curc = (*mb_ptr2char)(reginput); |
| 5004 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5005 | } |
| 5006 | else |
| 5007 | #endif |
| 5008 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5009 | curc = *reginput; |
| 5010 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5011 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5012 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5013 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5014 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5015 | go_to_nextline = FALSE; |
| 5016 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5017 | |
| 5018 | /* swap lists */ |
| 5019 | thislist = &list[flag]; |
| 5020 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5021 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5022 | ++nfa_listid; |
| 5023 | thislist->id = nfa_listid; |
| 5024 | nextlist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5025 | |
| 5026 | #ifdef ENABLE_LOG |
| 5027 | fprintf(log_fd, "------------------------------------------\n"); |
| 5028 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5029 | 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] | 5030 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5031 | { |
| 5032 | int i; |
| 5033 | |
| 5034 | for (i = 0; i < thislist->n; i++) |
| 5035 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5036 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5037 | fprintf(log_fd, "\n"); |
| 5038 | #endif |
| 5039 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5040 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5041 | fprintf(debug, "\n-------------------\n"); |
| 5042 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5043 | /* |
| 5044 | * If the state lists are empty we can stop. |
| 5045 | */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5046 | if (thislist->n == 0) |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5047 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5048 | |
| 5049 | /* compute nextlist */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5050 | for (listidx = 0; listidx < thislist->n; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5051 | { |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5052 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5053 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5054 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5055 | nfa_set_code(t->state->c); |
| 5056 | fprintf(debug, "%s, ", code); |
| 5057 | #endif |
| 5058 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5059 | { |
| 5060 | int col; |
| 5061 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5062 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5063 | col = -1; |
| 5064 | else if (REG_MULTI) |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5065 | col = t->subs.norm.list.multi[0].start.col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5066 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5067 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5068 | nfa_set_code(t->state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5069 | fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n", |
| 5070 | abs(t->state->id), (int)t->state->c, code, col, |
| 5071 | pim_info(&t->pim)); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5072 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5073 | #endif |
| 5074 | |
| 5075 | /* |
| 5076 | * Handle the possible codes of the current state. |
| 5077 | * The most important is NFA_MATCH. |
| 5078 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5079 | add_state = NULL; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5080 | add_here = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5081 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5082 | switch (t->state->c) |
| 5083 | { |
| 5084 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5085 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5086 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5087 | copy_sub(&submatch->norm, &t->subs.norm); |
| 5088 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5089 | if (nfa_has_zsubexpr) |
| 5090 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5091 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5092 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5093 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5094 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5095 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5096 | * states at this position. When the list of states is going |
| 5097 | * to be empty quit without advancing, so that "reginput" is |
| 5098 | * correct. */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5099 | if (nextlist->n == 0) |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5100 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5101 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5102 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5103 | |
| 5104 | case NFA_END_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5105 | case NFA_END_INVISIBLE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5106 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5107 | /* |
| 5108 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5109 | * NFA_START_INVISIBLE_BEFORE node. |
| 5110 | * They surround a zero-width group, used with "\@=", "\&", |
| 5111 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5112 | * If we got here, it means that the current "invisible" group |
| 5113 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5114 | * nfa_regmatch(). For a look-behind match only when it ends |
| 5115 | * in the position in "nfa_endp". |
| 5116 | * Submatches are stored in *m, and used in the parent call. |
| 5117 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5118 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5119 | if (nfa_endp != NULL) |
| 5120 | { |
| 5121 | if (REG_MULTI) |
| 5122 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 5123 | (int)reglnum, |
| 5124 | (int)nfa_endp->se_u.pos.lnum, |
| 5125 | (int)(reginput - regline), |
| 5126 | nfa_endp->se_u.pos.col); |
| 5127 | else |
| 5128 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 5129 | (int)(reginput - regline), |
| 5130 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5131 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5132 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5133 | /* If "nfa_endp" is set it's only a match if it ends at |
| 5134 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5135 | if (nfa_endp != NULL && (REG_MULTI |
| 5136 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 5137 | || (int)(reginput - regline) |
| 5138 | != nfa_endp->se_u.pos.col) |
| 5139 | : reginput != nfa_endp->se_u.ptr)) |
| 5140 | break; |
| 5141 | |
| 5142 | /* do not set submatches for \@! */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5143 | if (t->state->c != NFA_END_INVISIBLE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5144 | { |
| 5145 | copy_sub(&m->norm, &t->subs.norm); |
| 5146 | #ifdef FEAT_SYN_HL |
| 5147 | if (nfa_has_zsubexpr) |
| 5148 | copy_sub(&m->synt, &t->subs.synt); |
| 5149 | #endif |
| 5150 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5151 | #ifdef ENABLE_LOG |
| 5152 | fprintf(log_fd, "Match found:\n"); |
| 5153 | log_subsexpr(m); |
| 5154 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5155 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5156 | break; |
| 5157 | |
| 5158 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5159 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5160 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5161 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5162 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5163 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5164 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5165 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5166 | { |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5167 | #ifdef ENABLE_LOG |
| 5168 | fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", |
| 5169 | failure_chance(t->state->out, 0), |
| 5170 | failure_chance(t->state->out1->out, 0)); |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5171 | #endif |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5172 | /* Do it directly if there already is a PIM or when |
| 5173 | * nfa_postprocess() detected it will work better. */ |
| 5174 | if (t->pim.result != NFA_PIM_UNUSED |
| 5175 | || t->state->c == NFA_START_INVISIBLE_FIRST |
| 5176 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5177 | || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 5178 | || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5179 | { |
| 5180 | /* |
| 5181 | * First try matching the invisible match, then what |
| 5182 | * follows. |
| 5183 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5184 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5185 | submatch, m, &listids); |
| 5186 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5187 | /* for \@! and \@<! it is a match when the result is |
| 5188 | * FALSE */ |
| 5189 | if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5190 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5191 | || t->state->c |
| 5192 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5193 | || t->state->c |
| 5194 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5195 | { |
| 5196 | /* Copy submatch info from the recursive call */ |
| 5197 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5198 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5199 | if (nfa_has_zsubexpr) |
| 5200 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5201 | #endif |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5202 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5203 | /* t->state->out1 is the corresponding |
| 5204 | * END_INVISIBLE node; Add its out to the current |
| 5205 | * list (zero-width match). */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5206 | add_here = TRUE; |
| 5207 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5208 | } |
| 5209 | } |
| 5210 | else |
| 5211 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5212 | nfa_pim_T pim; |
| 5213 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5214 | /* |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5215 | * First try matching what follows. Only if a match |
| 5216 | * is found verify the invisible match matches. Add a |
| 5217 | * nfa_pim_T to the following states, it contains info |
| 5218 | * about the invisible match. |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5219 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5220 | pim.state = t->state; |
| 5221 | pim.result = NFA_PIM_TODO; |
| 5222 | pim.subs.norm.in_use = 0; |
| 5223 | #ifdef FEAT_SYN_HL |
| 5224 | pim.subs.synt.in_use = 0; |
| 5225 | #endif |
| 5226 | if (REG_MULTI) |
| 5227 | { |
| 5228 | pim.end.pos.col = (int)(reginput - regline); |
| 5229 | pim.end.pos.lnum = reglnum; |
| 5230 | } |
| 5231 | else |
| 5232 | pim.end.ptr = reginput; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5233 | |
| 5234 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 5235 | * node; Add its out to the current list (zero-width |
| 5236 | * match). */ |
| 5237 | addstate_here(thislist, t->state->out1->out, &t->subs, |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5238 | &pim, &listidx); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5239 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5240 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5241 | break; |
| 5242 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5243 | case NFA_START_PATTERN: |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5244 | { |
| 5245 | nfa_state_T *skip = NULL; |
| 5246 | #ifdef ENABLE_LOG |
| 5247 | int skip_lid = 0; |
| 5248 | #endif |
| 5249 | |
| 5250 | /* There is no point in trying to match the pattern if the |
| 5251 | * output state is not going to be added to the list. */ |
| 5252 | if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
| 5253 | { |
| 5254 | skip = t->state->out1->out; |
| 5255 | #ifdef ENABLE_LOG |
| 5256 | skip_lid = nextlist->id; |
| 5257 | #endif |
| 5258 | } |
| 5259 | else if (state_in_list(nextlist, |
| 5260 | t->state->out1->out->out, &t->subs)) |
| 5261 | { |
| 5262 | skip = t->state->out1->out->out; |
| 5263 | #ifdef ENABLE_LOG |
| 5264 | skip_lid = nextlist->id; |
| 5265 | #endif |
| 5266 | } |
| 5267 | else if(state_in_list(thislist, |
| 5268 | t->state->out1->out->out, &t->subs)) |
| 5269 | { |
| 5270 | skip = t->state->out1->out->out; |
| 5271 | #ifdef ENABLE_LOG |
| 5272 | skip_lid = thislist->id; |
| 5273 | #endif |
| 5274 | } |
| 5275 | if (skip != NULL) |
| 5276 | { |
| 5277 | #ifdef ENABLE_LOG |
| 5278 | nfa_set_code(skip->c); |
| 5279 | fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
| 5280 | abs(skip->id), skip_lid, skip->c, code); |
| 5281 | #endif |
| 5282 | break; |
| 5283 | } |
| 5284 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5285 | /* First try matching the pattern. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5286 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5287 | submatch, m, &listids); |
| 5288 | if (result) |
| 5289 | { |
| 5290 | int bytelen; |
| 5291 | |
| 5292 | #ifdef ENABLE_LOG |
| 5293 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 5294 | log_subsexpr(m); |
| 5295 | #endif |
| 5296 | /* Copy submatch info from the recursive call */ |
| 5297 | copy_sub_off(&t->subs.norm, &m->norm); |
| 5298 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5299 | if (nfa_has_zsubexpr) |
| 5300 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5301 | #endif |
| 5302 | /* Now we need to skip over the matched text and then |
| 5303 | * continue with what follows. */ |
| 5304 | if (REG_MULTI) |
| 5305 | /* TODO: multi-line match */ |
| 5306 | bytelen = m->norm.list.multi[0].end.col |
| 5307 | - (int)(reginput - regline); |
| 5308 | else |
| 5309 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 5310 | |
| 5311 | #ifdef ENABLE_LOG |
| 5312 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 5313 | #endif |
| 5314 | if (bytelen == 0) |
| 5315 | { |
| 5316 | /* empty match, output of corresponding |
| 5317 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 5318 | * position */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5319 | add_here = TRUE; |
| 5320 | add_state = t->state->out1->out->out; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5321 | } |
| 5322 | else if (bytelen <= clen) |
| 5323 | { |
| 5324 | /* match current character, output of corresponding |
| 5325 | * NFA_END_PATTERN to be used at next position. */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5326 | add_state = t->state->out1->out->out; |
| 5327 | add_off = clen; |
| 5328 | } |
| 5329 | else |
| 5330 | { |
| 5331 | /* skip over the matched characters, set character |
| 5332 | * count in NFA_SKIP */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5333 | add_state = t->state->out1->out; |
| 5334 | add_off = bytelen; |
| 5335 | add_count = bytelen - clen; |
| 5336 | } |
| 5337 | } |
| 5338 | break; |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5339 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5340 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5341 | case NFA_BOL: |
| 5342 | if (reginput == regline) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5343 | { |
| 5344 | add_here = TRUE; |
| 5345 | add_state = t->state->out; |
| 5346 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5347 | break; |
| 5348 | |
| 5349 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5350 | if (curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5351 | { |
| 5352 | add_here = TRUE; |
| 5353 | add_state = t->state->out; |
| 5354 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5355 | break; |
| 5356 | |
| 5357 | case NFA_BOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5358 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5359 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5360 | if (curc == NUL) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5361 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5362 | #ifdef FEAT_MBYTE |
| 5363 | else if (has_mbyte) |
| 5364 | { |
| 5365 | int this_class; |
| 5366 | |
| 5367 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5368 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5369 | if (this_class <= 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5370 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5371 | else if (reg_prev_class() == this_class) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5372 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5373 | } |
| 5374 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5375 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5376 | || (reginput > regline |
| 5377 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5378 | result = FALSE; |
| 5379 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5380 | { |
| 5381 | add_here = TRUE; |
| 5382 | add_state = t->state->out; |
| 5383 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5384 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5385 | |
| 5386 | case NFA_EOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5387 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5388 | if (reginput == regline) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5389 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5390 | #ifdef FEAT_MBYTE |
| 5391 | else if (has_mbyte) |
| 5392 | { |
| 5393 | int this_class, prev_class; |
| 5394 | |
| 5395 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5396 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5397 | prev_class = reg_prev_class(); |
| 5398 | if (this_class == prev_class |
| 5399 | || prev_class == 0 || prev_class == 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5400 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5401 | } |
| 5402 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5403 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5404 | || (reginput[0] != NUL |
| 5405 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5406 | result = FALSE; |
| 5407 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5408 | { |
| 5409 | add_here = TRUE; |
| 5410 | add_state = t->state->out; |
| 5411 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5412 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5413 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5414 | case NFA_BOF: |
| 5415 | if (reglnum == 0 && reginput == regline |
| 5416 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5417 | { |
| 5418 | add_here = TRUE; |
| 5419 | add_state = t->state->out; |
| 5420 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5421 | break; |
| 5422 | |
| 5423 | case NFA_EOF: |
| 5424 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5425 | { |
| 5426 | add_here = TRUE; |
| 5427 | add_state = t->state->out; |
| 5428 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5429 | break; |
| 5430 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5431 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5432 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5433 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5434 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5435 | int len = 0; |
| 5436 | nfa_state_T *end; |
| 5437 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5438 | int cchars[MAX_MCO]; |
| 5439 | int ccount = 0; |
| 5440 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5441 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5442 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5443 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5444 | if (utf_iscomposing(sta->c)) |
| 5445 | { |
| 5446 | /* Only match composing character(s), ignore base |
| 5447 | * character. Used for ".{composing}" and "{composing}" |
| 5448 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5449 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5450 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 5451 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5452 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5453 | /* If \Z was present, then ignore composing characters. |
| 5454 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5455 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5456 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5457 | else |
| 5458 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5459 | while (sta->c != NFA_END_COMPOSING) |
| 5460 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5461 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5462 | |
| 5463 | /* Check base character matches first, unless ignored. */ |
| 5464 | else if (len > 0 || mc == sta->c) |
| 5465 | { |
| 5466 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5467 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5468 | len += mb_char2len(mc); |
| 5469 | sta = sta->out; |
| 5470 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5471 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5472 | /* We don't care about the order of composing characters. |
| 5473 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5474 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5475 | { |
| 5476 | mc = mb_ptr2char(reginput + len); |
| 5477 | cchars[ccount++] = mc; |
| 5478 | len += mb_char2len(mc); |
| 5479 | if (ccount == MAX_MCO) |
| 5480 | break; |
| 5481 | } |
| 5482 | |
| 5483 | /* Check that each composing char in the pattern matches a |
| 5484 | * composing char in the text. We do not check if all |
| 5485 | * composing chars are matched. */ |
| 5486 | result = OK; |
| 5487 | while (sta->c != NFA_END_COMPOSING) |
| 5488 | { |
| 5489 | for (j = 0; j < ccount; ++j) |
| 5490 | if (cchars[j] == sta->c) |
| 5491 | break; |
| 5492 | if (j == ccount) |
| 5493 | { |
| 5494 | result = FAIL; |
| 5495 | break; |
| 5496 | } |
| 5497 | sta = sta->out; |
| 5498 | } |
| 5499 | } |
| 5500 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 5501 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5502 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5503 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5504 | ADD_STATE_IF_MATCH(end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5505 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5506 | } |
| 5507 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5508 | |
| 5509 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5510 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 5511 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5512 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5513 | go_to_nextline = TRUE; |
| 5514 | /* Pass -1 for the offset, which means taking the position |
| 5515 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5516 | add_state = t->state->out; |
| 5517 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5518 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5519 | else if (curc == '\n' && reg_line_lbr) |
| 5520 | { |
| 5521 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5522 | add_state = t->state->out; |
| 5523 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5524 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5525 | break; |
| 5526 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5527 | case NFA_START_COLL: |
| 5528 | case NFA_START_NEG_COLL: |
| 5529 | { |
| 5530 | /* What follows is a list of characters, until NFA_END_COLL. |
| 5531 | * One of them must match or none of them must match. */ |
| 5532 | nfa_state_T *state; |
| 5533 | int result_if_matched; |
| 5534 | int c1, c2; |
| 5535 | |
| 5536 | /* Never match EOL. If it's part of the collection it is added |
| 5537 | * as a separate state with an OR. */ |
| 5538 | if (curc == NUL) |
| 5539 | break; |
| 5540 | |
| 5541 | state = t->state->out; |
| 5542 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 5543 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5544 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5545 | if (state->c == NFA_END_COLL) |
| 5546 | { |
| 5547 | result = !result_if_matched; |
| 5548 | break; |
| 5549 | } |
| 5550 | if (state->c == NFA_RANGE_MIN) |
| 5551 | { |
| 5552 | c1 = state->val; |
| 5553 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 5554 | c2 = state->val; |
| 5555 | #ifdef ENABLE_LOG |
| 5556 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 5557 | curc, c1, c2); |
| 5558 | #endif |
| 5559 | if (curc >= c1 && curc <= c2) |
| 5560 | { |
| 5561 | result = result_if_matched; |
| 5562 | break; |
| 5563 | } |
| 5564 | if (ireg_ic) |
| 5565 | { |
| 5566 | int curc_low = MB_TOLOWER(curc); |
| 5567 | int done = FALSE; |
| 5568 | |
| 5569 | for ( ; c1 <= c2; ++c1) |
| 5570 | if (MB_TOLOWER(c1) == curc_low) |
| 5571 | { |
| 5572 | result = result_if_matched; |
| 5573 | done = TRUE; |
| 5574 | break; |
| 5575 | } |
| 5576 | if (done) |
| 5577 | break; |
| 5578 | } |
| 5579 | } |
| 5580 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 5581 | : (curc == state->c |
| 5582 | || (ireg_ic && MB_TOLOWER(curc) |
| 5583 | == MB_TOLOWER(state->c)))) |
| 5584 | { |
| 5585 | result = result_if_matched; |
| 5586 | break; |
| 5587 | } |
| 5588 | state = state->out; |
| 5589 | } |
| 5590 | if (result) |
| 5591 | { |
| 5592 | /* next state is in out of the NFA_END_COLL, out1 of |
| 5593 | * START points to the END state */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5594 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5595 | add_off = clen; |
| 5596 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5597 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5598 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5599 | |
| 5600 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5601 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5602 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5603 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5604 | add_state = t->state->out; |
| 5605 | add_off = clen; |
| 5606 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5607 | break; |
| 5608 | |
| 5609 | /* |
| 5610 | * Character classes like \a for alpha, \d for digit etc. |
| 5611 | */ |
| 5612 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5613 | result = vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5614 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5615 | break; |
| 5616 | |
| 5617 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5618 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5619 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5620 | break; |
| 5621 | |
| 5622 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5623 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5624 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5625 | break; |
| 5626 | |
| 5627 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5628 | result = !VIM_ISDIGIT(curc) |
| 5629 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5630 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5631 | break; |
| 5632 | |
| 5633 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5634 | result = vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5635 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5636 | break; |
| 5637 | |
| 5638 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5639 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5640 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5641 | break; |
| 5642 | |
| 5643 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | 0805049 | 2013-05-21 12:43:56 +0200 | [diff] [blame] | 5644 | result = ptr2cells(reginput) == 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5645 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5646 | break; |
| 5647 | |
| 5648 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5649 | result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5650 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5651 | break; |
| 5652 | |
| 5653 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5654 | result = vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5655 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5656 | break; |
| 5657 | |
| 5658 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5659 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5660 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5661 | break; |
| 5662 | |
| 5663 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5664 | result = ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5665 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5666 | break; |
| 5667 | |
| 5668 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5669 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5670 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5671 | break; |
| 5672 | |
| 5673 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5674 | result = ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5675 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5676 | break; |
| 5677 | |
| 5678 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5679 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5680 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5681 | break; |
| 5682 | |
| 5683 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5684 | result = ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5685 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5686 | break; |
| 5687 | |
| 5688 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5689 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5690 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5691 | break; |
| 5692 | |
| 5693 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5694 | result = ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5695 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5696 | break; |
| 5697 | |
| 5698 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5699 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5700 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5701 | break; |
| 5702 | |
| 5703 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5704 | result = ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5705 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5706 | break; |
| 5707 | |
| 5708 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5709 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5710 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5711 | break; |
| 5712 | |
| 5713 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5714 | result = ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5715 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5716 | break; |
| 5717 | |
| 5718 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5719 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5720 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5721 | break; |
| 5722 | |
| 5723 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5724 | result = ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5725 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5726 | break; |
| 5727 | |
| 5728 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5729 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5730 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5731 | break; |
| 5732 | |
| 5733 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5734 | result = ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5735 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5736 | break; |
| 5737 | |
| 5738 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5739 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5740 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5741 | break; |
| 5742 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5743 | case NFA_BACKREF1: |
| 5744 | case NFA_BACKREF2: |
| 5745 | case NFA_BACKREF3: |
| 5746 | case NFA_BACKREF4: |
| 5747 | case NFA_BACKREF5: |
| 5748 | case NFA_BACKREF6: |
| 5749 | case NFA_BACKREF7: |
| 5750 | case NFA_BACKREF8: |
| 5751 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5752 | #ifdef FEAT_SYN_HL |
| 5753 | case NFA_ZREF1: |
| 5754 | case NFA_ZREF2: |
| 5755 | case NFA_ZREF3: |
| 5756 | case NFA_ZREF4: |
| 5757 | case NFA_ZREF5: |
| 5758 | case NFA_ZREF6: |
| 5759 | case NFA_ZREF7: |
| 5760 | case NFA_ZREF8: |
| 5761 | case NFA_ZREF9: |
| 5762 | #endif |
| 5763 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5764 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5765 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5766 | int bytelen; |
| 5767 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5768 | if (t->state->c <= NFA_BACKREF9) |
| 5769 | { |
| 5770 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 5771 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 5772 | } |
| 5773 | #ifdef FEAT_SYN_HL |
| 5774 | else |
| 5775 | { |
| 5776 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 5777 | result = match_zref(subidx, &bytelen); |
| 5778 | } |
| 5779 | #endif |
| 5780 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5781 | if (result) |
| 5782 | { |
| 5783 | if (bytelen == 0) |
| 5784 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 5785 | /* empty match always works, output of NFA_SKIP to be |
| 5786 | * used next */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5787 | add_here = TRUE; |
| 5788 | add_state = t->state->out->out; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5789 | } |
| 5790 | else if (bytelen <= clen) |
| 5791 | { |
| 5792 | /* match current character, jump ahead to out of |
| 5793 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5794 | add_state = t->state->out->out; |
| 5795 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5796 | } |
| 5797 | else |
| 5798 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 5799 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5800 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5801 | add_state = t->state->out; |
| 5802 | add_off = bytelen; |
| 5803 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5804 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5805 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5806 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5807 | } |
| 5808 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 5809 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5810 | if (t->count - clen <= 0) |
| 5811 | { |
| 5812 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5813 | add_state = t->state->out; |
| 5814 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5815 | } |
| 5816 | else |
| 5817 | { |
| 5818 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5819 | add_state = t->state; |
| 5820 | add_off = 0; |
| 5821 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5822 | } |
| 5823 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5824 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5825 | case NFA_LNUM: |
| 5826 | case NFA_LNUM_GT: |
| 5827 | case NFA_LNUM_LT: |
| 5828 | result = (REG_MULTI && |
| 5829 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 5830 | (long_u)(reglnum + reg_firstlnum))); |
| 5831 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5832 | { |
| 5833 | add_here = TRUE; |
| 5834 | add_state = t->state->out; |
| 5835 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5836 | break; |
| 5837 | |
| 5838 | case NFA_COL: |
| 5839 | case NFA_COL_GT: |
| 5840 | case NFA_COL_LT: |
| 5841 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 5842 | (long_u)(reginput - regline) + 1); |
| 5843 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5844 | { |
| 5845 | add_here = TRUE; |
| 5846 | add_state = t->state->out; |
| 5847 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5848 | break; |
| 5849 | |
| 5850 | case NFA_VCOL: |
| 5851 | case NFA_VCOL_GT: |
| 5852 | case NFA_VCOL_LT: |
| 5853 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 5854 | (long_u)win_linetabsize( |
| 5855 | reg_win == NULL ? curwin : reg_win, |
| 5856 | regline, (colnr_T)(reginput - regline)) + 1); |
| 5857 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5858 | { |
| 5859 | add_here = TRUE; |
| 5860 | add_state = t->state->out; |
| 5861 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5862 | break; |
| 5863 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 5864 | case NFA_MARK: |
| 5865 | case NFA_MARK_GT: |
| 5866 | case NFA_MARK_LT: |
| 5867 | { |
| 5868 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 5869 | |
| 5870 | /* Compare the mark position to the match position. */ |
| 5871 | result = (pos != NULL /* mark doesn't exist */ |
| 5872 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 5873 | && (pos->lnum == reglnum + reg_firstlnum |
| 5874 | ? (pos->col == (colnr_T)(reginput - regline) |
| 5875 | ? t->state->c == NFA_MARK |
| 5876 | : (pos->col < (colnr_T)(reginput - regline) |
| 5877 | ? t->state->c == NFA_MARK_GT |
| 5878 | : t->state->c == NFA_MARK_LT)) |
| 5879 | : (pos->lnum < reglnum + reg_firstlnum |
| 5880 | ? t->state->c == NFA_MARK_GT |
| 5881 | : t->state->c == NFA_MARK_LT))); |
| 5882 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5883 | { |
| 5884 | add_here = TRUE; |
| 5885 | add_state = t->state->out; |
| 5886 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 5887 | break; |
| 5888 | } |
| 5889 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5890 | case NFA_CURSOR: |
| 5891 | result = (reg_win != NULL |
| 5892 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 5893 | && ((colnr_T)(reginput - regline) |
| 5894 | == reg_win->w_cursor.col)); |
| 5895 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5896 | { |
| 5897 | add_here = TRUE; |
| 5898 | add_state = t->state->out; |
| 5899 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 5900 | break; |
| 5901 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5902 | case NFA_VISUAL: |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5903 | #ifdef FEAT_VISUAL |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5904 | result = reg_match_visual(); |
| 5905 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5906 | { |
| 5907 | add_here = TRUE; |
| 5908 | add_state = t->state->out; |
| 5909 | } |
Bram Moolenaar | 78eae9a | 2013-06-05 11:02:05 +0200 | [diff] [blame] | 5910 | #endif |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 5911 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 5912 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5913 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5914 | { |
| 5915 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 5916 | |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5917 | /* TODO: put this in #ifdef later */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5918 | if (c < 0) |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5919 | EMSGN("INTERNAL: Negative state char: %ld", c); |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5920 | result = (c == curc); |
| 5921 | |
| 5922 | if (!result && ireg_ic) |
| 5923 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5924 | #ifdef FEAT_MBYTE |
| 5925 | /* If there is a composing character which is not being |
| 5926 | * ignored there can be no match. Match with composing |
| 5927 | * character uses NFA_COMPOSING above. */ |
| 5928 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5929 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5930 | result = FALSE; |
| 5931 | #endif |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5932 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5933 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 5934 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5935 | |
| 5936 | } /* switch (t->state->c) */ |
| 5937 | |
| 5938 | if (add_state != NULL) |
| 5939 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5940 | nfa_pim_T *pim; |
| 5941 | |
| 5942 | if (t->pim.result == NFA_PIM_UNUSED) |
| 5943 | pim = NULL; |
| 5944 | else |
| 5945 | pim = &t->pim; |
| 5946 | |
| 5947 | /* Handle the postponed invisible match if the match might end |
| 5948 | * without advancing and before the end of the line. */ |
| 5949 | if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5950 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5951 | if (pim->result == NFA_PIM_TODO) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5952 | { |
| 5953 | #ifdef ENABLE_LOG |
| 5954 | fprintf(log_fd, "\n"); |
| 5955 | fprintf(log_fd, "==================================\n"); |
| 5956 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 5957 | fprintf(log_fd, "\n"); |
| 5958 | #endif |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5959 | result = recursive_regmatch(pim->state, pim, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5960 | prog, submatch, m, &listids); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5961 | pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5962 | /* for \@! and \@<! it is a match when the result is |
| 5963 | * FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5964 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5965 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5966 | || pim->state->c |
| 5967 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5968 | || pim->state->c |
| 5969 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5970 | { |
| 5971 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5972 | copy_sub_off(&pim->subs.norm, &m->norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5973 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5974 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5975 | copy_sub_off(&pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5976 | #endif |
| 5977 | } |
| 5978 | } |
| 5979 | else |
| 5980 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5981 | result = (pim->result == NFA_PIM_MATCH); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5982 | #ifdef ENABLE_LOG |
| 5983 | fprintf(log_fd, "\n"); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5984 | 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] | 5985 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5986 | fprintf(log_fd, "\n"); |
| 5987 | #endif |
| 5988 | } |
| 5989 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5990 | /* for \@! and \@<! it is a match when result is FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5991 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5992 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5993 | || pim->state->c |
| 5994 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5995 | || pim->state->c |
| 5996 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5997 | { |
| 5998 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5999 | copy_sub_off(&t->subs.norm, &pim->subs.norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6000 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6001 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6002 | copy_sub_off(&t->subs.synt, &pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6003 | #endif |
| 6004 | } |
| 6005 | else |
| 6006 | /* look-behind match failed, don't add the state */ |
| 6007 | continue; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6008 | |
| 6009 | /* Postponed invisible match was handled, don't add it to |
| 6010 | * following states. */ |
| 6011 | pim = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6012 | } |
| 6013 | |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6014 | if (add_here) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6015 | addstate_here(thislist, add_state, &t->subs, pim, &listidx); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6016 | else |
| 6017 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6018 | addstate(nextlist, add_state, &t->subs, pim, add_off); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6019 | if (add_count > 0) |
| 6020 | nextlist->t[nextlist->n - 1].count = add_count; |
| 6021 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6022 | } |
| 6023 | |
| 6024 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 6025 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6026 | /* Look for the start of a match in the current position by adding the |
| 6027 | * start state to the list of states. |
| 6028 | * The first found match is the leftmost one, thus the order of states |
| 6029 | * matters! |
| 6030 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 6031 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6032 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6033 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6034 | if (nfa_match == FALSE |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6035 | && ((toplevel |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6036 | && reglnum == 0 |
| 6037 | && clen != 0 |
| 6038 | && (ireg_maxcol == 0 |
| 6039 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6040 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6041 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6042 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 6043 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6044 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6045 | < nfa_endp->se_u.pos.col)) |
| 6046 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6047 | { |
| 6048 | #ifdef ENABLE_LOG |
| 6049 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 6050 | #endif |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6051 | /* Inline optimized code for addstate() if we know the state is |
| 6052 | * the first MOPEN. */ |
| 6053 | if (toplevel) |
| 6054 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6055 | int add = TRUE; |
| 6056 | int c; |
| 6057 | |
| 6058 | if (prog->regstart != NUL && clen != 0) |
| 6059 | { |
| 6060 | if (nextlist->n == 0) |
| 6061 | { |
| 6062 | colnr_T col = (colnr_T)(reginput - regline) + clen; |
| 6063 | |
| 6064 | /* Nextlist is empty, we can skip ahead to the |
| 6065 | * character that must appear at the start. */ |
| 6066 | if (skip_to_start(prog->regstart, &col) == FAIL) |
| 6067 | break; |
| 6068 | #ifdef ENABLE_LOG |
| 6069 | fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
| 6070 | col - ((colnr_T)(reginput - regline) + clen)); |
| 6071 | #endif |
| 6072 | reginput = regline + col - clen; |
| 6073 | } |
| 6074 | else |
| 6075 | { |
| 6076 | /* Checking if the required start character matches is |
| 6077 | * cheaper than adding a state that won't match. */ |
| 6078 | c = PTR2CHAR(reginput + clen); |
| 6079 | if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c) |
| 6080 | != MB_TOLOWER(prog->regstart))) |
| 6081 | { |
| 6082 | #ifdef ENABLE_LOG |
| 6083 | fprintf(log_fd, " Skipping start state, regstart does not match\n"); |
| 6084 | #endif |
| 6085 | add = FALSE; |
| 6086 | } |
| 6087 | } |
| 6088 | } |
| 6089 | |
| 6090 | if (add) |
| 6091 | { |
| 6092 | if (REG_MULTI) |
| 6093 | m->norm.list.multi[0].start.col = |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6094 | (colnr_T)(reginput - regline) + clen; |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6095 | else |
| 6096 | m->norm.list.line[0].start = reginput + clen; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6097 | addstate(nextlist, start->out, m, NULL, clen); |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6098 | } |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6099 | } |
| 6100 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6101 | addstate(nextlist, start, m, NULL, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6102 | } |
| 6103 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6104 | #ifdef ENABLE_LOG |
| 6105 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6106 | { |
| 6107 | int i; |
| 6108 | |
| 6109 | for (i = 0; i < thislist->n; i++) |
| 6110 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 6111 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6112 | fprintf(log_fd, "\n"); |
| 6113 | #endif |
| 6114 | |
| 6115 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6116 | /* Advance to the next character, or advance to the next line, or |
| 6117 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6118 | if (clen != 0) |
| 6119 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6120 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 6121 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6122 | reg_nextline(); |
| 6123 | else |
| 6124 | break; |
| 6125 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6126 | |
| 6127 | #ifdef ENABLE_LOG |
| 6128 | if (log_fd != stderr) |
| 6129 | fclose(log_fd); |
| 6130 | log_fd = NULL; |
| 6131 | #endif |
| 6132 | |
| 6133 | theend: |
| 6134 | /* Free memory */ |
| 6135 | vim_free(list[0].t); |
| 6136 | vim_free(list[1].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6137 | vim_free(listids); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6138 | #undef ADD_STATE_IF_MATCH |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 6139 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6140 | fclose(debug); |
| 6141 | #endif |
| 6142 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6143 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6144 | } |
| 6145 | |
| 6146 | /* |
| 6147 | * Try match of "prog" with at regline["col"]. |
| 6148 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6149 | */ |
| 6150 | static long |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6151 | nfa_regtry(prog, col) |
| 6152 | nfa_regprog_T *prog; |
| 6153 | colnr_T col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6154 | { |
| 6155 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6156 | regsubs_T subs, m; |
| 6157 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6158 | #ifdef ENABLE_LOG |
| 6159 | FILE *f; |
| 6160 | #endif |
| 6161 | |
| 6162 | reginput = regline + col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6163 | |
| 6164 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6165 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6166 | if (f != NULL) |
| 6167 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6168 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6169 | #ifdef DEBUG |
| 6170 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 6171 | #endif |
| 6172 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6173 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 6174 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6175 | fprintf(f, "\n\n"); |
| 6176 | fclose(f); |
| 6177 | } |
| 6178 | else |
| 6179 | EMSG(_("Could not open temporary log file for writing ")); |
| 6180 | #endif |
| 6181 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6182 | clear_sub(&subs.norm); |
| 6183 | clear_sub(&m.norm); |
| 6184 | #ifdef FEAT_SYN_HL |
| 6185 | clear_sub(&subs.synt); |
| 6186 | clear_sub(&m.synt); |
| 6187 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6188 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 6189 | if (nfa_regmatch(prog, start, &subs, &m) == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6190 | return 0; |
| 6191 | |
| 6192 | cleanup_subexpr(); |
| 6193 | if (REG_MULTI) |
| 6194 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6195 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6196 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6197 | reg_startpos[i] = subs.norm.list.multi[i].start; |
| 6198 | reg_endpos[i] = subs.norm.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6199 | } |
| 6200 | |
| 6201 | if (reg_startpos[0].lnum < 0) |
| 6202 | { |
| 6203 | reg_startpos[0].lnum = 0; |
| 6204 | reg_startpos[0].col = col; |
| 6205 | } |
| 6206 | if (reg_endpos[0].lnum < 0) |
| 6207 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 6208 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6209 | reg_endpos[0].lnum = reglnum; |
| 6210 | reg_endpos[0].col = (int)(reginput - regline); |
| 6211 | } |
| 6212 | else |
| 6213 | /* Use line number of "\ze". */ |
| 6214 | reglnum = reg_endpos[0].lnum; |
| 6215 | } |
| 6216 | else |
| 6217 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6218 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6219 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6220 | reg_startp[i] = subs.norm.list.line[i].start; |
| 6221 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6222 | } |
| 6223 | |
| 6224 | if (reg_startp[0] == NULL) |
| 6225 | reg_startp[0] = regline + col; |
| 6226 | if (reg_endp[0] == NULL) |
| 6227 | reg_endp[0] = reginput; |
| 6228 | } |
| 6229 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6230 | #ifdef FEAT_SYN_HL |
| 6231 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 6232 | unref_extmatch(re_extmatch_out); |
| 6233 | re_extmatch_out = NULL; |
| 6234 | |
| 6235 | if (prog->reghasz == REX_SET) |
| 6236 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6237 | cleanup_zsubexpr(); |
| 6238 | re_extmatch_out = make_extmatch(); |
| 6239 | for (i = 0; i < subs.synt.in_use; i++) |
| 6240 | { |
| 6241 | if (REG_MULTI) |
| 6242 | { |
| 6243 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 6244 | |
| 6245 | /* Only accept single line matches. */ |
| 6246 | if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum) |
| 6247 | re_extmatch_out->matches[i] = |
| 6248 | vim_strnsave(reg_getline(mpos->start.lnum) |
| 6249 | + mpos->start.col, |
| 6250 | mpos->end.col - mpos->start.col); |
| 6251 | } |
| 6252 | else |
| 6253 | { |
| 6254 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 6255 | |
| 6256 | if (lpos->start != NULL && lpos->end != NULL) |
| 6257 | re_extmatch_out->matches[i] = |
| 6258 | vim_strnsave(lpos->start, |
| 6259 | (int)(lpos->end - lpos->start)); |
| 6260 | } |
| 6261 | } |
| 6262 | } |
| 6263 | #endif |
| 6264 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6265 | return 1 + reglnum; |
| 6266 | } |
| 6267 | |
| 6268 | /* |
| 6269 | * Match a regexp against a string ("line" points to the string) or multiple |
| 6270 | * lines ("line" is NULL, use reg_getline()). |
| 6271 | * |
| 6272 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6273 | */ |
| 6274 | static long |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6275 | nfa_regexec_both(line, startcol) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6276 | char_u *line; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6277 | colnr_T startcol; /* column to start looking for match */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6278 | { |
| 6279 | nfa_regprog_T *prog; |
| 6280 | long retval = 0L; |
| 6281 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6282 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6283 | |
| 6284 | if (REG_MULTI) |
| 6285 | { |
| 6286 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 6287 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 6288 | reg_startpos = reg_mmatch->startpos; |
| 6289 | reg_endpos = reg_mmatch->endpos; |
| 6290 | } |
| 6291 | else |
| 6292 | { |
| 6293 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 6294 | reg_startp = reg_match->startp; |
| 6295 | reg_endp = reg_match->endp; |
| 6296 | } |
| 6297 | |
| 6298 | /* Be paranoid... */ |
| 6299 | if (prog == NULL || line == NULL) |
| 6300 | { |
| 6301 | EMSG(_(e_null)); |
| 6302 | goto theend; |
| 6303 | } |
| 6304 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6305 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 6306 | if (prog->regflags & RF_ICASE) |
| 6307 | ireg_ic = TRUE; |
| 6308 | else if (prog->regflags & RF_NOICASE) |
| 6309 | ireg_ic = FALSE; |
| 6310 | |
| 6311 | #ifdef FEAT_MBYTE |
| 6312 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 6313 | if (prog->regflags & RF_ICOMBINE) |
| 6314 | ireg_icombine = TRUE; |
| 6315 | #endif |
| 6316 | |
| 6317 | regline = line; |
| 6318 | reglnum = 0; /* relative to line */ |
| 6319 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6320 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6321 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6322 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6323 | nfa_listid = 1; |
| 6324 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6325 | #ifdef DEBUG |
| 6326 | nfa_regengine.expr = prog->pattern; |
| 6327 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6328 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6329 | if (prog->reganch && col > 0) |
| 6330 | return 0L; |
| 6331 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6332 | need_clear_subexpr = TRUE; |
| 6333 | #ifdef FEAT_SYN_HL |
| 6334 | /* Clear the external match subpointers if necessary. */ |
| 6335 | if (prog->reghasz == REX_SET) |
| 6336 | { |
| 6337 | nfa_has_zsubexpr = TRUE; |
| 6338 | need_clear_zsubexpr = TRUE; |
| 6339 | } |
| 6340 | else |
| 6341 | nfa_has_zsubexpr = FALSE; |
| 6342 | #endif |
| 6343 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6344 | if (prog->regstart != NUL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6345 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6346 | /* Skip ahead until a character we know the match must start with. |
| 6347 | * When there is none there is no match. */ |
| 6348 | if (skip_to_start(prog->regstart, &col) == FAIL) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6349 | return 0L; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6350 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6351 | /* If match_text is set it contains the full text that must match. |
| 6352 | * Nothing else to try. Doesn't handle combining chars well. */ |
Bram Moolenaar | a940aa6 | 2013-06-08 23:30:04 +0200 | [diff] [blame] | 6353 | if (prog->match_text != NULL |
| 6354 | #ifdef FEAT_MBYTE |
| 6355 | && !ireg_icombine |
| 6356 | #endif |
| 6357 | ) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6358 | return find_match_text(col, prog->regstart, prog->match_text); |
| 6359 | } |
| 6360 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6361 | /* If the start column is past the maximum column: no need to try. */ |
| 6362 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 6363 | goto theend; |
| 6364 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6365 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6366 | for (i = 0; i < nstate; ++i) |
| 6367 | { |
| 6368 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6369 | prog->state[i].lastlist[0] = 0; |
| 6370 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6371 | } |
| 6372 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6373 | retval = nfa_regtry(prog, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6374 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6375 | #ifdef DEBUG |
| 6376 | nfa_regengine.expr = NULL; |
| 6377 | #endif |
| 6378 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6379 | theend: |
| 6380 | return retval; |
| 6381 | } |
| 6382 | |
| 6383 | /* |
| 6384 | * Compile a regular expression into internal code for the NFA matcher. |
| 6385 | * Returns the program in allocated space. Returns NULL for an error. |
| 6386 | */ |
| 6387 | static regprog_T * |
| 6388 | nfa_regcomp(expr, re_flags) |
| 6389 | char_u *expr; |
| 6390 | int re_flags; |
| 6391 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6392 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 6393 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6394 | int *postfix; |
| 6395 | |
| 6396 | if (expr == NULL) |
| 6397 | return NULL; |
| 6398 | |
| 6399 | #ifdef DEBUG |
| 6400 | nfa_regengine.expr = expr; |
| 6401 | #endif |
| 6402 | |
| 6403 | init_class_tab(); |
| 6404 | |
| 6405 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 6406 | return NULL; |
| 6407 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6408 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6409 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6410 | postfix = re2post(); |
| 6411 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6412 | { |
| 6413 | /* TODO: only give this error for debugging? */ |
| 6414 | if (post_ptr >= post_end) |
| 6415 | 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] | 6416 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6417 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6418 | |
| 6419 | /* |
| 6420 | * In order to build the NFA, we parse the input regexp twice: |
| 6421 | * 1. first pass to count size (so we can allocate space) |
| 6422 | * 2. second to emit code |
| 6423 | */ |
| 6424 | #ifdef ENABLE_LOG |
| 6425 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6426 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6427 | |
| 6428 | if (f != NULL) |
| 6429 | { |
| 6430 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 6431 | fclose(f); |
| 6432 | } |
| 6433 | } |
| 6434 | #endif |
| 6435 | |
| 6436 | /* |
| 6437 | * PASS 1 |
| 6438 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 6439 | */ |
| 6440 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6441 | |
Bram Moolenaar | 16619a2 | 2013-06-11 18:42:36 +0200 | [diff] [blame] | 6442 | /* allocate the regprog with space for the compiled regexp */ |
| 6443 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6444 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 6445 | if (prog == NULL) |
| 6446 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6447 | state_ptr = prog->state; |
| 6448 | |
| 6449 | /* |
| 6450 | * PASS 2 |
| 6451 | * Build the NFA |
| 6452 | */ |
| 6453 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 6454 | if (prog->start == NULL) |
| 6455 | goto fail; |
| 6456 | |
| 6457 | prog->regflags = regflags; |
| 6458 | prog->engine = &nfa_regengine; |
| 6459 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6460 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6461 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6462 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6463 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6464 | nfa_postprocess(prog); |
| 6465 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6466 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 6467 | prog->regstart = nfa_get_regstart(prog->start, 0); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6468 | prog->match_text = nfa_get_match_text(prog->start); |
| 6469 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6470 | #ifdef ENABLE_LOG |
| 6471 | nfa_postfix_dump(expr, OK); |
| 6472 | nfa_dump(prog); |
| 6473 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6474 | #ifdef FEAT_SYN_HL |
| 6475 | /* Remember whether this pattern has any \z specials in it. */ |
| 6476 | prog->reghasz = re_has_z; |
| 6477 | #endif |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6478 | #ifdef DEBUG |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6479 | prog->pattern = vim_strsave(expr); |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6480 | nfa_regengine.expr = NULL; |
| 6481 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6482 | |
| 6483 | out: |
| 6484 | vim_free(post_start); |
| 6485 | post_start = post_ptr = post_end = NULL; |
| 6486 | state_ptr = NULL; |
| 6487 | return (regprog_T *)prog; |
| 6488 | |
| 6489 | fail: |
| 6490 | vim_free(prog); |
| 6491 | prog = NULL; |
| 6492 | #ifdef ENABLE_LOG |
| 6493 | nfa_postfix_dump(expr, FAIL); |
| 6494 | #endif |
| 6495 | #ifdef DEBUG |
| 6496 | nfa_regengine.expr = NULL; |
| 6497 | #endif |
| 6498 | goto out; |
| 6499 | } |
| 6500 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6501 | /* |
| 6502 | * Free a compiled regexp program, returned by nfa_regcomp(). |
| 6503 | */ |
| 6504 | static void |
| 6505 | nfa_regfree(prog) |
| 6506 | regprog_T *prog; |
| 6507 | { |
| 6508 | if (prog != NULL) |
| 6509 | { |
| 6510 | vim_free(((nfa_regprog_T *)prog)->match_text); |
| 6511 | #ifdef DEBUG |
| 6512 | vim_free(((nfa_regprog_T *)prog)->pattern); |
| 6513 | #endif |
| 6514 | vim_free(prog); |
| 6515 | } |
| 6516 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6517 | |
| 6518 | /* |
| 6519 | * Match a regexp against a string. |
| 6520 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 6521 | * Uses curbuf for line count and 'iskeyword'. |
| 6522 | * |
| 6523 | * Return TRUE if there is a match, FALSE if not. |
| 6524 | */ |
| 6525 | static int |
| 6526 | nfa_regexec(rmp, line, col) |
| 6527 | regmatch_T *rmp; |
| 6528 | char_u *line; /* string to match against */ |
| 6529 | colnr_T col; /* column to start looking for match */ |
| 6530 | { |
| 6531 | reg_match = rmp; |
| 6532 | reg_mmatch = NULL; |
| 6533 | reg_maxline = 0; |
| 6534 | reg_line_lbr = FALSE; |
| 6535 | reg_buf = curbuf; |
| 6536 | reg_win = NULL; |
| 6537 | ireg_ic = rmp->rm_ic; |
| 6538 | #ifdef FEAT_MBYTE |
| 6539 | ireg_icombine = FALSE; |
| 6540 | #endif |
| 6541 | ireg_maxcol = 0; |
| 6542 | return (nfa_regexec_both(line, col) != 0); |
| 6543 | } |
| 6544 | |
| 6545 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 6546 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 6547 | |
| 6548 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 6549 | |
| 6550 | /* |
| 6551 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 6552 | */ |
| 6553 | static int |
| 6554 | nfa_regexec_nl(rmp, line, col) |
| 6555 | regmatch_T *rmp; |
| 6556 | char_u *line; /* string to match against */ |
| 6557 | colnr_T col; /* column to start looking for match */ |
| 6558 | { |
| 6559 | reg_match = rmp; |
| 6560 | reg_mmatch = NULL; |
| 6561 | reg_maxline = 0; |
| 6562 | reg_line_lbr = TRUE; |
| 6563 | reg_buf = curbuf; |
| 6564 | reg_win = NULL; |
| 6565 | ireg_ic = rmp->rm_ic; |
| 6566 | #ifdef FEAT_MBYTE |
| 6567 | ireg_icombine = FALSE; |
| 6568 | #endif |
| 6569 | ireg_maxcol = 0; |
| 6570 | return (nfa_regexec_both(line, col) != 0); |
| 6571 | } |
| 6572 | #endif |
| 6573 | |
| 6574 | |
| 6575 | /* |
| 6576 | * Match a regexp against multiple lines. |
| 6577 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 6578 | * Uses curbuf for line count and 'iskeyword'. |
| 6579 | * |
| 6580 | * Return zero if there is no match. Return number of lines contained in the |
| 6581 | * match otherwise. |
| 6582 | * |
| 6583 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 6584 | * |
| 6585 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 6586 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 6587 | * |
| 6588 | * +-------------------------+ |
| 6589 | * |a | |
| 6590 | * |b | |
| 6591 | * |c | |
| 6592 | * | | |
| 6593 | * +-------------------------+ |
| 6594 | * |
| 6595 | * then nfa_regexec_multi() returns 3. while the original |
| 6596 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 6597 | * |
| 6598 | * FIXME if this behavior is not compatible. |
| 6599 | */ |
| 6600 | static long |
| 6601 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 6602 | regmmatch_T *rmp; |
| 6603 | win_T *win; /* window in which to search or NULL */ |
| 6604 | buf_T *buf; /* buffer in which to search */ |
| 6605 | linenr_T lnum; /* nr of line to start looking for match */ |
| 6606 | colnr_T col; /* column to start looking for match */ |
| 6607 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 6608 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6609 | reg_match = NULL; |
| 6610 | reg_mmatch = rmp; |
| 6611 | reg_buf = buf; |
| 6612 | reg_win = win; |
| 6613 | reg_firstlnum = lnum; |
| 6614 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 6615 | reg_line_lbr = FALSE; |
| 6616 | ireg_ic = rmp->rmm_ic; |
| 6617 | #ifdef FEAT_MBYTE |
| 6618 | ireg_icombine = FALSE; |
| 6619 | #endif |
| 6620 | ireg_maxcol = rmp->rmm_maxcol; |
| 6621 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 6622 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6623 | } |
| 6624 | |
| 6625 | #ifdef DEBUG |
| 6626 | # undef ENABLE_LOG |
| 6627 | #endif |