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 | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 84 | NFA_ANY_COMPOSING, /* \%C: Any composing characters. */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 85 | NFA_OPT_CHARS, /* \%[abc] */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 86 | |
| 87 | /* The following are used only in the postfix form, not in the NFA */ |
| 88 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 89 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 90 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 91 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 92 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 93 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 94 | NFA_BACKREF1, /* \1 */ |
| 95 | NFA_BACKREF2, /* \2 */ |
| 96 | NFA_BACKREF3, /* \3 */ |
| 97 | NFA_BACKREF4, /* \4 */ |
| 98 | NFA_BACKREF5, /* \5 */ |
| 99 | NFA_BACKREF6, /* \6 */ |
| 100 | NFA_BACKREF7, /* \7 */ |
| 101 | NFA_BACKREF8, /* \8 */ |
| 102 | NFA_BACKREF9, /* \9 */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 103 | #ifdef FEAT_SYN_HL |
| 104 | NFA_ZREF1, /* \z1 */ |
| 105 | NFA_ZREF2, /* \z2 */ |
| 106 | NFA_ZREF3, /* \z3 */ |
| 107 | NFA_ZREF4, /* \z4 */ |
| 108 | NFA_ZREF5, /* \z5 */ |
| 109 | NFA_ZREF6, /* \z6 */ |
| 110 | NFA_ZREF7, /* \z7 */ |
| 111 | NFA_ZREF8, /* \z8 */ |
| 112 | NFA_ZREF9, /* \z9 */ |
| 113 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 114 | NFA_SKIP, /* Skip characters */ |
| 115 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 116 | NFA_MOPEN, |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 117 | NFA_MOPEN1, |
| 118 | NFA_MOPEN2, |
| 119 | NFA_MOPEN3, |
| 120 | NFA_MOPEN4, |
| 121 | NFA_MOPEN5, |
| 122 | NFA_MOPEN6, |
| 123 | NFA_MOPEN7, |
| 124 | NFA_MOPEN8, |
| 125 | NFA_MOPEN9, |
| 126 | |
| 127 | NFA_MCLOSE, |
| 128 | NFA_MCLOSE1, |
| 129 | NFA_MCLOSE2, |
| 130 | NFA_MCLOSE3, |
| 131 | NFA_MCLOSE4, |
| 132 | NFA_MCLOSE5, |
| 133 | NFA_MCLOSE6, |
| 134 | NFA_MCLOSE7, |
| 135 | NFA_MCLOSE8, |
| 136 | NFA_MCLOSE9, |
| 137 | |
| 138 | #ifdef FEAT_SYN_HL |
| 139 | NFA_ZOPEN, |
| 140 | NFA_ZOPEN1, |
| 141 | NFA_ZOPEN2, |
| 142 | NFA_ZOPEN3, |
| 143 | NFA_ZOPEN4, |
| 144 | NFA_ZOPEN5, |
| 145 | NFA_ZOPEN6, |
| 146 | NFA_ZOPEN7, |
| 147 | NFA_ZOPEN8, |
| 148 | NFA_ZOPEN9, |
| 149 | |
| 150 | NFA_ZCLOSE, |
| 151 | NFA_ZCLOSE1, |
| 152 | NFA_ZCLOSE2, |
| 153 | NFA_ZCLOSE3, |
| 154 | NFA_ZCLOSE4, |
| 155 | NFA_ZCLOSE5, |
| 156 | NFA_ZCLOSE6, |
| 157 | NFA_ZCLOSE7, |
| 158 | NFA_ZCLOSE8, |
| 159 | NFA_ZCLOSE9, |
| 160 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 161 | |
| 162 | /* NFA_FIRST_NL */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 163 | NFA_ANY, /* Match any one character. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 164 | NFA_IDENT, /* Match identifier char */ |
| 165 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 166 | NFA_KWORD, /* Match keyword char */ |
| 167 | NFA_SKWORD, /* Match word char but no digit */ |
| 168 | NFA_FNAME, /* Match file name char */ |
| 169 | NFA_SFNAME, /* Match file name char but no digit */ |
| 170 | NFA_PRINT, /* Match printable char */ |
| 171 | NFA_SPRINT, /* Match printable char but no digit */ |
| 172 | NFA_WHITE, /* Match whitespace char */ |
| 173 | NFA_NWHITE, /* Match non-whitespace char */ |
| 174 | NFA_DIGIT, /* Match digit char */ |
| 175 | NFA_NDIGIT, /* Match non-digit char */ |
| 176 | NFA_HEX, /* Match hex char */ |
| 177 | NFA_NHEX, /* Match non-hex char */ |
| 178 | NFA_OCTAL, /* Match octal char */ |
| 179 | NFA_NOCTAL, /* Match non-octal char */ |
| 180 | NFA_WORD, /* Match word char */ |
| 181 | NFA_NWORD, /* Match non-word char */ |
| 182 | NFA_HEAD, /* Match head char */ |
| 183 | NFA_NHEAD, /* Match non-head char */ |
| 184 | NFA_ALPHA, /* Match alpha char */ |
| 185 | NFA_NALPHA, /* Match non-alpha char */ |
| 186 | NFA_LOWER, /* Match lowercase char */ |
| 187 | NFA_NLOWER, /* Match non-lowercase char */ |
| 188 | NFA_UPPER, /* Match uppercase char */ |
| 189 | NFA_NUPPER, /* Match non-uppercase char */ |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 190 | NFA_LOWER_IC, /* Match [a-z] */ |
| 191 | NFA_NLOWER_IC, /* Match [^a-z] */ |
| 192 | NFA_UPPER_IC, /* Match [A-Z] */ |
| 193 | NFA_NUPPER_IC, /* Match [^A-Z] */ |
| 194 | |
| 195 | NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL, |
| 196 | NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL, |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 197 | |
| 198 | NFA_CURSOR, /* Match cursor pos */ |
| 199 | NFA_LNUM, /* Match line number */ |
| 200 | NFA_LNUM_GT, /* Match > line number */ |
| 201 | NFA_LNUM_LT, /* Match < line number */ |
| 202 | NFA_COL, /* Match cursor column */ |
| 203 | NFA_COL_GT, /* Match > cursor column */ |
| 204 | NFA_COL_LT, /* Match < cursor column */ |
| 205 | NFA_VCOL, /* Match cursor virtual column */ |
| 206 | NFA_VCOL_GT, /* Match > cursor virtual column */ |
| 207 | NFA_VCOL_LT, /* Match < cursor virtual column */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 208 | NFA_MARK, /* Match mark */ |
| 209 | NFA_MARK_GT, /* Match > mark */ |
| 210 | NFA_MARK_LT, /* Match < mark */ |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 211 | NFA_VISUAL, /* Match Visual area */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 212 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 213 | /* Character classes [:alnum:] etc */ |
| 214 | NFA_CLASS_ALNUM, |
| 215 | NFA_CLASS_ALPHA, |
| 216 | NFA_CLASS_BLANK, |
| 217 | NFA_CLASS_CNTRL, |
| 218 | NFA_CLASS_DIGIT, |
| 219 | NFA_CLASS_GRAPH, |
| 220 | NFA_CLASS_LOWER, |
| 221 | NFA_CLASS_PRINT, |
| 222 | NFA_CLASS_PUNCT, |
| 223 | NFA_CLASS_SPACE, |
| 224 | NFA_CLASS_UPPER, |
| 225 | NFA_CLASS_XDIGIT, |
| 226 | NFA_CLASS_TAB, |
| 227 | NFA_CLASS_RETURN, |
| 228 | NFA_CLASS_BACKSPACE, |
| 229 | NFA_CLASS_ESCAPE |
| 230 | }; |
| 231 | |
| 232 | /* Keep in sync with classchars. */ |
| 233 | static int nfa_classcodes[] = { |
| 234 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 235 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 236 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 237 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 238 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 239 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 240 | NFA_UPPER, NFA_NUPPER |
| 241 | }; |
| 242 | |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 243 | static char_u e_nul_found[] = N_("E865: (NFA) Regexp end encountered prematurely"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 244 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 245 | static char_u e_ill_char_class[] = N_("E877: (NFA regexp) Invalid character class: %ld"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 246 | |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 247 | /* re_flags passed to nfa_regcomp() */ |
| 248 | static int nfa_re_flags; |
| 249 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 250 | /* NFA regexp \ze operator encountered. */ |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 251 | static int nfa_has_zend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 252 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 253 | /* NFA regexp \1 .. \9 encountered. */ |
| 254 | static int nfa_has_backref; |
| 255 | |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 256 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 257 | /* NFA regexp has \z( ), set zsubexpr. */ |
| 258 | static int nfa_has_zsubexpr; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 259 | #endif |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 260 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 261 | /* Number of sub expressions actually being used during execution. 1 if only |
| 262 | * the whole match (subexpr 0) is used. */ |
| 263 | static int nfa_nsubexpr; |
| 264 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 265 | static int *post_start; /* holds the postfix form of r.e. */ |
| 266 | static int *post_end; |
| 267 | static int *post_ptr; |
| 268 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 269 | static int nstate; /* Number of states in the NFA. Also used when |
| 270 | * executing. */ |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 271 | static int istate; /* Index in the state vector, used in alloc_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 272 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 273 | /* If not NULL match must end at this position */ |
| 274 | static save_se_T *nfa_endp = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 275 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 276 | /* listid is global, so that it increases on recursive calls to |
| 277 | * nfa_regmatch(), which means we don't have to clear the lastlist field of |
| 278 | * all the states. */ |
| 279 | static int nfa_listid; |
| 280 | static int nfa_alt_listid; |
| 281 | |
| 282 | /* 0 for first call to nfa_regmatch(), 1 for recursive call. */ |
| 283 | static int nfa_ll_index = 0; |
| 284 | |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 285 | static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 286 | static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth)); |
| 287 | static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 288 | static char_u *nfa_get_match_text __ARGS((nfa_state_T *start)); |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 289 | static int realloc_post_list __ARGS((void)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 290 | 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] | 291 | static int nfa_emit_equi_class __ARGS((int c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 292 | static int nfa_regatom __ARGS((void)); |
| 293 | static int nfa_regpiece __ARGS((void)); |
| 294 | static int nfa_regconcat __ARGS((void)); |
| 295 | static int nfa_regbranch __ARGS((void)); |
| 296 | static int nfa_reg __ARGS((int paren)); |
| 297 | #ifdef DEBUG |
| 298 | static void nfa_set_code __ARGS((int c)); |
| 299 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 300 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 301 | 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] | 302 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 303 | #endif |
| 304 | static int *re2post __ARGS((void)); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 305 | 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] | 306 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
| 307 | static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 308 | 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] | 309 | static void nfa_postprocess __ARGS((nfa_regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 310 | static int check_char_class __ARGS((int class, int c)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 311 | static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list)); |
| 312 | static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 313 | static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos)); |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 314 | static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col, proftime_T *tm)); |
| 315 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 316 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 317 | static void nfa_regfree __ARGS((regprog_T *prog)); |
Bram Moolenaar | 2af78a1 | 2014-04-23 19:06:37 +0200 | [diff] [blame] | 318 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 319 | 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] | 320 | static int match_follows __ARGS((nfa_state_T *startstate, int depth)); |
| 321 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 322 | |
| 323 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 324 | #define EMIT(c) do { \ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 325 | if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 326 | return FAIL; \ |
| 327 | *post_ptr++ = c; \ |
| 328 | } while (0) |
| 329 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 330 | /* |
| 331 | * Initialize internal variables before NFA compilation. |
| 332 | * Return OK on success, FAIL otherwise. |
| 333 | */ |
| 334 | static int |
| 335 | nfa_regcomp_start(expr, re_flags) |
| 336 | char_u *expr; |
| 337 | int re_flags; /* see vim_regcomp() */ |
| 338 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 339 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 340 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 341 | |
| 342 | nstate = 0; |
| 343 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 344 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 345 | nstate_max = (int)(STRLEN(expr) + 1) * 25; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 346 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 347 | /* 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] | 348 | * When it is still not enough realloc_post_list() will be used. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 349 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 350 | |
| 351 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 352 | postfix_size = sizeof(int) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 353 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 354 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 355 | if (post_start == NULL) |
| 356 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 357 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 358 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 359 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 360 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 361 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 362 | /* shared with BT engine */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 363 | regcomp_start(expr, re_flags); |
| 364 | |
| 365 | return OK; |
| 366 | } |
| 367 | |
| 368 | /* |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 369 | * Figure out if the NFA state list starts with an anchor, must match at start |
| 370 | * of the line. |
| 371 | */ |
| 372 | static int |
| 373 | nfa_get_reganch(start, depth) |
| 374 | nfa_state_T *start; |
| 375 | int depth; |
| 376 | { |
| 377 | nfa_state_T *p = start; |
| 378 | |
| 379 | if (depth > 4) |
| 380 | return 0; |
| 381 | |
| 382 | while (p != NULL) |
| 383 | { |
| 384 | switch (p->c) |
| 385 | { |
| 386 | case NFA_BOL: |
| 387 | case NFA_BOF: |
| 388 | return 1; /* yes! */ |
| 389 | |
| 390 | case NFA_ZSTART: |
| 391 | case NFA_ZEND: |
| 392 | case NFA_CURSOR: |
| 393 | case NFA_VISUAL: |
| 394 | |
| 395 | case NFA_MOPEN: |
| 396 | case NFA_MOPEN1: |
| 397 | case NFA_MOPEN2: |
| 398 | case NFA_MOPEN3: |
| 399 | case NFA_MOPEN4: |
| 400 | case NFA_MOPEN5: |
| 401 | case NFA_MOPEN6: |
| 402 | case NFA_MOPEN7: |
| 403 | case NFA_MOPEN8: |
| 404 | case NFA_MOPEN9: |
| 405 | case NFA_NOPEN: |
| 406 | #ifdef FEAT_SYN_HL |
| 407 | case NFA_ZOPEN: |
| 408 | case NFA_ZOPEN1: |
| 409 | case NFA_ZOPEN2: |
| 410 | case NFA_ZOPEN3: |
| 411 | case NFA_ZOPEN4: |
| 412 | case NFA_ZOPEN5: |
| 413 | case NFA_ZOPEN6: |
| 414 | case NFA_ZOPEN7: |
| 415 | case NFA_ZOPEN8: |
| 416 | case NFA_ZOPEN9: |
| 417 | #endif |
| 418 | p = p->out; |
| 419 | break; |
| 420 | |
| 421 | case NFA_SPLIT: |
| 422 | return nfa_get_reganch(p->out, depth + 1) |
| 423 | && nfa_get_reganch(p->out1, depth + 1); |
| 424 | |
| 425 | default: |
| 426 | return 0; /* noooo */ |
| 427 | } |
| 428 | } |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * Figure out if the NFA state list starts with a character which must match |
| 434 | * at start of the match. |
| 435 | */ |
| 436 | static int |
| 437 | nfa_get_regstart(start, depth) |
| 438 | nfa_state_T *start; |
| 439 | int depth; |
| 440 | { |
| 441 | nfa_state_T *p = start; |
| 442 | |
| 443 | if (depth > 4) |
| 444 | return 0; |
| 445 | |
| 446 | while (p != NULL) |
| 447 | { |
| 448 | switch (p->c) |
| 449 | { |
| 450 | /* all kinds of zero-width matches */ |
| 451 | case NFA_BOL: |
| 452 | case NFA_BOF: |
| 453 | case NFA_BOW: |
| 454 | case NFA_EOW: |
| 455 | case NFA_ZSTART: |
| 456 | case NFA_ZEND: |
| 457 | case NFA_CURSOR: |
| 458 | case NFA_VISUAL: |
| 459 | case NFA_LNUM: |
| 460 | case NFA_LNUM_GT: |
| 461 | case NFA_LNUM_LT: |
| 462 | case NFA_COL: |
| 463 | case NFA_COL_GT: |
| 464 | case NFA_COL_LT: |
| 465 | case NFA_VCOL: |
| 466 | case NFA_VCOL_GT: |
| 467 | case NFA_VCOL_LT: |
| 468 | case NFA_MARK: |
| 469 | case NFA_MARK_GT: |
| 470 | case NFA_MARK_LT: |
| 471 | |
| 472 | case NFA_MOPEN: |
| 473 | case NFA_MOPEN1: |
| 474 | case NFA_MOPEN2: |
| 475 | case NFA_MOPEN3: |
| 476 | case NFA_MOPEN4: |
| 477 | case NFA_MOPEN5: |
| 478 | case NFA_MOPEN6: |
| 479 | case NFA_MOPEN7: |
| 480 | case NFA_MOPEN8: |
| 481 | case NFA_MOPEN9: |
| 482 | case NFA_NOPEN: |
| 483 | #ifdef FEAT_SYN_HL |
| 484 | case NFA_ZOPEN: |
| 485 | case NFA_ZOPEN1: |
| 486 | case NFA_ZOPEN2: |
| 487 | case NFA_ZOPEN3: |
| 488 | case NFA_ZOPEN4: |
| 489 | case NFA_ZOPEN5: |
| 490 | case NFA_ZOPEN6: |
| 491 | case NFA_ZOPEN7: |
| 492 | case NFA_ZOPEN8: |
| 493 | case NFA_ZOPEN9: |
| 494 | #endif |
| 495 | p = p->out; |
| 496 | break; |
| 497 | |
| 498 | case NFA_SPLIT: |
| 499 | { |
| 500 | int c1 = nfa_get_regstart(p->out, depth + 1); |
| 501 | int c2 = nfa_get_regstart(p->out1, depth + 1); |
| 502 | |
| 503 | if (c1 == c2) |
| 504 | return c1; /* yes! */ |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | default: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 509 | if (p->c > 0) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 510 | return p->c; /* yes! */ |
| 511 | return 0; |
| 512 | } |
| 513 | } |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 518 | * 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] | 519 | * else. If so return a string in allocated memory with what must match after |
| 520 | * regstart. Otherwise return NULL. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 521 | */ |
| 522 | static char_u * |
| 523 | nfa_get_match_text(start) |
| 524 | nfa_state_T *start; |
| 525 | { |
| 526 | nfa_state_T *p = start; |
| 527 | int len = 0; |
| 528 | char_u *ret; |
| 529 | char_u *s; |
| 530 | |
| 531 | if (p->c != NFA_MOPEN) |
| 532 | return NULL; /* just in case */ |
| 533 | p = p->out; |
| 534 | while (p->c > 0) |
| 535 | { |
| 536 | len += MB_CHAR2LEN(p->c); |
| 537 | p = p->out; |
| 538 | } |
| 539 | if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH) |
| 540 | return NULL; |
| 541 | |
| 542 | ret = alloc(len); |
| 543 | if (ret != NULL) |
| 544 | { |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 545 | p = start->out->out; /* skip first char, it goes into regstart */ |
| 546 | s = ret; |
| 547 | while (p->c > 0) |
| 548 | { |
| 549 | #ifdef FEAT_MBYTE |
| 550 | if (has_mbyte) |
| 551 | s += (*mb_char2bytes)(p->c, s); |
| 552 | else |
| 553 | #endif |
| 554 | *s++ = p->c; |
| 555 | p = p->out; |
| 556 | } |
| 557 | *s = NUL; |
| 558 | } |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 563 | * Allocate more space for post_start. Called when |
| 564 | * running above the estimated number of states. |
| 565 | */ |
| 566 | static int |
| 567 | realloc_post_list() |
| 568 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 569 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 570 | int new_max = nstate_max + 1000; |
| 571 | int *new_start; |
| 572 | int *old_start; |
| 573 | |
| 574 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 575 | if (new_start == NULL) |
| 576 | return FAIL; |
| 577 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 578 | old_start = post_start; |
| 579 | post_start = new_start; |
| 580 | post_ptr = new_start + (post_ptr - old_start); |
| 581 | post_end = post_start + new_max; |
| 582 | vim_free(old_start); |
| 583 | return OK; |
| 584 | } |
| 585 | |
| 586 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 587 | * Search between "start" and "end" and try to recognize a |
| 588 | * character class in expanded form. For example [0-9]. |
| 589 | * On success, return the id the character class to be emitted. |
| 590 | * On failure, return 0 (=FAIL) |
| 591 | * Start points to the first char of the range, while end should point |
| 592 | * to the closing brace. |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 593 | * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may |
| 594 | * need to be interpreted as [a-zA-Z]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 595 | */ |
| 596 | static int |
| 597 | nfa_recognize_char_class(start, end, extra_newl) |
| 598 | char_u *start; |
| 599 | char_u *end; |
| 600 | int extra_newl; |
| 601 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 602 | # define CLASS_not 0x80 |
| 603 | # define CLASS_af 0x40 |
| 604 | # define CLASS_AF 0x20 |
| 605 | # define CLASS_az 0x10 |
| 606 | # define CLASS_AZ 0x08 |
| 607 | # define CLASS_o7 0x04 |
| 608 | # define CLASS_o9 0x02 |
| 609 | # define CLASS_underscore 0x01 |
| 610 | |
| 611 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 612 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 613 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 614 | |
| 615 | if (extra_newl == TRUE) |
| 616 | newl = TRUE; |
| 617 | |
| 618 | if (*end != ']') |
| 619 | return FAIL; |
| 620 | p = start; |
| 621 | if (*p == '^') |
| 622 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 623 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 624 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 625 | } |
| 626 | |
| 627 | while (p < end) |
| 628 | { |
| 629 | if (p + 2 < end && *(p + 1) == '-') |
| 630 | { |
| 631 | switch (*p) |
| 632 | { |
| 633 | case '0': |
| 634 | if (*(p + 2) == '9') |
| 635 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 636 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 637 | break; |
| 638 | } |
| 639 | else |
| 640 | if (*(p + 2) == '7') |
| 641 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 642 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 643 | break; |
| 644 | } |
| 645 | case 'a': |
| 646 | if (*(p + 2) == 'z') |
| 647 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 648 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 649 | break; |
| 650 | } |
| 651 | else |
| 652 | if (*(p + 2) == 'f') |
| 653 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 654 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 655 | break; |
| 656 | } |
| 657 | case 'A': |
| 658 | if (*(p + 2) == 'Z') |
| 659 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 660 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 661 | break; |
| 662 | } |
| 663 | else |
| 664 | if (*(p + 2) == 'F') |
| 665 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 666 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 667 | break; |
| 668 | } |
| 669 | /* FALLTHROUGH */ |
| 670 | default: |
| 671 | return FAIL; |
| 672 | } |
| 673 | p += 3; |
| 674 | } |
| 675 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 676 | { |
| 677 | newl = TRUE; |
| 678 | p += 2; |
| 679 | } |
| 680 | else if (*p == '_') |
| 681 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 682 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 683 | p ++; |
| 684 | } |
| 685 | else if (*p == '\n') |
| 686 | { |
| 687 | newl = TRUE; |
| 688 | p ++; |
| 689 | } |
| 690 | else |
| 691 | return FAIL; |
| 692 | } /* while (p < end) */ |
| 693 | |
| 694 | if (p != end) |
| 695 | return FAIL; |
| 696 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 697 | if (newl == TRUE) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 698 | extra_newl = NFA_ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 699 | |
| 700 | switch (config) |
| 701 | { |
| 702 | case CLASS_o9: |
| 703 | return extra_newl + NFA_DIGIT; |
| 704 | case CLASS_not | CLASS_o9: |
| 705 | return extra_newl + NFA_NDIGIT; |
| 706 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 707 | return extra_newl + NFA_HEX; |
| 708 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 709 | return extra_newl + NFA_NHEX; |
| 710 | case CLASS_o7: |
| 711 | return extra_newl + NFA_OCTAL; |
| 712 | case CLASS_not | CLASS_o7: |
| 713 | return extra_newl + NFA_NOCTAL; |
| 714 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 715 | return extra_newl + NFA_WORD; |
| 716 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 717 | return extra_newl + NFA_NWORD; |
| 718 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 719 | return extra_newl + NFA_HEAD; |
| 720 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 721 | return extra_newl + NFA_NHEAD; |
| 722 | case CLASS_az | CLASS_AZ: |
| 723 | return extra_newl + NFA_ALPHA; |
| 724 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 725 | return extra_newl + NFA_NALPHA; |
| 726 | case CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 727 | return extra_newl + NFA_LOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 728 | case CLASS_not | CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 729 | return extra_newl + NFA_NLOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 730 | case CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 731 | return extra_newl + NFA_UPPER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 732 | case CLASS_not | CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 733 | return extra_newl + NFA_NUPPER_IC; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 734 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 735 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* |
| 739 | * Produce the bytes for equivalence class "c". |
| 740 | * Currently only handles latin1, latin9 and utf-8. |
| 741 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 742 | * equivalent to 'a OR b OR c' |
| 743 | * |
| 744 | * NOTE! When changing this function, also update reg_equi_class() |
| 745 | */ |
| 746 | static int |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 747 | nfa_emit_equi_class(c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 748 | int c; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 749 | { |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 750 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
| 751 | #ifdef FEAT_MBYTE |
| 752 | # define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT); |
| 753 | #else |
| 754 | # define EMITMBC(c) |
| 755 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 756 | |
| 757 | #ifdef FEAT_MBYTE |
| 758 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 759 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 760 | #endif |
| 761 | { |
| 762 | switch (c) |
| 763 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 764 | case 'A': case 0300: case 0301: case 0302: |
| 765 | case 0303: case 0304: case 0305: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 766 | CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd) |
| 767 | CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2) |
| 768 | EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302); |
| 769 | EMIT2(0303); EMIT2(0304); EMIT2(0305); |
| 770 | EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104) |
| 771 | EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0) |
| 772 | EMITMBC(0x1ea2) |
| 773 | return OK; |
| 774 | |
| 775 | case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06) |
| 776 | EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 777 | return OK; |
| 778 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 779 | case 'C': case 0307: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 780 | CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c) |
| 781 | EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108) |
| 782 | EMITMBC(0x10a) EMITMBC(0x10c) |
| 783 | return OK; |
| 784 | |
| 785 | case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a) |
| 786 | CASEMBC(0x1e0e) CASEMBC(0x1e10) |
| 787 | EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a) |
| 788 | EMITMBC(0x1e0e) EMITMBC(0x1e10) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 789 | return OK; |
| 790 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 791 | case 'E': case 0310: case 0311: case 0312: case 0313: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 792 | CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118) |
| 793 | CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc) |
| 794 | EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312); |
| 795 | EMIT2(0313); |
| 796 | EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116) |
| 797 | EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba) |
| 798 | EMITMBC(0x1ebc) |
| 799 | return OK; |
| 800 | |
| 801 | case 'F': CASEMBC(0x1e1e) |
| 802 | EMIT2('F'); EMITMBC(0x1e1e) |
| 803 | return OK; |
| 804 | |
| 805 | case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120) |
| 806 | CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4) |
| 807 | CASEMBC(0x1e20) |
| 808 | EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120) |
| 809 | EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6) |
| 810 | EMITMBC(0x1f4) EMITMBC(0x1e20) |
| 811 | return OK; |
| 812 | |
| 813 | case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22) |
| 814 | CASEMBC(0x1e26) CASEMBC(0x1e28) |
| 815 | EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22) |
| 816 | EMITMBC(0x1e26) EMITMBC(0x1e28) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 817 | return OK; |
| 818 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 819 | case 'I': case 0314: case 0315: case 0316: case 0317: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 820 | CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e) |
| 821 | CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8) |
| 822 | EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316); |
| 823 | EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a) |
| 824 | EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130) |
| 825 | EMITMBC(0x1cf) EMITMBC(0x1ec8) |
| 826 | return OK; |
| 827 | |
| 828 | case 'J': CASEMBC(0x134) |
| 829 | EMIT2('J'); EMITMBC(0x134) |
| 830 | return OK; |
| 831 | |
| 832 | case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30) |
| 833 | CASEMBC(0x1e34) |
| 834 | EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30) |
| 835 | EMITMBC(0x1e34) |
| 836 | return OK; |
| 837 | |
| 838 | case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d) |
| 839 | CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a) |
| 840 | EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d) |
| 841 | EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a) |
| 842 | return OK; |
| 843 | |
| 844 | case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40) |
| 845 | EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 846 | return OK; |
| 847 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 848 | case 'N': case 0321: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 849 | CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44) |
| 850 | CASEMBC(0x1e48) |
| 851 | EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145) |
| 852 | EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 853 | return OK; |
| 854 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 855 | case 'O': case 0322: case 0323: case 0324: case 0325: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 856 | case 0326: case 0330: |
| 857 | CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0) |
| 858 | CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece) |
| 859 | EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324); |
| 860 | EMIT2(0325); EMIT2(0326); EMIT2(0330); |
| 861 | EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150) |
| 862 | EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea) |
| 863 | EMITMBC(0x1ec) EMITMBC(0x1ece) |
| 864 | return OK; |
| 865 | |
| 866 | case 'P': case 0x1e54: case 0x1e56: |
| 867 | EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56) |
| 868 | return OK; |
| 869 | |
| 870 | case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158) |
| 871 | CASEMBC(0x1e58) CASEMBC(0x1e5e) |
| 872 | EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158) |
| 873 | EMITMBC(0x1e58) EMITMBC(0x1e5e) |
| 874 | return OK; |
| 875 | |
| 876 | case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e) |
| 877 | CASEMBC(0x160) CASEMBC(0x1e60) |
| 878 | EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e) |
| 879 | EMITMBC(0x160) EMITMBC(0x1e60) |
| 880 | return OK; |
| 881 | |
| 882 | case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166) |
| 883 | CASEMBC(0x1e6a) CASEMBC(0x1e6e) |
| 884 | EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166) |
| 885 | EMITMBC(0x1e6a) EMITMBC(0x1e6e) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 886 | return OK; |
| 887 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 888 | case 'U': case 0331: case 0332: case 0333: case 0334: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 889 | CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e) |
| 890 | CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3) |
| 891 | CASEMBC(0x1ee6) |
| 892 | EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333); |
| 893 | EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a) |
| 894 | EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170) |
| 895 | EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3) |
| 896 | EMITMBC(0x1ee6) |
| 897 | return OK; |
| 898 | |
| 899 | case 'V': CASEMBC(0x1e7c) |
| 900 | EMIT2('V'); EMITMBC(0x1e7c) |
| 901 | return OK; |
| 902 | |
| 903 | case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82) |
| 904 | CASEMBC(0x1e84) CASEMBC(0x1e86) |
| 905 | EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82) |
| 906 | EMITMBC(0x1e84) EMITMBC(0x1e86) |
| 907 | return OK; |
| 908 | |
| 909 | case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c) |
| 910 | EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 911 | return OK; |
| 912 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 913 | case 'Y': case 0335: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 914 | CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2) |
| 915 | CASEMBC(0x1ef6) CASEMBC(0x1ef8) |
| 916 | EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178) |
| 917 | EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6) |
| 918 | EMITMBC(0x1ef8) |
| 919 | return OK; |
| 920 | |
| 921 | case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d) |
| 922 | CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94) |
| 923 | EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d) |
| 924 | EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 925 | return OK; |
| 926 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 927 | case 'a': case 0340: case 0341: case 0342: |
| 928 | case 0343: case 0344: case 0345: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 929 | CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce) |
| 930 | CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3) |
| 931 | EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342); |
| 932 | EMIT2(0343); EMIT2(0344); EMIT2(0345); |
| 933 | EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105) |
| 934 | EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1) |
| 935 | EMITMBC(0x1ea3) |
| 936 | return OK; |
| 937 | |
| 938 | case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07) |
| 939 | EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 940 | return OK; |
| 941 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 942 | case 'c': case 0347: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 943 | CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d) |
| 944 | EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109) |
| 945 | EMITMBC(0x10b) EMITMBC(0x10d) |
| 946 | return OK; |
| 947 | |
Bram Moolenaar | 2c61ec6 | 2015-07-10 19:16:34 +0200 | [diff] [blame] | 948 | case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b) |
| 949 | CASEMBC(0x1e0f) CASEMBC(0x1e11) |
| 950 | EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) |
| 951 | EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 952 | return OK; |
| 953 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 954 | case 'e': case 0350: case 0351: case 0352: case 0353: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 955 | CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119) |
| 956 | CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd) |
| 957 | EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352); |
| 958 | EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115) |
| 959 | EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b) |
| 960 | EMITMBC(0x1ebb) EMITMBC(0x1ebd) |
| 961 | return OK; |
| 962 | |
| 963 | case 'f': CASEMBC(0x1e1f) |
| 964 | EMIT2('f'); EMITMBC(0x1e1f) |
| 965 | return OK; |
| 966 | |
| 967 | case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121) |
| 968 | CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5) |
| 969 | CASEMBC(0x1e21) |
| 970 | EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121) |
| 971 | EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7) |
| 972 | EMITMBC(0x1f5) EMITMBC(0x1e21) |
| 973 | return OK; |
| 974 | |
| 975 | case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23) |
| 976 | CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96) |
| 977 | EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23) |
| 978 | EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 979 | return OK; |
| 980 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 981 | case 'i': case 0354: case 0355: case 0356: case 0357: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 982 | CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f) |
| 983 | CASEMBC(0x1d0) CASEMBC(0x1ec9) |
| 984 | EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356); |
| 985 | EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b) |
| 986 | EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0) |
| 987 | EMITMBC(0x1ec9) |
| 988 | return OK; |
| 989 | |
| 990 | case 'j': CASEMBC(0x135) CASEMBC(0x1f0) |
| 991 | EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0) |
| 992 | return OK; |
| 993 | |
| 994 | case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31) |
| 995 | CASEMBC(0x1e35) |
| 996 | EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31) |
| 997 | EMITMBC(0x1e35) |
| 998 | return OK; |
| 999 | |
| 1000 | case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e) |
| 1001 | CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b) |
| 1002 | EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e) |
| 1003 | EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b) |
| 1004 | return OK; |
| 1005 | |
| 1006 | case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41) |
| 1007 | EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1008 | return OK; |
| 1009 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1010 | case 'n': case 0361: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1011 | CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149) |
| 1012 | CASEMBC(0x1e45) CASEMBC(0x1e49) |
| 1013 | EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146) |
| 1014 | EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45) |
| 1015 | EMITMBC(0x1e49) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1016 | return OK; |
| 1017 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1018 | case 'o': case 0362: case 0363: case 0364: case 0365: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1019 | case 0366: case 0370: |
| 1020 | CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1) |
| 1021 | CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf) |
| 1022 | EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364); |
| 1023 | EMIT2(0365); EMIT2(0366); EMIT2(0370); |
| 1024 | EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151) |
| 1025 | EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb) |
| 1026 | EMITMBC(0x1ed) EMITMBC(0x1ecf) |
| 1027 | return OK; |
| 1028 | |
| 1029 | case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57) |
| 1030 | EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57) |
| 1031 | return OK; |
| 1032 | |
| 1033 | case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159) |
| 1034 | CASEMBC(0x1e59) CASEMBC(0x1e5f) |
| 1035 | EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159) |
| 1036 | EMITMBC(0x1e59) EMITMBC(0x1e5f) |
| 1037 | return OK; |
| 1038 | |
| 1039 | case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f) |
| 1040 | CASEMBC(0x161) CASEMBC(0x1e61) |
| 1041 | EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f) |
| 1042 | EMITMBC(0x161) EMITMBC(0x1e61) |
| 1043 | return OK; |
| 1044 | |
| 1045 | case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167) |
| 1046 | CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97) |
| 1047 | EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167) |
| 1048 | EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1049 | return OK; |
| 1050 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1051 | case 'u': case 0371: case 0372: case 0373: case 0374: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1052 | CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f) |
| 1053 | CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4) |
| 1054 | CASEMBC(0x1ee7) |
| 1055 | EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373); |
| 1056 | EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b) |
| 1057 | EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171) |
| 1058 | EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4) |
| 1059 | EMITMBC(0x1ee7) |
| 1060 | return OK; |
| 1061 | |
| 1062 | case 'v': CASEMBC(0x1e7d) |
| 1063 | EMIT2('v'); EMITMBC(0x1e7d) |
| 1064 | return OK; |
| 1065 | |
| 1066 | case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83) |
| 1067 | CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98) |
| 1068 | EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83) |
| 1069 | EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98) |
| 1070 | return OK; |
| 1071 | |
| 1072 | case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d) |
| 1073 | EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1074 | return OK; |
| 1075 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1076 | case 'y': case 0375: case 0377: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1077 | CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99) |
| 1078 | CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9) |
| 1079 | EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177) |
| 1080 | EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3) |
| 1081 | EMITMBC(0x1ef7) EMITMBC(0x1ef9) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1082 | return OK; |
| 1083 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1084 | case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e) |
| 1085 | CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) |
| 1086 | EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e) |
| 1087 | EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95) |
| 1088 | return OK; |
| 1089 | |
| 1090 | /* default: character itself */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1094 | EMIT2(c); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1095 | return OK; |
| 1096 | #undef EMIT2 |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1097 | #undef EMITMBC |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * Code to parse regular expression. |
| 1102 | * |
| 1103 | * We try to reuse parsing functions in regexp.c to |
| 1104 | * minimize surprise and keep the syntax consistent. |
| 1105 | */ |
| 1106 | |
| 1107 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1108 | * Parse the lowest level. |
| 1109 | * |
| 1110 | * An atom can be one of a long list of items. Many atoms match one character |
| 1111 | * in the text. It is often an ordinary character or a character class. |
| 1112 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 1113 | * is only for syntax highlighting. |
| 1114 | * |
| 1115 | * atom ::= ordinary-atom |
| 1116 | * or \( pattern \) |
| 1117 | * or \%( pattern \) |
| 1118 | * or \z( pattern \) |
| 1119 | */ |
| 1120 | static int |
| 1121 | nfa_regatom() |
| 1122 | { |
| 1123 | int c; |
| 1124 | int charclass; |
| 1125 | int equiclass; |
| 1126 | int collclass; |
| 1127 | int got_coll_char; |
| 1128 | char_u *p; |
| 1129 | char_u *endp; |
| 1130 | #ifdef FEAT_MBYTE |
| 1131 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1132 | #endif |
| 1133 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1134 | int emit_range; |
| 1135 | int negated; |
| 1136 | int result; |
| 1137 | int startc = -1; |
| 1138 | int endc = -1; |
| 1139 | int oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1140 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1141 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1142 | switch (c) |
| 1143 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1144 | case NUL: |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1145 | EMSG_RET_FAIL(_(e_nul_found)); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1146 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1147 | case Magic('^'): |
| 1148 | EMIT(NFA_BOL); |
| 1149 | break; |
| 1150 | |
| 1151 | case Magic('$'): |
| 1152 | EMIT(NFA_EOL); |
| 1153 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1154 | had_eol = TRUE; |
| 1155 | #endif |
| 1156 | break; |
| 1157 | |
| 1158 | case Magic('<'): |
| 1159 | EMIT(NFA_BOW); |
| 1160 | break; |
| 1161 | |
| 1162 | case Magic('>'): |
| 1163 | EMIT(NFA_EOW); |
| 1164 | break; |
| 1165 | |
| 1166 | case Magic('_'): |
| 1167 | c = no_Magic(getchr()); |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1168 | if (c == NUL) |
| 1169 | EMSG_RET_FAIL(_(e_nul_found)); |
| 1170 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1171 | if (c == '^') /* "\_^" is start-of-line */ |
| 1172 | { |
| 1173 | EMIT(NFA_BOL); |
| 1174 | break; |
| 1175 | } |
| 1176 | if (c == '$') /* "\_$" is end-of-line */ |
| 1177 | { |
| 1178 | EMIT(NFA_EOL); |
| 1179 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1180 | had_eol = TRUE; |
| 1181 | #endif |
| 1182 | break; |
| 1183 | } |
| 1184 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1185 | extra = NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1186 | |
| 1187 | /* "\_[" is collection plus newline */ |
| 1188 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1189 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1190 | |
| 1191 | /* "\_x" is character class plus newline */ |
| 1192 | /*FALLTHROUGH*/ |
| 1193 | |
| 1194 | /* |
| 1195 | * Character classes. |
| 1196 | */ |
| 1197 | case Magic('.'): |
| 1198 | case Magic('i'): |
| 1199 | case Magic('I'): |
| 1200 | case Magic('k'): |
| 1201 | case Magic('K'): |
| 1202 | case Magic('f'): |
| 1203 | case Magic('F'): |
| 1204 | case Magic('p'): |
| 1205 | case Magic('P'): |
| 1206 | case Magic('s'): |
| 1207 | case Magic('S'): |
| 1208 | case Magic('d'): |
| 1209 | case Magic('D'): |
| 1210 | case Magic('x'): |
| 1211 | case Magic('X'): |
| 1212 | case Magic('o'): |
| 1213 | case Magic('O'): |
| 1214 | case Magic('w'): |
| 1215 | case Magic('W'): |
| 1216 | case Magic('h'): |
| 1217 | case Magic('H'): |
| 1218 | case Magic('a'): |
| 1219 | case Magic('A'): |
| 1220 | case Magic('l'): |
| 1221 | case Magic('L'): |
| 1222 | case Magic('u'): |
| 1223 | case Magic('U'): |
| 1224 | p = vim_strchr(classchars, no_Magic(c)); |
| 1225 | if (p == NULL) |
| 1226 | { |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1227 | if (extra == NFA_ADD_NL) |
| 1228 | { |
| 1229 | EMSGN(_(e_ill_char_class), c); |
| 1230 | rc_did_emsg = TRUE; |
| 1231 | return FAIL; |
| 1232 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1233 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 1234 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1235 | } |
| 1236 | #ifdef FEAT_MBYTE |
| 1237 | /* When '.' is followed by a composing char ignore the dot, so that |
| 1238 | * the composing char is matched here. */ |
| 1239 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 1240 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1241 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1242 | c = getchr(); |
| 1243 | goto nfa_do_multibyte; |
| 1244 | } |
| 1245 | #endif |
| 1246 | EMIT(nfa_classcodes[p - classchars]); |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1247 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1248 | { |
| 1249 | EMIT(NFA_NEWL); |
| 1250 | EMIT(NFA_OR); |
| 1251 | regflags |= RF_HASNL; |
| 1252 | } |
| 1253 | break; |
| 1254 | |
| 1255 | case Magic('n'): |
| 1256 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1257 | /* In a string "\n" matches a newline character. */ |
| 1258 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1259 | else |
| 1260 | { |
| 1261 | /* In buffer text "\n" matches the end of a line. */ |
| 1262 | EMIT(NFA_NEWL); |
| 1263 | regflags |= RF_HASNL; |
| 1264 | } |
| 1265 | break; |
| 1266 | |
| 1267 | case Magic('('): |
| 1268 | if (nfa_reg(REG_PAREN) == FAIL) |
| 1269 | return FAIL; /* cascaded error */ |
| 1270 | break; |
| 1271 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1272 | case Magic('|'): |
| 1273 | case Magic('&'): |
| 1274 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1275 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1276 | return FAIL; |
| 1277 | |
| 1278 | case Magic('='): |
| 1279 | case Magic('?'): |
| 1280 | case Magic('+'): |
| 1281 | case Magic('@'): |
| 1282 | case Magic('*'): |
| 1283 | case Magic('{'): |
| 1284 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1285 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1286 | return FAIL; |
| 1287 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 1288 | case Magic('~'): |
| 1289 | { |
| 1290 | char_u *lp; |
| 1291 | |
| 1292 | /* Previous substitute pattern. |
| 1293 | * Generated as "\%(pattern\)". */ |
| 1294 | if (reg_prev_sub == NULL) |
| 1295 | { |
| 1296 | EMSG(_(e_nopresub)); |
| 1297 | return FAIL; |
| 1298 | } |
| 1299 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 1300 | { |
| 1301 | EMIT(PTR2CHAR(lp)); |
| 1302 | if (lp != reg_prev_sub) |
| 1303 | EMIT(NFA_CONCAT); |
| 1304 | } |
| 1305 | EMIT(NFA_NOPEN); |
| 1306 | break; |
| 1307 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1308 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 1309 | case Magic('1'): |
| 1310 | case Magic('2'): |
| 1311 | case Magic('3'): |
| 1312 | case Magic('4'): |
| 1313 | case Magic('5'): |
| 1314 | case Magic('6'): |
| 1315 | case Magic('7'): |
| 1316 | case Magic('8'): |
| 1317 | case Magic('9'): |
| 1318 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 1319 | nfa_has_backref = TRUE; |
| 1320 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1321 | |
| 1322 | case Magic('z'): |
| 1323 | c = no_Magic(getchr()); |
| 1324 | switch (c) |
| 1325 | { |
| 1326 | case 's': |
| 1327 | EMIT(NFA_ZSTART); |
Bram Moolenaar | 2d46e60 | 2014-08-29 11:56:32 +0200 | [diff] [blame] | 1328 | if (re_mult_next("\\zs") == FAIL) |
| 1329 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1330 | break; |
| 1331 | case 'e': |
| 1332 | EMIT(NFA_ZEND); |
| 1333 | nfa_has_zend = TRUE; |
Bram Moolenaar | 2d46e60 | 2014-08-29 11:56:32 +0200 | [diff] [blame] | 1334 | if (re_mult_next("\\ze") == FAIL) |
| 1335 | return FAIL; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1336 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1337 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1338 | case '1': |
| 1339 | case '2': |
| 1340 | case '3': |
| 1341 | case '4': |
| 1342 | case '5': |
| 1343 | case '6': |
| 1344 | case '7': |
| 1345 | case '8': |
| 1346 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1347 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1348 | if (reg_do_extmatch != REX_USE) |
| 1349 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1350 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1351 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1352 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1353 | re_has_z = REX_USE; |
| 1354 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1355 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1356 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1357 | if (reg_do_extmatch != REX_SET) |
| 1358 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1359 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1360 | return FAIL; /* cascaded error */ |
| 1361 | re_has_z = REX_SET; |
| 1362 | break; |
| 1363 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1364 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1365 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1366 | no_Magic(c)); |
| 1367 | return FAIL; |
| 1368 | } |
| 1369 | break; |
| 1370 | |
| 1371 | case Magic('%'): |
| 1372 | c = no_Magic(getchr()); |
| 1373 | switch (c) |
| 1374 | { |
| 1375 | /* () without a back reference */ |
| 1376 | case '(': |
| 1377 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1378 | return FAIL; |
| 1379 | EMIT(NFA_NOPEN); |
| 1380 | break; |
| 1381 | |
| 1382 | case 'd': /* %d123 decimal */ |
| 1383 | case 'o': /* %o123 octal */ |
| 1384 | case 'x': /* %xab hex 2 */ |
| 1385 | case 'u': /* %uabcd hex 4 */ |
| 1386 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1387 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1388 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1389 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1390 | switch (c) |
| 1391 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1392 | case 'd': nr = getdecchrs(); break; |
| 1393 | case 'o': nr = getoctchrs(); break; |
| 1394 | case 'x': nr = gethexchrs(2); break; |
| 1395 | case 'u': nr = gethexchrs(4); break; |
| 1396 | case 'U': nr = gethexchrs(8); break; |
| 1397 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1398 | } |
| 1399 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1400 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1401 | EMSG2_RET_FAIL( |
| 1402 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1403 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1404 | /* A NUL is stored in the text as NL */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1405 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1406 | EMIT(nr == 0 ? 0x0a : nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1407 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1408 | break; |
| 1409 | |
| 1410 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1411 | * pattern -- regardless of whether or not it makes sense. */ |
| 1412 | case '^': |
| 1413 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | |
| 1416 | case '$': |
| 1417 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1418 | break; |
| 1419 | |
| 1420 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1421 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1422 | break; |
| 1423 | |
| 1424 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1425 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1426 | break; |
| 1427 | |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 1428 | case 'C': |
| 1429 | EMIT(NFA_ANY_COMPOSING); |
| 1430 | break; |
| 1431 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1432 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1433 | { |
| 1434 | int n; |
| 1435 | |
| 1436 | /* \%[abc] */ |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1437 | for (n = 0; (c = peekchr()) != ']'; ++n) |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1438 | { |
| 1439 | if (c == NUL) |
| 1440 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1441 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1442 | /* recursive call! */ |
| 1443 | if (nfa_regatom() == FAIL) |
| 1444 | return FAIL; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1445 | } |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1446 | getchr(); /* get the ] */ |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1447 | if (n == 0) |
| 1448 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1449 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1450 | EMIT(NFA_OPT_CHARS); |
| 1451 | EMIT(n); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1452 | |
| 1453 | /* Emit as "\%(\%[abc]\)" to be able to handle |
| 1454 | * "\%[abc]*" which would cause the empty string to be |
| 1455 | * matched an unlimited number of times. NFA_NOPEN is |
| 1456 | * added only once at a position, while NFA_SPLIT is |
| 1457 | * added multiple times. This is more efficient than |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 1458 | * not allowing NFA_SPLIT multiple times, it is used |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1459 | * a lot. */ |
| 1460 | EMIT(NFA_NOPEN); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1461 | break; |
| 1462 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1463 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1464 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1465 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1466 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1467 | int cmp = c; |
| 1468 | |
| 1469 | if (c == '<' || c == '>') |
| 1470 | c = getchr(); |
| 1471 | while (VIM_ISDIGIT(c)) |
| 1472 | { |
| 1473 | n = n * 10 + (c - '0'); |
| 1474 | c = getchr(); |
| 1475 | } |
| 1476 | if (c == 'l' || c == 'c' || c == 'v') |
| 1477 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1478 | if (c == 'l') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1479 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1480 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1481 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1482 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1483 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1484 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1485 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1486 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1487 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1488 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1489 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1490 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1491 | break; |
| 1492 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1493 | else if (c == '\'' && n == 0) |
| 1494 | { |
| 1495 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1496 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1497 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1498 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1499 | break; |
| 1500 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1501 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1502 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1503 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1504 | return FAIL; |
| 1505 | } |
| 1506 | break; |
| 1507 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1508 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1509 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1510 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1511 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1512 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1513 | * Each character is produced as a regular state, using |
| 1514 | * NFA_CONCAT to bind them together. |
| 1515 | * Besides normal characters there can be: |
| 1516 | * - character classes NFA_CLASS_* |
| 1517 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1518 | */ |
| 1519 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1520 | p = regparse; |
| 1521 | endp = skip_anyof(p); |
| 1522 | if (*endp == ']') |
| 1523 | { |
| 1524 | /* |
| 1525 | * Try to reverse engineer character classes. For example, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1526 | * 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] | 1527 | * and perform the necessary substitutions in the NFA. |
| 1528 | */ |
| 1529 | result = nfa_recognize_char_class(regparse, endp, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1530 | extra == NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1531 | if (result != FAIL) |
| 1532 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1533 | if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1534 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1535 | EMIT(result - NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1536 | EMIT(NFA_NEWL); |
| 1537 | EMIT(NFA_OR); |
| 1538 | } |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1539 | else |
| 1540 | EMIT(result); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1541 | regparse = endp; |
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 | return OK; |
| 1544 | } |
| 1545 | /* |
| 1546 | * Failed to recognize a character class. Use the simple |
| 1547 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1548 | */ |
| 1549 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1550 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1551 | if (*regparse == '^') /* negated range */ |
| 1552 | { |
| 1553 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1554 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1555 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1556 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1557 | else |
| 1558 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1559 | if (*regparse == '-') |
| 1560 | { |
| 1561 | startc = '-'; |
| 1562 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1563 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1564 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1565 | } |
| 1566 | /* Emit the OR branches for each character in the [] */ |
| 1567 | emit_range = FALSE; |
| 1568 | while (regparse < endp) |
| 1569 | { |
| 1570 | oldstartc = startc; |
| 1571 | startc = -1; |
| 1572 | got_coll_char = FALSE; |
| 1573 | if (*regparse == '[') |
| 1574 | { |
| 1575 | /* Check for [: :], [= =], [. .] */ |
| 1576 | equiclass = collclass = 0; |
| 1577 | charclass = get_char_class(®parse); |
| 1578 | if (charclass == CLASS_NONE) |
| 1579 | { |
| 1580 | equiclass = get_equi_class(®parse); |
| 1581 | if (equiclass == 0) |
| 1582 | collclass = get_coll_element(®parse); |
| 1583 | } |
| 1584 | |
| 1585 | /* Character class like [:alpha:] */ |
| 1586 | if (charclass != CLASS_NONE) |
| 1587 | { |
| 1588 | switch (charclass) |
| 1589 | { |
| 1590 | case CLASS_ALNUM: |
| 1591 | EMIT(NFA_CLASS_ALNUM); |
| 1592 | break; |
| 1593 | case CLASS_ALPHA: |
| 1594 | EMIT(NFA_CLASS_ALPHA); |
| 1595 | break; |
| 1596 | case CLASS_BLANK: |
| 1597 | EMIT(NFA_CLASS_BLANK); |
| 1598 | break; |
| 1599 | case CLASS_CNTRL: |
| 1600 | EMIT(NFA_CLASS_CNTRL); |
| 1601 | break; |
| 1602 | case CLASS_DIGIT: |
| 1603 | EMIT(NFA_CLASS_DIGIT); |
| 1604 | break; |
| 1605 | case CLASS_GRAPH: |
| 1606 | EMIT(NFA_CLASS_GRAPH); |
| 1607 | break; |
| 1608 | case CLASS_LOWER: |
| 1609 | EMIT(NFA_CLASS_LOWER); |
| 1610 | break; |
| 1611 | case CLASS_PRINT: |
| 1612 | EMIT(NFA_CLASS_PRINT); |
| 1613 | break; |
| 1614 | case CLASS_PUNCT: |
| 1615 | EMIT(NFA_CLASS_PUNCT); |
| 1616 | break; |
| 1617 | case CLASS_SPACE: |
| 1618 | EMIT(NFA_CLASS_SPACE); |
| 1619 | break; |
| 1620 | case CLASS_UPPER: |
| 1621 | EMIT(NFA_CLASS_UPPER); |
| 1622 | break; |
| 1623 | case CLASS_XDIGIT: |
| 1624 | EMIT(NFA_CLASS_XDIGIT); |
| 1625 | break; |
| 1626 | case CLASS_TAB: |
| 1627 | EMIT(NFA_CLASS_TAB); |
| 1628 | break; |
| 1629 | case CLASS_RETURN: |
| 1630 | EMIT(NFA_CLASS_RETURN); |
| 1631 | break; |
| 1632 | case CLASS_BACKSPACE: |
| 1633 | EMIT(NFA_CLASS_BACKSPACE); |
| 1634 | break; |
| 1635 | case CLASS_ESCAPE: |
| 1636 | EMIT(NFA_CLASS_ESCAPE); |
| 1637 | break; |
| 1638 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1639 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1640 | continue; |
| 1641 | } |
| 1642 | /* Try equivalence class [=a=] and the like */ |
| 1643 | if (equiclass != 0) |
| 1644 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1645 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1646 | if (result == FAIL) |
| 1647 | { |
| 1648 | /* should never happen */ |
| 1649 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1650 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1651 | continue; |
| 1652 | } |
| 1653 | /* Try collating class like [. .] */ |
| 1654 | if (collclass != 0) |
| 1655 | { |
| 1656 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1657 | /* Will emit the proper atom at the end of the |
| 1658 | * while loop. */ |
| 1659 | } |
| 1660 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1661 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1662 | * start character. */ |
| 1663 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1664 | { |
| 1665 | emit_range = TRUE; |
| 1666 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1667 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1668 | continue; /* reading the end of the range */ |
| 1669 | } |
| 1670 | |
| 1671 | /* Now handle simple and escaped characters. |
| 1672 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1673 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1674 | * 'cpoptions' is not included. |
| 1675 | * Posix doesn't recognize backslash at all. |
| 1676 | */ |
| 1677 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1678 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1679 | && regparse + 1 <= endp |
| 1680 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1681 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1682 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1683 | != NULL) |
| 1684 | ) |
| 1685 | ) |
| 1686 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1687 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1688 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1689 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1690 | startc = reg_string ? NL : NFA_NEWL; |
| 1691 | else |
| 1692 | if (*regparse == 'd' |
| 1693 | || *regparse == 'o' |
| 1694 | || *regparse == 'x' |
| 1695 | || *regparse == 'u' |
| 1696 | || *regparse == 'U' |
| 1697 | ) |
| 1698 | { |
| 1699 | /* TODO(RE) This needs more testing */ |
| 1700 | startc = coll_get_char(); |
| 1701 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1702 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1703 | } |
| 1704 | else |
| 1705 | { |
| 1706 | /* \r,\t,\e,\b */ |
| 1707 | startc = backslash_trans(*regparse); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | /* Normal printable char */ |
| 1712 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1713 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1714 | |
| 1715 | /* Previous char was '-', so this char is end of range. */ |
| 1716 | if (emit_range) |
| 1717 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1718 | endc = startc; |
| 1719 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1720 | if (startc > endc) |
| 1721 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1722 | |
| 1723 | if (endc > startc + 2) |
| 1724 | { |
| 1725 | /* Emit a range instead of the sequence of |
| 1726 | * individual characters. */ |
| 1727 | if (startc == 0) |
| 1728 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1729 | EMIT(1); |
| 1730 | else |
| 1731 | --post_ptr; /* remove NFA_CONCAT */ |
| 1732 | EMIT(endc); |
| 1733 | EMIT(NFA_RANGE); |
| 1734 | EMIT(NFA_CONCAT); |
| 1735 | } |
| 1736 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1737 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1738 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1739 | || (*mb_char2len)(endc) > 1)) |
| 1740 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1741 | /* Emit the characters in the range. |
| 1742 | * "startc" was already emitted, so skip it. |
| 1743 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1744 | for (c = startc + 1; c <= endc; c++) |
| 1745 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 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 | } |
| 1750 | else |
| 1751 | #endif |
| 1752 | { |
| 1753 | #ifdef EBCDIC |
| 1754 | int alpha_only = FALSE; |
| 1755 | |
| 1756 | /* for alphabetical range skip the gaps |
| 1757 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1758 | if (isalpha(startc) && isalpha(endc)) |
| 1759 | alpha_only = TRUE; |
| 1760 | #endif |
| 1761 | /* Emit the range. "startc" was already emitted, so |
| 1762 | * skip it. */ |
| 1763 | for (c = startc + 1; c <= endc; c++) |
| 1764 | #ifdef EBCDIC |
| 1765 | if (!alpha_only || isalpha(startc)) |
| 1766 | #endif |
| 1767 | { |
| 1768 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1769 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1770 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1771 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1772 | emit_range = FALSE; |
| 1773 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1774 | } |
| 1775 | else |
| 1776 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1777 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1778 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1779 | * Normally, simply emit startc. But if we get char |
| 1780 | * code=0 from a collating char, then replace it with |
| 1781 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1782 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1783 | * the backtracking engine. */ |
| 1784 | if (startc == NFA_NEWL) |
| 1785 | { |
| 1786 | /* Line break can't be matched as part of the |
| 1787 | * collection, add an OR below. But not for negated |
| 1788 | * range. */ |
| 1789 | if (!negated) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1790 | extra = NFA_ADD_NL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1791 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1792 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1793 | { |
| 1794 | if (got_coll_char == TRUE && startc == 0) |
| 1795 | EMIT(0x0a); |
| 1796 | else |
| 1797 | EMIT(startc); |
| 1798 | EMIT(NFA_CONCAT); |
| 1799 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1800 | } |
| 1801 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1802 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1803 | } /* while (p < endp) */ |
| 1804 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1805 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1806 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1807 | { |
| 1808 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1809 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1810 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1811 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1812 | /* skip the trailing ] */ |
| 1813 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1814 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1815 | |
| 1816 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1817 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1818 | EMIT(NFA_END_NEG_COLL); |
| 1819 | else |
| 1820 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1821 | |
| 1822 | /* \_[] also matches \n but it's not negated */ |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1823 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1824 | { |
| 1825 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1826 | EMIT(NFA_OR); |
| 1827 | } |
| 1828 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1829 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1830 | } /* if exists closing ] */ |
| 1831 | |
| 1832 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1833 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1834 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1835 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1836 | default: |
| 1837 | { |
| 1838 | #ifdef FEAT_MBYTE |
| 1839 | int plen; |
| 1840 | |
| 1841 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1842 | /* plen is length of current char with composing chars */ |
| 1843 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1844 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1845 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1846 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1847 | int i = 0; |
| 1848 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1849 | /* A base character plus composing characters, or just one |
| 1850 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1851 | * This requires creating a separate atom as if enclosing |
| 1852 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1853 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1854 | * building the postfix form, not the NFA itself; |
| 1855 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1856 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1857 | for (;;) |
| 1858 | { |
| 1859 | EMIT(c); |
| 1860 | if (i > 0) |
| 1861 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1862 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1863 | break; |
| 1864 | c = utf_ptr2char(old_regparse + i); |
| 1865 | } |
| 1866 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1867 | regparse = old_regparse + plen; |
| 1868 | } |
| 1869 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1870 | #endif |
| 1871 | { |
| 1872 | c = no_Magic(c); |
| 1873 | EMIT(c); |
| 1874 | } |
| 1875 | return OK; |
| 1876 | } |
| 1877 | } |
| 1878 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1879 | return OK; |
| 1880 | } |
| 1881 | |
| 1882 | /* |
| 1883 | * Parse something followed by possible [*+=]. |
| 1884 | * |
| 1885 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1886 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1887 | * characters: "", "a", "aa", etc. |
| 1888 | * |
| 1889 | * piece ::= atom |
| 1890 | * or atom multi |
| 1891 | */ |
| 1892 | static int |
| 1893 | nfa_regpiece() |
| 1894 | { |
| 1895 | int i; |
| 1896 | int op; |
| 1897 | int ret; |
| 1898 | long minval, maxval; |
| 1899 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1900 | parse_state_T old_state; |
| 1901 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1902 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1903 | int old_post_pos; |
| 1904 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1905 | int quest; |
| 1906 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1907 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1908 | * next. */ |
| 1909 | save_parse_state(&old_state); |
| 1910 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1911 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1912 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1913 | |
| 1914 | ret = nfa_regatom(); |
| 1915 | if (ret == FAIL) |
| 1916 | return FAIL; /* cascaded error */ |
| 1917 | |
| 1918 | op = peekchr(); |
| 1919 | if (re_multi_type(op) == NOT_MULTI) |
| 1920 | return OK; |
| 1921 | |
| 1922 | skipchr(); |
| 1923 | switch (op) |
| 1924 | { |
| 1925 | case Magic('*'): |
| 1926 | EMIT(NFA_STAR); |
| 1927 | break; |
| 1928 | |
| 1929 | case Magic('+'): |
| 1930 | /* |
| 1931 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1932 | * first and only submatch would be "aaa". But the backtracking |
| 1933 | * engine interprets the plus as "try matching one more time", and |
| 1934 | * a* matches a second time at the end of the input, the empty |
| 1935 | * string. |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1936 | * The submatch will be the empty string. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1937 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1938 | * In order to be consistent with the old engine, we replace |
| 1939 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1940 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1941 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1942 | curchr = -1; |
| 1943 | if (nfa_regatom() == FAIL) |
| 1944 | return FAIL; |
| 1945 | EMIT(NFA_STAR); |
| 1946 | EMIT(NFA_CONCAT); |
| 1947 | skipchr(); /* skip the \+ */ |
| 1948 | break; |
| 1949 | |
| 1950 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1951 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1952 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1953 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1954 | switch(op) |
| 1955 | { |
| 1956 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1957 | /* \@= */ |
| 1958 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1959 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1960 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1961 | /* \@! */ |
| 1962 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1963 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1964 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1965 | op = no_Magic(getchr()); |
| 1966 | if (op == '=') |
| 1967 | /* \@<= */ |
| 1968 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1969 | else if (op == '!') |
| 1970 | /* \@<! */ |
| 1971 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1972 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1973 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1974 | /* \@> */ |
| 1975 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1976 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1977 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1978 | if (i == 0) |
| 1979 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1980 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1981 | return FAIL; |
| 1982 | } |
| 1983 | EMIT(i); |
| 1984 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1985 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1986 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1987 | break; |
| 1988 | |
| 1989 | case Magic('?'): |
| 1990 | case Magic('='): |
| 1991 | EMIT(NFA_QUEST); |
| 1992 | break; |
| 1993 | |
| 1994 | case Magic('{'): |
| 1995 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1996 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1997 | * version of '?' |
| 1998 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1999 | * parenthesis have the same id |
| 2000 | */ |
| 2001 | |
| 2002 | greedy = TRUE; |
| 2003 | c2 = peekchr(); |
| 2004 | if (c2 == '-' || c2 == Magic('-')) |
| 2005 | { |
| 2006 | skipchr(); |
| 2007 | greedy = FALSE; |
| 2008 | } |
| 2009 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2010 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 2011 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2012 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 2013 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2014 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2015 | { |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2016 | if (greedy) /* { { (match the braces) */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2017 | /* \{}, \{0,} */ |
| 2018 | EMIT(NFA_STAR); |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2019 | else /* { { (match the braces) */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2020 | /* \{-}, \{-0,} */ |
| 2021 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2022 | break; |
| 2023 | } |
| 2024 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2025 | /* Special case: x{0} or x{-0} */ |
| 2026 | if (maxval == 0) |
| 2027 | { |
| 2028 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2029 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2030 | /* NFA_EMPTY is 0-length and works everywhere */ |
| 2031 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2032 | return OK; |
| 2033 | } |
| 2034 | |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2035 | /* The engine is very inefficient (uses too many states) when the |
Bram Moolenaar | a1d2c58 | 2015-02-10 18:18:17 +0100 | [diff] [blame] | 2036 | * maximum is much larger than the minimum and when the maximum is |
| 2037 | * large. Bail out if we can use the other engine. */ |
| 2038 | if ((nfa_re_flags & RE_AUTO) |
| 2039 | && (maxval > minval + 200 || maxval > 500)) |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2040 | return FAIL; |
| 2041 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2042 | /* Ignore previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2043 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2044 | /* Save parse state after the repeated atom and the \{} */ |
| 2045 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2046 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2047 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 2048 | for (i = 0; i < maxval; i++) |
| 2049 | { |
| 2050 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2051 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2052 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2053 | if (nfa_regatom() == FAIL) |
| 2054 | return FAIL; |
| 2055 | /* after "minval" times, atoms are optional */ |
| 2056 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2057 | { |
| 2058 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2059 | { |
| 2060 | if (greedy) |
| 2061 | EMIT(NFA_STAR); |
| 2062 | else |
| 2063 | EMIT(NFA_STAR_NONGREEDY); |
| 2064 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2065 | else |
| 2066 | EMIT(quest); |
| 2067 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2068 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2069 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2070 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 2071 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2075 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2076 | curchr = -1; |
| 2077 | |
| 2078 | break; |
| 2079 | |
| 2080 | |
| 2081 | default: |
| 2082 | break; |
| 2083 | } /* end switch */ |
| 2084 | |
| 2085 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2086 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2087 | 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] | 2088 | |
| 2089 | return OK; |
| 2090 | } |
| 2091 | |
| 2092 | /* |
| 2093 | * Parse one or more pieces, concatenated. It matches a match for the |
| 2094 | * first piece, followed by a match for the second piece, etc. Example: |
| 2095 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 2096 | * |
| 2097 | * concat ::= piece |
| 2098 | * or piece piece |
| 2099 | * or piece piece piece |
| 2100 | * etc. |
| 2101 | */ |
| 2102 | static int |
| 2103 | nfa_regconcat() |
| 2104 | { |
| 2105 | int cont = TRUE; |
| 2106 | int first = TRUE; |
| 2107 | |
| 2108 | while (cont) |
| 2109 | { |
| 2110 | switch (peekchr()) |
| 2111 | { |
| 2112 | case NUL: |
| 2113 | case Magic('|'): |
| 2114 | case Magic('&'): |
| 2115 | case Magic(')'): |
| 2116 | cont = FALSE; |
| 2117 | break; |
| 2118 | |
| 2119 | case Magic('Z'): |
| 2120 | #ifdef FEAT_MBYTE |
| 2121 | regflags |= RF_ICOMBINE; |
| 2122 | #endif |
| 2123 | skipchr_keepstart(); |
| 2124 | break; |
| 2125 | case Magic('c'): |
| 2126 | regflags |= RF_ICASE; |
| 2127 | skipchr_keepstart(); |
| 2128 | break; |
| 2129 | case Magic('C'): |
| 2130 | regflags |= RF_NOICASE; |
| 2131 | skipchr_keepstart(); |
| 2132 | break; |
| 2133 | case Magic('v'): |
| 2134 | reg_magic = MAGIC_ALL; |
| 2135 | skipchr_keepstart(); |
| 2136 | curchr = -1; |
| 2137 | break; |
| 2138 | case Magic('m'): |
| 2139 | reg_magic = MAGIC_ON; |
| 2140 | skipchr_keepstart(); |
| 2141 | curchr = -1; |
| 2142 | break; |
| 2143 | case Magic('M'): |
| 2144 | reg_magic = MAGIC_OFF; |
| 2145 | skipchr_keepstart(); |
| 2146 | curchr = -1; |
| 2147 | break; |
| 2148 | case Magic('V'): |
| 2149 | reg_magic = MAGIC_NONE; |
| 2150 | skipchr_keepstart(); |
| 2151 | curchr = -1; |
| 2152 | break; |
| 2153 | |
| 2154 | default: |
| 2155 | if (nfa_regpiece() == FAIL) |
| 2156 | return FAIL; |
| 2157 | if (first == FALSE) |
| 2158 | EMIT(NFA_CONCAT); |
| 2159 | else |
| 2160 | first = FALSE; |
| 2161 | break; |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | return OK; |
| 2166 | } |
| 2167 | |
| 2168 | /* |
| 2169 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 2170 | * last concat, but only if all the preceding concats also match at the same |
| 2171 | * position. Examples: |
| 2172 | * "foobeep\&..." matches "foo" in "foobeep". |
| 2173 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 2174 | * |
| 2175 | * branch ::= concat |
| 2176 | * or concat \& concat |
| 2177 | * or concat \& concat \& concat |
| 2178 | * etc. |
| 2179 | */ |
| 2180 | static int |
| 2181 | nfa_regbranch() |
| 2182 | { |
| 2183 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2184 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2185 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2186 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2187 | |
| 2188 | /* First branch, possibly the only one */ |
| 2189 | if (nfa_regconcat() == FAIL) |
| 2190 | return FAIL; |
| 2191 | |
| 2192 | ch = peekchr(); |
| 2193 | /* Try next concats */ |
| 2194 | while (ch == Magic('&')) |
| 2195 | { |
| 2196 | skipchr(); |
| 2197 | EMIT(NFA_NOPEN); |
| 2198 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2199 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2200 | if (nfa_regconcat() == FAIL) |
| 2201 | return FAIL; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2202 | /* if concat is empty do emit a node */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2203 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2204 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2205 | EMIT(NFA_CONCAT); |
| 2206 | ch = peekchr(); |
| 2207 | } |
| 2208 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2209 | /* if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2210 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2211 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2212 | |
| 2213 | return OK; |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 2218 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 2219 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 2220 | * is used. |
| 2221 | * |
| 2222 | * pattern ::= branch |
| 2223 | * or branch \| branch |
| 2224 | * or branch \| branch \| branch |
| 2225 | * etc. |
| 2226 | */ |
| 2227 | static int |
| 2228 | nfa_reg(paren) |
| 2229 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 2230 | { |
| 2231 | int parno = 0; |
| 2232 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2233 | if (paren == REG_PAREN) |
| 2234 | { |
| 2235 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2236 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2237 | parno = regnpar++; |
| 2238 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2239 | #ifdef FEAT_SYN_HL |
| 2240 | else if (paren == REG_ZPAREN) |
| 2241 | { |
| 2242 | /* Make a ZOPEN node. */ |
| 2243 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2244 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2245 | parno = regnzpar++; |
| 2246 | } |
| 2247 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2248 | |
| 2249 | if (nfa_regbranch() == FAIL) |
| 2250 | return FAIL; /* cascaded error */ |
| 2251 | |
| 2252 | while (peekchr() == Magic('|')) |
| 2253 | { |
| 2254 | skipchr(); |
| 2255 | if (nfa_regbranch() == FAIL) |
| 2256 | return FAIL; /* cascaded error */ |
| 2257 | EMIT(NFA_OR); |
| 2258 | } |
| 2259 | |
| 2260 | /* Check for proper termination. */ |
| 2261 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 2262 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2263 | if (paren == REG_NPAREN) |
| 2264 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 2265 | else |
| 2266 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 2267 | } |
| 2268 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 2269 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2270 | if (peekchr() == Magic(')')) |
| 2271 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 2272 | else |
| 2273 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 2274 | } |
| 2275 | /* |
| 2276 | * Here we set the flag allowing back references to this set of |
| 2277 | * parentheses. |
| 2278 | */ |
| 2279 | if (paren == REG_PAREN) |
| 2280 | { |
| 2281 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 2282 | EMIT(NFA_MOPEN + parno); |
| 2283 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2284 | #ifdef FEAT_SYN_HL |
| 2285 | else if (paren == REG_ZPAREN) |
| 2286 | EMIT(NFA_ZOPEN + parno); |
| 2287 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2288 | |
| 2289 | return OK; |
| 2290 | } |
| 2291 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2292 | #ifdef DEBUG |
| 2293 | static char_u code[50]; |
| 2294 | |
| 2295 | static void |
| 2296 | nfa_set_code(c) |
| 2297 | int c; |
| 2298 | { |
| 2299 | int addnl = FALSE; |
| 2300 | |
| 2301 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 2302 | { |
| 2303 | addnl = TRUE; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2304 | c -= NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2305 | } |
| 2306 | |
| 2307 | STRCPY(code, ""); |
| 2308 | switch (c) |
| 2309 | { |
| 2310 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 2311 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 2312 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 2313 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 2314 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 2315 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 2316 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2317 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 2318 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 2319 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 2320 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 2321 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 2322 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 2323 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 2324 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 2325 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2326 | #ifdef FEAT_SYN_HL |
| 2327 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 2328 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 2329 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 2330 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 2331 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 2332 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 2333 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 2334 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 2335 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 2336 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2337 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 2338 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2339 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2340 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2341 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 2342 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2343 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2344 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 2345 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 2346 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2347 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 2348 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 2349 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2350 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 2351 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2352 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2353 | case NFA_START_INVISIBLE_FIRST: |
| 2354 | STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2355 | case NFA_START_INVISIBLE_NEG: |
| 2356 | STRCPY(code, "NFA_START_INVISIBLE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2357 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 2358 | STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2359 | case NFA_START_INVISIBLE_BEFORE: |
| 2360 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2361 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 2362 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2363 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2364 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2365 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 2366 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2367 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2368 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2369 | case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2370 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2371 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2372 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2373 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2374 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2375 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2376 | case NFA_MOPEN: |
| 2377 | case NFA_MOPEN1: |
| 2378 | case NFA_MOPEN2: |
| 2379 | case NFA_MOPEN3: |
| 2380 | case NFA_MOPEN4: |
| 2381 | case NFA_MOPEN5: |
| 2382 | case NFA_MOPEN6: |
| 2383 | case NFA_MOPEN7: |
| 2384 | case NFA_MOPEN8: |
| 2385 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2386 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2387 | code[10] = c - NFA_MOPEN + '0'; |
| 2388 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2389 | case NFA_MCLOSE: |
| 2390 | case NFA_MCLOSE1: |
| 2391 | case NFA_MCLOSE2: |
| 2392 | case NFA_MCLOSE3: |
| 2393 | case NFA_MCLOSE4: |
| 2394 | case NFA_MCLOSE5: |
| 2395 | case NFA_MCLOSE6: |
| 2396 | case NFA_MCLOSE7: |
| 2397 | case NFA_MCLOSE8: |
| 2398 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2399 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2400 | code[11] = c - NFA_MCLOSE + '0'; |
| 2401 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2402 | #ifdef FEAT_SYN_HL |
| 2403 | case NFA_ZOPEN: |
| 2404 | case NFA_ZOPEN1: |
| 2405 | case NFA_ZOPEN2: |
| 2406 | case NFA_ZOPEN3: |
| 2407 | case NFA_ZOPEN4: |
| 2408 | case NFA_ZOPEN5: |
| 2409 | case NFA_ZOPEN6: |
| 2410 | case NFA_ZOPEN7: |
| 2411 | case NFA_ZOPEN8: |
| 2412 | case NFA_ZOPEN9: |
| 2413 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2414 | code[10] = c - NFA_ZOPEN + '0'; |
| 2415 | break; |
| 2416 | case NFA_ZCLOSE: |
| 2417 | case NFA_ZCLOSE1: |
| 2418 | case NFA_ZCLOSE2: |
| 2419 | case NFA_ZCLOSE3: |
| 2420 | case NFA_ZCLOSE4: |
| 2421 | case NFA_ZCLOSE5: |
| 2422 | case NFA_ZCLOSE6: |
| 2423 | case NFA_ZCLOSE7: |
| 2424 | case NFA_ZCLOSE8: |
| 2425 | case NFA_ZCLOSE9: |
| 2426 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2427 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2428 | break; |
| 2429 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2430 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2431 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2432 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2433 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2434 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2435 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2436 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2437 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2438 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2439 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2440 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2441 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2442 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2443 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2444 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2445 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2446 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2447 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2448 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2449 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 2450 | case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2451 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2452 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2453 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2454 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2455 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2456 | case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2457 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2458 | |
| 2459 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2460 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2461 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2462 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2463 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2464 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2465 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2466 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2467 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2468 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2469 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2470 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2471 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2472 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2473 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2474 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2475 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2476 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2477 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2478 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2479 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2480 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2481 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2482 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2483 | |
| 2484 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2485 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2486 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2487 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2488 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2489 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2490 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2491 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2492 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2493 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2494 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2495 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2496 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2497 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2498 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2499 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2500 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2501 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2502 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2503 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2504 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2505 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2506 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2507 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2508 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2509 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2510 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2511 | case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break; |
| 2512 | case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break; |
| 2513 | case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break; |
| 2514 | case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2515 | |
| 2516 | default: |
| 2517 | STRCPY(code, "CHAR(x)"); |
| 2518 | code[5] = c; |
| 2519 | } |
| 2520 | |
| 2521 | if (addnl == TRUE) |
| 2522 | STRCAT(code, " + NEWLINE "); |
| 2523 | |
| 2524 | } |
| 2525 | |
| 2526 | #ifdef ENABLE_LOG |
| 2527 | static FILE *log_fd; |
| 2528 | |
| 2529 | /* |
| 2530 | * Print the postfix notation of the current regexp. |
| 2531 | */ |
| 2532 | static void |
| 2533 | nfa_postfix_dump(expr, retval) |
| 2534 | char_u *expr; |
| 2535 | int retval; |
| 2536 | { |
| 2537 | int *p; |
| 2538 | FILE *f; |
| 2539 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2540 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2541 | if (f != NULL) |
| 2542 | { |
| 2543 | fprintf(f, "\n-------------------------\n"); |
| 2544 | if (retval == FAIL) |
| 2545 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2546 | else if (retval == OK) |
| 2547 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2548 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2549 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2550 | { |
| 2551 | nfa_set_code(*p); |
| 2552 | fprintf(f, "%s, ", code); |
| 2553 | } |
| 2554 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2555 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2556 | fprintf(f, "%d ", *p); |
| 2557 | fprintf(f, "\n\n"); |
| 2558 | fclose(f); |
| 2559 | } |
| 2560 | } |
| 2561 | |
| 2562 | /* |
| 2563 | * Print the NFA starting with a root node "state". |
| 2564 | */ |
| 2565 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2566 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2567 | FILE *debugf; |
| 2568 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2569 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2570 | garray_T indent; |
| 2571 | |
| 2572 | ga_init2(&indent, 1, 64); |
| 2573 | ga_append(&indent, '\0'); |
| 2574 | nfa_print_state2(debugf, state, &indent); |
| 2575 | ga_clear(&indent); |
| 2576 | } |
| 2577 | |
| 2578 | static void |
| 2579 | nfa_print_state2(debugf, state, indent) |
| 2580 | FILE *debugf; |
| 2581 | nfa_state_T *state; |
| 2582 | garray_T *indent; |
| 2583 | { |
| 2584 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2585 | |
| 2586 | if (state == NULL) |
| 2587 | return; |
| 2588 | |
| 2589 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2590 | |
| 2591 | /* Output indent */ |
| 2592 | p = (char_u *)indent->ga_data; |
| 2593 | if (indent->ga_len >= 3) |
| 2594 | { |
| 2595 | int last = indent->ga_len - 3; |
| 2596 | char_u save[2]; |
| 2597 | |
| 2598 | STRNCPY(save, &p[last], 2); |
| 2599 | STRNCPY(&p[last], "+-", 2); |
| 2600 | fprintf(debugf, " %s", p); |
| 2601 | STRNCPY(&p[last], save, 2); |
| 2602 | } |
| 2603 | else |
| 2604 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2605 | |
| 2606 | nfa_set_code(state->c); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2607 | fprintf(debugf, "%s (%d) (id=%d) val=%d\n", |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2608 | code, |
| 2609 | state->c, |
| 2610 | abs(state->id), |
| 2611 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2612 | if (state->id < 0) |
| 2613 | return; |
| 2614 | |
| 2615 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2616 | |
| 2617 | /* grow indent for state->out */ |
| 2618 | indent->ga_len -= 1; |
| 2619 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2620 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2621 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2622 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2623 | ga_append(indent, '\0'); |
| 2624 | |
| 2625 | nfa_print_state2(debugf, state->out, indent); |
| 2626 | |
| 2627 | /* replace last part of indent for state->out1 */ |
| 2628 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2629 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2630 | ga_append(indent, '\0'); |
| 2631 | |
| 2632 | nfa_print_state2(debugf, state->out1, indent); |
| 2633 | |
| 2634 | /* shrink indent */ |
| 2635 | indent->ga_len -= 3; |
| 2636 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2637 | } |
| 2638 | |
| 2639 | /* |
| 2640 | * Print the NFA state machine. |
| 2641 | */ |
| 2642 | static void |
| 2643 | nfa_dump(prog) |
| 2644 | nfa_regprog_T *prog; |
| 2645 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2646 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2647 | |
| 2648 | if (debugf != NULL) |
| 2649 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2650 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2651 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 2652 | if (prog->reganch) |
| 2653 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2654 | if (prog->regstart != NUL) |
| 2655 | fprintf(debugf, "regstart: %c (decimal: %d)\n", |
| 2656 | prog->regstart, prog->regstart); |
| 2657 | if (prog->match_text != NULL) |
| 2658 | fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2659 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2660 | fclose(debugf); |
| 2661 | } |
| 2662 | } |
| 2663 | #endif /* ENABLE_LOG */ |
| 2664 | #endif /* DEBUG */ |
| 2665 | |
| 2666 | /* |
| 2667 | * Parse r.e. @expr and convert it into postfix form. |
| 2668 | * Return the postfix string on success, NULL otherwise. |
| 2669 | */ |
| 2670 | static int * |
| 2671 | re2post() |
| 2672 | { |
| 2673 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2674 | return NULL; |
| 2675 | EMIT(NFA_MOPEN); |
| 2676 | return post_start; |
| 2677 | } |
| 2678 | |
| 2679 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2680 | |
| 2681 | /* |
| 2682 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2683 | * if c == MATCH, no arrows out; matching state. |
| 2684 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2685 | * If c < 256, labeled arrow with character c to out. |
| 2686 | */ |
| 2687 | |
| 2688 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2689 | |
| 2690 | /* |
| 2691 | * Allocate and initialize nfa_state_T. |
| 2692 | */ |
| 2693 | static nfa_state_T * |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2694 | alloc_state(c, out, out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2695 | int c; |
| 2696 | nfa_state_T *out; |
| 2697 | nfa_state_T *out1; |
| 2698 | { |
| 2699 | nfa_state_T *s; |
| 2700 | |
| 2701 | if (istate >= nstate) |
| 2702 | return NULL; |
| 2703 | |
| 2704 | s = &state_ptr[istate++]; |
| 2705 | |
| 2706 | s->c = c; |
| 2707 | s->out = out; |
| 2708 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2709 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2710 | |
| 2711 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2712 | s->lastlist[0] = 0; |
| 2713 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2714 | |
| 2715 | return s; |
| 2716 | } |
| 2717 | |
| 2718 | /* |
| 2719 | * A partially built NFA without the matching state filled in. |
| 2720 | * Frag_T.start points at the start state. |
| 2721 | * Frag_T.out is a list of places that need to be set to the |
| 2722 | * next state for this fragment. |
| 2723 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2724 | |
| 2725 | /* Since the out pointers in the list are always |
| 2726 | * uninitialized, we use the pointers themselves |
| 2727 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2728 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2729 | union Ptrlist |
| 2730 | { |
| 2731 | Ptrlist *next; |
| 2732 | nfa_state_T *s; |
| 2733 | }; |
| 2734 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2735 | struct Frag |
| 2736 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2737 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2738 | Ptrlist *out; |
| 2739 | }; |
| 2740 | typedef struct Frag Frag_T; |
| 2741 | |
| 2742 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2743 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2744 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2745 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2746 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2747 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2748 | |
| 2749 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2750 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2751 | */ |
| 2752 | static Frag_T |
| 2753 | frag(start, out) |
| 2754 | nfa_state_T *start; |
| 2755 | Ptrlist *out; |
| 2756 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2757 | Frag_T n; |
| 2758 | |
| 2759 | n.start = start; |
| 2760 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2761 | return n; |
| 2762 | } |
| 2763 | |
| 2764 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2765 | * Create singleton list containing just outp. |
| 2766 | */ |
| 2767 | static Ptrlist * |
| 2768 | list1(outp) |
| 2769 | nfa_state_T **outp; |
| 2770 | { |
| 2771 | Ptrlist *l; |
| 2772 | |
| 2773 | l = (Ptrlist *)outp; |
| 2774 | l->next = NULL; |
| 2775 | return l; |
| 2776 | } |
| 2777 | |
| 2778 | /* |
| 2779 | * Patch the list of states at out to point to start. |
| 2780 | */ |
| 2781 | static void |
| 2782 | patch(l, s) |
| 2783 | Ptrlist *l; |
| 2784 | nfa_state_T *s; |
| 2785 | { |
| 2786 | Ptrlist *next; |
| 2787 | |
| 2788 | for (; l; l = next) |
| 2789 | { |
| 2790 | next = l->next; |
| 2791 | l->s = s; |
| 2792 | } |
| 2793 | } |
| 2794 | |
| 2795 | |
| 2796 | /* |
| 2797 | * Join the two lists l1 and l2, returning the combination. |
| 2798 | */ |
| 2799 | static Ptrlist * |
| 2800 | append(l1, l2) |
| 2801 | Ptrlist *l1; |
| 2802 | Ptrlist *l2; |
| 2803 | { |
| 2804 | Ptrlist *oldl1; |
| 2805 | |
| 2806 | oldl1 = l1; |
| 2807 | while (l1->next) |
| 2808 | l1 = l1->next; |
| 2809 | l1->next = l2; |
| 2810 | return oldl1; |
| 2811 | } |
| 2812 | |
| 2813 | /* |
| 2814 | * Stack used for transforming postfix form into NFA. |
| 2815 | */ |
| 2816 | static Frag_T empty; |
| 2817 | |
| 2818 | static void |
| 2819 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2820 | int *postfix UNUSED; |
| 2821 | int *end UNUSED; |
| 2822 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2823 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2824 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2825 | FILE *df; |
| 2826 | int *p2; |
| 2827 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2828 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2829 | if (df) |
| 2830 | { |
| 2831 | fprintf(df, "Error popping the stack!\n"); |
| 2832 | #ifdef DEBUG |
| 2833 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2834 | #endif |
| 2835 | fprintf(df, "Postfix form is: "); |
| 2836 | #ifdef DEBUG |
| 2837 | for (p2 = postfix; p2 < end; p2++) |
| 2838 | { |
| 2839 | nfa_set_code(*p2); |
| 2840 | fprintf(df, "%s, ", code); |
| 2841 | } |
| 2842 | nfa_set_code(*p); |
| 2843 | fprintf(df, "\nCurrent position is: "); |
| 2844 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2845 | { |
| 2846 | nfa_set_code(*p2); |
| 2847 | fprintf(df, "%s, ", code); |
| 2848 | } |
| 2849 | #else |
| 2850 | for (p2 = postfix; p2 < end; p2++) |
| 2851 | { |
| 2852 | fprintf(df, "%d, ", *p2); |
| 2853 | } |
| 2854 | fprintf(df, "\nCurrent position is: "); |
| 2855 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2856 | { |
| 2857 | fprintf(df, "%d, ", *p2); |
| 2858 | } |
| 2859 | #endif |
| 2860 | fprintf(df, "\n--------------------------\n"); |
| 2861 | fclose(df); |
| 2862 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2863 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2864 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2865 | } |
| 2866 | |
| 2867 | /* |
| 2868 | * Push an item onto the stack. |
| 2869 | */ |
| 2870 | static void |
| 2871 | st_push(s, p, stack_end) |
| 2872 | Frag_T s; |
| 2873 | Frag_T **p; |
| 2874 | Frag_T *stack_end; |
| 2875 | { |
| 2876 | Frag_T *stackp = *p; |
| 2877 | |
| 2878 | if (stackp >= stack_end) |
| 2879 | return; |
| 2880 | *stackp = s; |
| 2881 | *p = *p + 1; |
| 2882 | } |
| 2883 | |
| 2884 | /* |
| 2885 | * Pop an item from the stack. |
| 2886 | */ |
| 2887 | static Frag_T |
| 2888 | st_pop(p, stack) |
| 2889 | Frag_T **p; |
| 2890 | Frag_T *stack; |
| 2891 | { |
| 2892 | Frag_T *stackp; |
| 2893 | |
| 2894 | *p = *p - 1; |
| 2895 | stackp = *p; |
| 2896 | if (stackp < stack) |
| 2897 | return empty; |
| 2898 | return **p; |
| 2899 | } |
| 2900 | |
| 2901 | /* |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2902 | * Estimate the maximum byte length of anything matching "state". |
| 2903 | * When unknown or unlimited return -1. |
| 2904 | */ |
| 2905 | static int |
| 2906 | nfa_max_width(startstate, depth) |
| 2907 | nfa_state_T *startstate; |
| 2908 | int depth; |
| 2909 | { |
| 2910 | int l, r; |
| 2911 | nfa_state_T *state = startstate; |
| 2912 | int len = 0; |
| 2913 | |
| 2914 | /* detect looping in a NFA_SPLIT */ |
| 2915 | if (depth > 4) |
| 2916 | return -1; |
| 2917 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 2918 | while (state != NULL) |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2919 | { |
| 2920 | switch (state->c) |
| 2921 | { |
| 2922 | case NFA_END_INVISIBLE: |
| 2923 | case NFA_END_INVISIBLE_NEG: |
| 2924 | /* the end, return what we have */ |
| 2925 | return len; |
| 2926 | |
| 2927 | case NFA_SPLIT: |
| 2928 | /* two alternatives, use the maximum */ |
| 2929 | l = nfa_max_width(state->out, depth + 1); |
| 2930 | r = nfa_max_width(state->out1, depth + 1); |
| 2931 | if (l < 0 || r < 0) |
| 2932 | return -1; |
| 2933 | return len + (l > r ? l : r); |
| 2934 | |
| 2935 | case NFA_ANY: |
| 2936 | case NFA_START_COLL: |
| 2937 | case NFA_START_NEG_COLL: |
| 2938 | /* matches some character, including composing chars */ |
| 2939 | #ifdef FEAT_MBYTE |
| 2940 | if (enc_utf8) |
| 2941 | len += MB_MAXBYTES; |
| 2942 | else if (has_mbyte) |
| 2943 | len += 2; |
| 2944 | else |
| 2945 | #endif |
| 2946 | ++len; |
| 2947 | if (state->c != NFA_ANY) |
| 2948 | { |
| 2949 | /* skip over the characters */ |
| 2950 | state = state->out1->out; |
| 2951 | continue; |
| 2952 | } |
| 2953 | break; |
| 2954 | |
| 2955 | case NFA_DIGIT: |
| 2956 | case NFA_WHITE: |
| 2957 | case NFA_HEX: |
| 2958 | case NFA_OCTAL: |
| 2959 | /* ascii */ |
| 2960 | ++len; |
| 2961 | break; |
| 2962 | |
| 2963 | case NFA_IDENT: |
| 2964 | case NFA_SIDENT: |
| 2965 | case NFA_KWORD: |
| 2966 | case NFA_SKWORD: |
| 2967 | case NFA_FNAME: |
| 2968 | case NFA_SFNAME: |
| 2969 | case NFA_PRINT: |
| 2970 | case NFA_SPRINT: |
| 2971 | case NFA_NWHITE: |
| 2972 | case NFA_NDIGIT: |
| 2973 | case NFA_NHEX: |
| 2974 | case NFA_NOCTAL: |
| 2975 | case NFA_WORD: |
| 2976 | case NFA_NWORD: |
| 2977 | case NFA_HEAD: |
| 2978 | case NFA_NHEAD: |
| 2979 | case NFA_ALPHA: |
| 2980 | case NFA_NALPHA: |
| 2981 | case NFA_LOWER: |
| 2982 | case NFA_NLOWER: |
| 2983 | case NFA_UPPER: |
| 2984 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2985 | case NFA_LOWER_IC: |
| 2986 | case NFA_NLOWER_IC: |
| 2987 | case NFA_UPPER_IC: |
| 2988 | case NFA_NUPPER_IC: |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 2989 | case NFA_ANY_COMPOSING: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2990 | /* possibly non-ascii */ |
| 2991 | #ifdef FEAT_MBYTE |
| 2992 | if (has_mbyte) |
| 2993 | len += 3; |
| 2994 | else |
| 2995 | #endif |
| 2996 | ++len; |
| 2997 | break; |
| 2998 | |
| 2999 | case NFA_START_INVISIBLE: |
| 3000 | case NFA_START_INVISIBLE_NEG: |
| 3001 | case NFA_START_INVISIBLE_BEFORE: |
| 3002 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 3003 | /* zero-width, out1 points to the END state */ |
| 3004 | state = state->out1->out; |
| 3005 | continue; |
| 3006 | |
| 3007 | case NFA_BACKREF1: |
| 3008 | case NFA_BACKREF2: |
| 3009 | case NFA_BACKREF3: |
| 3010 | case NFA_BACKREF4: |
| 3011 | case NFA_BACKREF5: |
| 3012 | case NFA_BACKREF6: |
| 3013 | case NFA_BACKREF7: |
| 3014 | case NFA_BACKREF8: |
| 3015 | case NFA_BACKREF9: |
| 3016 | #ifdef FEAT_SYN_HL |
| 3017 | case NFA_ZREF1: |
| 3018 | case NFA_ZREF2: |
| 3019 | case NFA_ZREF3: |
| 3020 | case NFA_ZREF4: |
| 3021 | case NFA_ZREF5: |
| 3022 | case NFA_ZREF6: |
| 3023 | case NFA_ZREF7: |
| 3024 | case NFA_ZREF8: |
| 3025 | case NFA_ZREF9: |
| 3026 | #endif |
| 3027 | case NFA_NEWL: |
| 3028 | case NFA_SKIP: |
| 3029 | /* unknown width */ |
| 3030 | return -1; |
| 3031 | |
| 3032 | case NFA_BOL: |
| 3033 | case NFA_EOL: |
| 3034 | case NFA_BOF: |
| 3035 | case NFA_EOF: |
| 3036 | case NFA_BOW: |
| 3037 | case NFA_EOW: |
| 3038 | case NFA_MOPEN: |
| 3039 | case NFA_MOPEN1: |
| 3040 | case NFA_MOPEN2: |
| 3041 | case NFA_MOPEN3: |
| 3042 | case NFA_MOPEN4: |
| 3043 | case NFA_MOPEN5: |
| 3044 | case NFA_MOPEN6: |
| 3045 | case NFA_MOPEN7: |
| 3046 | case NFA_MOPEN8: |
| 3047 | case NFA_MOPEN9: |
| 3048 | #ifdef FEAT_SYN_HL |
| 3049 | case NFA_ZOPEN: |
| 3050 | case NFA_ZOPEN1: |
| 3051 | case NFA_ZOPEN2: |
| 3052 | case NFA_ZOPEN3: |
| 3053 | case NFA_ZOPEN4: |
| 3054 | case NFA_ZOPEN5: |
| 3055 | case NFA_ZOPEN6: |
| 3056 | case NFA_ZOPEN7: |
| 3057 | case NFA_ZOPEN8: |
| 3058 | case NFA_ZOPEN9: |
| 3059 | case NFA_ZCLOSE: |
| 3060 | case NFA_ZCLOSE1: |
| 3061 | case NFA_ZCLOSE2: |
| 3062 | case NFA_ZCLOSE3: |
| 3063 | case NFA_ZCLOSE4: |
| 3064 | case NFA_ZCLOSE5: |
| 3065 | case NFA_ZCLOSE6: |
| 3066 | case NFA_ZCLOSE7: |
| 3067 | case NFA_ZCLOSE8: |
| 3068 | case NFA_ZCLOSE9: |
| 3069 | #endif |
| 3070 | case NFA_MCLOSE: |
| 3071 | case NFA_MCLOSE1: |
| 3072 | case NFA_MCLOSE2: |
| 3073 | case NFA_MCLOSE3: |
| 3074 | case NFA_MCLOSE4: |
| 3075 | case NFA_MCLOSE5: |
| 3076 | case NFA_MCLOSE6: |
| 3077 | case NFA_MCLOSE7: |
| 3078 | case NFA_MCLOSE8: |
| 3079 | case NFA_MCLOSE9: |
| 3080 | case NFA_NOPEN: |
| 3081 | case NFA_NCLOSE: |
| 3082 | |
| 3083 | case NFA_LNUM_GT: |
| 3084 | case NFA_LNUM_LT: |
| 3085 | case NFA_COL_GT: |
| 3086 | case NFA_COL_LT: |
| 3087 | case NFA_VCOL_GT: |
| 3088 | case NFA_VCOL_LT: |
| 3089 | case NFA_MARK_GT: |
| 3090 | case NFA_MARK_LT: |
| 3091 | case NFA_VISUAL: |
| 3092 | case NFA_LNUM: |
| 3093 | case NFA_CURSOR: |
| 3094 | case NFA_COL: |
| 3095 | case NFA_VCOL: |
| 3096 | case NFA_MARK: |
| 3097 | |
| 3098 | case NFA_ZSTART: |
| 3099 | case NFA_ZEND: |
| 3100 | case NFA_OPT_CHARS: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3101 | case NFA_EMPTY: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3102 | case NFA_START_PATTERN: |
| 3103 | case NFA_END_PATTERN: |
| 3104 | case NFA_COMPOSING: |
| 3105 | case NFA_END_COMPOSING: |
| 3106 | /* zero-width */ |
| 3107 | break; |
| 3108 | |
| 3109 | default: |
| 3110 | if (state->c < 0) |
| 3111 | /* don't know what this is */ |
| 3112 | return -1; |
| 3113 | /* normal character */ |
| 3114 | len += MB_CHAR2LEN(state->c); |
| 3115 | break; |
| 3116 | } |
| 3117 | |
| 3118 | /* normal way to continue */ |
| 3119 | state = state->out; |
| 3120 | } |
| 3121 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 3122 | /* unrecognized, "cannot happen" */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3123 | return -1; |
| 3124 | } |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3125 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3126 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3127 | * Convert a postfix form into its equivalent NFA. |
| 3128 | * Return the NFA start state on success, NULL otherwise. |
| 3129 | */ |
| 3130 | static nfa_state_T * |
| 3131 | post2nfa(postfix, end, nfa_calc_size) |
| 3132 | int *postfix; |
| 3133 | int *end; |
| 3134 | int nfa_calc_size; |
| 3135 | { |
| 3136 | int *p; |
| 3137 | int mopen; |
| 3138 | int mclose; |
| 3139 | Frag_T *stack = NULL; |
| 3140 | Frag_T *stackp = NULL; |
| 3141 | Frag_T *stack_end = NULL; |
| 3142 | Frag_T e1; |
| 3143 | Frag_T e2; |
| 3144 | Frag_T e; |
| 3145 | nfa_state_T *s; |
| 3146 | nfa_state_T *s1; |
| 3147 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3148 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3149 | |
| 3150 | if (postfix == NULL) |
| 3151 | return NULL; |
| 3152 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 3153 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3154 | #define POP() st_pop(&stackp, stack); \ |
| 3155 | if (stackp < stack) \ |
| 3156 | { \ |
| 3157 | st_error(postfix, end, p); \ |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3158 | vim_free(stack); \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3159 | return NULL; \ |
| 3160 | } |
| 3161 | |
| 3162 | if (nfa_calc_size == FALSE) |
| 3163 | { |
| 3164 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3165 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3166 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 3167 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3168 | } |
| 3169 | |
| 3170 | for (p = postfix; p < end; ++p) |
| 3171 | { |
| 3172 | switch (*p) |
| 3173 | { |
| 3174 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3175 | /* Concatenation. |
| 3176 | * Pay attention: this operator does not exist in the r.e. itself |
| 3177 | * (it is implicit, really). It is added when r.e. is translated |
| 3178 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3179 | if (nfa_calc_size == TRUE) |
| 3180 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3181 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3182 | break; |
| 3183 | } |
| 3184 | e2 = POP(); |
| 3185 | e1 = POP(); |
| 3186 | patch(e1.out, e2.start); |
| 3187 | PUSH(frag(e1.start, e2.out)); |
| 3188 | break; |
| 3189 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3190 | case NFA_OR: |
| 3191 | /* Alternation */ |
| 3192 | if (nfa_calc_size == TRUE) |
| 3193 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3194 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3195 | break; |
| 3196 | } |
| 3197 | e2 = POP(); |
| 3198 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3199 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3200 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3201 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3202 | PUSH(frag(s, append(e1.out, e2.out))); |
| 3203 | break; |
| 3204 | |
| 3205 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3206 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3207 | if (nfa_calc_size == TRUE) |
| 3208 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3209 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3210 | break; |
| 3211 | } |
| 3212 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3213 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3214 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3215 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3216 | patch(e.out, s); |
| 3217 | PUSH(frag(s, list1(&s->out1))); |
| 3218 | break; |
| 3219 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3220 | case NFA_STAR_NONGREEDY: |
| 3221 | /* Zero or more, prefer zero */ |
| 3222 | if (nfa_calc_size == TRUE) |
| 3223 | { |
| 3224 | nstate++; |
| 3225 | break; |
| 3226 | } |
| 3227 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3228 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3229 | if (s == NULL) |
| 3230 | goto theend; |
| 3231 | patch(e.out, s); |
| 3232 | PUSH(frag(s, list1(&s->out))); |
| 3233 | break; |
| 3234 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3235 | case NFA_QUEST: |
| 3236 | /* one or zero atoms=> greedy match */ |
| 3237 | if (nfa_calc_size == TRUE) |
| 3238 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3239 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3240 | break; |
| 3241 | } |
| 3242 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3243 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3244 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3245 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3246 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 3247 | break; |
| 3248 | |
| 3249 | case NFA_QUEST_NONGREEDY: |
| 3250 | /* zero or one atoms => non-greedy match */ |
| 3251 | if (nfa_calc_size == TRUE) |
| 3252 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3253 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3254 | break; |
| 3255 | } |
| 3256 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3257 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3258 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3259 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3260 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 3261 | break; |
| 3262 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3263 | case NFA_END_COLL: |
| 3264 | case NFA_END_NEG_COLL: |
| 3265 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 3266 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 3267 | * add the output to the start. */ |
| 3268 | if (nfa_calc_size == TRUE) |
| 3269 | { |
| 3270 | nstate++; |
| 3271 | break; |
| 3272 | } |
| 3273 | e = POP(); |
| 3274 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 3275 | if (s == NULL) |
| 3276 | goto theend; |
| 3277 | patch(e.out, s); |
| 3278 | e.start->out1 = s; |
| 3279 | PUSH(frag(e.start, list1(&s->out))); |
| 3280 | break; |
| 3281 | |
| 3282 | case NFA_RANGE: |
| 3283 | /* Before this are two characters, the low and high end of a |
| 3284 | * range. Turn them into two states with MIN and MAX. */ |
| 3285 | if (nfa_calc_size == TRUE) |
| 3286 | { |
| 3287 | /* nstate += 0; */ |
| 3288 | break; |
| 3289 | } |
| 3290 | e2 = POP(); |
| 3291 | e1 = POP(); |
| 3292 | e2.start->val = e2.start->c; |
| 3293 | e2.start->c = NFA_RANGE_MAX; |
| 3294 | e1.start->val = e1.start->c; |
| 3295 | e1.start->c = NFA_RANGE_MIN; |
| 3296 | patch(e1.out, e2.start); |
| 3297 | PUSH(frag(e1.start, e2.out)); |
| 3298 | break; |
| 3299 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3300 | case NFA_EMPTY: |
| 3301 | /* 0-length, used in a repetition with max/min count of 0 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3302 | if (nfa_calc_size == TRUE) |
| 3303 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3304 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3305 | break; |
| 3306 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3307 | s = alloc_state(NFA_EMPTY, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3308 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3309 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3310 | PUSH(frag(s, list1(&s->out))); |
| 3311 | break; |
| 3312 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3313 | case NFA_OPT_CHARS: |
| 3314 | { |
| 3315 | int n; |
| 3316 | |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 3317 | /* \%[abc] implemented as: |
| 3318 | * NFA_SPLIT |
| 3319 | * +-CHAR(a) |
| 3320 | * | +-NFA_SPLIT |
| 3321 | * | +-CHAR(b) |
| 3322 | * | | +-NFA_SPLIT |
| 3323 | * | | +-CHAR(c) |
| 3324 | * | | | +-next |
| 3325 | * | | +- next |
| 3326 | * | +- next |
| 3327 | * +- next |
| 3328 | */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3329 | n = *++p; /* get number of characters */ |
| 3330 | if (nfa_calc_size == TRUE) |
| 3331 | { |
| 3332 | nstate += n; |
| 3333 | break; |
| 3334 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 3335 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3336 | e1.out = NULL; /* stores list with out1's */ |
| 3337 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 3338 | while (n-- > 0) |
| 3339 | { |
| 3340 | e = POP(); /* get character */ |
| 3341 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 3342 | if (s == NULL) |
| 3343 | goto theend; |
| 3344 | if (e1.out == NULL) |
| 3345 | e1 = e; |
| 3346 | patch(e.out, s1); |
| 3347 | append(e1.out, list1(&s->out1)); |
| 3348 | s1 = s; |
| 3349 | } |
| 3350 | PUSH(frag(s, e1.out)); |
| 3351 | break; |
| 3352 | } |
| 3353 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3354 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3355 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3356 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3357 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3358 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3359 | { |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3360 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 3361 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3362 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3363 | int start_state; |
| 3364 | int end_state; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3365 | int n = 0; |
| 3366 | nfa_state_T *zend; |
| 3367 | nfa_state_T *skip; |
| 3368 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3369 | switch (*p) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3370 | { |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3371 | case NFA_PREV_ATOM_NO_WIDTH: |
| 3372 | start_state = NFA_START_INVISIBLE; |
| 3373 | end_state = NFA_END_INVISIBLE; |
| 3374 | break; |
| 3375 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 3376 | start_state = NFA_START_INVISIBLE_NEG; |
| 3377 | end_state = NFA_END_INVISIBLE_NEG; |
| 3378 | break; |
| 3379 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3380 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 3381 | end_state = NFA_END_INVISIBLE; |
| 3382 | break; |
| 3383 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 3384 | start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
| 3385 | end_state = NFA_END_INVISIBLE_NEG; |
| 3386 | break; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 3387 | default: /* NFA_PREV_ATOM_LIKE_PATTERN: */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3388 | start_state = NFA_START_PATTERN; |
| 3389 | end_state = NFA_END_PATTERN; |
| 3390 | break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3391 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3392 | |
| 3393 | if (before) |
| 3394 | n = *++p; /* get the count */ |
| 3395 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3396 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3397 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3398 | * The \@<= operator: match for the preceding atom. |
| 3399 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3400 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3401 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3402 | |
| 3403 | if (nfa_calc_size == TRUE) |
| 3404 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3405 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3406 | break; |
| 3407 | } |
| 3408 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3409 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3410 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3411 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3412 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3413 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3414 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3415 | goto theend; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3416 | if (pattern) |
| 3417 | { |
| 3418 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 3419 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 3420 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 3421 | s1->out= skip; |
| 3422 | patch(e.out, zend); |
| 3423 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3424 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3425 | else |
| 3426 | { |
| 3427 | patch(e.out, s1); |
| 3428 | PUSH(frag(s, list1(&s1->out))); |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3429 | if (before) |
| 3430 | { |
| 3431 | if (n <= 0) |
| 3432 | /* See if we can guess the maximum width, it avoids a |
| 3433 | * lot of pointless tries. */ |
| 3434 | n = nfa_max_width(e.start, 0); |
| 3435 | s->val = n; /* store the count */ |
| 3436 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3437 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3438 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3439 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3440 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3441 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3442 | case NFA_COMPOSING: /* char with composing char */ |
| 3443 | #if 0 |
| 3444 | /* TODO */ |
| 3445 | if (regflags & RF_ICOMBINE) |
| 3446 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3447 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3448 | } |
| 3449 | #endif |
| 3450 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3451 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3452 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3453 | case NFA_MOPEN: /* \( \) Submatch */ |
| 3454 | case NFA_MOPEN1: |
| 3455 | case NFA_MOPEN2: |
| 3456 | case NFA_MOPEN3: |
| 3457 | case NFA_MOPEN4: |
| 3458 | case NFA_MOPEN5: |
| 3459 | case NFA_MOPEN6: |
| 3460 | case NFA_MOPEN7: |
| 3461 | case NFA_MOPEN8: |
| 3462 | case NFA_MOPEN9: |
| 3463 | #ifdef FEAT_SYN_HL |
| 3464 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 3465 | case NFA_ZOPEN1: |
| 3466 | case NFA_ZOPEN2: |
| 3467 | case NFA_ZOPEN3: |
| 3468 | case NFA_ZOPEN4: |
| 3469 | case NFA_ZOPEN5: |
| 3470 | case NFA_ZOPEN6: |
| 3471 | case NFA_ZOPEN7: |
| 3472 | case NFA_ZOPEN8: |
| 3473 | case NFA_ZOPEN9: |
| 3474 | #endif |
| 3475 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3476 | if (nfa_calc_size == TRUE) |
| 3477 | { |
| 3478 | nstate += 2; |
| 3479 | break; |
| 3480 | } |
| 3481 | |
| 3482 | mopen = *p; |
| 3483 | switch (*p) |
| 3484 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3485 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 3486 | #ifdef FEAT_SYN_HL |
| 3487 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 3488 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 3489 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 3490 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 3491 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 3492 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 3493 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 3494 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 3495 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 3496 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 3497 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3498 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3499 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3500 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3501 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3502 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3503 | mclose = *p + NSUBEXP; |
| 3504 | break; |
| 3505 | } |
| 3506 | |
| 3507 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 3508 | * the empty regexp "". In this case, the NFA will be |
| 3509 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 3510 | * empty groups of parenthesis, and empty mbyte chars */ |
| 3511 | if (stackp == stack) |
| 3512 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3513 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3514 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3515 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3516 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3517 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3518 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3519 | patch(list1(&s->out), s1); |
| 3520 | PUSH(frag(s, list1(&s1->out))); |
| 3521 | break; |
| 3522 | } |
| 3523 | |
| 3524 | /* At least one node was emitted before NFA_MOPEN, so |
| 3525 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 3526 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3527 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3528 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3529 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3530 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3531 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3532 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3533 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3534 | patch(e.out, s1); |
| 3535 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3536 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3537 | if (mopen == NFA_COMPOSING) |
| 3538 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3539 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3540 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3541 | |
| 3542 | PUSH(frag(s, list1(&s1->out))); |
| 3543 | break; |
| 3544 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3545 | case NFA_BACKREF1: |
| 3546 | case NFA_BACKREF2: |
| 3547 | case NFA_BACKREF3: |
| 3548 | case NFA_BACKREF4: |
| 3549 | case NFA_BACKREF5: |
| 3550 | case NFA_BACKREF6: |
| 3551 | case NFA_BACKREF7: |
| 3552 | case NFA_BACKREF8: |
| 3553 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3554 | #ifdef FEAT_SYN_HL |
| 3555 | case NFA_ZREF1: |
| 3556 | case NFA_ZREF2: |
| 3557 | case NFA_ZREF3: |
| 3558 | case NFA_ZREF4: |
| 3559 | case NFA_ZREF5: |
| 3560 | case NFA_ZREF6: |
| 3561 | case NFA_ZREF7: |
| 3562 | case NFA_ZREF8: |
| 3563 | case NFA_ZREF9: |
| 3564 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3565 | if (nfa_calc_size == TRUE) |
| 3566 | { |
| 3567 | nstate += 2; |
| 3568 | break; |
| 3569 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3570 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3571 | if (s == NULL) |
| 3572 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3573 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3574 | if (s1 == NULL) |
| 3575 | goto theend; |
| 3576 | patch(list1(&s->out), s1); |
| 3577 | PUSH(frag(s, list1(&s1->out))); |
| 3578 | break; |
| 3579 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3580 | case NFA_LNUM: |
| 3581 | case NFA_LNUM_GT: |
| 3582 | case NFA_LNUM_LT: |
| 3583 | case NFA_VCOL: |
| 3584 | case NFA_VCOL_GT: |
| 3585 | case NFA_VCOL_LT: |
| 3586 | case NFA_COL: |
| 3587 | case NFA_COL_GT: |
| 3588 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 3589 | case NFA_MARK: |
| 3590 | case NFA_MARK_GT: |
| 3591 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3592 | { |
| 3593 | int n = *++p; /* lnum, col or mark name */ |
| 3594 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3595 | if (nfa_calc_size == TRUE) |
| 3596 | { |
| 3597 | nstate += 1; |
| 3598 | break; |
| 3599 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3600 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3601 | if (s == NULL) |
| 3602 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3603 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3604 | PUSH(frag(s, list1(&s->out))); |
| 3605 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3606 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3607 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3608 | case NFA_ZSTART: |
| 3609 | case NFA_ZEND: |
| 3610 | default: |
| 3611 | /* Operands */ |
| 3612 | if (nfa_calc_size == TRUE) |
| 3613 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3614 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3615 | break; |
| 3616 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3617 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3618 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3619 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3620 | PUSH(frag(s, list1(&s->out))); |
| 3621 | break; |
| 3622 | |
| 3623 | } /* switch(*p) */ |
| 3624 | |
| 3625 | } /* for(p = postfix; *p; ++p) */ |
| 3626 | |
| 3627 | if (nfa_calc_size == TRUE) |
| 3628 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3629 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3630 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3631 | } |
| 3632 | |
| 3633 | e = POP(); |
| 3634 | if (stackp != stack) |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3635 | { |
| 3636 | vim_free(stack); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3637 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3638 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3639 | |
| 3640 | if (istate >= nstate) |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3641 | { |
| 3642 | vim_free(stack); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3643 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3644 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3645 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3646 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3647 | matchstate->c = NFA_MATCH; |
| 3648 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3649 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3650 | |
| 3651 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3652 | ret = e.start; |
| 3653 | |
| 3654 | theend: |
| 3655 | vim_free(stack); |
| 3656 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3657 | |
| 3658 | #undef POP1 |
| 3659 | #undef PUSH1 |
| 3660 | #undef POP2 |
| 3661 | #undef PUSH2 |
| 3662 | #undef POP |
| 3663 | #undef PUSH |
| 3664 | } |
| 3665 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3666 | /* |
| 3667 | * After building the NFA program, inspect it to add optimization hints. |
| 3668 | */ |
| 3669 | static void |
| 3670 | nfa_postprocess(prog) |
| 3671 | nfa_regprog_T *prog; |
| 3672 | { |
| 3673 | int i; |
| 3674 | int c; |
| 3675 | |
| 3676 | for (i = 0; i < prog->nstate; ++i) |
| 3677 | { |
| 3678 | c = prog->state[i].c; |
| 3679 | if (c == NFA_START_INVISIBLE |
| 3680 | || c == NFA_START_INVISIBLE_NEG |
| 3681 | || c == NFA_START_INVISIBLE_BEFORE |
| 3682 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3683 | { |
| 3684 | int directly; |
| 3685 | |
| 3686 | /* Do it directly when what follows is possibly the end of the |
| 3687 | * match. */ |
| 3688 | if (match_follows(prog->state[i].out1->out, 0)) |
| 3689 | directly = TRUE; |
| 3690 | else |
| 3691 | { |
| 3692 | int ch_invisible = failure_chance(prog->state[i].out, 0); |
| 3693 | int ch_follows = failure_chance(prog->state[i].out1->out, 0); |
| 3694 | |
| 3695 | /* Postpone when the invisible match is expensive or has a |
| 3696 | * lower chance of failing. */ |
| 3697 | if (c == NFA_START_INVISIBLE_BEFORE |
| 3698 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3699 | { |
| 3700 | /* "before" matches are very expensive when |
| 3701 | * unbounded, always prefer what follows then, |
| 3702 | * unless what follows will always match. |
| 3703 | * Otherwise strongly prefer what follows. */ |
| 3704 | if (prog->state[i].val <= 0 && ch_follows > 0) |
| 3705 | directly = FALSE; |
| 3706 | else |
| 3707 | directly = ch_follows * 10 < ch_invisible; |
| 3708 | } |
| 3709 | else |
| 3710 | { |
| 3711 | /* normal invisible, first do the one with the |
| 3712 | * highest failure chance */ |
| 3713 | directly = ch_follows < ch_invisible; |
| 3714 | } |
| 3715 | } |
| 3716 | if (directly) |
| 3717 | /* switch to the _FIRST state */ |
| 3718 | ++prog->state[i].c; |
| 3719 | } |
| 3720 | } |
| 3721 | } |
| 3722 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3723 | /**************************************************************** |
| 3724 | * NFA execution code. |
| 3725 | ****************************************************************/ |
| 3726 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3727 | typedef struct |
| 3728 | { |
| 3729 | int in_use; /* number of subexpr with useful info */ |
| 3730 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3731 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3732 | union |
| 3733 | { |
| 3734 | struct multipos |
| 3735 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3736 | linenr_T start_lnum; |
| 3737 | linenr_T end_lnum; |
| 3738 | colnr_T start_col; |
| 3739 | colnr_T end_col; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3740 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3741 | struct linepos |
| 3742 | { |
| 3743 | char_u *start; |
| 3744 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3745 | } line[NSUBEXP]; |
| 3746 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3747 | } regsub_T; |
| 3748 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3749 | typedef struct |
| 3750 | { |
| 3751 | regsub_T norm; /* \( .. \) matches */ |
| 3752 | #ifdef FEAT_SYN_HL |
| 3753 | regsub_T synt; /* \z( .. \) matches */ |
| 3754 | #endif |
| 3755 | } regsubs_T; |
| 3756 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3757 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3758 | typedef struct nfa_pim_S nfa_pim_T; |
| 3759 | struct nfa_pim_S |
| 3760 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3761 | int result; /* NFA_PIM_*, see below */ |
| 3762 | nfa_state_T *state; /* the invisible match start state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3763 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3764 | union |
| 3765 | { |
| 3766 | lpos_T pos; |
| 3767 | char_u *ptr; |
| 3768 | } end; /* where the match must end */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3769 | }; |
| 3770 | |
| 3771 | /* Values for done in nfa_pim_T. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3772 | #define NFA_PIM_UNUSED 0 /* pim not used */ |
| 3773 | #define NFA_PIM_TODO 1 /* pim not done yet */ |
| 3774 | #define NFA_PIM_MATCH 2 /* pim executed, matches */ |
| 3775 | #define NFA_PIM_NOMATCH 3 /* pim executed, no match */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3776 | |
| 3777 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3778 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3779 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3780 | { |
| 3781 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3782 | int count; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3783 | nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed |
| 3784 | * invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3785 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3786 | } nfa_thread_T; |
| 3787 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3788 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3789 | typedef struct |
| 3790 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3791 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3792 | int n; /* nr of states currently in "t" */ |
| 3793 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3794 | int id; /* ID of the list */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 3795 | int has_pim; /* TRUE when any state has a PIM */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3796 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3797 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3798 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3799 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 3800 | static void log_subexpr __ARGS((regsub_T *sub)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3801 | static char *pim_info __ARGS((nfa_pim_T *pim)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3802 | |
| 3803 | static void |
| 3804 | log_subsexpr(subs) |
| 3805 | regsubs_T *subs; |
| 3806 | { |
| 3807 | log_subexpr(&subs->norm); |
| 3808 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3809 | if (nfa_has_zsubexpr) |
| 3810 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3811 | # endif |
| 3812 | } |
| 3813 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3814 | static void |
| 3815 | log_subexpr(sub) |
| 3816 | regsub_T *sub; |
| 3817 | { |
| 3818 | int j; |
| 3819 | |
| 3820 | for (j = 0; j < sub->in_use; j++) |
| 3821 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3822 | 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] | 3823 | j, |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3824 | sub->list.multi[j].start_col, |
| 3825 | (int)sub->list.multi[j].start_lnum, |
| 3826 | sub->list.multi[j].end_col, |
| 3827 | (int)sub->list.multi[j].end_lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3828 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3829 | { |
| 3830 | char *s = (char *)sub->list.line[j].start; |
| 3831 | char *e = (char *)sub->list.line[j].end; |
| 3832 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3833 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3834 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3835 | s == NULL ? "NULL" : s, |
| 3836 | e == NULL ? "NULL" : e); |
| 3837 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3838 | } |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3839 | |
| 3840 | static char * |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3841 | pim_info(pim) |
| 3842 | nfa_pim_T *pim; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3843 | { |
| 3844 | static char buf[30]; |
| 3845 | |
| 3846 | if (pim == NULL || pim->result == NFA_PIM_UNUSED) |
| 3847 | buf[0] = NUL; |
| 3848 | else |
| 3849 | { |
| 3850 | sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col |
| 3851 | : (int)(pim->end.ptr - reginput)); |
| 3852 | } |
| 3853 | return buf; |
| 3854 | } |
| 3855 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3856 | #endif |
| 3857 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3858 | /* Used during execution: whether a match has been found. */ |
| 3859 | static int nfa_match; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 3860 | #ifdef FEAT_RELTIME |
| 3861 | static proftime_T *nfa_time_limit; |
| 3862 | static int nfa_time_count; |
| 3863 | #endif |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3864 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3865 | 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] | 3866 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3867 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3868 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3869 | static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3870 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3871 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 3872 | static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim)); |
| 3873 | 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] | 3874 | 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] | 3875 | 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] | 3876 | 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] | 3877 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3878 | /* |
| 3879 | * Copy postponed invisible match info from "from" to "to". |
| 3880 | */ |
| 3881 | static void |
| 3882 | copy_pim(to, from) |
| 3883 | nfa_pim_T *to; |
| 3884 | nfa_pim_T *from; |
| 3885 | { |
| 3886 | to->result = from->result; |
| 3887 | to->state = from->state; |
| 3888 | copy_sub(&to->subs.norm, &from->subs.norm); |
| 3889 | #ifdef FEAT_SYN_HL |
| 3890 | if (nfa_has_zsubexpr) |
| 3891 | copy_sub(&to->subs.synt, &from->subs.synt); |
| 3892 | #endif |
| 3893 | to->end = from->end; |
| 3894 | } |
| 3895 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3896 | static void |
| 3897 | clear_sub(sub) |
| 3898 | regsub_T *sub; |
| 3899 | { |
| 3900 | if (REG_MULTI) |
| 3901 | /* Use 0xff to set lnum to -1 */ |
| 3902 | vim_memset(sub->list.multi, 0xff, |
| 3903 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3904 | else |
| 3905 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3906 | sub->in_use = 0; |
| 3907 | } |
| 3908 | |
| 3909 | /* |
| 3910 | * Copy the submatches from "from" to "to". |
| 3911 | */ |
| 3912 | static void |
| 3913 | copy_sub(to, from) |
| 3914 | regsub_T *to; |
| 3915 | regsub_T *from; |
| 3916 | { |
| 3917 | to->in_use = from->in_use; |
| 3918 | if (from->in_use > 0) |
| 3919 | { |
| 3920 | /* Copy the match start and end positions. */ |
| 3921 | if (REG_MULTI) |
| 3922 | mch_memmove(&to->list.multi[0], |
| 3923 | &from->list.multi[0], |
| 3924 | sizeof(struct multipos) * from->in_use); |
| 3925 | else |
| 3926 | mch_memmove(&to->list.line[0], |
| 3927 | &from->list.line[0], |
| 3928 | sizeof(struct linepos) * from->in_use); |
| 3929 | } |
| 3930 | } |
| 3931 | |
| 3932 | /* |
| 3933 | * Like copy_sub() but exclude the main match. |
| 3934 | */ |
| 3935 | static void |
| 3936 | copy_sub_off(to, from) |
| 3937 | regsub_T *to; |
| 3938 | regsub_T *from; |
| 3939 | { |
| 3940 | if (to->in_use < from->in_use) |
| 3941 | to->in_use = from->in_use; |
| 3942 | if (from->in_use > 1) |
| 3943 | { |
| 3944 | /* Copy the match start and end positions. */ |
| 3945 | if (REG_MULTI) |
| 3946 | mch_memmove(&to->list.multi[1], |
| 3947 | &from->list.multi[1], |
| 3948 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3949 | else |
| 3950 | mch_memmove(&to->list.line[1], |
| 3951 | &from->list.line[1], |
| 3952 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3953 | } |
| 3954 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3955 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3956 | /* |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3957 | * Like copy_sub() but only do the end of the main match if \ze is present. |
| 3958 | */ |
| 3959 | static void |
| 3960 | copy_ze_off(to, from) |
| 3961 | regsub_T *to; |
| 3962 | regsub_T *from; |
| 3963 | { |
| 3964 | if (nfa_has_zend) |
| 3965 | { |
| 3966 | if (REG_MULTI) |
| 3967 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3968 | if (from->list.multi[0].end_lnum >= 0) |
| 3969 | { |
| 3970 | to->list.multi[0].end_lnum = from->list.multi[0].end_lnum; |
| 3971 | to->list.multi[0].end_col = from->list.multi[0].end_col; |
| 3972 | } |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3973 | } |
| 3974 | else |
| 3975 | { |
| 3976 | if (from->list.line[0].end != NULL) |
| 3977 | to->list.line[0].end = from->list.line[0].end; |
| 3978 | } |
| 3979 | } |
| 3980 | } |
| 3981 | |
| 3982 | /* |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3983 | * Return TRUE if "sub1" and "sub2" have the same start positions. |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 3984 | * When using back-references also check the end position. |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3985 | */ |
| 3986 | static int |
| 3987 | sub_equal(sub1, sub2) |
| 3988 | regsub_T *sub1; |
| 3989 | regsub_T *sub2; |
| 3990 | { |
| 3991 | int i; |
| 3992 | int todo; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3993 | linenr_T s1; |
| 3994 | linenr_T s2; |
| 3995 | char_u *sp1; |
| 3996 | char_u *sp2; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3997 | |
| 3998 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3999 | if (REG_MULTI) |
| 4000 | { |
| 4001 | for (i = 0; i < todo; ++i) |
| 4002 | { |
| 4003 | if (i < sub1->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4004 | s1 = sub1->list.multi[i].start_lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4005 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4006 | s1 = -1; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4007 | if (i < sub2->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4008 | s2 = sub2->list.multi[i].start_lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4009 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4010 | s2 = -1; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 4011 | if (s1 != s2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4012 | return FALSE; |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4013 | if (s1 != -1 && sub1->list.multi[i].start_col |
| 4014 | != sub2->list.multi[i].start_col) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4015 | return FALSE; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 4016 | |
| 4017 | if (nfa_has_backref) |
| 4018 | { |
| 4019 | if (i < sub1->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4020 | s1 = sub1->list.multi[i].end_lnum; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 4021 | else |
| 4022 | s1 = -1; |
| 4023 | if (i < sub2->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4024 | s2 = sub2->list.multi[i].end_lnum; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 4025 | else |
| 4026 | s2 = -1; |
| 4027 | if (s1 != s2) |
| 4028 | return FALSE; |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4029 | if (s1 != -1 && sub1->list.multi[i].end_col |
| 4030 | != sub2->list.multi[i].end_col) |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 4031 | return FALSE; |
| 4032 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4033 | } |
| 4034 | } |
| 4035 | else |
| 4036 | { |
| 4037 | for (i = 0; i < todo; ++i) |
| 4038 | { |
| 4039 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4040 | sp1 = sub1->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4041 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4042 | sp1 = NULL; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4043 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4044 | sp2 = sub2->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4045 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4046 | sp2 = NULL; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 4047 | if (sp1 != sp2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4048 | return FALSE; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 4049 | if (nfa_has_backref) |
| 4050 | { |
| 4051 | if (i < sub1->in_use) |
| 4052 | sp1 = sub1->list.line[i].end; |
| 4053 | else |
| 4054 | sp1 = NULL; |
| 4055 | if (i < sub2->in_use) |
| 4056 | sp2 = sub2->list.line[i].end; |
| 4057 | else |
| 4058 | sp2 = NULL; |
| 4059 | if (sp1 != sp2) |
| 4060 | return FALSE; |
| 4061 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | return TRUE; |
| 4066 | } |
| 4067 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4068 | #ifdef ENABLE_LOG |
| 4069 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4070 | report_state(char *action, |
| 4071 | regsub_T *sub, |
| 4072 | nfa_state_T *state, |
| 4073 | int lid, |
| 4074 | nfa_pim_T *pim) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4075 | { |
| 4076 | int col; |
| 4077 | |
| 4078 | if (sub->in_use <= 0) |
| 4079 | col = -1; |
| 4080 | else if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4081 | col = sub->list.multi[0].start_col; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4082 | else |
| 4083 | col = (int)(sub->list.line[0].start - regline); |
| 4084 | nfa_set_code(state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4085 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n", |
| 4086 | action, abs(state->id), lid, state->c, code, col, |
| 4087 | pim_info(pim)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4088 | } |
| 4089 | #endif |
| 4090 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4091 | /* |
| 4092 | * Return TRUE if the same state is already in list "l" with the same |
| 4093 | * positions as "subs". |
| 4094 | */ |
| 4095 | static int |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4096 | has_state_with_pos(l, state, subs, pim) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4097 | nfa_list_T *l; /* runtime state list */ |
| 4098 | nfa_state_T *state; /* state to update */ |
| 4099 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4100 | nfa_pim_T *pim; /* postponed match or NULL */ |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4101 | { |
| 4102 | nfa_thread_T *thread; |
| 4103 | int i; |
| 4104 | |
| 4105 | for (i = 0; i < l->n; ++i) |
| 4106 | { |
| 4107 | thread = &l->t[i]; |
| 4108 | if (thread->state->id == state->id |
| 4109 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 4110 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 4111 | && (!nfa_has_zsubexpr |
| 4112 | || sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4113 | #endif |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4114 | && pim_equal(&thread->pim, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4115 | return TRUE; |
| 4116 | } |
| 4117 | return FALSE; |
| 4118 | } |
| 4119 | |
| 4120 | /* |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4121 | * Return TRUE if "one" and "two" are equal. That includes when both are not |
| 4122 | * set. |
| 4123 | */ |
| 4124 | static int |
| 4125 | pim_equal(one, two) |
| 4126 | nfa_pim_T *one; |
| 4127 | nfa_pim_T *two; |
| 4128 | { |
| 4129 | int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED); |
| 4130 | int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED); |
| 4131 | |
| 4132 | if (one_unused) |
| 4133 | /* one is unused: equal when two is also unused */ |
| 4134 | return two_unused; |
| 4135 | if (two_unused) |
| 4136 | /* one is used and two is not: not equal */ |
| 4137 | return FALSE; |
Bram Moolenaar | 3f0df06 | 2013-08-14 13:34:25 +0200 | [diff] [blame] | 4138 | /* compare the state id */ |
| 4139 | if (one->state->id != two->state->id) |
| 4140 | return FALSE; |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4141 | /* compare the position */ |
| 4142 | if (REG_MULTI) |
| 4143 | return one->end.pos.lnum == two->end.pos.lnum |
| 4144 | && one->end.pos.col == two->end.pos.col; |
| 4145 | return one->end.ptr == two->end.ptr; |
| 4146 | } |
| 4147 | |
| 4148 | /* |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4149 | * Return TRUE if "state" leads to a NFA_MATCH without advancing the input. |
| 4150 | */ |
| 4151 | static int |
| 4152 | match_follows(startstate, depth) |
| 4153 | nfa_state_T *startstate; |
| 4154 | int depth; |
| 4155 | { |
| 4156 | nfa_state_T *state = startstate; |
| 4157 | |
| 4158 | /* avoid too much recursion */ |
| 4159 | if (depth > 10) |
| 4160 | return FALSE; |
| 4161 | |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4162 | while (state != NULL) |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4163 | { |
| 4164 | switch (state->c) |
| 4165 | { |
| 4166 | case NFA_MATCH: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4167 | case NFA_MCLOSE: |
| 4168 | case NFA_END_INVISIBLE: |
| 4169 | case NFA_END_INVISIBLE_NEG: |
| 4170 | case NFA_END_PATTERN: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4171 | return TRUE; |
| 4172 | |
| 4173 | case NFA_SPLIT: |
| 4174 | return match_follows(state->out, depth + 1) |
| 4175 | || match_follows(state->out1, depth + 1); |
| 4176 | |
| 4177 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4178 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4179 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4180 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4181 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4182 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4183 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4184 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4185 | case NFA_COMPOSING: |
| 4186 | /* skip ahead to next state */ |
| 4187 | state = state->out1->out; |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4188 | continue; |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4189 | |
| 4190 | case NFA_ANY: |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 4191 | case NFA_ANY_COMPOSING: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4192 | case NFA_IDENT: |
| 4193 | case NFA_SIDENT: |
| 4194 | case NFA_KWORD: |
| 4195 | case NFA_SKWORD: |
| 4196 | case NFA_FNAME: |
| 4197 | case NFA_SFNAME: |
| 4198 | case NFA_PRINT: |
| 4199 | case NFA_SPRINT: |
| 4200 | case NFA_WHITE: |
| 4201 | case NFA_NWHITE: |
| 4202 | case NFA_DIGIT: |
| 4203 | case NFA_NDIGIT: |
| 4204 | case NFA_HEX: |
| 4205 | case NFA_NHEX: |
| 4206 | case NFA_OCTAL: |
| 4207 | case NFA_NOCTAL: |
| 4208 | case NFA_WORD: |
| 4209 | case NFA_NWORD: |
| 4210 | case NFA_HEAD: |
| 4211 | case NFA_NHEAD: |
| 4212 | case NFA_ALPHA: |
| 4213 | case NFA_NALPHA: |
| 4214 | case NFA_LOWER: |
| 4215 | case NFA_NLOWER: |
| 4216 | case NFA_UPPER: |
| 4217 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 4218 | case NFA_LOWER_IC: |
| 4219 | case NFA_NLOWER_IC: |
| 4220 | case NFA_UPPER_IC: |
| 4221 | case NFA_NUPPER_IC: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4222 | case NFA_START_COLL: |
| 4223 | case NFA_START_NEG_COLL: |
| 4224 | case NFA_NEWL: |
| 4225 | /* state will advance input */ |
| 4226 | return FALSE; |
| 4227 | |
| 4228 | default: |
| 4229 | if (state->c > 0) |
| 4230 | /* state will advance input */ |
| 4231 | return FALSE; |
| 4232 | |
| 4233 | /* Others: zero-width or possibly zero-width, might still find |
| 4234 | * a match at the same position, keep looking. */ |
| 4235 | break; |
| 4236 | } |
| 4237 | state = state->out; |
| 4238 | } |
| 4239 | return FALSE; |
| 4240 | } |
| 4241 | |
| 4242 | |
| 4243 | /* |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4244 | * Return TRUE if "state" is already in list "l". |
| 4245 | */ |
| 4246 | static int |
| 4247 | state_in_list(l, state, subs) |
| 4248 | nfa_list_T *l; /* runtime state list */ |
| 4249 | nfa_state_T *state; /* state to update */ |
| 4250 | regsubs_T *subs; /* pointers to subexpressions */ |
| 4251 | { |
| 4252 | if (state->lastlist[nfa_ll_index] == l->id) |
| 4253 | { |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4254 | if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4255 | return TRUE; |
| 4256 | } |
| 4257 | return FALSE; |
| 4258 | } |
| 4259 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4260 | /* |
| 4261 | * Add "state" and possibly what follows to state list ".". |
| 4262 | * Returns "subs_arg", possibly copied into temp_subs. |
| 4263 | */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4264 | static regsubs_T * |
| 4265 | addstate(l, state, subs_arg, pim, off) |
| 4266 | nfa_list_T *l; /* runtime state list */ |
| 4267 | nfa_state_T *state; /* state to update */ |
| 4268 | regsubs_T *subs_arg; /* pointers to subexpressions */ |
| 4269 | nfa_pim_T *pim; /* postponed look-behind match */ |
| 4270 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4271 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4272 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4273 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4274 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4275 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4276 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4277 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4278 | regsub_T *sub; |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4279 | regsubs_T *subs = subs_arg; |
| 4280 | static regsubs_T temp_subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4281 | #ifdef ENABLE_LOG |
| 4282 | int did_print = FALSE; |
| 4283 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4284 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4285 | switch (state->c) |
| 4286 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4287 | case NFA_NCLOSE: |
| 4288 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4289 | case NFA_MCLOSE1: |
| 4290 | case NFA_MCLOSE2: |
| 4291 | case NFA_MCLOSE3: |
| 4292 | case NFA_MCLOSE4: |
| 4293 | case NFA_MCLOSE5: |
| 4294 | case NFA_MCLOSE6: |
| 4295 | case NFA_MCLOSE7: |
| 4296 | case NFA_MCLOSE8: |
| 4297 | case NFA_MCLOSE9: |
| 4298 | #ifdef FEAT_SYN_HL |
| 4299 | case NFA_ZCLOSE: |
| 4300 | case NFA_ZCLOSE1: |
| 4301 | case NFA_ZCLOSE2: |
| 4302 | case NFA_ZCLOSE3: |
| 4303 | case NFA_ZCLOSE4: |
| 4304 | case NFA_ZCLOSE5: |
| 4305 | case NFA_ZCLOSE6: |
| 4306 | case NFA_ZCLOSE7: |
| 4307 | case NFA_ZCLOSE8: |
| 4308 | case NFA_ZCLOSE9: |
| 4309 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4310 | case NFA_MOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4311 | case NFA_ZEND: |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4312 | case NFA_SPLIT: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4313 | case NFA_EMPTY: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4314 | /* These nodes are not added themselves but their "out" and/or |
| 4315 | * "out1" may be added below. */ |
| 4316 | break; |
| 4317 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4318 | case NFA_BOL: |
| 4319 | case NFA_BOF: |
| 4320 | /* "^" won't match past end-of-line, don't bother trying. |
| 4321 | * Except when at the end of the line, or when we are going to the |
| 4322 | * next line for a look-behind match. */ |
| 4323 | if (reginput > regline |
| 4324 | && *reginput != NUL |
| 4325 | && (nfa_endp == NULL |
| 4326 | || !REG_MULTI |
| 4327 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 4328 | goto skip_add; |
| 4329 | /* FALLTHROUGH */ |
| 4330 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4331 | case NFA_MOPEN1: |
| 4332 | case NFA_MOPEN2: |
| 4333 | case NFA_MOPEN3: |
| 4334 | case NFA_MOPEN4: |
| 4335 | case NFA_MOPEN5: |
| 4336 | case NFA_MOPEN6: |
| 4337 | case NFA_MOPEN7: |
| 4338 | case NFA_MOPEN8: |
| 4339 | case NFA_MOPEN9: |
| 4340 | #ifdef FEAT_SYN_HL |
| 4341 | case NFA_ZOPEN: |
| 4342 | case NFA_ZOPEN1: |
| 4343 | case NFA_ZOPEN2: |
| 4344 | case NFA_ZOPEN3: |
| 4345 | case NFA_ZOPEN4: |
| 4346 | case NFA_ZOPEN5: |
| 4347 | case NFA_ZOPEN6: |
| 4348 | case NFA_ZOPEN7: |
| 4349 | case NFA_ZOPEN8: |
| 4350 | case NFA_ZOPEN9: |
| 4351 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4352 | case NFA_NOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4353 | case NFA_ZSTART: |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4354 | /* These nodes need to be added so that we can bail out when it |
| 4355 | * was added to this list before at the same position to avoid an |
| 4356 | * endless loop for "\(\)*" */ |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4357 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4358 | default: |
Bram Moolenaar | 272fb58 | 2013-11-21 16:03:40 +0100 | [diff] [blame] | 4359 | if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4360 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4361 | /* This state is already in the list, don't add it again, |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4362 | * unless it is an MOPEN that is used for a backreference or |
Bram Moolenaar | 9c23506 | 2014-05-13 16:44:29 +0200 | [diff] [blame] | 4363 | * when there is a PIM. For NFA_MATCH check the position, |
| 4364 | * lower position is preferred. */ |
| 4365 | if (!nfa_has_backref && pim == NULL && !l->has_pim |
| 4366 | && state->c != NFA_MATCH) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4367 | { |
| 4368 | skip_add: |
| 4369 | #ifdef ENABLE_LOG |
| 4370 | nfa_set_code(state->c); |
| 4371 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 4372 | abs(state->id), l->id, state->c, code); |
| 4373 | #endif |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4374 | return subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4375 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4376 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4377 | /* Do not add the state again when it exists with the same |
| 4378 | * positions. */ |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4379 | if (has_state_with_pos(l, state, subs, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4380 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4381 | } |
| 4382 | |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4383 | /* When there are backreferences or PIMs the number of states may |
| 4384 | * be (a lot) bigger than anticipated. */ |
| 4385 | if (l->n == l->len) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4386 | { |
| 4387 | int newlen = l->len * 3 / 2 + 50; |
| 4388 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4389 | if (subs != &temp_subs) |
| 4390 | { |
| 4391 | /* "subs" may point into the current array, need to make a |
| 4392 | * copy before it becomes invalid. */ |
| 4393 | copy_sub(&temp_subs.norm, &subs->norm); |
| 4394 | #ifdef FEAT_SYN_HL |
| 4395 | if (nfa_has_zsubexpr) |
| 4396 | copy_sub(&temp_subs.synt, &subs->synt); |
| 4397 | #endif |
| 4398 | subs = &temp_subs; |
| 4399 | } |
| 4400 | |
Bram Moolenaar | a1d2c58 | 2015-02-10 18:18:17 +0100 | [diff] [blame] | 4401 | /* TODO: check for vim_realloc() returning NULL. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4402 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 4403 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4404 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4405 | |
| 4406 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4407 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4408 | thread = &l->t[l->n++]; |
| 4409 | thread->state = state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4410 | if (pim == NULL) |
| 4411 | thread->pim.result = NFA_PIM_UNUSED; |
| 4412 | else |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4413 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4414 | copy_pim(&thread->pim, pim); |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4415 | l->has_pim = TRUE; |
| 4416 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4417 | copy_sub(&thread->subs.norm, &subs->norm); |
| 4418 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4419 | if (nfa_has_zsubexpr) |
| 4420 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4421 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4422 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4423 | report_state("Adding", &thread->subs.norm, state, l->id, pim); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4424 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4425 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4429 | if (!did_print) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4430 | report_state("Processing", &subs->norm, state, l->id, pim); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4431 | #endif |
| 4432 | switch (state->c) |
| 4433 | { |
| 4434 | case NFA_MATCH: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4435 | break; |
| 4436 | |
| 4437 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4438 | /* order matters here */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4439 | subs = addstate(l, state->out, subs, pim, off); |
| 4440 | subs = addstate(l, state->out1, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4441 | break; |
| 4442 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4443 | case NFA_EMPTY: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4444 | case NFA_NOPEN: |
| 4445 | case NFA_NCLOSE: |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4446 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4447 | break; |
| 4448 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4449 | case NFA_MOPEN: |
| 4450 | case NFA_MOPEN1: |
| 4451 | case NFA_MOPEN2: |
| 4452 | case NFA_MOPEN3: |
| 4453 | case NFA_MOPEN4: |
| 4454 | case NFA_MOPEN5: |
| 4455 | case NFA_MOPEN6: |
| 4456 | case NFA_MOPEN7: |
| 4457 | case NFA_MOPEN8: |
| 4458 | case NFA_MOPEN9: |
| 4459 | #ifdef FEAT_SYN_HL |
| 4460 | case NFA_ZOPEN: |
| 4461 | case NFA_ZOPEN1: |
| 4462 | case NFA_ZOPEN2: |
| 4463 | case NFA_ZOPEN3: |
| 4464 | case NFA_ZOPEN4: |
| 4465 | case NFA_ZOPEN5: |
| 4466 | case NFA_ZOPEN6: |
| 4467 | case NFA_ZOPEN7: |
| 4468 | case NFA_ZOPEN8: |
| 4469 | case NFA_ZOPEN9: |
| 4470 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4471 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4472 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4473 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4474 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4475 | sub = &subs->norm; |
| 4476 | } |
| 4477 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4478 | else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4479 | { |
| 4480 | subidx = state->c - NFA_ZOPEN; |
| 4481 | sub = &subs->synt; |
| 4482 | } |
| 4483 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4484 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4485 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4486 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4487 | sub = &subs->norm; |
| 4488 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4489 | |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4490 | /* avoid compiler warnings */ |
| 4491 | save_ptr = NULL; |
| 4492 | save_lpos.lnum = 0; |
| 4493 | save_lpos.col = 0; |
| 4494 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4495 | /* Set the position (with "off" added) in the subexpression. Save |
| 4496 | * and restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4497 | if (REG_MULTI) |
| 4498 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4499 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4500 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4501 | save_lpos.lnum = sub->list.multi[subidx].start_lnum; |
| 4502 | save_lpos.col = sub->list.multi[subidx].start_col; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4503 | save_in_use = -1; |
| 4504 | } |
| 4505 | else |
| 4506 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4507 | save_in_use = sub->in_use; |
| 4508 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4509 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4510 | sub->list.multi[i].start_lnum = -1; |
| 4511 | sub->list.multi[i].end_lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4512 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4513 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4514 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4515 | if (off == -1) |
| 4516 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4517 | sub->list.multi[subidx].start_lnum = reglnum + 1; |
| 4518 | sub->list.multi[subidx].start_col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4519 | } |
| 4520 | else |
| 4521 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4522 | sub->list.multi[subidx].start_lnum = reglnum; |
| 4523 | sub->list.multi[subidx].start_col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4524 | (colnr_T)(reginput - regline + off); |
| 4525 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4526 | } |
| 4527 | else |
| 4528 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4529 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4530 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4531 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4532 | save_in_use = -1; |
| 4533 | } |
| 4534 | else |
| 4535 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4536 | save_in_use = sub->in_use; |
| 4537 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4538 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4539 | sub->list.line[i].start = NULL; |
| 4540 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4541 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4542 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4543 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4544 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4545 | } |
| 4546 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4547 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4548 | /* "subs" may have changed, need to set "sub" again */ |
| 4549 | #ifdef FEAT_SYN_HL |
| 4550 | if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
| 4551 | sub = &subs->synt; |
| 4552 | else |
| 4553 | #endif |
| 4554 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4555 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4556 | if (save_in_use == -1) |
| 4557 | { |
| 4558 | if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4559 | { |
| 4560 | sub->list.multi[subidx].start_lnum = save_lpos.lnum; |
| 4561 | sub->list.multi[subidx].start_col = save_lpos.col; |
| 4562 | } |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4563 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4564 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4565 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4566 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4567 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4568 | break; |
| 4569 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4570 | case NFA_MCLOSE: |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4571 | if (nfa_has_zend && (REG_MULTI |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4572 | ? subs->norm.list.multi[0].end_lnum >= 0 |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4573 | : subs->norm.list.line[0].end != NULL)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4574 | { |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4575 | /* Do not overwrite the position set by \ze. */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4576 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4577 | break; |
| 4578 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4579 | case NFA_MCLOSE1: |
| 4580 | case NFA_MCLOSE2: |
| 4581 | case NFA_MCLOSE3: |
| 4582 | case NFA_MCLOSE4: |
| 4583 | case NFA_MCLOSE5: |
| 4584 | case NFA_MCLOSE6: |
| 4585 | case NFA_MCLOSE7: |
| 4586 | case NFA_MCLOSE8: |
| 4587 | case NFA_MCLOSE9: |
| 4588 | #ifdef FEAT_SYN_HL |
| 4589 | case NFA_ZCLOSE: |
| 4590 | case NFA_ZCLOSE1: |
| 4591 | case NFA_ZCLOSE2: |
| 4592 | case NFA_ZCLOSE3: |
| 4593 | case NFA_ZCLOSE4: |
| 4594 | case NFA_ZCLOSE5: |
| 4595 | case NFA_ZCLOSE6: |
| 4596 | case NFA_ZCLOSE7: |
| 4597 | case NFA_ZCLOSE8: |
| 4598 | case NFA_ZCLOSE9: |
| 4599 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4600 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4601 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4602 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4603 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4604 | sub = &subs->norm; |
| 4605 | } |
| 4606 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4607 | else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4608 | { |
| 4609 | subidx = state->c - NFA_ZCLOSE; |
| 4610 | sub = &subs->synt; |
| 4611 | } |
| 4612 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4613 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4614 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4615 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4616 | sub = &subs->norm; |
| 4617 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4618 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4619 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 4620 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4621 | save_in_use = sub->in_use; |
| 4622 | if (sub->in_use <= subidx) |
| 4623 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4624 | if (REG_MULTI) |
| 4625 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4626 | save_lpos.lnum = sub->list.multi[subidx].end_lnum; |
| 4627 | save_lpos.col = sub->list.multi[subidx].end_col; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4628 | if (off == -1) |
| 4629 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4630 | sub->list.multi[subidx].end_lnum = reglnum + 1; |
| 4631 | sub->list.multi[subidx].end_col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4632 | } |
| 4633 | else |
| 4634 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4635 | sub->list.multi[subidx].end_lnum = reglnum; |
| 4636 | sub->list.multi[subidx].end_col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4637 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4638 | } |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4639 | /* avoid compiler warnings */ |
| 4640 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4641 | } |
| 4642 | else |
| 4643 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4644 | save_ptr = sub->list.line[subidx].end; |
| 4645 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4646 | /* avoid compiler warnings */ |
| 4647 | save_lpos.lnum = 0; |
| 4648 | save_lpos.col = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4649 | } |
| 4650 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4651 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4652 | /* "subs" may have changed, need to set "sub" again */ |
| 4653 | #ifdef FEAT_SYN_HL |
| 4654 | if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
| 4655 | sub = &subs->synt; |
| 4656 | else |
| 4657 | #endif |
| 4658 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4659 | |
| 4660 | if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4661 | { |
| 4662 | sub->list.multi[subidx].end_lnum = save_lpos.lnum; |
| 4663 | sub->list.multi[subidx].end_col = save_lpos.col; |
| 4664 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4665 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4666 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4667 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4668 | break; |
| 4669 | } |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4670 | return subs; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4671 | } |
| 4672 | |
| 4673 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4674 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 4675 | * Used for zero-width matches, next state to use is the added one. |
| 4676 | * This makes sure the order of states to be tried does not change, which |
| 4677 | * matters for alternatives. |
| 4678 | */ |
| 4679 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4680 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4681 | nfa_list_T *l; /* runtime state list */ |
| 4682 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4683 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4684 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4685 | int *ip; |
| 4686 | { |
| 4687 | int tlen = l->n; |
| 4688 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4689 | int listidx = *ip; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4690 | |
| 4691 | /* 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] | 4692 | addstate(l, state, subs, pim, 0); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4693 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4694 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4695 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4696 | return; |
| 4697 | |
| 4698 | /* re-order to put the new state at the current position */ |
| 4699 | count = l->n - tlen; |
Bram Moolenaar | a50d02d | 2013-06-16 15:43:50 +0200 | [diff] [blame] | 4700 | if (count == 0) |
| 4701 | return; /* no state got added */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4702 | if (count == 1) |
| 4703 | { |
| 4704 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4705 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4706 | } |
| 4707 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4708 | { |
Bram Moolenaar | 55480dc | 2013-06-30 13:17:24 +0200 | [diff] [blame] | 4709 | if (l->n + count - 1 >= l->len) |
| 4710 | { |
| 4711 | /* not enough space to move the new states, reallocate the list |
| 4712 | * and move the states to the right position */ |
| 4713 | nfa_thread_T *newl; |
| 4714 | |
| 4715 | l->len = l->len * 3 / 2 + 50; |
| 4716 | newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T)); |
| 4717 | if (newl == NULL) |
| 4718 | return; |
| 4719 | mch_memmove(&(newl[0]), |
| 4720 | &(l->t[0]), |
| 4721 | sizeof(nfa_thread_T) * listidx); |
| 4722 | mch_memmove(&(newl[listidx]), |
| 4723 | &(l->t[l->n - count]), |
| 4724 | sizeof(nfa_thread_T) * count); |
| 4725 | mch_memmove(&(newl[listidx + count]), |
| 4726 | &(l->t[listidx + 1]), |
| 4727 | sizeof(nfa_thread_T) * (l->n - count - listidx - 1)); |
| 4728 | vim_free(l->t); |
| 4729 | l->t = newl; |
| 4730 | } |
| 4731 | else |
| 4732 | { |
| 4733 | /* make space for new states, then move them from the |
| 4734 | * end to the current position */ |
| 4735 | mch_memmove(&(l->t[listidx + count]), |
| 4736 | &(l->t[listidx + 1]), |
| 4737 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 4738 | mch_memmove(&(l->t[listidx]), |
| 4739 | &(l->t[l->n - 1]), |
| 4740 | sizeof(nfa_thread_T) * count); |
| 4741 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4742 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4743 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4744 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4745 | } |
| 4746 | |
| 4747 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4748 | * Check character class "class" against current character c. |
| 4749 | */ |
| 4750 | static int |
| 4751 | check_char_class(class, c) |
| 4752 | int class; |
| 4753 | int c; |
| 4754 | { |
| 4755 | switch (class) |
| 4756 | { |
| 4757 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4758 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4759 | return OK; |
| 4760 | break; |
| 4761 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4762 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4763 | return OK; |
| 4764 | break; |
| 4765 | case NFA_CLASS_BLANK: |
| 4766 | if (c == ' ' || c == '\t') |
| 4767 | return OK; |
| 4768 | break; |
| 4769 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4770 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4771 | return OK; |
| 4772 | break; |
| 4773 | case NFA_CLASS_DIGIT: |
| 4774 | if (VIM_ISDIGIT(c)) |
| 4775 | return OK; |
| 4776 | break; |
| 4777 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4778 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4779 | return OK; |
| 4780 | break; |
| 4781 | case NFA_CLASS_LOWER: |
| 4782 | if (MB_ISLOWER(c)) |
| 4783 | return OK; |
| 4784 | break; |
| 4785 | case NFA_CLASS_PRINT: |
| 4786 | if (vim_isprintc(c)) |
| 4787 | return OK; |
| 4788 | break; |
| 4789 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4790 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4791 | return OK; |
| 4792 | break; |
| 4793 | case NFA_CLASS_SPACE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4794 | if ((c >= 9 && c <= 13) || (c == ' ')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4795 | return OK; |
| 4796 | break; |
| 4797 | case NFA_CLASS_UPPER: |
| 4798 | if (MB_ISUPPER(c)) |
| 4799 | return OK; |
| 4800 | break; |
| 4801 | case NFA_CLASS_XDIGIT: |
| 4802 | if (vim_isxdigit(c)) |
| 4803 | return OK; |
| 4804 | break; |
| 4805 | case NFA_CLASS_TAB: |
| 4806 | if (c == '\t') |
| 4807 | return OK; |
| 4808 | break; |
| 4809 | case NFA_CLASS_RETURN: |
| 4810 | if (c == '\r') |
| 4811 | return OK; |
| 4812 | break; |
| 4813 | case NFA_CLASS_BACKSPACE: |
| 4814 | if (c == '\b') |
| 4815 | return OK; |
| 4816 | break; |
| 4817 | case NFA_CLASS_ESCAPE: |
| 4818 | if (c == '\033') |
| 4819 | return OK; |
| 4820 | break; |
| 4821 | |
| 4822 | default: |
| 4823 | /* should not be here :P */ |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 4824 | EMSGN(_(e_ill_char_class), class); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 4825 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4826 | } |
| 4827 | return FAIL; |
| 4828 | } |
| 4829 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4830 | /* |
| 4831 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4832 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4833 | */ |
| 4834 | static int |
| 4835 | match_backref(sub, subidx, bytelen) |
| 4836 | regsub_T *sub; /* pointers to subexpressions */ |
| 4837 | int subidx; |
| 4838 | int *bytelen; /* out: length of match in bytes */ |
| 4839 | { |
| 4840 | int len; |
| 4841 | |
| 4842 | if (sub->in_use <= subidx) |
| 4843 | { |
| 4844 | retempty: |
| 4845 | /* backref was not set, match an empty string */ |
| 4846 | *bytelen = 0; |
| 4847 | return TRUE; |
| 4848 | } |
| 4849 | |
| 4850 | if (REG_MULTI) |
| 4851 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4852 | if (sub->list.multi[subidx].start_lnum < 0 |
| 4853 | || sub->list.multi[subidx].end_lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4854 | goto retempty; |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4855 | if (sub->list.multi[subidx].start_lnum == reglnum |
| 4856 | && sub->list.multi[subidx].end_lnum == reglnum) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4857 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4858 | len = sub->list.multi[subidx].end_col |
| 4859 | - sub->list.multi[subidx].start_col; |
| 4860 | if (cstrncmp(regline + sub->list.multi[subidx].start_col, |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4861 | reginput, &len) == 0) |
| 4862 | { |
| 4863 | *bytelen = len; |
| 4864 | return TRUE; |
| 4865 | } |
| 4866 | } |
| 4867 | else |
| 4868 | { |
| 4869 | if (match_with_backref( |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4870 | sub->list.multi[subidx].start_lnum, |
| 4871 | sub->list.multi[subidx].start_col, |
| 4872 | sub->list.multi[subidx].end_lnum, |
| 4873 | sub->list.multi[subidx].end_col, |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4874 | bytelen) == RA_MATCH) |
| 4875 | return TRUE; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4876 | } |
| 4877 | } |
| 4878 | else |
| 4879 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4880 | if (sub->list.line[subidx].start == NULL |
| 4881 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4882 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4883 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 4884 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4885 | { |
| 4886 | *bytelen = len; |
| 4887 | return TRUE; |
| 4888 | } |
| 4889 | } |
| 4890 | return FALSE; |
| 4891 | } |
| 4892 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4893 | #ifdef FEAT_SYN_HL |
| 4894 | |
| 4895 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 4896 | |
| 4897 | /* |
| 4898 | * Check for a match with \z subexpression "subidx". |
| 4899 | * Return TRUE if it matches. |
| 4900 | */ |
| 4901 | static int |
| 4902 | match_zref(subidx, bytelen) |
| 4903 | int subidx; |
| 4904 | int *bytelen; /* out: length of match in bytes */ |
| 4905 | { |
| 4906 | int len; |
| 4907 | |
| 4908 | cleanup_zsubexpr(); |
| 4909 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 4910 | { |
| 4911 | /* backref was not set, match an empty string */ |
| 4912 | *bytelen = 0; |
| 4913 | return TRUE; |
| 4914 | } |
| 4915 | |
| 4916 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 4917 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 4918 | { |
| 4919 | *bytelen = len; |
| 4920 | return TRUE; |
| 4921 | } |
| 4922 | return FALSE; |
| 4923 | } |
| 4924 | #endif |
| 4925 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4926 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4927 | * Save list IDs for all NFA states of "prog" into "list". |
| 4928 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4929 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4930 | */ |
| 4931 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4932 | nfa_save_listids(prog, list) |
| 4933 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4934 | int *list; |
| 4935 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4936 | int i; |
| 4937 | nfa_state_T *p; |
| 4938 | |
| 4939 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 4940 | p = &prog->state[0]; |
| 4941 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4942 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4943 | list[i] = p->lastlist[1]; |
| 4944 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4945 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | /* |
| 4950 | * Restore list IDs from "list" to all NFA states. |
| 4951 | */ |
| 4952 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4953 | nfa_restore_listids(prog, list) |
| 4954 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4955 | int *list; |
| 4956 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4957 | int i; |
| 4958 | nfa_state_T *p; |
| 4959 | |
| 4960 | p = &prog->state[0]; |
| 4961 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4962 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4963 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4964 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4965 | } |
| 4966 | } |
| 4967 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4968 | static int |
| 4969 | nfa_re_num_cmp(val, op, pos) |
| 4970 | long_u val; |
| 4971 | int op; |
| 4972 | long_u pos; |
| 4973 | { |
| 4974 | if (op == 1) return pos > val; |
| 4975 | if (op == 2) return pos < val; |
| 4976 | return val == pos; |
| 4977 | } |
| 4978 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4979 | 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] | 4980 | 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] | 4981 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4982 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4983 | * Recursively call nfa_regmatch() |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4984 | * "pim" is NULL or contains info about a Postponed Invisible Match (start |
| 4985 | * position). |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4986 | */ |
| 4987 | static int |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4988 | recursive_regmatch(state, pim, prog, submatch, m, listids) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4989 | nfa_state_T *state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4990 | nfa_pim_T *pim; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4991 | nfa_regprog_T *prog; |
| 4992 | regsubs_T *submatch; |
| 4993 | regsubs_T *m; |
| 4994 | int **listids; |
| 4995 | { |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4996 | int save_reginput_col = (int)(reginput - regline); |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4997 | int save_reglnum = reglnum; |
| 4998 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4999 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5000 | save_se_T *save_nfa_endp = nfa_endp; |
| 5001 | save_se_T endpos; |
| 5002 | save_se_T *endposp = NULL; |
| 5003 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5004 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5005 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5006 | if (pim != NULL) |
| 5007 | { |
| 5008 | /* start at the position where the postponed match was */ |
| 5009 | if (REG_MULTI) |
| 5010 | reginput = regline + pim->end.pos.col; |
| 5011 | else |
| 5012 | reginput = pim->end.ptr; |
| 5013 | } |
| 5014 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5015 | if (state->c == NFA_START_INVISIBLE_BEFORE |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5016 | || state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 5017 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG |
| 5018 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5019 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5020 | /* The recursive match must end at the current position. When "pim" is |
| 5021 | * not NULL it specifies the current position. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5022 | endposp = &endpos; |
| 5023 | if (REG_MULTI) |
| 5024 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5025 | if (pim == NULL) |
| 5026 | { |
| 5027 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 5028 | endpos.se_u.pos.lnum = reglnum; |
| 5029 | } |
| 5030 | else |
| 5031 | endpos.se_u.pos = pim->end.pos; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5032 | } |
| 5033 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5034 | { |
| 5035 | if (pim == NULL) |
| 5036 | endpos.se_u.ptr = reginput; |
| 5037 | else |
| 5038 | endpos.se_u.ptr = pim->end.ptr; |
| 5039 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5040 | |
| 5041 | /* Go back the specified number of bytes, or as far as the |
| 5042 | * start of the previous line, to try matching "\@<=" or |
Bram Moolenaar | 3fb14bc | 2013-07-14 12:34:56 +0200 | [diff] [blame] | 5043 | * not matching "\@<!". This is very inefficient, limit the number of |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 5044 | * bytes if possible. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5045 | if (state->val <= 0) |
| 5046 | { |
| 5047 | if (REG_MULTI) |
| 5048 | { |
| 5049 | regline = reg_getline(--reglnum); |
| 5050 | if (regline == NULL) |
| 5051 | /* can't go before the first line */ |
| 5052 | regline = reg_getline(++reglnum); |
| 5053 | } |
| 5054 | reginput = regline; |
| 5055 | } |
| 5056 | else |
| 5057 | { |
| 5058 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 5059 | { |
| 5060 | /* Not enough bytes in this line, go to end of |
| 5061 | * previous line. */ |
| 5062 | regline = reg_getline(--reglnum); |
| 5063 | if (regline == NULL) |
| 5064 | { |
| 5065 | /* can't go before the first line */ |
| 5066 | regline = reg_getline(++reglnum); |
| 5067 | reginput = regline; |
| 5068 | } |
| 5069 | else |
| 5070 | reginput = regline + STRLEN(regline); |
| 5071 | } |
| 5072 | if ((int)(reginput - regline) >= state->val) |
| 5073 | { |
| 5074 | reginput -= state->val; |
| 5075 | #ifdef FEAT_MBYTE |
| 5076 | if (has_mbyte) |
| 5077 | reginput -= mb_head_off(regline, reginput); |
| 5078 | #endif |
| 5079 | } |
| 5080 | else |
| 5081 | reginput = regline; |
| 5082 | } |
| 5083 | } |
| 5084 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5085 | #ifdef ENABLE_LOG |
| 5086 | if (log_fd != stderr) |
| 5087 | fclose(log_fd); |
| 5088 | log_fd = NULL; |
| 5089 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5090 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 5091 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 5092 | if (nfa_ll_index == 1) |
| 5093 | { |
| 5094 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 5095 | * values and clear them. */ |
| 5096 | if (*listids == NULL) |
| 5097 | { |
| 5098 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 5099 | if (*listids == NULL) |
| 5100 | { |
| 5101 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 5102 | return 0; |
| 5103 | } |
| 5104 | } |
| 5105 | nfa_save_listids(prog, *listids); |
| 5106 | need_restore = TRUE; |
| 5107 | /* any value of nfa_listid will do */ |
| 5108 | } |
| 5109 | else |
| 5110 | { |
| 5111 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 5112 | * entry. Make sure nfa_listid is different from a previous recursive |
| 5113 | * call, because some states may still have this ID. */ |
| 5114 | ++nfa_ll_index; |
| 5115 | if (nfa_listid <= nfa_alt_listid) |
| 5116 | nfa_listid = nfa_alt_listid; |
| 5117 | } |
| 5118 | |
| 5119 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 5120 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5121 | nfa_endp = endposp; |
| 5122 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5123 | |
| 5124 | if (need_restore) |
| 5125 | nfa_restore_listids(prog, *listids); |
| 5126 | else |
| 5127 | { |
| 5128 | --nfa_ll_index; |
| 5129 | nfa_alt_listid = nfa_listid; |
| 5130 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5131 | |
| 5132 | /* restore position in input text */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5133 | reglnum = save_reglnum; |
Bram Moolenaar | 484d241 | 2013-06-13 19:47:07 +0200 | [diff] [blame] | 5134 | if (REG_MULTI) |
| 5135 | regline = reg_getline(reglnum); |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 5136 | reginput = regline + save_reginput_col; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5137 | nfa_match = save_nfa_match; |
| 5138 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5139 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5140 | |
| 5141 | #ifdef ENABLE_LOG |
| 5142 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 5143 | if (log_fd != NULL) |
| 5144 | { |
| 5145 | fprintf(log_fd, "****************************\n"); |
| 5146 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 5147 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5148 | fprintf(log_fd, "****************************\n"); |
| 5149 | } |
| 5150 | else |
| 5151 | { |
| 5152 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5153 | log_fd = stderr; |
| 5154 | } |
| 5155 | #endif |
| 5156 | |
| 5157 | return result; |
| 5158 | } |
| 5159 | |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5160 | static int skip_to_start __ARGS((int c, colnr_T *colp)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5161 | 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] | 5162 | |
| 5163 | /* |
| 5164 | * Estimate the chance of a match with "state" failing. |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5165 | * empty match: 0 |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5166 | * NFA_ANY: 1 |
| 5167 | * specific character: 99 |
| 5168 | */ |
| 5169 | static int |
| 5170 | failure_chance(state, depth) |
| 5171 | nfa_state_T *state; |
| 5172 | int depth; |
| 5173 | { |
| 5174 | int c = state->c; |
| 5175 | int l, r; |
| 5176 | |
| 5177 | /* detect looping */ |
| 5178 | if (depth > 4) |
| 5179 | return 1; |
| 5180 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5181 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5182 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5183 | case NFA_SPLIT: |
| 5184 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 5185 | /* avoid recursive stuff */ |
| 5186 | return 1; |
| 5187 | /* two alternatives, use the lowest failure chance */ |
| 5188 | l = failure_chance(state->out, depth + 1); |
| 5189 | r = failure_chance(state->out1, depth + 1); |
| 5190 | return l < r ? l : r; |
| 5191 | |
| 5192 | case NFA_ANY: |
| 5193 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5194 | return 1; |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5195 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5196 | case NFA_MATCH: |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5197 | case NFA_MCLOSE: |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 5198 | case NFA_ANY_COMPOSING: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5199 | /* empty match works always */ |
| 5200 | return 0; |
| 5201 | |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5202 | case NFA_START_INVISIBLE: |
| 5203 | case NFA_START_INVISIBLE_FIRST: |
| 5204 | case NFA_START_INVISIBLE_NEG: |
| 5205 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 5206 | case NFA_START_INVISIBLE_BEFORE: |
| 5207 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 5208 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 5209 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 5210 | case NFA_START_PATTERN: |
| 5211 | /* recursive regmatch is expensive, use low failure chance */ |
| 5212 | return 5; |
| 5213 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5214 | case NFA_BOL: |
| 5215 | case NFA_EOL: |
| 5216 | case NFA_BOF: |
| 5217 | case NFA_EOF: |
| 5218 | case NFA_NEWL: |
| 5219 | return 99; |
| 5220 | |
| 5221 | case NFA_BOW: |
| 5222 | case NFA_EOW: |
| 5223 | return 90; |
| 5224 | |
| 5225 | case NFA_MOPEN: |
| 5226 | case NFA_MOPEN1: |
| 5227 | case NFA_MOPEN2: |
| 5228 | case NFA_MOPEN3: |
| 5229 | case NFA_MOPEN4: |
| 5230 | case NFA_MOPEN5: |
| 5231 | case NFA_MOPEN6: |
| 5232 | case NFA_MOPEN7: |
| 5233 | case NFA_MOPEN8: |
| 5234 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5235 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5236 | case NFA_ZOPEN: |
| 5237 | case NFA_ZOPEN1: |
| 5238 | case NFA_ZOPEN2: |
| 5239 | case NFA_ZOPEN3: |
| 5240 | case NFA_ZOPEN4: |
| 5241 | case NFA_ZOPEN5: |
| 5242 | case NFA_ZOPEN6: |
| 5243 | case NFA_ZOPEN7: |
| 5244 | case NFA_ZOPEN8: |
| 5245 | case NFA_ZOPEN9: |
| 5246 | case NFA_ZCLOSE: |
| 5247 | case NFA_ZCLOSE1: |
| 5248 | case NFA_ZCLOSE2: |
| 5249 | case NFA_ZCLOSE3: |
| 5250 | case NFA_ZCLOSE4: |
| 5251 | case NFA_ZCLOSE5: |
| 5252 | case NFA_ZCLOSE6: |
| 5253 | case NFA_ZCLOSE7: |
| 5254 | case NFA_ZCLOSE8: |
| 5255 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5256 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5257 | case NFA_NOPEN: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5258 | case NFA_MCLOSE1: |
| 5259 | case NFA_MCLOSE2: |
| 5260 | case NFA_MCLOSE3: |
| 5261 | case NFA_MCLOSE4: |
| 5262 | case NFA_MCLOSE5: |
| 5263 | case NFA_MCLOSE6: |
| 5264 | case NFA_MCLOSE7: |
| 5265 | case NFA_MCLOSE8: |
| 5266 | case NFA_MCLOSE9: |
| 5267 | case NFA_NCLOSE: |
| 5268 | return failure_chance(state->out, depth + 1); |
| 5269 | |
| 5270 | case NFA_BACKREF1: |
| 5271 | case NFA_BACKREF2: |
| 5272 | case NFA_BACKREF3: |
| 5273 | case NFA_BACKREF4: |
| 5274 | case NFA_BACKREF5: |
| 5275 | case NFA_BACKREF6: |
| 5276 | case NFA_BACKREF7: |
| 5277 | case NFA_BACKREF8: |
| 5278 | case NFA_BACKREF9: |
| 5279 | #ifdef FEAT_SYN_HL |
| 5280 | case NFA_ZREF1: |
| 5281 | case NFA_ZREF2: |
| 5282 | case NFA_ZREF3: |
| 5283 | case NFA_ZREF4: |
| 5284 | case NFA_ZREF5: |
| 5285 | case NFA_ZREF6: |
| 5286 | case NFA_ZREF7: |
| 5287 | case NFA_ZREF8: |
| 5288 | case NFA_ZREF9: |
| 5289 | #endif |
| 5290 | /* backreferences don't match in many places */ |
| 5291 | return 94; |
| 5292 | |
| 5293 | case NFA_LNUM_GT: |
| 5294 | case NFA_LNUM_LT: |
| 5295 | case NFA_COL_GT: |
| 5296 | case NFA_COL_LT: |
| 5297 | case NFA_VCOL_GT: |
| 5298 | case NFA_VCOL_LT: |
| 5299 | case NFA_MARK_GT: |
| 5300 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5301 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5302 | /* before/after positions don't match very often */ |
| 5303 | return 85; |
| 5304 | |
| 5305 | case NFA_LNUM: |
| 5306 | return 90; |
| 5307 | |
| 5308 | case NFA_CURSOR: |
| 5309 | case NFA_COL: |
| 5310 | case NFA_VCOL: |
| 5311 | case NFA_MARK: |
| 5312 | /* specific positions rarely match */ |
| 5313 | return 98; |
| 5314 | |
| 5315 | case NFA_COMPOSING: |
| 5316 | return 95; |
| 5317 | |
| 5318 | default: |
| 5319 | if (c > 0) |
| 5320 | /* character match fails often */ |
| 5321 | return 95; |
| 5322 | } |
| 5323 | |
| 5324 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5325 | return 50; |
| 5326 | } |
| 5327 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5328 | /* |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5329 | * Skip until the char "c" we know a match must start with. |
| 5330 | */ |
| 5331 | static int |
| 5332 | skip_to_start(c, colp) |
| 5333 | int c; |
| 5334 | colnr_T *colp; |
| 5335 | { |
| 5336 | char_u *s; |
| 5337 | |
| 5338 | /* Used often, do some work to avoid call overhead. */ |
| 5339 | if (!ireg_ic |
| 5340 | #ifdef FEAT_MBYTE |
| 5341 | && !has_mbyte |
| 5342 | #endif |
| 5343 | ) |
| 5344 | s = vim_strbyte(regline + *colp, c); |
| 5345 | else |
| 5346 | s = cstrchr(regline + *colp, c); |
| 5347 | if (s == NULL) |
| 5348 | return FAIL; |
| 5349 | *colp = (int)(s - regline); |
| 5350 | return OK; |
| 5351 | } |
| 5352 | |
| 5353 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5354 | * Check for a match with match_text. |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 5355 | * Called after skip_to_start() has found regstart. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5356 | * Returns zero for no match, 1 for a match. |
| 5357 | */ |
| 5358 | static long |
| 5359 | find_match_text(startcol, regstart, match_text) |
| 5360 | colnr_T startcol; |
| 5361 | int regstart; |
| 5362 | char_u *match_text; |
| 5363 | { |
| 5364 | colnr_T col = startcol; |
| 5365 | int c1, c2; |
| 5366 | int len1, len2; |
| 5367 | int match; |
| 5368 | |
| 5369 | for (;;) |
| 5370 | { |
| 5371 | match = TRUE; |
| 5372 | len2 = MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5373 | for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
| 5374 | { |
| 5375 | c1 = PTR2CHAR(match_text + len1); |
| 5376 | c2 = PTR2CHAR(regline + col + len2); |
| 5377 | if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
| 5378 | { |
| 5379 | match = FALSE; |
| 5380 | break; |
| 5381 | } |
| 5382 | len2 += MB_CHAR2LEN(c2); |
| 5383 | } |
| 5384 | if (match |
| 5385 | #ifdef FEAT_MBYTE |
| 5386 | /* check that no composing char follows */ |
| 5387 | && !(enc_utf8 |
| 5388 | && utf_iscomposing(PTR2CHAR(regline + col + len2))) |
| 5389 | #endif |
| 5390 | ) |
| 5391 | { |
| 5392 | cleanup_subexpr(); |
| 5393 | if (REG_MULTI) |
| 5394 | { |
| 5395 | reg_startpos[0].lnum = reglnum; |
| 5396 | reg_startpos[0].col = col; |
| 5397 | reg_endpos[0].lnum = reglnum; |
| 5398 | reg_endpos[0].col = col + len2; |
| 5399 | } |
| 5400 | else |
| 5401 | { |
| 5402 | reg_startp[0] = regline + col; |
| 5403 | reg_endp[0] = regline + col + len2; |
| 5404 | } |
| 5405 | return 1L; |
| 5406 | } |
| 5407 | |
| 5408 | /* Try finding regstart after the current match. */ |
| 5409 | col += MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5410 | if (skip_to_start(regstart, &col) == FAIL) |
| 5411 | break; |
| 5412 | } |
| 5413 | return 0L; |
| 5414 | } |
| 5415 | |
| 5416 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5417 | * Main matching routine. |
| 5418 | * |
| 5419 | * Run NFA to determine whether it matches reginput. |
| 5420 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5421 | * 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] | 5422 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5423 | * Return TRUE if there is a match, FALSE otherwise. |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5424 | * When there is a match "submatch" contains the positions. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5425 | * Note: Caller must ensure that: start != NULL. |
| 5426 | */ |
| 5427 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5428 | nfa_regmatch(prog, start, submatch, m) |
| 5429 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5430 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5431 | regsubs_T *submatch; |
| 5432 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5433 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5434 | int result; |
Bram Moolenaar | aaf3047 | 2015-01-27 14:40:00 +0100 | [diff] [blame] | 5435 | size_t size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5436 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5437 | int go_to_nextline = FALSE; |
| 5438 | nfa_thread_T *t; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5439 | nfa_list_T list[2]; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5440 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5441 | nfa_list_T *thislist; |
| 5442 | nfa_list_T *nextlist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5443 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5444 | nfa_state_T *add_state; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5445 | int add_here; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5446 | int add_count; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 5447 | int add_off = 0; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5448 | int toplevel = start->c == NFA_MOPEN; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5449 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5450 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5451 | |
| 5452 | if (debug == NULL) |
| 5453 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5454 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5455 | return FALSE; |
| 5456 | } |
| 5457 | #endif |
Bram Moolenaar | 41f1205 | 2013-08-25 17:01:42 +0200 | [diff] [blame] | 5458 | /* Some patterns may take a long time to match, especially when using |
| 5459 | * recursive_regmatch(). Allow interrupting them with CTRL-C. */ |
| 5460 | fast_breakcheck(); |
| 5461 | if (got_int) |
| 5462 | return FALSE; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 5463 | #ifdef FEAT_RELTIME |
| 5464 | if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit)) |
| 5465 | return FALSE; |
| 5466 | #endif |
Bram Moolenaar | 41f1205 | 2013-08-25 17:01:42 +0200 | [diff] [blame] | 5467 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5468 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5469 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5470 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5471 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5472 | |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5473 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5474 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5475 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5476 | list[1].len = nstate + 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5477 | if (list[0].t == NULL || list[1].t == NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5478 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5479 | |
| 5480 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5481 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5482 | if (log_fd != NULL) |
| 5483 | { |
| 5484 | fprintf(log_fd, "**********************************\n"); |
| 5485 | nfa_set_code(start->c); |
| 5486 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 5487 | abs(start->id), code); |
| 5488 | fprintf(log_fd, "**********************************\n"); |
| 5489 | } |
| 5490 | else |
| 5491 | { |
| 5492 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5493 | log_fd = stderr; |
| 5494 | } |
| 5495 | #endif |
| 5496 | |
| 5497 | thislist = &list[0]; |
| 5498 | thislist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5499 | thislist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5500 | nextlist = &list[1]; |
| 5501 | nextlist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5502 | nextlist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5503 | #ifdef ENABLE_LOG |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5504 | fprintf(log_fd, "(---) STARTSTATE first\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5505 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5506 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5507 | |
| 5508 | /* Inline optimized code for addstate(thislist, start, m, 0) if we know |
| 5509 | * it's the first MOPEN. */ |
| 5510 | if (toplevel) |
| 5511 | { |
| 5512 | if (REG_MULTI) |
| 5513 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5514 | m->norm.list.multi[0].start_lnum = reglnum; |
| 5515 | m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5516 | } |
| 5517 | else |
| 5518 | m->norm.list.line[0].start = reginput; |
| 5519 | m->norm.in_use = 1; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5520 | addstate(thislist, start->out, m, NULL, 0); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5521 | } |
| 5522 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5523 | addstate(thislist, start, m, NULL, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5524 | |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5525 | #define ADD_STATE_IF_MATCH(state) \ |
| 5526 | if (result) { \ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5527 | add_state = state->out; \ |
| 5528 | add_off = clen; \ |
| 5529 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5530 | |
| 5531 | /* |
| 5532 | * Run for each character. |
| 5533 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5534 | for (;;) |
| 5535 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5536 | int curc; |
| 5537 | int clen; |
| 5538 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5539 | #ifdef FEAT_MBYTE |
| 5540 | if (has_mbyte) |
| 5541 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5542 | curc = (*mb_ptr2char)(reginput); |
| 5543 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5544 | } |
| 5545 | else |
| 5546 | #endif |
| 5547 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5548 | curc = *reginput; |
| 5549 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5550 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5551 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5552 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5553 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5554 | go_to_nextline = FALSE; |
| 5555 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5556 | |
| 5557 | /* swap lists */ |
| 5558 | thislist = &list[flag]; |
| 5559 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5560 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5561 | nextlist->has_pim = FALSE; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5562 | ++nfa_listid; |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 5563 | if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES) |
| 5564 | { |
| 5565 | /* too many states, retry with old engine */ |
| 5566 | nfa_match = NFA_TOO_EXPENSIVE; |
| 5567 | goto theend; |
| 5568 | } |
| 5569 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5570 | thislist->id = nfa_listid; |
| 5571 | nextlist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5572 | |
| 5573 | #ifdef ENABLE_LOG |
| 5574 | fprintf(log_fd, "------------------------------------------\n"); |
| 5575 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5576 | 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] | 5577 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5578 | { |
| 5579 | int i; |
| 5580 | |
| 5581 | for (i = 0; i < thislist->n; i++) |
| 5582 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5583 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5584 | fprintf(log_fd, "\n"); |
| 5585 | #endif |
| 5586 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5587 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5588 | fprintf(debug, "\n-------------------\n"); |
| 5589 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5590 | /* |
| 5591 | * If the state lists are empty we can stop. |
| 5592 | */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5593 | if (thislist->n == 0) |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5594 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5595 | |
| 5596 | /* compute nextlist */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5597 | for (listidx = 0; listidx < thislist->n; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5598 | { |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5599 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5600 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5601 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5602 | nfa_set_code(t->state->c); |
| 5603 | fprintf(debug, "%s, ", code); |
| 5604 | #endif |
| 5605 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5606 | { |
| 5607 | int col; |
| 5608 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5609 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5610 | col = -1; |
| 5611 | else if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5612 | col = t->subs.norm.list.multi[0].start_col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5613 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5614 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5615 | nfa_set_code(t->state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5616 | fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n", |
| 5617 | abs(t->state->id), (int)t->state->c, code, col, |
| 5618 | pim_info(&t->pim)); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5619 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5620 | #endif |
| 5621 | |
| 5622 | /* |
| 5623 | * Handle the possible codes of the current state. |
| 5624 | * The most important is NFA_MATCH. |
| 5625 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5626 | add_state = NULL; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5627 | add_here = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5628 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5629 | switch (t->state->c) |
| 5630 | { |
| 5631 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5632 | { |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 5633 | #ifdef FEAT_MBYTE |
| 5634 | /* If the match ends before a composing characters and |
| 5635 | * ireg_icombine is not set, that is not really a match. */ |
| 5636 | if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc)) |
| 5637 | break; |
| 5638 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5639 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5640 | copy_sub(&submatch->norm, &t->subs.norm); |
| 5641 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5642 | if (nfa_has_zsubexpr) |
| 5643 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5644 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5645 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5646 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5647 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5648 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5649 | * states at this position. When the list of states is going |
| 5650 | * to be empty quit without advancing, so that "reginput" is |
| 5651 | * correct. */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5652 | if (nextlist->n == 0) |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5653 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5654 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5655 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5656 | |
| 5657 | case NFA_END_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5658 | case NFA_END_INVISIBLE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5659 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5660 | /* |
| 5661 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5662 | * NFA_START_INVISIBLE_BEFORE node. |
| 5663 | * They surround a zero-width group, used with "\@=", "\&", |
| 5664 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5665 | * If we got here, it means that the current "invisible" group |
| 5666 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5667 | * nfa_regmatch(). For a look-behind match only when it ends |
| 5668 | * in the position in "nfa_endp". |
| 5669 | * Submatches are stored in *m, and used in the parent call. |
| 5670 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5671 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5672 | if (nfa_endp != NULL) |
| 5673 | { |
| 5674 | if (REG_MULTI) |
| 5675 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 5676 | (int)reglnum, |
| 5677 | (int)nfa_endp->se_u.pos.lnum, |
| 5678 | (int)(reginput - regline), |
| 5679 | nfa_endp->se_u.pos.col); |
| 5680 | else |
| 5681 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 5682 | (int)(reginput - regline), |
| 5683 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5684 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5685 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5686 | /* If "nfa_endp" is set it's only a match if it ends at |
| 5687 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5688 | if (nfa_endp != NULL && (REG_MULTI |
| 5689 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 5690 | || (int)(reginput - regline) |
| 5691 | != nfa_endp->se_u.pos.col) |
| 5692 | : reginput != nfa_endp->se_u.ptr)) |
| 5693 | break; |
| 5694 | |
| 5695 | /* do not set submatches for \@! */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5696 | if (t->state->c != NFA_END_INVISIBLE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5697 | { |
| 5698 | copy_sub(&m->norm, &t->subs.norm); |
| 5699 | #ifdef FEAT_SYN_HL |
| 5700 | if (nfa_has_zsubexpr) |
| 5701 | copy_sub(&m->synt, &t->subs.synt); |
| 5702 | #endif |
| 5703 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5704 | #ifdef ENABLE_LOG |
| 5705 | fprintf(log_fd, "Match found:\n"); |
| 5706 | log_subsexpr(m); |
| 5707 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5708 | nfa_match = TRUE; |
Bram Moolenaar | 78c93e4 | 2013-09-05 16:05:36 +0200 | [diff] [blame] | 5709 | /* See comment above at "goto nextchar". */ |
| 5710 | if (nextlist->n == 0) |
| 5711 | clen = 0; |
| 5712 | goto nextchar; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5713 | |
| 5714 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5715 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5716 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5717 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5718 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5719 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5720 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5721 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5722 | { |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5723 | #ifdef ENABLE_LOG |
| 5724 | fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", |
| 5725 | failure_chance(t->state->out, 0), |
| 5726 | failure_chance(t->state->out1->out, 0)); |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5727 | #endif |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5728 | /* Do it directly if there already is a PIM or when |
| 5729 | * nfa_postprocess() detected it will work better. */ |
| 5730 | if (t->pim.result != NFA_PIM_UNUSED |
| 5731 | || t->state->c == NFA_START_INVISIBLE_FIRST |
| 5732 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5733 | || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 5734 | || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5735 | { |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5736 | int in_use = m->norm.in_use; |
| 5737 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5738 | /* Copy submatch info for the recursive call, opposite |
| 5739 | * of what happens on success below. */ |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5740 | copy_sub_off(&m->norm, &t->subs.norm); |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5741 | #ifdef FEAT_SYN_HL |
| 5742 | if (nfa_has_zsubexpr) |
| 5743 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5744 | #endif |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5745 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5746 | /* |
| 5747 | * First try matching the invisible match, then what |
| 5748 | * follows. |
| 5749 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5750 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5751 | submatch, m, &listids); |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 5752 | if (result == NFA_TOO_EXPENSIVE) |
| 5753 | { |
| 5754 | nfa_match = result; |
| 5755 | goto theend; |
| 5756 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5757 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5758 | /* for \@! and \@<! it is a match when the result is |
| 5759 | * FALSE */ |
| 5760 | if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5761 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5762 | || t->state->c |
| 5763 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5764 | || t->state->c |
| 5765 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5766 | { |
| 5767 | /* Copy submatch info from the recursive call */ |
| 5768 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5769 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5770 | if (nfa_has_zsubexpr) |
| 5771 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5772 | #endif |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5773 | /* If the pattern has \ze and it matched in the |
| 5774 | * sub pattern, use it. */ |
| 5775 | copy_ze_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5776 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5777 | /* t->state->out1 is the corresponding |
| 5778 | * END_INVISIBLE node; Add its out to the current |
| 5779 | * list (zero-width match). */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5780 | add_here = TRUE; |
| 5781 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5782 | } |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5783 | m->norm.in_use = in_use; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5784 | } |
| 5785 | else |
| 5786 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5787 | nfa_pim_T pim; |
| 5788 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5789 | /* |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5790 | * First try matching what follows. Only if a match |
| 5791 | * is found verify the invisible match matches. Add a |
| 5792 | * nfa_pim_T to the following states, it contains info |
| 5793 | * about the invisible match. |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5794 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5795 | pim.state = t->state; |
| 5796 | pim.result = NFA_PIM_TODO; |
| 5797 | pim.subs.norm.in_use = 0; |
| 5798 | #ifdef FEAT_SYN_HL |
| 5799 | pim.subs.synt.in_use = 0; |
| 5800 | #endif |
| 5801 | if (REG_MULTI) |
| 5802 | { |
| 5803 | pim.end.pos.col = (int)(reginput - regline); |
| 5804 | pim.end.pos.lnum = reglnum; |
| 5805 | } |
| 5806 | else |
| 5807 | pim.end.ptr = reginput; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5808 | |
| 5809 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 5810 | * node; Add its out to the current list (zero-width |
| 5811 | * match). */ |
| 5812 | addstate_here(thislist, t->state->out1->out, &t->subs, |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5813 | &pim, &listidx); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5814 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5815 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5816 | break; |
| 5817 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5818 | case NFA_START_PATTERN: |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5819 | { |
| 5820 | nfa_state_T *skip = NULL; |
| 5821 | #ifdef ENABLE_LOG |
| 5822 | int skip_lid = 0; |
| 5823 | #endif |
| 5824 | |
| 5825 | /* There is no point in trying to match the pattern if the |
| 5826 | * output state is not going to be added to the list. */ |
| 5827 | if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
| 5828 | { |
| 5829 | skip = t->state->out1->out; |
| 5830 | #ifdef ENABLE_LOG |
| 5831 | skip_lid = nextlist->id; |
| 5832 | #endif |
| 5833 | } |
| 5834 | else if (state_in_list(nextlist, |
| 5835 | t->state->out1->out->out, &t->subs)) |
| 5836 | { |
| 5837 | skip = t->state->out1->out->out; |
| 5838 | #ifdef ENABLE_LOG |
| 5839 | skip_lid = nextlist->id; |
| 5840 | #endif |
| 5841 | } |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5842 | else if (state_in_list(thislist, |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5843 | t->state->out1->out->out, &t->subs)) |
| 5844 | { |
| 5845 | skip = t->state->out1->out->out; |
| 5846 | #ifdef ENABLE_LOG |
| 5847 | skip_lid = thislist->id; |
| 5848 | #endif |
| 5849 | } |
| 5850 | if (skip != NULL) |
| 5851 | { |
| 5852 | #ifdef ENABLE_LOG |
| 5853 | nfa_set_code(skip->c); |
| 5854 | fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
| 5855 | abs(skip->id), skip_lid, skip->c, code); |
| 5856 | #endif |
| 5857 | break; |
| 5858 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5859 | /* Copy submatch info to the recursive call, opposite of what |
| 5860 | * happens afterwards. */ |
| 5861 | copy_sub_off(&m->norm, &t->subs.norm); |
| 5862 | #ifdef FEAT_SYN_HL |
| 5863 | if (nfa_has_zsubexpr) |
| 5864 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5865 | #endif |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5866 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5867 | /* First try matching the pattern. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5868 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5869 | submatch, m, &listids); |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 5870 | if (result == NFA_TOO_EXPENSIVE) |
| 5871 | { |
| 5872 | nfa_match = result; |
| 5873 | goto theend; |
| 5874 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5875 | if (result) |
| 5876 | { |
| 5877 | int bytelen; |
| 5878 | |
| 5879 | #ifdef ENABLE_LOG |
| 5880 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 5881 | log_subsexpr(m); |
| 5882 | #endif |
| 5883 | /* Copy submatch info from the recursive call */ |
| 5884 | copy_sub_off(&t->subs.norm, &m->norm); |
| 5885 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5886 | if (nfa_has_zsubexpr) |
| 5887 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5888 | #endif |
| 5889 | /* Now we need to skip over the matched text and then |
| 5890 | * continue with what follows. */ |
| 5891 | if (REG_MULTI) |
| 5892 | /* TODO: multi-line match */ |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5893 | bytelen = m->norm.list.multi[0].end_col |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5894 | - (int)(reginput - regline); |
| 5895 | else |
| 5896 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 5897 | |
| 5898 | #ifdef ENABLE_LOG |
| 5899 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 5900 | #endif |
| 5901 | if (bytelen == 0) |
| 5902 | { |
| 5903 | /* empty match, output of corresponding |
| 5904 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 5905 | * position */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5906 | add_here = TRUE; |
| 5907 | add_state = t->state->out1->out->out; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5908 | } |
| 5909 | else if (bytelen <= clen) |
| 5910 | { |
| 5911 | /* match current character, output of corresponding |
| 5912 | * NFA_END_PATTERN to be used at next position. */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5913 | add_state = t->state->out1->out->out; |
| 5914 | add_off = clen; |
| 5915 | } |
| 5916 | else |
| 5917 | { |
| 5918 | /* skip over the matched characters, set character |
| 5919 | * count in NFA_SKIP */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5920 | add_state = t->state->out1->out; |
| 5921 | add_off = bytelen; |
| 5922 | add_count = bytelen - clen; |
| 5923 | } |
| 5924 | } |
| 5925 | break; |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5926 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5927 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5928 | case NFA_BOL: |
| 5929 | if (reginput == regline) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5930 | { |
| 5931 | add_here = TRUE; |
| 5932 | add_state = t->state->out; |
| 5933 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5934 | break; |
| 5935 | |
| 5936 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5937 | if (curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5938 | { |
| 5939 | add_here = TRUE; |
| 5940 | add_state = t->state->out; |
| 5941 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5942 | break; |
| 5943 | |
| 5944 | case NFA_BOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5945 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5946 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5947 | if (curc == NUL) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5948 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5949 | #ifdef FEAT_MBYTE |
| 5950 | else if (has_mbyte) |
| 5951 | { |
| 5952 | int this_class; |
| 5953 | |
| 5954 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5955 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5956 | if (this_class <= 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5957 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5958 | else if (reg_prev_class() == this_class) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5959 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5960 | } |
| 5961 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5962 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5963 | || (reginput > regline |
| 5964 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5965 | result = FALSE; |
| 5966 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5967 | { |
| 5968 | add_here = TRUE; |
| 5969 | add_state = t->state->out; |
| 5970 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5971 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5972 | |
| 5973 | case NFA_EOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5974 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5975 | if (reginput == regline) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5976 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5977 | #ifdef FEAT_MBYTE |
| 5978 | else if (has_mbyte) |
| 5979 | { |
| 5980 | int this_class, prev_class; |
| 5981 | |
| 5982 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5983 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5984 | prev_class = reg_prev_class(); |
| 5985 | if (this_class == prev_class |
| 5986 | || prev_class == 0 || prev_class == 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5987 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5988 | } |
| 5989 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5990 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5991 | || (reginput[0] != NUL |
| 5992 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5993 | result = FALSE; |
| 5994 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5995 | { |
| 5996 | add_here = TRUE; |
| 5997 | add_state = t->state->out; |
| 5998 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5999 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6000 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 6001 | case NFA_BOF: |
| 6002 | if (reglnum == 0 && reginput == regline |
| 6003 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6004 | { |
| 6005 | add_here = TRUE; |
| 6006 | add_state = t->state->out; |
| 6007 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 6008 | break; |
| 6009 | |
| 6010 | case NFA_EOF: |
| 6011 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6012 | { |
| 6013 | add_here = TRUE; |
| 6014 | add_state = t->state->out; |
| 6015 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 6016 | break; |
| 6017 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6018 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6019 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6020 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6021 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6022 | int len = 0; |
| 6023 | nfa_state_T *end; |
| 6024 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 6025 | int cchars[MAX_MCO]; |
| 6026 | int ccount = 0; |
| 6027 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6028 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6029 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6030 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 6031 | if (utf_iscomposing(sta->c)) |
| 6032 | { |
| 6033 | /* Only match composing character(s), ignore base |
| 6034 | * character. Used for ".{composing}" and "{composing}" |
| 6035 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6036 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 6037 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 6038 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6039 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 6040 | /* If \Z was present, then ignore composing characters. |
| 6041 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6042 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 6043 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 6044 | else |
| 6045 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 6046 | while (sta->c != NFA_END_COMPOSING) |
| 6047 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6048 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 6049 | |
| 6050 | /* Check base character matches first, unless ignored. */ |
| 6051 | else if (len > 0 || mc == sta->c) |
| 6052 | { |
| 6053 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 6054 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 6055 | len += mb_char2len(mc); |
| 6056 | sta = sta->out; |
| 6057 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6058 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 6059 | /* We don't care about the order of composing characters. |
| 6060 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6061 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 6062 | { |
| 6063 | mc = mb_ptr2char(reginput + len); |
| 6064 | cchars[ccount++] = mc; |
| 6065 | len += mb_char2len(mc); |
| 6066 | if (ccount == MAX_MCO) |
| 6067 | break; |
| 6068 | } |
| 6069 | |
| 6070 | /* Check that each composing char in the pattern matches a |
| 6071 | * composing char in the text. We do not check if all |
| 6072 | * composing chars are matched. */ |
| 6073 | result = OK; |
| 6074 | while (sta->c != NFA_END_COMPOSING) |
| 6075 | { |
| 6076 | for (j = 0; j < ccount; ++j) |
| 6077 | if (cchars[j] == sta->c) |
| 6078 | break; |
| 6079 | if (j == ccount) |
| 6080 | { |
| 6081 | result = FAIL; |
| 6082 | break; |
| 6083 | } |
| 6084 | sta = sta->out; |
| 6085 | } |
| 6086 | } |
| 6087 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 6088 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 6089 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6090 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6091 | ADD_STATE_IF_MATCH(end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6092 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6093 | } |
| 6094 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6095 | |
| 6096 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6097 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 6098 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6099 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6100 | go_to_nextline = TRUE; |
| 6101 | /* Pass -1 for the offset, which means taking the position |
| 6102 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6103 | add_state = t->state->out; |
| 6104 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6105 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6106 | else if (curc == '\n' && reg_line_lbr) |
| 6107 | { |
| 6108 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6109 | add_state = t->state->out; |
| 6110 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6111 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6112 | break; |
| 6113 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6114 | case NFA_START_COLL: |
| 6115 | case NFA_START_NEG_COLL: |
| 6116 | { |
| 6117 | /* What follows is a list of characters, until NFA_END_COLL. |
| 6118 | * One of them must match or none of them must match. */ |
| 6119 | nfa_state_T *state; |
| 6120 | int result_if_matched; |
| 6121 | int c1, c2; |
| 6122 | |
| 6123 | /* Never match EOL. If it's part of the collection it is added |
| 6124 | * as a separate state with an OR. */ |
| 6125 | if (curc == NUL) |
| 6126 | break; |
| 6127 | |
| 6128 | state = t->state->out; |
| 6129 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 6130 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6131 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6132 | if (state->c == NFA_END_COLL) |
| 6133 | { |
| 6134 | result = !result_if_matched; |
| 6135 | break; |
| 6136 | } |
| 6137 | if (state->c == NFA_RANGE_MIN) |
| 6138 | { |
| 6139 | c1 = state->val; |
| 6140 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 6141 | c2 = state->val; |
| 6142 | #ifdef ENABLE_LOG |
| 6143 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 6144 | curc, c1, c2); |
| 6145 | #endif |
| 6146 | if (curc >= c1 && curc <= c2) |
| 6147 | { |
| 6148 | result = result_if_matched; |
| 6149 | break; |
| 6150 | } |
| 6151 | if (ireg_ic) |
| 6152 | { |
| 6153 | int curc_low = MB_TOLOWER(curc); |
| 6154 | int done = FALSE; |
| 6155 | |
| 6156 | for ( ; c1 <= c2; ++c1) |
| 6157 | if (MB_TOLOWER(c1) == curc_low) |
| 6158 | { |
| 6159 | result = result_if_matched; |
| 6160 | done = TRUE; |
| 6161 | break; |
| 6162 | } |
| 6163 | if (done) |
| 6164 | break; |
| 6165 | } |
| 6166 | } |
| 6167 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 6168 | : (curc == state->c |
| 6169 | || (ireg_ic && MB_TOLOWER(curc) |
| 6170 | == MB_TOLOWER(state->c)))) |
| 6171 | { |
| 6172 | result = result_if_matched; |
| 6173 | break; |
| 6174 | } |
| 6175 | state = state->out; |
| 6176 | } |
| 6177 | if (result) |
| 6178 | { |
| 6179 | /* next state is in out of the NFA_END_COLL, out1 of |
| 6180 | * START points to the END state */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6181 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6182 | add_off = clen; |
| 6183 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6184 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6185 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6186 | |
| 6187 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6188 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6189 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6190 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6191 | add_state = t->state->out; |
| 6192 | add_off = clen; |
| 6193 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6194 | break; |
| 6195 | |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 6196 | case NFA_ANY_COMPOSING: |
| 6197 | /* On a composing character skip over it. Otherwise do |
| 6198 | * nothing. Always matches. */ |
| 6199 | #ifdef FEAT_MBYTE |
| 6200 | if (enc_utf8 && utf_iscomposing(curc)) |
| 6201 | { |
| 6202 | add_off = clen; |
| 6203 | } |
| 6204 | else |
| 6205 | #endif |
| 6206 | { |
| 6207 | add_here = TRUE; |
| 6208 | add_off = 0; |
| 6209 | } |
| 6210 | add_state = t->state->out; |
| 6211 | break; |
| 6212 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6213 | /* |
| 6214 | * Character classes like \a for alpha, \d for digit etc. |
| 6215 | */ |
| 6216 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6217 | result = vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6218 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6219 | break; |
| 6220 | |
| 6221 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6222 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6223 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6224 | break; |
| 6225 | |
| 6226 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 6227 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6228 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6229 | break; |
| 6230 | |
| 6231 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6232 | result = !VIM_ISDIGIT(curc) |
| 6233 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6234 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6235 | break; |
| 6236 | |
| 6237 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6238 | result = vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6239 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6240 | break; |
| 6241 | |
| 6242 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6243 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6244 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6245 | break; |
| 6246 | |
| 6247 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6248 | result = vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6249 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6250 | break; |
| 6251 | |
| 6252 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6253 | result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6254 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6255 | break; |
| 6256 | |
| 6257 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6258 | result = vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6259 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6260 | break; |
| 6261 | |
| 6262 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6263 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6264 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6265 | break; |
| 6266 | |
| 6267 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6268 | result = ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6269 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6270 | break; |
| 6271 | |
| 6272 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6273 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6274 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6275 | break; |
| 6276 | |
| 6277 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6278 | result = ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6279 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6280 | break; |
| 6281 | |
| 6282 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6283 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6284 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6285 | break; |
| 6286 | |
| 6287 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6288 | result = ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6289 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6290 | break; |
| 6291 | |
| 6292 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6293 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6294 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6295 | break; |
| 6296 | |
| 6297 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6298 | result = ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6299 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6300 | break; |
| 6301 | |
| 6302 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6303 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6304 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6305 | break; |
| 6306 | |
| 6307 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6308 | result = ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6309 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6310 | break; |
| 6311 | |
| 6312 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6313 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6314 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6315 | break; |
| 6316 | |
| 6317 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6318 | result = ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6319 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6320 | break; |
| 6321 | |
| 6322 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6323 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6324 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6325 | break; |
| 6326 | |
| 6327 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6328 | result = ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6329 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6330 | break; |
| 6331 | |
| 6332 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6333 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6334 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6335 | break; |
| 6336 | |
| 6337 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6338 | result = ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6339 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6340 | break; |
| 6341 | |
| 6342 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6343 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6344 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6345 | break; |
| 6346 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 6347 | case NFA_LOWER_IC: /* [a-z] */ |
| 6348 | result = ri_lower(curc) || (ireg_ic && ri_upper(curc)); |
| 6349 | ADD_STATE_IF_MATCH(t->state); |
| 6350 | break; |
| 6351 | |
| 6352 | case NFA_NLOWER_IC: /* [^a-z] */ |
| 6353 | result = curc != NUL |
| 6354 | && !(ri_lower(curc) || (ireg_ic && ri_upper(curc))); |
| 6355 | ADD_STATE_IF_MATCH(t->state); |
| 6356 | break; |
| 6357 | |
| 6358 | case NFA_UPPER_IC: /* [A-Z] */ |
| 6359 | result = ri_upper(curc) || (ireg_ic && ri_lower(curc)); |
| 6360 | ADD_STATE_IF_MATCH(t->state); |
| 6361 | break; |
| 6362 | |
| 6363 | case NFA_NUPPER_IC: /* ^[A-Z] */ |
| 6364 | result = curc != NUL |
| 6365 | && !(ri_upper(curc) || (ireg_ic && ri_lower(curc))); |
| 6366 | ADD_STATE_IF_MATCH(t->state); |
| 6367 | break; |
| 6368 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6369 | case NFA_BACKREF1: |
| 6370 | case NFA_BACKREF2: |
| 6371 | case NFA_BACKREF3: |
| 6372 | case NFA_BACKREF4: |
| 6373 | case NFA_BACKREF5: |
| 6374 | case NFA_BACKREF6: |
| 6375 | case NFA_BACKREF7: |
| 6376 | case NFA_BACKREF8: |
| 6377 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6378 | #ifdef FEAT_SYN_HL |
| 6379 | case NFA_ZREF1: |
| 6380 | case NFA_ZREF2: |
| 6381 | case NFA_ZREF3: |
| 6382 | case NFA_ZREF4: |
| 6383 | case NFA_ZREF5: |
| 6384 | case NFA_ZREF6: |
| 6385 | case NFA_ZREF7: |
| 6386 | case NFA_ZREF8: |
| 6387 | case NFA_ZREF9: |
| 6388 | #endif |
| 6389 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6390 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6391 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6392 | int bytelen; |
| 6393 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6394 | if (t->state->c <= NFA_BACKREF9) |
| 6395 | { |
| 6396 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 6397 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 6398 | } |
| 6399 | #ifdef FEAT_SYN_HL |
| 6400 | else |
| 6401 | { |
| 6402 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 6403 | result = match_zref(subidx, &bytelen); |
| 6404 | } |
| 6405 | #endif |
| 6406 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6407 | if (result) |
| 6408 | { |
| 6409 | if (bytelen == 0) |
| 6410 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 6411 | /* empty match always works, output of NFA_SKIP to be |
| 6412 | * used next */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6413 | add_here = TRUE; |
| 6414 | add_state = t->state->out->out; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6415 | } |
| 6416 | else if (bytelen <= clen) |
| 6417 | { |
| 6418 | /* match current character, jump ahead to out of |
| 6419 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6420 | add_state = t->state->out->out; |
| 6421 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6422 | } |
| 6423 | else |
| 6424 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 6425 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6426 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6427 | add_state = t->state->out; |
| 6428 | add_off = bytelen; |
| 6429 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6430 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6431 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6432 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6433 | } |
| 6434 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 6435 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6436 | if (t->count - clen <= 0) |
| 6437 | { |
| 6438 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6439 | add_state = t->state->out; |
| 6440 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6441 | } |
| 6442 | else |
| 6443 | { |
| 6444 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6445 | add_state = t->state; |
| 6446 | add_off = 0; |
| 6447 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6448 | } |
| 6449 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6450 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6451 | case NFA_LNUM: |
| 6452 | case NFA_LNUM_GT: |
| 6453 | case NFA_LNUM_LT: |
| 6454 | result = (REG_MULTI && |
| 6455 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 6456 | (long_u)(reglnum + reg_firstlnum))); |
| 6457 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6458 | { |
| 6459 | add_here = TRUE; |
| 6460 | add_state = t->state->out; |
| 6461 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6462 | break; |
| 6463 | |
| 6464 | case NFA_COL: |
| 6465 | case NFA_COL_GT: |
| 6466 | case NFA_COL_LT: |
| 6467 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 6468 | (long_u)(reginput - regline) + 1); |
| 6469 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6470 | { |
| 6471 | add_here = TRUE; |
| 6472 | add_state = t->state->out; |
| 6473 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6474 | break; |
| 6475 | |
| 6476 | case NFA_VCOL: |
| 6477 | case NFA_VCOL_GT: |
| 6478 | case NFA_VCOL_LT: |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6479 | { |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6480 | int op = t->state->c - NFA_VCOL; |
| 6481 | colnr_T col = (colnr_T)(reginput - regline); |
Bram Moolenaar | ef795d1 | 2015-01-18 16:46:32 +0100 | [diff] [blame] | 6482 | win_T *wp = reg_win == NULL ? curwin : reg_win; |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6483 | |
| 6484 | /* Bail out quickly when there can't be a match, avoid the |
| 6485 | * overhead of win_linetabsize() on long lines. */ |
Bram Moolenaar | 4f36dc3 | 2015-03-05 17:16:06 +0100 | [diff] [blame] | 6486 | if (op != 1 && col > t->state->val |
| 6487 | #ifdef FEAT_MBYTE |
| 6488 | * (has_mbyte ? MB_MAXBYTES : 1) |
| 6489 | #endif |
| 6490 | ) |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6491 | break; |
Bram Moolenaar | ef795d1 | 2015-01-18 16:46:32 +0100 | [diff] [blame] | 6492 | result = FALSE; |
| 6493 | if (op == 1 && col - 1 > t->state->val && col > 100) |
| 6494 | { |
| 6495 | int ts = wp->w_buffer->b_p_ts; |
| 6496 | |
| 6497 | /* Guess that a character won't use more columns than |
| 6498 | * 'tabstop', with a minimum of 4. */ |
| 6499 | if (ts < 4) |
| 6500 | ts = 4; |
| 6501 | result = col > t->state->val * ts; |
| 6502 | } |
| 6503 | if (!result) |
| 6504 | result = nfa_re_num_cmp(t->state->val, op, |
| 6505 | (long_u)win_linetabsize(wp, regline, col) + 1); |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6506 | if (result) |
| 6507 | { |
| 6508 | add_here = TRUE; |
| 6509 | add_state = t->state->out; |
| 6510 | } |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6511 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6512 | break; |
| 6513 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6514 | case NFA_MARK: |
| 6515 | case NFA_MARK_GT: |
| 6516 | case NFA_MARK_LT: |
| 6517 | { |
| 6518 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 6519 | |
| 6520 | /* Compare the mark position to the match position. */ |
| 6521 | result = (pos != NULL /* mark doesn't exist */ |
| 6522 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 6523 | && (pos->lnum == reglnum + reg_firstlnum |
| 6524 | ? (pos->col == (colnr_T)(reginput - regline) |
| 6525 | ? t->state->c == NFA_MARK |
| 6526 | : (pos->col < (colnr_T)(reginput - regline) |
| 6527 | ? t->state->c == NFA_MARK_GT |
| 6528 | : t->state->c == NFA_MARK_LT)) |
| 6529 | : (pos->lnum < reglnum + reg_firstlnum |
| 6530 | ? t->state->c == NFA_MARK_GT |
| 6531 | : t->state->c == NFA_MARK_LT))); |
| 6532 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6533 | { |
| 6534 | add_here = TRUE; |
| 6535 | add_state = t->state->out; |
| 6536 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6537 | break; |
| 6538 | } |
| 6539 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6540 | case NFA_CURSOR: |
| 6541 | result = (reg_win != NULL |
| 6542 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 6543 | && ((colnr_T)(reginput - regline) |
| 6544 | == reg_win->w_cursor.col)); |
| 6545 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6546 | { |
| 6547 | add_here = TRUE; |
| 6548 | add_state = t->state->out; |
| 6549 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6550 | break; |
| 6551 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6552 | case NFA_VISUAL: |
| 6553 | result = reg_match_visual(); |
| 6554 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6555 | { |
| 6556 | add_here = TRUE; |
| 6557 | add_state = t->state->out; |
| 6558 | } |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 6559 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6560 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6561 | case NFA_MOPEN1: |
| 6562 | case NFA_MOPEN2: |
| 6563 | case NFA_MOPEN3: |
| 6564 | case NFA_MOPEN4: |
| 6565 | case NFA_MOPEN5: |
| 6566 | case NFA_MOPEN6: |
| 6567 | case NFA_MOPEN7: |
| 6568 | case NFA_MOPEN8: |
| 6569 | case NFA_MOPEN9: |
| 6570 | #ifdef FEAT_SYN_HL |
| 6571 | case NFA_ZOPEN: |
| 6572 | case NFA_ZOPEN1: |
| 6573 | case NFA_ZOPEN2: |
| 6574 | case NFA_ZOPEN3: |
| 6575 | case NFA_ZOPEN4: |
| 6576 | case NFA_ZOPEN5: |
| 6577 | case NFA_ZOPEN6: |
| 6578 | case NFA_ZOPEN7: |
| 6579 | case NFA_ZOPEN8: |
| 6580 | case NFA_ZOPEN9: |
| 6581 | #endif |
| 6582 | case NFA_NOPEN: |
| 6583 | case NFA_ZSTART: |
| 6584 | /* These states are only added to be able to bail out when |
| 6585 | * they are added again, nothing is to be done. */ |
| 6586 | break; |
| 6587 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6588 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6589 | { |
| 6590 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6591 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6592 | #ifdef DEBUG |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6593 | if (c < 0) |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6594 | EMSGN("INTERNAL: Negative state char: %ld", c); |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6595 | #endif |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6596 | result = (c == curc); |
| 6597 | |
| 6598 | if (!result && ireg_ic) |
| 6599 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6600 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 6601 | /* If ireg_icombine is not set only skip over the character |
| 6602 | * itself. When it is set skip over composing characters. */ |
| 6603 | if (result && enc_utf8 && !ireg_icombine) |
Bram Moolenaar | 2186ffa | 2015-05-04 10:33:15 +0200 | [diff] [blame] | 6604 | clen = utf_ptr2len(reginput); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6605 | #endif |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6606 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6607 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6608 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6609 | |
| 6610 | } /* switch (t->state->c) */ |
| 6611 | |
| 6612 | if (add_state != NULL) |
| 6613 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6614 | nfa_pim_T *pim; |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6615 | nfa_pim_T pim_copy; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6616 | |
| 6617 | if (t->pim.result == NFA_PIM_UNUSED) |
| 6618 | pim = NULL; |
| 6619 | else |
| 6620 | pim = &t->pim; |
| 6621 | |
| 6622 | /* Handle the postponed invisible match if the match might end |
| 6623 | * without advancing and before the end of the line. */ |
| 6624 | if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6625 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6626 | if (pim->result == NFA_PIM_TODO) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6627 | { |
| 6628 | #ifdef ENABLE_LOG |
| 6629 | fprintf(log_fd, "\n"); |
| 6630 | fprintf(log_fd, "==================================\n"); |
| 6631 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 6632 | fprintf(log_fd, "\n"); |
| 6633 | #endif |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6634 | result = recursive_regmatch(pim->state, pim, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6635 | prog, submatch, m, &listids); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6636 | pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6637 | /* for \@! and \@<! it is a match when the result is |
| 6638 | * FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6639 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6640 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6641 | || pim->state->c |
| 6642 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6643 | || pim->state->c |
| 6644 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6645 | { |
| 6646 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6647 | copy_sub_off(&pim->subs.norm, &m->norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6648 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6649 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6650 | copy_sub_off(&pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6651 | #endif |
| 6652 | } |
| 6653 | } |
| 6654 | else |
| 6655 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6656 | result = (pim->result == NFA_PIM_MATCH); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6657 | #ifdef ENABLE_LOG |
| 6658 | fprintf(log_fd, "\n"); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6659 | 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] | 6660 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 6661 | fprintf(log_fd, "\n"); |
| 6662 | #endif |
| 6663 | } |
| 6664 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6665 | /* for \@! and \@<! it is a match when result is FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6666 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6667 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6668 | || pim->state->c |
| 6669 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6670 | || pim->state->c |
| 6671 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6672 | { |
| 6673 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6674 | copy_sub_off(&t->subs.norm, &pim->subs.norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6675 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6676 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6677 | copy_sub_off(&t->subs.synt, &pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6678 | #endif |
| 6679 | } |
| 6680 | else |
| 6681 | /* look-behind match failed, don't add the state */ |
| 6682 | continue; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6683 | |
| 6684 | /* Postponed invisible match was handled, don't add it to |
| 6685 | * following states. */ |
| 6686 | pim = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6687 | } |
| 6688 | |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6689 | /* If "pim" points into l->t it will become invalid when |
| 6690 | * adding the state causes the list to be reallocated. Make a |
| 6691 | * local copy to avoid that. */ |
| 6692 | if (pim == &t->pim) |
| 6693 | { |
| 6694 | copy_pim(&pim_copy, pim); |
| 6695 | pim = &pim_copy; |
| 6696 | } |
| 6697 | |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6698 | if (add_here) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6699 | addstate_here(thislist, add_state, &t->subs, pim, &listidx); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6700 | else |
| 6701 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6702 | addstate(nextlist, add_state, &t->subs, pim, add_off); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6703 | if (add_count > 0) |
| 6704 | nextlist->t[nextlist->n - 1].count = add_count; |
| 6705 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6706 | } |
| 6707 | |
| 6708 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 6709 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6710 | /* Look for the start of a match in the current position by adding the |
| 6711 | * start state to the list of states. |
| 6712 | * The first found match is the leftmost one, thus the order of states |
| 6713 | * matters! |
| 6714 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 6715 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6716 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6717 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6718 | if (nfa_match == FALSE |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6719 | && ((toplevel |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6720 | && reglnum == 0 |
| 6721 | && clen != 0 |
| 6722 | && (ireg_maxcol == 0 |
| 6723 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6724 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6725 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6726 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 6727 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6728 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6729 | < nfa_endp->se_u.pos.col)) |
| 6730 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6731 | { |
| 6732 | #ifdef ENABLE_LOG |
| 6733 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 6734 | #endif |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6735 | /* Inline optimized code for addstate() if we know the state is |
| 6736 | * the first MOPEN. */ |
| 6737 | if (toplevel) |
| 6738 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6739 | int add = TRUE; |
| 6740 | int c; |
| 6741 | |
| 6742 | if (prog->regstart != NUL && clen != 0) |
| 6743 | { |
| 6744 | if (nextlist->n == 0) |
| 6745 | { |
| 6746 | colnr_T col = (colnr_T)(reginput - regline) + clen; |
| 6747 | |
| 6748 | /* Nextlist is empty, we can skip ahead to the |
| 6749 | * character that must appear at the start. */ |
| 6750 | if (skip_to_start(prog->regstart, &col) == FAIL) |
| 6751 | break; |
| 6752 | #ifdef ENABLE_LOG |
| 6753 | fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
| 6754 | col - ((colnr_T)(reginput - regline) + clen)); |
| 6755 | #endif |
| 6756 | reginput = regline + col - clen; |
| 6757 | } |
| 6758 | else |
| 6759 | { |
| 6760 | /* Checking if the required start character matches is |
| 6761 | * cheaper than adding a state that won't match. */ |
| 6762 | c = PTR2CHAR(reginput + clen); |
| 6763 | if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c) |
| 6764 | != MB_TOLOWER(prog->regstart))) |
| 6765 | { |
| 6766 | #ifdef ENABLE_LOG |
| 6767 | fprintf(log_fd, " Skipping start state, regstart does not match\n"); |
| 6768 | #endif |
| 6769 | add = FALSE; |
| 6770 | } |
| 6771 | } |
| 6772 | } |
| 6773 | |
| 6774 | if (add) |
| 6775 | { |
| 6776 | if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6777 | m->norm.list.multi[0].start_col = |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6778 | (colnr_T)(reginput - regline) + clen; |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6779 | else |
| 6780 | m->norm.list.line[0].start = reginput + clen; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6781 | addstate(nextlist, start->out, m, NULL, clen); |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6782 | } |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6783 | } |
| 6784 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6785 | addstate(nextlist, start, m, NULL, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6786 | } |
| 6787 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6788 | #ifdef ENABLE_LOG |
| 6789 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6790 | { |
| 6791 | int i; |
| 6792 | |
| 6793 | for (i = 0; i < thislist->n; i++) |
| 6794 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 6795 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6796 | fprintf(log_fd, "\n"); |
| 6797 | #endif |
| 6798 | |
| 6799 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6800 | /* Advance to the next character, or advance to the next line, or |
| 6801 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6802 | if (clen != 0) |
| 6803 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6804 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 6805 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6806 | reg_nextline(); |
| 6807 | else |
| 6808 | break; |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6809 | |
| 6810 | /* Allow interrupting with CTRL-C. */ |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6811 | line_breakcheck(); |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6812 | if (got_int) |
| 6813 | break; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6814 | #ifdef FEAT_RELTIME |
| 6815 | /* Check for timeout once in a twenty times to avoid overhead. */ |
| 6816 | if (nfa_time_limit != NULL && ++nfa_time_count == 20) |
| 6817 | { |
| 6818 | nfa_time_count = 0; |
| 6819 | if (profile_passed_limit(nfa_time_limit)) |
| 6820 | break; |
| 6821 | } |
| 6822 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6823 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6824 | |
| 6825 | #ifdef ENABLE_LOG |
| 6826 | if (log_fd != stderr) |
| 6827 | fclose(log_fd); |
| 6828 | log_fd = NULL; |
| 6829 | #endif |
| 6830 | |
| 6831 | theend: |
| 6832 | /* Free memory */ |
| 6833 | vim_free(list[0].t); |
| 6834 | vim_free(list[1].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6835 | vim_free(listids); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6836 | #undef ADD_STATE_IF_MATCH |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 6837 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6838 | fclose(debug); |
| 6839 | #endif |
| 6840 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6841 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6842 | } |
| 6843 | |
| 6844 | /* |
| 6845 | * Try match of "prog" with at regline["col"]. |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 6846 | * Returns <= 0 for failure, number of lines contained in the match otherwise. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6847 | */ |
| 6848 | static long |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6849 | nfa_regtry(prog, col, tm) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6850 | nfa_regprog_T *prog; |
| 6851 | colnr_T col; |
Bram Moolenaar | 168e049 | 2015-02-05 20:29:59 +0100 | [diff] [blame] | 6852 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6853 | { |
| 6854 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6855 | regsubs_T subs, m; |
| 6856 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 6857 | int result; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6858 | #ifdef ENABLE_LOG |
| 6859 | FILE *f; |
| 6860 | #endif |
| 6861 | |
| 6862 | reginput = regline + col; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6863 | #ifdef FEAT_RELTIME |
| 6864 | nfa_time_limit = tm; |
| 6865 | nfa_time_count = 0; |
| 6866 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6867 | |
| 6868 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6869 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6870 | if (f != NULL) |
| 6871 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6872 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6873 | #ifdef DEBUG |
| 6874 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 6875 | #endif |
| 6876 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6877 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 6878 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6879 | fprintf(f, "\n\n"); |
| 6880 | fclose(f); |
| 6881 | } |
| 6882 | else |
| 6883 | EMSG(_("Could not open temporary log file for writing ")); |
| 6884 | #endif |
| 6885 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6886 | clear_sub(&subs.norm); |
| 6887 | clear_sub(&m.norm); |
| 6888 | #ifdef FEAT_SYN_HL |
| 6889 | clear_sub(&subs.synt); |
| 6890 | clear_sub(&m.synt); |
| 6891 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6892 | |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 6893 | result = nfa_regmatch(prog, start, &subs, &m); |
| 6894 | if (result == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6895 | return 0; |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 6896 | else if (result == NFA_TOO_EXPENSIVE) |
| 6897 | return result; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6898 | |
| 6899 | cleanup_subexpr(); |
| 6900 | if (REG_MULTI) |
| 6901 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6902 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6903 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6904 | reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum; |
| 6905 | reg_startpos[i].col = subs.norm.list.multi[i].start_col; |
| 6906 | |
| 6907 | reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum; |
| 6908 | reg_endpos[i].col = subs.norm.list.multi[i].end_col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6909 | } |
| 6910 | |
| 6911 | if (reg_startpos[0].lnum < 0) |
| 6912 | { |
| 6913 | reg_startpos[0].lnum = 0; |
| 6914 | reg_startpos[0].col = col; |
| 6915 | } |
| 6916 | if (reg_endpos[0].lnum < 0) |
| 6917 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 6918 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6919 | reg_endpos[0].lnum = reglnum; |
| 6920 | reg_endpos[0].col = (int)(reginput - regline); |
| 6921 | } |
| 6922 | else |
| 6923 | /* Use line number of "\ze". */ |
| 6924 | reglnum = reg_endpos[0].lnum; |
| 6925 | } |
| 6926 | else |
| 6927 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6928 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6929 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6930 | reg_startp[i] = subs.norm.list.line[i].start; |
| 6931 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6932 | } |
| 6933 | |
| 6934 | if (reg_startp[0] == NULL) |
| 6935 | reg_startp[0] = regline + col; |
| 6936 | if (reg_endp[0] == NULL) |
| 6937 | reg_endp[0] = reginput; |
| 6938 | } |
| 6939 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6940 | #ifdef FEAT_SYN_HL |
| 6941 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 6942 | unref_extmatch(re_extmatch_out); |
| 6943 | re_extmatch_out = NULL; |
| 6944 | |
| 6945 | if (prog->reghasz == REX_SET) |
| 6946 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6947 | cleanup_zsubexpr(); |
| 6948 | re_extmatch_out = make_extmatch(); |
| 6949 | for (i = 0; i < subs.synt.in_use; i++) |
| 6950 | { |
| 6951 | if (REG_MULTI) |
| 6952 | { |
| 6953 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 6954 | |
Bram Moolenaar | 5a4e160 | 2014-04-06 21:34:04 +0200 | [diff] [blame] | 6955 | /* Only accept single line matches that are valid. */ |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6956 | if (mpos->start_lnum >= 0 |
| 6957 | && mpos->start_lnum == mpos->end_lnum |
| 6958 | && mpos->end_col >= mpos->start_col) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6959 | re_extmatch_out->matches[i] = |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6960 | vim_strnsave(reg_getline(mpos->start_lnum) |
| 6961 | + mpos->start_col, |
| 6962 | mpos->end_col - mpos->start_col); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6963 | } |
| 6964 | else |
| 6965 | { |
| 6966 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 6967 | |
| 6968 | if (lpos->start != NULL && lpos->end != NULL) |
| 6969 | re_extmatch_out->matches[i] = |
| 6970 | vim_strnsave(lpos->start, |
| 6971 | (int)(lpos->end - lpos->start)); |
| 6972 | } |
| 6973 | } |
| 6974 | } |
| 6975 | #endif |
| 6976 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6977 | return 1 + reglnum; |
| 6978 | } |
| 6979 | |
| 6980 | /* |
| 6981 | * Match a regexp against a string ("line" points to the string) or multiple |
| 6982 | * lines ("line" is NULL, use reg_getline()). |
| 6983 | * |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 6984 | * Returns <= 0 for failure, number of lines contained in the match otherwise. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6985 | */ |
| 6986 | static long |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6987 | nfa_regexec_both(line, startcol, tm) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6988 | char_u *line; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6989 | colnr_T startcol; /* column to start looking for match */ |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6990 | proftime_T *tm; /* timeout limit or NULL */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6991 | { |
| 6992 | nfa_regprog_T *prog; |
| 6993 | long retval = 0L; |
| 6994 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6995 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6996 | |
| 6997 | if (REG_MULTI) |
| 6998 | { |
| 6999 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 7000 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 7001 | reg_startpos = reg_mmatch->startpos; |
| 7002 | reg_endpos = reg_mmatch->endpos; |
| 7003 | } |
| 7004 | else |
| 7005 | { |
| 7006 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 7007 | reg_startp = reg_match->startp; |
| 7008 | reg_endp = reg_match->endp; |
| 7009 | } |
| 7010 | |
| 7011 | /* Be paranoid... */ |
| 7012 | if (prog == NULL || line == NULL) |
| 7013 | { |
| 7014 | EMSG(_(e_null)); |
| 7015 | goto theend; |
| 7016 | } |
| 7017 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7018 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 7019 | if (prog->regflags & RF_ICASE) |
| 7020 | ireg_ic = TRUE; |
| 7021 | else if (prog->regflags & RF_NOICASE) |
| 7022 | ireg_ic = FALSE; |
| 7023 | |
| 7024 | #ifdef FEAT_MBYTE |
| 7025 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 7026 | if (prog->regflags & RF_ICOMBINE) |
| 7027 | ireg_icombine = TRUE; |
| 7028 | #endif |
| 7029 | |
| 7030 | regline = line; |
| 7031 | reglnum = 0; /* relative to line */ |
| 7032 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 7033 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 7034 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 7035 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 7036 | nfa_listid = 1; |
| 7037 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7038 | nfa_regengine.expr = prog->pattern; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7039 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7040 | if (prog->reganch && col > 0) |
| 7041 | return 0L; |
| 7042 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7043 | need_clear_subexpr = TRUE; |
| 7044 | #ifdef FEAT_SYN_HL |
| 7045 | /* Clear the external match subpointers if necessary. */ |
| 7046 | if (prog->reghasz == REX_SET) |
| 7047 | { |
| 7048 | nfa_has_zsubexpr = TRUE; |
| 7049 | need_clear_zsubexpr = TRUE; |
| 7050 | } |
| 7051 | else |
| 7052 | nfa_has_zsubexpr = FALSE; |
| 7053 | #endif |
| 7054 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7055 | if (prog->regstart != NUL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7056 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 7057 | /* Skip ahead until a character we know the match must start with. |
| 7058 | * When there is none there is no match. */ |
| 7059 | if (skip_to_start(prog->regstart, &col) == FAIL) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7060 | return 0L; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7061 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7062 | /* If match_text is set it contains the full text that must match. |
| 7063 | * Nothing else to try. Doesn't handle combining chars well. */ |
Bram Moolenaar | a940aa6 | 2013-06-08 23:30:04 +0200 | [diff] [blame] | 7064 | if (prog->match_text != NULL |
| 7065 | #ifdef FEAT_MBYTE |
| 7066 | && !ireg_icombine |
| 7067 | #endif |
| 7068 | ) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7069 | return find_match_text(col, prog->regstart, prog->match_text); |
| 7070 | } |
| 7071 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7072 | /* If the start column is past the maximum column: no need to try. */ |
| 7073 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 7074 | goto theend; |
| 7075 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 7076 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7077 | for (i = 0; i < nstate; ++i) |
| 7078 | { |
| 7079 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 7080 | prog->state[i].lastlist[0] = 0; |
| 7081 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7082 | } |
| 7083 | |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 7084 | retval = nfa_regtry(prog, col, tm); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7085 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7086 | nfa_regengine.expr = NULL; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7087 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7088 | theend: |
| 7089 | return retval; |
| 7090 | } |
| 7091 | |
| 7092 | /* |
| 7093 | * Compile a regular expression into internal code for the NFA matcher. |
| 7094 | * Returns the program in allocated space. Returns NULL for an error. |
| 7095 | */ |
| 7096 | static regprog_T * |
| 7097 | nfa_regcomp(expr, re_flags) |
| 7098 | char_u *expr; |
| 7099 | int re_flags; |
| 7100 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7101 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 7102 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7103 | int *postfix; |
| 7104 | |
| 7105 | if (expr == NULL) |
| 7106 | return NULL; |
| 7107 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7108 | nfa_regengine.expr = expr; |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 7109 | nfa_re_flags = re_flags; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7110 | |
| 7111 | init_class_tab(); |
| 7112 | |
| 7113 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 7114 | return NULL; |
| 7115 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7116 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7117 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7118 | postfix = re2post(); |
| 7119 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 7120 | { |
| 7121 | /* TODO: only give this error for debugging? */ |
| 7122 | if (post_ptr >= post_end) |
| 7123 | 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] | 7124 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 7125 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7126 | |
| 7127 | /* |
| 7128 | * In order to build the NFA, we parse the input regexp twice: |
| 7129 | * 1. first pass to count size (so we can allocate space) |
| 7130 | * 2. second to emit code |
| 7131 | */ |
| 7132 | #ifdef ENABLE_LOG |
| 7133 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 7134 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7135 | |
| 7136 | if (f != NULL) |
| 7137 | { |
| 7138 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 7139 | fclose(f); |
| 7140 | } |
| 7141 | } |
| 7142 | #endif |
| 7143 | |
| 7144 | /* |
| 7145 | * PASS 1 |
| 7146 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 7147 | */ |
| 7148 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7149 | |
Bram Moolenaar | 16619a2 | 2013-06-11 18:42:36 +0200 | [diff] [blame] | 7150 | /* allocate the regprog with space for the compiled regexp */ |
| 7151 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7152 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 7153 | if (prog == NULL) |
| 7154 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7155 | state_ptr = prog->state; |
| 7156 | |
| 7157 | /* |
| 7158 | * PASS 2 |
| 7159 | * Build the NFA |
| 7160 | */ |
| 7161 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 7162 | if (prog->start == NULL) |
| 7163 | goto fail; |
| 7164 | |
| 7165 | prog->regflags = regflags; |
| 7166 | prog->engine = &nfa_regengine; |
| 7167 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 7168 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 7169 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 7170 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7171 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 7172 | nfa_postprocess(prog); |
| 7173 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7174 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 7175 | prog->regstart = nfa_get_regstart(prog->start, 0); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7176 | prog->match_text = nfa_get_match_text(prog->start); |
| 7177 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7178 | #ifdef ENABLE_LOG |
| 7179 | nfa_postfix_dump(expr, OK); |
| 7180 | nfa_dump(prog); |
| 7181 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 7182 | #ifdef FEAT_SYN_HL |
| 7183 | /* Remember whether this pattern has any \z specials in it. */ |
| 7184 | prog->reghasz = re_has_z; |
| 7185 | #endif |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7186 | prog->pattern = vim_strsave(expr); |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7187 | nfa_regengine.expr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7188 | |
| 7189 | out: |
| 7190 | vim_free(post_start); |
| 7191 | post_start = post_ptr = post_end = NULL; |
| 7192 | state_ptr = NULL; |
| 7193 | return (regprog_T *)prog; |
| 7194 | |
| 7195 | fail: |
| 7196 | vim_free(prog); |
| 7197 | prog = NULL; |
| 7198 | #ifdef ENABLE_LOG |
| 7199 | nfa_postfix_dump(expr, FAIL); |
| 7200 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7201 | nfa_regengine.expr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7202 | goto out; |
| 7203 | } |
| 7204 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7205 | /* |
| 7206 | * Free a compiled regexp program, returned by nfa_regcomp(). |
| 7207 | */ |
| 7208 | static void |
| 7209 | nfa_regfree(prog) |
| 7210 | regprog_T *prog; |
| 7211 | { |
| 7212 | if (prog != NULL) |
| 7213 | { |
| 7214 | vim_free(((nfa_regprog_T *)prog)->match_text); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7215 | vim_free(((nfa_regprog_T *)prog)->pattern); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7216 | vim_free(prog); |
| 7217 | } |
| 7218 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7219 | |
| 7220 | /* |
| 7221 | * Match a regexp against a string. |
| 7222 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 7223 | * Uses curbuf for line count and 'iskeyword'. |
Bram Moolenaar | 2af78a1 | 2014-04-23 19:06:37 +0200 | [diff] [blame] | 7224 | * If "line_lbr" is TRUE consider a "\n" in "line" to be a line break. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7225 | * |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 7226 | * Returns <= 0 for failure, number of lines contained in the match otherwise. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7227 | */ |
| 7228 | static int |
Bram Moolenaar | 2af78a1 | 2014-04-23 19:06:37 +0200 | [diff] [blame] | 7229 | nfa_regexec_nl(rmp, line, col, line_lbr) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7230 | regmatch_T *rmp; |
| 7231 | char_u *line; /* string to match against */ |
| 7232 | colnr_T col; /* column to start looking for match */ |
Bram Moolenaar | 2af78a1 | 2014-04-23 19:06:37 +0200 | [diff] [blame] | 7233 | int line_lbr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7234 | { |
| 7235 | reg_match = rmp; |
| 7236 | reg_mmatch = NULL; |
| 7237 | reg_maxline = 0; |
Bram Moolenaar | 2af78a1 | 2014-04-23 19:06:37 +0200 | [diff] [blame] | 7238 | reg_line_lbr = line_lbr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7239 | reg_buf = curbuf; |
| 7240 | reg_win = NULL; |
| 7241 | ireg_ic = rmp->rm_ic; |
| 7242 | #ifdef FEAT_MBYTE |
| 7243 | ireg_icombine = FALSE; |
| 7244 | #endif |
| 7245 | ireg_maxcol = 0; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 7246 | return nfa_regexec_both(line, col, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7247 | } |
| 7248 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7249 | |
| 7250 | /* |
| 7251 | * Match a regexp against multiple lines. |
| 7252 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 7253 | * Uses curbuf for line count and 'iskeyword'. |
| 7254 | * |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 7255 | * Return <= 0 if there is no match. Return number of lines contained in the |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7256 | * match otherwise. |
| 7257 | * |
| 7258 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 7259 | * |
| 7260 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 7261 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 7262 | * |
| 7263 | * +-------------------------+ |
| 7264 | * |a | |
| 7265 | * |b | |
| 7266 | * |c | |
| 7267 | * | | |
| 7268 | * +-------------------------+ |
| 7269 | * |
| 7270 | * then nfa_regexec_multi() returns 3. while the original |
| 7271 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 7272 | * |
| 7273 | * FIXME if this behavior is not compatible. |
| 7274 | */ |
| 7275 | static long |
| 7276 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 7277 | regmmatch_T *rmp; |
| 7278 | win_T *win; /* window in which to search or NULL */ |
| 7279 | buf_T *buf; /* buffer in which to search */ |
| 7280 | linenr_T lnum; /* nr of line to start looking for match */ |
| 7281 | colnr_T col; /* column to start looking for match */ |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 7282 | proftime_T *tm; /* timeout limit or NULL */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7283 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7284 | reg_match = NULL; |
| 7285 | reg_mmatch = rmp; |
| 7286 | reg_buf = buf; |
| 7287 | reg_win = win; |
| 7288 | reg_firstlnum = lnum; |
| 7289 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 7290 | reg_line_lbr = FALSE; |
| 7291 | ireg_ic = rmp->rmm_ic; |
| 7292 | #ifdef FEAT_MBYTE |
| 7293 | ireg_icombine = FALSE; |
| 7294 | #endif |
| 7295 | ireg_maxcol = rmp->rmm_maxcol; |
| 7296 | |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 7297 | return nfa_regexec_both(NULL, col, tm); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7298 | } |
| 7299 | |
| 7300 | #ifdef DEBUG |
| 7301 | # undef ENABLE_LOG |
| 7302 | #endif |