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 | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 32 | /* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */ |
| 33 | #define NFA_ADD_NL 31 |
| 34 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 35 | enum |
| 36 | { |
| 37 | NFA_SPLIT = -1024, |
| 38 | NFA_MATCH, |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 39 | NFA_EMPTY, /* matches 0-length */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 40 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 41 | NFA_START_COLL, /* [abc] start */ |
| 42 | NFA_END_COLL, /* [abc] end */ |
| 43 | NFA_START_NEG_COLL, /* [^abc] start */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 44 | NFA_END_NEG_COLL, /* [^abc] end (postfix only) */ |
| 45 | NFA_RANGE, /* range of the two previous items |
| 46 | * (postfix only) */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 47 | NFA_RANGE_MIN, /* low end of a range */ |
| 48 | NFA_RANGE_MAX, /* high end of a range */ |
| 49 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 50 | NFA_CONCAT, /* concatenate two previous items (postfix |
| 51 | * only) */ |
| 52 | NFA_OR, /* \| (postfix only) */ |
| 53 | NFA_STAR, /* greedy * (posfix only) */ |
| 54 | NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */ |
| 55 | NFA_QUEST, /* greedy \? (postfix only) */ |
| 56 | NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 57 | |
| 58 | NFA_BOL, /* ^ Begin line */ |
| 59 | NFA_EOL, /* $ End line */ |
| 60 | NFA_BOW, /* \< Begin word */ |
| 61 | NFA_EOW, /* \> End word */ |
| 62 | NFA_BOF, /* \%^ Begin file */ |
| 63 | NFA_EOF, /* \%$ End file */ |
| 64 | NFA_NEWL, |
| 65 | NFA_ZSTART, /* Used for \zs */ |
| 66 | NFA_ZEND, /* Used for \ze */ |
| 67 | NFA_NOPEN, /* Start of subexpression marked with \%( */ |
| 68 | NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ |
| 69 | NFA_START_INVISIBLE, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 70 | NFA_START_INVISIBLE_FIRST, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 71 | NFA_START_INVISIBLE_NEG, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 72 | NFA_START_INVISIBLE_NEG_FIRST, |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 73 | NFA_START_INVISIBLE_BEFORE, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 74 | NFA_START_INVISIBLE_BEFORE_FIRST, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 75 | NFA_START_INVISIBLE_BEFORE_NEG, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 76 | NFA_START_INVISIBLE_BEFORE_NEG_FIRST, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 77 | NFA_START_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 78 | NFA_END_INVISIBLE, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 79 | NFA_END_INVISIBLE_NEG, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 80 | NFA_END_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 81 | NFA_COMPOSING, /* Next nodes in NFA are part of the |
| 82 | composing multibyte char */ |
| 83 | NFA_END_COMPOSING, /* End of a composing char in the NFA */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 84 | NFA_OPT_CHARS, /* \%[abc] */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 85 | |
| 86 | /* The following are used only in the postfix form, not in the NFA */ |
| 87 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 88 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 89 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 90 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 91 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 92 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 93 | NFA_BACKREF1, /* \1 */ |
| 94 | NFA_BACKREF2, /* \2 */ |
| 95 | NFA_BACKREF3, /* \3 */ |
| 96 | NFA_BACKREF4, /* \4 */ |
| 97 | NFA_BACKREF5, /* \5 */ |
| 98 | NFA_BACKREF6, /* \6 */ |
| 99 | NFA_BACKREF7, /* \7 */ |
| 100 | NFA_BACKREF8, /* \8 */ |
| 101 | NFA_BACKREF9, /* \9 */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 102 | #ifdef FEAT_SYN_HL |
| 103 | NFA_ZREF1, /* \z1 */ |
| 104 | NFA_ZREF2, /* \z2 */ |
| 105 | NFA_ZREF3, /* \z3 */ |
| 106 | NFA_ZREF4, /* \z4 */ |
| 107 | NFA_ZREF5, /* \z5 */ |
| 108 | NFA_ZREF6, /* \z6 */ |
| 109 | NFA_ZREF7, /* \z7 */ |
| 110 | NFA_ZREF8, /* \z8 */ |
| 111 | NFA_ZREF9, /* \z9 */ |
| 112 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 113 | NFA_SKIP, /* Skip characters */ |
| 114 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 115 | NFA_MOPEN, |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 116 | NFA_MOPEN1, |
| 117 | NFA_MOPEN2, |
| 118 | NFA_MOPEN3, |
| 119 | NFA_MOPEN4, |
| 120 | NFA_MOPEN5, |
| 121 | NFA_MOPEN6, |
| 122 | NFA_MOPEN7, |
| 123 | NFA_MOPEN8, |
| 124 | NFA_MOPEN9, |
| 125 | |
| 126 | NFA_MCLOSE, |
| 127 | NFA_MCLOSE1, |
| 128 | NFA_MCLOSE2, |
| 129 | NFA_MCLOSE3, |
| 130 | NFA_MCLOSE4, |
| 131 | NFA_MCLOSE5, |
| 132 | NFA_MCLOSE6, |
| 133 | NFA_MCLOSE7, |
| 134 | NFA_MCLOSE8, |
| 135 | NFA_MCLOSE9, |
| 136 | |
| 137 | #ifdef FEAT_SYN_HL |
| 138 | NFA_ZOPEN, |
| 139 | NFA_ZOPEN1, |
| 140 | NFA_ZOPEN2, |
| 141 | NFA_ZOPEN3, |
| 142 | NFA_ZOPEN4, |
| 143 | NFA_ZOPEN5, |
| 144 | NFA_ZOPEN6, |
| 145 | NFA_ZOPEN7, |
| 146 | NFA_ZOPEN8, |
| 147 | NFA_ZOPEN9, |
| 148 | |
| 149 | NFA_ZCLOSE, |
| 150 | NFA_ZCLOSE1, |
| 151 | NFA_ZCLOSE2, |
| 152 | NFA_ZCLOSE3, |
| 153 | NFA_ZCLOSE4, |
| 154 | NFA_ZCLOSE5, |
| 155 | NFA_ZCLOSE6, |
| 156 | NFA_ZCLOSE7, |
| 157 | NFA_ZCLOSE8, |
| 158 | NFA_ZCLOSE9, |
| 159 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 160 | |
| 161 | /* NFA_FIRST_NL */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 162 | NFA_ANY, /* Match any one character. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 163 | NFA_IDENT, /* Match identifier char */ |
| 164 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 165 | NFA_KWORD, /* Match keyword char */ |
| 166 | NFA_SKWORD, /* Match word char but no digit */ |
| 167 | NFA_FNAME, /* Match file name char */ |
| 168 | NFA_SFNAME, /* Match file name char but no digit */ |
| 169 | NFA_PRINT, /* Match printable char */ |
| 170 | NFA_SPRINT, /* Match printable char but no digit */ |
| 171 | NFA_WHITE, /* Match whitespace char */ |
| 172 | NFA_NWHITE, /* Match non-whitespace char */ |
| 173 | NFA_DIGIT, /* Match digit char */ |
| 174 | NFA_NDIGIT, /* Match non-digit char */ |
| 175 | NFA_HEX, /* Match hex char */ |
| 176 | NFA_NHEX, /* Match non-hex char */ |
| 177 | NFA_OCTAL, /* Match octal char */ |
| 178 | NFA_NOCTAL, /* Match non-octal char */ |
| 179 | NFA_WORD, /* Match word char */ |
| 180 | NFA_NWORD, /* Match non-word char */ |
| 181 | NFA_HEAD, /* Match head char */ |
| 182 | NFA_NHEAD, /* Match non-head char */ |
| 183 | NFA_ALPHA, /* Match alpha char */ |
| 184 | NFA_NALPHA, /* Match non-alpha char */ |
| 185 | NFA_LOWER, /* Match lowercase char */ |
| 186 | NFA_NLOWER, /* Match non-lowercase char */ |
| 187 | NFA_UPPER, /* Match uppercase char */ |
| 188 | NFA_NUPPER, /* Match non-uppercase char */ |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 189 | NFA_LOWER_IC, /* Match [a-z] */ |
| 190 | NFA_NLOWER_IC, /* Match [^a-z] */ |
| 191 | NFA_UPPER_IC, /* Match [A-Z] */ |
| 192 | NFA_NUPPER_IC, /* Match [^A-Z] */ |
| 193 | |
| 194 | NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL, |
| 195 | NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL, |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 196 | |
| 197 | NFA_CURSOR, /* Match cursor pos */ |
| 198 | NFA_LNUM, /* Match line number */ |
| 199 | NFA_LNUM_GT, /* Match > line number */ |
| 200 | NFA_LNUM_LT, /* Match < line number */ |
| 201 | NFA_COL, /* Match cursor column */ |
| 202 | NFA_COL_GT, /* Match > cursor column */ |
| 203 | NFA_COL_LT, /* Match < cursor column */ |
| 204 | NFA_VCOL, /* Match cursor virtual column */ |
| 205 | NFA_VCOL_GT, /* Match > cursor virtual column */ |
| 206 | NFA_VCOL_LT, /* Match < cursor virtual column */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 207 | NFA_MARK, /* Match mark */ |
| 208 | NFA_MARK_GT, /* Match > mark */ |
| 209 | NFA_MARK_LT, /* Match < mark */ |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 210 | NFA_VISUAL, /* Match Visual area */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 211 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 212 | /* Character classes [:alnum:] etc */ |
| 213 | NFA_CLASS_ALNUM, |
| 214 | NFA_CLASS_ALPHA, |
| 215 | NFA_CLASS_BLANK, |
| 216 | NFA_CLASS_CNTRL, |
| 217 | NFA_CLASS_DIGIT, |
| 218 | NFA_CLASS_GRAPH, |
| 219 | NFA_CLASS_LOWER, |
| 220 | NFA_CLASS_PRINT, |
| 221 | NFA_CLASS_PUNCT, |
| 222 | NFA_CLASS_SPACE, |
| 223 | NFA_CLASS_UPPER, |
| 224 | NFA_CLASS_XDIGIT, |
| 225 | NFA_CLASS_TAB, |
| 226 | NFA_CLASS_RETURN, |
| 227 | NFA_CLASS_BACKSPACE, |
| 228 | NFA_CLASS_ESCAPE |
| 229 | }; |
| 230 | |
| 231 | /* Keep in sync with classchars. */ |
| 232 | static int nfa_classcodes[] = { |
| 233 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 234 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 235 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 236 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 237 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 238 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 239 | NFA_UPPER, NFA_NUPPER |
| 240 | }; |
| 241 | |
| 242 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
| 243 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 244 | /* NFA regexp \ze operator encountered. */ |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 245 | static int nfa_has_zend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 246 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 247 | /* NFA regexp \1 .. \9 encountered. */ |
| 248 | static int nfa_has_backref; |
| 249 | |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 250 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 251 | /* NFA regexp has \z( ), set zsubexpr. */ |
| 252 | static int nfa_has_zsubexpr; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 253 | #endif |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 254 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 255 | /* Number of sub expressions actually being used during execution. 1 if only |
| 256 | * the whole match (subexpr 0) is used. */ |
| 257 | static int nfa_nsubexpr; |
| 258 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 259 | static int *post_start; /* holds the postfix form of r.e. */ |
| 260 | static int *post_end; |
| 261 | static int *post_ptr; |
| 262 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 263 | static int nstate; /* Number of states in the NFA. Also used when |
| 264 | * executing. */ |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 265 | static int istate; /* Index in the state vector, used in alloc_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 266 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 267 | /* If not NULL match must end at this position */ |
| 268 | static save_se_T *nfa_endp = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 269 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 270 | /* listid is global, so that it increases on recursive calls to |
| 271 | * nfa_regmatch(), which means we don't have to clear the lastlist field of |
| 272 | * all the states. */ |
| 273 | static int nfa_listid; |
| 274 | static int nfa_alt_listid; |
| 275 | |
| 276 | /* 0 for first call to nfa_regmatch(), 1 for recursive call. */ |
| 277 | static int nfa_ll_index = 0; |
| 278 | |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 279 | static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 280 | static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth)); |
| 281 | static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 282 | static char_u *nfa_get_match_text __ARGS((nfa_state_T *start)); |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 283 | static int realloc_post_list __ARGS((void)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 284 | 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] | 285 | static int nfa_emit_equi_class __ARGS((int c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 286 | static int nfa_regatom __ARGS((void)); |
| 287 | static int nfa_regpiece __ARGS((void)); |
| 288 | static int nfa_regconcat __ARGS((void)); |
| 289 | static int nfa_regbranch __ARGS((void)); |
| 290 | static int nfa_reg __ARGS((int paren)); |
| 291 | #ifdef DEBUG |
| 292 | static void nfa_set_code __ARGS((int c)); |
| 293 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 294 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 295 | 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] | 296 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 297 | #endif |
| 298 | static int *re2post __ARGS((void)); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 299 | static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 300 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
| 301 | static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 302 | 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] | 303 | static void nfa_postprocess __ARGS((nfa_regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 304 | static int check_char_class __ARGS((int class, int c)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 305 | static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list)); |
| 306 | static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 307 | 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] | 308 | static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 309 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 310 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 311 | static void nfa_regfree __ARGS((regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 312 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 313 | 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] | 314 | static int match_follows __ARGS((nfa_state_T *startstate, int depth)); |
| 315 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 316 | |
| 317 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 318 | #define EMIT(c) do { \ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 319 | if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 320 | return FAIL; \ |
| 321 | *post_ptr++ = c; \ |
| 322 | } while (0) |
| 323 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 324 | /* |
| 325 | * Initialize internal variables before NFA compilation. |
| 326 | * Return OK on success, FAIL otherwise. |
| 327 | */ |
| 328 | static int |
| 329 | nfa_regcomp_start(expr, re_flags) |
| 330 | char_u *expr; |
| 331 | int re_flags; /* see vim_regcomp() */ |
| 332 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 333 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 334 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 335 | |
| 336 | nstate = 0; |
| 337 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 338 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 339 | nstate_max = (int)(STRLEN(expr) + 1) * 25; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 340 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 341 | /* 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] | 342 | * When it is still not enough realloc_post_list() will be used. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 343 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 344 | |
| 345 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 346 | postfix_size = sizeof(int) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 347 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 348 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 349 | if (post_start == NULL) |
| 350 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 351 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 352 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 353 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 354 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 355 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 356 | /* shared with BT engine */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 357 | regcomp_start(expr, re_flags); |
| 358 | |
| 359 | return OK; |
| 360 | } |
| 361 | |
| 362 | /* |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 363 | * Figure out if the NFA state list starts with an anchor, must match at start |
| 364 | * of the line. |
| 365 | */ |
| 366 | static int |
| 367 | nfa_get_reganch(start, depth) |
| 368 | nfa_state_T *start; |
| 369 | int depth; |
| 370 | { |
| 371 | nfa_state_T *p = start; |
| 372 | |
| 373 | if (depth > 4) |
| 374 | return 0; |
| 375 | |
| 376 | while (p != NULL) |
| 377 | { |
| 378 | switch (p->c) |
| 379 | { |
| 380 | case NFA_BOL: |
| 381 | case NFA_BOF: |
| 382 | return 1; /* yes! */ |
| 383 | |
| 384 | case NFA_ZSTART: |
| 385 | case NFA_ZEND: |
| 386 | case NFA_CURSOR: |
| 387 | case NFA_VISUAL: |
| 388 | |
| 389 | case NFA_MOPEN: |
| 390 | case NFA_MOPEN1: |
| 391 | case NFA_MOPEN2: |
| 392 | case NFA_MOPEN3: |
| 393 | case NFA_MOPEN4: |
| 394 | case NFA_MOPEN5: |
| 395 | case NFA_MOPEN6: |
| 396 | case NFA_MOPEN7: |
| 397 | case NFA_MOPEN8: |
| 398 | case NFA_MOPEN9: |
| 399 | case NFA_NOPEN: |
| 400 | #ifdef FEAT_SYN_HL |
| 401 | case NFA_ZOPEN: |
| 402 | case NFA_ZOPEN1: |
| 403 | case NFA_ZOPEN2: |
| 404 | case NFA_ZOPEN3: |
| 405 | case NFA_ZOPEN4: |
| 406 | case NFA_ZOPEN5: |
| 407 | case NFA_ZOPEN6: |
| 408 | case NFA_ZOPEN7: |
| 409 | case NFA_ZOPEN8: |
| 410 | case NFA_ZOPEN9: |
| 411 | #endif |
| 412 | p = p->out; |
| 413 | break; |
| 414 | |
| 415 | case NFA_SPLIT: |
| 416 | return nfa_get_reganch(p->out, depth + 1) |
| 417 | && nfa_get_reganch(p->out1, depth + 1); |
| 418 | |
| 419 | default: |
| 420 | return 0; /* noooo */ |
| 421 | } |
| 422 | } |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | /* |
| 427 | * Figure out if the NFA state list starts with a character which must match |
| 428 | * at start of the match. |
| 429 | */ |
| 430 | static int |
| 431 | nfa_get_regstart(start, depth) |
| 432 | nfa_state_T *start; |
| 433 | int depth; |
| 434 | { |
| 435 | nfa_state_T *p = start; |
| 436 | |
| 437 | if (depth > 4) |
| 438 | return 0; |
| 439 | |
| 440 | while (p != NULL) |
| 441 | { |
| 442 | switch (p->c) |
| 443 | { |
| 444 | /* all kinds of zero-width matches */ |
| 445 | case NFA_BOL: |
| 446 | case NFA_BOF: |
| 447 | case NFA_BOW: |
| 448 | case NFA_EOW: |
| 449 | case NFA_ZSTART: |
| 450 | case NFA_ZEND: |
| 451 | case NFA_CURSOR: |
| 452 | case NFA_VISUAL: |
| 453 | case NFA_LNUM: |
| 454 | case NFA_LNUM_GT: |
| 455 | case NFA_LNUM_LT: |
| 456 | case NFA_COL: |
| 457 | case NFA_COL_GT: |
| 458 | case NFA_COL_LT: |
| 459 | case NFA_VCOL: |
| 460 | case NFA_VCOL_GT: |
| 461 | case NFA_VCOL_LT: |
| 462 | case NFA_MARK: |
| 463 | case NFA_MARK_GT: |
| 464 | case NFA_MARK_LT: |
| 465 | |
| 466 | case NFA_MOPEN: |
| 467 | case NFA_MOPEN1: |
| 468 | case NFA_MOPEN2: |
| 469 | case NFA_MOPEN3: |
| 470 | case NFA_MOPEN4: |
| 471 | case NFA_MOPEN5: |
| 472 | case NFA_MOPEN6: |
| 473 | case NFA_MOPEN7: |
| 474 | case NFA_MOPEN8: |
| 475 | case NFA_MOPEN9: |
| 476 | case NFA_NOPEN: |
| 477 | #ifdef FEAT_SYN_HL |
| 478 | case NFA_ZOPEN: |
| 479 | case NFA_ZOPEN1: |
| 480 | case NFA_ZOPEN2: |
| 481 | case NFA_ZOPEN3: |
| 482 | case NFA_ZOPEN4: |
| 483 | case NFA_ZOPEN5: |
| 484 | case NFA_ZOPEN6: |
| 485 | case NFA_ZOPEN7: |
| 486 | case NFA_ZOPEN8: |
| 487 | case NFA_ZOPEN9: |
| 488 | #endif |
| 489 | p = p->out; |
| 490 | break; |
| 491 | |
| 492 | case NFA_SPLIT: |
| 493 | { |
| 494 | int c1 = nfa_get_regstart(p->out, depth + 1); |
| 495 | int c2 = nfa_get_regstart(p->out1, depth + 1); |
| 496 | |
| 497 | if (c1 == c2) |
| 498 | return c1; /* yes! */ |
| 499 | return 0; |
| 500 | } |
| 501 | |
| 502 | default: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 503 | if (p->c > 0) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 504 | return p->c; /* yes! */ |
| 505 | return 0; |
| 506 | } |
| 507 | } |
| 508 | return 0; |
| 509 | } |
| 510 | |
| 511 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 512 | * 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] | 513 | * else. If so return a string in allocated memory with what must match after |
| 514 | * regstart. Otherwise return NULL. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 515 | */ |
| 516 | static char_u * |
| 517 | nfa_get_match_text(start) |
| 518 | nfa_state_T *start; |
| 519 | { |
| 520 | nfa_state_T *p = start; |
| 521 | int len = 0; |
| 522 | char_u *ret; |
| 523 | char_u *s; |
| 524 | |
| 525 | if (p->c != NFA_MOPEN) |
| 526 | return NULL; /* just in case */ |
| 527 | p = p->out; |
| 528 | while (p->c > 0) |
| 529 | { |
| 530 | len += MB_CHAR2LEN(p->c); |
| 531 | p = p->out; |
| 532 | } |
| 533 | if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH) |
| 534 | return NULL; |
| 535 | |
| 536 | ret = alloc(len); |
| 537 | if (ret != NULL) |
| 538 | { |
| 539 | len = 0; |
| 540 | p = start->out->out; /* skip first char, it goes into regstart */ |
| 541 | s = ret; |
| 542 | while (p->c > 0) |
| 543 | { |
| 544 | #ifdef FEAT_MBYTE |
| 545 | if (has_mbyte) |
| 546 | s += (*mb_char2bytes)(p->c, s); |
| 547 | else |
| 548 | #endif |
| 549 | *s++ = p->c; |
| 550 | p = p->out; |
| 551 | } |
| 552 | *s = NUL; |
| 553 | } |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 558 | * Allocate more space for post_start. Called when |
| 559 | * running above the estimated number of states. |
| 560 | */ |
| 561 | static int |
| 562 | realloc_post_list() |
| 563 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 564 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 565 | int new_max = nstate_max + 1000; |
| 566 | int *new_start; |
| 567 | int *old_start; |
| 568 | |
| 569 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 570 | if (new_start == NULL) |
| 571 | return FAIL; |
| 572 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 573 | old_start = post_start; |
| 574 | post_start = new_start; |
| 575 | post_ptr = new_start + (post_ptr - old_start); |
| 576 | post_end = post_start + new_max; |
| 577 | vim_free(old_start); |
| 578 | return OK; |
| 579 | } |
| 580 | |
| 581 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 582 | * Search between "start" and "end" and try to recognize a |
| 583 | * character class in expanded form. For example [0-9]. |
| 584 | * On success, return the id the character class to be emitted. |
| 585 | * On failure, return 0 (=FAIL) |
| 586 | * Start points to the first char of the range, while end should point |
| 587 | * to the closing brace. |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 588 | * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may |
| 589 | * need to be interpreted as [a-zA-Z]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 590 | */ |
| 591 | static int |
| 592 | nfa_recognize_char_class(start, end, extra_newl) |
| 593 | char_u *start; |
| 594 | char_u *end; |
| 595 | int extra_newl; |
| 596 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 597 | # define CLASS_not 0x80 |
| 598 | # define CLASS_af 0x40 |
| 599 | # define CLASS_AF 0x20 |
| 600 | # define CLASS_az 0x10 |
| 601 | # define CLASS_AZ 0x08 |
| 602 | # define CLASS_o7 0x04 |
| 603 | # define CLASS_o9 0x02 |
| 604 | # define CLASS_underscore 0x01 |
| 605 | |
| 606 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 607 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 608 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 609 | |
| 610 | if (extra_newl == TRUE) |
| 611 | newl = TRUE; |
| 612 | |
| 613 | if (*end != ']') |
| 614 | return FAIL; |
| 615 | p = start; |
| 616 | if (*p == '^') |
| 617 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 618 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 619 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | while (p < end) |
| 623 | { |
| 624 | if (p + 2 < end && *(p + 1) == '-') |
| 625 | { |
| 626 | switch (*p) |
| 627 | { |
| 628 | case '0': |
| 629 | if (*(p + 2) == '9') |
| 630 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 631 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | else |
| 635 | if (*(p + 2) == '7') |
| 636 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 637 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 638 | break; |
| 639 | } |
| 640 | case 'a': |
| 641 | if (*(p + 2) == 'z') |
| 642 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 643 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 644 | break; |
| 645 | } |
| 646 | else |
| 647 | if (*(p + 2) == 'f') |
| 648 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 649 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 650 | break; |
| 651 | } |
| 652 | case 'A': |
| 653 | if (*(p + 2) == 'Z') |
| 654 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 655 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 656 | break; |
| 657 | } |
| 658 | else |
| 659 | if (*(p + 2) == 'F') |
| 660 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 661 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 662 | break; |
| 663 | } |
| 664 | /* FALLTHROUGH */ |
| 665 | default: |
| 666 | return FAIL; |
| 667 | } |
| 668 | p += 3; |
| 669 | } |
| 670 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 671 | { |
| 672 | newl = TRUE; |
| 673 | p += 2; |
| 674 | } |
| 675 | else if (*p == '_') |
| 676 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 677 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 678 | p ++; |
| 679 | } |
| 680 | else if (*p == '\n') |
| 681 | { |
| 682 | newl = TRUE; |
| 683 | p ++; |
| 684 | } |
| 685 | else |
| 686 | return FAIL; |
| 687 | } /* while (p < end) */ |
| 688 | |
| 689 | if (p != end) |
| 690 | return FAIL; |
| 691 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 692 | if (newl == TRUE) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 693 | extra_newl = NFA_ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 694 | |
| 695 | switch (config) |
| 696 | { |
| 697 | case CLASS_o9: |
| 698 | return extra_newl + NFA_DIGIT; |
| 699 | case CLASS_not | CLASS_o9: |
| 700 | return extra_newl + NFA_NDIGIT; |
| 701 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 702 | return extra_newl + NFA_HEX; |
| 703 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 704 | return extra_newl + NFA_NHEX; |
| 705 | case CLASS_o7: |
| 706 | return extra_newl + NFA_OCTAL; |
| 707 | case CLASS_not | CLASS_o7: |
| 708 | return extra_newl + NFA_NOCTAL; |
| 709 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 710 | return extra_newl + NFA_WORD; |
| 711 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 712 | return extra_newl + NFA_NWORD; |
| 713 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 714 | return extra_newl + NFA_HEAD; |
| 715 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 716 | return extra_newl + NFA_NHEAD; |
| 717 | case CLASS_az | CLASS_AZ: |
| 718 | return extra_newl + NFA_ALPHA; |
| 719 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 720 | return extra_newl + NFA_NALPHA; |
| 721 | case CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 722 | return extra_newl + NFA_LOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 723 | case CLASS_not | CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 724 | return extra_newl + NFA_NLOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 725 | case CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 726 | return extra_newl + NFA_UPPER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 727 | case CLASS_not | CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 728 | return extra_newl + NFA_NUPPER_IC; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 729 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 730 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | /* |
| 734 | * Produce the bytes for equivalence class "c". |
| 735 | * Currently only handles latin1, latin9 and utf-8. |
| 736 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 737 | * equivalent to 'a OR b OR c' |
| 738 | * |
| 739 | * NOTE! When changing this function, also update reg_equi_class() |
| 740 | */ |
| 741 | static int |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 742 | nfa_emit_equi_class(c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 743 | int c; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 744 | { |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 745 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
| 746 | #ifdef FEAT_MBYTE |
| 747 | # define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT); |
| 748 | #else |
| 749 | # define EMITMBC(c) |
| 750 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 751 | |
| 752 | #ifdef FEAT_MBYTE |
| 753 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 754 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 755 | #endif |
| 756 | { |
| 757 | switch (c) |
| 758 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 759 | case 'A': case 0300: case 0301: case 0302: |
| 760 | case 0303: case 0304: case 0305: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 761 | CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd) |
| 762 | CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2) |
| 763 | EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302); |
| 764 | EMIT2(0303); EMIT2(0304); EMIT2(0305); |
| 765 | EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104) |
| 766 | EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0) |
| 767 | EMITMBC(0x1ea2) |
| 768 | return OK; |
| 769 | |
| 770 | case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06) |
| 771 | EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06) |
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 'C': case 0307: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 775 | CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c) |
| 776 | EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108) |
| 777 | EMITMBC(0x10a) EMITMBC(0x10c) |
| 778 | return OK; |
| 779 | |
| 780 | case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a) |
| 781 | CASEMBC(0x1e0e) CASEMBC(0x1e10) |
| 782 | EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a) |
| 783 | EMITMBC(0x1e0e) EMITMBC(0x1e10) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 784 | return OK; |
| 785 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 786 | case 'E': case 0310: case 0311: case 0312: case 0313: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 787 | CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118) |
| 788 | CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc) |
| 789 | EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312); |
| 790 | EMIT2(0313); |
| 791 | EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116) |
| 792 | EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba) |
| 793 | EMITMBC(0x1ebc) |
| 794 | return OK; |
| 795 | |
| 796 | case 'F': CASEMBC(0x1e1e) |
| 797 | EMIT2('F'); EMITMBC(0x1e1e) |
| 798 | return OK; |
| 799 | |
| 800 | case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120) |
| 801 | CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4) |
| 802 | CASEMBC(0x1e20) |
| 803 | EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120) |
| 804 | EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6) |
| 805 | EMITMBC(0x1f4) EMITMBC(0x1e20) |
| 806 | return OK; |
| 807 | |
| 808 | case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22) |
| 809 | CASEMBC(0x1e26) CASEMBC(0x1e28) |
| 810 | EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22) |
| 811 | EMITMBC(0x1e26) EMITMBC(0x1e28) |
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 'I': case 0314: case 0315: case 0316: case 0317: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 815 | CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e) |
| 816 | CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8) |
| 817 | EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316); |
| 818 | EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a) |
| 819 | EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130) |
| 820 | EMITMBC(0x1cf) EMITMBC(0x1ec8) |
| 821 | return OK; |
| 822 | |
| 823 | case 'J': CASEMBC(0x134) |
| 824 | EMIT2('J'); EMITMBC(0x134) |
| 825 | return OK; |
| 826 | |
| 827 | case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30) |
| 828 | CASEMBC(0x1e34) |
| 829 | EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30) |
| 830 | EMITMBC(0x1e34) |
| 831 | return OK; |
| 832 | |
| 833 | case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d) |
| 834 | CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a) |
| 835 | EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d) |
| 836 | EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a) |
| 837 | return OK; |
| 838 | |
| 839 | case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40) |
| 840 | EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 841 | return OK; |
| 842 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 843 | case 'N': case 0321: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 844 | CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44) |
| 845 | CASEMBC(0x1e48) |
| 846 | EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145) |
| 847 | EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 848 | return OK; |
| 849 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 850 | case 'O': case 0322: case 0323: case 0324: case 0325: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 851 | case 0326: case 0330: |
| 852 | CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0) |
| 853 | CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece) |
| 854 | EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324); |
| 855 | EMIT2(0325); EMIT2(0326); EMIT2(0330); |
| 856 | EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150) |
| 857 | EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea) |
| 858 | EMITMBC(0x1ec) EMITMBC(0x1ece) |
| 859 | return OK; |
| 860 | |
| 861 | case 'P': case 0x1e54: case 0x1e56: |
| 862 | EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56) |
| 863 | return OK; |
| 864 | |
| 865 | case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158) |
| 866 | CASEMBC(0x1e58) CASEMBC(0x1e5e) |
| 867 | EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158) |
| 868 | EMITMBC(0x1e58) EMITMBC(0x1e5e) |
| 869 | return OK; |
| 870 | |
| 871 | case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e) |
| 872 | CASEMBC(0x160) CASEMBC(0x1e60) |
| 873 | EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e) |
| 874 | EMITMBC(0x160) EMITMBC(0x1e60) |
| 875 | return OK; |
| 876 | |
| 877 | case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166) |
| 878 | CASEMBC(0x1e6a) CASEMBC(0x1e6e) |
| 879 | EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166) |
| 880 | EMITMBC(0x1e6a) EMITMBC(0x1e6e) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 881 | return OK; |
| 882 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 883 | case 'U': case 0331: case 0332: case 0333: case 0334: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 884 | CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e) |
| 885 | CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3) |
| 886 | CASEMBC(0x1ee6) |
| 887 | EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333); |
| 888 | EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a) |
| 889 | EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170) |
| 890 | EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3) |
| 891 | EMITMBC(0x1ee6) |
| 892 | return OK; |
| 893 | |
| 894 | case 'V': CASEMBC(0x1e7c) |
| 895 | EMIT2('V'); EMITMBC(0x1e7c) |
| 896 | return OK; |
| 897 | |
| 898 | case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82) |
| 899 | CASEMBC(0x1e84) CASEMBC(0x1e86) |
| 900 | EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82) |
| 901 | EMITMBC(0x1e84) EMITMBC(0x1e86) |
| 902 | return OK; |
| 903 | |
| 904 | case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c) |
| 905 | EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 906 | return OK; |
| 907 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 908 | case 'Y': case 0335: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 909 | CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2) |
| 910 | CASEMBC(0x1ef6) CASEMBC(0x1ef8) |
| 911 | EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178) |
| 912 | EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6) |
| 913 | EMITMBC(0x1ef8) |
| 914 | return OK; |
| 915 | |
| 916 | case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d) |
| 917 | CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94) |
| 918 | EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d) |
| 919 | EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 920 | return OK; |
| 921 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 922 | case 'a': case 0340: case 0341: case 0342: |
| 923 | case 0343: case 0344: case 0345: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 924 | CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce) |
| 925 | CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3) |
| 926 | EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342); |
| 927 | EMIT2(0343); EMIT2(0344); EMIT2(0345); |
| 928 | EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105) |
| 929 | EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1) |
| 930 | EMITMBC(0x1ea3) |
| 931 | return OK; |
| 932 | |
| 933 | case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07) |
| 934 | EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 935 | return OK; |
| 936 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 937 | case 'c': case 0347: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 938 | CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d) |
| 939 | EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109) |
| 940 | EMITMBC(0x10b) EMITMBC(0x10d) |
| 941 | return OK; |
| 942 | |
| 943 | case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b) |
| 944 | CASEMBC(0x1e11) |
| 945 | EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) EMITMBC(0x1e0b) |
| 946 | EMITMBC(0x01e0f) EMITMBC(0x1e11) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 947 | return OK; |
| 948 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 949 | case 'e': case 0350: case 0351: case 0352: case 0353: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 950 | CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119) |
| 951 | CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd) |
| 952 | EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352); |
| 953 | EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115) |
| 954 | EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b) |
| 955 | EMITMBC(0x1ebb) EMITMBC(0x1ebd) |
| 956 | return OK; |
| 957 | |
| 958 | case 'f': CASEMBC(0x1e1f) |
| 959 | EMIT2('f'); EMITMBC(0x1e1f) |
| 960 | return OK; |
| 961 | |
| 962 | case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121) |
| 963 | CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5) |
| 964 | CASEMBC(0x1e21) |
| 965 | EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121) |
| 966 | EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7) |
| 967 | EMITMBC(0x1f5) EMITMBC(0x1e21) |
| 968 | return OK; |
| 969 | |
| 970 | case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23) |
| 971 | CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96) |
| 972 | EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23) |
| 973 | EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 974 | return OK; |
| 975 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 976 | case 'i': case 0354: case 0355: case 0356: case 0357: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 977 | CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f) |
| 978 | CASEMBC(0x1d0) CASEMBC(0x1ec9) |
| 979 | EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356); |
| 980 | EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b) |
| 981 | EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0) |
| 982 | EMITMBC(0x1ec9) |
| 983 | return OK; |
| 984 | |
| 985 | case 'j': CASEMBC(0x135) CASEMBC(0x1f0) |
| 986 | EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0) |
| 987 | return OK; |
| 988 | |
| 989 | case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31) |
| 990 | CASEMBC(0x1e35) |
| 991 | EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31) |
| 992 | EMITMBC(0x1e35) |
| 993 | return OK; |
| 994 | |
| 995 | case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e) |
| 996 | CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b) |
| 997 | EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e) |
| 998 | EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b) |
| 999 | return OK; |
| 1000 | |
| 1001 | case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41) |
| 1002 | EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1003 | return OK; |
| 1004 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1005 | case 'n': case 0361: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1006 | CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149) |
| 1007 | CASEMBC(0x1e45) CASEMBC(0x1e49) |
| 1008 | EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146) |
| 1009 | EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45) |
| 1010 | EMITMBC(0x1e49) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1011 | return OK; |
| 1012 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1013 | case 'o': case 0362: case 0363: case 0364: case 0365: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1014 | case 0366: case 0370: |
| 1015 | CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1) |
| 1016 | CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf) |
| 1017 | EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364); |
| 1018 | EMIT2(0365); EMIT2(0366); EMIT2(0370); |
| 1019 | EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151) |
| 1020 | EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb) |
| 1021 | EMITMBC(0x1ed) EMITMBC(0x1ecf) |
| 1022 | return OK; |
| 1023 | |
| 1024 | case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57) |
| 1025 | EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57) |
| 1026 | return OK; |
| 1027 | |
| 1028 | case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159) |
| 1029 | CASEMBC(0x1e59) CASEMBC(0x1e5f) |
| 1030 | EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159) |
| 1031 | EMITMBC(0x1e59) EMITMBC(0x1e5f) |
| 1032 | return OK; |
| 1033 | |
| 1034 | case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f) |
| 1035 | CASEMBC(0x161) CASEMBC(0x1e61) |
| 1036 | EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f) |
| 1037 | EMITMBC(0x161) EMITMBC(0x1e61) |
| 1038 | return OK; |
| 1039 | |
| 1040 | case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167) |
| 1041 | CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97) |
| 1042 | EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167) |
| 1043 | EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1044 | return OK; |
| 1045 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1046 | case 'u': case 0371: case 0372: case 0373: case 0374: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1047 | CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f) |
| 1048 | CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4) |
| 1049 | CASEMBC(0x1ee7) |
| 1050 | EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373); |
| 1051 | EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b) |
| 1052 | EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171) |
| 1053 | EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4) |
| 1054 | EMITMBC(0x1ee7) |
| 1055 | return OK; |
| 1056 | |
| 1057 | case 'v': CASEMBC(0x1e7d) |
| 1058 | EMIT2('v'); EMITMBC(0x1e7d) |
| 1059 | return OK; |
| 1060 | |
| 1061 | case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83) |
| 1062 | CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98) |
| 1063 | EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83) |
| 1064 | EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98) |
| 1065 | return OK; |
| 1066 | |
| 1067 | case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d) |
| 1068 | EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1069 | return OK; |
| 1070 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1071 | case 'y': case 0375: case 0377: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1072 | CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99) |
| 1073 | CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9) |
| 1074 | EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177) |
| 1075 | EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3) |
| 1076 | EMITMBC(0x1ef7) EMITMBC(0x1ef9) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1077 | return OK; |
| 1078 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1079 | case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e) |
| 1080 | CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) |
| 1081 | EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e) |
| 1082 | EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95) |
| 1083 | return OK; |
| 1084 | |
| 1085 | /* default: character itself */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1089 | EMIT2(c); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1090 | return OK; |
| 1091 | #undef EMIT2 |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1092 | #undef EMITMBC |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | /* |
| 1096 | * Code to parse regular expression. |
| 1097 | * |
| 1098 | * We try to reuse parsing functions in regexp.c to |
| 1099 | * minimize surprise and keep the syntax consistent. |
| 1100 | */ |
| 1101 | |
| 1102 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1103 | * Parse the lowest level. |
| 1104 | * |
| 1105 | * An atom can be one of a long list of items. Many atoms match one character |
| 1106 | * in the text. It is often an ordinary character or a character class. |
| 1107 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 1108 | * is only for syntax highlighting. |
| 1109 | * |
| 1110 | * atom ::= ordinary-atom |
| 1111 | * or \( pattern \) |
| 1112 | * or \%( pattern \) |
| 1113 | * or \z( pattern \) |
| 1114 | */ |
| 1115 | static int |
| 1116 | nfa_regatom() |
| 1117 | { |
| 1118 | int c; |
| 1119 | int charclass; |
| 1120 | int equiclass; |
| 1121 | int collclass; |
| 1122 | int got_coll_char; |
| 1123 | char_u *p; |
| 1124 | char_u *endp; |
| 1125 | #ifdef FEAT_MBYTE |
| 1126 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1127 | #endif |
| 1128 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1129 | int emit_range; |
| 1130 | int negated; |
| 1131 | int result; |
| 1132 | int startc = -1; |
| 1133 | int endc = -1; |
| 1134 | int oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1135 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1136 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1137 | switch (c) |
| 1138 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1139 | case NUL: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1140 | EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); |
| 1141 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1142 | case Magic('^'): |
| 1143 | EMIT(NFA_BOL); |
| 1144 | break; |
| 1145 | |
| 1146 | case Magic('$'): |
| 1147 | EMIT(NFA_EOL); |
| 1148 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1149 | had_eol = TRUE; |
| 1150 | #endif |
| 1151 | break; |
| 1152 | |
| 1153 | case Magic('<'): |
| 1154 | EMIT(NFA_BOW); |
| 1155 | break; |
| 1156 | |
| 1157 | case Magic('>'): |
| 1158 | EMIT(NFA_EOW); |
| 1159 | break; |
| 1160 | |
| 1161 | case Magic('_'): |
| 1162 | c = no_Magic(getchr()); |
| 1163 | if (c == '^') /* "\_^" is start-of-line */ |
| 1164 | { |
| 1165 | EMIT(NFA_BOL); |
| 1166 | break; |
| 1167 | } |
| 1168 | if (c == '$') /* "\_$" is end-of-line */ |
| 1169 | { |
| 1170 | EMIT(NFA_EOL); |
| 1171 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1172 | had_eol = TRUE; |
| 1173 | #endif |
| 1174 | break; |
| 1175 | } |
| 1176 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1177 | extra = NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1178 | |
| 1179 | /* "\_[" is collection plus newline */ |
| 1180 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1181 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1182 | |
| 1183 | /* "\_x" is character class plus newline */ |
| 1184 | /*FALLTHROUGH*/ |
| 1185 | |
| 1186 | /* |
| 1187 | * Character classes. |
| 1188 | */ |
| 1189 | case Magic('.'): |
| 1190 | case Magic('i'): |
| 1191 | case Magic('I'): |
| 1192 | case Magic('k'): |
| 1193 | case Magic('K'): |
| 1194 | case Magic('f'): |
| 1195 | case Magic('F'): |
| 1196 | case Magic('p'): |
| 1197 | case Magic('P'): |
| 1198 | case Magic('s'): |
| 1199 | case Magic('S'): |
| 1200 | case Magic('d'): |
| 1201 | case Magic('D'): |
| 1202 | case Magic('x'): |
| 1203 | case Magic('X'): |
| 1204 | case Magic('o'): |
| 1205 | case Magic('O'): |
| 1206 | case Magic('w'): |
| 1207 | case Magic('W'): |
| 1208 | case Magic('h'): |
| 1209 | case Magic('H'): |
| 1210 | case Magic('a'): |
| 1211 | case Magic('A'): |
| 1212 | case Magic('l'): |
| 1213 | case Magic('L'): |
| 1214 | case Magic('u'): |
| 1215 | case Magic('U'): |
| 1216 | p = vim_strchr(classchars, no_Magic(c)); |
| 1217 | if (p == NULL) |
| 1218 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1219 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 1220 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1221 | } |
| 1222 | #ifdef FEAT_MBYTE |
| 1223 | /* When '.' is followed by a composing char ignore the dot, so that |
| 1224 | * the composing char is matched here. */ |
| 1225 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 1226 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1227 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1228 | c = getchr(); |
| 1229 | goto nfa_do_multibyte; |
| 1230 | } |
| 1231 | #endif |
| 1232 | EMIT(nfa_classcodes[p - classchars]); |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1233 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1234 | { |
| 1235 | EMIT(NFA_NEWL); |
| 1236 | EMIT(NFA_OR); |
| 1237 | regflags |= RF_HASNL; |
| 1238 | } |
| 1239 | break; |
| 1240 | |
| 1241 | case Magic('n'): |
| 1242 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1243 | /* In a string "\n" matches a newline character. */ |
| 1244 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1245 | else |
| 1246 | { |
| 1247 | /* In buffer text "\n" matches the end of a line. */ |
| 1248 | EMIT(NFA_NEWL); |
| 1249 | regflags |= RF_HASNL; |
| 1250 | } |
| 1251 | break; |
| 1252 | |
| 1253 | case Magic('('): |
| 1254 | if (nfa_reg(REG_PAREN) == FAIL) |
| 1255 | return FAIL; /* cascaded error */ |
| 1256 | break; |
| 1257 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1258 | case Magic('|'): |
| 1259 | case Magic('&'): |
| 1260 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1261 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1262 | return FAIL; |
| 1263 | |
| 1264 | case Magic('='): |
| 1265 | case Magic('?'): |
| 1266 | case Magic('+'): |
| 1267 | case Magic('@'): |
| 1268 | case Magic('*'): |
| 1269 | case Magic('{'): |
| 1270 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1271 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1272 | return FAIL; |
| 1273 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 1274 | case Magic('~'): |
| 1275 | { |
| 1276 | char_u *lp; |
| 1277 | |
| 1278 | /* Previous substitute pattern. |
| 1279 | * Generated as "\%(pattern\)". */ |
| 1280 | if (reg_prev_sub == NULL) |
| 1281 | { |
| 1282 | EMSG(_(e_nopresub)); |
| 1283 | return FAIL; |
| 1284 | } |
| 1285 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 1286 | { |
| 1287 | EMIT(PTR2CHAR(lp)); |
| 1288 | if (lp != reg_prev_sub) |
| 1289 | EMIT(NFA_CONCAT); |
| 1290 | } |
| 1291 | EMIT(NFA_NOPEN); |
| 1292 | break; |
| 1293 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1294 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 1295 | case Magic('1'): |
| 1296 | case Magic('2'): |
| 1297 | case Magic('3'): |
| 1298 | case Magic('4'): |
| 1299 | case Magic('5'): |
| 1300 | case Magic('6'): |
| 1301 | case Magic('7'): |
| 1302 | case Magic('8'): |
| 1303 | case Magic('9'): |
| 1304 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 1305 | nfa_has_backref = TRUE; |
| 1306 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1307 | |
| 1308 | case Magic('z'): |
| 1309 | c = no_Magic(getchr()); |
| 1310 | switch (c) |
| 1311 | { |
| 1312 | case 's': |
| 1313 | EMIT(NFA_ZSTART); |
| 1314 | break; |
| 1315 | case 'e': |
| 1316 | EMIT(NFA_ZEND); |
| 1317 | nfa_has_zend = TRUE; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1318 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1319 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1320 | case '1': |
| 1321 | case '2': |
| 1322 | case '3': |
| 1323 | case '4': |
| 1324 | case '5': |
| 1325 | case '6': |
| 1326 | case '7': |
| 1327 | case '8': |
| 1328 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1329 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1330 | if (reg_do_extmatch != REX_USE) |
| 1331 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1332 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1333 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1334 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1335 | re_has_z = REX_USE; |
| 1336 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1337 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1338 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1339 | if (reg_do_extmatch != REX_SET) |
| 1340 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1341 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1342 | return FAIL; /* cascaded error */ |
| 1343 | re_has_z = REX_SET; |
| 1344 | break; |
| 1345 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1346 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1347 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1348 | no_Magic(c)); |
| 1349 | return FAIL; |
| 1350 | } |
| 1351 | break; |
| 1352 | |
| 1353 | case Magic('%'): |
| 1354 | c = no_Magic(getchr()); |
| 1355 | switch (c) |
| 1356 | { |
| 1357 | /* () without a back reference */ |
| 1358 | case '(': |
| 1359 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1360 | return FAIL; |
| 1361 | EMIT(NFA_NOPEN); |
| 1362 | break; |
| 1363 | |
| 1364 | case 'd': /* %d123 decimal */ |
| 1365 | case 'o': /* %o123 octal */ |
| 1366 | case 'x': /* %xab hex 2 */ |
| 1367 | case 'u': /* %uabcd hex 4 */ |
| 1368 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1369 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1370 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1371 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1372 | switch (c) |
| 1373 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1374 | case 'd': nr = getdecchrs(); break; |
| 1375 | case 'o': nr = getoctchrs(); break; |
| 1376 | case 'x': nr = gethexchrs(2); break; |
| 1377 | case 'u': nr = gethexchrs(4); break; |
| 1378 | case 'U': nr = gethexchrs(8); break; |
| 1379 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1382 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1383 | EMSG2_RET_FAIL( |
| 1384 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1385 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1386 | /* A NUL is stored in the text as NL */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1387 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1388 | EMIT(nr == 0 ? 0x0a : nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1389 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1390 | break; |
| 1391 | |
| 1392 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1393 | * pattern -- regardless of whether or not it makes sense. */ |
| 1394 | case '^': |
| 1395 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1396 | break; |
| 1397 | |
| 1398 | case '$': |
| 1399 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1400 | break; |
| 1401 | |
| 1402 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1403 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1404 | break; |
| 1405 | |
| 1406 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1407 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1408 | break; |
| 1409 | |
| 1410 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1411 | { |
| 1412 | int n; |
| 1413 | |
| 1414 | /* \%[abc] */ |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1415 | for (n = 0; (c = peekchr()) != ']'; ++n) |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1416 | { |
| 1417 | if (c == NUL) |
| 1418 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1419 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1420 | /* recursive call! */ |
| 1421 | if (nfa_regatom() == FAIL) |
| 1422 | return FAIL; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1423 | } |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1424 | getchr(); /* get the ] */ |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1425 | if (n == 0) |
| 1426 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1427 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1428 | EMIT(NFA_OPT_CHARS); |
| 1429 | EMIT(n); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1430 | |
| 1431 | /* Emit as "\%(\%[abc]\)" to be able to handle |
| 1432 | * "\%[abc]*" which would cause the empty string to be |
| 1433 | * matched an unlimited number of times. NFA_NOPEN is |
| 1434 | * added only once at a position, while NFA_SPLIT is |
| 1435 | * added multiple times. This is more efficient than |
| 1436 | * not allowsing NFA_SPLIT multiple times, it is used |
| 1437 | * a lot. */ |
| 1438 | EMIT(NFA_NOPEN); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1439 | break; |
| 1440 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1441 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1442 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1443 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1444 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1445 | int cmp = c; |
| 1446 | |
| 1447 | if (c == '<' || c == '>') |
| 1448 | c = getchr(); |
| 1449 | while (VIM_ISDIGIT(c)) |
| 1450 | { |
| 1451 | n = n * 10 + (c - '0'); |
| 1452 | c = getchr(); |
| 1453 | } |
| 1454 | if (c == 'l' || c == 'c' || c == 'v') |
| 1455 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1456 | if (c == 'l') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1457 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1458 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1459 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1460 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1461 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1462 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1463 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1464 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1465 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1466 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1467 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1468 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1469 | break; |
| 1470 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1471 | else if (c == '\'' && n == 0) |
| 1472 | { |
| 1473 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1474 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1475 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1476 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1477 | break; |
| 1478 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1479 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1480 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1481 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1482 | return FAIL; |
| 1483 | } |
| 1484 | break; |
| 1485 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1486 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1487 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1488 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1489 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1490 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1491 | * Each character is produced as a regular state, using |
| 1492 | * NFA_CONCAT to bind them together. |
| 1493 | * Besides normal characters there can be: |
| 1494 | * - character classes NFA_CLASS_* |
| 1495 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1496 | */ |
| 1497 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1498 | p = regparse; |
| 1499 | endp = skip_anyof(p); |
| 1500 | if (*endp == ']') |
| 1501 | { |
| 1502 | /* |
| 1503 | * Try to reverse engineer character classes. For example, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1504 | * recognize that [0-9] stands for \d and [A-Za-z_] for \h, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1505 | * and perform the necessary substitutions in the NFA. |
| 1506 | */ |
| 1507 | result = nfa_recognize_char_class(regparse, endp, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1508 | extra == NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1509 | if (result != FAIL) |
| 1510 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1511 | if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1512 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1513 | EMIT(result - NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1514 | EMIT(NFA_NEWL); |
| 1515 | EMIT(NFA_OR); |
| 1516 | } |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1517 | else |
| 1518 | EMIT(result); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1519 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1520 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1521 | return OK; |
| 1522 | } |
| 1523 | /* |
| 1524 | * Failed to recognize a character class. Use the simple |
| 1525 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1526 | */ |
| 1527 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1528 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1529 | if (*regparse == '^') /* negated range */ |
| 1530 | { |
| 1531 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1532 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1533 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1534 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1535 | else |
| 1536 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1537 | if (*regparse == '-') |
| 1538 | { |
| 1539 | startc = '-'; |
| 1540 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1541 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1542 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1543 | } |
| 1544 | /* Emit the OR branches for each character in the [] */ |
| 1545 | emit_range = FALSE; |
| 1546 | while (regparse < endp) |
| 1547 | { |
| 1548 | oldstartc = startc; |
| 1549 | startc = -1; |
| 1550 | got_coll_char = FALSE; |
| 1551 | if (*regparse == '[') |
| 1552 | { |
| 1553 | /* Check for [: :], [= =], [. .] */ |
| 1554 | equiclass = collclass = 0; |
| 1555 | charclass = get_char_class(®parse); |
| 1556 | if (charclass == CLASS_NONE) |
| 1557 | { |
| 1558 | equiclass = get_equi_class(®parse); |
| 1559 | if (equiclass == 0) |
| 1560 | collclass = get_coll_element(®parse); |
| 1561 | } |
| 1562 | |
| 1563 | /* Character class like [:alpha:] */ |
| 1564 | if (charclass != CLASS_NONE) |
| 1565 | { |
| 1566 | switch (charclass) |
| 1567 | { |
| 1568 | case CLASS_ALNUM: |
| 1569 | EMIT(NFA_CLASS_ALNUM); |
| 1570 | break; |
| 1571 | case CLASS_ALPHA: |
| 1572 | EMIT(NFA_CLASS_ALPHA); |
| 1573 | break; |
| 1574 | case CLASS_BLANK: |
| 1575 | EMIT(NFA_CLASS_BLANK); |
| 1576 | break; |
| 1577 | case CLASS_CNTRL: |
| 1578 | EMIT(NFA_CLASS_CNTRL); |
| 1579 | break; |
| 1580 | case CLASS_DIGIT: |
| 1581 | EMIT(NFA_CLASS_DIGIT); |
| 1582 | break; |
| 1583 | case CLASS_GRAPH: |
| 1584 | EMIT(NFA_CLASS_GRAPH); |
| 1585 | break; |
| 1586 | case CLASS_LOWER: |
| 1587 | EMIT(NFA_CLASS_LOWER); |
| 1588 | break; |
| 1589 | case CLASS_PRINT: |
| 1590 | EMIT(NFA_CLASS_PRINT); |
| 1591 | break; |
| 1592 | case CLASS_PUNCT: |
| 1593 | EMIT(NFA_CLASS_PUNCT); |
| 1594 | break; |
| 1595 | case CLASS_SPACE: |
| 1596 | EMIT(NFA_CLASS_SPACE); |
| 1597 | break; |
| 1598 | case CLASS_UPPER: |
| 1599 | EMIT(NFA_CLASS_UPPER); |
| 1600 | break; |
| 1601 | case CLASS_XDIGIT: |
| 1602 | EMIT(NFA_CLASS_XDIGIT); |
| 1603 | break; |
| 1604 | case CLASS_TAB: |
| 1605 | EMIT(NFA_CLASS_TAB); |
| 1606 | break; |
| 1607 | case CLASS_RETURN: |
| 1608 | EMIT(NFA_CLASS_RETURN); |
| 1609 | break; |
| 1610 | case CLASS_BACKSPACE: |
| 1611 | EMIT(NFA_CLASS_BACKSPACE); |
| 1612 | break; |
| 1613 | case CLASS_ESCAPE: |
| 1614 | EMIT(NFA_CLASS_ESCAPE); |
| 1615 | break; |
| 1616 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1617 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1618 | continue; |
| 1619 | } |
| 1620 | /* Try equivalence class [=a=] and the like */ |
| 1621 | if (equiclass != 0) |
| 1622 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1623 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1624 | if (result == FAIL) |
| 1625 | { |
| 1626 | /* should never happen */ |
| 1627 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1628 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1629 | continue; |
| 1630 | } |
| 1631 | /* Try collating class like [. .] */ |
| 1632 | if (collclass != 0) |
| 1633 | { |
| 1634 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1635 | /* Will emit the proper atom at the end of the |
| 1636 | * while loop. */ |
| 1637 | } |
| 1638 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1639 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1640 | * start character. */ |
| 1641 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1642 | { |
| 1643 | emit_range = TRUE; |
| 1644 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1645 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1646 | continue; /* reading the end of the range */ |
| 1647 | } |
| 1648 | |
| 1649 | /* Now handle simple and escaped characters. |
| 1650 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1651 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1652 | * 'cpoptions' is not included. |
| 1653 | * Posix doesn't recognize backslash at all. |
| 1654 | */ |
| 1655 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1656 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1657 | && regparse + 1 <= endp |
| 1658 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1659 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1660 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1661 | != NULL) |
| 1662 | ) |
| 1663 | ) |
| 1664 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1665 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1666 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1667 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1668 | startc = reg_string ? NL : NFA_NEWL; |
| 1669 | else |
| 1670 | if (*regparse == 'd' |
| 1671 | || *regparse == 'o' |
| 1672 | || *regparse == 'x' |
| 1673 | || *regparse == 'u' |
| 1674 | || *regparse == 'U' |
| 1675 | ) |
| 1676 | { |
| 1677 | /* TODO(RE) This needs more testing */ |
| 1678 | startc = coll_get_char(); |
| 1679 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1680 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1681 | } |
| 1682 | else |
| 1683 | { |
| 1684 | /* \r,\t,\e,\b */ |
| 1685 | startc = backslash_trans(*regparse); |
| 1686 | } |
| 1687 | } |
| 1688 | |
| 1689 | /* Normal printable char */ |
| 1690 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1691 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1692 | |
| 1693 | /* Previous char was '-', so this char is end of range. */ |
| 1694 | if (emit_range) |
| 1695 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1696 | endc = startc; |
| 1697 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1698 | if (startc > endc) |
| 1699 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1700 | |
| 1701 | if (endc > startc + 2) |
| 1702 | { |
| 1703 | /* Emit a range instead of the sequence of |
| 1704 | * individual characters. */ |
| 1705 | if (startc == 0) |
| 1706 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1707 | EMIT(1); |
| 1708 | else |
| 1709 | --post_ptr; /* remove NFA_CONCAT */ |
| 1710 | EMIT(endc); |
| 1711 | EMIT(NFA_RANGE); |
| 1712 | EMIT(NFA_CONCAT); |
| 1713 | } |
| 1714 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1715 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1716 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1717 | || (*mb_char2len)(endc) > 1)) |
| 1718 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1719 | /* Emit the characters in the range. |
| 1720 | * "startc" was already emitted, so skip it. |
| 1721 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1722 | for (c = startc + 1; c <= endc; c++) |
| 1723 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1724 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1725 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1726 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1727 | } |
| 1728 | else |
| 1729 | #endif |
| 1730 | { |
| 1731 | #ifdef EBCDIC |
| 1732 | int alpha_only = FALSE; |
| 1733 | |
| 1734 | /* for alphabetical range skip the gaps |
| 1735 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1736 | if (isalpha(startc) && isalpha(endc)) |
| 1737 | alpha_only = TRUE; |
| 1738 | #endif |
| 1739 | /* Emit the range. "startc" was already emitted, so |
| 1740 | * skip it. */ |
| 1741 | for (c = startc + 1; c <= endc; c++) |
| 1742 | #ifdef EBCDIC |
| 1743 | if (!alpha_only || isalpha(startc)) |
| 1744 | #endif |
| 1745 | { |
| 1746 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1747 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1748 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1749 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1750 | emit_range = FALSE; |
| 1751 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1752 | } |
| 1753 | else |
| 1754 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1755 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1756 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1757 | * Normally, simply emit startc. But if we get char |
| 1758 | * code=0 from a collating char, then replace it with |
| 1759 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1760 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1761 | * the backtracking engine. */ |
| 1762 | if (startc == NFA_NEWL) |
| 1763 | { |
| 1764 | /* Line break can't be matched as part of the |
| 1765 | * collection, add an OR below. But not for negated |
| 1766 | * range. */ |
| 1767 | if (!negated) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1768 | extra = NFA_ADD_NL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1769 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1770 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1771 | { |
| 1772 | if (got_coll_char == TRUE && startc == 0) |
| 1773 | EMIT(0x0a); |
| 1774 | else |
| 1775 | EMIT(startc); |
| 1776 | EMIT(NFA_CONCAT); |
| 1777 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1778 | } |
| 1779 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1780 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1781 | } /* while (p < endp) */ |
| 1782 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1783 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1784 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1785 | { |
| 1786 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1787 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1788 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1789 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1790 | /* skip the trailing ] */ |
| 1791 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1792 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1793 | |
| 1794 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1795 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1796 | EMIT(NFA_END_NEG_COLL); |
| 1797 | else |
| 1798 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1799 | |
| 1800 | /* \_[] also matches \n but it's not negated */ |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1801 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1802 | { |
| 1803 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1804 | EMIT(NFA_OR); |
| 1805 | } |
| 1806 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1807 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1808 | } /* if exists closing ] */ |
| 1809 | |
| 1810 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1811 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1812 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1813 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1814 | default: |
| 1815 | { |
| 1816 | #ifdef FEAT_MBYTE |
| 1817 | int plen; |
| 1818 | |
| 1819 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1820 | /* plen is length of current char with composing chars */ |
| 1821 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1822 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1823 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1824 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1825 | int i = 0; |
| 1826 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1827 | /* A base character plus composing characters, or just one |
| 1828 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1829 | * This requires creating a separate atom as if enclosing |
| 1830 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1831 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1832 | * building the postfix form, not the NFA itself; |
| 1833 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1834 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1835 | for (;;) |
| 1836 | { |
| 1837 | EMIT(c); |
| 1838 | if (i > 0) |
| 1839 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1840 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1841 | break; |
| 1842 | c = utf_ptr2char(old_regparse + i); |
| 1843 | } |
| 1844 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1845 | regparse = old_regparse + plen; |
| 1846 | } |
| 1847 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1848 | #endif |
| 1849 | { |
| 1850 | c = no_Magic(c); |
| 1851 | EMIT(c); |
| 1852 | } |
| 1853 | return OK; |
| 1854 | } |
| 1855 | } |
| 1856 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1857 | return OK; |
| 1858 | } |
| 1859 | |
| 1860 | /* |
| 1861 | * Parse something followed by possible [*+=]. |
| 1862 | * |
| 1863 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1864 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1865 | * characters: "", "a", "aa", etc. |
| 1866 | * |
| 1867 | * piece ::= atom |
| 1868 | * or atom multi |
| 1869 | */ |
| 1870 | static int |
| 1871 | nfa_regpiece() |
| 1872 | { |
| 1873 | int i; |
| 1874 | int op; |
| 1875 | int ret; |
| 1876 | long minval, maxval; |
| 1877 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1878 | parse_state_T old_state; |
| 1879 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1880 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1881 | int old_post_pos; |
| 1882 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1883 | int quest; |
| 1884 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1885 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1886 | * next. */ |
| 1887 | save_parse_state(&old_state); |
| 1888 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1889 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1890 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1891 | |
| 1892 | ret = nfa_regatom(); |
| 1893 | if (ret == FAIL) |
| 1894 | return FAIL; /* cascaded error */ |
| 1895 | |
| 1896 | op = peekchr(); |
| 1897 | if (re_multi_type(op) == NOT_MULTI) |
| 1898 | return OK; |
| 1899 | |
| 1900 | skipchr(); |
| 1901 | switch (op) |
| 1902 | { |
| 1903 | case Magic('*'): |
| 1904 | EMIT(NFA_STAR); |
| 1905 | break; |
| 1906 | |
| 1907 | case Magic('+'): |
| 1908 | /* |
| 1909 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1910 | * first and only submatch would be "aaa". But the backtracking |
| 1911 | * engine interprets the plus as "try matching one more time", and |
| 1912 | * a* matches a second time at the end of the input, the empty |
| 1913 | * string. |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1914 | * The submatch will be the empty string. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1915 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1916 | * In order to be consistent with the old engine, we replace |
| 1917 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1918 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1919 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1920 | curchr = -1; |
| 1921 | if (nfa_regatom() == FAIL) |
| 1922 | return FAIL; |
| 1923 | EMIT(NFA_STAR); |
| 1924 | EMIT(NFA_CONCAT); |
| 1925 | skipchr(); /* skip the \+ */ |
| 1926 | break; |
| 1927 | |
| 1928 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1929 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1930 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1931 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1932 | switch(op) |
| 1933 | { |
| 1934 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1935 | /* \@= */ |
| 1936 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1937 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1938 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1939 | /* \@! */ |
| 1940 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1941 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1942 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1943 | op = no_Magic(getchr()); |
| 1944 | if (op == '=') |
| 1945 | /* \@<= */ |
| 1946 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1947 | else if (op == '!') |
| 1948 | /* \@<! */ |
| 1949 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1950 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1951 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1952 | /* \@> */ |
| 1953 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1954 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1955 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1956 | if (i == 0) |
| 1957 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1958 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1959 | return FAIL; |
| 1960 | } |
| 1961 | EMIT(i); |
| 1962 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1963 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1964 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1965 | break; |
| 1966 | |
| 1967 | case Magic('?'): |
| 1968 | case Magic('='): |
| 1969 | EMIT(NFA_QUEST); |
| 1970 | break; |
| 1971 | |
| 1972 | case Magic('{'): |
| 1973 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1974 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1975 | * version of '?' |
| 1976 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1977 | * parenthesis have the same id |
| 1978 | */ |
| 1979 | |
| 1980 | greedy = TRUE; |
| 1981 | c2 = peekchr(); |
| 1982 | if (c2 == '-' || c2 == Magic('-')) |
| 1983 | { |
| 1984 | skipchr(); |
| 1985 | greedy = FALSE; |
| 1986 | } |
| 1987 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1988 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 1989 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1990 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 1991 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1992 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1993 | { |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 1994 | if (greedy) |
| 1995 | /* \{}, \{0,} */ |
| 1996 | EMIT(NFA_STAR); |
| 1997 | else |
| 1998 | /* \{-}, \{-0,} */ |
| 1999 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2000 | break; |
| 2001 | } |
| 2002 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2003 | /* Special case: x{0} or x{-0} */ |
| 2004 | if (maxval == 0) |
| 2005 | { |
| 2006 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2007 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2008 | /* NFA_EMPTY is 0-length and works everywhere */ |
| 2009 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2010 | return OK; |
| 2011 | } |
| 2012 | |
| 2013 | /* Ignore previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2014 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2015 | /* Save parse state after the repeated atom and the \{} */ |
| 2016 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2017 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2018 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 2019 | for (i = 0; i < maxval; i++) |
| 2020 | { |
| 2021 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2022 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2023 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2024 | if (nfa_regatom() == FAIL) |
| 2025 | return FAIL; |
| 2026 | /* after "minval" times, atoms are optional */ |
| 2027 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2028 | { |
| 2029 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2030 | { |
| 2031 | if (greedy) |
| 2032 | EMIT(NFA_STAR); |
| 2033 | else |
| 2034 | EMIT(NFA_STAR_NONGREEDY); |
| 2035 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2036 | else |
| 2037 | EMIT(quest); |
| 2038 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2039 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2040 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2041 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 2042 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2043 | } |
| 2044 | |
| 2045 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2046 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2047 | curchr = -1; |
| 2048 | |
| 2049 | break; |
| 2050 | |
| 2051 | |
| 2052 | default: |
| 2053 | break; |
| 2054 | } /* end switch */ |
| 2055 | |
| 2056 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2057 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2058 | 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] | 2059 | |
| 2060 | return OK; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * Parse one or more pieces, concatenated. It matches a match for the |
| 2065 | * first piece, followed by a match for the second piece, etc. Example: |
| 2066 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 2067 | * |
| 2068 | * concat ::= piece |
| 2069 | * or piece piece |
| 2070 | * or piece piece piece |
| 2071 | * etc. |
| 2072 | */ |
| 2073 | static int |
| 2074 | nfa_regconcat() |
| 2075 | { |
| 2076 | int cont = TRUE; |
| 2077 | int first = TRUE; |
| 2078 | |
| 2079 | while (cont) |
| 2080 | { |
| 2081 | switch (peekchr()) |
| 2082 | { |
| 2083 | case NUL: |
| 2084 | case Magic('|'): |
| 2085 | case Magic('&'): |
| 2086 | case Magic(')'): |
| 2087 | cont = FALSE; |
| 2088 | break; |
| 2089 | |
| 2090 | case Magic('Z'): |
| 2091 | #ifdef FEAT_MBYTE |
| 2092 | regflags |= RF_ICOMBINE; |
| 2093 | #endif |
| 2094 | skipchr_keepstart(); |
| 2095 | break; |
| 2096 | case Magic('c'): |
| 2097 | regflags |= RF_ICASE; |
| 2098 | skipchr_keepstart(); |
| 2099 | break; |
| 2100 | case Magic('C'): |
| 2101 | regflags |= RF_NOICASE; |
| 2102 | skipchr_keepstart(); |
| 2103 | break; |
| 2104 | case Magic('v'): |
| 2105 | reg_magic = MAGIC_ALL; |
| 2106 | skipchr_keepstart(); |
| 2107 | curchr = -1; |
| 2108 | break; |
| 2109 | case Magic('m'): |
| 2110 | reg_magic = MAGIC_ON; |
| 2111 | skipchr_keepstart(); |
| 2112 | curchr = -1; |
| 2113 | break; |
| 2114 | case Magic('M'): |
| 2115 | reg_magic = MAGIC_OFF; |
| 2116 | skipchr_keepstart(); |
| 2117 | curchr = -1; |
| 2118 | break; |
| 2119 | case Magic('V'): |
| 2120 | reg_magic = MAGIC_NONE; |
| 2121 | skipchr_keepstart(); |
| 2122 | curchr = -1; |
| 2123 | break; |
| 2124 | |
| 2125 | default: |
| 2126 | if (nfa_regpiece() == FAIL) |
| 2127 | return FAIL; |
| 2128 | if (first == FALSE) |
| 2129 | EMIT(NFA_CONCAT); |
| 2130 | else |
| 2131 | first = FALSE; |
| 2132 | break; |
| 2133 | } |
| 2134 | } |
| 2135 | |
| 2136 | return OK; |
| 2137 | } |
| 2138 | |
| 2139 | /* |
| 2140 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 2141 | * last concat, but only if all the preceding concats also match at the same |
| 2142 | * position. Examples: |
| 2143 | * "foobeep\&..." matches "foo" in "foobeep". |
| 2144 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 2145 | * |
| 2146 | * branch ::= concat |
| 2147 | * or concat \& concat |
| 2148 | * or concat \& concat \& concat |
| 2149 | * etc. |
| 2150 | */ |
| 2151 | static int |
| 2152 | nfa_regbranch() |
| 2153 | { |
| 2154 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2155 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2156 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2157 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2158 | |
| 2159 | /* First branch, possibly the only one */ |
| 2160 | if (nfa_regconcat() == FAIL) |
| 2161 | return FAIL; |
| 2162 | |
| 2163 | ch = peekchr(); |
| 2164 | /* Try next concats */ |
| 2165 | while (ch == Magic('&')) |
| 2166 | { |
| 2167 | skipchr(); |
| 2168 | EMIT(NFA_NOPEN); |
| 2169 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2170 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2171 | if (nfa_regconcat() == FAIL) |
| 2172 | return FAIL; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2173 | /* if concat is empty do emit a node */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2174 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2175 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2176 | EMIT(NFA_CONCAT); |
| 2177 | ch = peekchr(); |
| 2178 | } |
| 2179 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2180 | /* if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2181 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2182 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2183 | |
| 2184 | return OK; |
| 2185 | } |
| 2186 | |
| 2187 | /* |
| 2188 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 2189 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 2190 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 2191 | * is used. |
| 2192 | * |
| 2193 | * pattern ::= branch |
| 2194 | * or branch \| branch |
| 2195 | * or branch \| branch \| branch |
| 2196 | * etc. |
| 2197 | */ |
| 2198 | static int |
| 2199 | nfa_reg(paren) |
| 2200 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 2201 | { |
| 2202 | int parno = 0; |
| 2203 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2204 | if (paren == REG_PAREN) |
| 2205 | { |
| 2206 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2207 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2208 | parno = regnpar++; |
| 2209 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2210 | #ifdef FEAT_SYN_HL |
| 2211 | else if (paren == REG_ZPAREN) |
| 2212 | { |
| 2213 | /* Make a ZOPEN node. */ |
| 2214 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2215 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2216 | parno = regnzpar++; |
| 2217 | } |
| 2218 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2219 | |
| 2220 | if (nfa_regbranch() == FAIL) |
| 2221 | return FAIL; /* cascaded error */ |
| 2222 | |
| 2223 | while (peekchr() == Magic('|')) |
| 2224 | { |
| 2225 | skipchr(); |
| 2226 | if (nfa_regbranch() == FAIL) |
| 2227 | return FAIL; /* cascaded error */ |
| 2228 | EMIT(NFA_OR); |
| 2229 | } |
| 2230 | |
| 2231 | /* Check for proper termination. */ |
| 2232 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 2233 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2234 | if (paren == REG_NPAREN) |
| 2235 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 2236 | else |
| 2237 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 2238 | } |
| 2239 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 2240 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2241 | if (peekchr() == Magic(')')) |
| 2242 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 2243 | else |
| 2244 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 2245 | } |
| 2246 | /* |
| 2247 | * Here we set the flag allowing back references to this set of |
| 2248 | * parentheses. |
| 2249 | */ |
| 2250 | if (paren == REG_PAREN) |
| 2251 | { |
| 2252 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 2253 | EMIT(NFA_MOPEN + parno); |
| 2254 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2255 | #ifdef FEAT_SYN_HL |
| 2256 | else if (paren == REG_ZPAREN) |
| 2257 | EMIT(NFA_ZOPEN + parno); |
| 2258 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2259 | |
| 2260 | return OK; |
| 2261 | } |
| 2262 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2263 | #ifdef DEBUG |
| 2264 | static char_u code[50]; |
| 2265 | |
| 2266 | static void |
| 2267 | nfa_set_code(c) |
| 2268 | int c; |
| 2269 | { |
| 2270 | int addnl = FALSE; |
| 2271 | |
| 2272 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 2273 | { |
| 2274 | addnl = TRUE; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2275 | c -= NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | STRCPY(code, ""); |
| 2279 | switch (c) |
| 2280 | { |
| 2281 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 2282 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 2283 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 2284 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 2285 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 2286 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 2287 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2288 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 2289 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 2290 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 2291 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 2292 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 2293 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 2294 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 2295 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 2296 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2297 | #ifdef FEAT_SYN_HL |
| 2298 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 2299 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 2300 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 2301 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 2302 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 2303 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 2304 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 2305 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 2306 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 2307 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2308 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 2309 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2310 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2311 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2312 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 2313 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2314 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2315 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 2316 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 2317 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2318 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 2319 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 2320 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2321 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 2322 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2323 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2324 | case NFA_START_INVISIBLE_FIRST: |
| 2325 | STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2326 | case NFA_START_INVISIBLE_NEG: |
| 2327 | STRCPY(code, "NFA_START_INVISIBLE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2328 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 2329 | STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2330 | case NFA_START_INVISIBLE_BEFORE: |
| 2331 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2332 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 2333 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2334 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2335 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2336 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 2337 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2338 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2339 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2340 | case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2341 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2342 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2343 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2344 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2345 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2346 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2347 | case NFA_MOPEN: |
| 2348 | case NFA_MOPEN1: |
| 2349 | case NFA_MOPEN2: |
| 2350 | case NFA_MOPEN3: |
| 2351 | case NFA_MOPEN4: |
| 2352 | case NFA_MOPEN5: |
| 2353 | case NFA_MOPEN6: |
| 2354 | case NFA_MOPEN7: |
| 2355 | case NFA_MOPEN8: |
| 2356 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2357 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2358 | code[10] = c - NFA_MOPEN + '0'; |
| 2359 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2360 | case NFA_MCLOSE: |
| 2361 | case NFA_MCLOSE1: |
| 2362 | case NFA_MCLOSE2: |
| 2363 | case NFA_MCLOSE3: |
| 2364 | case NFA_MCLOSE4: |
| 2365 | case NFA_MCLOSE5: |
| 2366 | case NFA_MCLOSE6: |
| 2367 | case NFA_MCLOSE7: |
| 2368 | case NFA_MCLOSE8: |
| 2369 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2370 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2371 | code[11] = c - NFA_MCLOSE + '0'; |
| 2372 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2373 | #ifdef FEAT_SYN_HL |
| 2374 | case NFA_ZOPEN: |
| 2375 | case NFA_ZOPEN1: |
| 2376 | case NFA_ZOPEN2: |
| 2377 | case NFA_ZOPEN3: |
| 2378 | case NFA_ZOPEN4: |
| 2379 | case NFA_ZOPEN5: |
| 2380 | case NFA_ZOPEN6: |
| 2381 | case NFA_ZOPEN7: |
| 2382 | case NFA_ZOPEN8: |
| 2383 | case NFA_ZOPEN9: |
| 2384 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2385 | code[10] = c - NFA_ZOPEN + '0'; |
| 2386 | break; |
| 2387 | case NFA_ZCLOSE: |
| 2388 | case NFA_ZCLOSE1: |
| 2389 | case NFA_ZCLOSE2: |
| 2390 | case NFA_ZCLOSE3: |
| 2391 | case NFA_ZCLOSE4: |
| 2392 | case NFA_ZCLOSE5: |
| 2393 | case NFA_ZCLOSE6: |
| 2394 | case NFA_ZCLOSE7: |
| 2395 | case NFA_ZCLOSE8: |
| 2396 | case NFA_ZCLOSE9: |
| 2397 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2398 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2399 | break; |
| 2400 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2401 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2402 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2403 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2404 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2405 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2406 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2407 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2408 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2409 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2410 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2411 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2412 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2413 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2414 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2415 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2416 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2417 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2418 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2419 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2420 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
| 2421 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2422 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2423 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2424 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2425 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2426 | case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2427 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2428 | |
| 2429 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2430 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2431 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2432 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2433 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2434 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2435 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2436 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2437 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2438 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2439 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2440 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2441 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2442 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2443 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2444 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2445 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2446 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2447 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2448 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2449 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2450 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2451 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2452 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2453 | |
| 2454 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2455 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2456 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2457 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2458 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2459 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2460 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2461 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2462 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2463 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2464 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2465 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2466 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2467 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2468 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2469 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2470 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2471 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2472 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2473 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2474 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2475 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2476 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2477 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2478 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2479 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2480 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2481 | case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break; |
| 2482 | case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break; |
| 2483 | case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break; |
| 2484 | case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2485 | |
| 2486 | default: |
| 2487 | STRCPY(code, "CHAR(x)"); |
| 2488 | code[5] = c; |
| 2489 | } |
| 2490 | |
| 2491 | if (addnl == TRUE) |
| 2492 | STRCAT(code, " + NEWLINE "); |
| 2493 | |
| 2494 | } |
| 2495 | |
| 2496 | #ifdef ENABLE_LOG |
| 2497 | static FILE *log_fd; |
| 2498 | |
| 2499 | /* |
| 2500 | * Print the postfix notation of the current regexp. |
| 2501 | */ |
| 2502 | static void |
| 2503 | nfa_postfix_dump(expr, retval) |
| 2504 | char_u *expr; |
| 2505 | int retval; |
| 2506 | { |
| 2507 | int *p; |
| 2508 | FILE *f; |
| 2509 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2510 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2511 | if (f != NULL) |
| 2512 | { |
| 2513 | fprintf(f, "\n-------------------------\n"); |
| 2514 | if (retval == FAIL) |
| 2515 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2516 | else if (retval == OK) |
| 2517 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2518 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2519 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2520 | { |
| 2521 | nfa_set_code(*p); |
| 2522 | fprintf(f, "%s, ", code); |
| 2523 | } |
| 2524 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2525 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2526 | fprintf(f, "%d ", *p); |
| 2527 | fprintf(f, "\n\n"); |
| 2528 | fclose(f); |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | /* |
| 2533 | * Print the NFA starting with a root node "state". |
| 2534 | */ |
| 2535 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2536 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2537 | FILE *debugf; |
| 2538 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2539 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2540 | garray_T indent; |
| 2541 | |
| 2542 | ga_init2(&indent, 1, 64); |
| 2543 | ga_append(&indent, '\0'); |
| 2544 | nfa_print_state2(debugf, state, &indent); |
| 2545 | ga_clear(&indent); |
| 2546 | } |
| 2547 | |
| 2548 | static void |
| 2549 | nfa_print_state2(debugf, state, indent) |
| 2550 | FILE *debugf; |
| 2551 | nfa_state_T *state; |
| 2552 | garray_T *indent; |
| 2553 | { |
| 2554 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2555 | |
| 2556 | if (state == NULL) |
| 2557 | return; |
| 2558 | |
| 2559 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2560 | |
| 2561 | /* Output indent */ |
| 2562 | p = (char_u *)indent->ga_data; |
| 2563 | if (indent->ga_len >= 3) |
| 2564 | { |
| 2565 | int last = indent->ga_len - 3; |
| 2566 | char_u save[2]; |
| 2567 | |
| 2568 | STRNCPY(save, &p[last], 2); |
| 2569 | STRNCPY(&p[last], "+-", 2); |
| 2570 | fprintf(debugf, " %s", p); |
| 2571 | STRNCPY(&p[last], save, 2); |
| 2572 | } |
| 2573 | else |
| 2574 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2575 | |
| 2576 | nfa_set_code(state->c); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2577 | fprintf(debugf, "%s (%d) (id=%d) val=%d\n", |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2578 | code, |
| 2579 | state->c, |
| 2580 | abs(state->id), |
| 2581 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2582 | if (state->id < 0) |
| 2583 | return; |
| 2584 | |
| 2585 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2586 | |
| 2587 | /* grow indent for state->out */ |
| 2588 | indent->ga_len -= 1; |
| 2589 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2590 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2591 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2592 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2593 | ga_append(indent, '\0'); |
| 2594 | |
| 2595 | nfa_print_state2(debugf, state->out, indent); |
| 2596 | |
| 2597 | /* replace last part of indent for state->out1 */ |
| 2598 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2599 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2600 | ga_append(indent, '\0'); |
| 2601 | |
| 2602 | nfa_print_state2(debugf, state->out1, indent); |
| 2603 | |
| 2604 | /* shrink indent */ |
| 2605 | indent->ga_len -= 3; |
| 2606 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | /* |
| 2610 | * Print the NFA state machine. |
| 2611 | */ |
| 2612 | static void |
| 2613 | nfa_dump(prog) |
| 2614 | nfa_regprog_T *prog; |
| 2615 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2616 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2617 | |
| 2618 | if (debugf != NULL) |
| 2619 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2620 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2621 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 2622 | if (prog->reganch) |
| 2623 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2624 | if (prog->regstart != NUL) |
| 2625 | fprintf(debugf, "regstart: %c (decimal: %d)\n", |
| 2626 | prog->regstart, prog->regstart); |
| 2627 | if (prog->match_text != NULL) |
| 2628 | fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2629 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2630 | fclose(debugf); |
| 2631 | } |
| 2632 | } |
| 2633 | #endif /* ENABLE_LOG */ |
| 2634 | #endif /* DEBUG */ |
| 2635 | |
| 2636 | /* |
| 2637 | * Parse r.e. @expr and convert it into postfix form. |
| 2638 | * Return the postfix string on success, NULL otherwise. |
| 2639 | */ |
| 2640 | static int * |
| 2641 | re2post() |
| 2642 | { |
| 2643 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2644 | return NULL; |
| 2645 | EMIT(NFA_MOPEN); |
| 2646 | return post_start; |
| 2647 | } |
| 2648 | |
| 2649 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2650 | |
| 2651 | /* |
| 2652 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2653 | * if c == MATCH, no arrows out; matching state. |
| 2654 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2655 | * If c < 256, labeled arrow with character c to out. |
| 2656 | */ |
| 2657 | |
| 2658 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2659 | |
| 2660 | /* |
| 2661 | * Allocate and initialize nfa_state_T. |
| 2662 | */ |
| 2663 | static nfa_state_T * |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2664 | alloc_state(c, out, out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2665 | int c; |
| 2666 | nfa_state_T *out; |
| 2667 | nfa_state_T *out1; |
| 2668 | { |
| 2669 | nfa_state_T *s; |
| 2670 | |
| 2671 | if (istate >= nstate) |
| 2672 | return NULL; |
| 2673 | |
| 2674 | s = &state_ptr[istate++]; |
| 2675 | |
| 2676 | s->c = c; |
| 2677 | s->out = out; |
| 2678 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2679 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2680 | |
| 2681 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2682 | s->lastlist[0] = 0; |
| 2683 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2684 | |
| 2685 | return s; |
| 2686 | } |
| 2687 | |
| 2688 | /* |
| 2689 | * A partially built NFA without the matching state filled in. |
| 2690 | * Frag_T.start points at the start state. |
| 2691 | * Frag_T.out is a list of places that need to be set to the |
| 2692 | * next state for this fragment. |
| 2693 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2694 | |
| 2695 | /* Since the out pointers in the list are always |
| 2696 | * uninitialized, we use the pointers themselves |
| 2697 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2698 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2699 | union Ptrlist |
| 2700 | { |
| 2701 | Ptrlist *next; |
| 2702 | nfa_state_T *s; |
| 2703 | }; |
| 2704 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2705 | struct Frag |
| 2706 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2707 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2708 | Ptrlist *out; |
| 2709 | }; |
| 2710 | typedef struct Frag Frag_T; |
| 2711 | |
| 2712 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2713 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2714 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2715 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2716 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2717 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2718 | |
| 2719 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2720 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2721 | */ |
| 2722 | static Frag_T |
| 2723 | frag(start, out) |
| 2724 | nfa_state_T *start; |
| 2725 | Ptrlist *out; |
| 2726 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2727 | Frag_T n; |
| 2728 | |
| 2729 | n.start = start; |
| 2730 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2731 | return n; |
| 2732 | } |
| 2733 | |
| 2734 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2735 | * Create singleton list containing just outp. |
| 2736 | */ |
| 2737 | static Ptrlist * |
| 2738 | list1(outp) |
| 2739 | nfa_state_T **outp; |
| 2740 | { |
| 2741 | Ptrlist *l; |
| 2742 | |
| 2743 | l = (Ptrlist *)outp; |
| 2744 | l->next = NULL; |
| 2745 | return l; |
| 2746 | } |
| 2747 | |
| 2748 | /* |
| 2749 | * Patch the list of states at out to point to start. |
| 2750 | */ |
| 2751 | static void |
| 2752 | patch(l, s) |
| 2753 | Ptrlist *l; |
| 2754 | nfa_state_T *s; |
| 2755 | { |
| 2756 | Ptrlist *next; |
| 2757 | |
| 2758 | for (; l; l = next) |
| 2759 | { |
| 2760 | next = l->next; |
| 2761 | l->s = s; |
| 2762 | } |
| 2763 | } |
| 2764 | |
| 2765 | |
| 2766 | /* |
| 2767 | * Join the two lists l1 and l2, returning the combination. |
| 2768 | */ |
| 2769 | static Ptrlist * |
| 2770 | append(l1, l2) |
| 2771 | Ptrlist *l1; |
| 2772 | Ptrlist *l2; |
| 2773 | { |
| 2774 | Ptrlist *oldl1; |
| 2775 | |
| 2776 | oldl1 = l1; |
| 2777 | while (l1->next) |
| 2778 | l1 = l1->next; |
| 2779 | l1->next = l2; |
| 2780 | return oldl1; |
| 2781 | } |
| 2782 | |
| 2783 | /* |
| 2784 | * Stack used for transforming postfix form into NFA. |
| 2785 | */ |
| 2786 | static Frag_T empty; |
| 2787 | |
| 2788 | static void |
| 2789 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2790 | int *postfix UNUSED; |
| 2791 | int *end UNUSED; |
| 2792 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2793 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2794 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2795 | FILE *df; |
| 2796 | int *p2; |
| 2797 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2798 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2799 | if (df) |
| 2800 | { |
| 2801 | fprintf(df, "Error popping the stack!\n"); |
| 2802 | #ifdef DEBUG |
| 2803 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2804 | #endif |
| 2805 | fprintf(df, "Postfix form is: "); |
| 2806 | #ifdef DEBUG |
| 2807 | for (p2 = postfix; p2 < end; p2++) |
| 2808 | { |
| 2809 | nfa_set_code(*p2); |
| 2810 | fprintf(df, "%s, ", code); |
| 2811 | } |
| 2812 | nfa_set_code(*p); |
| 2813 | fprintf(df, "\nCurrent position is: "); |
| 2814 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2815 | { |
| 2816 | nfa_set_code(*p2); |
| 2817 | fprintf(df, "%s, ", code); |
| 2818 | } |
| 2819 | #else |
| 2820 | for (p2 = postfix; p2 < end; p2++) |
| 2821 | { |
| 2822 | fprintf(df, "%d, ", *p2); |
| 2823 | } |
| 2824 | fprintf(df, "\nCurrent position is: "); |
| 2825 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2826 | { |
| 2827 | fprintf(df, "%d, ", *p2); |
| 2828 | } |
| 2829 | #endif |
| 2830 | fprintf(df, "\n--------------------------\n"); |
| 2831 | fclose(df); |
| 2832 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2833 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2834 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2835 | } |
| 2836 | |
| 2837 | /* |
| 2838 | * Push an item onto the stack. |
| 2839 | */ |
| 2840 | static void |
| 2841 | st_push(s, p, stack_end) |
| 2842 | Frag_T s; |
| 2843 | Frag_T **p; |
| 2844 | Frag_T *stack_end; |
| 2845 | { |
| 2846 | Frag_T *stackp = *p; |
| 2847 | |
| 2848 | if (stackp >= stack_end) |
| 2849 | return; |
| 2850 | *stackp = s; |
| 2851 | *p = *p + 1; |
| 2852 | } |
| 2853 | |
| 2854 | /* |
| 2855 | * Pop an item from the stack. |
| 2856 | */ |
| 2857 | static Frag_T |
| 2858 | st_pop(p, stack) |
| 2859 | Frag_T **p; |
| 2860 | Frag_T *stack; |
| 2861 | { |
| 2862 | Frag_T *stackp; |
| 2863 | |
| 2864 | *p = *p - 1; |
| 2865 | stackp = *p; |
| 2866 | if (stackp < stack) |
| 2867 | return empty; |
| 2868 | return **p; |
| 2869 | } |
| 2870 | |
| 2871 | /* |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2872 | * Estimate the maximum byte length of anything matching "state". |
| 2873 | * When unknown or unlimited return -1. |
| 2874 | */ |
| 2875 | static int |
| 2876 | nfa_max_width(startstate, depth) |
| 2877 | nfa_state_T *startstate; |
| 2878 | int depth; |
| 2879 | { |
| 2880 | int l, r; |
| 2881 | nfa_state_T *state = startstate; |
| 2882 | int len = 0; |
| 2883 | |
| 2884 | /* detect looping in a NFA_SPLIT */ |
| 2885 | if (depth > 4) |
| 2886 | return -1; |
| 2887 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 2888 | while (state != NULL) |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2889 | { |
| 2890 | switch (state->c) |
| 2891 | { |
| 2892 | case NFA_END_INVISIBLE: |
| 2893 | case NFA_END_INVISIBLE_NEG: |
| 2894 | /* the end, return what we have */ |
| 2895 | return len; |
| 2896 | |
| 2897 | case NFA_SPLIT: |
| 2898 | /* two alternatives, use the maximum */ |
| 2899 | l = nfa_max_width(state->out, depth + 1); |
| 2900 | r = nfa_max_width(state->out1, depth + 1); |
| 2901 | if (l < 0 || r < 0) |
| 2902 | return -1; |
| 2903 | return len + (l > r ? l : r); |
| 2904 | |
| 2905 | case NFA_ANY: |
| 2906 | case NFA_START_COLL: |
| 2907 | case NFA_START_NEG_COLL: |
| 2908 | /* matches some character, including composing chars */ |
| 2909 | #ifdef FEAT_MBYTE |
| 2910 | if (enc_utf8) |
| 2911 | len += MB_MAXBYTES; |
| 2912 | else if (has_mbyte) |
| 2913 | len += 2; |
| 2914 | else |
| 2915 | #endif |
| 2916 | ++len; |
| 2917 | if (state->c != NFA_ANY) |
| 2918 | { |
| 2919 | /* skip over the characters */ |
| 2920 | state = state->out1->out; |
| 2921 | continue; |
| 2922 | } |
| 2923 | break; |
| 2924 | |
| 2925 | case NFA_DIGIT: |
| 2926 | case NFA_WHITE: |
| 2927 | case NFA_HEX: |
| 2928 | case NFA_OCTAL: |
| 2929 | /* ascii */ |
| 2930 | ++len; |
| 2931 | break; |
| 2932 | |
| 2933 | case NFA_IDENT: |
| 2934 | case NFA_SIDENT: |
| 2935 | case NFA_KWORD: |
| 2936 | case NFA_SKWORD: |
| 2937 | case NFA_FNAME: |
| 2938 | case NFA_SFNAME: |
| 2939 | case NFA_PRINT: |
| 2940 | case NFA_SPRINT: |
| 2941 | case NFA_NWHITE: |
| 2942 | case NFA_NDIGIT: |
| 2943 | case NFA_NHEX: |
| 2944 | case NFA_NOCTAL: |
| 2945 | case NFA_WORD: |
| 2946 | case NFA_NWORD: |
| 2947 | case NFA_HEAD: |
| 2948 | case NFA_NHEAD: |
| 2949 | case NFA_ALPHA: |
| 2950 | case NFA_NALPHA: |
| 2951 | case NFA_LOWER: |
| 2952 | case NFA_NLOWER: |
| 2953 | case NFA_UPPER: |
| 2954 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2955 | case NFA_LOWER_IC: |
| 2956 | case NFA_NLOWER_IC: |
| 2957 | case NFA_UPPER_IC: |
| 2958 | case NFA_NUPPER_IC: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2959 | /* possibly non-ascii */ |
| 2960 | #ifdef FEAT_MBYTE |
| 2961 | if (has_mbyte) |
| 2962 | len += 3; |
| 2963 | else |
| 2964 | #endif |
| 2965 | ++len; |
| 2966 | break; |
| 2967 | |
| 2968 | case NFA_START_INVISIBLE: |
| 2969 | case NFA_START_INVISIBLE_NEG: |
| 2970 | case NFA_START_INVISIBLE_BEFORE: |
| 2971 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2972 | /* zero-width, out1 points to the END state */ |
| 2973 | state = state->out1->out; |
| 2974 | continue; |
| 2975 | |
| 2976 | case NFA_BACKREF1: |
| 2977 | case NFA_BACKREF2: |
| 2978 | case NFA_BACKREF3: |
| 2979 | case NFA_BACKREF4: |
| 2980 | case NFA_BACKREF5: |
| 2981 | case NFA_BACKREF6: |
| 2982 | case NFA_BACKREF7: |
| 2983 | case NFA_BACKREF8: |
| 2984 | case NFA_BACKREF9: |
| 2985 | #ifdef FEAT_SYN_HL |
| 2986 | case NFA_ZREF1: |
| 2987 | case NFA_ZREF2: |
| 2988 | case NFA_ZREF3: |
| 2989 | case NFA_ZREF4: |
| 2990 | case NFA_ZREF5: |
| 2991 | case NFA_ZREF6: |
| 2992 | case NFA_ZREF7: |
| 2993 | case NFA_ZREF8: |
| 2994 | case NFA_ZREF9: |
| 2995 | #endif |
| 2996 | case NFA_NEWL: |
| 2997 | case NFA_SKIP: |
| 2998 | /* unknown width */ |
| 2999 | return -1; |
| 3000 | |
| 3001 | case NFA_BOL: |
| 3002 | case NFA_EOL: |
| 3003 | case NFA_BOF: |
| 3004 | case NFA_EOF: |
| 3005 | case NFA_BOW: |
| 3006 | case NFA_EOW: |
| 3007 | case NFA_MOPEN: |
| 3008 | case NFA_MOPEN1: |
| 3009 | case NFA_MOPEN2: |
| 3010 | case NFA_MOPEN3: |
| 3011 | case NFA_MOPEN4: |
| 3012 | case NFA_MOPEN5: |
| 3013 | case NFA_MOPEN6: |
| 3014 | case NFA_MOPEN7: |
| 3015 | case NFA_MOPEN8: |
| 3016 | case NFA_MOPEN9: |
| 3017 | #ifdef FEAT_SYN_HL |
| 3018 | case NFA_ZOPEN: |
| 3019 | case NFA_ZOPEN1: |
| 3020 | case NFA_ZOPEN2: |
| 3021 | case NFA_ZOPEN3: |
| 3022 | case NFA_ZOPEN4: |
| 3023 | case NFA_ZOPEN5: |
| 3024 | case NFA_ZOPEN6: |
| 3025 | case NFA_ZOPEN7: |
| 3026 | case NFA_ZOPEN8: |
| 3027 | case NFA_ZOPEN9: |
| 3028 | case NFA_ZCLOSE: |
| 3029 | case NFA_ZCLOSE1: |
| 3030 | case NFA_ZCLOSE2: |
| 3031 | case NFA_ZCLOSE3: |
| 3032 | case NFA_ZCLOSE4: |
| 3033 | case NFA_ZCLOSE5: |
| 3034 | case NFA_ZCLOSE6: |
| 3035 | case NFA_ZCLOSE7: |
| 3036 | case NFA_ZCLOSE8: |
| 3037 | case NFA_ZCLOSE9: |
| 3038 | #endif |
| 3039 | case NFA_MCLOSE: |
| 3040 | case NFA_MCLOSE1: |
| 3041 | case NFA_MCLOSE2: |
| 3042 | case NFA_MCLOSE3: |
| 3043 | case NFA_MCLOSE4: |
| 3044 | case NFA_MCLOSE5: |
| 3045 | case NFA_MCLOSE6: |
| 3046 | case NFA_MCLOSE7: |
| 3047 | case NFA_MCLOSE8: |
| 3048 | case NFA_MCLOSE9: |
| 3049 | case NFA_NOPEN: |
| 3050 | case NFA_NCLOSE: |
| 3051 | |
| 3052 | case NFA_LNUM_GT: |
| 3053 | case NFA_LNUM_LT: |
| 3054 | case NFA_COL_GT: |
| 3055 | case NFA_COL_LT: |
| 3056 | case NFA_VCOL_GT: |
| 3057 | case NFA_VCOL_LT: |
| 3058 | case NFA_MARK_GT: |
| 3059 | case NFA_MARK_LT: |
| 3060 | case NFA_VISUAL: |
| 3061 | case NFA_LNUM: |
| 3062 | case NFA_CURSOR: |
| 3063 | case NFA_COL: |
| 3064 | case NFA_VCOL: |
| 3065 | case NFA_MARK: |
| 3066 | |
| 3067 | case NFA_ZSTART: |
| 3068 | case NFA_ZEND: |
| 3069 | case NFA_OPT_CHARS: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3070 | case NFA_EMPTY: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3071 | case NFA_START_PATTERN: |
| 3072 | case NFA_END_PATTERN: |
| 3073 | case NFA_COMPOSING: |
| 3074 | case NFA_END_COMPOSING: |
| 3075 | /* zero-width */ |
| 3076 | break; |
| 3077 | |
| 3078 | default: |
| 3079 | if (state->c < 0) |
| 3080 | /* don't know what this is */ |
| 3081 | return -1; |
| 3082 | /* normal character */ |
| 3083 | len += MB_CHAR2LEN(state->c); |
| 3084 | break; |
| 3085 | } |
| 3086 | |
| 3087 | /* normal way to continue */ |
| 3088 | state = state->out; |
| 3089 | } |
| 3090 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 3091 | /* unrecognized, "cannot happen" */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3092 | return -1; |
| 3093 | } |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3094 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3095 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3096 | * Convert a postfix form into its equivalent NFA. |
| 3097 | * Return the NFA start state on success, NULL otherwise. |
| 3098 | */ |
| 3099 | static nfa_state_T * |
| 3100 | post2nfa(postfix, end, nfa_calc_size) |
| 3101 | int *postfix; |
| 3102 | int *end; |
| 3103 | int nfa_calc_size; |
| 3104 | { |
| 3105 | int *p; |
| 3106 | int mopen; |
| 3107 | int mclose; |
| 3108 | Frag_T *stack = NULL; |
| 3109 | Frag_T *stackp = NULL; |
| 3110 | Frag_T *stack_end = NULL; |
| 3111 | Frag_T e1; |
| 3112 | Frag_T e2; |
| 3113 | Frag_T e; |
| 3114 | nfa_state_T *s; |
| 3115 | nfa_state_T *s1; |
| 3116 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3117 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3118 | |
| 3119 | if (postfix == NULL) |
| 3120 | return NULL; |
| 3121 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 3122 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3123 | #define POP() st_pop(&stackp, stack); \ |
| 3124 | if (stackp < stack) \ |
| 3125 | { \ |
| 3126 | st_error(postfix, end, p); \ |
| 3127 | return NULL; \ |
| 3128 | } |
| 3129 | |
| 3130 | if (nfa_calc_size == FALSE) |
| 3131 | { |
| 3132 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3133 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3134 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 3135 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | for (p = postfix; p < end; ++p) |
| 3139 | { |
| 3140 | switch (*p) |
| 3141 | { |
| 3142 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3143 | /* Concatenation. |
| 3144 | * Pay attention: this operator does not exist in the r.e. itself |
| 3145 | * (it is implicit, really). It is added when r.e. is translated |
| 3146 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3147 | if (nfa_calc_size == TRUE) |
| 3148 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3149 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3150 | break; |
| 3151 | } |
| 3152 | e2 = POP(); |
| 3153 | e1 = POP(); |
| 3154 | patch(e1.out, e2.start); |
| 3155 | PUSH(frag(e1.start, e2.out)); |
| 3156 | break; |
| 3157 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3158 | case NFA_OR: |
| 3159 | /* Alternation */ |
| 3160 | if (nfa_calc_size == TRUE) |
| 3161 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3162 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3163 | break; |
| 3164 | } |
| 3165 | e2 = POP(); |
| 3166 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3167 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3168 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3169 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3170 | PUSH(frag(s, append(e1.out, e2.out))); |
| 3171 | break; |
| 3172 | |
| 3173 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3174 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3175 | if (nfa_calc_size == TRUE) |
| 3176 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3177 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3178 | break; |
| 3179 | } |
| 3180 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3181 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3182 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3183 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3184 | patch(e.out, s); |
| 3185 | PUSH(frag(s, list1(&s->out1))); |
| 3186 | break; |
| 3187 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3188 | case NFA_STAR_NONGREEDY: |
| 3189 | /* Zero or more, prefer zero */ |
| 3190 | if (nfa_calc_size == TRUE) |
| 3191 | { |
| 3192 | nstate++; |
| 3193 | break; |
| 3194 | } |
| 3195 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3196 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3197 | if (s == NULL) |
| 3198 | goto theend; |
| 3199 | patch(e.out, s); |
| 3200 | PUSH(frag(s, list1(&s->out))); |
| 3201 | break; |
| 3202 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3203 | case NFA_QUEST: |
| 3204 | /* one or zero atoms=> greedy match */ |
| 3205 | if (nfa_calc_size == TRUE) |
| 3206 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3207 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3208 | break; |
| 3209 | } |
| 3210 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3211 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3212 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3213 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3214 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 3215 | break; |
| 3216 | |
| 3217 | case NFA_QUEST_NONGREEDY: |
| 3218 | /* zero or one atoms => non-greedy match */ |
| 3219 | if (nfa_calc_size == TRUE) |
| 3220 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3221 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3222 | break; |
| 3223 | } |
| 3224 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3225 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3226 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3227 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3228 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 3229 | break; |
| 3230 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3231 | case NFA_END_COLL: |
| 3232 | case NFA_END_NEG_COLL: |
| 3233 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 3234 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 3235 | * add the output to the start. */ |
| 3236 | if (nfa_calc_size == TRUE) |
| 3237 | { |
| 3238 | nstate++; |
| 3239 | break; |
| 3240 | } |
| 3241 | e = POP(); |
| 3242 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 3243 | if (s == NULL) |
| 3244 | goto theend; |
| 3245 | patch(e.out, s); |
| 3246 | e.start->out1 = s; |
| 3247 | PUSH(frag(e.start, list1(&s->out))); |
| 3248 | break; |
| 3249 | |
| 3250 | case NFA_RANGE: |
| 3251 | /* Before this are two characters, the low and high end of a |
| 3252 | * range. Turn them into two states with MIN and MAX. */ |
| 3253 | if (nfa_calc_size == TRUE) |
| 3254 | { |
| 3255 | /* nstate += 0; */ |
| 3256 | break; |
| 3257 | } |
| 3258 | e2 = POP(); |
| 3259 | e1 = POP(); |
| 3260 | e2.start->val = e2.start->c; |
| 3261 | e2.start->c = NFA_RANGE_MAX; |
| 3262 | e1.start->val = e1.start->c; |
| 3263 | e1.start->c = NFA_RANGE_MIN; |
| 3264 | patch(e1.out, e2.start); |
| 3265 | PUSH(frag(e1.start, e2.out)); |
| 3266 | break; |
| 3267 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3268 | case NFA_EMPTY: |
| 3269 | /* 0-length, used in a repetition with max/min count of 0 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3270 | if (nfa_calc_size == TRUE) |
| 3271 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3272 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3273 | break; |
| 3274 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3275 | s = alloc_state(NFA_EMPTY, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3276 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3277 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3278 | PUSH(frag(s, list1(&s->out))); |
| 3279 | break; |
| 3280 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3281 | case NFA_OPT_CHARS: |
| 3282 | { |
| 3283 | int n; |
| 3284 | |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 3285 | /* \%[abc] implemented as: |
| 3286 | * NFA_SPLIT |
| 3287 | * +-CHAR(a) |
| 3288 | * | +-NFA_SPLIT |
| 3289 | * | +-CHAR(b) |
| 3290 | * | | +-NFA_SPLIT |
| 3291 | * | | +-CHAR(c) |
| 3292 | * | | | +-next |
| 3293 | * | | +- next |
| 3294 | * | +- next |
| 3295 | * +- next |
| 3296 | */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3297 | n = *++p; /* get number of characters */ |
| 3298 | if (nfa_calc_size == TRUE) |
| 3299 | { |
| 3300 | nstate += n; |
| 3301 | break; |
| 3302 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 3303 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3304 | e1.out = NULL; /* stores list with out1's */ |
| 3305 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 3306 | while (n-- > 0) |
| 3307 | { |
| 3308 | e = POP(); /* get character */ |
| 3309 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 3310 | if (s == NULL) |
| 3311 | goto theend; |
| 3312 | if (e1.out == NULL) |
| 3313 | e1 = e; |
| 3314 | patch(e.out, s1); |
| 3315 | append(e1.out, list1(&s->out1)); |
| 3316 | s1 = s; |
| 3317 | } |
| 3318 | PUSH(frag(s, e1.out)); |
| 3319 | break; |
| 3320 | } |
| 3321 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3322 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3323 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3324 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3325 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3326 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3327 | { |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3328 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 3329 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3330 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3331 | int start_state; |
| 3332 | int end_state; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3333 | int n = 0; |
| 3334 | nfa_state_T *zend; |
| 3335 | nfa_state_T *skip; |
| 3336 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3337 | switch (*p) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3338 | { |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3339 | case NFA_PREV_ATOM_NO_WIDTH: |
| 3340 | start_state = NFA_START_INVISIBLE; |
| 3341 | end_state = NFA_END_INVISIBLE; |
| 3342 | break; |
| 3343 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 3344 | start_state = NFA_START_INVISIBLE_NEG; |
| 3345 | end_state = NFA_END_INVISIBLE_NEG; |
| 3346 | break; |
| 3347 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3348 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 3349 | end_state = NFA_END_INVISIBLE; |
| 3350 | break; |
| 3351 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 3352 | start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
| 3353 | end_state = NFA_END_INVISIBLE_NEG; |
| 3354 | break; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 3355 | default: /* NFA_PREV_ATOM_LIKE_PATTERN: */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3356 | start_state = NFA_START_PATTERN; |
| 3357 | end_state = NFA_END_PATTERN; |
| 3358 | break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3359 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3360 | |
| 3361 | if (before) |
| 3362 | n = *++p; /* get the count */ |
| 3363 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3364 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3365 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3366 | * The \@<= operator: match for the preceding atom. |
| 3367 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3368 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3369 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3370 | |
| 3371 | if (nfa_calc_size == TRUE) |
| 3372 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3373 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3374 | break; |
| 3375 | } |
| 3376 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3377 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3378 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3379 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3380 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3381 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3382 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3383 | goto theend; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3384 | if (pattern) |
| 3385 | { |
| 3386 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 3387 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 3388 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 3389 | s1->out= skip; |
| 3390 | patch(e.out, zend); |
| 3391 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3392 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3393 | else |
| 3394 | { |
| 3395 | patch(e.out, s1); |
| 3396 | PUSH(frag(s, list1(&s1->out))); |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3397 | if (before) |
| 3398 | { |
| 3399 | if (n <= 0) |
| 3400 | /* See if we can guess the maximum width, it avoids a |
| 3401 | * lot of pointless tries. */ |
| 3402 | n = nfa_max_width(e.start, 0); |
| 3403 | s->val = n; /* store the count */ |
| 3404 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3405 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3406 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3407 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3408 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3409 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3410 | case NFA_COMPOSING: /* char with composing char */ |
| 3411 | #if 0 |
| 3412 | /* TODO */ |
| 3413 | if (regflags & RF_ICOMBINE) |
| 3414 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3415 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3416 | } |
| 3417 | #endif |
| 3418 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3419 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3420 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3421 | case NFA_MOPEN: /* \( \) Submatch */ |
| 3422 | case NFA_MOPEN1: |
| 3423 | case NFA_MOPEN2: |
| 3424 | case NFA_MOPEN3: |
| 3425 | case NFA_MOPEN4: |
| 3426 | case NFA_MOPEN5: |
| 3427 | case NFA_MOPEN6: |
| 3428 | case NFA_MOPEN7: |
| 3429 | case NFA_MOPEN8: |
| 3430 | case NFA_MOPEN9: |
| 3431 | #ifdef FEAT_SYN_HL |
| 3432 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 3433 | case NFA_ZOPEN1: |
| 3434 | case NFA_ZOPEN2: |
| 3435 | case NFA_ZOPEN3: |
| 3436 | case NFA_ZOPEN4: |
| 3437 | case NFA_ZOPEN5: |
| 3438 | case NFA_ZOPEN6: |
| 3439 | case NFA_ZOPEN7: |
| 3440 | case NFA_ZOPEN8: |
| 3441 | case NFA_ZOPEN9: |
| 3442 | #endif |
| 3443 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3444 | if (nfa_calc_size == TRUE) |
| 3445 | { |
| 3446 | nstate += 2; |
| 3447 | break; |
| 3448 | } |
| 3449 | |
| 3450 | mopen = *p; |
| 3451 | switch (*p) |
| 3452 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3453 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 3454 | #ifdef FEAT_SYN_HL |
| 3455 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 3456 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 3457 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 3458 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 3459 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 3460 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 3461 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 3462 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 3463 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 3464 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 3465 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3466 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3467 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3468 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3469 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3470 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3471 | mclose = *p + NSUBEXP; |
| 3472 | break; |
| 3473 | } |
| 3474 | |
| 3475 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 3476 | * the empty regexp "". In this case, the NFA will be |
| 3477 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 3478 | * empty groups of parenthesis, and empty mbyte chars */ |
| 3479 | if (stackp == stack) |
| 3480 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3481 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3482 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3483 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3484 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3485 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3486 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3487 | patch(list1(&s->out), s1); |
| 3488 | PUSH(frag(s, list1(&s1->out))); |
| 3489 | break; |
| 3490 | } |
| 3491 | |
| 3492 | /* At least one node was emitted before NFA_MOPEN, so |
| 3493 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 3494 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3495 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3496 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3497 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3498 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3499 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3500 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3501 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3502 | patch(e.out, s1); |
| 3503 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3504 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3505 | if (mopen == NFA_COMPOSING) |
| 3506 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3507 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3508 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3509 | |
| 3510 | PUSH(frag(s, list1(&s1->out))); |
| 3511 | break; |
| 3512 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3513 | case NFA_BACKREF1: |
| 3514 | case NFA_BACKREF2: |
| 3515 | case NFA_BACKREF3: |
| 3516 | case NFA_BACKREF4: |
| 3517 | case NFA_BACKREF5: |
| 3518 | case NFA_BACKREF6: |
| 3519 | case NFA_BACKREF7: |
| 3520 | case NFA_BACKREF8: |
| 3521 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3522 | #ifdef FEAT_SYN_HL |
| 3523 | case NFA_ZREF1: |
| 3524 | case NFA_ZREF2: |
| 3525 | case NFA_ZREF3: |
| 3526 | case NFA_ZREF4: |
| 3527 | case NFA_ZREF5: |
| 3528 | case NFA_ZREF6: |
| 3529 | case NFA_ZREF7: |
| 3530 | case NFA_ZREF8: |
| 3531 | case NFA_ZREF9: |
| 3532 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3533 | if (nfa_calc_size == TRUE) |
| 3534 | { |
| 3535 | nstate += 2; |
| 3536 | break; |
| 3537 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3538 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3539 | if (s == NULL) |
| 3540 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3541 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3542 | if (s1 == NULL) |
| 3543 | goto theend; |
| 3544 | patch(list1(&s->out), s1); |
| 3545 | PUSH(frag(s, list1(&s1->out))); |
| 3546 | break; |
| 3547 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3548 | case NFA_LNUM: |
| 3549 | case NFA_LNUM_GT: |
| 3550 | case NFA_LNUM_LT: |
| 3551 | case NFA_VCOL: |
| 3552 | case NFA_VCOL_GT: |
| 3553 | case NFA_VCOL_LT: |
| 3554 | case NFA_COL: |
| 3555 | case NFA_COL_GT: |
| 3556 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 3557 | case NFA_MARK: |
| 3558 | case NFA_MARK_GT: |
| 3559 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3560 | { |
| 3561 | int n = *++p; /* lnum, col or mark name */ |
| 3562 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3563 | if (nfa_calc_size == TRUE) |
| 3564 | { |
| 3565 | nstate += 1; |
| 3566 | break; |
| 3567 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3568 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3569 | if (s == NULL) |
| 3570 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3571 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3572 | PUSH(frag(s, list1(&s->out))); |
| 3573 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3574 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3575 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3576 | case NFA_ZSTART: |
| 3577 | case NFA_ZEND: |
| 3578 | default: |
| 3579 | /* Operands */ |
| 3580 | if (nfa_calc_size == TRUE) |
| 3581 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3582 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3583 | break; |
| 3584 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3585 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3586 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3587 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3588 | PUSH(frag(s, list1(&s->out))); |
| 3589 | break; |
| 3590 | |
| 3591 | } /* switch(*p) */ |
| 3592 | |
| 3593 | } /* for(p = postfix; *p; ++p) */ |
| 3594 | |
| 3595 | if (nfa_calc_size == TRUE) |
| 3596 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3597 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3598 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | e = POP(); |
| 3602 | if (stackp != stack) |
| 3603 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 3604 | |
| 3605 | if (istate >= nstate) |
| 3606 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 3607 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3608 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3609 | matchstate->c = NFA_MATCH; |
| 3610 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3611 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3612 | |
| 3613 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3614 | ret = e.start; |
| 3615 | |
| 3616 | theend: |
| 3617 | vim_free(stack); |
| 3618 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3619 | |
| 3620 | #undef POP1 |
| 3621 | #undef PUSH1 |
| 3622 | #undef POP2 |
| 3623 | #undef PUSH2 |
| 3624 | #undef POP |
| 3625 | #undef PUSH |
| 3626 | } |
| 3627 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3628 | /* |
| 3629 | * After building the NFA program, inspect it to add optimization hints. |
| 3630 | */ |
| 3631 | static void |
| 3632 | nfa_postprocess(prog) |
| 3633 | nfa_regprog_T *prog; |
| 3634 | { |
| 3635 | int i; |
| 3636 | int c; |
| 3637 | |
| 3638 | for (i = 0; i < prog->nstate; ++i) |
| 3639 | { |
| 3640 | c = prog->state[i].c; |
| 3641 | if (c == NFA_START_INVISIBLE |
| 3642 | || c == NFA_START_INVISIBLE_NEG |
| 3643 | || c == NFA_START_INVISIBLE_BEFORE |
| 3644 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3645 | { |
| 3646 | int directly; |
| 3647 | |
| 3648 | /* Do it directly when what follows is possibly the end of the |
| 3649 | * match. */ |
| 3650 | if (match_follows(prog->state[i].out1->out, 0)) |
| 3651 | directly = TRUE; |
| 3652 | else |
| 3653 | { |
| 3654 | int ch_invisible = failure_chance(prog->state[i].out, 0); |
| 3655 | int ch_follows = failure_chance(prog->state[i].out1->out, 0); |
| 3656 | |
| 3657 | /* Postpone when the invisible match is expensive or has a |
| 3658 | * lower chance of failing. */ |
| 3659 | if (c == NFA_START_INVISIBLE_BEFORE |
| 3660 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3661 | { |
| 3662 | /* "before" matches are very expensive when |
| 3663 | * unbounded, always prefer what follows then, |
| 3664 | * unless what follows will always match. |
| 3665 | * Otherwise strongly prefer what follows. */ |
| 3666 | if (prog->state[i].val <= 0 && ch_follows > 0) |
| 3667 | directly = FALSE; |
| 3668 | else |
| 3669 | directly = ch_follows * 10 < ch_invisible; |
| 3670 | } |
| 3671 | else |
| 3672 | { |
| 3673 | /* normal invisible, first do the one with the |
| 3674 | * highest failure chance */ |
| 3675 | directly = ch_follows < ch_invisible; |
| 3676 | } |
| 3677 | } |
| 3678 | if (directly) |
| 3679 | /* switch to the _FIRST state */ |
| 3680 | ++prog->state[i].c; |
| 3681 | } |
| 3682 | } |
| 3683 | } |
| 3684 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3685 | /**************************************************************** |
| 3686 | * NFA execution code. |
| 3687 | ****************************************************************/ |
| 3688 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3689 | typedef struct |
| 3690 | { |
| 3691 | int in_use; /* number of subexpr with useful info */ |
| 3692 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3693 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3694 | union |
| 3695 | { |
| 3696 | struct multipos |
| 3697 | { |
| 3698 | lpos_T start; |
| 3699 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3700 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3701 | struct linepos |
| 3702 | { |
| 3703 | char_u *start; |
| 3704 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3705 | } line[NSUBEXP]; |
| 3706 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3707 | } regsub_T; |
| 3708 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3709 | typedef struct |
| 3710 | { |
| 3711 | regsub_T norm; /* \( .. \) matches */ |
| 3712 | #ifdef FEAT_SYN_HL |
| 3713 | regsub_T synt; /* \z( .. \) matches */ |
| 3714 | #endif |
| 3715 | } regsubs_T; |
| 3716 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3717 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3718 | typedef struct nfa_pim_S nfa_pim_T; |
| 3719 | struct nfa_pim_S |
| 3720 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3721 | int result; /* NFA_PIM_*, see below */ |
| 3722 | nfa_state_T *state; /* the invisible match start state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3723 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3724 | union |
| 3725 | { |
| 3726 | lpos_T pos; |
| 3727 | char_u *ptr; |
| 3728 | } end; /* where the match must end */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3729 | }; |
| 3730 | |
| 3731 | /* Values for done in nfa_pim_T. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3732 | #define NFA_PIM_UNUSED 0 /* pim not used */ |
| 3733 | #define NFA_PIM_TODO 1 /* pim not done yet */ |
| 3734 | #define NFA_PIM_MATCH 2 /* pim executed, matches */ |
| 3735 | #define NFA_PIM_NOMATCH 3 /* pim executed, no match */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3736 | |
| 3737 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3738 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3739 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3740 | { |
| 3741 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3742 | int count; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3743 | nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed |
| 3744 | * invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3745 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3746 | } nfa_thread_T; |
| 3747 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3748 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3749 | typedef struct |
| 3750 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3751 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3752 | int n; /* nr of states currently in "t" */ |
| 3753 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3754 | int id; /* ID of the list */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 3755 | int has_pim; /* TRUE when any state has a PIM */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3756 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3757 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3758 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3759 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 3760 | static void log_subexpr __ARGS((regsub_T *sub)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3761 | static char *pim_info __ARGS((nfa_pim_T *pim)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3762 | |
| 3763 | static void |
| 3764 | log_subsexpr(subs) |
| 3765 | regsubs_T *subs; |
| 3766 | { |
| 3767 | log_subexpr(&subs->norm); |
| 3768 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3769 | if (nfa_has_zsubexpr) |
| 3770 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3771 | # endif |
| 3772 | } |
| 3773 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3774 | static void |
| 3775 | log_subexpr(sub) |
| 3776 | regsub_T *sub; |
| 3777 | { |
| 3778 | int j; |
| 3779 | |
| 3780 | for (j = 0; j < sub->in_use; j++) |
| 3781 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3782 | 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] | 3783 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3784 | sub->list.multi[j].start.col, |
| 3785 | (int)sub->list.multi[j].start.lnum, |
| 3786 | sub->list.multi[j].end.col, |
| 3787 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3788 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3789 | { |
| 3790 | char *s = (char *)sub->list.line[j].start; |
| 3791 | char *e = (char *)sub->list.line[j].end; |
| 3792 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3793 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3794 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3795 | s == NULL ? "NULL" : s, |
| 3796 | e == NULL ? "NULL" : e); |
| 3797 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3798 | } |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3799 | |
| 3800 | static char * |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3801 | pim_info(pim) |
| 3802 | nfa_pim_T *pim; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3803 | { |
| 3804 | static char buf[30]; |
| 3805 | |
| 3806 | if (pim == NULL || pim->result == NFA_PIM_UNUSED) |
| 3807 | buf[0] = NUL; |
| 3808 | else |
| 3809 | { |
| 3810 | sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col |
| 3811 | : (int)(pim->end.ptr - reginput)); |
| 3812 | } |
| 3813 | return buf; |
| 3814 | } |
| 3815 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3816 | #endif |
| 3817 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3818 | /* Used during execution: whether a match has been found. */ |
| 3819 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3820 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3821 | 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] | 3822 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3823 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3824 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3825 | static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3826 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3827 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 3828 | static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim)); |
| 3829 | static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two)); |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3830 | static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs)); |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 3831 | static regsubs_T *addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off)); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3832 | 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] | 3833 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3834 | /* |
| 3835 | * Copy postponed invisible match info from "from" to "to". |
| 3836 | */ |
| 3837 | static void |
| 3838 | copy_pim(to, from) |
| 3839 | nfa_pim_T *to; |
| 3840 | nfa_pim_T *from; |
| 3841 | { |
| 3842 | to->result = from->result; |
| 3843 | to->state = from->state; |
| 3844 | copy_sub(&to->subs.norm, &from->subs.norm); |
| 3845 | #ifdef FEAT_SYN_HL |
| 3846 | if (nfa_has_zsubexpr) |
| 3847 | copy_sub(&to->subs.synt, &from->subs.synt); |
| 3848 | #endif |
| 3849 | to->end = from->end; |
| 3850 | } |
| 3851 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3852 | static void |
| 3853 | clear_sub(sub) |
| 3854 | regsub_T *sub; |
| 3855 | { |
| 3856 | if (REG_MULTI) |
| 3857 | /* Use 0xff to set lnum to -1 */ |
| 3858 | vim_memset(sub->list.multi, 0xff, |
| 3859 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3860 | else |
| 3861 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3862 | sub->in_use = 0; |
| 3863 | } |
| 3864 | |
| 3865 | /* |
| 3866 | * Copy the submatches from "from" to "to". |
| 3867 | */ |
| 3868 | static void |
| 3869 | copy_sub(to, from) |
| 3870 | regsub_T *to; |
| 3871 | regsub_T *from; |
| 3872 | { |
| 3873 | to->in_use = from->in_use; |
| 3874 | if (from->in_use > 0) |
| 3875 | { |
| 3876 | /* Copy the match start and end positions. */ |
| 3877 | if (REG_MULTI) |
| 3878 | mch_memmove(&to->list.multi[0], |
| 3879 | &from->list.multi[0], |
| 3880 | sizeof(struct multipos) * from->in_use); |
| 3881 | else |
| 3882 | mch_memmove(&to->list.line[0], |
| 3883 | &from->list.line[0], |
| 3884 | sizeof(struct linepos) * from->in_use); |
| 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | /* |
| 3889 | * Like copy_sub() but exclude the main match. |
| 3890 | */ |
| 3891 | static void |
| 3892 | copy_sub_off(to, from) |
| 3893 | regsub_T *to; |
| 3894 | regsub_T *from; |
| 3895 | { |
| 3896 | if (to->in_use < from->in_use) |
| 3897 | to->in_use = from->in_use; |
| 3898 | if (from->in_use > 1) |
| 3899 | { |
| 3900 | /* Copy the match start and end positions. */ |
| 3901 | if (REG_MULTI) |
| 3902 | mch_memmove(&to->list.multi[1], |
| 3903 | &from->list.multi[1], |
| 3904 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3905 | else |
| 3906 | mch_memmove(&to->list.line[1], |
| 3907 | &from->list.line[1], |
| 3908 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3909 | } |
| 3910 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3911 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3912 | /* |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3913 | * Like copy_sub() but only do the end of the main match if \ze is present. |
| 3914 | */ |
| 3915 | static void |
| 3916 | copy_ze_off(to, from) |
| 3917 | regsub_T *to; |
| 3918 | regsub_T *from; |
| 3919 | { |
| 3920 | if (nfa_has_zend) |
| 3921 | { |
| 3922 | if (REG_MULTI) |
| 3923 | { |
| 3924 | if (from->list.multi[0].end.lnum >= 0) |
| 3925 | to->list.multi[0].end = from->list.multi[0].end; |
| 3926 | } |
| 3927 | else |
| 3928 | { |
| 3929 | if (from->list.line[0].end != NULL) |
| 3930 | to->list.line[0].end = from->list.line[0].end; |
| 3931 | } |
| 3932 | } |
| 3933 | } |
| 3934 | |
| 3935 | /* |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3936 | * Return TRUE if "sub1" and "sub2" have the same start positions. |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3937 | */ |
| 3938 | static int |
| 3939 | sub_equal(sub1, sub2) |
| 3940 | regsub_T *sub1; |
| 3941 | regsub_T *sub2; |
| 3942 | { |
| 3943 | int i; |
| 3944 | int todo; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3945 | linenr_T s1; |
| 3946 | linenr_T s2; |
| 3947 | char_u *sp1; |
| 3948 | char_u *sp2; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3949 | |
| 3950 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3951 | if (REG_MULTI) |
| 3952 | { |
| 3953 | for (i = 0; i < todo; ++i) |
| 3954 | { |
| 3955 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3956 | s1 = sub1->list.multi[i].start.lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3957 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3958 | s1 = -1; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3959 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3960 | s2 = sub2->list.multi[i].start.lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3961 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3962 | s2 = -1; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3963 | if (s1 != s2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3964 | return FALSE; |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3965 | if (s1 != -1 && sub1->list.multi[i].start.col |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3966 | != sub2->list.multi[i].start.col) |
| 3967 | return FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3968 | } |
| 3969 | } |
| 3970 | else |
| 3971 | { |
| 3972 | for (i = 0; i < todo; ++i) |
| 3973 | { |
| 3974 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3975 | sp1 = sub1->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3976 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3977 | sp1 = NULL; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3978 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3979 | sp2 = sub2->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3980 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3981 | sp2 = NULL; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3982 | if (sp1 != sp2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3983 | return FALSE; |
| 3984 | } |
| 3985 | } |
| 3986 | |
| 3987 | return TRUE; |
| 3988 | } |
| 3989 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3990 | #ifdef ENABLE_LOG |
| 3991 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3992 | report_state(char *action, |
| 3993 | regsub_T *sub, |
| 3994 | nfa_state_T *state, |
| 3995 | int lid, |
| 3996 | nfa_pim_T *pim) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 3997 | { |
| 3998 | int col; |
| 3999 | |
| 4000 | if (sub->in_use <= 0) |
| 4001 | col = -1; |
| 4002 | else if (REG_MULTI) |
| 4003 | col = sub->list.multi[0].start.col; |
| 4004 | else |
| 4005 | col = (int)(sub->list.line[0].start - regline); |
| 4006 | nfa_set_code(state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4007 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n", |
| 4008 | action, abs(state->id), lid, state->c, code, col, |
| 4009 | pim_info(pim)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4010 | } |
| 4011 | #endif |
| 4012 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4013 | /* |
| 4014 | * Return TRUE if the same state is already in list "l" with the same |
| 4015 | * positions as "subs". |
| 4016 | */ |
| 4017 | static int |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4018 | has_state_with_pos(l, state, subs, pim) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4019 | nfa_list_T *l; /* runtime state list */ |
| 4020 | nfa_state_T *state; /* state to update */ |
| 4021 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4022 | nfa_pim_T *pim; /* postponed match or NULL */ |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4023 | { |
| 4024 | nfa_thread_T *thread; |
| 4025 | int i; |
| 4026 | |
| 4027 | for (i = 0; i < l->n; ++i) |
| 4028 | { |
| 4029 | thread = &l->t[i]; |
| 4030 | if (thread->state->id == state->id |
| 4031 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 4032 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 4033 | && (!nfa_has_zsubexpr |
| 4034 | || sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4035 | #endif |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4036 | && pim_equal(&thread->pim, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4037 | return TRUE; |
| 4038 | } |
| 4039 | return FALSE; |
| 4040 | } |
| 4041 | |
| 4042 | /* |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4043 | * Return TRUE if "one" and "two" are equal. That includes when both are not |
| 4044 | * set. |
| 4045 | */ |
| 4046 | static int |
| 4047 | pim_equal(one, two) |
| 4048 | nfa_pim_T *one; |
| 4049 | nfa_pim_T *two; |
| 4050 | { |
| 4051 | int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED); |
| 4052 | int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED); |
| 4053 | |
| 4054 | if (one_unused) |
| 4055 | /* one is unused: equal when two is also unused */ |
| 4056 | return two_unused; |
| 4057 | if (two_unused) |
| 4058 | /* one is used and two is not: not equal */ |
| 4059 | return FALSE; |
Bram Moolenaar | 3f0df06 | 2013-08-14 13:34:25 +0200 | [diff] [blame] | 4060 | /* compare the state id */ |
| 4061 | if (one->state->id != two->state->id) |
| 4062 | return FALSE; |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4063 | /* compare the position */ |
| 4064 | if (REG_MULTI) |
| 4065 | return one->end.pos.lnum == two->end.pos.lnum |
| 4066 | && one->end.pos.col == two->end.pos.col; |
| 4067 | return one->end.ptr == two->end.ptr; |
| 4068 | } |
| 4069 | |
| 4070 | /* |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4071 | * Return TRUE if "state" leads to a NFA_MATCH without advancing the input. |
| 4072 | */ |
| 4073 | static int |
| 4074 | match_follows(startstate, depth) |
| 4075 | nfa_state_T *startstate; |
| 4076 | int depth; |
| 4077 | { |
| 4078 | nfa_state_T *state = startstate; |
| 4079 | |
| 4080 | /* avoid too much recursion */ |
| 4081 | if (depth > 10) |
| 4082 | return FALSE; |
| 4083 | |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4084 | while (state != NULL) |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4085 | { |
| 4086 | switch (state->c) |
| 4087 | { |
| 4088 | case NFA_MATCH: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4089 | case NFA_MCLOSE: |
| 4090 | case NFA_END_INVISIBLE: |
| 4091 | case NFA_END_INVISIBLE_NEG: |
| 4092 | case NFA_END_PATTERN: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4093 | return TRUE; |
| 4094 | |
| 4095 | case NFA_SPLIT: |
| 4096 | return match_follows(state->out, depth + 1) |
| 4097 | || match_follows(state->out1, depth + 1); |
| 4098 | |
| 4099 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4100 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4101 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4102 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4103 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4104 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4105 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4106 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4107 | case NFA_COMPOSING: |
| 4108 | /* skip ahead to next state */ |
| 4109 | state = state->out1->out; |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4110 | continue; |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4111 | |
| 4112 | case NFA_ANY: |
| 4113 | case NFA_IDENT: |
| 4114 | case NFA_SIDENT: |
| 4115 | case NFA_KWORD: |
| 4116 | case NFA_SKWORD: |
| 4117 | case NFA_FNAME: |
| 4118 | case NFA_SFNAME: |
| 4119 | case NFA_PRINT: |
| 4120 | case NFA_SPRINT: |
| 4121 | case NFA_WHITE: |
| 4122 | case NFA_NWHITE: |
| 4123 | case NFA_DIGIT: |
| 4124 | case NFA_NDIGIT: |
| 4125 | case NFA_HEX: |
| 4126 | case NFA_NHEX: |
| 4127 | case NFA_OCTAL: |
| 4128 | case NFA_NOCTAL: |
| 4129 | case NFA_WORD: |
| 4130 | case NFA_NWORD: |
| 4131 | case NFA_HEAD: |
| 4132 | case NFA_NHEAD: |
| 4133 | case NFA_ALPHA: |
| 4134 | case NFA_NALPHA: |
| 4135 | case NFA_LOWER: |
| 4136 | case NFA_NLOWER: |
| 4137 | case NFA_UPPER: |
| 4138 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 4139 | case NFA_LOWER_IC: |
| 4140 | case NFA_NLOWER_IC: |
| 4141 | case NFA_UPPER_IC: |
| 4142 | case NFA_NUPPER_IC: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4143 | case NFA_START_COLL: |
| 4144 | case NFA_START_NEG_COLL: |
| 4145 | case NFA_NEWL: |
| 4146 | /* state will advance input */ |
| 4147 | return FALSE; |
| 4148 | |
| 4149 | default: |
| 4150 | if (state->c > 0) |
| 4151 | /* state will advance input */ |
| 4152 | return FALSE; |
| 4153 | |
| 4154 | /* Others: zero-width or possibly zero-width, might still find |
| 4155 | * a match at the same position, keep looking. */ |
| 4156 | break; |
| 4157 | } |
| 4158 | state = state->out; |
| 4159 | } |
| 4160 | return FALSE; |
| 4161 | } |
| 4162 | |
| 4163 | |
| 4164 | /* |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4165 | * Return TRUE if "state" is already in list "l". |
| 4166 | */ |
| 4167 | static int |
| 4168 | state_in_list(l, state, subs) |
| 4169 | nfa_list_T *l; /* runtime state list */ |
| 4170 | nfa_state_T *state; /* state to update */ |
| 4171 | regsubs_T *subs; /* pointers to subexpressions */ |
| 4172 | { |
| 4173 | if (state->lastlist[nfa_ll_index] == l->id) |
| 4174 | { |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4175 | if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4176 | return TRUE; |
| 4177 | } |
| 4178 | return FALSE; |
| 4179 | } |
| 4180 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4181 | /* |
| 4182 | * Add "state" and possibly what follows to state list ".". |
| 4183 | * Returns "subs_arg", possibly copied into temp_subs. |
| 4184 | */ |
| 4185 | |
| 4186 | static regsubs_T * |
| 4187 | addstate(l, state, subs_arg, pim, off) |
| 4188 | nfa_list_T *l; /* runtime state list */ |
| 4189 | nfa_state_T *state; /* state to update */ |
| 4190 | regsubs_T *subs_arg; /* pointers to subexpressions */ |
| 4191 | nfa_pim_T *pim; /* postponed look-behind match */ |
| 4192 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4193 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4194 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4195 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4196 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4197 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4198 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4199 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4200 | regsub_T *sub; |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4201 | regsubs_T *subs = subs_arg; |
| 4202 | static regsubs_T temp_subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4203 | #ifdef ENABLE_LOG |
| 4204 | int did_print = FALSE; |
| 4205 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4206 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4207 | switch (state->c) |
| 4208 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4209 | case NFA_NCLOSE: |
| 4210 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4211 | case NFA_MCLOSE1: |
| 4212 | case NFA_MCLOSE2: |
| 4213 | case NFA_MCLOSE3: |
| 4214 | case NFA_MCLOSE4: |
| 4215 | case NFA_MCLOSE5: |
| 4216 | case NFA_MCLOSE6: |
| 4217 | case NFA_MCLOSE7: |
| 4218 | case NFA_MCLOSE8: |
| 4219 | case NFA_MCLOSE9: |
| 4220 | #ifdef FEAT_SYN_HL |
| 4221 | case NFA_ZCLOSE: |
| 4222 | case NFA_ZCLOSE1: |
| 4223 | case NFA_ZCLOSE2: |
| 4224 | case NFA_ZCLOSE3: |
| 4225 | case NFA_ZCLOSE4: |
| 4226 | case NFA_ZCLOSE5: |
| 4227 | case NFA_ZCLOSE6: |
| 4228 | case NFA_ZCLOSE7: |
| 4229 | case NFA_ZCLOSE8: |
| 4230 | case NFA_ZCLOSE9: |
| 4231 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4232 | case NFA_MOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4233 | case NFA_ZEND: |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4234 | case NFA_SPLIT: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4235 | case NFA_EMPTY: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4236 | /* These nodes are not added themselves but their "out" and/or |
| 4237 | * "out1" may be added below. */ |
| 4238 | break; |
| 4239 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4240 | case NFA_BOL: |
| 4241 | case NFA_BOF: |
| 4242 | /* "^" won't match past end-of-line, don't bother trying. |
| 4243 | * Except when at the end of the line, or when we are going to the |
| 4244 | * next line for a look-behind match. */ |
| 4245 | if (reginput > regline |
| 4246 | && *reginput != NUL |
| 4247 | && (nfa_endp == NULL |
| 4248 | || !REG_MULTI |
| 4249 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 4250 | goto skip_add; |
| 4251 | /* FALLTHROUGH */ |
| 4252 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4253 | case NFA_MOPEN1: |
| 4254 | case NFA_MOPEN2: |
| 4255 | case NFA_MOPEN3: |
| 4256 | case NFA_MOPEN4: |
| 4257 | case NFA_MOPEN5: |
| 4258 | case NFA_MOPEN6: |
| 4259 | case NFA_MOPEN7: |
| 4260 | case NFA_MOPEN8: |
| 4261 | case NFA_MOPEN9: |
| 4262 | #ifdef FEAT_SYN_HL |
| 4263 | case NFA_ZOPEN: |
| 4264 | case NFA_ZOPEN1: |
| 4265 | case NFA_ZOPEN2: |
| 4266 | case NFA_ZOPEN3: |
| 4267 | case NFA_ZOPEN4: |
| 4268 | case NFA_ZOPEN5: |
| 4269 | case NFA_ZOPEN6: |
| 4270 | case NFA_ZOPEN7: |
| 4271 | case NFA_ZOPEN8: |
| 4272 | case NFA_ZOPEN9: |
| 4273 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4274 | case NFA_NOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4275 | case NFA_ZSTART: |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4276 | /* These nodes need to be added so that we can bail out when it |
| 4277 | * was added to this list before at the same position to avoid an |
| 4278 | * endless loop for "\(\)*" */ |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4279 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4280 | default: |
Bram Moolenaar | 272fb58 | 2013-11-21 16:03:40 +0100 | [diff] [blame] | 4281 | if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4282 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4283 | /* This state is already in the list, don't add it again, |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4284 | * unless it is an MOPEN that is used for a backreference or |
| 4285 | * when there is a PIM. */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4286 | if (!nfa_has_backref && pim == NULL && !l->has_pim) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4287 | { |
| 4288 | skip_add: |
| 4289 | #ifdef ENABLE_LOG |
| 4290 | nfa_set_code(state->c); |
| 4291 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 4292 | abs(state->id), l->id, state->c, code); |
| 4293 | #endif |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4294 | return subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4295 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4296 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4297 | /* Do not add the state again when it exists with the same |
| 4298 | * positions. */ |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4299 | if (has_state_with_pos(l, state, subs, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4300 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4301 | } |
| 4302 | |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4303 | /* When there are backreferences or PIMs the number of states may |
| 4304 | * be (a lot) bigger than anticipated. */ |
| 4305 | if (l->n == l->len) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4306 | { |
| 4307 | int newlen = l->len * 3 / 2 + 50; |
| 4308 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4309 | if (subs != &temp_subs) |
| 4310 | { |
| 4311 | /* "subs" may point into the current array, need to make a |
| 4312 | * copy before it becomes invalid. */ |
| 4313 | copy_sub(&temp_subs.norm, &subs->norm); |
| 4314 | #ifdef FEAT_SYN_HL |
| 4315 | if (nfa_has_zsubexpr) |
| 4316 | copy_sub(&temp_subs.synt, &subs->synt); |
| 4317 | #endif |
| 4318 | subs = &temp_subs; |
| 4319 | } |
| 4320 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4321 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 4322 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4323 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4324 | |
| 4325 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4326 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4327 | thread = &l->t[l->n++]; |
| 4328 | thread->state = state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4329 | if (pim == NULL) |
| 4330 | thread->pim.result = NFA_PIM_UNUSED; |
| 4331 | else |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4332 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4333 | copy_pim(&thread->pim, pim); |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4334 | l->has_pim = TRUE; |
| 4335 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4336 | copy_sub(&thread->subs.norm, &subs->norm); |
| 4337 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4338 | if (nfa_has_zsubexpr) |
| 4339 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4340 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4341 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4342 | report_state("Adding", &thread->subs.norm, state, l->id, pim); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4343 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4344 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4348 | if (!did_print) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4349 | report_state("Processing", &subs->norm, state, l->id, pim); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4350 | #endif |
| 4351 | switch (state->c) |
| 4352 | { |
| 4353 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4354 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4355 | break; |
| 4356 | |
| 4357 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4358 | /* order matters here */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4359 | subs = addstate(l, state->out, subs, pim, off); |
| 4360 | subs = addstate(l, state->out1, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4361 | break; |
| 4362 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4363 | case NFA_EMPTY: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4364 | case NFA_NOPEN: |
| 4365 | case NFA_NCLOSE: |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4366 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4367 | break; |
| 4368 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4369 | case NFA_MOPEN: |
| 4370 | case NFA_MOPEN1: |
| 4371 | case NFA_MOPEN2: |
| 4372 | case NFA_MOPEN3: |
| 4373 | case NFA_MOPEN4: |
| 4374 | case NFA_MOPEN5: |
| 4375 | case NFA_MOPEN6: |
| 4376 | case NFA_MOPEN7: |
| 4377 | case NFA_MOPEN8: |
| 4378 | case NFA_MOPEN9: |
| 4379 | #ifdef FEAT_SYN_HL |
| 4380 | case NFA_ZOPEN: |
| 4381 | case NFA_ZOPEN1: |
| 4382 | case NFA_ZOPEN2: |
| 4383 | case NFA_ZOPEN3: |
| 4384 | case NFA_ZOPEN4: |
| 4385 | case NFA_ZOPEN5: |
| 4386 | case NFA_ZOPEN6: |
| 4387 | case NFA_ZOPEN7: |
| 4388 | case NFA_ZOPEN8: |
| 4389 | case NFA_ZOPEN9: |
| 4390 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4391 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4392 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4393 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4394 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4395 | sub = &subs->norm; |
| 4396 | } |
| 4397 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4398 | else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4399 | { |
| 4400 | subidx = state->c - NFA_ZOPEN; |
| 4401 | sub = &subs->synt; |
| 4402 | } |
| 4403 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4404 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4405 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4406 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4407 | sub = &subs->norm; |
| 4408 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4409 | |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4410 | /* avoid compiler warnings */ |
| 4411 | save_ptr = NULL; |
| 4412 | save_lpos.lnum = 0; |
| 4413 | save_lpos.col = 0; |
| 4414 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4415 | /* Set the position (with "off" added) in the subexpression. Save |
| 4416 | * and restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4417 | if (REG_MULTI) |
| 4418 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4419 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4420 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4421 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4422 | save_in_use = -1; |
| 4423 | } |
| 4424 | else |
| 4425 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4426 | save_in_use = sub->in_use; |
| 4427 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4428 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4429 | sub->list.multi[i].start.lnum = -1; |
| 4430 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4431 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4432 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4433 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4434 | if (off == -1) |
| 4435 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4436 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 4437 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4438 | } |
| 4439 | else |
| 4440 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4441 | sub->list.multi[subidx].start.lnum = reglnum; |
| 4442 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4443 | (colnr_T)(reginput - regline + off); |
| 4444 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4445 | } |
| 4446 | else |
| 4447 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4448 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4449 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4450 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4451 | save_in_use = -1; |
| 4452 | } |
| 4453 | else |
| 4454 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4455 | save_in_use = sub->in_use; |
| 4456 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4457 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4458 | sub->list.line[i].start = NULL; |
| 4459 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4460 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4461 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4462 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4463 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4464 | } |
| 4465 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4466 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4467 | /* "subs" may have changed, need to set "sub" again */ |
| 4468 | #ifdef FEAT_SYN_HL |
| 4469 | if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
| 4470 | sub = &subs->synt; |
| 4471 | else |
| 4472 | #endif |
| 4473 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4474 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4475 | if (save_in_use == -1) |
| 4476 | { |
| 4477 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4478 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4479 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4480 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4481 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4482 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4483 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4484 | break; |
| 4485 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4486 | case NFA_MCLOSE: |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4487 | if (nfa_has_zend && (REG_MULTI |
| 4488 | ? subs->norm.list.multi[0].end.lnum >= 0 |
| 4489 | : subs->norm.list.line[0].end != NULL)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4490 | { |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4491 | /* Do not overwrite the position set by \ze. */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4492 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4493 | break; |
| 4494 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4495 | case NFA_MCLOSE1: |
| 4496 | case NFA_MCLOSE2: |
| 4497 | case NFA_MCLOSE3: |
| 4498 | case NFA_MCLOSE4: |
| 4499 | case NFA_MCLOSE5: |
| 4500 | case NFA_MCLOSE6: |
| 4501 | case NFA_MCLOSE7: |
| 4502 | case NFA_MCLOSE8: |
| 4503 | case NFA_MCLOSE9: |
| 4504 | #ifdef FEAT_SYN_HL |
| 4505 | case NFA_ZCLOSE: |
| 4506 | case NFA_ZCLOSE1: |
| 4507 | case NFA_ZCLOSE2: |
| 4508 | case NFA_ZCLOSE3: |
| 4509 | case NFA_ZCLOSE4: |
| 4510 | case NFA_ZCLOSE5: |
| 4511 | case NFA_ZCLOSE6: |
| 4512 | case NFA_ZCLOSE7: |
| 4513 | case NFA_ZCLOSE8: |
| 4514 | case NFA_ZCLOSE9: |
| 4515 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4516 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4517 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4518 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4519 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4520 | sub = &subs->norm; |
| 4521 | } |
| 4522 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4523 | else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4524 | { |
| 4525 | subidx = state->c - NFA_ZCLOSE; |
| 4526 | sub = &subs->synt; |
| 4527 | } |
| 4528 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4529 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4530 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4531 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4532 | sub = &subs->norm; |
| 4533 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4534 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4535 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 4536 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4537 | save_in_use = sub->in_use; |
| 4538 | if (sub->in_use <= subidx) |
| 4539 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4540 | if (REG_MULTI) |
| 4541 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4542 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4543 | if (off == -1) |
| 4544 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4545 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 4546 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4547 | } |
| 4548 | else |
| 4549 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4550 | sub->list.multi[subidx].end.lnum = reglnum; |
| 4551 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4552 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4553 | } |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4554 | /* avoid compiler warnings */ |
| 4555 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4556 | } |
| 4557 | else |
| 4558 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4559 | save_ptr = sub->list.line[subidx].end; |
| 4560 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4561 | /* avoid compiler warnings */ |
| 4562 | save_lpos.lnum = 0; |
| 4563 | save_lpos.col = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4564 | } |
| 4565 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4566 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4567 | /* "subs" may have changed, need to set "sub" again */ |
| 4568 | #ifdef FEAT_SYN_HL |
| 4569 | if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
| 4570 | sub = &subs->synt; |
| 4571 | else |
| 4572 | #endif |
| 4573 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4574 | |
| 4575 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4576 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4577 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4578 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4579 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4580 | break; |
| 4581 | } |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4582 | return subs; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4583 | } |
| 4584 | |
| 4585 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4586 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 4587 | * Used for zero-width matches, next state to use is the added one. |
| 4588 | * This makes sure the order of states to be tried does not change, which |
| 4589 | * matters for alternatives. |
| 4590 | */ |
| 4591 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4592 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4593 | nfa_list_T *l; /* runtime state list */ |
| 4594 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4595 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4596 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4597 | int *ip; |
| 4598 | { |
| 4599 | int tlen = l->n; |
| 4600 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4601 | int listidx = *ip; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4602 | |
| 4603 | /* 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] | 4604 | addstate(l, state, subs, pim, 0); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4605 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4606 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4607 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4608 | return; |
| 4609 | |
| 4610 | /* re-order to put the new state at the current position */ |
| 4611 | count = l->n - tlen; |
Bram Moolenaar | a50d02d | 2013-06-16 15:43:50 +0200 | [diff] [blame] | 4612 | if (count == 0) |
| 4613 | return; /* no state got added */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4614 | if (count == 1) |
| 4615 | { |
| 4616 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4617 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4618 | } |
| 4619 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4620 | { |
Bram Moolenaar | 55480dc | 2013-06-30 13:17:24 +0200 | [diff] [blame] | 4621 | if (l->n + count - 1 >= l->len) |
| 4622 | { |
| 4623 | /* not enough space to move the new states, reallocate the list |
| 4624 | * and move the states to the right position */ |
| 4625 | nfa_thread_T *newl; |
| 4626 | |
| 4627 | l->len = l->len * 3 / 2 + 50; |
| 4628 | newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T)); |
| 4629 | if (newl == NULL) |
| 4630 | return; |
| 4631 | mch_memmove(&(newl[0]), |
| 4632 | &(l->t[0]), |
| 4633 | sizeof(nfa_thread_T) * listidx); |
| 4634 | mch_memmove(&(newl[listidx]), |
| 4635 | &(l->t[l->n - count]), |
| 4636 | sizeof(nfa_thread_T) * count); |
| 4637 | mch_memmove(&(newl[listidx + count]), |
| 4638 | &(l->t[listidx + 1]), |
| 4639 | sizeof(nfa_thread_T) * (l->n - count - listidx - 1)); |
| 4640 | vim_free(l->t); |
| 4641 | l->t = newl; |
| 4642 | } |
| 4643 | else |
| 4644 | { |
| 4645 | /* make space for new states, then move them from the |
| 4646 | * end to the current position */ |
| 4647 | mch_memmove(&(l->t[listidx + count]), |
| 4648 | &(l->t[listidx + 1]), |
| 4649 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 4650 | mch_memmove(&(l->t[listidx]), |
| 4651 | &(l->t[l->n - 1]), |
| 4652 | sizeof(nfa_thread_T) * count); |
| 4653 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4654 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4655 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4656 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4660 | * Check character class "class" against current character c. |
| 4661 | */ |
| 4662 | static int |
| 4663 | check_char_class(class, c) |
| 4664 | int class; |
| 4665 | int c; |
| 4666 | { |
| 4667 | switch (class) |
| 4668 | { |
| 4669 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4670 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4671 | return OK; |
| 4672 | break; |
| 4673 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4674 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4675 | return OK; |
| 4676 | break; |
| 4677 | case NFA_CLASS_BLANK: |
| 4678 | if (c == ' ' || c == '\t') |
| 4679 | return OK; |
| 4680 | break; |
| 4681 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4682 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4683 | return OK; |
| 4684 | break; |
| 4685 | case NFA_CLASS_DIGIT: |
| 4686 | if (VIM_ISDIGIT(c)) |
| 4687 | return OK; |
| 4688 | break; |
| 4689 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4690 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4691 | return OK; |
| 4692 | break; |
| 4693 | case NFA_CLASS_LOWER: |
| 4694 | if (MB_ISLOWER(c)) |
| 4695 | return OK; |
| 4696 | break; |
| 4697 | case NFA_CLASS_PRINT: |
| 4698 | if (vim_isprintc(c)) |
| 4699 | return OK; |
| 4700 | break; |
| 4701 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4702 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4703 | return OK; |
| 4704 | break; |
| 4705 | case NFA_CLASS_SPACE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4706 | if ((c >= 9 && c <= 13) || (c == ' ')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4707 | return OK; |
| 4708 | break; |
| 4709 | case NFA_CLASS_UPPER: |
| 4710 | if (MB_ISUPPER(c)) |
| 4711 | return OK; |
| 4712 | break; |
| 4713 | case NFA_CLASS_XDIGIT: |
| 4714 | if (vim_isxdigit(c)) |
| 4715 | return OK; |
| 4716 | break; |
| 4717 | case NFA_CLASS_TAB: |
| 4718 | if (c == '\t') |
| 4719 | return OK; |
| 4720 | break; |
| 4721 | case NFA_CLASS_RETURN: |
| 4722 | if (c == '\r') |
| 4723 | return OK; |
| 4724 | break; |
| 4725 | case NFA_CLASS_BACKSPACE: |
| 4726 | if (c == '\b') |
| 4727 | return OK; |
| 4728 | break; |
| 4729 | case NFA_CLASS_ESCAPE: |
| 4730 | if (c == '\033') |
| 4731 | return OK; |
| 4732 | break; |
| 4733 | |
| 4734 | default: |
| 4735 | /* should not be here :P */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 4736 | EMSGN("E877: (NFA regexp) Invalid character class: %ld", class); |
| 4737 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4738 | } |
| 4739 | return FAIL; |
| 4740 | } |
| 4741 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4742 | /* |
| 4743 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4744 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4745 | */ |
| 4746 | static int |
| 4747 | match_backref(sub, subidx, bytelen) |
| 4748 | regsub_T *sub; /* pointers to subexpressions */ |
| 4749 | int subidx; |
| 4750 | int *bytelen; /* out: length of match in bytes */ |
| 4751 | { |
| 4752 | int len; |
| 4753 | |
| 4754 | if (sub->in_use <= subidx) |
| 4755 | { |
| 4756 | retempty: |
| 4757 | /* backref was not set, match an empty string */ |
| 4758 | *bytelen = 0; |
| 4759 | return TRUE; |
| 4760 | } |
| 4761 | |
| 4762 | if (REG_MULTI) |
| 4763 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4764 | if (sub->list.multi[subidx].start.lnum < 0 |
| 4765 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4766 | goto retempty; |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4767 | if (sub->list.multi[subidx].start.lnum == reglnum |
| 4768 | && sub->list.multi[subidx].end.lnum == reglnum) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4769 | { |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4770 | len = sub->list.multi[subidx].end.col |
| 4771 | - sub->list.multi[subidx].start.col; |
| 4772 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
| 4773 | reginput, &len) == 0) |
| 4774 | { |
| 4775 | *bytelen = len; |
| 4776 | return TRUE; |
| 4777 | } |
| 4778 | } |
| 4779 | else |
| 4780 | { |
| 4781 | if (match_with_backref( |
| 4782 | sub->list.multi[subidx].start.lnum, |
| 4783 | sub->list.multi[subidx].start.col, |
| 4784 | sub->list.multi[subidx].end.lnum, |
| 4785 | sub->list.multi[subidx].end.col, |
| 4786 | bytelen) == RA_MATCH) |
| 4787 | return TRUE; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4788 | } |
| 4789 | } |
| 4790 | else |
| 4791 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4792 | if (sub->list.line[subidx].start == NULL |
| 4793 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4794 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4795 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 4796 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4797 | { |
| 4798 | *bytelen = len; |
| 4799 | return TRUE; |
| 4800 | } |
| 4801 | } |
| 4802 | return FALSE; |
| 4803 | } |
| 4804 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4805 | #ifdef FEAT_SYN_HL |
| 4806 | |
| 4807 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 4808 | |
| 4809 | /* |
| 4810 | * Check for a match with \z subexpression "subidx". |
| 4811 | * Return TRUE if it matches. |
| 4812 | */ |
| 4813 | static int |
| 4814 | match_zref(subidx, bytelen) |
| 4815 | int subidx; |
| 4816 | int *bytelen; /* out: length of match in bytes */ |
| 4817 | { |
| 4818 | int len; |
| 4819 | |
| 4820 | cleanup_zsubexpr(); |
| 4821 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 4822 | { |
| 4823 | /* backref was not set, match an empty string */ |
| 4824 | *bytelen = 0; |
| 4825 | return TRUE; |
| 4826 | } |
| 4827 | |
| 4828 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 4829 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 4830 | { |
| 4831 | *bytelen = len; |
| 4832 | return TRUE; |
| 4833 | } |
| 4834 | return FALSE; |
| 4835 | } |
| 4836 | #endif |
| 4837 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4838 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4839 | * Save list IDs for all NFA states of "prog" into "list". |
| 4840 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4841 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4842 | */ |
| 4843 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4844 | nfa_save_listids(prog, list) |
| 4845 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4846 | int *list; |
| 4847 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4848 | int i; |
| 4849 | nfa_state_T *p; |
| 4850 | |
| 4851 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 4852 | p = &prog->state[0]; |
| 4853 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4854 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4855 | list[i] = p->lastlist[1]; |
| 4856 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4857 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4858 | } |
| 4859 | } |
| 4860 | |
| 4861 | /* |
| 4862 | * Restore list IDs from "list" to all NFA states. |
| 4863 | */ |
| 4864 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4865 | nfa_restore_listids(prog, list) |
| 4866 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4867 | int *list; |
| 4868 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4869 | int i; |
| 4870 | nfa_state_T *p; |
| 4871 | |
| 4872 | p = &prog->state[0]; |
| 4873 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4874 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4875 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4876 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4880 | static int |
| 4881 | nfa_re_num_cmp(val, op, pos) |
| 4882 | long_u val; |
| 4883 | int op; |
| 4884 | long_u pos; |
| 4885 | { |
| 4886 | if (op == 1) return pos > val; |
| 4887 | if (op == 2) return pos < val; |
| 4888 | return val == pos; |
| 4889 | } |
| 4890 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4891 | 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] | 4892 | 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] | 4893 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4894 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4895 | * Recursively call nfa_regmatch() |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4896 | * "pim" is NULL or contains info about a Postponed Invisible Match (start |
| 4897 | * position). |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4898 | */ |
| 4899 | static int |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4900 | recursive_regmatch(state, pim, prog, submatch, m, listids) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4901 | nfa_state_T *state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4902 | nfa_pim_T *pim; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4903 | nfa_regprog_T *prog; |
| 4904 | regsubs_T *submatch; |
| 4905 | regsubs_T *m; |
| 4906 | int **listids; |
| 4907 | { |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4908 | int save_reginput_col = (int)(reginput - regline); |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4909 | int save_reglnum = reglnum; |
| 4910 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4911 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4912 | save_se_T *save_nfa_endp = nfa_endp; |
| 4913 | save_se_T endpos; |
| 4914 | save_se_T *endposp = NULL; |
| 4915 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4916 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4917 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4918 | if (pim != NULL) |
| 4919 | { |
| 4920 | /* start at the position where the postponed match was */ |
| 4921 | if (REG_MULTI) |
| 4922 | reginput = regline + pim->end.pos.col; |
| 4923 | else |
| 4924 | reginput = pim->end.ptr; |
| 4925 | } |
| 4926 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4927 | if (state->c == NFA_START_INVISIBLE_BEFORE |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4928 | || state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 4929 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG |
| 4930 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4931 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4932 | /* The recursive match must end at the current position. When "pim" is |
| 4933 | * not NULL it specifies the current position. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4934 | endposp = &endpos; |
| 4935 | if (REG_MULTI) |
| 4936 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4937 | if (pim == NULL) |
| 4938 | { |
| 4939 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 4940 | endpos.se_u.pos.lnum = reglnum; |
| 4941 | } |
| 4942 | else |
| 4943 | endpos.se_u.pos = pim->end.pos; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4944 | } |
| 4945 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4946 | { |
| 4947 | if (pim == NULL) |
| 4948 | endpos.se_u.ptr = reginput; |
| 4949 | else |
| 4950 | endpos.se_u.ptr = pim->end.ptr; |
| 4951 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4952 | |
| 4953 | /* Go back the specified number of bytes, or as far as the |
| 4954 | * start of the previous line, to try matching "\@<=" or |
Bram Moolenaar | 3fb14bc | 2013-07-14 12:34:56 +0200 | [diff] [blame] | 4955 | * not matching "\@<!". This is very inefficient, limit the number of |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4956 | * bytes if possible. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4957 | if (state->val <= 0) |
| 4958 | { |
| 4959 | if (REG_MULTI) |
| 4960 | { |
| 4961 | regline = reg_getline(--reglnum); |
| 4962 | if (regline == NULL) |
| 4963 | /* can't go before the first line */ |
| 4964 | regline = reg_getline(++reglnum); |
| 4965 | } |
| 4966 | reginput = regline; |
| 4967 | } |
| 4968 | else |
| 4969 | { |
| 4970 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 4971 | { |
| 4972 | /* Not enough bytes in this line, go to end of |
| 4973 | * previous line. */ |
| 4974 | regline = reg_getline(--reglnum); |
| 4975 | if (regline == NULL) |
| 4976 | { |
| 4977 | /* can't go before the first line */ |
| 4978 | regline = reg_getline(++reglnum); |
| 4979 | reginput = regline; |
| 4980 | } |
| 4981 | else |
| 4982 | reginput = regline + STRLEN(regline); |
| 4983 | } |
| 4984 | if ((int)(reginput - regline) >= state->val) |
| 4985 | { |
| 4986 | reginput -= state->val; |
| 4987 | #ifdef FEAT_MBYTE |
| 4988 | if (has_mbyte) |
| 4989 | reginput -= mb_head_off(regline, reginput); |
| 4990 | #endif |
| 4991 | } |
| 4992 | else |
| 4993 | reginput = regline; |
| 4994 | } |
| 4995 | } |
| 4996 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4997 | #ifdef ENABLE_LOG |
| 4998 | if (log_fd != stderr) |
| 4999 | fclose(log_fd); |
| 5000 | log_fd = NULL; |
| 5001 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5002 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 5003 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 5004 | if (nfa_ll_index == 1) |
| 5005 | { |
| 5006 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 5007 | * values and clear them. */ |
| 5008 | if (*listids == NULL) |
| 5009 | { |
| 5010 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 5011 | if (*listids == NULL) |
| 5012 | { |
| 5013 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 5014 | return 0; |
| 5015 | } |
| 5016 | } |
| 5017 | nfa_save_listids(prog, *listids); |
| 5018 | need_restore = TRUE; |
| 5019 | /* any value of nfa_listid will do */ |
| 5020 | } |
| 5021 | else |
| 5022 | { |
| 5023 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 5024 | * entry. Make sure nfa_listid is different from a previous recursive |
| 5025 | * call, because some states may still have this ID. */ |
| 5026 | ++nfa_ll_index; |
| 5027 | if (nfa_listid <= nfa_alt_listid) |
| 5028 | nfa_listid = nfa_alt_listid; |
| 5029 | } |
| 5030 | |
| 5031 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 5032 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5033 | nfa_endp = endposp; |
| 5034 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5035 | |
| 5036 | if (need_restore) |
| 5037 | nfa_restore_listids(prog, *listids); |
| 5038 | else |
| 5039 | { |
| 5040 | --nfa_ll_index; |
| 5041 | nfa_alt_listid = nfa_listid; |
| 5042 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5043 | |
| 5044 | /* restore position in input text */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5045 | reglnum = save_reglnum; |
Bram Moolenaar | 484d241 | 2013-06-13 19:47:07 +0200 | [diff] [blame] | 5046 | if (REG_MULTI) |
| 5047 | regline = reg_getline(reglnum); |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 5048 | reginput = regline + save_reginput_col; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5049 | nfa_match = save_nfa_match; |
| 5050 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5051 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5052 | |
| 5053 | #ifdef ENABLE_LOG |
| 5054 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 5055 | if (log_fd != NULL) |
| 5056 | { |
| 5057 | fprintf(log_fd, "****************************\n"); |
| 5058 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 5059 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5060 | fprintf(log_fd, "****************************\n"); |
| 5061 | } |
| 5062 | else |
| 5063 | { |
| 5064 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5065 | log_fd = stderr; |
| 5066 | } |
| 5067 | #endif |
| 5068 | |
| 5069 | return result; |
| 5070 | } |
| 5071 | |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5072 | static int skip_to_start __ARGS((int c, colnr_T *colp)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5073 | 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] | 5074 | |
| 5075 | /* |
| 5076 | * Estimate the chance of a match with "state" failing. |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5077 | * empty match: 0 |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5078 | * NFA_ANY: 1 |
| 5079 | * specific character: 99 |
| 5080 | */ |
| 5081 | static int |
| 5082 | failure_chance(state, depth) |
| 5083 | nfa_state_T *state; |
| 5084 | int depth; |
| 5085 | { |
| 5086 | int c = state->c; |
| 5087 | int l, r; |
| 5088 | |
| 5089 | /* detect looping */ |
| 5090 | if (depth > 4) |
| 5091 | return 1; |
| 5092 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5093 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5094 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5095 | case NFA_SPLIT: |
| 5096 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 5097 | /* avoid recursive stuff */ |
| 5098 | return 1; |
| 5099 | /* two alternatives, use the lowest failure chance */ |
| 5100 | l = failure_chance(state->out, depth + 1); |
| 5101 | r = failure_chance(state->out1, depth + 1); |
| 5102 | return l < r ? l : r; |
| 5103 | |
| 5104 | case NFA_ANY: |
| 5105 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5106 | return 1; |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5107 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5108 | case NFA_MATCH: |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5109 | case NFA_MCLOSE: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5110 | /* empty match works always */ |
| 5111 | return 0; |
| 5112 | |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5113 | case NFA_START_INVISIBLE: |
| 5114 | case NFA_START_INVISIBLE_FIRST: |
| 5115 | case NFA_START_INVISIBLE_NEG: |
| 5116 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 5117 | case NFA_START_INVISIBLE_BEFORE: |
| 5118 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 5119 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 5120 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 5121 | case NFA_START_PATTERN: |
| 5122 | /* recursive regmatch is expensive, use low failure chance */ |
| 5123 | return 5; |
| 5124 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5125 | case NFA_BOL: |
| 5126 | case NFA_EOL: |
| 5127 | case NFA_BOF: |
| 5128 | case NFA_EOF: |
| 5129 | case NFA_NEWL: |
| 5130 | return 99; |
| 5131 | |
| 5132 | case NFA_BOW: |
| 5133 | case NFA_EOW: |
| 5134 | return 90; |
| 5135 | |
| 5136 | case NFA_MOPEN: |
| 5137 | case NFA_MOPEN1: |
| 5138 | case NFA_MOPEN2: |
| 5139 | case NFA_MOPEN3: |
| 5140 | case NFA_MOPEN4: |
| 5141 | case NFA_MOPEN5: |
| 5142 | case NFA_MOPEN6: |
| 5143 | case NFA_MOPEN7: |
| 5144 | case NFA_MOPEN8: |
| 5145 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5146 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5147 | case NFA_ZOPEN: |
| 5148 | case NFA_ZOPEN1: |
| 5149 | case NFA_ZOPEN2: |
| 5150 | case NFA_ZOPEN3: |
| 5151 | case NFA_ZOPEN4: |
| 5152 | case NFA_ZOPEN5: |
| 5153 | case NFA_ZOPEN6: |
| 5154 | case NFA_ZOPEN7: |
| 5155 | case NFA_ZOPEN8: |
| 5156 | case NFA_ZOPEN9: |
| 5157 | case NFA_ZCLOSE: |
| 5158 | case NFA_ZCLOSE1: |
| 5159 | case NFA_ZCLOSE2: |
| 5160 | case NFA_ZCLOSE3: |
| 5161 | case NFA_ZCLOSE4: |
| 5162 | case NFA_ZCLOSE5: |
| 5163 | case NFA_ZCLOSE6: |
| 5164 | case NFA_ZCLOSE7: |
| 5165 | case NFA_ZCLOSE8: |
| 5166 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5167 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5168 | case NFA_NOPEN: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5169 | case NFA_MCLOSE1: |
| 5170 | case NFA_MCLOSE2: |
| 5171 | case NFA_MCLOSE3: |
| 5172 | case NFA_MCLOSE4: |
| 5173 | case NFA_MCLOSE5: |
| 5174 | case NFA_MCLOSE6: |
| 5175 | case NFA_MCLOSE7: |
| 5176 | case NFA_MCLOSE8: |
| 5177 | case NFA_MCLOSE9: |
| 5178 | case NFA_NCLOSE: |
| 5179 | return failure_chance(state->out, depth + 1); |
| 5180 | |
| 5181 | case NFA_BACKREF1: |
| 5182 | case NFA_BACKREF2: |
| 5183 | case NFA_BACKREF3: |
| 5184 | case NFA_BACKREF4: |
| 5185 | case NFA_BACKREF5: |
| 5186 | case NFA_BACKREF6: |
| 5187 | case NFA_BACKREF7: |
| 5188 | case NFA_BACKREF8: |
| 5189 | case NFA_BACKREF9: |
| 5190 | #ifdef FEAT_SYN_HL |
| 5191 | case NFA_ZREF1: |
| 5192 | case NFA_ZREF2: |
| 5193 | case NFA_ZREF3: |
| 5194 | case NFA_ZREF4: |
| 5195 | case NFA_ZREF5: |
| 5196 | case NFA_ZREF6: |
| 5197 | case NFA_ZREF7: |
| 5198 | case NFA_ZREF8: |
| 5199 | case NFA_ZREF9: |
| 5200 | #endif |
| 5201 | /* backreferences don't match in many places */ |
| 5202 | return 94; |
| 5203 | |
| 5204 | case NFA_LNUM_GT: |
| 5205 | case NFA_LNUM_LT: |
| 5206 | case NFA_COL_GT: |
| 5207 | case NFA_COL_LT: |
| 5208 | case NFA_VCOL_GT: |
| 5209 | case NFA_VCOL_LT: |
| 5210 | case NFA_MARK_GT: |
| 5211 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5212 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5213 | /* before/after positions don't match very often */ |
| 5214 | return 85; |
| 5215 | |
| 5216 | case NFA_LNUM: |
| 5217 | return 90; |
| 5218 | |
| 5219 | case NFA_CURSOR: |
| 5220 | case NFA_COL: |
| 5221 | case NFA_VCOL: |
| 5222 | case NFA_MARK: |
| 5223 | /* specific positions rarely match */ |
| 5224 | return 98; |
| 5225 | |
| 5226 | case NFA_COMPOSING: |
| 5227 | return 95; |
| 5228 | |
| 5229 | default: |
| 5230 | if (c > 0) |
| 5231 | /* character match fails often */ |
| 5232 | return 95; |
| 5233 | } |
| 5234 | |
| 5235 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5236 | return 50; |
| 5237 | } |
| 5238 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5239 | /* |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5240 | * Skip until the char "c" we know a match must start with. |
| 5241 | */ |
| 5242 | static int |
| 5243 | skip_to_start(c, colp) |
| 5244 | int c; |
| 5245 | colnr_T *colp; |
| 5246 | { |
| 5247 | char_u *s; |
| 5248 | |
| 5249 | /* Used often, do some work to avoid call overhead. */ |
| 5250 | if (!ireg_ic |
| 5251 | #ifdef FEAT_MBYTE |
| 5252 | && !has_mbyte |
| 5253 | #endif |
| 5254 | ) |
| 5255 | s = vim_strbyte(regline + *colp, c); |
| 5256 | else |
| 5257 | s = cstrchr(regline + *colp, c); |
| 5258 | if (s == NULL) |
| 5259 | return FAIL; |
| 5260 | *colp = (int)(s - regline); |
| 5261 | return OK; |
| 5262 | } |
| 5263 | |
| 5264 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5265 | * Check for a match with match_text. |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 5266 | * Called after skip_to_start() has found regstart. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5267 | * Returns zero for no match, 1 for a match. |
| 5268 | */ |
| 5269 | static long |
| 5270 | find_match_text(startcol, regstart, match_text) |
| 5271 | colnr_T startcol; |
| 5272 | int regstart; |
| 5273 | char_u *match_text; |
| 5274 | { |
| 5275 | colnr_T col = startcol; |
| 5276 | int c1, c2; |
| 5277 | int len1, len2; |
| 5278 | int match; |
| 5279 | |
| 5280 | for (;;) |
| 5281 | { |
| 5282 | match = TRUE; |
| 5283 | len2 = MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5284 | for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
| 5285 | { |
| 5286 | c1 = PTR2CHAR(match_text + len1); |
| 5287 | c2 = PTR2CHAR(regline + col + len2); |
| 5288 | if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
| 5289 | { |
| 5290 | match = FALSE; |
| 5291 | break; |
| 5292 | } |
| 5293 | len2 += MB_CHAR2LEN(c2); |
| 5294 | } |
| 5295 | if (match |
| 5296 | #ifdef FEAT_MBYTE |
| 5297 | /* check that no composing char follows */ |
| 5298 | && !(enc_utf8 |
| 5299 | && utf_iscomposing(PTR2CHAR(regline + col + len2))) |
| 5300 | #endif |
| 5301 | ) |
| 5302 | { |
| 5303 | cleanup_subexpr(); |
| 5304 | if (REG_MULTI) |
| 5305 | { |
| 5306 | reg_startpos[0].lnum = reglnum; |
| 5307 | reg_startpos[0].col = col; |
| 5308 | reg_endpos[0].lnum = reglnum; |
| 5309 | reg_endpos[0].col = col + len2; |
| 5310 | } |
| 5311 | else |
| 5312 | { |
| 5313 | reg_startp[0] = regline + col; |
| 5314 | reg_endp[0] = regline + col + len2; |
| 5315 | } |
| 5316 | return 1L; |
| 5317 | } |
| 5318 | |
| 5319 | /* Try finding regstart after the current match. */ |
| 5320 | col += MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5321 | if (skip_to_start(regstart, &col) == FAIL) |
| 5322 | break; |
| 5323 | } |
| 5324 | return 0L; |
| 5325 | } |
| 5326 | |
| 5327 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5328 | * Main matching routine. |
| 5329 | * |
| 5330 | * Run NFA to determine whether it matches reginput. |
| 5331 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5332 | * 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] | 5333 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5334 | * Return TRUE if there is a match, FALSE otherwise. |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5335 | * When there is a match "submatch" contains the positions. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5336 | * Note: Caller must ensure that: start != NULL. |
| 5337 | */ |
| 5338 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5339 | nfa_regmatch(prog, start, submatch, m) |
| 5340 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5341 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5342 | regsubs_T *submatch; |
| 5343 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5344 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5345 | int result; |
| 5346 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5347 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5348 | int go_to_nextline = FALSE; |
| 5349 | nfa_thread_T *t; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5350 | nfa_list_T list[2]; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5351 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5352 | nfa_list_T *thislist; |
| 5353 | nfa_list_T *nextlist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5354 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5355 | nfa_state_T *add_state; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5356 | int add_here; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5357 | int add_count; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 5358 | int add_off = 0; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5359 | int toplevel = start->c == NFA_MOPEN; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5360 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5361 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5362 | |
| 5363 | if (debug == NULL) |
| 5364 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5365 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5366 | return FALSE; |
| 5367 | } |
| 5368 | #endif |
Bram Moolenaar | 41f1205 | 2013-08-25 17:01:42 +0200 | [diff] [blame] | 5369 | /* Some patterns may take a long time to match, especially when using |
| 5370 | * recursive_regmatch(). Allow interrupting them with CTRL-C. */ |
| 5371 | fast_breakcheck(); |
| 5372 | if (got_int) |
| 5373 | return FALSE; |
| 5374 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5375 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5376 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5377 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5378 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5379 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5380 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5381 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5382 | list[1].len = nstate + 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5383 | if (list[0].t == NULL || list[1].t == NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5384 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5385 | |
| 5386 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5387 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5388 | if (log_fd != NULL) |
| 5389 | { |
| 5390 | fprintf(log_fd, "**********************************\n"); |
| 5391 | nfa_set_code(start->c); |
| 5392 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 5393 | abs(start->id), code); |
| 5394 | fprintf(log_fd, "**********************************\n"); |
| 5395 | } |
| 5396 | else |
| 5397 | { |
| 5398 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5399 | log_fd = stderr; |
| 5400 | } |
| 5401 | #endif |
| 5402 | |
| 5403 | thislist = &list[0]; |
| 5404 | thislist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5405 | thislist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5406 | nextlist = &list[1]; |
| 5407 | nextlist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5408 | nextlist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5409 | #ifdef ENABLE_LOG |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5410 | fprintf(log_fd, "(---) STARTSTATE first\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5411 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5412 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5413 | |
| 5414 | /* Inline optimized code for addstate(thislist, start, m, 0) if we know |
| 5415 | * it's the first MOPEN. */ |
| 5416 | if (toplevel) |
| 5417 | { |
| 5418 | if (REG_MULTI) |
| 5419 | { |
| 5420 | m->norm.list.multi[0].start.lnum = reglnum; |
| 5421 | m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline); |
| 5422 | } |
| 5423 | else |
| 5424 | m->norm.list.line[0].start = reginput; |
| 5425 | m->norm.in_use = 1; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5426 | addstate(thislist, start->out, m, NULL, 0); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5427 | } |
| 5428 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5429 | addstate(thislist, start, m, NULL, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5430 | |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5431 | #define ADD_STATE_IF_MATCH(state) \ |
| 5432 | if (result) { \ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5433 | add_state = state->out; \ |
| 5434 | add_off = clen; \ |
| 5435 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5436 | |
| 5437 | /* |
| 5438 | * Run for each character. |
| 5439 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5440 | for (;;) |
| 5441 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5442 | int curc; |
| 5443 | int clen; |
| 5444 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5445 | #ifdef FEAT_MBYTE |
| 5446 | if (has_mbyte) |
| 5447 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5448 | curc = (*mb_ptr2char)(reginput); |
| 5449 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5450 | } |
| 5451 | else |
| 5452 | #endif |
| 5453 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5454 | curc = *reginput; |
| 5455 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5456 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5457 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5458 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5459 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5460 | go_to_nextline = FALSE; |
| 5461 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5462 | |
| 5463 | /* swap lists */ |
| 5464 | thislist = &list[flag]; |
| 5465 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5466 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5467 | nextlist->has_pim = FALSE; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5468 | ++nfa_listid; |
| 5469 | thislist->id = nfa_listid; |
| 5470 | nextlist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5471 | |
| 5472 | #ifdef ENABLE_LOG |
| 5473 | fprintf(log_fd, "------------------------------------------\n"); |
| 5474 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5475 | 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] | 5476 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5477 | { |
| 5478 | int i; |
| 5479 | |
| 5480 | for (i = 0; i < thislist->n; i++) |
| 5481 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5482 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5483 | fprintf(log_fd, "\n"); |
| 5484 | #endif |
| 5485 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5486 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5487 | fprintf(debug, "\n-------------------\n"); |
| 5488 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5489 | /* |
| 5490 | * If the state lists are empty we can stop. |
| 5491 | */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5492 | if (thislist->n == 0) |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5493 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5494 | |
| 5495 | /* compute nextlist */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5496 | for (listidx = 0; listidx < thislist->n; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5497 | { |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5498 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5499 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5500 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5501 | nfa_set_code(t->state->c); |
| 5502 | fprintf(debug, "%s, ", code); |
| 5503 | #endif |
| 5504 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5505 | { |
| 5506 | int col; |
| 5507 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5508 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5509 | col = -1; |
| 5510 | else if (REG_MULTI) |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5511 | col = t->subs.norm.list.multi[0].start.col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5512 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5513 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5514 | nfa_set_code(t->state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5515 | fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n", |
| 5516 | abs(t->state->id), (int)t->state->c, code, col, |
| 5517 | pim_info(&t->pim)); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5518 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5519 | #endif |
| 5520 | |
| 5521 | /* |
| 5522 | * Handle the possible codes of the current state. |
| 5523 | * The most important is NFA_MATCH. |
| 5524 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5525 | add_state = NULL; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5526 | add_here = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5527 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5528 | switch (t->state->c) |
| 5529 | { |
| 5530 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5531 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5532 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5533 | copy_sub(&submatch->norm, &t->subs.norm); |
| 5534 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5535 | if (nfa_has_zsubexpr) |
| 5536 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5537 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5538 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5539 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5540 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5541 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5542 | * states at this position. When the list of states is going |
| 5543 | * to be empty quit without advancing, so that "reginput" is |
| 5544 | * correct. */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5545 | if (nextlist->n == 0) |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5546 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5547 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5548 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5549 | |
| 5550 | case NFA_END_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5551 | case NFA_END_INVISIBLE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5552 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5553 | /* |
| 5554 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5555 | * NFA_START_INVISIBLE_BEFORE node. |
| 5556 | * They surround a zero-width group, used with "\@=", "\&", |
| 5557 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5558 | * If we got here, it means that the current "invisible" group |
| 5559 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5560 | * nfa_regmatch(). For a look-behind match only when it ends |
| 5561 | * in the position in "nfa_endp". |
| 5562 | * Submatches are stored in *m, and used in the parent call. |
| 5563 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5564 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5565 | if (nfa_endp != NULL) |
| 5566 | { |
| 5567 | if (REG_MULTI) |
| 5568 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 5569 | (int)reglnum, |
| 5570 | (int)nfa_endp->se_u.pos.lnum, |
| 5571 | (int)(reginput - regline), |
| 5572 | nfa_endp->se_u.pos.col); |
| 5573 | else |
| 5574 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 5575 | (int)(reginput - regline), |
| 5576 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5577 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5578 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5579 | /* If "nfa_endp" is set it's only a match if it ends at |
| 5580 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5581 | if (nfa_endp != NULL && (REG_MULTI |
| 5582 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 5583 | || (int)(reginput - regline) |
| 5584 | != nfa_endp->se_u.pos.col) |
| 5585 | : reginput != nfa_endp->se_u.ptr)) |
| 5586 | break; |
| 5587 | |
| 5588 | /* do not set submatches for \@! */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5589 | if (t->state->c != NFA_END_INVISIBLE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5590 | { |
| 5591 | copy_sub(&m->norm, &t->subs.norm); |
| 5592 | #ifdef FEAT_SYN_HL |
| 5593 | if (nfa_has_zsubexpr) |
| 5594 | copy_sub(&m->synt, &t->subs.synt); |
| 5595 | #endif |
| 5596 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5597 | #ifdef ENABLE_LOG |
| 5598 | fprintf(log_fd, "Match found:\n"); |
| 5599 | log_subsexpr(m); |
| 5600 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5601 | nfa_match = TRUE; |
Bram Moolenaar | 78c93e4 | 2013-09-05 16:05:36 +0200 | [diff] [blame] | 5602 | /* See comment above at "goto nextchar". */ |
| 5603 | if (nextlist->n == 0) |
| 5604 | clen = 0; |
| 5605 | goto nextchar; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5606 | |
| 5607 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5608 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5609 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5610 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5611 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5612 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5613 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5614 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5615 | { |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5616 | #ifdef ENABLE_LOG |
| 5617 | fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", |
| 5618 | failure_chance(t->state->out, 0), |
| 5619 | failure_chance(t->state->out1->out, 0)); |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5620 | #endif |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5621 | /* Do it directly if there already is a PIM or when |
| 5622 | * nfa_postprocess() detected it will work better. */ |
| 5623 | if (t->pim.result != NFA_PIM_UNUSED |
| 5624 | || t->state->c == NFA_START_INVISIBLE_FIRST |
| 5625 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5626 | || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 5627 | || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5628 | { |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5629 | int in_use = m->norm.in_use; |
| 5630 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5631 | /* Copy submatch info for the recursive call, opposite |
| 5632 | * of what happens on success below. */ |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5633 | copy_sub_off(&m->norm, &t->subs.norm); |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5634 | #ifdef FEAT_SYN_HL |
| 5635 | if (nfa_has_zsubexpr) |
| 5636 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5637 | #endif |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5638 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5639 | /* |
| 5640 | * First try matching the invisible match, then what |
| 5641 | * follows. |
| 5642 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5643 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5644 | submatch, m, &listids); |
| 5645 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5646 | /* for \@! and \@<! it is a match when the result is |
| 5647 | * FALSE */ |
| 5648 | if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5649 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5650 | || t->state->c |
| 5651 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5652 | || t->state->c |
| 5653 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5654 | { |
| 5655 | /* Copy submatch info from the recursive call */ |
| 5656 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5657 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5658 | if (nfa_has_zsubexpr) |
| 5659 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5660 | #endif |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5661 | /* If the pattern has \ze and it matched in the |
| 5662 | * sub pattern, use it. */ |
| 5663 | copy_ze_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5664 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5665 | /* t->state->out1 is the corresponding |
| 5666 | * END_INVISIBLE node; Add its out to the current |
| 5667 | * list (zero-width match). */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5668 | add_here = TRUE; |
| 5669 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5670 | } |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5671 | m->norm.in_use = in_use; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5672 | } |
| 5673 | else |
| 5674 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5675 | nfa_pim_T pim; |
| 5676 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5677 | /* |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5678 | * First try matching what follows. Only if a match |
| 5679 | * is found verify the invisible match matches. Add a |
| 5680 | * nfa_pim_T to the following states, it contains info |
| 5681 | * about the invisible match. |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5682 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5683 | pim.state = t->state; |
| 5684 | pim.result = NFA_PIM_TODO; |
| 5685 | pim.subs.norm.in_use = 0; |
| 5686 | #ifdef FEAT_SYN_HL |
| 5687 | pim.subs.synt.in_use = 0; |
| 5688 | #endif |
| 5689 | if (REG_MULTI) |
| 5690 | { |
| 5691 | pim.end.pos.col = (int)(reginput - regline); |
| 5692 | pim.end.pos.lnum = reglnum; |
| 5693 | } |
| 5694 | else |
| 5695 | pim.end.ptr = reginput; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5696 | |
| 5697 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 5698 | * node; Add its out to the current list (zero-width |
| 5699 | * match). */ |
| 5700 | addstate_here(thislist, t->state->out1->out, &t->subs, |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5701 | &pim, &listidx); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5702 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5703 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5704 | break; |
| 5705 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5706 | case NFA_START_PATTERN: |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5707 | { |
| 5708 | nfa_state_T *skip = NULL; |
| 5709 | #ifdef ENABLE_LOG |
| 5710 | int skip_lid = 0; |
| 5711 | #endif |
| 5712 | |
| 5713 | /* There is no point in trying to match the pattern if the |
| 5714 | * output state is not going to be added to the list. */ |
| 5715 | if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
| 5716 | { |
| 5717 | skip = t->state->out1->out; |
| 5718 | #ifdef ENABLE_LOG |
| 5719 | skip_lid = nextlist->id; |
| 5720 | #endif |
| 5721 | } |
| 5722 | else if (state_in_list(nextlist, |
| 5723 | t->state->out1->out->out, &t->subs)) |
| 5724 | { |
| 5725 | skip = t->state->out1->out->out; |
| 5726 | #ifdef ENABLE_LOG |
| 5727 | skip_lid = nextlist->id; |
| 5728 | #endif |
| 5729 | } |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5730 | else if (state_in_list(thislist, |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5731 | t->state->out1->out->out, &t->subs)) |
| 5732 | { |
| 5733 | skip = t->state->out1->out->out; |
| 5734 | #ifdef ENABLE_LOG |
| 5735 | skip_lid = thislist->id; |
| 5736 | #endif |
| 5737 | } |
| 5738 | if (skip != NULL) |
| 5739 | { |
| 5740 | #ifdef ENABLE_LOG |
| 5741 | nfa_set_code(skip->c); |
| 5742 | fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
| 5743 | abs(skip->id), skip_lid, skip->c, code); |
| 5744 | #endif |
| 5745 | break; |
| 5746 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5747 | /* Copy submatch info to the recursive call, opposite of what |
| 5748 | * happens afterwards. */ |
| 5749 | copy_sub_off(&m->norm, &t->subs.norm); |
| 5750 | #ifdef FEAT_SYN_HL |
| 5751 | if (nfa_has_zsubexpr) |
| 5752 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5753 | #endif |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5754 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5755 | /* First try matching the pattern. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5756 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5757 | submatch, m, &listids); |
| 5758 | if (result) |
| 5759 | { |
| 5760 | int bytelen; |
| 5761 | |
| 5762 | #ifdef ENABLE_LOG |
| 5763 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 5764 | log_subsexpr(m); |
| 5765 | #endif |
| 5766 | /* Copy submatch info from the recursive call */ |
| 5767 | copy_sub_off(&t->subs.norm, &m->norm); |
| 5768 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5769 | if (nfa_has_zsubexpr) |
| 5770 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5771 | #endif |
| 5772 | /* Now we need to skip over the matched text and then |
| 5773 | * continue with what follows. */ |
| 5774 | if (REG_MULTI) |
| 5775 | /* TODO: multi-line match */ |
| 5776 | bytelen = m->norm.list.multi[0].end.col |
| 5777 | - (int)(reginput - regline); |
| 5778 | else |
| 5779 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 5780 | |
| 5781 | #ifdef ENABLE_LOG |
| 5782 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 5783 | #endif |
| 5784 | if (bytelen == 0) |
| 5785 | { |
| 5786 | /* empty match, output of corresponding |
| 5787 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 5788 | * position */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5789 | add_here = TRUE; |
| 5790 | add_state = t->state->out1->out->out; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5791 | } |
| 5792 | else if (bytelen <= clen) |
| 5793 | { |
| 5794 | /* match current character, output of corresponding |
| 5795 | * NFA_END_PATTERN to be used at next position. */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5796 | add_state = t->state->out1->out->out; |
| 5797 | add_off = clen; |
| 5798 | } |
| 5799 | else |
| 5800 | { |
| 5801 | /* skip over the matched characters, set character |
| 5802 | * count in NFA_SKIP */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5803 | add_state = t->state->out1->out; |
| 5804 | add_off = bytelen; |
| 5805 | add_count = bytelen - clen; |
| 5806 | } |
| 5807 | } |
| 5808 | break; |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5809 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5810 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5811 | case NFA_BOL: |
| 5812 | if (reginput == regline) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5813 | { |
| 5814 | add_here = TRUE; |
| 5815 | add_state = t->state->out; |
| 5816 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5817 | break; |
| 5818 | |
| 5819 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5820 | if (curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5821 | { |
| 5822 | add_here = TRUE; |
| 5823 | add_state = t->state->out; |
| 5824 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5825 | break; |
| 5826 | |
| 5827 | case NFA_BOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5828 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5829 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5830 | if (curc == NUL) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5831 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5832 | #ifdef FEAT_MBYTE |
| 5833 | else if (has_mbyte) |
| 5834 | { |
| 5835 | int this_class; |
| 5836 | |
| 5837 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5838 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5839 | if (this_class <= 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5840 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5841 | else if (reg_prev_class() == this_class) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5842 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5843 | } |
| 5844 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5845 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5846 | || (reginput > regline |
| 5847 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5848 | result = FALSE; |
| 5849 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5850 | { |
| 5851 | add_here = TRUE; |
| 5852 | add_state = t->state->out; |
| 5853 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5854 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5855 | |
| 5856 | case NFA_EOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5857 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5858 | if (reginput == regline) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5859 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5860 | #ifdef FEAT_MBYTE |
| 5861 | else if (has_mbyte) |
| 5862 | { |
| 5863 | int this_class, prev_class; |
| 5864 | |
| 5865 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5866 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5867 | prev_class = reg_prev_class(); |
| 5868 | if (this_class == prev_class |
| 5869 | || prev_class == 0 || prev_class == 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5870 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5871 | } |
| 5872 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5873 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5874 | || (reginput[0] != NUL |
| 5875 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5876 | result = FALSE; |
| 5877 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5878 | { |
| 5879 | add_here = TRUE; |
| 5880 | add_state = t->state->out; |
| 5881 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5882 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5883 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5884 | case NFA_BOF: |
| 5885 | if (reglnum == 0 && reginput == regline |
| 5886 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5887 | { |
| 5888 | add_here = TRUE; |
| 5889 | add_state = t->state->out; |
| 5890 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5891 | break; |
| 5892 | |
| 5893 | case NFA_EOF: |
| 5894 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5895 | { |
| 5896 | add_here = TRUE; |
| 5897 | add_state = t->state->out; |
| 5898 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5899 | break; |
| 5900 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5901 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5902 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5903 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5904 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5905 | int len = 0; |
| 5906 | nfa_state_T *end; |
| 5907 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5908 | int cchars[MAX_MCO]; |
| 5909 | int ccount = 0; |
| 5910 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5911 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5912 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5913 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5914 | if (utf_iscomposing(sta->c)) |
| 5915 | { |
| 5916 | /* Only match composing character(s), ignore base |
| 5917 | * character. Used for ".{composing}" and "{composing}" |
| 5918 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5919 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5920 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 5921 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5922 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5923 | /* If \Z was present, then ignore composing characters. |
| 5924 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5925 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5926 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5927 | else |
| 5928 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5929 | while (sta->c != NFA_END_COMPOSING) |
| 5930 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5931 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5932 | |
| 5933 | /* Check base character matches first, unless ignored. */ |
| 5934 | else if (len > 0 || mc == sta->c) |
| 5935 | { |
| 5936 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5937 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5938 | len += mb_char2len(mc); |
| 5939 | sta = sta->out; |
| 5940 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5941 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5942 | /* We don't care about the order of composing characters. |
| 5943 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5944 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5945 | { |
| 5946 | mc = mb_ptr2char(reginput + len); |
| 5947 | cchars[ccount++] = mc; |
| 5948 | len += mb_char2len(mc); |
| 5949 | if (ccount == MAX_MCO) |
| 5950 | break; |
| 5951 | } |
| 5952 | |
| 5953 | /* Check that each composing char in the pattern matches a |
| 5954 | * composing char in the text. We do not check if all |
| 5955 | * composing chars are matched. */ |
| 5956 | result = OK; |
| 5957 | while (sta->c != NFA_END_COMPOSING) |
| 5958 | { |
| 5959 | for (j = 0; j < ccount; ++j) |
| 5960 | if (cchars[j] == sta->c) |
| 5961 | break; |
| 5962 | if (j == ccount) |
| 5963 | { |
| 5964 | result = FAIL; |
| 5965 | break; |
| 5966 | } |
| 5967 | sta = sta->out; |
| 5968 | } |
| 5969 | } |
| 5970 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 5971 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5972 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5973 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5974 | ADD_STATE_IF_MATCH(end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5975 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5976 | } |
| 5977 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5978 | |
| 5979 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5980 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 5981 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5982 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5983 | go_to_nextline = TRUE; |
| 5984 | /* Pass -1 for the offset, which means taking the position |
| 5985 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5986 | add_state = t->state->out; |
| 5987 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5988 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5989 | else if (curc == '\n' && reg_line_lbr) |
| 5990 | { |
| 5991 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5992 | add_state = t->state->out; |
| 5993 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5994 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5995 | break; |
| 5996 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 5997 | case NFA_START_COLL: |
| 5998 | case NFA_START_NEG_COLL: |
| 5999 | { |
| 6000 | /* What follows is a list of characters, until NFA_END_COLL. |
| 6001 | * One of them must match or none of them must match. */ |
| 6002 | nfa_state_T *state; |
| 6003 | int result_if_matched; |
| 6004 | int c1, c2; |
| 6005 | |
| 6006 | /* Never match EOL. If it's part of the collection it is added |
| 6007 | * as a separate state with an OR. */ |
| 6008 | if (curc == NUL) |
| 6009 | break; |
| 6010 | |
| 6011 | state = t->state->out; |
| 6012 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 6013 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6014 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6015 | if (state->c == NFA_END_COLL) |
| 6016 | { |
| 6017 | result = !result_if_matched; |
| 6018 | break; |
| 6019 | } |
| 6020 | if (state->c == NFA_RANGE_MIN) |
| 6021 | { |
| 6022 | c1 = state->val; |
| 6023 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 6024 | c2 = state->val; |
| 6025 | #ifdef ENABLE_LOG |
| 6026 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 6027 | curc, c1, c2); |
| 6028 | #endif |
| 6029 | if (curc >= c1 && curc <= c2) |
| 6030 | { |
| 6031 | result = result_if_matched; |
| 6032 | break; |
| 6033 | } |
| 6034 | if (ireg_ic) |
| 6035 | { |
| 6036 | int curc_low = MB_TOLOWER(curc); |
| 6037 | int done = FALSE; |
| 6038 | |
| 6039 | for ( ; c1 <= c2; ++c1) |
| 6040 | if (MB_TOLOWER(c1) == curc_low) |
| 6041 | { |
| 6042 | result = result_if_matched; |
| 6043 | done = TRUE; |
| 6044 | break; |
| 6045 | } |
| 6046 | if (done) |
| 6047 | break; |
| 6048 | } |
| 6049 | } |
| 6050 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 6051 | : (curc == state->c |
| 6052 | || (ireg_ic && MB_TOLOWER(curc) |
| 6053 | == MB_TOLOWER(state->c)))) |
| 6054 | { |
| 6055 | result = result_if_matched; |
| 6056 | break; |
| 6057 | } |
| 6058 | state = state->out; |
| 6059 | } |
| 6060 | if (result) |
| 6061 | { |
| 6062 | /* next state is in out of the NFA_END_COLL, out1 of |
| 6063 | * START points to the END state */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6064 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6065 | add_off = clen; |
| 6066 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6067 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6068 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6069 | |
| 6070 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6071 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6072 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6073 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6074 | add_state = t->state->out; |
| 6075 | add_off = clen; |
| 6076 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6077 | break; |
| 6078 | |
| 6079 | /* |
| 6080 | * Character classes like \a for alpha, \d for digit etc. |
| 6081 | */ |
| 6082 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6083 | result = vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6084 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6085 | break; |
| 6086 | |
| 6087 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6088 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6089 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6090 | break; |
| 6091 | |
| 6092 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 6093 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6094 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6095 | break; |
| 6096 | |
| 6097 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6098 | result = !VIM_ISDIGIT(curc) |
| 6099 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6100 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6101 | break; |
| 6102 | |
| 6103 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6104 | result = vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6105 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6106 | break; |
| 6107 | |
| 6108 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6109 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6110 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6111 | break; |
| 6112 | |
| 6113 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6114 | result = vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6115 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6116 | break; |
| 6117 | |
| 6118 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6119 | result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6120 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6121 | break; |
| 6122 | |
| 6123 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6124 | result = vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6125 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6126 | break; |
| 6127 | |
| 6128 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6129 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6130 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6131 | break; |
| 6132 | |
| 6133 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6134 | result = ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6135 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6136 | break; |
| 6137 | |
| 6138 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6139 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6140 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6141 | break; |
| 6142 | |
| 6143 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6144 | result = ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6145 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6146 | break; |
| 6147 | |
| 6148 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6149 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6150 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6151 | break; |
| 6152 | |
| 6153 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6154 | result = ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6155 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6156 | break; |
| 6157 | |
| 6158 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6159 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6160 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6161 | break; |
| 6162 | |
| 6163 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6164 | result = ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6165 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6166 | break; |
| 6167 | |
| 6168 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6169 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6170 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6171 | break; |
| 6172 | |
| 6173 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6174 | result = ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6175 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6176 | break; |
| 6177 | |
| 6178 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6179 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6180 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6181 | break; |
| 6182 | |
| 6183 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6184 | result = ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6185 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6186 | break; |
| 6187 | |
| 6188 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6189 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6190 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6191 | break; |
| 6192 | |
| 6193 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6194 | result = ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6195 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6196 | break; |
| 6197 | |
| 6198 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6199 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6200 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6201 | break; |
| 6202 | |
| 6203 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6204 | result = ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6205 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6206 | break; |
| 6207 | |
| 6208 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6209 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6210 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6211 | break; |
| 6212 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 6213 | case NFA_LOWER_IC: /* [a-z] */ |
| 6214 | result = ri_lower(curc) || (ireg_ic && ri_upper(curc)); |
| 6215 | ADD_STATE_IF_MATCH(t->state); |
| 6216 | break; |
| 6217 | |
| 6218 | case NFA_NLOWER_IC: /* [^a-z] */ |
| 6219 | result = curc != NUL |
| 6220 | && !(ri_lower(curc) || (ireg_ic && ri_upper(curc))); |
| 6221 | ADD_STATE_IF_MATCH(t->state); |
| 6222 | break; |
| 6223 | |
| 6224 | case NFA_UPPER_IC: /* [A-Z] */ |
| 6225 | result = ri_upper(curc) || (ireg_ic && ri_lower(curc)); |
| 6226 | ADD_STATE_IF_MATCH(t->state); |
| 6227 | break; |
| 6228 | |
| 6229 | case NFA_NUPPER_IC: /* ^[A-Z] */ |
| 6230 | result = curc != NUL |
| 6231 | && !(ri_upper(curc) || (ireg_ic && ri_lower(curc))); |
| 6232 | ADD_STATE_IF_MATCH(t->state); |
| 6233 | break; |
| 6234 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6235 | case NFA_BACKREF1: |
| 6236 | case NFA_BACKREF2: |
| 6237 | case NFA_BACKREF3: |
| 6238 | case NFA_BACKREF4: |
| 6239 | case NFA_BACKREF5: |
| 6240 | case NFA_BACKREF6: |
| 6241 | case NFA_BACKREF7: |
| 6242 | case NFA_BACKREF8: |
| 6243 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6244 | #ifdef FEAT_SYN_HL |
| 6245 | case NFA_ZREF1: |
| 6246 | case NFA_ZREF2: |
| 6247 | case NFA_ZREF3: |
| 6248 | case NFA_ZREF4: |
| 6249 | case NFA_ZREF5: |
| 6250 | case NFA_ZREF6: |
| 6251 | case NFA_ZREF7: |
| 6252 | case NFA_ZREF8: |
| 6253 | case NFA_ZREF9: |
| 6254 | #endif |
| 6255 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6256 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6257 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6258 | int bytelen; |
| 6259 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6260 | if (t->state->c <= NFA_BACKREF9) |
| 6261 | { |
| 6262 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 6263 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 6264 | } |
| 6265 | #ifdef FEAT_SYN_HL |
| 6266 | else |
| 6267 | { |
| 6268 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 6269 | result = match_zref(subidx, &bytelen); |
| 6270 | } |
| 6271 | #endif |
| 6272 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6273 | if (result) |
| 6274 | { |
| 6275 | if (bytelen == 0) |
| 6276 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 6277 | /* empty match always works, output of NFA_SKIP to be |
| 6278 | * used next */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6279 | add_here = TRUE; |
| 6280 | add_state = t->state->out->out; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6281 | } |
| 6282 | else if (bytelen <= clen) |
| 6283 | { |
| 6284 | /* match current character, jump ahead to out of |
| 6285 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6286 | add_state = t->state->out->out; |
| 6287 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6288 | } |
| 6289 | else |
| 6290 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 6291 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6292 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6293 | add_state = t->state->out; |
| 6294 | add_off = bytelen; |
| 6295 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6296 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6297 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6298 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6299 | } |
| 6300 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 6301 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6302 | if (t->count - clen <= 0) |
| 6303 | { |
| 6304 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6305 | add_state = t->state->out; |
| 6306 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6307 | } |
| 6308 | else |
| 6309 | { |
| 6310 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6311 | add_state = t->state; |
| 6312 | add_off = 0; |
| 6313 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6314 | } |
| 6315 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6316 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6317 | case NFA_LNUM: |
| 6318 | case NFA_LNUM_GT: |
| 6319 | case NFA_LNUM_LT: |
| 6320 | result = (REG_MULTI && |
| 6321 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 6322 | (long_u)(reglnum + reg_firstlnum))); |
| 6323 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6324 | { |
| 6325 | add_here = TRUE; |
| 6326 | add_state = t->state->out; |
| 6327 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6328 | break; |
| 6329 | |
| 6330 | case NFA_COL: |
| 6331 | case NFA_COL_GT: |
| 6332 | case NFA_COL_LT: |
| 6333 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 6334 | (long_u)(reginput - regline) + 1); |
| 6335 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6336 | { |
| 6337 | add_here = TRUE; |
| 6338 | add_state = t->state->out; |
| 6339 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6340 | break; |
| 6341 | |
| 6342 | case NFA_VCOL: |
| 6343 | case NFA_VCOL_GT: |
| 6344 | case NFA_VCOL_LT: |
| 6345 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 6346 | (long_u)win_linetabsize( |
| 6347 | reg_win == NULL ? curwin : reg_win, |
| 6348 | regline, (colnr_T)(reginput - regline)) + 1); |
| 6349 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6350 | { |
| 6351 | add_here = TRUE; |
| 6352 | add_state = t->state->out; |
| 6353 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6354 | break; |
| 6355 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6356 | case NFA_MARK: |
| 6357 | case NFA_MARK_GT: |
| 6358 | case NFA_MARK_LT: |
| 6359 | { |
| 6360 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 6361 | |
| 6362 | /* Compare the mark position to the match position. */ |
| 6363 | result = (pos != NULL /* mark doesn't exist */ |
| 6364 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 6365 | && (pos->lnum == reglnum + reg_firstlnum |
| 6366 | ? (pos->col == (colnr_T)(reginput - regline) |
| 6367 | ? t->state->c == NFA_MARK |
| 6368 | : (pos->col < (colnr_T)(reginput - regline) |
| 6369 | ? t->state->c == NFA_MARK_GT |
| 6370 | : t->state->c == NFA_MARK_LT)) |
| 6371 | : (pos->lnum < reglnum + reg_firstlnum |
| 6372 | ? t->state->c == NFA_MARK_GT |
| 6373 | : t->state->c == NFA_MARK_LT))); |
| 6374 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6375 | { |
| 6376 | add_here = TRUE; |
| 6377 | add_state = t->state->out; |
| 6378 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6379 | break; |
| 6380 | } |
| 6381 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6382 | case NFA_CURSOR: |
| 6383 | result = (reg_win != NULL |
| 6384 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 6385 | && ((colnr_T)(reginput - regline) |
| 6386 | == reg_win->w_cursor.col)); |
| 6387 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6388 | { |
| 6389 | add_here = TRUE; |
| 6390 | add_state = t->state->out; |
| 6391 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6392 | break; |
| 6393 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6394 | case NFA_VISUAL: |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 6395 | #ifdef FEAT_VISUAL |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6396 | result = reg_match_visual(); |
| 6397 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6398 | { |
| 6399 | add_here = TRUE; |
| 6400 | add_state = t->state->out; |
| 6401 | } |
Bram Moolenaar | 78eae9a | 2013-06-05 11:02:05 +0200 | [diff] [blame] | 6402 | #endif |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 6403 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6404 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6405 | case NFA_MOPEN1: |
| 6406 | case NFA_MOPEN2: |
| 6407 | case NFA_MOPEN3: |
| 6408 | case NFA_MOPEN4: |
| 6409 | case NFA_MOPEN5: |
| 6410 | case NFA_MOPEN6: |
| 6411 | case NFA_MOPEN7: |
| 6412 | case NFA_MOPEN8: |
| 6413 | case NFA_MOPEN9: |
| 6414 | #ifdef FEAT_SYN_HL |
| 6415 | case NFA_ZOPEN: |
| 6416 | case NFA_ZOPEN1: |
| 6417 | case NFA_ZOPEN2: |
| 6418 | case NFA_ZOPEN3: |
| 6419 | case NFA_ZOPEN4: |
| 6420 | case NFA_ZOPEN5: |
| 6421 | case NFA_ZOPEN6: |
| 6422 | case NFA_ZOPEN7: |
| 6423 | case NFA_ZOPEN8: |
| 6424 | case NFA_ZOPEN9: |
| 6425 | #endif |
| 6426 | case NFA_NOPEN: |
| 6427 | case NFA_ZSTART: |
| 6428 | /* These states are only added to be able to bail out when |
| 6429 | * they are added again, nothing is to be done. */ |
| 6430 | break; |
| 6431 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6432 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6433 | { |
| 6434 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6435 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6436 | #ifdef DEBUG |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6437 | if (c < 0) |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6438 | EMSGN("INTERNAL: Negative state char: %ld", c); |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6439 | #endif |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6440 | result = (c == curc); |
| 6441 | |
| 6442 | if (!result && ireg_ic) |
| 6443 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6444 | #ifdef FEAT_MBYTE |
| 6445 | /* If there is a composing character which is not being |
| 6446 | * ignored there can be no match. Match with composing |
| 6447 | * character uses NFA_COMPOSING above. */ |
| 6448 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6449 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6450 | result = FALSE; |
| 6451 | #endif |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6452 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6453 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6454 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6455 | |
| 6456 | } /* switch (t->state->c) */ |
| 6457 | |
| 6458 | if (add_state != NULL) |
| 6459 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6460 | nfa_pim_T *pim; |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6461 | nfa_pim_T pim_copy; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6462 | |
| 6463 | if (t->pim.result == NFA_PIM_UNUSED) |
| 6464 | pim = NULL; |
| 6465 | else |
| 6466 | pim = &t->pim; |
| 6467 | |
| 6468 | /* Handle the postponed invisible match if the match might end |
| 6469 | * without advancing and before the end of the line. */ |
| 6470 | if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6471 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6472 | if (pim->result == NFA_PIM_TODO) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6473 | { |
| 6474 | #ifdef ENABLE_LOG |
| 6475 | fprintf(log_fd, "\n"); |
| 6476 | fprintf(log_fd, "==================================\n"); |
| 6477 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 6478 | fprintf(log_fd, "\n"); |
| 6479 | #endif |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6480 | result = recursive_regmatch(pim->state, pim, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6481 | prog, submatch, m, &listids); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6482 | pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6483 | /* for \@! and \@<! it is a match when the result is |
| 6484 | * FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6485 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6486 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6487 | || pim->state->c |
| 6488 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6489 | || pim->state->c |
| 6490 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6491 | { |
| 6492 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6493 | copy_sub_off(&pim->subs.norm, &m->norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6494 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6495 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6496 | copy_sub_off(&pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6497 | #endif |
| 6498 | } |
| 6499 | } |
| 6500 | else |
| 6501 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6502 | result = (pim->result == NFA_PIM_MATCH); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6503 | #ifdef ENABLE_LOG |
| 6504 | fprintf(log_fd, "\n"); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6505 | 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] | 6506 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 6507 | fprintf(log_fd, "\n"); |
| 6508 | #endif |
| 6509 | } |
| 6510 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6511 | /* for \@! and \@<! it is a match when result is FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6512 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6513 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6514 | || pim->state->c |
| 6515 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6516 | || pim->state->c |
| 6517 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6518 | { |
| 6519 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6520 | copy_sub_off(&t->subs.norm, &pim->subs.norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6521 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6522 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6523 | copy_sub_off(&t->subs.synt, &pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6524 | #endif |
| 6525 | } |
| 6526 | else |
| 6527 | /* look-behind match failed, don't add the state */ |
| 6528 | continue; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6529 | |
| 6530 | /* Postponed invisible match was handled, don't add it to |
| 6531 | * following states. */ |
| 6532 | pim = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6533 | } |
| 6534 | |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6535 | /* If "pim" points into l->t it will become invalid when |
| 6536 | * adding the state causes the list to be reallocated. Make a |
| 6537 | * local copy to avoid that. */ |
| 6538 | if (pim == &t->pim) |
| 6539 | { |
| 6540 | copy_pim(&pim_copy, pim); |
| 6541 | pim = &pim_copy; |
| 6542 | } |
| 6543 | |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6544 | if (add_here) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6545 | addstate_here(thislist, add_state, &t->subs, pim, &listidx); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6546 | else |
| 6547 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6548 | addstate(nextlist, add_state, &t->subs, pim, add_off); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6549 | if (add_count > 0) |
| 6550 | nextlist->t[nextlist->n - 1].count = add_count; |
| 6551 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6552 | } |
| 6553 | |
| 6554 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 6555 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6556 | /* Look for the start of a match in the current position by adding the |
| 6557 | * start state to the list of states. |
| 6558 | * The first found match is the leftmost one, thus the order of states |
| 6559 | * matters! |
| 6560 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 6561 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6562 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6563 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6564 | if (nfa_match == FALSE |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6565 | && ((toplevel |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6566 | && reglnum == 0 |
| 6567 | && clen != 0 |
| 6568 | && (ireg_maxcol == 0 |
| 6569 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6570 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6571 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6572 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 6573 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6574 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6575 | < nfa_endp->se_u.pos.col)) |
| 6576 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6577 | { |
| 6578 | #ifdef ENABLE_LOG |
| 6579 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 6580 | #endif |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6581 | /* Inline optimized code for addstate() if we know the state is |
| 6582 | * the first MOPEN. */ |
| 6583 | if (toplevel) |
| 6584 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6585 | int add = TRUE; |
| 6586 | int c; |
| 6587 | |
| 6588 | if (prog->regstart != NUL && clen != 0) |
| 6589 | { |
| 6590 | if (nextlist->n == 0) |
| 6591 | { |
| 6592 | colnr_T col = (colnr_T)(reginput - regline) + clen; |
| 6593 | |
| 6594 | /* Nextlist is empty, we can skip ahead to the |
| 6595 | * character that must appear at the start. */ |
| 6596 | if (skip_to_start(prog->regstart, &col) == FAIL) |
| 6597 | break; |
| 6598 | #ifdef ENABLE_LOG |
| 6599 | fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
| 6600 | col - ((colnr_T)(reginput - regline) + clen)); |
| 6601 | #endif |
| 6602 | reginput = regline + col - clen; |
| 6603 | } |
| 6604 | else |
| 6605 | { |
| 6606 | /* Checking if the required start character matches is |
| 6607 | * cheaper than adding a state that won't match. */ |
| 6608 | c = PTR2CHAR(reginput + clen); |
| 6609 | if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c) |
| 6610 | != MB_TOLOWER(prog->regstart))) |
| 6611 | { |
| 6612 | #ifdef ENABLE_LOG |
| 6613 | fprintf(log_fd, " Skipping start state, regstart does not match\n"); |
| 6614 | #endif |
| 6615 | add = FALSE; |
| 6616 | } |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | if (add) |
| 6621 | { |
| 6622 | if (REG_MULTI) |
| 6623 | m->norm.list.multi[0].start.col = |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6624 | (colnr_T)(reginput - regline) + clen; |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6625 | else |
| 6626 | m->norm.list.line[0].start = reginput + clen; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6627 | addstate(nextlist, start->out, m, NULL, clen); |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6628 | } |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6629 | } |
| 6630 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6631 | addstate(nextlist, start, m, NULL, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6632 | } |
| 6633 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6634 | #ifdef ENABLE_LOG |
| 6635 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6636 | { |
| 6637 | int i; |
| 6638 | |
| 6639 | for (i = 0; i < thislist->n; i++) |
| 6640 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 6641 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6642 | fprintf(log_fd, "\n"); |
| 6643 | #endif |
| 6644 | |
| 6645 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6646 | /* Advance to the next character, or advance to the next line, or |
| 6647 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6648 | if (clen != 0) |
| 6649 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6650 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 6651 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6652 | reg_nextline(); |
| 6653 | else |
| 6654 | break; |
| 6655 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6656 | |
| 6657 | #ifdef ENABLE_LOG |
| 6658 | if (log_fd != stderr) |
| 6659 | fclose(log_fd); |
| 6660 | log_fd = NULL; |
| 6661 | #endif |
| 6662 | |
| 6663 | theend: |
| 6664 | /* Free memory */ |
| 6665 | vim_free(list[0].t); |
| 6666 | vim_free(list[1].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6667 | vim_free(listids); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6668 | #undef ADD_STATE_IF_MATCH |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 6669 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6670 | fclose(debug); |
| 6671 | #endif |
| 6672 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6673 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6674 | } |
| 6675 | |
| 6676 | /* |
| 6677 | * Try match of "prog" with at regline["col"]. |
| 6678 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6679 | */ |
| 6680 | static long |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6681 | nfa_regtry(prog, col) |
| 6682 | nfa_regprog_T *prog; |
| 6683 | colnr_T col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6684 | { |
| 6685 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6686 | regsubs_T subs, m; |
| 6687 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6688 | #ifdef ENABLE_LOG |
| 6689 | FILE *f; |
| 6690 | #endif |
| 6691 | |
| 6692 | reginput = regline + col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6693 | |
| 6694 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6695 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6696 | if (f != NULL) |
| 6697 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6698 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6699 | #ifdef DEBUG |
| 6700 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 6701 | #endif |
| 6702 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6703 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 6704 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6705 | fprintf(f, "\n\n"); |
| 6706 | fclose(f); |
| 6707 | } |
| 6708 | else |
| 6709 | EMSG(_("Could not open temporary log file for writing ")); |
| 6710 | #endif |
| 6711 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6712 | clear_sub(&subs.norm); |
| 6713 | clear_sub(&m.norm); |
| 6714 | #ifdef FEAT_SYN_HL |
| 6715 | clear_sub(&subs.synt); |
| 6716 | clear_sub(&m.synt); |
| 6717 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6718 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 6719 | if (nfa_regmatch(prog, start, &subs, &m) == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6720 | return 0; |
| 6721 | |
| 6722 | cleanup_subexpr(); |
| 6723 | if (REG_MULTI) |
| 6724 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6725 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6726 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6727 | reg_startpos[i] = subs.norm.list.multi[i].start; |
| 6728 | reg_endpos[i] = subs.norm.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6729 | } |
| 6730 | |
| 6731 | if (reg_startpos[0].lnum < 0) |
| 6732 | { |
| 6733 | reg_startpos[0].lnum = 0; |
| 6734 | reg_startpos[0].col = col; |
| 6735 | } |
| 6736 | if (reg_endpos[0].lnum < 0) |
| 6737 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 6738 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6739 | reg_endpos[0].lnum = reglnum; |
| 6740 | reg_endpos[0].col = (int)(reginput - regline); |
| 6741 | } |
| 6742 | else |
| 6743 | /* Use line number of "\ze". */ |
| 6744 | reglnum = reg_endpos[0].lnum; |
| 6745 | } |
| 6746 | else |
| 6747 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6748 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6749 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6750 | reg_startp[i] = subs.norm.list.line[i].start; |
| 6751 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | if (reg_startp[0] == NULL) |
| 6755 | reg_startp[0] = regline + col; |
| 6756 | if (reg_endp[0] == NULL) |
| 6757 | reg_endp[0] = reginput; |
| 6758 | } |
| 6759 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6760 | #ifdef FEAT_SYN_HL |
| 6761 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 6762 | unref_extmatch(re_extmatch_out); |
| 6763 | re_extmatch_out = NULL; |
| 6764 | |
| 6765 | if (prog->reghasz == REX_SET) |
| 6766 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6767 | cleanup_zsubexpr(); |
| 6768 | re_extmatch_out = make_extmatch(); |
| 6769 | for (i = 0; i < subs.synt.in_use; i++) |
| 6770 | { |
| 6771 | if (REG_MULTI) |
| 6772 | { |
| 6773 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 6774 | |
| 6775 | /* Only accept single line matches. */ |
| 6776 | if (mpos->start.lnum >= 0 && mpos->start.lnum == mpos->end.lnum) |
| 6777 | re_extmatch_out->matches[i] = |
| 6778 | vim_strnsave(reg_getline(mpos->start.lnum) |
| 6779 | + mpos->start.col, |
| 6780 | mpos->end.col - mpos->start.col); |
| 6781 | } |
| 6782 | else |
| 6783 | { |
| 6784 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 6785 | |
| 6786 | if (lpos->start != NULL && lpos->end != NULL) |
| 6787 | re_extmatch_out->matches[i] = |
| 6788 | vim_strnsave(lpos->start, |
| 6789 | (int)(lpos->end - lpos->start)); |
| 6790 | } |
| 6791 | } |
| 6792 | } |
| 6793 | #endif |
| 6794 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6795 | return 1 + reglnum; |
| 6796 | } |
| 6797 | |
| 6798 | /* |
| 6799 | * Match a regexp against a string ("line" points to the string) or multiple |
| 6800 | * lines ("line" is NULL, use reg_getline()). |
| 6801 | * |
| 6802 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6803 | */ |
| 6804 | static long |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6805 | nfa_regexec_both(line, startcol) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6806 | char_u *line; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6807 | colnr_T startcol; /* column to start looking for match */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6808 | { |
| 6809 | nfa_regprog_T *prog; |
| 6810 | long retval = 0L; |
| 6811 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6812 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6813 | |
| 6814 | if (REG_MULTI) |
| 6815 | { |
| 6816 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 6817 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 6818 | reg_startpos = reg_mmatch->startpos; |
| 6819 | reg_endpos = reg_mmatch->endpos; |
| 6820 | } |
| 6821 | else |
| 6822 | { |
| 6823 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 6824 | reg_startp = reg_match->startp; |
| 6825 | reg_endp = reg_match->endp; |
| 6826 | } |
| 6827 | |
| 6828 | /* Be paranoid... */ |
| 6829 | if (prog == NULL || line == NULL) |
| 6830 | { |
| 6831 | EMSG(_(e_null)); |
| 6832 | goto theend; |
| 6833 | } |
| 6834 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6835 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 6836 | if (prog->regflags & RF_ICASE) |
| 6837 | ireg_ic = TRUE; |
| 6838 | else if (prog->regflags & RF_NOICASE) |
| 6839 | ireg_ic = FALSE; |
| 6840 | |
| 6841 | #ifdef FEAT_MBYTE |
| 6842 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 6843 | if (prog->regflags & RF_ICOMBINE) |
| 6844 | ireg_icombine = TRUE; |
| 6845 | #endif |
| 6846 | |
| 6847 | regline = line; |
| 6848 | reglnum = 0; /* relative to line */ |
| 6849 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6850 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6851 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6852 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6853 | nfa_listid = 1; |
| 6854 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6855 | #ifdef DEBUG |
| 6856 | nfa_regengine.expr = prog->pattern; |
| 6857 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6858 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6859 | if (prog->reganch && col > 0) |
| 6860 | return 0L; |
| 6861 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6862 | need_clear_subexpr = TRUE; |
| 6863 | #ifdef FEAT_SYN_HL |
| 6864 | /* Clear the external match subpointers if necessary. */ |
| 6865 | if (prog->reghasz == REX_SET) |
| 6866 | { |
| 6867 | nfa_has_zsubexpr = TRUE; |
| 6868 | need_clear_zsubexpr = TRUE; |
| 6869 | } |
| 6870 | else |
| 6871 | nfa_has_zsubexpr = FALSE; |
| 6872 | #endif |
| 6873 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6874 | if (prog->regstart != NUL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6875 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6876 | /* Skip ahead until a character we know the match must start with. |
| 6877 | * When there is none there is no match. */ |
| 6878 | if (skip_to_start(prog->regstart, &col) == FAIL) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6879 | return 0L; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6880 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6881 | /* If match_text is set it contains the full text that must match. |
| 6882 | * Nothing else to try. Doesn't handle combining chars well. */ |
Bram Moolenaar | a940aa6 | 2013-06-08 23:30:04 +0200 | [diff] [blame] | 6883 | if (prog->match_text != NULL |
| 6884 | #ifdef FEAT_MBYTE |
| 6885 | && !ireg_icombine |
| 6886 | #endif |
| 6887 | ) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6888 | return find_match_text(col, prog->regstart, prog->match_text); |
| 6889 | } |
| 6890 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6891 | /* If the start column is past the maximum column: no need to try. */ |
| 6892 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 6893 | goto theend; |
| 6894 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6895 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6896 | for (i = 0; i < nstate; ++i) |
| 6897 | { |
| 6898 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6899 | prog->state[i].lastlist[0] = 0; |
| 6900 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6901 | } |
| 6902 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6903 | retval = nfa_regtry(prog, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6904 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6905 | #ifdef DEBUG |
| 6906 | nfa_regengine.expr = NULL; |
| 6907 | #endif |
| 6908 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6909 | theend: |
| 6910 | return retval; |
| 6911 | } |
| 6912 | |
| 6913 | /* |
| 6914 | * Compile a regular expression into internal code for the NFA matcher. |
| 6915 | * Returns the program in allocated space. Returns NULL for an error. |
| 6916 | */ |
| 6917 | static regprog_T * |
| 6918 | nfa_regcomp(expr, re_flags) |
| 6919 | char_u *expr; |
| 6920 | int re_flags; |
| 6921 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6922 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 6923 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6924 | int *postfix; |
| 6925 | |
| 6926 | if (expr == NULL) |
| 6927 | return NULL; |
| 6928 | |
| 6929 | #ifdef DEBUG |
| 6930 | nfa_regengine.expr = expr; |
| 6931 | #endif |
| 6932 | |
| 6933 | init_class_tab(); |
| 6934 | |
| 6935 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 6936 | return NULL; |
| 6937 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6938 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6939 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6940 | postfix = re2post(); |
| 6941 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6942 | { |
| 6943 | /* TODO: only give this error for debugging? */ |
| 6944 | if (post_ptr >= post_end) |
| 6945 | 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] | 6946 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6947 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6948 | |
| 6949 | /* |
| 6950 | * In order to build the NFA, we parse the input regexp twice: |
| 6951 | * 1. first pass to count size (so we can allocate space) |
| 6952 | * 2. second to emit code |
| 6953 | */ |
| 6954 | #ifdef ENABLE_LOG |
| 6955 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6956 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6957 | |
| 6958 | if (f != NULL) |
| 6959 | { |
| 6960 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 6961 | fclose(f); |
| 6962 | } |
| 6963 | } |
| 6964 | #endif |
| 6965 | |
| 6966 | /* |
| 6967 | * PASS 1 |
| 6968 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 6969 | */ |
| 6970 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6971 | |
Bram Moolenaar | 16619a2 | 2013-06-11 18:42:36 +0200 | [diff] [blame] | 6972 | /* allocate the regprog with space for the compiled regexp */ |
| 6973 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6974 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 6975 | if (prog == NULL) |
| 6976 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6977 | state_ptr = prog->state; |
| 6978 | |
| 6979 | /* |
| 6980 | * PASS 2 |
| 6981 | * Build the NFA |
| 6982 | */ |
| 6983 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 6984 | if (prog->start == NULL) |
| 6985 | goto fail; |
| 6986 | |
| 6987 | prog->regflags = regflags; |
| 6988 | prog->engine = &nfa_regengine; |
| 6989 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6990 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6991 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6992 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6993 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6994 | nfa_postprocess(prog); |
| 6995 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6996 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 6997 | prog->regstart = nfa_get_regstart(prog->start, 0); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6998 | prog->match_text = nfa_get_match_text(prog->start); |
| 6999 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7000 | #ifdef ENABLE_LOG |
| 7001 | nfa_postfix_dump(expr, OK); |
| 7002 | nfa_dump(prog); |
| 7003 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 7004 | #ifdef FEAT_SYN_HL |
| 7005 | /* Remember whether this pattern has any \z specials in it. */ |
| 7006 | prog->reghasz = re_has_z; |
| 7007 | #endif |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7008 | #ifdef DEBUG |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7009 | prog->pattern = vim_strsave(expr); |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7010 | nfa_regengine.expr = NULL; |
| 7011 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7012 | |
| 7013 | out: |
| 7014 | vim_free(post_start); |
| 7015 | post_start = post_ptr = post_end = NULL; |
| 7016 | state_ptr = NULL; |
| 7017 | return (regprog_T *)prog; |
| 7018 | |
| 7019 | fail: |
| 7020 | vim_free(prog); |
| 7021 | prog = NULL; |
| 7022 | #ifdef ENABLE_LOG |
| 7023 | nfa_postfix_dump(expr, FAIL); |
| 7024 | #endif |
| 7025 | #ifdef DEBUG |
| 7026 | nfa_regengine.expr = NULL; |
| 7027 | #endif |
| 7028 | goto out; |
| 7029 | } |
| 7030 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7031 | /* |
| 7032 | * Free a compiled regexp program, returned by nfa_regcomp(). |
| 7033 | */ |
| 7034 | static void |
| 7035 | nfa_regfree(prog) |
| 7036 | regprog_T *prog; |
| 7037 | { |
| 7038 | if (prog != NULL) |
| 7039 | { |
| 7040 | vim_free(((nfa_regprog_T *)prog)->match_text); |
| 7041 | #ifdef DEBUG |
| 7042 | vim_free(((nfa_regprog_T *)prog)->pattern); |
| 7043 | #endif |
| 7044 | vim_free(prog); |
| 7045 | } |
| 7046 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7047 | |
| 7048 | /* |
| 7049 | * Match a regexp against a string. |
| 7050 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 7051 | * Uses curbuf for line count and 'iskeyword'. |
| 7052 | * |
| 7053 | * Return TRUE if there is a match, FALSE if not. |
| 7054 | */ |
| 7055 | static int |
| 7056 | nfa_regexec(rmp, line, col) |
| 7057 | regmatch_T *rmp; |
| 7058 | char_u *line; /* string to match against */ |
| 7059 | colnr_T col; /* column to start looking for match */ |
| 7060 | { |
| 7061 | reg_match = rmp; |
| 7062 | reg_mmatch = NULL; |
| 7063 | reg_maxline = 0; |
| 7064 | reg_line_lbr = FALSE; |
| 7065 | reg_buf = curbuf; |
| 7066 | reg_win = NULL; |
| 7067 | ireg_ic = rmp->rm_ic; |
| 7068 | #ifdef FEAT_MBYTE |
| 7069 | ireg_icombine = FALSE; |
| 7070 | #endif |
| 7071 | ireg_maxcol = 0; |
| 7072 | return (nfa_regexec_both(line, col) != 0); |
| 7073 | } |
| 7074 | |
| 7075 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 7076 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 7077 | |
| 7078 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 7079 | |
| 7080 | /* |
| 7081 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 7082 | */ |
| 7083 | static int |
| 7084 | nfa_regexec_nl(rmp, line, col) |
| 7085 | regmatch_T *rmp; |
| 7086 | char_u *line; /* string to match against */ |
| 7087 | colnr_T col; /* column to start looking for match */ |
| 7088 | { |
| 7089 | reg_match = rmp; |
| 7090 | reg_mmatch = NULL; |
| 7091 | reg_maxline = 0; |
| 7092 | reg_line_lbr = TRUE; |
| 7093 | reg_buf = curbuf; |
| 7094 | reg_win = NULL; |
| 7095 | ireg_ic = rmp->rm_ic; |
| 7096 | #ifdef FEAT_MBYTE |
| 7097 | ireg_icombine = FALSE; |
| 7098 | #endif |
| 7099 | ireg_maxcol = 0; |
| 7100 | return (nfa_regexec_both(line, col) != 0); |
| 7101 | } |
| 7102 | #endif |
| 7103 | |
| 7104 | |
| 7105 | /* |
| 7106 | * Match a regexp against multiple lines. |
| 7107 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 7108 | * Uses curbuf for line count and 'iskeyword'. |
| 7109 | * |
| 7110 | * Return zero if there is no match. Return number of lines contained in the |
| 7111 | * match otherwise. |
| 7112 | * |
| 7113 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 7114 | * |
| 7115 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 7116 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 7117 | * |
| 7118 | * +-------------------------+ |
| 7119 | * |a | |
| 7120 | * |b | |
| 7121 | * |c | |
| 7122 | * | | |
| 7123 | * +-------------------------+ |
| 7124 | * |
| 7125 | * then nfa_regexec_multi() returns 3. while the original |
| 7126 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 7127 | * |
| 7128 | * FIXME if this behavior is not compatible. |
| 7129 | */ |
| 7130 | static long |
| 7131 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 7132 | regmmatch_T *rmp; |
| 7133 | win_T *win; /* window in which to search or NULL */ |
| 7134 | buf_T *buf; /* buffer in which to search */ |
| 7135 | linenr_T lnum; /* nr of line to start looking for match */ |
| 7136 | colnr_T col; /* column to start looking for match */ |
| 7137 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 7138 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7139 | reg_match = NULL; |
| 7140 | reg_mmatch = rmp; |
| 7141 | reg_buf = buf; |
| 7142 | reg_win = win; |
| 7143 | reg_firstlnum = lnum; |
| 7144 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 7145 | reg_line_lbr = FALSE; |
| 7146 | ireg_ic = rmp->rmm_ic; |
| 7147 | #ifdef FEAT_MBYTE |
| 7148 | ireg_icombine = FALSE; |
| 7149 | #endif |
| 7150 | ireg_maxcol = rmp->rmm_maxcol; |
| 7151 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 7152 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7153 | } |
| 7154 | |
| 7155 | #ifdef DEBUG |
| 7156 | # undef ENABLE_LOG |
| 7157 | #endif |