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 | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 285 | static int nfa_regcomp_start(char_u *expr, int re_flags); |
| 286 | static int nfa_get_reganch(nfa_state_T *start, int depth); |
| 287 | static int nfa_get_regstart(nfa_state_T *start, int depth); |
| 288 | static char_u *nfa_get_match_text(nfa_state_T *start); |
| 289 | static int realloc_post_list(void); |
| 290 | static int nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl); |
| 291 | static int nfa_emit_equi_class(int c); |
| 292 | static int nfa_regatom(void); |
| 293 | static int nfa_regpiece(void); |
| 294 | static int nfa_regconcat(void); |
| 295 | static int nfa_regbranch(void); |
| 296 | static int nfa_reg(int paren); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 297 | #ifdef DEBUG |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 298 | static void nfa_set_code(int c); |
| 299 | static void nfa_postfix_dump(char_u *expr, int retval); |
| 300 | static void nfa_print_state(FILE *debugf, nfa_state_T *state); |
| 301 | static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent); |
| 302 | static void nfa_dump(nfa_regprog_T *prog); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 303 | #endif |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 304 | static int *re2post(void); |
| 305 | static nfa_state_T *alloc_state(int c, nfa_state_T *out, nfa_state_T *out1); |
| 306 | static void st_error(int *postfix, int *end, int *p); |
| 307 | static int nfa_max_width(nfa_state_T *startstate, int depth); |
| 308 | static nfa_state_T *post2nfa(int *postfix, int *end, int nfa_calc_size); |
| 309 | static void nfa_postprocess(nfa_regprog_T *prog); |
| 310 | static int check_char_class(int class, int c); |
| 311 | static void nfa_save_listids(nfa_regprog_T *prog, int *list); |
| 312 | static void nfa_restore_listids(nfa_regprog_T *prog, int *list); |
| 313 | static int nfa_re_num_cmp(long_u val, int op, long_u pos); |
| 314 | static long nfa_regtry(nfa_regprog_T *prog, colnr_T col, proftime_T *tm); |
| 315 | static long nfa_regexec_both(char_u *line, colnr_T col, proftime_T *tm); |
| 316 | static regprog_T *nfa_regcomp(char_u *expr, int re_flags); |
| 317 | static void nfa_regfree(regprog_T *prog); |
| 318 | static int nfa_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr); |
| 319 | static long nfa_regexec_multi(regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm); |
| 320 | static int match_follows(nfa_state_T *startstate, int depth); |
| 321 | static int failure_chance(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 |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 335 | nfa_regcomp_start( |
| 336 | char_u *expr, |
| 337 | int re_flags) /* see vim_regcomp() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 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 |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 373 | nfa_get_reganch(nfa_state_T *start, int depth) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 374 | { |
| 375 | nfa_state_T *p = start; |
| 376 | |
| 377 | if (depth > 4) |
| 378 | return 0; |
| 379 | |
| 380 | while (p != NULL) |
| 381 | { |
| 382 | switch (p->c) |
| 383 | { |
| 384 | case NFA_BOL: |
| 385 | case NFA_BOF: |
| 386 | return 1; /* yes! */ |
| 387 | |
| 388 | case NFA_ZSTART: |
| 389 | case NFA_ZEND: |
| 390 | case NFA_CURSOR: |
| 391 | case NFA_VISUAL: |
| 392 | |
| 393 | case NFA_MOPEN: |
| 394 | case NFA_MOPEN1: |
| 395 | case NFA_MOPEN2: |
| 396 | case NFA_MOPEN3: |
| 397 | case NFA_MOPEN4: |
| 398 | case NFA_MOPEN5: |
| 399 | case NFA_MOPEN6: |
| 400 | case NFA_MOPEN7: |
| 401 | case NFA_MOPEN8: |
| 402 | case NFA_MOPEN9: |
| 403 | case NFA_NOPEN: |
| 404 | #ifdef FEAT_SYN_HL |
| 405 | case NFA_ZOPEN: |
| 406 | case NFA_ZOPEN1: |
| 407 | case NFA_ZOPEN2: |
| 408 | case NFA_ZOPEN3: |
| 409 | case NFA_ZOPEN4: |
| 410 | case NFA_ZOPEN5: |
| 411 | case NFA_ZOPEN6: |
| 412 | case NFA_ZOPEN7: |
| 413 | case NFA_ZOPEN8: |
| 414 | case NFA_ZOPEN9: |
| 415 | #endif |
| 416 | p = p->out; |
| 417 | break; |
| 418 | |
| 419 | case NFA_SPLIT: |
| 420 | return nfa_get_reganch(p->out, depth + 1) |
| 421 | && nfa_get_reganch(p->out1, depth + 1); |
| 422 | |
| 423 | default: |
| 424 | return 0; /* noooo */ |
| 425 | } |
| 426 | } |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * Figure out if the NFA state list starts with a character which must match |
| 432 | * at start of the match. |
| 433 | */ |
| 434 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 435 | nfa_get_regstart(nfa_state_T *start, int depth) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 436 | { |
| 437 | nfa_state_T *p = start; |
| 438 | |
| 439 | if (depth > 4) |
| 440 | return 0; |
| 441 | |
| 442 | while (p != NULL) |
| 443 | { |
| 444 | switch (p->c) |
| 445 | { |
| 446 | /* all kinds of zero-width matches */ |
| 447 | case NFA_BOL: |
| 448 | case NFA_BOF: |
| 449 | case NFA_BOW: |
| 450 | case NFA_EOW: |
| 451 | case NFA_ZSTART: |
| 452 | case NFA_ZEND: |
| 453 | case NFA_CURSOR: |
| 454 | case NFA_VISUAL: |
| 455 | case NFA_LNUM: |
| 456 | case NFA_LNUM_GT: |
| 457 | case NFA_LNUM_LT: |
| 458 | case NFA_COL: |
| 459 | case NFA_COL_GT: |
| 460 | case NFA_COL_LT: |
| 461 | case NFA_VCOL: |
| 462 | case NFA_VCOL_GT: |
| 463 | case NFA_VCOL_LT: |
| 464 | case NFA_MARK: |
| 465 | case NFA_MARK_GT: |
| 466 | case NFA_MARK_LT: |
| 467 | |
| 468 | case NFA_MOPEN: |
| 469 | case NFA_MOPEN1: |
| 470 | case NFA_MOPEN2: |
| 471 | case NFA_MOPEN3: |
| 472 | case NFA_MOPEN4: |
| 473 | case NFA_MOPEN5: |
| 474 | case NFA_MOPEN6: |
| 475 | case NFA_MOPEN7: |
| 476 | case NFA_MOPEN8: |
| 477 | case NFA_MOPEN9: |
| 478 | case NFA_NOPEN: |
| 479 | #ifdef FEAT_SYN_HL |
| 480 | case NFA_ZOPEN: |
| 481 | case NFA_ZOPEN1: |
| 482 | case NFA_ZOPEN2: |
| 483 | case NFA_ZOPEN3: |
| 484 | case NFA_ZOPEN4: |
| 485 | case NFA_ZOPEN5: |
| 486 | case NFA_ZOPEN6: |
| 487 | case NFA_ZOPEN7: |
| 488 | case NFA_ZOPEN8: |
| 489 | case NFA_ZOPEN9: |
| 490 | #endif |
| 491 | p = p->out; |
| 492 | break; |
| 493 | |
| 494 | case NFA_SPLIT: |
| 495 | { |
| 496 | int c1 = nfa_get_regstart(p->out, depth + 1); |
| 497 | int c2 = nfa_get_regstart(p->out1, depth + 1); |
| 498 | |
| 499 | if (c1 == c2) |
| 500 | return c1; /* yes! */ |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | default: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 505 | if (p->c > 0) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 506 | return p->c; /* yes! */ |
| 507 | return 0; |
| 508 | } |
| 509 | } |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 514 | * 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] | 515 | * else. If so return a string in allocated memory with what must match after |
| 516 | * regstart. Otherwise return NULL. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 517 | */ |
| 518 | static char_u * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 519 | nfa_get_match_text(nfa_state_T *start) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 520 | { |
| 521 | nfa_state_T *p = start; |
| 522 | int len = 0; |
| 523 | char_u *ret; |
| 524 | char_u *s; |
| 525 | |
| 526 | if (p->c != NFA_MOPEN) |
| 527 | return NULL; /* just in case */ |
| 528 | p = p->out; |
| 529 | while (p->c > 0) |
| 530 | { |
| 531 | len += MB_CHAR2LEN(p->c); |
| 532 | p = p->out; |
| 533 | } |
| 534 | if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH) |
| 535 | return NULL; |
| 536 | |
| 537 | ret = alloc(len); |
| 538 | if (ret != NULL) |
| 539 | { |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 540 | p = start->out->out; /* skip first char, it goes into regstart */ |
| 541 | s = ret; |
| 542 | while (p->c > 0) |
| 543 | { |
| 544 | #ifdef FEAT_MBYTE |
| 545 | if (has_mbyte) |
| 546 | s += (*mb_char2bytes)(p->c, s); |
| 547 | else |
| 548 | #endif |
| 549 | *s++ = p->c; |
| 550 | p = p->out; |
| 551 | } |
| 552 | *s = NUL; |
| 553 | } |
| 554 | return ret; |
| 555 | } |
| 556 | |
| 557 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 558 | * Allocate more space for post_start. Called when |
| 559 | * running above the estimated number of states. |
| 560 | */ |
| 561 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 562 | realloc_post_list(void) |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 563 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 564 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 565 | int new_max = nstate_max + 1000; |
| 566 | int *new_start; |
| 567 | int *old_start; |
| 568 | |
| 569 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 570 | if (new_start == NULL) |
| 571 | return FAIL; |
| 572 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 573 | old_start = post_start; |
| 574 | post_start = new_start; |
| 575 | post_ptr = new_start + (post_ptr - old_start); |
| 576 | post_end = post_start + new_max; |
| 577 | vim_free(old_start); |
| 578 | return OK; |
| 579 | } |
| 580 | |
| 581 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 582 | * Search between "start" and "end" and try to recognize a |
| 583 | * character class in expanded form. For example [0-9]. |
| 584 | * On success, return the id the character class to be emitted. |
| 585 | * On failure, return 0 (=FAIL) |
| 586 | * Start points to the first char of the range, while end should point |
| 587 | * to the closing brace. |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 588 | * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may |
| 589 | * need to be interpreted as [a-zA-Z]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 590 | */ |
| 591 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 592 | nfa_recognize_char_class(char_u *start, char_u *end, int extra_newl) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 593 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 594 | # define CLASS_not 0x80 |
| 595 | # define CLASS_af 0x40 |
| 596 | # define CLASS_AF 0x20 |
| 597 | # define CLASS_az 0x10 |
| 598 | # define CLASS_AZ 0x08 |
| 599 | # define CLASS_o7 0x04 |
| 600 | # define CLASS_o9 0x02 |
| 601 | # define CLASS_underscore 0x01 |
| 602 | |
| 603 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 604 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 605 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 606 | |
| 607 | if (extra_newl == TRUE) |
| 608 | newl = TRUE; |
| 609 | |
| 610 | if (*end != ']') |
| 611 | return FAIL; |
| 612 | p = start; |
| 613 | if (*p == '^') |
| 614 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 615 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 616 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | while (p < end) |
| 620 | { |
| 621 | if (p + 2 < end && *(p + 1) == '-') |
| 622 | { |
| 623 | switch (*p) |
| 624 | { |
| 625 | case '0': |
| 626 | if (*(p + 2) == '9') |
| 627 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 628 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 629 | break; |
| 630 | } |
| 631 | else |
| 632 | if (*(p + 2) == '7') |
| 633 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 634 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 635 | break; |
| 636 | } |
| 637 | case 'a': |
| 638 | if (*(p + 2) == 'z') |
| 639 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 640 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 641 | break; |
| 642 | } |
| 643 | else |
| 644 | if (*(p + 2) == 'f') |
| 645 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 646 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 647 | break; |
| 648 | } |
| 649 | case 'A': |
| 650 | if (*(p + 2) == 'Z') |
| 651 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 652 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 653 | break; |
| 654 | } |
| 655 | else |
| 656 | if (*(p + 2) == 'F') |
| 657 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 658 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 659 | break; |
| 660 | } |
| 661 | /* FALLTHROUGH */ |
| 662 | default: |
| 663 | return FAIL; |
| 664 | } |
| 665 | p += 3; |
| 666 | } |
| 667 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 668 | { |
| 669 | newl = TRUE; |
| 670 | p += 2; |
| 671 | } |
| 672 | else if (*p == '_') |
| 673 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 674 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 675 | p ++; |
| 676 | } |
| 677 | else if (*p == '\n') |
| 678 | { |
| 679 | newl = TRUE; |
| 680 | p ++; |
| 681 | } |
| 682 | else |
| 683 | return FAIL; |
| 684 | } /* while (p < end) */ |
| 685 | |
| 686 | if (p != end) |
| 687 | return FAIL; |
| 688 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 689 | if (newl == TRUE) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 690 | extra_newl = NFA_ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 691 | |
| 692 | switch (config) |
| 693 | { |
| 694 | case CLASS_o9: |
| 695 | return extra_newl + NFA_DIGIT; |
| 696 | case CLASS_not | CLASS_o9: |
| 697 | return extra_newl + NFA_NDIGIT; |
| 698 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 699 | return extra_newl + NFA_HEX; |
| 700 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 701 | return extra_newl + NFA_NHEX; |
| 702 | case CLASS_o7: |
| 703 | return extra_newl + NFA_OCTAL; |
| 704 | case CLASS_not | CLASS_o7: |
| 705 | return extra_newl + NFA_NOCTAL; |
| 706 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 707 | return extra_newl + NFA_WORD; |
| 708 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 709 | return extra_newl + NFA_NWORD; |
| 710 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 711 | return extra_newl + NFA_HEAD; |
| 712 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 713 | return extra_newl + NFA_NHEAD; |
| 714 | case CLASS_az | CLASS_AZ: |
| 715 | return extra_newl + NFA_ALPHA; |
| 716 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 717 | return extra_newl + NFA_NALPHA; |
| 718 | case CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 719 | return extra_newl + NFA_LOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 720 | case CLASS_not | CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 721 | return extra_newl + NFA_NLOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 722 | case CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 723 | return extra_newl + NFA_UPPER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 724 | case CLASS_not | CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 725 | return extra_newl + NFA_NUPPER_IC; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 726 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 727 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | /* |
| 731 | * Produce the bytes for equivalence class "c". |
| 732 | * Currently only handles latin1, latin9 and utf-8. |
| 733 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 734 | * equivalent to 'a OR b OR c' |
| 735 | * |
| 736 | * NOTE! When changing this function, also update reg_equi_class() |
| 737 | */ |
| 738 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 739 | nfa_emit_equi_class(int c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 740 | { |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 741 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
| 742 | #ifdef FEAT_MBYTE |
| 743 | # define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT); |
| 744 | #else |
| 745 | # define EMITMBC(c) |
| 746 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 747 | |
| 748 | #ifdef FEAT_MBYTE |
| 749 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 750 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 751 | #endif |
| 752 | { |
| 753 | switch (c) |
| 754 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 755 | case 'A': case 0300: case 0301: case 0302: |
| 756 | case 0303: case 0304: case 0305: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 757 | CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd) |
| 758 | CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2) |
| 759 | EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302); |
| 760 | EMIT2(0303); EMIT2(0304); EMIT2(0305); |
| 761 | EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104) |
| 762 | EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0) |
| 763 | EMITMBC(0x1ea2) |
| 764 | return OK; |
| 765 | |
| 766 | case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06) |
| 767 | EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 768 | return OK; |
| 769 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 770 | case 'C': case 0307: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 771 | CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c) |
| 772 | EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108) |
| 773 | EMITMBC(0x10a) EMITMBC(0x10c) |
| 774 | return OK; |
| 775 | |
| 776 | case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a) |
| 777 | CASEMBC(0x1e0e) CASEMBC(0x1e10) |
| 778 | EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a) |
| 779 | EMITMBC(0x1e0e) EMITMBC(0x1e10) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 780 | return OK; |
| 781 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 782 | case 'E': case 0310: case 0311: case 0312: case 0313: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 783 | CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118) |
| 784 | CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc) |
| 785 | EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312); |
| 786 | EMIT2(0313); |
| 787 | EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116) |
| 788 | EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba) |
| 789 | EMITMBC(0x1ebc) |
| 790 | return OK; |
| 791 | |
| 792 | case 'F': CASEMBC(0x1e1e) |
| 793 | EMIT2('F'); EMITMBC(0x1e1e) |
| 794 | return OK; |
| 795 | |
| 796 | case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120) |
| 797 | CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4) |
| 798 | CASEMBC(0x1e20) |
| 799 | EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120) |
| 800 | EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6) |
| 801 | EMITMBC(0x1f4) EMITMBC(0x1e20) |
| 802 | return OK; |
| 803 | |
| 804 | case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22) |
| 805 | CASEMBC(0x1e26) CASEMBC(0x1e28) |
| 806 | EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22) |
| 807 | EMITMBC(0x1e26) EMITMBC(0x1e28) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 808 | return OK; |
| 809 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 810 | case 'I': case 0314: case 0315: case 0316: case 0317: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 811 | CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e) |
| 812 | CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8) |
| 813 | EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316); |
| 814 | EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a) |
| 815 | EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130) |
| 816 | EMITMBC(0x1cf) EMITMBC(0x1ec8) |
| 817 | return OK; |
| 818 | |
| 819 | case 'J': CASEMBC(0x134) |
| 820 | EMIT2('J'); EMITMBC(0x134) |
| 821 | return OK; |
| 822 | |
| 823 | case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30) |
| 824 | CASEMBC(0x1e34) |
| 825 | EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30) |
| 826 | EMITMBC(0x1e34) |
| 827 | return OK; |
| 828 | |
| 829 | case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d) |
| 830 | CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a) |
| 831 | EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d) |
| 832 | EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a) |
| 833 | return OK; |
| 834 | |
| 835 | case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40) |
| 836 | EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 837 | return OK; |
| 838 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 839 | case 'N': case 0321: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 840 | CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44) |
| 841 | CASEMBC(0x1e48) |
| 842 | EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145) |
| 843 | EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 844 | return OK; |
| 845 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 846 | case 'O': case 0322: case 0323: case 0324: case 0325: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 847 | case 0326: case 0330: |
| 848 | CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0) |
| 849 | CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece) |
| 850 | EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324); |
| 851 | EMIT2(0325); EMIT2(0326); EMIT2(0330); |
| 852 | EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150) |
| 853 | EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea) |
| 854 | EMITMBC(0x1ec) EMITMBC(0x1ece) |
| 855 | return OK; |
| 856 | |
| 857 | case 'P': case 0x1e54: case 0x1e56: |
| 858 | EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56) |
| 859 | return OK; |
| 860 | |
| 861 | case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158) |
| 862 | CASEMBC(0x1e58) CASEMBC(0x1e5e) |
| 863 | EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158) |
| 864 | EMITMBC(0x1e58) EMITMBC(0x1e5e) |
| 865 | return OK; |
| 866 | |
| 867 | case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e) |
| 868 | CASEMBC(0x160) CASEMBC(0x1e60) |
| 869 | EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e) |
| 870 | EMITMBC(0x160) EMITMBC(0x1e60) |
| 871 | return OK; |
| 872 | |
| 873 | case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166) |
| 874 | CASEMBC(0x1e6a) CASEMBC(0x1e6e) |
| 875 | EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166) |
| 876 | EMITMBC(0x1e6a) EMITMBC(0x1e6e) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 877 | return OK; |
| 878 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 879 | case 'U': case 0331: case 0332: case 0333: case 0334: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 880 | CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e) |
| 881 | CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3) |
| 882 | CASEMBC(0x1ee6) |
| 883 | EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333); |
| 884 | EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a) |
| 885 | EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170) |
| 886 | EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3) |
| 887 | EMITMBC(0x1ee6) |
| 888 | return OK; |
| 889 | |
| 890 | case 'V': CASEMBC(0x1e7c) |
| 891 | EMIT2('V'); EMITMBC(0x1e7c) |
| 892 | return OK; |
| 893 | |
| 894 | case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82) |
| 895 | CASEMBC(0x1e84) CASEMBC(0x1e86) |
| 896 | EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82) |
| 897 | EMITMBC(0x1e84) EMITMBC(0x1e86) |
| 898 | return OK; |
| 899 | |
| 900 | case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c) |
| 901 | EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 902 | return OK; |
| 903 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 904 | case 'Y': case 0335: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 905 | CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2) |
| 906 | CASEMBC(0x1ef6) CASEMBC(0x1ef8) |
| 907 | EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178) |
| 908 | EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6) |
| 909 | EMITMBC(0x1ef8) |
| 910 | return OK; |
| 911 | |
| 912 | case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d) |
| 913 | CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94) |
| 914 | EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d) |
| 915 | EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 916 | return OK; |
| 917 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 918 | case 'a': case 0340: case 0341: case 0342: |
| 919 | case 0343: case 0344: case 0345: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 920 | CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce) |
| 921 | CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3) |
| 922 | EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342); |
| 923 | EMIT2(0343); EMIT2(0344); EMIT2(0345); |
| 924 | EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105) |
| 925 | EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1) |
| 926 | EMITMBC(0x1ea3) |
| 927 | return OK; |
| 928 | |
| 929 | case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07) |
| 930 | EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 931 | return OK; |
| 932 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 933 | case 'c': case 0347: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 934 | CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d) |
| 935 | EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109) |
| 936 | EMITMBC(0x10b) EMITMBC(0x10d) |
| 937 | return OK; |
| 938 | |
Bram Moolenaar | 2c61ec6 | 2015-07-10 19:16:34 +0200 | [diff] [blame] | 939 | case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b) |
| 940 | CASEMBC(0x1e0f) CASEMBC(0x1e11) |
| 941 | EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) |
| 942 | EMITMBC(0x1e0b) EMITMBC(0x1e0f) EMITMBC(0x1e11) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 943 | return OK; |
| 944 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 945 | case 'e': case 0350: case 0351: case 0352: case 0353: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 946 | CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119) |
| 947 | CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd) |
| 948 | EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352); |
| 949 | EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115) |
| 950 | EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b) |
| 951 | EMITMBC(0x1ebb) EMITMBC(0x1ebd) |
| 952 | return OK; |
| 953 | |
| 954 | case 'f': CASEMBC(0x1e1f) |
| 955 | EMIT2('f'); EMITMBC(0x1e1f) |
| 956 | return OK; |
| 957 | |
| 958 | case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121) |
| 959 | CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5) |
| 960 | CASEMBC(0x1e21) |
| 961 | EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121) |
| 962 | EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7) |
| 963 | EMITMBC(0x1f5) EMITMBC(0x1e21) |
| 964 | return OK; |
| 965 | |
| 966 | case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23) |
| 967 | CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96) |
| 968 | EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23) |
| 969 | EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 970 | return OK; |
| 971 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 972 | case 'i': case 0354: case 0355: case 0356: case 0357: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 973 | CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f) |
| 974 | CASEMBC(0x1d0) CASEMBC(0x1ec9) |
| 975 | EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356); |
| 976 | EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b) |
| 977 | EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0) |
| 978 | EMITMBC(0x1ec9) |
| 979 | return OK; |
| 980 | |
| 981 | case 'j': CASEMBC(0x135) CASEMBC(0x1f0) |
| 982 | EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0) |
| 983 | return OK; |
| 984 | |
| 985 | case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31) |
| 986 | CASEMBC(0x1e35) |
| 987 | EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31) |
| 988 | EMITMBC(0x1e35) |
| 989 | return OK; |
| 990 | |
| 991 | case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e) |
| 992 | CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b) |
| 993 | EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e) |
| 994 | EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b) |
| 995 | return OK; |
| 996 | |
| 997 | case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41) |
| 998 | EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 999 | return OK; |
| 1000 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1001 | case 'n': case 0361: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1002 | CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149) |
| 1003 | CASEMBC(0x1e45) CASEMBC(0x1e49) |
| 1004 | EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146) |
| 1005 | EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45) |
| 1006 | EMITMBC(0x1e49) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1007 | return OK; |
| 1008 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1009 | case 'o': case 0362: case 0363: case 0364: case 0365: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1010 | case 0366: case 0370: |
| 1011 | CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1) |
| 1012 | CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf) |
| 1013 | EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364); |
| 1014 | EMIT2(0365); EMIT2(0366); EMIT2(0370); |
| 1015 | EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151) |
| 1016 | EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb) |
| 1017 | EMITMBC(0x1ed) EMITMBC(0x1ecf) |
| 1018 | return OK; |
| 1019 | |
| 1020 | case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57) |
| 1021 | EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57) |
| 1022 | return OK; |
| 1023 | |
| 1024 | case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159) |
| 1025 | CASEMBC(0x1e59) CASEMBC(0x1e5f) |
| 1026 | EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159) |
| 1027 | EMITMBC(0x1e59) EMITMBC(0x1e5f) |
| 1028 | return OK; |
| 1029 | |
| 1030 | case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f) |
| 1031 | CASEMBC(0x161) CASEMBC(0x1e61) |
| 1032 | EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f) |
| 1033 | EMITMBC(0x161) EMITMBC(0x1e61) |
| 1034 | return OK; |
| 1035 | |
| 1036 | case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167) |
| 1037 | CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97) |
| 1038 | EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167) |
| 1039 | EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1040 | return OK; |
| 1041 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1042 | case 'u': case 0371: case 0372: case 0373: case 0374: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1043 | CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f) |
| 1044 | CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4) |
| 1045 | CASEMBC(0x1ee7) |
| 1046 | EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373); |
| 1047 | EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b) |
| 1048 | EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171) |
| 1049 | EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4) |
| 1050 | EMITMBC(0x1ee7) |
| 1051 | return OK; |
| 1052 | |
| 1053 | case 'v': CASEMBC(0x1e7d) |
| 1054 | EMIT2('v'); EMITMBC(0x1e7d) |
| 1055 | return OK; |
| 1056 | |
| 1057 | case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83) |
| 1058 | CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98) |
| 1059 | EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83) |
| 1060 | EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98) |
| 1061 | return OK; |
| 1062 | |
| 1063 | case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d) |
| 1064 | EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1065 | return OK; |
| 1066 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1067 | case 'y': case 0375: case 0377: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1068 | CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99) |
| 1069 | CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9) |
| 1070 | EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177) |
| 1071 | EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3) |
| 1072 | EMITMBC(0x1ef7) EMITMBC(0x1ef9) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1073 | return OK; |
| 1074 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1075 | case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e) |
| 1076 | CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) |
| 1077 | EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e) |
| 1078 | EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95) |
| 1079 | return OK; |
| 1080 | |
| 1081 | /* default: character itself */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1082 | } |
| 1083 | } |
| 1084 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1085 | EMIT2(c); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1086 | return OK; |
| 1087 | #undef EMIT2 |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1088 | #undef EMITMBC |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | /* |
| 1092 | * Code to parse regular expression. |
| 1093 | * |
| 1094 | * We try to reuse parsing functions in regexp.c to |
| 1095 | * minimize surprise and keep the syntax consistent. |
| 1096 | */ |
| 1097 | |
| 1098 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1099 | * Parse the lowest level. |
| 1100 | * |
| 1101 | * An atom can be one of a long list of items. Many atoms match one character |
| 1102 | * in the text. It is often an ordinary character or a character class. |
| 1103 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 1104 | * is only for syntax highlighting. |
| 1105 | * |
| 1106 | * atom ::= ordinary-atom |
| 1107 | * or \( pattern \) |
| 1108 | * or \%( pattern \) |
| 1109 | * or \z( pattern \) |
| 1110 | */ |
| 1111 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1112 | nfa_regatom(void) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1113 | { |
| 1114 | int c; |
| 1115 | int charclass; |
| 1116 | int equiclass; |
| 1117 | int collclass; |
| 1118 | int got_coll_char; |
| 1119 | char_u *p; |
| 1120 | char_u *endp; |
| 1121 | #ifdef FEAT_MBYTE |
| 1122 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1123 | #endif |
| 1124 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1125 | int emit_range; |
| 1126 | int negated; |
| 1127 | int result; |
| 1128 | int startc = -1; |
| 1129 | int endc = -1; |
| 1130 | int oldstartc = -1; |
Bram Moolenaar | 7c29f38 | 2016-02-12 19:08:15 +0100 | [diff] [blame] | 1131 | int save_prev_at_start = prev_at_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1132 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1133 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1134 | switch (c) |
| 1135 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1136 | case NUL: |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1137 | EMSG_RET_FAIL(_(e_nul_found)); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1138 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1139 | case Magic('^'): |
| 1140 | EMIT(NFA_BOL); |
| 1141 | break; |
| 1142 | |
| 1143 | case Magic('$'): |
| 1144 | EMIT(NFA_EOL); |
| 1145 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1146 | had_eol = TRUE; |
| 1147 | #endif |
| 1148 | break; |
| 1149 | |
| 1150 | case Magic('<'): |
| 1151 | EMIT(NFA_BOW); |
| 1152 | break; |
| 1153 | |
| 1154 | case Magic('>'): |
| 1155 | EMIT(NFA_EOW); |
| 1156 | break; |
| 1157 | |
| 1158 | case Magic('_'): |
| 1159 | c = no_Magic(getchr()); |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1160 | if (c == NUL) |
| 1161 | EMSG_RET_FAIL(_(e_nul_found)); |
| 1162 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1163 | if (c == '^') /* "\_^" is start-of-line */ |
| 1164 | { |
| 1165 | EMIT(NFA_BOL); |
| 1166 | break; |
| 1167 | } |
| 1168 | if (c == '$') /* "\_$" is end-of-line */ |
| 1169 | { |
| 1170 | EMIT(NFA_EOL); |
| 1171 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1172 | had_eol = TRUE; |
| 1173 | #endif |
| 1174 | break; |
| 1175 | } |
| 1176 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1177 | extra = NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1178 | |
| 1179 | /* "\_[" is collection plus newline */ |
| 1180 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1181 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1182 | |
| 1183 | /* "\_x" is character class plus newline */ |
| 1184 | /*FALLTHROUGH*/ |
| 1185 | |
| 1186 | /* |
| 1187 | * Character classes. |
| 1188 | */ |
| 1189 | case Magic('.'): |
| 1190 | case Magic('i'): |
| 1191 | case Magic('I'): |
| 1192 | case Magic('k'): |
| 1193 | case Magic('K'): |
| 1194 | case Magic('f'): |
| 1195 | case Magic('F'): |
| 1196 | case Magic('p'): |
| 1197 | case Magic('P'): |
| 1198 | case Magic('s'): |
| 1199 | case Magic('S'): |
| 1200 | case Magic('d'): |
| 1201 | case Magic('D'): |
| 1202 | case Magic('x'): |
| 1203 | case Magic('X'): |
| 1204 | case Magic('o'): |
| 1205 | case Magic('O'): |
| 1206 | case Magic('w'): |
| 1207 | case Magic('W'): |
| 1208 | case Magic('h'): |
| 1209 | case Magic('H'): |
| 1210 | case Magic('a'): |
| 1211 | case Magic('A'): |
| 1212 | case Magic('l'): |
| 1213 | case Magic('L'): |
| 1214 | case Magic('u'): |
| 1215 | case Magic('U'): |
| 1216 | p = vim_strchr(classchars, no_Magic(c)); |
| 1217 | if (p == NULL) |
| 1218 | { |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1219 | if (extra == NFA_ADD_NL) |
| 1220 | { |
| 1221 | EMSGN(_(e_ill_char_class), c); |
| 1222 | rc_did_emsg = TRUE; |
| 1223 | return FAIL; |
| 1224 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1225 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 1226 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1227 | } |
| 1228 | #ifdef FEAT_MBYTE |
| 1229 | /* When '.' is followed by a composing char ignore the dot, so that |
| 1230 | * the composing char is matched here. */ |
| 1231 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 1232 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1233 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1234 | c = getchr(); |
| 1235 | goto nfa_do_multibyte; |
| 1236 | } |
| 1237 | #endif |
| 1238 | EMIT(nfa_classcodes[p - classchars]); |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1239 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1240 | { |
| 1241 | EMIT(NFA_NEWL); |
| 1242 | EMIT(NFA_OR); |
| 1243 | regflags |= RF_HASNL; |
| 1244 | } |
| 1245 | break; |
| 1246 | |
| 1247 | case Magic('n'): |
| 1248 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1249 | /* In a string "\n" matches a newline character. */ |
| 1250 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1251 | else |
| 1252 | { |
| 1253 | /* In buffer text "\n" matches the end of a line. */ |
| 1254 | EMIT(NFA_NEWL); |
| 1255 | regflags |= RF_HASNL; |
| 1256 | } |
| 1257 | break; |
| 1258 | |
| 1259 | case Magic('('): |
| 1260 | if (nfa_reg(REG_PAREN) == FAIL) |
| 1261 | return FAIL; /* cascaded error */ |
| 1262 | break; |
| 1263 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1264 | case Magic('|'): |
| 1265 | case Magic('&'): |
| 1266 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1267 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1268 | return FAIL; |
| 1269 | |
| 1270 | case Magic('='): |
| 1271 | case Magic('?'): |
| 1272 | case Magic('+'): |
| 1273 | case Magic('@'): |
| 1274 | case Magic('*'): |
| 1275 | case Magic('{'): |
| 1276 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1277 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1278 | return FAIL; |
| 1279 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 1280 | case Magic('~'): |
| 1281 | { |
| 1282 | char_u *lp; |
| 1283 | |
| 1284 | /* Previous substitute pattern. |
| 1285 | * Generated as "\%(pattern\)". */ |
| 1286 | if (reg_prev_sub == NULL) |
| 1287 | { |
| 1288 | EMSG(_(e_nopresub)); |
| 1289 | return FAIL; |
| 1290 | } |
| 1291 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 1292 | { |
| 1293 | EMIT(PTR2CHAR(lp)); |
| 1294 | if (lp != reg_prev_sub) |
| 1295 | EMIT(NFA_CONCAT); |
| 1296 | } |
| 1297 | EMIT(NFA_NOPEN); |
| 1298 | break; |
| 1299 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1300 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 1301 | case Magic('1'): |
| 1302 | case Magic('2'): |
| 1303 | case Magic('3'): |
| 1304 | case Magic('4'): |
| 1305 | case Magic('5'): |
| 1306 | case Magic('6'): |
| 1307 | case Magic('7'): |
| 1308 | case Magic('8'): |
| 1309 | case Magic('9'): |
| 1310 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 1311 | nfa_has_backref = TRUE; |
| 1312 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1313 | |
| 1314 | case Magic('z'): |
| 1315 | c = no_Magic(getchr()); |
| 1316 | switch (c) |
| 1317 | { |
| 1318 | case 's': |
| 1319 | EMIT(NFA_ZSTART); |
Bram Moolenaar | 2d46e60 | 2014-08-29 11:56:32 +0200 | [diff] [blame] | 1320 | if (re_mult_next("\\zs") == FAIL) |
| 1321 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1322 | break; |
| 1323 | case 'e': |
| 1324 | EMIT(NFA_ZEND); |
| 1325 | nfa_has_zend = TRUE; |
Bram Moolenaar | 2d46e60 | 2014-08-29 11:56:32 +0200 | [diff] [blame] | 1326 | if (re_mult_next("\\ze") == FAIL) |
| 1327 | return FAIL; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1328 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1329 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1330 | case '1': |
| 1331 | case '2': |
| 1332 | case '3': |
| 1333 | case '4': |
| 1334 | case '5': |
| 1335 | case '6': |
| 1336 | case '7': |
| 1337 | case '8': |
| 1338 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1339 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1340 | if (reg_do_extmatch != REX_USE) |
| 1341 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1342 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1343 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1344 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1345 | re_has_z = REX_USE; |
| 1346 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1347 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1348 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1349 | if (reg_do_extmatch != REX_SET) |
| 1350 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1351 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1352 | return FAIL; /* cascaded error */ |
| 1353 | re_has_z = REX_SET; |
| 1354 | break; |
| 1355 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1356 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1357 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1358 | no_Magic(c)); |
| 1359 | return FAIL; |
| 1360 | } |
| 1361 | break; |
| 1362 | |
| 1363 | case Magic('%'): |
| 1364 | c = no_Magic(getchr()); |
| 1365 | switch (c) |
| 1366 | { |
| 1367 | /* () without a back reference */ |
| 1368 | case '(': |
| 1369 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1370 | return FAIL; |
| 1371 | EMIT(NFA_NOPEN); |
| 1372 | break; |
| 1373 | |
| 1374 | case 'd': /* %d123 decimal */ |
| 1375 | case 'o': /* %o123 octal */ |
| 1376 | case 'x': /* %xab hex 2 */ |
| 1377 | case 'u': /* %uabcd hex 4 */ |
| 1378 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1379 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1380 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1381 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1382 | switch (c) |
| 1383 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1384 | case 'd': nr = getdecchrs(); break; |
| 1385 | case 'o': nr = getoctchrs(); break; |
| 1386 | case 'x': nr = gethexchrs(2); break; |
| 1387 | case 'u': nr = gethexchrs(4); break; |
| 1388 | case 'U': nr = gethexchrs(8); break; |
| 1389 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1390 | } |
| 1391 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1392 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1393 | EMSG2_RET_FAIL( |
| 1394 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1395 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1396 | /* A NUL is stored in the text as NL */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1397 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1398 | EMIT(nr == 0 ? 0x0a : nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1399 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1400 | break; |
| 1401 | |
| 1402 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1403 | * pattern -- regardless of whether or not it makes sense. */ |
| 1404 | case '^': |
| 1405 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1406 | break; |
| 1407 | |
| 1408 | case '$': |
| 1409 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1410 | break; |
| 1411 | |
| 1412 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1413 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1414 | break; |
| 1415 | |
| 1416 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1417 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1418 | break; |
| 1419 | |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 1420 | case 'C': |
| 1421 | EMIT(NFA_ANY_COMPOSING); |
| 1422 | break; |
| 1423 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1424 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1425 | { |
| 1426 | int n; |
| 1427 | |
| 1428 | /* \%[abc] */ |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1429 | for (n = 0; (c = peekchr()) != ']'; ++n) |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1430 | { |
| 1431 | if (c == NUL) |
| 1432 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1433 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1434 | /* recursive call! */ |
| 1435 | if (nfa_regatom() == FAIL) |
| 1436 | return FAIL; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1437 | } |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1438 | getchr(); /* get the ] */ |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1439 | if (n == 0) |
| 1440 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1441 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1442 | EMIT(NFA_OPT_CHARS); |
| 1443 | EMIT(n); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1444 | |
| 1445 | /* Emit as "\%(\%[abc]\)" to be able to handle |
| 1446 | * "\%[abc]*" which would cause the empty string to be |
| 1447 | * matched an unlimited number of times. NFA_NOPEN is |
| 1448 | * added only once at a position, while NFA_SPLIT is |
| 1449 | * added multiple times. This is more efficient than |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 1450 | * not allowing NFA_SPLIT multiple times, it is used |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1451 | * a lot. */ |
| 1452 | EMIT(NFA_NOPEN); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1453 | break; |
| 1454 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1455 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1456 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1457 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1458 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1459 | int cmp = c; |
| 1460 | |
| 1461 | if (c == '<' || c == '>') |
| 1462 | c = getchr(); |
| 1463 | while (VIM_ISDIGIT(c)) |
| 1464 | { |
| 1465 | n = n * 10 + (c - '0'); |
| 1466 | c = getchr(); |
| 1467 | } |
| 1468 | if (c == 'l' || c == 'c' || c == 'v') |
| 1469 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1470 | if (c == 'l') |
Bram Moolenaar | 7c29f38 | 2016-02-12 19:08:15 +0100 | [diff] [blame] | 1471 | { |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1472 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1473 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1474 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 7c29f38 | 2016-02-12 19:08:15 +0100 | [diff] [blame] | 1475 | if (save_prev_at_start) |
| 1476 | at_start = TRUE; |
| 1477 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1478 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1479 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1480 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1481 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1482 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1483 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1484 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1485 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1486 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1487 | break; |
| 1488 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1489 | else if (c == '\'' && n == 0) |
| 1490 | { |
| 1491 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1492 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1493 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1494 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1495 | break; |
| 1496 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1497 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1498 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1499 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1500 | return FAIL; |
| 1501 | } |
| 1502 | break; |
| 1503 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1504 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1505 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1506 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1507 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1508 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1509 | * Each character is produced as a regular state, using |
| 1510 | * NFA_CONCAT to bind them together. |
| 1511 | * Besides normal characters there can be: |
| 1512 | * - character classes NFA_CLASS_* |
| 1513 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1514 | */ |
| 1515 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1516 | p = regparse; |
| 1517 | endp = skip_anyof(p); |
| 1518 | if (*endp == ']') |
| 1519 | { |
| 1520 | /* |
| 1521 | * Try to reverse engineer character classes. For example, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1522 | * 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] | 1523 | * and perform the necessary substitutions in the NFA. |
| 1524 | */ |
| 1525 | result = nfa_recognize_char_class(regparse, endp, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1526 | extra == NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1527 | if (result != FAIL) |
| 1528 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1529 | if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1530 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1531 | EMIT(result - NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1532 | EMIT(NFA_NEWL); |
| 1533 | EMIT(NFA_OR); |
| 1534 | } |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1535 | else |
| 1536 | EMIT(result); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1537 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1538 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1539 | return OK; |
| 1540 | } |
| 1541 | /* |
| 1542 | * Failed to recognize a character class. Use the simple |
| 1543 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1544 | */ |
| 1545 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1546 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1547 | if (*regparse == '^') /* negated range */ |
| 1548 | { |
| 1549 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1550 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1551 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1552 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1553 | else |
| 1554 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1555 | if (*regparse == '-') |
| 1556 | { |
| 1557 | startc = '-'; |
| 1558 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1559 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1560 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1561 | } |
| 1562 | /* Emit the OR branches for each character in the [] */ |
| 1563 | emit_range = FALSE; |
| 1564 | while (regparse < endp) |
| 1565 | { |
| 1566 | oldstartc = startc; |
| 1567 | startc = -1; |
| 1568 | got_coll_char = FALSE; |
| 1569 | if (*regparse == '[') |
| 1570 | { |
| 1571 | /* Check for [: :], [= =], [. .] */ |
| 1572 | equiclass = collclass = 0; |
| 1573 | charclass = get_char_class(®parse); |
| 1574 | if (charclass == CLASS_NONE) |
| 1575 | { |
| 1576 | equiclass = get_equi_class(®parse); |
| 1577 | if (equiclass == 0) |
| 1578 | collclass = get_coll_element(®parse); |
| 1579 | } |
| 1580 | |
| 1581 | /* Character class like [:alpha:] */ |
| 1582 | if (charclass != CLASS_NONE) |
| 1583 | { |
| 1584 | switch (charclass) |
| 1585 | { |
| 1586 | case CLASS_ALNUM: |
| 1587 | EMIT(NFA_CLASS_ALNUM); |
| 1588 | break; |
| 1589 | case CLASS_ALPHA: |
| 1590 | EMIT(NFA_CLASS_ALPHA); |
| 1591 | break; |
| 1592 | case CLASS_BLANK: |
| 1593 | EMIT(NFA_CLASS_BLANK); |
| 1594 | break; |
| 1595 | case CLASS_CNTRL: |
| 1596 | EMIT(NFA_CLASS_CNTRL); |
| 1597 | break; |
| 1598 | case CLASS_DIGIT: |
| 1599 | EMIT(NFA_CLASS_DIGIT); |
| 1600 | break; |
| 1601 | case CLASS_GRAPH: |
| 1602 | EMIT(NFA_CLASS_GRAPH); |
| 1603 | break; |
| 1604 | case CLASS_LOWER: |
| 1605 | EMIT(NFA_CLASS_LOWER); |
| 1606 | break; |
| 1607 | case CLASS_PRINT: |
| 1608 | EMIT(NFA_CLASS_PRINT); |
| 1609 | break; |
| 1610 | case CLASS_PUNCT: |
| 1611 | EMIT(NFA_CLASS_PUNCT); |
| 1612 | break; |
| 1613 | case CLASS_SPACE: |
| 1614 | EMIT(NFA_CLASS_SPACE); |
| 1615 | break; |
| 1616 | case CLASS_UPPER: |
| 1617 | EMIT(NFA_CLASS_UPPER); |
| 1618 | break; |
| 1619 | case CLASS_XDIGIT: |
| 1620 | EMIT(NFA_CLASS_XDIGIT); |
| 1621 | break; |
| 1622 | case CLASS_TAB: |
| 1623 | EMIT(NFA_CLASS_TAB); |
| 1624 | break; |
| 1625 | case CLASS_RETURN: |
| 1626 | EMIT(NFA_CLASS_RETURN); |
| 1627 | break; |
| 1628 | case CLASS_BACKSPACE: |
| 1629 | EMIT(NFA_CLASS_BACKSPACE); |
| 1630 | break; |
| 1631 | case CLASS_ESCAPE: |
| 1632 | EMIT(NFA_CLASS_ESCAPE); |
| 1633 | break; |
| 1634 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1635 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1636 | continue; |
| 1637 | } |
| 1638 | /* Try equivalence class [=a=] and the like */ |
| 1639 | if (equiclass != 0) |
| 1640 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1641 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1642 | if (result == FAIL) |
| 1643 | { |
| 1644 | /* should never happen */ |
| 1645 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1646 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1647 | continue; |
| 1648 | } |
| 1649 | /* Try collating class like [. .] */ |
| 1650 | if (collclass != 0) |
| 1651 | { |
| 1652 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1653 | /* Will emit the proper atom at the end of the |
| 1654 | * while loop. */ |
| 1655 | } |
| 1656 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1657 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1658 | * start character. */ |
| 1659 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1660 | { |
| 1661 | emit_range = TRUE; |
| 1662 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1663 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1664 | continue; /* reading the end of the range */ |
| 1665 | } |
| 1666 | |
| 1667 | /* Now handle simple and escaped characters. |
| 1668 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1669 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1670 | * 'cpoptions' is not included. |
| 1671 | * Posix doesn't recognize backslash at all. |
| 1672 | */ |
| 1673 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1674 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1675 | && regparse + 1 <= endp |
| 1676 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1677 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1678 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1679 | != NULL) |
| 1680 | ) |
| 1681 | ) |
| 1682 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1683 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1684 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1685 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1686 | startc = reg_string ? NL : NFA_NEWL; |
| 1687 | else |
| 1688 | if (*regparse == 'd' |
| 1689 | || *regparse == 'o' |
| 1690 | || *regparse == 'x' |
| 1691 | || *regparse == 'u' |
| 1692 | || *regparse == 'U' |
| 1693 | ) |
| 1694 | { |
| 1695 | /* TODO(RE) This needs more testing */ |
| 1696 | startc = coll_get_char(); |
| 1697 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1698 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1699 | } |
| 1700 | else |
| 1701 | { |
| 1702 | /* \r,\t,\e,\b */ |
| 1703 | startc = backslash_trans(*regparse); |
| 1704 | } |
| 1705 | } |
| 1706 | |
| 1707 | /* Normal printable char */ |
| 1708 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1709 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1710 | |
| 1711 | /* Previous char was '-', so this char is end of range. */ |
| 1712 | if (emit_range) |
| 1713 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1714 | endc = startc; |
| 1715 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1716 | if (startc > endc) |
| 1717 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1718 | |
| 1719 | if (endc > startc + 2) |
| 1720 | { |
| 1721 | /* Emit a range instead of the sequence of |
| 1722 | * individual characters. */ |
| 1723 | if (startc == 0) |
| 1724 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1725 | EMIT(1); |
| 1726 | else |
| 1727 | --post_ptr; /* remove NFA_CONCAT */ |
| 1728 | EMIT(endc); |
| 1729 | EMIT(NFA_RANGE); |
| 1730 | EMIT(NFA_CONCAT); |
| 1731 | } |
| 1732 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1733 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1734 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1735 | || (*mb_char2len)(endc) > 1)) |
| 1736 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1737 | /* Emit the characters in the range. |
| 1738 | * "startc" was already emitted, so skip it. |
| 1739 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1740 | for (c = startc + 1; c <= endc; c++) |
| 1741 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1742 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1743 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1744 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1745 | } |
| 1746 | else |
| 1747 | #endif |
| 1748 | { |
| 1749 | #ifdef EBCDIC |
| 1750 | int alpha_only = FALSE; |
| 1751 | |
| 1752 | /* for alphabetical range skip the gaps |
| 1753 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1754 | if (isalpha(startc) && isalpha(endc)) |
| 1755 | alpha_only = TRUE; |
| 1756 | #endif |
| 1757 | /* Emit the range. "startc" was already emitted, so |
| 1758 | * skip it. */ |
| 1759 | for (c = startc + 1; c <= endc; c++) |
| 1760 | #ifdef EBCDIC |
| 1761 | if (!alpha_only || isalpha(startc)) |
| 1762 | #endif |
| 1763 | { |
| 1764 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1765 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1766 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1767 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1768 | emit_range = FALSE; |
| 1769 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1770 | } |
| 1771 | else |
| 1772 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1773 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1774 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1775 | * Normally, simply emit startc. But if we get char |
| 1776 | * code=0 from a collating char, then replace it with |
| 1777 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1778 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1779 | * the backtracking engine. */ |
| 1780 | if (startc == NFA_NEWL) |
| 1781 | { |
| 1782 | /* Line break can't be matched as part of the |
| 1783 | * collection, add an OR below. But not for negated |
| 1784 | * range. */ |
| 1785 | if (!negated) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1786 | extra = NFA_ADD_NL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1787 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1788 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1789 | { |
| 1790 | if (got_coll_char == TRUE && startc == 0) |
| 1791 | EMIT(0x0a); |
| 1792 | else |
| 1793 | EMIT(startc); |
| 1794 | EMIT(NFA_CONCAT); |
| 1795 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1796 | } |
| 1797 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1798 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1799 | } /* while (p < endp) */ |
| 1800 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1801 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1802 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1803 | { |
| 1804 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1805 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1806 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1807 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1808 | /* skip the trailing ] */ |
| 1809 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1810 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1811 | |
| 1812 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1813 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1814 | EMIT(NFA_END_NEG_COLL); |
| 1815 | else |
| 1816 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1817 | |
| 1818 | /* \_[] also matches \n but it's not negated */ |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1819 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1820 | { |
| 1821 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1822 | EMIT(NFA_OR); |
| 1823 | } |
| 1824 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1825 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1826 | } /* if exists closing ] */ |
| 1827 | |
| 1828 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1829 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1830 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1831 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1832 | default: |
| 1833 | { |
| 1834 | #ifdef FEAT_MBYTE |
| 1835 | int plen; |
| 1836 | |
| 1837 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1838 | /* plen is length of current char with composing chars */ |
| 1839 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1840 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1841 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1842 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1843 | int i = 0; |
| 1844 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1845 | /* A base character plus composing characters, or just one |
| 1846 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1847 | * This requires creating a separate atom as if enclosing |
| 1848 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1849 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1850 | * building the postfix form, not the NFA itself; |
| 1851 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1852 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1853 | for (;;) |
| 1854 | { |
| 1855 | EMIT(c); |
| 1856 | if (i > 0) |
| 1857 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1858 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1859 | break; |
| 1860 | c = utf_ptr2char(old_regparse + i); |
| 1861 | } |
| 1862 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1863 | regparse = old_regparse + plen; |
| 1864 | } |
| 1865 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1866 | #endif |
| 1867 | { |
| 1868 | c = no_Magic(c); |
| 1869 | EMIT(c); |
| 1870 | } |
| 1871 | return OK; |
| 1872 | } |
| 1873 | } |
| 1874 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1875 | return OK; |
| 1876 | } |
| 1877 | |
| 1878 | /* |
| 1879 | * Parse something followed by possible [*+=]. |
| 1880 | * |
| 1881 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1882 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1883 | * characters: "", "a", "aa", etc. |
| 1884 | * |
| 1885 | * piece ::= atom |
| 1886 | * or atom multi |
| 1887 | */ |
| 1888 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 1889 | nfa_regpiece(void) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1890 | { |
| 1891 | int i; |
| 1892 | int op; |
| 1893 | int ret; |
| 1894 | long minval, maxval; |
| 1895 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1896 | parse_state_T old_state; |
| 1897 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1898 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1899 | int old_post_pos; |
| 1900 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1901 | int quest; |
| 1902 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1903 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1904 | * next. */ |
| 1905 | save_parse_state(&old_state); |
| 1906 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1907 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1908 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1909 | |
| 1910 | ret = nfa_regatom(); |
| 1911 | if (ret == FAIL) |
| 1912 | return FAIL; /* cascaded error */ |
| 1913 | |
| 1914 | op = peekchr(); |
| 1915 | if (re_multi_type(op) == NOT_MULTI) |
| 1916 | return OK; |
| 1917 | |
| 1918 | skipchr(); |
| 1919 | switch (op) |
| 1920 | { |
| 1921 | case Magic('*'): |
| 1922 | EMIT(NFA_STAR); |
| 1923 | break; |
| 1924 | |
| 1925 | case Magic('+'): |
| 1926 | /* |
| 1927 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1928 | * first and only submatch would be "aaa". But the backtracking |
| 1929 | * engine interprets the plus as "try matching one more time", and |
| 1930 | * a* matches a second time at the end of the input, the empty |
| 1931 | * string. |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1932 | * The submatch will be the empty string. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1933 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1934 | * In order to be consistent with the old engine, we replace |
| 1935 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1936 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1937 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1938 | curchr = -1; |
| 1939 | if (nfa_regatom() == FAIL) |
| 1940 | return FAIL; |
| 1941 | EMIT(NFA_STAR); |
| 1942 | EMIT(NFA_CONCAT); |
| 1943 | skipchr(); /* skip the \+ */ |
| 1944 | break; |
| 1945 | |
| 1946 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1947 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1948 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1949 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1950 | switch(op) |
| 1951 | { |
| 1952 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1953 | /* \@= */ |
| 1954 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1955 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1956 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1957 | /* \@! */ |
| 1958 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1959 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1960 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1961 | op = no_Magic(getchr()); |
| 1962 | if (op == '=') |
| 1963 | /* \@<= */ |
| 1964 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1965 | else if (op == '!') |
| 1966 | /* \@<! */ |
| 1967 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1968 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1969 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1970 | /* \@> */ |
| 1971 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1972 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1973 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1974 | if (i == 0) |
| 1975 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1976 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1977 | return FAIL; |
| 1978 | } |
| 1979 | EMIT(i); |
| 1980 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1981 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1982 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1983 | break; |
| 1984 | |
| 1985 | case Magic('?'): |
| 1986 | case Magic('='): |
| 1987 | EMIT(NFA_QUEST); |
| 1988 | break; |
| 1989 | |
| 1990 | case Magic('{'): |
| 1991 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1992 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1993 | * version of '?' |
| 1994 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1995 | * parenthesis have the same id |
| 1996 | */ |
| 1997 | |
| 1998 | greedy = TRUE; |
| 1999 | c2 = peekchr(); |
| 2000 | if (c2 == '-' || c2 == Magic('-')) |
| 2001 | { |
| 2002 | skipchr(); |
| 2003 | greedy = FALSE; |
| 2004 | } |
| 2005 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2006 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 2007 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2008 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 2009 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2010 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2011 | { |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2012 | if (greedy) /* { { (match the braces) */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2013 | /* \{}, \{0,} */ |
| 2014 | EMIT(NFA_STAR); |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2015 | else /* { { (match the braces) */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2016 | /* \{-}, \{-0,} */ |
| 2017 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2018 | break; |
| 2019 | } |
| 2020 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2021 | /* Special case: x{0} or x{-0} */ |
| 2022 | if (maxval == 0) |
| 2023 | { |
| 2024 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2025 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2026 | /* NFA_EMPTY is 0-length and works everywhere */ |
| 2027 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2028 | return OK; |
| 2029 | } |
| 2030 | |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2031 | /* The engine is very inefficient (uses too many states) when the |
Bram Moolenaar | a1d2c58 | 2015-02-10 18:18:17 +0100 | [diff] [blame] | 2032 | * maximum is much larger than the minimum and when the maximum is |
| 2033 | * large. Bail out if we can use the other engine. */ |
| 2034 | if ((nfa_re_flags & RE_AUTO) |
| 2035 | && (maxval > minval + 200 || maxval > 500)) |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 2036 | return FAIL; |
| 2037 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2038 | /* Ignore previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2039 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2040 | /* Save parse state after the repeated atom and the \{} */ |
| 2041 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2042 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2043 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 2044 | for (i = 0; i < maxval; i++) |
| 2045 | { |
| 2046 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2047 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2048 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2049 | if (nfa_regatom() == FAIL) |
| 2050 | return FAIL; |
| 2051 | /* after "minval" times, atoms are optional */ |
| 2052 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2053 | { |
| 2054 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2055 | { |
| 2056 | if (greedy) |
| 2057 | EMIT(NFA_STAR); |
| 2058 | else |
| 2059 | EMIT(NFA_STAR_NONGREEDY); |
| 2060 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2061 | else |
| 2062 | EMIT(quest); |
| 2063 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2064 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2065 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2066 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 2067 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2071 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2072 | curchr = -1; |
| 2073 | |
| 2074 | break; |
| 2075 | |
| 2076 | |
| 2077 | default: |
| 2078 | break; |
| 2079 | } /* end switch */ |
| 2080 | |
| 2081 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2082 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2083 | 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] | 2084 | |
| 2085 | return OK; |
| 2086 | } |
| 2087 | |
| 2088 | /* |
| 2089 | * Parse one or more pieces, concatenated. It matches a match for the |
| 2090 | * first piece, followed by a match for the second piece, etc. Example: |
| 2091 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 2092 | * |
| 2093 | * concat ::= piece |
| 2094 | * or piece piece |
| 2095 | * or piece piece piece |
| 2096 | * etc. |
| 2097 | */ |
| 2098 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2099 | nfa_regconcat(void) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2100 | { |
| 2101 | int cont = TRUE; |
| 2102 | int first = TRUE; |
| 2103 | |
| 2104 | while (cont) |
| 2105 | { |
| 2106 | switch (peekchr()) |
| 2107 | { |
| 2108 | case NUL: |
| 2109 | case Magic('|'): |
| 2110 | case Magic('&'): |
| 2111 | case Magic(')'): |
| 2112 | cont = FALSE; |
| 2113 | break; |
| 2114 | |
| 2115 | case Magic('Z'): |
| 2116 | #ifdef FEAT_MBYTE |
| 2117 | regflags |= RF_ICOMBINE; |
| 2118 | #endif |
| 2119 | skipchr_keepstart(); |
| 2120 | break; |
| 2121 | case Magic('c'): |
| 2122 | regflags |= RF_ICASE; |
| 2123 | skipchr_keepstart(); |
| 2124 | break; |
| 2125 | case Magic('C'): |
| 2126 | regflags |= RF_NOICASE; |
| 2127 | skipchr_keepstart(); |
| 2128 | break; |
| 2129 | case Magic('v'): |
| 2130 | reg_magic = MAGIC_ALL; |
| 2131 | skipchr_keepstart(); |
| 2132 | curchr = -1; |
| 2133 | break; |
| 2134 | case Magic('m'): |
| 2135 | reg_magic = MAGIC_ON; |
| 2136 | skipchr_keepstart(); |
| 2137 | curchr = -1; |
| 2138 | break; |
| 2139 | case Magic('M'): |
| 2140 | reg_magic = MAGIC_OFF; |
| 2141 | skipchr_keepstart(); |
| 2142 | curchr = -1; |
| 2143 | break; |
| 2144 | case Magic('V'): |
| 2145 | reg_magic = MAGIC_NONE; |
| 2146 | skipchr_keepstart(); |
| 2147 | curchr = -1; |
| 2148 | break; |
| 2149 | |
| 2150 | default: |
| 2151 | if (nfa_regpiece() == FAIL) |
| 2152 | return FAIL; |
| 2153 | if (first == FALSE) |
| 2154 | EMIT(NFA_CONCAT); |
| 2155 | else |
| 2156 | first = FALSE; |
| 2157 | break; |
| 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | return OK; |
| 2162 | } |
| 2163 | |
| 2164 | /* |
| 2165 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 2166 | * last concat, but only if all the preceding concats also match at the same |
| 2167 | * position. Examples: |
| 2168 | * "foobeep\&..." matches "foo" in "foobeep". |
| 2169 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 2170 | * |
| 2171 | * branch ::= concat |
| 2172 | * or concat \& concat |
| 2173 | * or concat \& concat \& concat |
| 2174 | * etc. |
| 2175 | */ |
| 2176 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2177 | nfa_regbranch(void) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2178 | { |
| 2179 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2180 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2181 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2182 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2183 | |
| 2184 | /* First branch, possibly the only one */ |
| 2185 | if (nfa_regconcat() == FAIL) |
| 2186 | return FAIL; |
| 2187 | |
| 2188 | ch = peekchr(); |
| 2189 | /* Try next concats */ |
| 2190 | while (ch == Magic('&')) |
| 2191 | { |
| 2192 | skipchr(); |
| 2193 | EMIT(NFA_NOPEN); |
| 2194 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2195 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2196 | if (nfa_regconcat() == FAIL) |
| 2197 | return FAIL; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2198 | /* if concat is empty do emit a node */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2199 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2200 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2201 | EMIT(NFA_CONCAT); |
| 2202 | ch = peekchr(); |
| 2203 | } |
| 2204 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2205 | /* if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2206 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2207 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2208 | |
| 2209 | return OK; |
| 2210 | } |
| 2211 | |
| 2212 | /* |
| 2213 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 2214 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 2215 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 2216 | * is used. |
| 2217 | * |
| 2218 | * pattern ::= branch |
| 2219 | * or branch \| branch |
| 2220 | * or branch \| branch \| branch |
| 2221 | * etc. |
| 2222 | */ |
| 2223 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2224 | nfa_reg( |
| 2225 | int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2226 | { |
| 2227 | int parno = 0; |
| 2228 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2229 | if (paren == REG_PAREN) |
| 2230 | { |
| 2231 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2232 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2233 | parno = regnpar++; |
| 2234 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2235 | #ifdef FEAT_SYN_HL |
| 2236 | else if (paren == REG_ZPAREN) |
| 2237 | { |
| 2238 | /* Make a ZOPEN node. */ |
| 2239 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2240 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2241 | parno = regnzpar++; |
| 2242 | } |
| 2243 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2244 | |
| 2245 | if (nfa_regbranch() == FAIL) |
| 2246 | return FAIL; /* cascaded error */ |
| 2247 | |
| 2248 | while (peekchr() == Magic('|')) |
| 2249 | { |
| 2250 | skipchr(); |
| 2251 | if (nfa_regbranch() == FAIL) |
| 2252 | return FAIL; /* cascaded error */ |
| 2253 | EMIT(NFA_OR); |
| 2254 | } |
| 2255 | |
| 2256 | /* Check for proper termination. */ |
| 2257 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 2258 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2259 | if (paren == REG_NPAREN) |
| 2260 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 2261 | else |
| 2262 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 2263 | } |
| 2264 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 2265 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2266 | if (peekchr() == Magic(')')) |
| 2267 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 2268 | else |
| 2269 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 2270 | } |
| 2271 | /* |
| 2272 | * Here we set the flag allowing back references to this set of |
| 2273 | * parentheses. |
| 2274 | */ |
| 2275 | if (paren == REG_PAREN) |
| 2276 | { |
| 2277 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 2278 | EMIT(NFA_MOPEN + parno); |
| 2279 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2280 | #ifdef FEAT_SYN_HL |
| 2281 | else if (paren == REG_ZPAREN) |
| 2282 | EMIT(NFA_ZOPEN + parno); |
| 2283 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2284 | |
| 2285 | return OK; |
| 2286 | } |
| 2287 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2288 | #ifdef DEBUG |
| 2289 | static char_u code[50]; |
| 2290 | |
| 2291 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2292 | nfa_set_code(int c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2293 | { |
| 2294 | int addnl = FALSE; |
| 2295 | |
| 2296 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 2297 | { |
| 2298 | addnl = TRUE; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2299 | c -= NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | STRCPY(code, ""); |
| 2303 | switch (c) |
| 2304 | { |
| 2305 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 2306 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 2307 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 2308 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 2309 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 2310 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 2311 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2312 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 2313 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 2314 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 2315 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 2316 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 2317 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 2318 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 2319 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 2320 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2321 | #ifdef FEAT_SYN_HL |
| 2322 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 2323 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 2324 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 2325 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 2326 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 2327 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 2328 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 2329 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 2330 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 2331 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2332 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 2333 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2334 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2335 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2336 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 2337 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2338 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2339 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 2340 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 2341 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2342 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 2343 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 2344 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2345 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 2346 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2347 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2348 | case NFA_START_INVISIBLE_FIRST: |
| 2349 | STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2350 | case NFA_START_INVISIBLE_NEG: |
| 2351 | STRCPY(code, "NFA_START_INVISIBLE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2352 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 2353 | STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2354 | case NFA_START_INVISIBLE_BEFORE: |
| 2355 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2356 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 2357 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2358 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2359 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2360 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 2361 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2362 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2363 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2364 | case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2365 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2366 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2367 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2368 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2369 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2370 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2371 | case NFA_MOPEN: |
| 2372 | case NFA_MOPEN1: |
| 2373 | case NFA_MOPEN2: |
| 2374 | case NFA_MOPEN3: |
| 2375 | case NFA_MOPEN4: |
| 2376 | case NFA_MOPEN5: |
| 2377 | case NFA_MOPEN6: |
| 2378 | case NFA_MOPEN7: |
| 2379 | case NFA_MOPEN8: |
| 2380 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2381 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2382 | code[10] = c - NFA_MOPEN + '0'; |
| 2383 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2384 | case NFA_MCLOSE: |
| 2385 | case NFA_MCLOSE1: |
| 2386 | case NFA_MCLOSE2: |
| 2387 | case NFA_MCLOSE3: |
| 2388 | case NFA_MCLOSE4: |
| 2389 | case NFA_MCLOSE5: |
| 2390 | case NFA_MCLOSE6: |
| 2391 | case NFA_MCLOSE7: |
| 2392 | case NFA_MCLOSE8: |
| 2393 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2394 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2395 | code[11] = c - NFA_MCLOSE + '0'; |
| 2396 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2397 | #ifdef FEAT_SYN_HL |
| 2398 | case NFA_ZOPEN: |
| 2399 | case NFA_ZOPEN1: |
| 2400 | case NFA_ZOPEN2: |
| 2401 | case NFA_ZOPEN3: |
| 2402 | case NFA_ZOPEN4: |
| 2403 | case NFA_ZOPEN5: |
| 2404 | case NFA_ZOPEN6: |
| 2405 | case NFA_ZOPEN7: |
| 2406 | case NFA_ZOPEN8: |
| 2407 | case NFA_ZOPEN9: |
| 2408 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2409 | code[10] = c - NFA_ZOPEN + '0'; |
| 2410 | break; |
| 2411 | case NFA_ZCLOSE: |
| 2412 | case NFA_ZCLOSE1: |
| 2413 | case NFA_ZCLOSE2: |
| 2414 | case NFA_ZCLOSE3: |
| 2415 | case NFA_ZCLOSE4: |
| 2416 | case NFA_ZCLOSE5: |
| 2417 | case NFA_ZCLOSE6: |
| 2418 | case NFA_ZCLOSE7: |
| 2419 | case NFA_ZCLOSE8: |
| 2420 | case NFA_ZCLOSE9: |
| 2421 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2422 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2423 | break; |
| 2424 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2425 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2426 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2427 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2428 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2429 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2430 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2431 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2432 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2433 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2434 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2435 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2436 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2437 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2438 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2439 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2440 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2441 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2442 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2443 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2444 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 2445 | case NFA_ANY_COMPOSING: STRCPY(code, "NFA_ANY_COMPOSING "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2446 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2447 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2448 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2449 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2450 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2451 | case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2452 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2453 | |
| 2454 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2455 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2456 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2457 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2458 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2459 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2460 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2461 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2462 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2463 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2464 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2465 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2466 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2467 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2468 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2469 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2470 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2471 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2472 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2473 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2474 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2475 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2476 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2477 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2478 | |
| 2479 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2480 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2481 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2482 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2483 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2484 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2485 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2486 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2487 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2488 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2489 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2490 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2491 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2492 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2493 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2494 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2495 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2496 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2497 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2498 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2499 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2500 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2501 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2502 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2503 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2504 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2505 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2506 | case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break; |
| 2507 | case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break; |
| 2508 | case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break; |
| 2509 | case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2510 | |
| 2511 | default: |
| 2512 | STRCPY(code, "CHAR(x)"); |
| 2513 | code[5] = c; |
| 2514 | } |
| 2515 | |
| 2516 | if (addnl == TRUE) |
| 2517 | STRCAT(code, " + NEWLINE "); |
| 2518 | |
| 2519 | } |
| 2520 | |
| 2521 | #ifdef ENABLE_LOG |
| 2522 | static FILE *log_fd; |
| 2523 | |
| 2524 | /* |
| 2525 | * Print the postfix notation of the current regexp. |
| 2526 | */ |
| 2527 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2528 | nfa_postfix_dump(char_u *expr, int retval) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2529 | { |
| 2530 | int *p; |
| 2531 | FILE *f; |
| 2532 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2533 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2534 | if (f != NULL) |
| 2535 | { |
| 2536 | fprintf(f, "\n-------------------------\n"); |
| 2537 | if (retval == FAIL) |
| 2538 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2539 | else if (retval == OK) |
| 2540 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2541 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2542 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2543 | { |
| 2544 | nfa_set_code(*p); |
| 2545 | fprintf(f, "%s, ", code); |
| 2546 | } |
| 2547 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2548 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2549 | fprintf(f, "%d ", *p); |
| 2550 | fprintf(f, "\n\n"); |
| 2551 | fclose(f); |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /* |
| 2556 | * Print the NFA starting with a root node "state". |
| 2557 | */ |
| 2558 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2559 | nfa_print_state(FILE *debugf, nfa_state_T *state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2560 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2561 | garray_T indent; |
| 2562 | |
| 2563 | ga_init2(&indent, 1, 64); |
| 2564 | ga_append(&indent, '\0'); |
| 2565 | nfa_print_state2(debugf, state, &indent); |
| 2566 | ga_clear(&indent); |
| 2567 | } |
| 2568 | |
| 2569 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2570 | nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent) |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2571 | { |
| 2572 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2573 | |
| 2574 | if (state == NULL) |
| 2575 | return; |
| 2576 | |
| 2577 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2578 | |
| 2579 | /* Output indent */ |
| 2580 | p = (char_u *)indent->ga_data; |
| 2581 | if (indent->ga_len >= 3) |
| 2582 | { |
| 2583 | int last = indent->ga_len - 3; |
| 2584 | char_u save[2]; |
| 2585 | |
| 2586 | STRNCPY(save, &p[last], 2); |
| 2587 | STRNCPY(&p[last], "+-", 2); |
| 2588 | fprintf(debugf, " %s", p); |
| 2589 | STRNCPY(&p[last], save, 2); |
| 2590 | } |
| 2591 | else |
| 2592 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2593 | |
| 2594 | nfa_set_code(state->c); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2595 | fprintf(debugf, "%s (%d) (id=%d) val=%d\n", |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2596 | code, |
| 2597 | state->c, |
| 2598 | abs(state->id), |
| 2599 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2600 | if (state->id < 0) |
| 2601 | return; |
| 2602 | |
| 2603 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2604 | |
| 2605 | /* grow indent for state->out */ |
| 2606 | indent->ga_len -= 1; |
| 2607 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2608 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2609 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2610 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2611 | ga_append(indent, '\0'); |
| 2612 | |
| 2613 | nfa_print_state2(debugf, state->out, indent); |
| 2614 | |
| 2615 | /* replace last part of indent for state->out1 */ |
| 2616 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2617 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2618 | ga_append(indent, '\0'); |
| 2619 | |
| 2620 | nfa_print_state2(debugf, state->out1, indent); |
| 2621 | |
| 2622 | /* shrink indent */ |
| 2623 | indent->ga_len -= 3; |
| 2624 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2625 | } |
| 2626 | |
| 2627 | /* |
| 2628 | * Print the NFA state machine. |
| 2629 | */ |
| 2630 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2631 | nfa_dump(nfa_regprog_T *prog) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2632 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2633 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2634 | |
| 2635 | if (debugf != NULL) |
| 2636 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2637 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2638 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 2639 | if (prog->reganch) |
| 2640 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2641 | if (prog->regstart != NUL) |
| 2642 | fprintf(debugf, "regstart: %c (decimal: %d)\n", |
| 2643 | prog->regstart, prog->regstart); |
| 2644 | if (prog->match_text != NULL) |
| 2645 | fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2646 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2647 | fclose(debugf); |
| 2648 | } |
| 2649 | } |
| 2650 | #endif /* ENABLE_LOG */ |
| 2651 | #endif /* DEBUG */ |
| 2652 | |
| 2653 | /* |
| 2654 | * Parse r.e. @expr and convert it into postfix form. |
| 2655 | * Return the postfix string on success, NULL otherwise. |
| 2656 | */ |
| 2657 | static int * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2658 | re2post(void) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2659 | { |
| 2660 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2661 | return NULL; |
| 2662 | EMIT(NFA_MOPEN); |
| 2663 | return post_start; |
| 2664 | } |
| 2665 | |
| 2666 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2667 | |
| 2668 | /* |
| 2669 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2670 | * if c == MATCH, no arrows out; matching state. |
| 2671 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2672 | * If c < 256, labeled arrow with character c to out. |
| 2673 | */ |
| 2674 | |
| 2675 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2676 | |
| 2677 | /* |
| 2678 | * Allocate and initialize nfa_state_T. |
| 2679 | */ |
| 2680 | static nfa_state_T * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2681 | alloc_state(int c, nfa_state_T *out, nfa_state_T *out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2682 | { |
| 2683 | nfa_state_T *s; |
| 2684 | |
| 2685 | if (istate >= nstate) |
| 2686 | return NULL; |
| 2687 | |
| 2688 | s = &state_ptr[istate++]; |
| 2689 | |
| 2690 | s->c = c; |
| 2691 | s->out = out; |
| 2692 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2693 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2694 | |
| 2695 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2696 | s->lastlist[0] = 0; |
| 2697 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2698 | |
| 2699 | return s; |
| 2700 | } |
| 2701 | |
| 2702 | /* |
| 2703 | * A partially built NFA without the matching state filled in. |
| 2704 | * Frag_T.start points at the start state. |
| 2705 | * Frag_T.out is a list of places that need to be set to the |
| 2706 | * next state for this fragment. |
| 2707 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2708 | |
| 2709 | /* Since the out pointers in the list are always |
| 2710 | * uninitialized, we use the pointers themselves |
| 2711 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2712 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2713 | union Ptrlist |
| 2714 | { |
| 2715 | Ptrlist *next; |
| 2716 | nfa_state_T *s; |
| 2717 | }; |
| 2718 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2719 | struct Frag |
| 2720 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2721 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2722 | Ptrlist *out; |
| 2723 | }; |
| 2724 | typedef struct Frag Frag_T; |
| 2725 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 2726 | static Frag_T frag(nfa_state_T *start, Ptrlist *out); |
| 2727 | static Ptrlist *list1(nfa_state_T **outp); |
| 2728 | static void patch(Ptrlist *l, nfa_state_T *s); |
| 2729 | static Ptrlist *append(Ptrlist *l1, Ptrlist *l2); |
| 2730 | static void st_push(Frag_T s, Frag_T **p, Frag_T *stack_end); |
| 2731 | static Frag_T st_pop(Frag_T **p, Frag_T *stack); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2732 | |
| 2733 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2734 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2735 | */ |
| 2736 | static Frag_T |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2737 | frag(nfa_state_T *start, Ptrlist *out) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2738 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2739 | Frag_T n; |
| 2740 | |
| 2741 | n.start = start; |
| 2742 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2743 | return n; |
| 2744 | } |
| 2745 | |
| 2746 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2747 | * Create singleton list containing just outp. |
| 2748 | */ |
| 2749 | static Ptrlist * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2750 | list1( |
| 2751 | nfa_state_T **outp) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2752 | { |
| 2753 | Ptrlist *l; |
| 2754 | |
| 2755 | l = (Ptrlist *)outp; |
| 2756 | l->next = NULL; |
| 2757 | return l; |
| 2758 | } |
| 2759 | |
| 2760 | /* |
| 2761 | * Patch the list of states at out to point to start. |
| 2762 | */ |
| 2763 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2764 | patch(Ptrlist *l, nfa_state_T *s) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2765 | { |
| 2766 | Ptrlist *next; |
| 2767 | |
| 2768 | for (; l; l = next) |
| 2769 | { |
| 2770 | next = l->next; |
| 2771 | l->s = s; |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | |
| 2776 | /* |
| 2777 | * Join the two lists l1 and l2, returning the combination. |
| 2778 | */ |
| 2779 | static Ptrlist * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2780 | append(Ptrlist *l1, Ptrlist *l2) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2781 | { |
| 2782 | Ptrlist *oldl1; |
| 2783 | |
| 2784 | oldl1 = l1; |
| 2785 | while (l1->next) |
| 2786 | l1 = l1->next; |
| 2787 | l1->next = l2; |
| 2788 | return oldl1; |
| 2789 | } |
| 2790 | |
| 2791 | /* |
| 2792 | * Stack used for transforming postfix form into NFA. |
| 2793 | */ |
| 2794 | static Frag_T empty; |
| 2795 | |
| 2796 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2797 | st_error(int *postfix UNUSED, int *end UNUSED, int *p UNUSED) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2798 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2799 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2800 | FILE *df; |
| 2801 | int *p2; |
| 2802 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2803 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2804 | if (df) |
| 2805 | { |
| 2806 | fprintf(df, "Error popping the stack!\n"); |
| 2807 | #ifdef DEBUG |
| 2808 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2809 | #endif |
| 2810 | fprintf(df, "Postfix form is: "); |
| 2811 | #ifdef DEBUG |
| 2812 | for (p2 = postfix; p2 < end; p2++) |
| 2813 | { |
| 2814 | nfa_set_code(*p2); |
| 2815 | fprintf(df, "%s, ", code); |
| 2816 | } |
| 2817 | nfa_set_code(*p); |
| 2818 | fprintf(df, "\nCurrent position is: "); |
| 2819 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2820 | { |
| 2821 | nfa_set_code(*p2); |
| 2822 | fprintf(df, "%s, ", code); |
| 2823 | } |
| 2824 | #else |
| 2825 | for (p2 = postfix; p2 < end; p2++) |
| 2826 | { |
| 2827 | fprintf(df, "%d, ", *p2); |
| 2828 | } |
| 2829 | fprintf(df, "\nCurrent position is: "); |
| 2830 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2831 | { |
| 2832 | fprintf(df, "%d, ", *p2); |
| 2833 | } |
| 2834 | #endif |
| 2835 | fprintf(df, "\n--------------------------\n"); |
| 2836 | fclose(df); |
| 2837 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2838 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2839 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2840 | } |
| 2841 | |
| 2842 | /* |
| 2843 | * Push an item onto the stack. |
| 2844 | */ |
| 2845 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2846 | st_push(Frag_T s, Frag_T **p, Frag_T *stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2847 | { |
| 2848 | Frag_T *stackp = *p; |
| 2849 | |
| 2850 | if (stackp >= stack_end) |
| 2851 | return; |
| 2852 | *stackp = s; |
| 2853 | *p = *p + 1; |
| 2854 | } |
| 2855 | |
| 2856 | /* |
| 2857 | * Pop an item from the stack. |
| 2858 | */ |
| 2859 | static Frag_T |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2860 | st_pop(Frag_T **p, Frag_T *stack) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2861 | { |
| 2862 | Frag_T *stackp; |
| 2863 | |
| 2864 | *p = *p - 1; |
| 2865 | stackp = *p; |
| 2866 | if (stackp < stack) |
| 2867 | return empty; |
| 2868 | return **p; |
| 2869 | } |
| 2870 | |
| 2871 | /* |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2872 | * Estimate the maximum byte length of anything matching "state". |
| 2873 | * When unknown or unlimited return -1. |
| 2874 | */ |
| 2875 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 2876 | nfa_max_width(nfa_state_T *startstate, int depth) |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2877 | { |
| 2878 | int l, r; |
| 2879 | nfa_state_T *state = startstate; |
| 2880 | int len = 0; |
| 2881 | |
| 2882 | /* detect looping in a NFA_SPLIT */ |
| 2883 | if (depth > 4) |
| 2884 | return -1; |
| 2885 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 2886 | while (state != NULL) |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2887 | { |
| 2888 | switch (state->c) |
| 2889 | { |
| 2890 | case NFA_END_INVISIBLE: |
| 2891 | case NFA_END_INVISIBLE_NEG: |
| 2892 | /* the end, return what we have */ |
| 2893 | return len; |
| 2894 | |
| 2895 | case NFA_SPLIT: |
| 2896 | /* two alternatives, use the maximum */ |
| 2897 | l = nfa_max_width(state->out, depth + 1); |
| 2898 | r = nfa_max_width(state->out1, depth + 1); |
| 2899 | if (l < 0 || r < 0) |
| 2900 | return -1; |
| 2901 | return len + (l > r ? l : r); |
| 2902 | |
| 2903 | case NFA_ANY: |
| 2904 | case NFA_START_COLL: |
| 2905 | case NFA_START_NEG_COLL: |
| 2906 | /* matches some character, including composing chars */ |
| 2907 | #ifdef FEAT_MBYTE |
| 2908 | if (enc_utf8) |
| 2909 | len += MB_MAXBYTES; |
| 2910 | else if (has_mbyte) |
| 2911 | len += 2; |
| 2912 | else |
| 2913 | #endif |
| 2914 | ++len; |
| 2915 | if (state->c != NFA_ANY) |
| 2916 | { |
| 2917 | /* skip over the characters */ |
| 2918 | state = state->out1->out; |
| 2919 | continue; |
| 2920 | } |
| 2921 | break; |
| 2922 | |
| 2923 | case NFA_DIGIT: |
| 2924 | case NFA_WHITE: |
| 2925 | case NFA_HEX: |
| 2926 | case NFA_OCTAL: |
| 2927 | /* ascii */ |
| 2928 | ++len; |
| 2929 | break; |
| 2930 | |
| 2931 | case NFA_IDENT: |
| 2932 | case NFA_SIDENT: |
| 2933 | case NFA_KWORD: |
| 2934 | case NFA_SKWORD: |
| 2935 | case NFA_FNAME: |
| 2936 | case NFA_SFNAME: |
| 2937 | case NFA_PRINT: |
| 2938 | case NFA_SPRINT: |
| 2939 | case NFA_NWHITE: |
| 2940 | case NFA_NDIGIT: |
| 2941 | case NFA_NHEX: |
| 2942 | case NFA_NOCTAL: |
| 2943 | case NFA_WORD: |
| 2944 | case NFA_NWORD: |
| 2945 | case NFA_HEAD: |
| 2946 | case NFA_NHEAD: |
| 2947 | case NFA_ALPHA: |
| 2948 | case NFA_NALPHA: |
| 2949 | case NFA_LOWER: |
| 2950 | case NFA_NLOWER: |
| 2951 | case NFA_UPPER: |
| 2952 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2953 | case NFA_LOWER_IC: |
| 2954 | case NFA_NLOWER_IC: |
| 2955 | case NFA_UPPER_IC: |
| 2956 | case NFA_NUPPER_IC: |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 2957 | case NFA_ANY_COMPOSING: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2958 | /* possibly non-ascii */ |
| 2959 | #ifdef FEAT_MBYTE |
| 2960 | if (has_mbyte) |
| 2961 | len += 3; |
| 2962 | else |
| 2963 | #endif |
| 2964 | ++len; |
| 2965 | break; |
| 2966 | |
| 2967 | case NFA_START_INVISIBLE: |
| 2968 | case NFA_START_INVISIBLE_NEG: |
| 2969 | case NFA_START_INVISIBLE_BEFORE: |
| 2970 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2971 | /* zero-width, out1 points to the END state */ |
| 2972 | state = state->out1->out; |
| 2973 | continue; |
| 2974 | |
| 2975 | case NFA_BACKREF1: |
| 2976 | case NFA_BACKREF2: |
| 2977 | case NFA_BACKREF3: |
| 2978 | case NFA_BACKREF4: |
| 2979 | case NFA_BACKREF5: |
| 2980 | case NFA_BACKREF6: |
| 2981 | case NFA_BACKREF7: |
| 2982 | case NFA_BACKREF8: |
| 2983 | case NFA_BACKREF9: |
| 2984 | #ifdef FEAT_SYN_HL |
| 2985 | case NFA_ZREF1: |
| 2986 | case NFA_ZREF2: |
| 2987 | case NFA_ZREF3: |
| 2988 | case NFA_ZREF4: |
| 2989 | case NFA_ZREF5: |
| 2990 | case NFA_ZREF6: |
| 2991 | case NFA_ZREF7: |
| 2992 | case NFA_ZREF8: |
| 2993 | case NFA_ZREF9: |
| 2994 | #endif |
| 2995 | case NFA_NEWL: |
| 2996 | case NFA_SKIP: |
| 2997 | /* unknown width */ |
| 2998 | return -1; |
| 2999 | |
| 3000 | case NFA_BOL: |
| 3001 | case NFA_EOL: |
| 3002 | case NFA_BOF: |
| 3003 | case NFA_EOF: |
| 3004 | case NFA_BOW: |
| 3005 | case NFA_EOW: |
| 3006 | case NFA_MOPEN: |
| 3007 | case NFA_MOPEN1: |
| 3008 | case NFA_MOPEN2: |
| 3009 | case NFA_MOPEN3: |
| 3010 | case NFA_MOPEN4: |
| 3011 | case NFA_MOPEN5: |
| 3012 | case NFA_MOPEN6: |
| 3013 | case NFA_MOPEN7: |
| 3014 | case NFA_MOPEN8: |
| 3015 | case NFA_MOPEN9: |
| 3016 | #ifdef FEAT_SYN_HL |
| 3017 | case NFA_ZOPEN: |
| 3018 | case NFA_ZOPEN1: |
| 3019 | case NFA_ZOPEN2: |
| 3020 | case NFA_ZOPEN3: |
| 3021 | case NFA_ZOPEN4: |
| 3022 | case NFA_ZOPEN5: |
| 3023 | case NFA_ZOPEN6: |
| 3024 | case NFA_ZOPEN7: |
| 3025 | case NFA_ZOPEN8: |
| 3026 | case NFA_ZOPEN9: |
| 3027 | case NFA_ZCLOSE: |
| 3028 | case NFA_ZCLOSE1: |
| 3029 | case NFA_ZCLOSE2: |
| 3030 | case NFA_ZCLOSE3: |
| 3031 | case NFA_ZCLOSE4: |
| 3032 | case NFA_ZCLOSE5: |
| 3033 | case NFA_ZCLOSE6: |
| 3034 | case NFA_ZCLOSE7: |
| 3035 | case NFA_ZCLOSE8: |
| 3036 | case NFA_ZCLOSE9: |
| 3037 | #endif |
| 3038 | case NFA_MCLOSE: |
| 3039 | case NFA_MCLOSE1: |
| 3040 | case NFA_MCLOSE2: |
| 3041 | case NFA_MCLOSE3: |
| 3042 | case NFA_MCLOSE4: |
| 3043 | case NFA_MCLOSE5: |
| 3044 | case NFA_MCLOSE6: |
| 3045 | case NFA_MCLOSE7: |
| 3046 | case NFA_MCLOSE8: |
| 3047 | case NFA_MCLOSE9: |
| 3048 | case NFA_NOPEN: |
| 3049 | case NFA_NCLOSE: |
| 3050 | |
| 3051 | case NFA_LNUM_GT: |
| 3052 | case NFA_LNUM_LT: |
| 3053 | case NFA_COL_GT: |
| 3054 | case NFA_COL_LT: |
| 3055 | case NFA_VCOL_GT: |
| 3056 | case NFA_VCOL_LT: |
| 3057 | case NFA_MARK_GT: |
| 3058 | case NFA_MARK_LT: |
| 3059 | case NFA_VISUAL: |
| 3060 | case NFA_LNUM: |
| 3061 | case NFA_CURSOR: |
| 3062 | case NFA_COL: |
| 3063 | case NFA_VCOL: |
| 3064 | case NFA_MARK: |
| 3065 | |
| 3066 | case NFA_ZSTART: |
| 3067 | case NFA_ZEND: |
| 3068 | case NFA_OPT_CHARS: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3069 | case NFA_EMPTY: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3070 | case NFA_START_PATTERN: |
| 3071 | case NFA_END_PATTERN: |
| 3072 | case NFA_COMPOSING: |
| 3073 | case NFA_END_COMPOSING: |
| 3074 | /* zero-width */ |
| 3075 | break; |
| 3076 | |
| 3077 | default: |
| 3078 | if (state->c < 0) |
| 3079 | /* don't know what this is */ |
| 3080 | return -1; |
| 3081 | /* normal character */ |
| 3082 | len += MB_CHAR2LEN(state->c); |
| 3083 | break; |
| 3084 | } |
| 3085 | |
| 3086 | /* normal way to continue */ |
| 3087 | state = state->out; |
| 3088 | } |
| 3089 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 3090 | /* unrecognized, "cannot happen" */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3091 | return -1; |
| 3092 | } |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3093 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3094 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3095 | * Convert a postfix form into its equivalent NFA. |
| 3096 | * Return the NFA start state on success, NULL otherwise. |
| 3097 | */ |
| 3098 | static nfa_state_T * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3099 | post2nfa(int *postfix, int *end, int nfa_calc_size) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3100 | { |
| 3101 | int *p; |
| 3102 | int mopen; |
| 3103 | int mclose; |
| 3104 | Frag_T *stack = NULL; |
| 3105 | Frag_T *stackp = NULL; |
| 3106 | Frag_T *stack_end = NULL; |
| 3107 | Frag_T e1; |
| 3108 | Frag_T e2; |
| 3109 | Frag_T e; |
| 3110 | nfa_state_T *s; |
| 3111 | nfa_state_T *s1; |
| 3112 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3113 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3114 | |
| 3115 | if (postfix == NULL) |
| 3116 | return NULL; |
| 3117 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 3118 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3119 | #define POP() st_pop(&stackp, stack); \ |
| 3120 | if (stackp < stack) \ |
| 3121 | { \ |
| 3122 | st_error(postfix, end, p); \ |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3123 | vim_free(stack); \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3124 | return NULL; \ |
| 3125 | } |
| 3126 | |
| 3127 | if (nfa_calc_size == FALSE) |
| 3128 | { |
| 3129 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3130 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3131 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 3132 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3133 | } |
| 3134 | |
| 3135 | for (p = postfix; p < end; ++p) |
| 3136 | { |
| 3137 | switch (*p) |
| 3138 | { |
| 3139 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3140 | /* Concatenation. |
| 3141 | * Pay attention: this operator does not exist in the r.e. itself |
| 3142 | * (it is implicit, really). It is added when r.e. is translated |
| 3143 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3144 | if (nfa_calc_size == TRUE) |
| 3145 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3146 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3147 | break; |
| 3148 | } |
| 3149 | e2 = POP(); |
| 3150 | e1 = POP(); |
| 3151 | patch(e1.out, e2.start); |
| 3152 | PUSH(frag(e1.start, e2.out)); |
| 3153 | break; |
| 3154 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3155 | case NFA_OR: |
| 3156 | /* Alternation */ |
| 3157 | if (nfa_calc_size == TRUE) |
| 3158 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3159 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3160 | break; |
| 3161 | } |
| 3162 | e2 = POP(); |
| 3163 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3164 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3165 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3166 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3167 | PUSH(frag(s, append(e1.out, e2.out))); |
| 3168 | break; |
| 3169 | |
| 3170 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3171 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3172 | if (nfa_calc_size == TRUE) |
| 3173 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3174 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3175 | break; |
| 3176 | } |
| 3177 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3178 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3179 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3180 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3181 | patch(e.out, s); |
| 3182 | PUSH(frag(s, list1(&s->out1))); |
| 3183 | break; |
| 3184 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3185 | case NFA_STAR_NONGREEDY: |
| 3186 | /* Zero or more, prefer zero */ |
| 3187 | if (nfa_calc_size == TRUE) |
| 3188 | { |
| 3189 | nstate++; |
| 3190 | break; |
| 3191 | } |
| 3192 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3193 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3194 | if (s == NULL) |
| 3195 | goto theend; |
| 3196 | patch(e.out, s); |
| 3197 | PUSH(frag(s, list1(&s->out))); |
| 3198 | break; |
| 3199 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3200 | case NFA_QUEST: |
| 3201 | /* one or zero atoms=> greedy match */ |
| 3202 | if (nfa_calc_size == TRUE) |
| 3203 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3204 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3205 | break; |
| 3206 | } |
| 3207 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3208 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3209 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3210 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3211 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 3212 | break; |
| 3213 | |
| 3214 | case NFA_QUEST_NONGREEDY: |
| 3215 | /* zero or one atoms => non-greedy match */ |
| 3216 | if (nfa_calc_size == TRUE) |
| 3217 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3218 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3219 | break; |
| 3220 | } |
| 3221 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3222 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3223 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3224 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3225 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 3226 | break; |
| 3227 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3228 | case NFA_END_COLL: |
| 3229 | case NFA_END_NEG_COLL: |
| 3230 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 3231 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 3232 | * add the output to the start. */ |
| 3233 | if (nfa_calc_size == TRUE) |
| 3234 | { |
| 3235 | nstate++; |
| 3236 | break; |
| 3237 | } |
| 3238 | e = POP(); |
| 3239 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 3240 | if (s == NULL) |
| 3241 | goto theend; |
| 3242 | patch(e.out, s); |
| 3243 | e.start->out1 = s; |
| 3244 | PUSH(frag(e.start, list1(&s->out))); |
| 3245 | break; |
| 3246 | |
| 3247 | case NFA_RANGE: |
| 3248 | /* Before this are two characters, the low and high end of a |
| 3249 | * range. Turn them into two states with MIN and MAX. */ |
| 3250 | if (nfa_calc_size == TRUE) |
| 3251 | { |
| 3252 | /* nstate += 0; */ |
| 3253 | break; |
| 3254 | } |
| 3255 | e2 = POP(); |
| 3256 | e1 = POP(); |
| 3257 | e2.start->val = e2.start->c; |
| 3258 | e2.start->c = NFA_RANGE_MAX; |
| 3259 | e1.start->val = e1.start->c; |
| 3260 | e1.start->c = NFA_RANGE_MIN; |
| 3261 | patch(e1.out, e2.start); |
| 3262 | PUSH(frag(e1.start, e2.out)); |
| 3263 | break; |
| 3264 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3265 | case NFA_EMPTY: |
| 3266 | /* 0-length, used in a repetition with max/min count of 0 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3267 | if (nfa_calc_size == TRUE) |
| 3268 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3269 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3270 | break; |
| 3271 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3272 | s = alloc_state(NFA_EMPTY, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3273 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3274 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3275 | PUSH(frag(s, list1(&s->out))); |
| 3276 | break; |
| 3277 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3278 | case NFA_OPT_CHARS: |
| 3279 | { |
| 3280 | int n; |
| 3281 | |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 3282 | /* \%[abc] implemented as: |
| 3283 | * NFA_SPLIT |
| 3284 | * +-CHAR(a) |
| 3285 | * | +-NFA_SPLIT |
| 3286 | * | +-CHAR(b) |
| 3287 | * | | +-NFA_SPLIT |
| 3288 | * | | +-CHAR(c) |
| 3289 | * | | | +-next |
| 3290 | * | | +- next |
| 3291 | * | +- next |
| 3292 | * +- next |
| 3293 | */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3294 | n = *++p; /* get number of characters */ |
| 3295 | if (nfa_calc_size == TRUE) |
| 3296 | { |
| 3297 | nstate += n; |
| 3298 | break; |
| 3299 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 3300 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3301 | e1.out = NULL; /* stores list with out1's */ |
| 3302 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 3303 | while (n-- > 0) |
| 3304 | { |
| 3305 | e = POP(); /* get character */ |
| 3306 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 3307 | if (s == NULL) |
| 3308 | goto theend; |
| 3309 | if (e1.out == NULL) |
| 3310 | e1 = e; |
| 3311 | patch(e.out, s1); |
| 3312 | append(e1.out, list1(&s->out1)); |
| 3313 | s1 = s; |
| 3314 | } |
| 3315 | PUSH(frag(s, e1.out)); |
| 3316 | break; |
| 3317 | } |
| 3318 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3319 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3320 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3321 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3322 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3323 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3324 | { |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3325 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 3326 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3327 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3328 | int start_state; |
| 3329 | int end_state; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3330 | int n = 0; |
| 3331 | nfa_state_T *zend; |
| 3332 | nfa_state_T *skip; |
| 3333 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3334 | switch (*p) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3335 | { |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3336 | case NFA_PREV_ATOM_NO_WIDTH: |
| 3337 | start_state = NFA_START_INVISIBLE; |
| 3338 | end_state = NFA_END_INVISIBLE; |
| 3339 | break; |
| 3340 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 3341 | start_state = NFA_START_INVISIBLE_NEG; |
| 3342 | end_state = NFA_END_INVISIBLE_NEG; |
| 3343 | break; |
| 3344 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3345 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 3346 | end_state = NFA_END_INVISIBLE; |
| 3347 | break; |
| 3348 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 3349 | start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
| 3350 | end_state = NFA_END_INVISIBLE_NEG; |
| 3351 | break; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 3352 | default: /* NFA_PREV_ATOM_LIKE_PATTERN: */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3353 | start_state = NFA_START_PATTERN; |
| 3354 | end_state = NFA_END_PATTERN; |
| 3355 | break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3356 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3357 | |
| 3358 | if (before) |
| 3359 | n = *++p; /* get the count */ |
| 3360 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3361 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3362 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3363 | * The \@<= operator: match for the preceding atom. |
| 3364 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3365 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3366 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3367 | |
| 3368 | if (nfa_calc_size == TRUE) |
| 3369 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3370 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3371 | break; |
| 3372 | } |
| 3373 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3374 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3375 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3376 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3377 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3378 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3379 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3380 | goto theend; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3381 | if (pattern) |
| 3382 | { |
| 3383 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 3384 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 3385 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 3386 | s1->out= skip; |
| 3387 | patch(e.out, zend); |
| 3388 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3389 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3390 | else |
| 3391 | { |
| 3392 | patch(e.out, s1); |
| 3393 | PUSH(frag(s, list1(&s1->out))); |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3394 | if (before) |
| 3395 | { |
| 3396 | if (n <= 0) |
| 3397 | /* See if we can guess the maximum width, it avoids a |
| 3398 | * lot of pointless tries. */ |
| 3399 | n = nfa_max_width(e.start, 0); |
| 3400 | s->val = n; /* store the count */ |
| 3401 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3402 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3403 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3404 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3405 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3406 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3407 | case NFA_COMPOSING: /* char with composing char */ |
| 3408 | #if 0 |
| 3409 | /* TODO */ |
| 3410 | if (regflags & RF_ICOMBINE) |
| 3411 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3412 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3413 | } |
| 3414 | #endif |
| 3415 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3416 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3417 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3418 | case NFA_MOPEN: /* \( \) Submatch */ |
| 3419 | case NFA_MOPEN1: |
| 3420 | case NFA_MOPEN2: |
| 3421 | case NFA_MOPEN3: |
| 3422 | case NFA_MOPEN4: |
| 3423 | case NFA_MOPEN5: |
| 3424 | case NFA_MOPEN6: |
| 3425 | case NFA_MOPEN7: |
| 3426 | case NFA_MOPEN8: |
| 3427 | case NFA_MOPEN9: |
| 3428 | #ifdef FEAT_SYN_HL |
| 3429 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 3430 | case NFA_ZOPEN1: |
| 3431 | case NFA_ZOPEN2: |
| 3432 | case NFA_ZOPEN3: |
| 3433 | case NFA_ZOPEN4: |
| 3434 | case NFA_ZOPEN5: |
| 3435 | case NFA_ZOPEN6: |
| 3436 | case NFA_ZOPEN7: |
| 3437 | case NFA_ZOPEN8: |
| 3438 | case NFA_ZOPEN9: |
| 3439 | #endif |
| 3440 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3441 | if (nfa_calc_size == TRUE) |
| 3442 | { |
| 3443 | nstate += 2; |
| 3444 | break; |
| 3445 | } |
| 3446 | |
| 3447 | mopen = *p; |
| 3448 | switch (*p) |
| 3449 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3450 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 3451 | #ifdef FEAT_SYN_HL |
| 3452 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 3453 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 3454 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 3455 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 3456 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 3457 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 3458 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 3459 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 3460 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 3461 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 3462 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3463 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3464 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3465 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3466 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3467 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3468 | mclose = *p + NSUBEXP; |
| 3469 | break; |
| 3470 | } |
| 3471 | |
| 3472 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 3473 | * the empty regexp "". In this case, the NFA will be |
| 3474 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 3475 | * empty groups of parenthesis, and empty mbyte chars */ |
| 3476 | if (stackp == stack) |
| 3477 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3478 | s = alloc_state(mopen, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3479 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3480 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3481 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3482 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3483 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3484 | patch(list1(&s->out), s1); |
| 3485 | PUSH(frag(s, list1(&s1->out))); |
| 3486 | break; |
| 3487 | } |
| 3488 | |
| 3489 | /* At least one node was emitted before NFA_MOPEN, so |
| 3490 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 3491 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3492 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3493 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3494 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3495 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3496 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3497 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3498 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3499 | patch(e.out, s1); |
| 3500 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3501 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3502 | if (mopen == NFA_COMPOSING) |
| 3503 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3504 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3505 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3506 | |
| 3507 | PUSH(frag(s, list1(&s1->out))); |
| 3508 | break; |
| 3509 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3510 | case NFA_BACKREF1: |
| 3511 | case NFA_BACKREF2: |
| 3512 | case NFA_BACKREF3: |
| 3513 | case NFA_BACKREF4: |
| 3514 | case NFA_BACKREF5: |
| 3515 | case NFA_BACKREF6: |
| 3516 | case NFA_BACKREF7: |
| 3517 | case NFA_BACKREF8: |
| 3518 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3519 | #ifdef FEAT_SYN_HL |
| 3520 | case NFA_ZREF1: |
| 3521 | case NFA_ZREF2: |
| 3522 | case NFA_ZREF3: |
| 3523 | case NFA_ZREF4: |
| 3524 | case NFA_ZREF5: |
| 3525 | case NFA_ZREF6: |
| 3526 | case NFA_ZREF7: |
| 3527 | case NFA_ZREF8: |
| 3528 | case NFA_ZREF9: |
| 3529 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3530 | if (nfa_calc_size == TRUE) |
| 3531 | { |
| 3532 | nstate += 2; |
| 3533 | break; |
| 3534 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3535 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3536 | if (s == NULL) |
| 3537 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3538 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3539 | if (s1 == NULL) |
| 3540 | goto theend; |
| 3541 | patch(list1(&s->out), s1); |
| 3542 | PUSH(frag(s, list1(&s1->out))); |
| 3543 | break; |
| 3544 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3545 | case NFA_LNUM: |
| 3546 | case NFA_LNUM_GT: |
| 3547 | case NFA_LNUM_LT: |
| 3548 | case NFA_VCOL: |
| 3549 | case NFA_VCOL_GT: |
| 3550 | case NFA_VCOL_LT: |
| 3551 | case NFA_COL: |
| 3552 | case NFA_COL_GT: |
| 3553 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 3554 | case NFA_MARK: |
| 3555 | case NFA_MARK_GT: |
| 3556 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3557 | { |
| 3558 | int n = *++p; /* lnum, col or mark name */ |
| 3559 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3560 | if (nfa_calc_size == TRUE) |
| 3561 | { |
| 3562 | nstate += 1; |
| 3563 | break; |
| 3564 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3565 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3566 | if (s == NULL) |
| 3567 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3568 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3569 | PUSH(frag(s, list1(&s->out))); |
| 3570 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3571 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3572 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3573 | case NFA_ZSTART: |
| 3574 | case NFA_ZEND: |
| 3575 | default: |
| 3576 | /* Operands */ |
| 3577 | if (nfa_calc_size == TRUE) |
| 3578 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3579 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3580 | break; |
| 3581 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3582 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3583 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3584 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3585 | PUSH(frag(s, list1(&s->out))); |
| 3586 | break; |
| 3587 | |
| 3588 | } /* switch(*p) */ |
| 3589 | |
| 3590 | } /* for(p = postfix; *p; ++p) */ |
| 3591 | |
| 3592 | if (nfa_calc_size == TRUE) |
| 3593 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3594 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3595 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3596 | } |
| 3597 | |
| 3598 | e = POP(); |
| 3599 | if (stackp != stack) |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3600 | { |
| 3601 | vim_free(stack); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3602 | 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] | 3603 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3604 | |
| 3605 | if (istate >= nstate) |
Bram Moolenaar | 50ab994 | 2015-04-13 15:28:12 +0200 | [diff] [blame] | 3606 | { |
| 3607 | vim_free(stack); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3608 | 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] | 3609 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3610 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3611 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3612 | matchstate->c = NFA_MATCH; |
| 3613 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3614 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3615 | |
| 3616 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3617 | ret = e.start; |
| 3618 | |
| 3619 | theend: |
| 3620 | vim_free(stack); |
| 3621 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3622 | |
| 3623 | #undef POP1 |
| 3624 | #undef PUSH1 |
| 3625 | #undef POP2 |
| 3626 | #undef PUSH2 |
| 3627 | #undef POP |
| 3628 | #undef PUSH |
| 3629 | } |
| 3630 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3631 | /* |
| 3632 | * After building the NFA program, inspect it to add optimization hints. |
| 3633 | */ |
| 3634 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3635 | nfa_postprocess(nfa_regprog_T *prog) |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3636 | { |
| 3637 | int i; |
| 3638 | int c; |
| 3639 | |
| 3640 | for (i = 0; i < prog->nstate; ++i) |
| 3641 | { |
| 3642 | c = prog->state[i].c; |
| 3643 | if (c == NFA_START_INVISIBLE |
| 3644 | || c == NFA_START_INVISIBLE_NEG |
| 3645 | || c == NFA_START_INVISIBLE_BEFORE |
| 3646 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3647 | { |
| 3648 | int directly; |
| 3649 | |
| 3650 | /* Do it directly when what follows is possibly the end of the |
| 3651 | * match. */ |
| 3652 | if (match_follows(prog->state[i].out1->out, 0)) |
| 3653 | directly = TRUE; |
| 3654 | else |
| 3655 | { |
| 3656 | int ch_invisible = failure_chance(prog->state[i].out, 0); |
| 3657 | int ch_follows = failure_chance(prog->state[i].out1->out, 0); |
| 3658 | |
| 3659 | /* Postpone when the invisible match is expensive or has a |
| 3660 | * lower chance of failing. */ |
| 3661 | if (c == NFA_START_INVISIBLE_BEFORE |
| 3662 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3663 | { |
| 3664 | /* "before" matches are very expensive when |
| 3665 | * unbounded, always prefer what follows then, |
| 3666 | * unless what follows will always match. |
| 3667 | * Otherwise strongly prefer what follows. */ |
| 3668 | if (prog->state[i].val <= 0 && ch_follows > 0) |
| 3669 | directly = FALSE; |
| 3670 | else |
| 3671 | directly = ch_follows * 10 < ch_invisible; |
| 3672 | } |
| 3673 | else |
| 3674 | { |
| 3675 | /* normal invisible, first do the one with the |
| 3676 | * highest failure chance */ |
| 3677 | directly = ch_follows < ch_invisible; |
| 3678 | } |
| 3679 | } |
| 3680 | if (directly) |
| 3681 | /* switch to the _FIRST state */ |
| 3682 | ++prog->state[i].c; |
| 3683 | } |
| 3684 | } |
| 3685 | } |
| 3686 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3687 | /**************************************************************** |
| 3688 | * NFA execution code. |
| 3689 | ****************************************************************/ |
| 3690 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3691 | typedef struct |
| 3692 | { |
| 3693 | int in_use; /* number of subexpr with useful info */ |
| 3694 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3695 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3696 | union |
| 3697 | { |
| 3698 | struct multipos |
| 3699 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3700 | linenr_T start_lnum; |
| 3701 | linenr_T end_lnum; |
| 3702 | colnr_T start_col; |
| 3703 | colnr_T end_col; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3704 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3705 | struct linepos |
| 3706 | { |
| 3707 | char_u *start; |
| 3708 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3709 | } line[NSUBEXP]; |
| 3710 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3711 | } regsub_T; |
| 3712 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3713 | typedef struct |
| 3714 | { |
| 3715 | regsub_T norm; /* \( .. \) matches */ |
| 3716 | #ifdef FEAT_SYN_HL |
| 3717 | regsub_T synt; /* \z( .. \) matches */ |
| 3718 | #endif |
| 3719 | } regsubs_T; |
| 3720 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3721 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3722 | typedef struct nfa_pim_S nfa_pim_T; |
| 3723 | struct nfa_pim_S |
| 3724 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3725 | int result; /* NFA_PIM_*, see below */ |
| 3726 | nfa_state_T *state; /* the invisible match start state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3727 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3728 | union |
| 3729 | { |
| 3730 | lpos_T pos; |
| 3731 | char_u *ptr; |
| 3732 | } end; /* where the match must end */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3733 | }; |
| 3734 | |
| 3735 | /* Values for done in nfa_pim_T. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3736 | #define NFA_PIM_UNUSED 0 /* pim not used */ |
| 3737 | #define NFA_PIM_TODO 1 /* pim not done yet */ |
| 3738 | #define NFA_PIM_MATCH 2 /* pim executed, matches */ |
| 3739 | #define NFA_PIM_NOMATCH 3 /* pim executed, no match */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3740 | |
| 3741 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3742 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3743 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3744 | { |
| 3745 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3746 | int count; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3747 | nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed |
| 3748 | * invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3749 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3750 | } nfa_thread_T; |
| 3751 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3752 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3753 | typedef struct |
| 3754 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3755 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3756 | int n; /* nr of states currently in "t" */ |
| 3757 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3758 | int id; /* ID of the list */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 3759 | int has_pim; /* TRUE when any state has a PIM */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3760 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3761 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3762 | #ifdef ENABLE_LOG |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 3763 | static void log_subsexpr(regsubs_T *subs); |
| 3764 | static void log_subexpr(regsub_T *sub); |
| 3765 | static char *pim_info(nfa_pim_T *pim); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3766 | |
| 3767 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3768 | log_subsexpr(regsubs_T *subs) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3769 | { |
| 3770 | log_subexpr(&subs->norm); |
| 3771 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3772 | if (nfa_has_zsubexpr) |
| 3773 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3774 | # endif |
| 3775 | } |
| 3776 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3777 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3778 | log_subexpr(regsub_T *sub) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3779 | { |
| 3780 | int j; |
| 3781 | |
| 3782 | for (j = 0; j < sub->in_use; j++) |
| 3783 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3784 | 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] | 3785 | j, |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3786 | sub->list.multi[j].start_col, |
| 3787 | (int)sub->list.multi[j].start_lnum, |
| 3788 | sub->list.multi[j].end_col, |
| 3789 | (int)sub->list.multi[j].end_lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3790 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3791 | { |
| 3792 | char *s = (char *)sub->list.line[j].start; |
| 3793 | char *e = (char *)sub->list.line[j].end; |
| 3794 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3795 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3796 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3797 | s == NULL ? "NULL" : s, |
| 3798 | e == NULL ? "NULL" : e); |
| 3799 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3800 | } |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3801 | |
| 3802 | static char * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3803 | pim_info(nfa_pim_T *pim) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3804 | { |
| 3805 | static char buf[30]; |
| 3806 | |
| 3807 | if (pim == NULL || pim->result == NFA_PIM_UNUSED) |
| 3808 | buf[0] = NUL; |
| 3809 | else |
| 3810 | { |
| 3811 | sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col |
| 3812 | : (int)(pim->end.ptr - reginput)); |
| 3813 | } |
| 3814 | return buf; |
| 3815 | } |
| 3816 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3817 | #endif |
| 3818 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3819 | /* Used during execution: whether a match has been found. */ |
| 3820 | static int nfa_match; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 3821 | #ifdef FEAT_RELTIME |
| 3822 | static proftime_T *nfa_time_limit; |
| 3823 | static int nfa_time_count; |
| 3824 | #endif |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3825 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 3826 | static void copy_pim(nfa_pim_T *to, nfa_pim_T *from); |
| 3827 | static void clear_sub(regsub_T *sub); |
| 3828 | static void copy_sub(regsub_T *to, regsub_T *from); |
| 3829 | static void copy_sub_off(regsub_T *to, regsub_T *from); |
| 3830 | static void copy_ze_off(regsub_T *to, regsub_T *from); |
| 3831 | static int sub_equal(regsub_T *sub1, regsub_T *sub2); |
| 3832 | static int match_backref(regsub_T *sub, int subidx, int *bytelen); |
| 3833 | static int has_state_with_pos(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim); |
| 3834 | static int pim_equal(nfa_pim_T *one, nfa_pim_T *two); |
| 3835 | static int state_in_list(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs); |
| 3836 | static regsubs_T *addstate(nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off); |
| 3837 | static void addstate_here(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] | 3838 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3839 | /* |
| 3840 | * Copy postponed invisible match info from "from" to "to". |
| 3841 | */ |
| 3842 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3843 | copy_pim(nfa_pim_T *to, nfa_pim_T *from) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3844 | { |
| 3845 | to->result = from->result; |
| 3846 | to->state = from->state; |
| 3847 | copy_sub(&to->subs.norm, &from->subs.norm); |
| 3848 | #ifdef FEAT_SYN_HL |
| 3849 | if (nfa_has_zsubexpr) |
| 3850 | copy_sub(&to->subs.synt, &from->subs.synt); |
| 3851 | #endif |
| 3852 | to->end = from->end; |
| 3853 | } |
| 3854 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3855 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3856 | clear_sub(regsub_T *sub) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3857 | { |
| 3858 | if (REG_MULTI) |
| 3859 | /* Use 0xff to set lnum to -1 */ |
| 3860 | vim_memset(sub->list.multi, 0xff, |
| 3861 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3862 | else |
| 3863 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3864 | sub->in_use = 0; |
| 3865 | } |
| 3866 | |
| 3867 | /* |
| 3868 | * Copy the submatches from "from" to "to". |
| 3869 | */ |
| 3870 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3871 | copy_sub(regsub_T *to, regsub_T *from) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3872 | { |
| 3873 | to->in_use = from->in_use; |
| 3874 | if (from->in_use > 0) |
| 3875 | { |
| 3876 | /* Copy the match start and end positions. */ |
| 3877 | if (REG_MULTI) |
| 3878 | mch_memmove(&to->list.multi[0], |
| 3879 | &from->list.multi[0], |
| 3880 | sizeof(struct multipos) * from->in_use); |
| 3881 | else |
| 3882 | mch_memmove(&to->list.line[0], |
| 3883 | &from->list.line[0], |
| 3884 | sizeof(struct linepos) * from->in_use); |
| 3885 | } |
| 3886 | } |
| 3887 | |
| 3888 | /* |
| 3889 | * Like copy_sub() but exclude the main match. |
| 3890 | */ |
| 3891 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3892 | copy_sub_off(regsub_T *to, regsub_T *from) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3893 | { |
| 3894 | if (to->in_use < from->in_use) |
| 3895 | to->in_use = from->in_use; |
| 3896 | if (from->in_use > 1) |
| 3897 | { |
| 3898 | /* Copy the match start and end positions. */ |
| 3899 | if (REG_MULTI) |
| 3900 | mch_memmove(&to->list.multi[1], |
| 3901 | &from->list.multi[1], |
| 3902 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3903 | else |
| 3904 | mch_memmove(&to->list.line[1], |
| 3905 | &from->list.line[1], |
| 3906 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3907 | } |
| 3908 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3909 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3910 | /* |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3911 | * Like copy_sub() but only do the end of the main match if \ze is present. |
| 3912 | */ |
| 3913 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3914 | copy_ze_off(regsub_T *to, regsub_T *from) |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3915 | { |
| 3916 | if (nfa_has_zend) |
| 3917 | { |
| 3918 | if (REG_MULTI) |
| 3919 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3920 | if (from->list.multi[0].end_lnum >= 0) |
| 3921 | { |
| 3922 | to->list.multi[0].end_lnum = from->list.multi[0].end_lnum; |
| 3923 | to->list.multi[0].end_col = from->list.multi[0].end_col; |
| 3924 | } |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3925 | } |
| 3926 | else |
| 3927 | { |
| 3928 | if (from->list.line[0].end != NULL) |
| 3929 | to->list.line[0].end = from->list.line[0].end; |
| 3930 | } |
| 3931 | } |
| 3932 | } |
| 3933 | |
| 3934 | /* |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3935 | * Return TRUE if "sub1" and "sub2" have the same start positions. |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 3936 | * When using back-references also check the end position. |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3937 | */ |
| 3938 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 3939 | sub_equal(regsub_T *sub1, regsub_T *sub2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3940 | { |
| 3941 | int i; |
| 3942 | int todo; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3943 | linenr_T s1; |
| 3944 | linenr_T s2; |
| 3945 | char_u *sp1; |
| 3946 | char_u *sp2; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3947 | |
| 3948 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3949 | if (REG_MULTI) |
| 3950 | { |
| 3951 | for (i = 0; i < todo; ++i) |
| 3952 | { |
| 3953 | if (i < sub1->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3954 | s1 = sub1->list.multi[i].start_lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3955 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3956 | s1 = -1; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3957 | if (i < sub2->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3958 | s2 = sub2->list.multi[i].start_lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3959 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3960 | s2 = -1; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3961 | if (s1 != s2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3962 | return FALSE; |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3963 | if (s1 != -1 && sub1->list.multi[i].start_col |
| 3964 | != sub2->list.multi[i].start_col) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3965 | return FALSE; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 3966 | |
| 3967 | if (nfa_has_backref) |
| 3968 | { |
| 3969 | if (i < sub1->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3970 | s1 = sub1->list.multi[i].end_lnum; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 3971 | else |
| 3972 | s1 = -1; |
| 3973 | if (i < sub2->in_use) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3974 | s2 = sub2->list.multi[i].end_lnum; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 3975 | else |
| 3976 | s2 = -1; |
| 3977 | if (s1 != s2) |
| 3978 | return FALSE; |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 3979 | if (s1 != -1 && sub1->list.multi[i].end_col |
| 3980 | != sub2->list.multi[i].end_col) |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 3981 | return FALSE; |
| 3982 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3983 | } |
| 3984 | } |
| 3985 | else |
| 3986 | { |
| 3987 | for (i = 0; i < todo; ++i) |
| 3988 | { |
| 3989 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3990 | sp1 = sub1->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3991 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3992 | sp1 = NULL; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3993 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3994 | sp2 = sub2->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3995 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3996 | sp2 = NULL; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3997 | if (sp1 != sp2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3998 | return FALSE; |
Bram Moolenaar | ee48253 | 2014-05-13 15:56:51 +0200 | [diff] [blame] | 3999 | if (nfa_has_backref) |
| 4000 | { |
| 4001 | if (i < sub1->in_use) |
| 4002 | sp1 = sub1->list.line[i].end; |
| 4003 | else |
| 4004 | sp1 = NULL; |
| 4005 | if (i < sub2->in_use) |
| 4006 | sp2 = sub2->list.line[i].end; |
| 4007 | else |
| 4008 | sp2 = NULL; |
| 4009 | if (sp1 != sp2) |
| 4010 | return FALSE; |
| 4011 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | return TRUE; |
| 4016 | } |
| 4017 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4018 | #ifdef ENABLE_LOG |
| 4019 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4020 | report_state(char *action, |
| 4021 | regsub_T *sub, |
| 4022 | nfa_state_T *state, |
| 4023 | int lid, |
| 4024 | nfa_pim_T *pim) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4025 | { |
| 4026 | int col; |
| 4027 | |
| 4028 | if (sub->in_use <= 0) |
| 4029 | col = -1; |
| 4030 | else if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4031 | col = sub->list.multi[0].start_col; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4032 | else |
| 4033 | col = (int)(sub->list.line[0].start - regline); |
| 4034 | nfa_set_code(state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4035 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n", |
| 4036 | action, abs(state->id), lid, state->c, code, col, |
| 4037 | pim_info(pim)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4038 | } |
| 4039 | #endif |
| 4040 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4041 | /* |
| 4042 | * Return TRUE if the same state is already in list "l" with the same |
| 4043 | * positions as "subs". |
| 4044 | */ |
| 4045 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4046 | has_state_with_pos( |
| 4047 | nfa_list_T *l, /* runtime state list */ |
| 4048 | nfa_state_T *state, /* state to update */ |
| 4049 | regsubs_T *subs, /* pointers to subexpressions */ |
| 4050 | nfa_pim_T *pim) /* postponed match or NULL */ |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4051 | { |
| 4052 | nfa_thread_T *thread; |
| 4053 | int i; |
| 4054 | |
| 4055 | for (i = 0; i < l->n; ++i) |
| 4056 | { |
| 4057 | thread = &l->t[i]; |
| 4058 | if (thread->state->id == state->id |
| 4059 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 4060 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 4061 | && (!nfa_has_zsubexpr |
| 4062 | || sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4063 | #endif |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4064 | && pim_equal(&thread->pim, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4065 | return TRUE; |
| 4066 | } |
| 4067 | return FALSE; |
| 4068 | } |
| 4069 | |
| 4070 | /* |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4071 | * Return TRUE if "one" and "two" are equal. That includes when both are not |
| 4072 | * set. |
| 4073 | */ |
| 4074 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4075 | pim_equal(nfa_pim_T *one, nfa_pim_T *two) |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4076 | { |
| 4077 | int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED); |
| 4078 | int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED); |
| 4079 | |
| 4080 | if (one_unused) |
| 4081 | /* one is unused: equal when two is also unused */ |
| 4082 | return two_unused; |
| 4083 | if (two_unused) |
| 4084 | /* one is used and two is not: not equal */ |
| 4085 | return FALSE; |
Bram Moolenaar | 3f0df06 | 2013-08-14 13:34:25 +0200 | [diff] [blame] | 4086 | /* compare the state id */ |
| 4087 | if (one->state->id != two->state->id) |
| 4088 | return FALSE; |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4089 | /* compare the position */ |
| 4090 | if (REG_MULTI) |
| 4091 | return one->end.pos.lnum == two->end.pos.lnum |
| 4092 | && one->end.pos.col == two->end.pos.col; |
| 4093 | return one->end.ptr == two->end.ptr; |
| 4094 | } |
| 4095 | |
| 4096 | /* |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4097 | * Return TRUE if "state" leads to a NFA_MATCH without advancing the input. |
| 4098 | */ |
| 4099 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4100 | match_follows(nfa_state_T *startstate, int depth) |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4101 | { |
| 4102 | nfa_state_T *state = startstate; |
| 4103 | |
| 4104 | /* avoid too much recursion */ |
| 4105 | if (depth > 10) |
| 4106 | return FALSE; |
| 4107 | |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4108 | while (state != NULL) |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4109 | { |
| 4110 | switch (state->c) |
| 4111 | { |
| 4112 | case NFA_MATCH: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4113 | case NFA_MCLOSE: |
| 4114 | case NFA_END_INVISIBLE: |
| 4115 | case NFA_END_INVISIBLE_NEG: |
| 4116 | case NFA_END_PATTERN: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4117 | return TRUE; |
| 4118 | |
| 4119 | case NFA_SPLIT: |
| 4120 | return match_follows(state->out, depth + 1) |
| 4121 | || match_follows(state->out1, depth + 1); |
| 4122 | |
| 4123 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4124 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4125 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4126 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4127 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4128 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4129 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4130 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4131 | case NFA_COMPOSING: |
| 4132 | /* skip ahead to next state */ |
| 4133 | state = state->out1->out; |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4134 | continue; |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4135 | |
| 4136 | case NFA_ANY: |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 4137 | case NFA_ANY_COMPOSING: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4138 | case NFA_IDENT: |
| 4139 | case NFA_SIDENT: |
| 4140 | case NFA_KWORD: |
| 4141 | case NFA_SKWORD: |
| 4142 | case NFA_FNAME: |
| 4143 | case NFA_SFNAME: |
| 4144 | case NFA_PRINT: |
| 4145 | case NFA_SPRINT: |
| 4146 | case NFA_WHITE: |
| 4147 | case NFA_NWHITE: |
| 4148 | case NFA_DIGIT: |
| 4149 | case NFA_NDIGIT: |
| 4150 | case NFA_HEX: |
| 4151 | case NFA_NHEX: |
| 4152 | case NFA_OCTAL: |
| 4153 | case NFA_NOCTAL: |
| 4154 | case NFA_WORD: |
| 4155 | case NFA_NWORD: |
| 4156 | case NFA_HEAD: |
| 4157 | case NFA_NHEAD: |
| 4158 | case NFA_ALPHA: |
| 4159 | case NFA_NALPHA: |
| 4160 | case NFA_LOWER: |
| 4161 | case NFA_NLOWER: |
| 4162 | case NFA_UPPER: |
| 4163 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 4164 | case NFA_LOWER_IC: |
| 4165 | case NFA_NLOWER_IC: |
| 4166 | case NFA_UPPER_IC: |
| 4167 | case NFA_NUPPER_IC: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4168 | case NFA_START_COLL: |
| 4169 | case NFA_START_NEG_COLL: |
| 4170 | case NFA_NEWL: |
| 4171 | /* state will advance input */ |
| 4172 | return FALSE; |
| 4173 | |
| 4174 | default: |
| 4175 | if (state->c > 0) |
| 4176 | /* state will advance input */ |
| 4177 | return FALSE; |
| 4178 | |
| 4179 | /* Others: zero-width or possibly zero-width, might still find |
| 4180 | * a match at the same position, keep looking. */ |
| 4181 | break; |
| 4182 | } |
| 4183 | state = state->out; |
| 4184 | } |
| 4185 | return FALSE; |
| 4186 | } |
| 4187 | |
| 4188 | |
| 4189 | /* |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4190 | * Return TRUE if "state" is already in list "l". |
| 4191 | */ |
| 4192 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4193 | state_in_list( |
| 4194 | nfa_list_T *l, /* runtime state list */ |
| 4195 | nfa_state_T *state, /* state to update */ |
| 4196 | regsubs_T *subs) /* pointers to subexpressions */ |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4197 | { |
| 4198 | if (state->lastlist[nfa_ll_index] == l->id) |
| 4199 | { |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4200 | if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4201 | return TRUE; |
| 4202 | } |
| 4203 | return FALSE; |
| 4204 | } |
| 4205 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4206 | /* |
| 4207 | * Add "state" and possibly what follows to state list ".". |
| 4208 | * Returns "subs_arg", possibly copied into temp_subs. |
| 4209 | */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4210 | static regsubs_T * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4211 | addstate( |
| 4212 | nfa_list_T *l, /* runtime state list */ |
| 4213 | nfa_state_T *state, /* state to update */ |
| 4214 | regsubs_T *subs_arg, /* pointers to subexpressions */ |
| 4215 | nfa_pim_T *pim, /* postponed look-behind match */ |
| 4216 | int off) /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4217 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4218 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4219 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4220 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4221 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4222 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4223 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4224 | regsub_T *sub; |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4225 | regsubs_T *subs = subs_arg; |
| 4226 | static regsubs_T temp_subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4227 | #ifdef ENABLE_LOG |
| 4228 | int did_print = FALSE; |
| 4229 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4230 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4231 | switch (state->c) |
| 4232 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4233 | case NFA_NCLOSE: |
| 4234 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4235 | case NFA_MCLOSE1: |
| 4236 | case NFA_MCLOSE2: |
| 4237 | case NFA_MCLOSE3: |
| 4238 | case NFA_MCLOSE4: |
| 4239 | case NFA_MCLOSE5: |
| 4240 | case NFA_MCLOSE6: |
| 4241 | case NFA_MCLOSE7: |
| 4242 | case NFA_MCLOSE8: |
| 4243 | case NFA_MCLOSE9: |
| 4244 | #ifdef FEAT_SYN_HL |
| 4245 | case NFA_ZCLOSE: |
| 4246 | case NFA_ZCLOSE1: |
| 4247 | case NFA_ZCLOSE2: |
| 4248 | case NFA_ZCLOSE3: |
| 4249 | case NFA_ZCLOSE4: |
| 4250 | case NFA_ZCLOSE5: |
| 4251 | case NFA_ZCLOSE6: |
| 4252 | case NFA_ZCLOSE7: |
| 4253 | case NFA_ZCLOSE8: |
| 4254 | case NFA_ZCLOSE9: |
| 4255 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4256 | case NFA_MOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4257 | case NFA_ZEND: |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4258 | case NFA_SPLIT: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4259 | case NFA_EMPTY: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4260 | /* These nodes are not added themselves but their "out" and/or |
| 4261 | * "out1" may be added below. */ |
| 4262 | break; |
| 4263 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4264 | case NFA_BOL: |
| 4265 | case NFA_BOF: |
| 4266 | /* "^" won't match past end-of-line, don't bother trying. |
| 4267 | * Except when at the end of the line, or when we are going to the |
| 4268 | * next line for a look-behind match. */ |
| 4269 | if (reginput > regline |
| 4270 | && *reginput != NUL |
| 4271 | && (nfa_endp == NULL |
| 4272 | || !REG_MULTI |
| 4273 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 4274 | goto skip_add; |
| 4275 | /* FALLTHROUGH */ |
| 4276 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4277 | case NFA_MOPEN1: |
| 4278 | case NFA_MOPEN2: |
| 4279 | case NFA_MOPEN3: |
| 4280 | case NFA_MOPEN4: |
| 4281 | case NFA_MOPEN5: |
| 4282 | case NFA_MOPEN6: |
| 4283 | case NFA_MOPEN7: |
| 4284 | case NFA_MOPEN8: |
| 4285 | case NFA_MOPEN9: |
| 4286 | #ifdef FEAT_SYN_HL |
| 4287 | case NFA_ZOPEN: |
| 4288 | case NFA_ZOPEN1: |
| 4289 | case NFA_ZOPEN2: |
| 4290 | case NFA_ZOPEN3: |
| 4291 | case NFA_ZOPEN4: |
| 4292 | case NFA_ZOPEN5: |
| 4293 | case NFA_ZOPEN6: |
| 4294 | case NFA_ZOPEN7: |
| 4295 | case NFA_ZOPEN8: |
| 4296 | case NFA_ZOPEN9: |
| 4297 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4298 | case NFA_NOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4299 | case NFA_ZSTART: |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4300 | /* These nodes need to be added so that we can bail out when it |
| 4301 | * was added to this list before at the same position to avoid an |
| 4302 | * endless loop for "\(\)*" */ |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4303 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4304 | default: |
Bram Moolenaar | 272fb58 | 2013-11-21 16:03:40 +0100 | [diff] [blame] | 4305 | if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4306 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4307 | /* This state is already in the list, don't add it again, |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4308 | * unless it is an MOPEN that is used for a backreference or |
Bram Moolenaar | 9c23506 | 2014-05-13 16:44:29 +0200 | [diff] [blame] | 4309 | * when there is a PIM. For NFA_MATCH check the position, |
| 4310 | * lower position is preferred. */ |
| 4311 | if (!nfa_has_backref && pim == NULL && !l->has_pim |
| 4312 | && state->c != NFA_MATCH) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4313 | { |
| 4314 | skip_add: |
| 4315 | #ifdef ENABLE_LOG |
| 4316 | nfa_set_code(state->c); |
| 4317 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 4318 | abs(state->id), l->id, state->c, code); |
| 4319 | #endif |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4320 | return subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4321 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4322 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4323 | /* Do not add the state again when it exists with the same |
| 4324 | * positions. */ |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4325 | if (has_state_with_pos(l, state, subs, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4326 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4327 | } |
| 4328 | |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4329 | /* When there are backreferences or PIMs the number of states may |
| 4330 | * be (a lot) bigger than anticipated. */ |
| 4331 | if (l->n == l->len) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4332 | { |
| 4333 | int newlen = l->len * 3 / 2 + 50; |
| 4334 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4335 | if (subs != &temp_subs) |
| 4336 | { |
| 4337 | /* "subs" may point into the current array, need to make a |
| 4338 | * copy before it becomes invalid. */ |
| 4339 | copy_sub(&temp_subs.norm, &subs->norm); |
| 4340 | #ifdef FEAT_SYN_HL |
| 4341 | if (nfa_has_zsubexpr) |
| 4342 | copy_sub(&temp_subs.synt, &subs->synt); |
| 4343 | #endif |
| 4344 | subs = &temp_subs; |
| 4345 | } |
| 4346 | |
Bram Moolenaar | a1d2c58 | 2015-02-10 18:18:17 +0100 | [diff] [blame] | 4347 | /* TODO: check for vim_realloc() returning NULL. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4348 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 4349 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4350 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4351 | |
| 4352 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4353 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4354 | thread = &l->t[l->n++]; |
| 4355 | thread->state = state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4356 | if (pim == NULL) |
| 4357 | thread->pim.result = NFA_PIM_UNUSED; |
| 4358 | else |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4359 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4360 | copy_pim(&thread->pim, pim); |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4361 | l->has_pim = TRUE; |
| 4362 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4363 | copy_sub(&thread->subs.norm, &subs->norm); |
| 4364 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4365 | if (nfa_has_zsubexpr) |
| 4366 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4367 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4368 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4369 | report_state("Adding", &thread->subs.norm, state, l->id, pim); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4370 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4371 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4372 | } |
| 4373 | |
| 4374 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4375 | if (!did_print) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4376 | report_state("Processing", &subs->norm, state, l->id, pim); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4377 | #endif |
| 4378 | switch (state->c) |
| 4379 | { |
| 4380 | case NFA_MATCH: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4381 | break; |
| 4382 | |
| 4383 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4384 | /* order matters here */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4385 | subs = addstate(l, state->out, subs, pim, off); |
| 4386 | subs = addstate(l, state->out1, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4387 | break; |
| 4388 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4389 | case NFA_EMPTY: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4390 | case NFA_NOPEN: |
| 4391 | case NFA_NCLOSE: |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4392 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4393 | break; |
| 4394 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4395 | case NFA_MOPEN: |
| 4396 | case NFA_MOPEN1: |
| 4397 | case NFA_MOPEN2: |
| 4398 | case NFA_MOPEN3: |
| 4399 | case NFA_MOPEN4: |
| 4400 | case NFA_MOPEN5: |
| 4401 | case NFA_MOPEN6: |
| 4402 | case NFA_MOPEN7: |
| 4403 | case NFA_MOPEN8: |
| 4404 | case NFA_MOPEN9: |
| 4405 | #ifdef FEAT_SYN_HL |
| 4406 | case NFA_ZOPEN: |
| 4407 | case NFA_ZOPEN1: |
| 4408 | case NFA_ZOPEN2: |
| 4409 | case NFA_ZOPEN3: |
| 4410 | case NFA_ZOPEN4: |
| 4411 | case NFA_ZOPEN5: |
| 4412 | case NFA_ZOPEN6: |
| 4413 | case NFA_ZOPEN7: |
| 4414 | case NFA_ZOPEN8: |
| 4415 | case NFA_ZOPEN9: |
| 4416 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4417 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4418 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4419 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4420 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4421 | sub = &subs->norm; |
| 4422 | } |
| 4423 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4424 | else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4425 | { |
| 4426 | subidx = state->c - NFA_ZOPEN; |
| 4427 | sub = &subs->synt; |
| 4428 | } |
| 4429 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4430 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4431 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4432 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4433 | sub = &subs->norm; |
| 4434 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4435 | |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4436 | /* avoid compiler warnings */ |
| 4437 | save_ptr = NULL; |
| 4438 | save_lpos.lnum = 0; |
| 4439 | save_lpos.col = 0; |
| 4440 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4441 | /* Set the position (with "off" added) in the subexpression. Save |
| 4442 | * and restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4443 | if (REG_MULTI) |
| 4444 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4445 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4446 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4447 | save_lpos.lnum = sub->list.multi[subidx].start_lnum; |
| 4448 | save_lpos.col = sub->list.multi[subidx].start_col; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4449 | save_in_use = -1; |
| 4450 | } |
| 4451 | else |
| 4452 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4453 | save_in_use = sub->in_use; |
| 4454 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4455 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4456 | sub->list.multi[i].start_lnum = -1; |
| 4457 | sub->list.multi[i].end_lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4458 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4459 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4460 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4461 | if (off == -1) |
| 4462 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4463 | sub->list.multi[subidx].start_lnum = reglnum + 1; |
| 4464 | sub->list.multi[subidx].start_col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4465 | } |
| 4466 | else |
| 4467 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4468 | sub->list.multi[subidx].start_lnum = reglnum; |
| 4469 | sub->list.multi[subidx].start_col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4470 | (colnr_T)(reginput - regline + off); |
| 4471 | } |
Bram Moolenaar | c2b717e | 2015-09-29 15:06:14 +0200 | [diff] [blame] | 4472 | sub->list.multi[subidx].end_lnum = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4473 | } |
| 4474 | else |
| 4475 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4476 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4477 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4478 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4479 | save_in_use = -1; |
| 4480 | } |
| 4481 | else |
| 4482 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4483 | save_in_use = sub->in_use; |
| 4484 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4485 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4486 | sub->list.line[i].start = NULL; |
| 4487 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4488 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4489 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4490 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4491 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4492 | } |
| 4493 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4494 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4495 | /* "subs" may have changed, need to set "sub" again */ |
| 4496 | #ifdef FEAT_SYN_HL |
| 4497 | if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
| 4498 | sub = &subs->synt; |
| 4499 | else |
| 4500 | #endif |
| 4501 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4502 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4503 | if (save_in_use == -1) |
| 4504 | { |
| 4505 | if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4506 | { |
| 4507 | sub->list.multi[subidx].start_lnum = save_lpos.lnum; |
| 4508 | sub->list.multi[subidx].start_col = save_lpos.col; |
| 4509 | } |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4510 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4511 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4512 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4513 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4514 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4515 | break; |
| 4516 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4517 | case NFA_MCLOSE: |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4518 | if (nfa_has_zend && (REG_MULTI |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4519 | ? subs->norm.list.multi[0].end_lnum >= 0 |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4520 | : subs->norm.list.line[0].end != NULL)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4521 | { |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4522 | /* Do not overwrite the position set by \ze. */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4523 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4524 | break; |
| 4525 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4526 | case NFA_MCLOSE1: |
| 4527 | case NFA_MCLOSE2: |
| 4528 | case NFA_MCLOSE3: |
| 4529 | case NFA_MCLOSE4: |
| 4530 | case NFA_MCLOSE5: |
| 4531 | case NFA_MCLOSE6: |
| 4532 | case NFA_MCLOSE7: |
| 4533 | case NFA_MCLOSE8: |
| 4534 | case NFA_MCLOSE9: |
| 4535 | #ifdef FEAT_SYN_HL |
| 4536 | case NFA_ZCLOSE: |
| 4537 | case NFA_ZCLOSE1: |
| 4538 | case NFA_ZCLOSE2: |
| 4539 | case NFA_ZCLOSE3: |
| 4540 | case NFA_ZCLOSE4: |
| 4541 | case NFA_ZCLOSE5: |
| 4542 | case NFA_ZCLOSE6: |
| 4543 | case NFA_ZCLOSE7: |
| 4544 | case NFA_ZCLOSE8: |
| 4545 | case NFA_ZCLOSE9: |
| 4546 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4547 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4548 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4549 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4550 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4551 | sub = &subs->norm; |
| 4552 | } |
| 4553 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4554 | else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4555 | { |
| 4556 | subidx = state->c - NFA_ZCLOSE; |
| 4557 | sub = &subs->synt; |
| 4558 | } |
| 4559 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4560 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4561 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4562 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4563 | sub = &subs->norm; |
| 4564 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4565 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4566 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 4567 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4568 | save_in_use = sub->in_use; |
| 4569 | if (sub->in_use <= subidx) |
| 4570 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4571 | if (REG_MULTI) |
| 4572 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4573 | save_lpos.lnum = sub->list.multi[subidx].end_lnum; |
| 4574 | save_lpos.col = sub->list.multi[subidx].end_col; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4575 | if (off == -1) |
| 4576 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4577 | sub->list.multi[subidx].end_lnum = reglnum + 1; |
| 4578 | sub->list.multi[subidx].end_col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4579 | } |
| 4580 | else |
| 4581 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4582 | sub->list.multi[subidx].end_lnum = reglnum; |
| 4583 | sub->list.multi[subidx].end_col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4584 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4585 | } |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4586 | /* avoid compiler warnings */ |
| 4587 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4588 | } |
| 4589 | else |
| 4590 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4591 | save_ptr = sub->list.line[subidx].end; |
| 4592 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4593 | /* avoid compiler warnings */ |
| 4594 | save_lpos.lnum = 0; |
| 4595 | save_lpos.col = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4596 | } |
| 4597 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4598 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4599 | /* "subs" may have changed, need to set "sub" again */ |
| 4600 | #ifdef FEAT_SYN_HL |
| 4601 | if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
| 4602 | sub = &subs->synt; |
| 4603 | else |
| 4604 | #endif |
| 4605 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4606 | |
| 4607 | if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4608 | { |
| 4609 | sub->list.multi[subidx].end_lnum = save_lpos.lnum; |
| 4610 | sub->list.multi[subidx].end_col = save_lpos.col; |
| 4611 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4612 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4613 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4614 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4615 | break; |
| 4616 | } |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4617 | return subs; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4621 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 4622 | * Used for zero-width matches, next state to use is the added one. |
| 4623 | * This makes sure the order of states to be tried does not change, which |
| 4624 | * matters for alternatives. |
| 4625 | */ |
| 4626 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4627 | addstate_here( |
| 4628 | nfa_list_T *l, /* runtime state list */ |
| 4629 | nfa_state_T *state, /* state to update */ |
| 4630 | regsubs_T *subs, /* pointers to subexpressions */ |
| 4631 | nfa_pim_T *pim, /* postponed look-behind match */ |
| 4632 | int *ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4633 | { |
| 4634 | int tlen = l->n; |
| 4635 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4636 | int listidx = *ip; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4637 | |
| 4638 | /* 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] | 4639 | addstate(l, state, subs, pim, 0); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4640 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4641 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4642 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4643 | return; |
| 4644 | |
| 4645 | /* re-order to put the new state at the current position */ |
| 4646 | count = l->n - tlen; |
Bram Moolenaar | a50d02d | 2013-06-16 15:43:50 +0200 | [diff] [blame] | 4647 | if (count == 0) |
| 4648 | return; /* no state got added */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4649 | if (count == 1) |
| 4650 | { |
| 4651 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4652 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4653 | } |
| 4654 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4655 | { |
Bram Moolenaar | 55480dc | 2013-06-30 13:17:24 +0200 | [diff] [blame] | 4656 | if (l->n + count - 1 >= l->len) |
| 4657 | { |
| 4658 | /* not enough space to move the new states, reallocate the list |
| 4659 | * and move the states to the right position */ |
| 4660 | nfa_thread_T *newl; |
| 4661 | |
| 4662 | l->len = l->len * 3 / 2 + 50; |
| 4663 | newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T)); |
| 4664 | if (newl == NULL) |
| 4665 | return; |
| 4666 | mch_memmove(&(newl[0]), |
| 4667 | &(l->t[0]), |
| 4668 | sizeof(nfa_thread_T) * listidx); |
| 4669 | mch_memmove(&(newl[listidx]), |
| 4670 | &(l->t[l->n - count]), |
| 4671 | sizeof(nfa_thread_T) * count); |
| 4672 | mch_memmove(&(newl[listidx + count]), |
| 4673 | &(l->t[listidx + 1]), |
| 4674 | sizeof(nfa_thread_T) * (l->n - count - listidx - 1)); |
| 4675 | vim_free(l->t); |
| 4676 | l->t = newl; |
| 4677 | } |
| 4678 | else |
| 4679 | { |
| 4680 | /* make space for new states, then move them from the |
| 4681 | * end to the current position */ |
| 4682 | mch_memmove(&(l->t[listidx + count]), |
| 4683 | &(l->t[listidx + 1]), |
| 4684 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 4685 | mch_memmove(&(l->t[listidx]), |
| 4686 | &(l->t[l->n - 1]), |
| 4687 | sizeof(nfa_thread_T) * count); |
| 4688 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4689 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4690 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4691 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4695 | * Check character class "class" against current character c. |
| 4696 | */ |
| 4697 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4698 | check_char_class(int class, int c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4699 | { |
| 4700 | switch (class) |
| 4701 | { |
| 4702 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4703 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4704 | return OK; |
| 4705 | break; |
| 4706 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4707 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4708 | return OK; |
| 4709 | break; |
| 4710 | case NFA_CLASS_BLANK: |
| 4711 | if (c == ' ' || c == '\t') |
| 4712 | return OK; |
| 4713 | break; |
| 4714 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4715 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4716 | return OK; |
| 4717 | break; |
| 4718 | case NFA_CLASS_DIGIT: |
| 4719 | if (VIM_ISDIGIT(c)) |
| 4720 | return OK; |
| 4721 | break; |
| 4722 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4723 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4724 | return OK; |
| 4725 | break; |
| 4726 | case NFA_CLASS_LOWER: |
| 4727 | if (MB_ISLOWER(c)) |
| 4728 | return OK; |
| 4729 | break; |
| 4730 | case NFA_CLASS_PRINT: |
| 4731 | if (vim_isprintc(c)) |
| 4732 | return OK; |
| 4733 | break; |
| 4734 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4735 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4736 | return OK; |
| 4737 | break; |
| 4738 | case NFA_CLASS_SPACE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4739 | if ((c >= 9 && c <= 13) || (c == ' ')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4740 | return OK; |
| 4741 | break; |
| 4742 | case NFA_CLASS_UPPER: |
| 4743 | if (MB_ISUPPER(c)) |
| 4744 | return OK; |
| 4745 | break; |
| 4746 | case NFA_CLASS_XDIGIT: |
| 4747 | if (vim_isxdigit(c)) |
| 4748 | return OK; |
| 4749 | break; |
| 4750 | case NFA_CLASS_TAB: |
| 4751 | if (c == '\t') |
| 4752 | return OK; |
| 4753 | break; |
| 4754 | case NFA_CLASS_RETURN: |
| 4755 | if (c == '\r') |
| 4756 | return OK; |
| 4757 | break; |
| 4758 | case NFA_CLASS_BACKSPACE: |
| 4759 | if (c == '\b') |
| 4760 | return OK; |
| 4761 | break; |
| 4762 | case NFA_CLASS_ESCAPE: |
| 4763 | if (c == '\033') |
| 4764 | return OK; |
| 4765 | break; |
| 4766 | |
| 4767 | default: |
| 4768 | /* should not be here :P */ |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 4769 | EMSGN(_(e_ill_char_class), class); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 4770 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4771 | } |
| 4772 | return FAIL; |
| 4773 | } |
| 4774 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4775 | /* |
| 4776 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4777 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4778 | */ |
| 4779 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4780 | match_backref( |
| 4781 | regsub_T *sub, /* pointers to subexpressions */ |
| 4782 | int subidx, |
| 4783 | int *bytelen) /* out: length of match in bytes */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4784 | { |
| 4785 | int len; |
| 4786 | |
| 4787 | if (sub->in_use <= subidx) |
| 4788 | { |
| 4789 | retempty: |
| 4790 | /* backref was not set, match an empty string */ |
| 4791 | *bytelen = 0; |
| 4792 | return TRUE; |
| 4793 | } |
| 4794 | |
| 4795 | if (REG_MULTI) |
| 4796 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4797 | if (sub->list.multi[subidx].start_lnum < 0 |
| 4798 | || sub->list.multi[subidx].end_lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4799 | goto retempty; |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4800 | if (sub->list.multi[subidx].start_lnum == reglnum |
| 4801 | && sub->list.multi[subidx].end_lnum == reglnum) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4802 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4803 | len = sub->list.multi[subidx].end_col |
| 4804 | - sub->list.multi[subidx].start_col; |
| 4805 | if (cstrncmp(regline + sub->list.multi[subidx].start_col, |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4806 | reginput, &len) == 0) |
| 4807 | { |
| 4808 | *bytelen = len; |
| 4809 | return TRUE; |
| 4810 | } |
| 4811 | } |
| 4812 | else |
| 4813 | { |
| 4814 | if (match_with_backref( |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 4815 | sub->list.multi[subidx].start_lnum, |
| 4816 | sub->list.multi[subidx].start_col, |
| 4817 | sub->list.multi[subidx].end_lnum, |
| 4818 | sub->list.multi[subidx].end_col, |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4819 | bytelen) == RA_MATCH) |
| 4820 | return TRUE; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4821 | } |
| 4822 | } |
| 4823 | else |
| 4824 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4825 | if (sub->list.line[subidx].start == NULL |
| 4826 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4827 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4828 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 4829 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4830 | { |
| 4831 | *bytelen = len; |
| 4832 | return TRUE; |
| 4833 | } |
| 4834 | } |
| 4835 | return FALSE; |
| 4836 | } |
| 4837 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4838 | #ifdef FEAT_SYN_HL |
| 4839 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 4840 | static int match_zref(int subidx, int *bytelen); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4841 | |
| 4842 | /* |
| 4843 | * Check for a match with \z subexpression "subidx". |
| 4844 | * Return TRUE if it matches. |
| 4845 | */ |
| 4846 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4847 | match_zref( |
| 4848 | int subidx, |
| 4849 | int *bytelen) /* out: length of match in bytes */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4850 | { |
| 4851 | int len; |
| 4852 | |
| 4853 | cleanup_zsubexpr(); |
| 4854 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 4855 | { |
| 4856 | /* backref was not set, match an empty string */ |
| 4857 | *bytelen = 0; |
| 4858 | return TRUE; |
| 4859 | } |
| 4860 | |
| 4861 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 4862 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 4863 | { |
| 4864 | *bytelen = len; |
| 4865 | return TRUE; |
| 4866 | } |
| 4867 | return FALSE; |
| 4868 | } |
| 4869 | #endif |
| 4870 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4871 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4872 | * Save list IDs for all NFA states of "prog" into "list". |
| 4873 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4874 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4875 | */ |
| 4876 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4877 | nfa_save_listids(nfa_regprog_T *prog, int *list) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4878 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4879 | int i; |
| 4880 | nfa_state_T *p; |
| 4881 | |
| 4882 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 4883 | p = &prog->state[0]; |
| 4884 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4885 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4886 | list[i] = p->lastlist[1]; |
| 4887 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4888 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | |
| 4892 | /* |
| 4893 | * Restore list IDs from "list" to all NFA states. |
| 4894 | */ |
| 4895 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4896 | nfa_restore_listids(nfa_regprog_T *prog, int *list) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4897 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4898 | int i; |
| 4899 | nfa_state_T *p; |
| 4900 | |
| 4901 | p = &prog->state[0]; |
| 4902 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4903 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4904 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4905 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4906 | } |
| 4907 | } |
| 4908 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4909 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4910 | nfa_re_num_cmp(long_u val, int op, long_u pos) |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4911 | { |
| 4912 | if (op == 1) return pos > val; |
| 4913 | if (op == 2) return pos < val; |
| 4914 | return val == pos; |
| 4915 | } |
| 4916 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 4917 | static int recursive_regmatch(nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids); |
| 4918 | static int nfa_regmatch(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] | 4919 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4920 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4921 | * Recursively call nfa_regmatch() |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4922 | * "pim" is NULL or contains info about a Postponed Invisible Match (start |
| 4923 | * position). |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4924 | */ |
| 4925 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 4926 | recursive_regmatch( |
| 4927 | nfa_state_T *state, |
| 4928 | nfa_pim_T *pim, |
| 4929 | nfa_regprog_T *prog, |
| 4930 | regsubs_T *submatch, |
| 4931 | regsubs_T *m, |
| 4932 | int **listids) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4933 | { |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4934 | int save_reginput_col = (int)(reginput - regline); |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4935 | int save_reglnum = reglnum; |
| 4936 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4937 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4938 | save_se_T *save_nfa_endp = nfa_endp; |
| 4939 | save_se_T endpos; |
| 4940 | save_se_T *endposp = NULL; |
| 4941 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4942 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4943 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4944 | if (pim != NULL) |
| 4945 | { |
| 4946 | /* start at the position where the postponed match was */ |
| 4947 | if (REG_MULTI) |
| 4948 | reginput = regline + pim->end.pos.col; |
| 4949 | else |
| 4950 | reginput = pim->end.ptr; |
| 4951 | } |
| 4952 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4953 | if (state->c == NFA_START_INVISIBLE_BEFORE |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4954 | || state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 4955 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG |
| 4956 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4957 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4958 | /* The recursive match must end at the current position. When "pim" is |
| 4959 | * not NULL it specifies the current position. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4960 | endposp = &endpos; |
| 4961 | if (REG_MULTI) |
| 4962 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4963 | if (pim == NULL) |
| 4964 | { |
| 4965 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 4966 | endpos.se_u.pos.lnum = reglnum; |
| 4967 | } |
| 4968 | else |
| 4969 | endpos.se_u.pos = pim->end.pos; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4970 | } |
| 4971 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4972 | { |
| 4973 | if (pim == NULL) |
| 4974 | endpos.se_u.ptr = reginput; |
| 4975 | else |
| 4976 | endpos.se_u.ptr = pim->end.ptr; |
| 4977 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4978 | |
| 4979 | /* Go back the specified number of bytes, or as far as the |
| 4980 | * start of the previous line, to try matching "\@<=" or |
Bram Moolenaar | 3fb14bc | 2013-07-14 12:34:56 +0200 | [diff] [blame] | 4981 | * not matching "\@<!". This is very inefficient, limit the number of |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4982 | * bytes if possible. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4983 | if (state->val <= 0) |
| 4984 | { |
| 4985 | if (REG_MULTI) |
| 4986 | { |
| 4987 | regline = reg_getline(--reglnum); |
| 4988 | if (regline == NULL) |
| 4989 | /* can't go before the first line */ |
| 4990 | regline = reg_getline(++reglnum); |
| 4991 | } |
| 4992 | reginput = regline; |
| 4993 | } |
| 4994 | else |
| 4995 | { |
| 4996 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 4997 | { |
| 4998 | /* Not enough bytes in this line, go to end of |
| 4999 | * previous line. */ |
| 5000 | regline = reg_getline(--reglnum); |
| 5001 | if (regline == NULL) |
| 5002 | { |
| 5003 | /* can't go before the first line */ |
| 5004 | regline = reg_getline(++reglnum); |
| 5005 | reginput = regline; |
| 5006 | } |
| 5007 | else |
| 5008 | reginput = regline + STRLEN(regline); |
| 5009 | } |
| 5010 | if ((int)(reginput - regline) >= state->val) |
| 5011 | { |
| 5012 | reginput -= state->val; |
| 5013 | #ifdef FEAT_MBYTE |
| 5014 | if (has_mbyte) |
| 5015 | reginput -= mb_head_off(regline, reginput); |
| 5016 | #endif |
| 5017 | } |
| 5018 | else |
| 5019 | reginput = regline; |
| 5020 | } |
| 5021 | } |
| 5022 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5023 | #ifdef ENABLE_LOG |
| 5024 | if (log_fd != stderr) |
| 5025 | fclose(log_fd); |
| 5026 | log_fd = NULL; |
| 5027 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5028 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 5029 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 5030 | if (nfa_ll_index == 1) |
| 5031 | { |
| 5032 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 5033 | * values and clear them. */ |
| 5034 | if (*listids == NULL) |
| 5035 | { |
| 5036 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 5037 | if (*listids == NULL) |
| 5038 | { |
| 5039 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 5040 | return 0; |
| 5041 | } |
| 5042 | } |
| 5043 | nfa_save_listids(prog, *listids); |
| 5044 | need_restore = TRUE; |
| 5045 | /* any value of nfa_listid will do */ |
| 5046 | } |
| 5047 | else |
| 5048 | { |
| 5049 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 5050 | * entry. Make sure nfa_listid is different from a previous recursive |
| 5051 | * call, because some states may still have this ID. */ |
| 5052 | ++nfa_ll_index; |
| 5053 | if (nfa_listid <= nfa_alt_listid) |
| 5054 | nfa_listid = nfa_alt_listid; |
| 5055 | } |
| 5056 | |
| 5057 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 5058 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5059 | nfa_endp = endposp; |
| 5060 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5061 | |
| 5062 | if (need_restore) |
| 5063 | nfa_restore_listids(prog, *listids); |
| 5064 | else |
| 5065 | { |
| 5066 | --nfa_ll_index; |
| 5067 | nfa_alt_listid = nfa_listid; |
| 5068 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5069 | |
| 5070 | /* restore position in input text */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5071 | reglnum = save_reglnum; |
Bram Moolenaar | 484d241 | 2013-06-13 19:47:07 +0200 | [diff] [blame] | 5072 | if (REG_MULTI) |
| 5073 | regline = reg_getline(reglnum); |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 5074 | reginput = regline + save_reginput_col; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5075 | nfa_match = save_nfa_match; |
| 5076 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5077 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5078 | |
| 5079 | #ifdef ENABLE_LOG |
| 5080 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 5081 | if (log_fd != NULL) |
| 5082 | { |
| 5083 | fprintf(log_fd, "****************************\n"); |
| 5084 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 5085 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5086 | fprintf(log_fd, "****************************\n"); |
| 5087 | } |
| 5088 | else |
| 5089 | { |
| 5090 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5091 | log_fd = stderr; |
| 5092 | } |
| 5093 | #endif |
| 5094 | |
| 5095 | return result; |
| 5096 | } |
| 5097 | |
Bram Moolenaar | baaa7e9 | 2016-01-29 22:47:03 +0100 | [diff] [blame] | 5098 | static int skip_to_start(int c, colnr_T *colp); |
| 5099 | static long find_match_text(colnr_T startcol, int regstart, char_u *match_text); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5100 | |
| 5101 | /* |
| 5102 | * Estimate the chance of a match with "state" failing. |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5103 | * empty match: 0 |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5104 | * NFA_ANY: 1 |
| 5105 | * specific character: 99 |
| 5106 | */ |
| 5107 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5108 | failure_chance(nfa_state_T *state, int depth) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5109 | { |
| 5110 | int c = state->c; |
| 5111 | int l, r; |
| 5112 | |
| 5113 | /* detect looping */ |
| 5114 | if (depth > 4) |
| 5115 | return 1; |
| 5116 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5117 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5118 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5119 | case NFA_SPLIT: |
| 5120 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 5121 | /* avoid recursive stuff */ |
| 5122 | return 1; |
| 5123 | /* two alternatives, use the lowest failure chance */ |
| 5124 | l = failure_chance(state->out, depth + 1); |
| 5125 | r = failure_chance(state->out1, depth + 1); |
| 5126 | return l < r ? l : r; |
| 5127 | |
| 5128 | case NFA_ANY: |
| 5129 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5130 | return 1; |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5131 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5132 | case NFA_MATCH: |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5133 | case NFA_MCLOSE: |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 5134 | case NFA_ANY_COMPOSING: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5135 | /* empty match works always */ |
| 5136 | return 0; |
| 5137 | |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5138 | case NFA_START_INVISIBLE: |
| 5139 | case NFA_START_INVISIBLE_FIRST: |
| 5140 | case NFA_START_INVISIBLE_NEG: |
| 5141 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 5142 | case NFA_START_INVISIBLE_BEFORE: |
| 5143 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 5144 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 5145 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 5146 | case NFA_START_PATTERN: |
| 5147 | /* recursive regmatch is expensive, use low failure chance */ |
| 5148 | return 5; |
| 5149 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5150 | case NFA_BOL: |
| 5151 | case NFA_EOL: |
| 5152 | case NFA_BOF: |
| 5153 | case NFA_EOF: |
| 5154 | case NFA_NEWL: |
| 5155 | return 99; |
| 5156 | |
| 5157 | case NFA_BOW: |
| 5158 | case NFA_EOW: |
| 5159 | return 90; |
| 5160 | |
| 5161 | case NFA_MOPEN: |
| 5162 | case NFA_MOPEN1: |
| 5163 | case NFA_MOPEN2: |
| 5164 | case NFA_MOPEN3: |
| 5165 | case NFA_MOPEN4: |
| 5166 | case NFA_MOPEN5: |
| 5167 | case NFA_MOPEN6: |
| 5168 | case NFA_MOPEN7: |
| 5169 | case NFA_MOPEN8: |
| 5170 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5171 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5172 | case NFA_ZOPEN: |
| 5173 | case NFA_ZOPEN1: |
| 5174 | case NFA_ZOPEN2: |
| 5175 | case NFA_ZOPEN3: |
| 5176 | case NFA_ZOPEN4: |
| 5177 | case NFA_ZOPEN5: |
| 5178 | case NFA_ZOPEN6: |
| 5179 | case NFA_ZOPEN7: |
| 5180 | case NFA_ZOPEN8: |
| 5181 | case NFA_ZOPEN9: |
| 5182 | case NFA_ZCLOSE: |
| 5183 | case NFA_ZCLOSE1: |
| 5184 | case NFA_ZCLOSE2: |
| 5185 | case NFA_ZCLOSE3: |
| 5186 | case NFA_ZCLOSE4: |
| 5187 | case NFA_ZCLOSE5: |
| 5188 | case NFA_ZCLOSE6: |
| 5189 | case NFA_ZCLOSE7: |
| 5190 | case NFA_ZCLOSE8: |
| 5191 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5192 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5193 | case NFA_NOPEN: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5194 | case NFA_MCLOSE1: |
| 5195 | case NFA_MCLOSE2: |
| 5196 | case NFA_MCLOSE3: |
| 5197 | case NFA_MCLOSE4: |
| 5198 | case NFA_MCLOSE5: |
| 5199 | case NFA_MCLOSE6: |
| 5200 | case NFA_MCLOSE7: |
| 5201 | case NFA_MCLOSE8: |
| 5202 | case NFA_MCLOSE9: |
| 5203 | case NFA_NCLOSE: |
| 5204 | return failure_chance(state->out, depth + 1); |
| 5205 | |
| 5206 | case NFA_BACKREF1: |
| 5207 | case NFA_BACKREF2: |
| 5208 | case NFA_BACKREF3: |
| 5209 | case NFA_BACKREF4: |
| 5210 | case NFA_BACKREF5: |
| 5211 | case NFA_BACKREF6: |
| 5212 | case NFA_BACKREF7: |
| 5213 | case NFA_BACKREF8: |
| 5214 | case NFA_BACKREF9: |
| 5215 | #ifdef FEAT_SYN_HL |
| 5216 | case NFA_ZREF1: |
| 5217 | case NFA_ZREF2: |
| 5218 | case NFA_ZREF3: |
| 5219 | case NFA_ZREF4: |
| 5220 | case NFA_ZREF5: |
| 5221 | case NFA_ZREF6: |
| 5222 | case NFA_ZREF7: |
| 5223 | case NFA_ZREF8: |
| 5224 | case NFA_ZREF9: |
| 5225 | #endif |
| 5226 | /* backreferences don't match in many places */ |
| 5227 | return 94; |
| 5228 | |
| 5229 | case NFA_LNUM_GT: |
| 5230 | case NFA_LNUM_LT: |
| 5231 | case NFA_COL_GT: |
| 5232 | case NFA_COL_LT: |
| 5233 | case NFA_VCOL_GT: |
| 5234 | case NFA_VCOL_LT: |
| 5235 | case NFA_MARK_GT: |
| 5236 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5237 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5238 | /* before/after positions don't match very often */ |
| 5239 | return 85; |
| 5240 | |
| 5241 | case NFA_LNUM: |
| 5242 | return 90; |
| 5243 | |
| 5244 | case NFA_CURSOR: |
| 5245 | case NFA_COL: |
| 5246 | case NFA_VCOL: |
| 5247 | case NFA_MARK: |
| 5248 | /* specific positions rarely match */ |
| 5249 | return 98; |
| 5250 | |
| 5251 | case NFA_COMPOSING: |
| 5252 | return 95; |
| 5253 | |
| 5254 | default: |
| 5255 | if (c > 0) |
| 5256 | /* character match fails often */ |
| 5257 | return 95; |
| 5258 | } |
| 5259 | |
| 5260 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5261 | return 50; |
| 5262 | } |
| 5263 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5264 | /* |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5265 | * Skip until the char "c" we know a match must start with. |
| 5266 | */ |
| 5267 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5268 | skip_to_start(int c, colnr_T *colp) |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5269 | { |
| 5270 | char_u *s; |
| 5271 | |
| 5272 | /* Used often, do some work to avoid call overhead. */ |
| 5273 | if (!ireg_ic |
| 5274 | #ifdef FEAT_MBYTE |
| 5275 | && !has_mbyte |
| 5276 | #endif |
| 5277 | ) |
| 5278 | s = vim_strbyte(regline + *colp, c); |
| 5279 | else |
| 5280 | s = cstrchr(regline + *colp, c); |
| 5281 | if (s == NULL) |
| 5282 | return FAIL; |
| 5283 | *colp = (int)(s - regline); |
| 5284 | return OK; |
| 5285 | } |
| 5286 | |
| 5287 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5288 | * Check for a match with match_text. |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 5289 | * Called after skip_to_start() has found regstart. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5290 | * Returns zero for no match, 1 for a match. |
| 5291 | */ |
| 5292 | static long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5293 | find_match_text(colnr_T startcol, int regstart, char_u *match_text) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5294 | { |
| 5295 | colnr_T col = startcol; |
| 5296 | int c1, c2; |
| 5297 | int len1, len2; |
| 5298 | int match; |
| 5299 | |
| 5300 | for (;;) |
| 5301 | { |
| 5302 | match = TRUE; |
| 5303 | len2 = MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5304 | for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
| 5305 | { |
| 5306 | c1 = PTR2CHAR(match_text + len1); |
| 5307 | c2 = PTR2CHAR(regline + col + len2); |
| 5308 | if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
| 5309 | { |
| 5310 | match = FALSE; |
| 5311 | break; |
| 5312 | } |
| 5313 | len2 += MB_CHAR2LEN(c2); |
| 5314 | } |
| 5315 | if (match |
| 5316 | #ifdef FEAT_MBYTE |
| 5317 | /* check that no composing char follows */ |
| 5318 | && !(enc_utf8 |
| 5319 | && utf_iscomposing(PTR2CHAR(regline + col + len2))) |
| 5320 | #endif |
| 5321 | ) |
| 5322 | { |
| 5323 | cleanup_subexpr(); |
| 5324 | if (REG_MULTI) |
| 5325 | { |
| 5326 | reg_startpos[0].lnum = reglnum; |
| 5327 | reg_startpos[0].col = col; |
| 5328 | reg_endpos[0].lnum = reglnum; |
| 5329 | reg_endpos[0].col = col + len2; |
| 5330 | } |
| 5331 | else |
| 5332 | { |
| 5333 | reg_startp[0] = regline + col; |
| 5334 | reg_endp[0] = regline + col + len2; |
| 5335 | } |
| 5336 | return 1L; |
| 5337 | } |
| 5338 | |
| 5339 | /* Try finding regstart after the current match. */ |
| 5340 | col += MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5341 | if (skip_to_start(regstart, &col) == FAIL) |
| 5342 | break; |
| 5343 | } |
| 5344 | return 0L; |
| 5345 | } |
| 5346 | |
| 5347 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5348 | * Main matching routine. |
| 5349 | * |
| 5350 | * Run NFA to determine whether it matches reginput. |
| 5351 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5352 | * 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] | 5353 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5354 | * Return TRUE if there is a match, FALSE otherwise. |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5355 | * When there is a match "submatch" contains the positions. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5356 | * Note: Caller must ensure that: start != NULL. |
| 5357 | */ |
| 5358 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 5359 | nfa_regmatch( |
| 5360 | nfa_regprog_T *prog, |
| 5361 | nfa_state_T *start, |
| 5362 | regsubs_T *submatch, |
| 5363 | regsubs_T *m) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5364 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5365 | int result; |
Bram Moolenaar | aaf3047 | 2015-01-27 14:40:00 +0100 | [diff] [blame] | 5366 | size_t size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5367 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5368 | int go_to_nextline = FALSE; |
| 5369 | nfa_thread_T *t; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5370 | nfa_list_T list[2]; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5371 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5372 | nfa_list_T *thislist; |
| 5373 | nfa_list_T *nextlist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5374 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5375 | nfa_state_T *add_state; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5376 | int add_here; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5377 | int add_count; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 5378 | int add_off = 0; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5379 | int toplevel = start->c == NFA_MOPEN; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5380 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5381 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5382 | |
| 5383 | if (debug == NULL) |
| 5384 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5385 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5386 | return FALSE; |
| 5387 | } |
| 5388 | #endif |
Bram Moolenaar | 41f1205 | 2013-08-25 17:01:42 +0200 | [diff] [blame] | 5389 | /* Some patterns may take a long time to match, especially when using |
| 5390 | * recursive_regmatch(). Allow interrupting them with CTRL-C. */ |
| 5391 | fast_breakcheck(); |
| 5392 | if (got_int) |
| 5393 | return FALSE; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 5394 | #ifdef FEAT_RELTIME |
| 5395 | if (nfa_time_limit != NULL && profile_passed_limit(nfa_time_limit)) |
| 5396 | return FALSE; |
| 5397 | #endif |
Bram Moolenaar | 41f1205 | 2013-08-25 17:01:42 +0200 | [diff] [blame] | 5398 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5399 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5400 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5401 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5402 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5403 | |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5404 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5405 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5406 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5407 | list[1].len = nstate + 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5408 | if (list[0].t == NULL || list[1].t == NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5409 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5410 | |
| 5411 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5412 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5413 | if (log_fd != NULL) |
| 5414 | { |
| 5415 | fprintf(log_fd, "**********************************\n"); |
| 5416 | nfa_set_code(start->c); |
| 5417 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 5418 | abs(start->id), code); |
| 5419 | fprintf(log_fd, "**********************************\n"); |
| 5420 | } |
| 5421 | else |
| 5422 | { |
| 5423 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5424 | log_fd = stderr; |
| 5425 | } |
| 5426 | #endif |
| 5427 | |
| 5428 | thislist = &list[0]; |
| 5429 | thislist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5430 | thislist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5431 | nextlist = &list[1]; |
| 5432 | nextlist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5433 | nextlist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5434 | #ifdef ENABLE_LOG |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5435 | fprintf(log_fd, "(---) STARTSTATE first\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5436 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5437 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5438 | |
| 5439 | /* Inline optimized code for addstate(thislist, start, m, 0) if we know |
| 5440 | * it's the first MOPEN. */ |
| 5441 | if (toplevel) |
| 5442 | { |
| 5443 | if (REG_MULTI) |
| 5444 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5445 | m->norm.list.multi[0].start_lnum = reglnum; |
| 5446 | m->norm.list.multi[0].start_col = (colnr_T)(reginput - regline); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5447 | } |
| 5448 | else |
| 5449 | m->norm.list.line[0].start = reginput; |
| 5450 | m->norm.in_use = 1; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5451 | addstate(thislist, start->out, m, NULL, 0); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5452 | } |
| 5453 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5454 | addstate(thislist, start, m, NULL, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5455 | |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5456 | #define ADD_STATE_IF_MATCH(state) \ |
| 5457 | if (result) { \ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5458 | add_state = state->out; \ |
| 5459 | add_off = clen; \ |
| 5460 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5461 | |
| 5462 | /* |
| 5463 | * Run for each character. |
| 5464 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5465 | for (;;) |
| 5466 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5467 | int curc; |
| 5468 | int clen; |
| 5469 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5470 | #ifdef FEAT_MBYTE |
| 5471 | if (has_mbyte) |
| 5472 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5473 | curc = (*mb_ptr2char)(reginput); |
| 5474 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5475 | } |
| 5476 | else |
| 5477 | #endif |
| 5478 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5479 | curc = *reginput; |
| 5480 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5481 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5482 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5483 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5484 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5485 | go_to_nextline = FALSE; |
| 5486 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5487 | |
| 5488 | /* swap lists */ |
| 5489 | thislist = &list[flag]; |
| 5490 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5491 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5492 | nextlist->has_pim = FALSE; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5493 | ++nfa_listid; |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 5494 | if (prog->re_engine == AUTOMATIC_ENGINE && nfa_listid >= NFA_MAX_STATES) |
| 5495 | { |
| 5496 | /* too many states, retry with old engine */ |
| 5497 | nfa_match = NFA_TOO_EXPENSIVE; |
| 5498 | goto theend; |
| 5499 | } |
| 5500 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5501 | thislist->id = nfa_listid; |
| 5502 | nextlist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5503 | |
| 5504 | #ifdef ENABLE_LOG |
| 5505 | fprintf(log_fd, "------------------------------------------\n"); |
| 5506 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5507 | 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] | 5508 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5509 | { |
| 5510 | int i; |
| 5511 | |
| 5512 | for (i = 0; i < thislist->n; i++) |
| 5513 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5514 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5515 | fprintf(log_fd, "\n"); |
| 5516 | #endif |
| 5517 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5518 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5519 | fprintf(debug, "\n-------------------\n"); |
| 5520 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5521 | /* |
| 5522 | * If the state lists are empty we can stop. |
| 5523 | */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5524 | if (thislist->n == 0) |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5525 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5526 | |
| 5527 | /* compute nextlist */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5528 | for (listidx = 0; listidx < thislist->n; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5529 | { |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5530 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5531 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5532 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5533 | nfa_set_code(t->state->c); |
| 5534 | fprintf(debug, "%s, ", code); |
| 5535 | #endif |
| 5536 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5537 | { |
| 5538 | int col; |
| 5539 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5540 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5541 | col = -1; |
| 5542 | else if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5543 | col = t->subs.norm.list.multi[0].start_col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5544 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5545 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5546 | nfa_set_code(t->state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5547 | fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n", |
| 5548 | abs(t->state->id), (int)t->state->c, code, col, |
| 5549 | pim_info(&t->pim)); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5550 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5551 | #endif |
| 5552 | |
| 5553 | /* |
| 5554 | * Handle the possible codes of the current state. |
| 5555 | * The most important is NFA_MATCH. |
| 5556 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5557 | add_state = NULL; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5558 | add_here = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5559 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5560 | switch (t->state->c) |
| 5561 | { |
| 5562 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5563 | { |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 5564 | #ifdef FEAT_MBYTE |
| 5565 | /* If the match ends before a composing characters and |
| 5566 | * ireg_icombine is not set, that is not really a match. */ |
| 5567 | if (enc_utf8 && !ireg_icombine && utf_iscomposing(curc)) |
| 5568 | break; |
| 5569 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5570 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5571 | copy_sub(&submatch->norm, &t->subs.norm); |
| 5572 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5573 | if (nfa_has_zsubexpr) |
| 5574 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5575 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5576 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5577 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5578 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5579 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5580 | * states at this position. When the list of states is going |
| 5581 | * to be empty quit without advancing, so that "reginput" is |
| 5582 | * correct. */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5583 | if (nextlist->n == 0) |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5584 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5585 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5586 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5587 | |
| 5588 | case NFA_END_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5589 | case NFA_END_INVISIBLE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5590 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5591 | /* |
| 5592 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5593 | * NFA_START_INVISIBLE_BEFORE node. |
| 5594 | * They surround a zero-width group, used with "\@=", "\&", |
| 5595 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5596 | * If we got here, it means that the current "invisible" group |
| 5597 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5598 | * nfa_regmatch(). For a look-behind match only when it ends |
| 5599 | * in the position in "nfa_endp". |
| 5600 | * Submatches are stored in *m, and used in the parent call. |
| 5601 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5602 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5603 | if (nfa_endp != NULL) |
| 5604 | { |
| 5605 | if (REG_MULTI) |
| 5606 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 5607 | (int)reglnum, |
| 5608 | (int)nfa_endp->se_u.pos.lnum, |
| 5609 | (int)(reginput - regline), |
| 5610 | nfa_endp->se_u.pos.col); |
| 5611 | else |
| 5612 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 5613 | (int)(reginput - regline), |
| 5614 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5615 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5616 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5617 | /* If "nfa_endp" is set it's only a match if it ends at |
| 5618 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5619 | if (nfa_endp != NULL && (REG_MULTI |
| 5620 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 5621 | || (int)(reginput - regline) |
| 5622 | != nfa_endp->se_u.pos.col) |
| 5623 | : reginput != nfa_endp->se_u.ptr)) |
| 5624 | break; |
| 5625 | |
| 5626 | /* do not set submatches for \@! */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5627 | if (t->state->c != NFA_END_INVISIBLE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5628 | { |
| 5629 | copy_sub(&m->norm, &t->subs.norm); |
| 5630 | #ifdef FEAT_SYN_HL |
| 5631 | if (nfa_has_zsubexpr) |
| 5632 | copy_sub(&m->synt, &t->subs.synt); |
| 5633 | #endif |
| 5634 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5635 | #ifdef ENABLE_LOG |
| 5636 | fprintf(log_fd, "Match found:\n"); |
| 5637 | log_subsexpr(m); |
| 5638 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5639 | nfa_match = TRUE; |
Bram Moolenaar | 78c93e4 | 2013-09-05 16:05:36 +0200 | [diff] [blame] | 5640 | /* See comment above at "goto nextchar". */ |
| 5641 | if (nextlist->n == 0) |
| 5642 | clen = 0; |
| 5643 | goto nextchar; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5644 | |
| 5645 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5646 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5647 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5648 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5649 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5650 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5651 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5652 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5653 | { |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5654 | #ifdef ENABLE_LOG |
| 5655 | fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", |
| 5656 | failure_chance(t->state->out, 0), |
| 5657 | failure_chance(t->state->out1->out, 0)); |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5658 | #endif |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5659 | /* Do it directly if there already is a PIM or when |
| 5660 | * nfa_postprocess() detected it will work better. */ |
| 5661 | if (t->pim.result != NFA_PIM_UNUSED |
| 5662 | || t->state->c == NFA_START_INVISIBLE_FIRST |
| 5663 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5664 | || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 5665 | || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5666 | { |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5667 | int in_use = m->norm.in_use; |
| 5668 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5669 | /* Copy submatch info for the recursive call, opposite |
| 5670 | * of what happens on success below. */ |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5671 | copy_sub_off(&m->norm, &t->subs.norm); |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5672 | #ifdef FEAT_SYN_HL |
| 5673 | if (nfa_has_zsubexpr) |
| 5674 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5675 | #endif |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5676 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5677 | /* |
| 5678 | * First try matching the invisible match, then what |
| 5679 | * follows. |
| 5680 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5681 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5682 | submatch, m, &listids); |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 5683 | if (result == NFA_TOO_EXPENSIVE) |
| 5684 | { |
| 5685 | nfa_match = result; |
| 5686 | goto theend; |
| 5687 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5688 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5689 | /* for \@! and \@<! it is a match when the result is |
| 5690 | * FALSE */ |
| 5691 | if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5692 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5693 | || t->state->c |
| 5694 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5695 | || t->state->c |
| 5696 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5697 | { |
| 5698 | /* Copy submatch info from the recursive call */ |
| 5699 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5700 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5701 | if (nfa_has_zsubexpr) |
| 5702 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5703 | #endif |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5704 | /* If the pattern has \ze and it matched in the |
| 5705 | * sub pattern, use it. */ |
| 5706 | copy_ze_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5707 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5708 | /* t->state->out1 is the corresponding |
| 5709 | * END_INVISIBLE node; Add its out to the current |
| 5710 | * list (zero-width match). */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5711 | add_here = TRUE; |
| 5712 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5713 | } |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5714 | m->norm.in_use = in_use; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5715 | } |
| 5716 | else |
| 5717 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5718 | nfa_pim_T pim; |
| 5719 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5720 | /* |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5721 | * First try matching what follows. Only if a match |
| 5722 | * is found verify the invisible match matches. Add a |
| 5723 | * nfa_pim_T to the following states, it contains info |
| 5724 | * about the invisible match. |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5725 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5726 | pim.state = t->state; |
| 5727 | pim.result = NFA_PIM_TODO; |
| 5728 | pim.subs.norm.in_use = 0; |
| 5729 | #ifdef FEAT_SYN_HL |
| 5730 | pim.subs.synt.in_use = 0; |
| 5731 | #endif |
| 5732 | if (REG_MULTI) |
| 5733 | { |
| 5734 | pim.end.pos.col = (int)(reginput - regline); |
| 5735 | pim.end.pos.lnum = reglnum; |
| 5736 | } |
| 5737 | else |
| 5738 | pim.end.ptr = reginput; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5739 | |
| 5740 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 5741 | * node; Add its out to the current list (zero-width |
| 5742 | * match). */ |
| 5743 | addstate_here(thislist, t->state->out1->out, &t->subs, |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5744 | &pim, &listidx); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5745 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5746 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5747 | break; |
| 5748 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5749 | case NFA_START_PATTERN: |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5750 | { |
| 5751 | nfa_state_T *skip = NULL; |
| 5752 | #ifdef ENABLE_LOG |
| 5753 | int skip_lid = 0; |
| 5754 | #endif |
| 5755 | |
| 5756 | /* There is no point in trying to match the pattern if the |
| 5757 | * output state is not going to be added to the list. */ |
| 5758 | if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
| 5759 | { |
| 5760 | skip = t->state->out1->out; |
| 5761 | #ifdef ENABLE_LOG |
| 5762 | skip_lid = nextlist->id; |
| 5763 | #endif |
| 5764 | } |
| 5765 | else if (state_in_list(nextlist, |
| 5766 | t->state->out1->out->out, &t->subs)) |
| 5767 | { |
| 5768 | skip = t->state->out1->out->out; |
| 5769 | #ifdef ENABLE_LOG |
| 5770 | skip_lid = nextlist->id; |
| 5771 | #endif |
| 5772 | } |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5773 | else if (state_in_list(thislist, |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5774 | t->state->out1->out->out, &t->subs)) |
| 5775 | { |
| 5776 | skip = t->state->out1->out->out; |
| 5777 | #ifdef ENABLE_LOG |
| 5778 | skip_lid = thislist->id; |
| 5779 | #endif |
| 5780 | } |
| 5781 | if (skip != NULL) |
| 5782 | { |
| 5783 | #ifdef ENABLE_LOG |
| 5784 | nfa_set_code(skip->c); |
| 5785 | fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
| 5786 | abs(skip->id), skip_lid, skip->c, code); |
| 5787 | #endif |
| 5788 | break; |
| 5789 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5790 | /* Copy submatch info to the recursive call, opposite of what |
| 5791 | * happens afterwards. */ |
| 5792 | copy_sub_off(&m->norm, &t->subs.norm); |
| 5793 | #ifdef FEAT_SYN_HL |
| 5794 | if (nfa_has_zsubexpr) |
| 5795 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5796 | #endif |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5797 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5798 | /* First try matching the pattern. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5799 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5800 | submatch, m, &listids); |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 5801 | if (result == NFA_TOO_EXPENSIVE) |
| 5802 | { |
| 5803 | nfa_match = result; |
| 5804 | goto theend; |
| 5805 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5806 | if (result) |
| 5807 | { |
| 5808 | int bytelen; |
| 5809 | |
| 5810 | #ifdef ENABLE_LOG |
| 5811 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 5812 | log_subsexpr(m); |
| 5813 | #endif |
| 5814 | /* Copy submatch info from the recursive call */ |
| 5815 | copy_sub_off(&t->subs.norm, &m->norm); |
| 5816 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5817 | if (nfa_has_zsubexpr) |
| 5818 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5819 | #endif |
| 5820 | /* Now we need to skip over the matched text and then |
| 5821 | * continue with what follows. */ |
| 5822 | if (REG_MULTI) |
| 5823 | /* TODO: multi-line match */ |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 5824 | bytelen = m->norm.list.multi[0].end_col |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5825 | - (int)(reginput - regline); |
| 5826 | else |
| 5827 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 5828 | |
| 5829 | #ifdef ENABLE_LOG |
| 5830 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 5831 | #endif |
| 5832 | if (bytelen == 0) |
| 5833 | { |
| 5834 | /* empty match, output of corresponding |
| 5835 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 5836 | * position */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5837 | add_here = TRUE; |
| 5838 | add_state = t->state->out1->out->out; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5839 | } |
| 5840 | else if (bytelen <= clen) |
| 5841 | { |
| 5842 | /* match current character, output of corresponding |
| 5843 | * NFA_END_PATTERN to be used at next position. */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5844 | add_state = t->state->out1->out->out; |
| 5845 | add_off = clen; |
| 5846 | } |
| 5847 | else |
| 5848 | { |
| 5849 | /* skip over the matched characters, set character |
| 5850 | * count in NFA_SKIP */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5851 | add_state = t->state->out1->out; |
| 5852 | add_off = bytelen; |
| 5853 | add_count = bytelen - clen; |
| 5854 | } |
| 5855 | } |
| 5856 | break; |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5857 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5858 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5859 | case NFA_BOL: |
| 5860 | if (reginput == regline) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5861 | { |
| 5862 | add_here = TRUE; |
| 5863 | add_state = t->state->out; |
| 5864 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5865 | break; |
| 5866 | |
| 5867 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5868 | if (curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5869 | { |
| 5870 | add_here = TRUE; |
| 5871 | add_state = t->state->out; |
| 5872 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5873 | break; |
| 5874 | |
| 5875 | case NFA_BOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5876 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5877 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5878 | if (curc == NUL) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5879 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5880 | #ifdef FEAT_MBYTE |
| 5881 | else if (has_mbyte) |
| 5882 | { |
| 5883 | int this_class; |
| 5884 | |
| 5885 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5886 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5887 | if (this_class <= 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5888 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5889 | else if (reg_prev_class() == this_class) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5890 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5891 | } |
| 5892 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5893 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5894 | || (reginput > regline |
| 5895 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5896 | result = FALSE; |
| 5897 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5898 | { |
| 5899 | add_here = TRUE; |
| 5900 | add_state = t->state->out; |
| 5901 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5902 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5903 | |
| 5904 | case NFA_EOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5905 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5906 | if (reginput == regline) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5907 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5908 | #ifdef FEAT_MBYTE |
| 5909 | else if (has_mbyte) |
| 5910 | { |
| 5911 | int this_class, prev_class; |
| 5912 | |
| 5913 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5914 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5915 | prev_class = reg_prev_class(); |
| 5916 | if (this_class == prev_class |
| 5917 | || prev_class == 0 || prev_class == 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5918 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5919 | } |
| 5920 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5921 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5922 | || (reginput[0] != NUL |
| 5923 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5924 | result = FALSE; |
| 5925 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5926 | { |
| 5927 | add_here = TRUE; |
| 5928 | add_state = t->state->out; |
| 5929 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5930 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5931 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5932 | case NFA_BOF: |
| 5933 | if (reglnum == 0 && reginput == regline |
| 5934 | && (!REG_MULTI || reg_firstlnum == 1)) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5935 | { |
| 5936 | add_here = TRUE; |
| 5937 | add_state = t->state->out; |
| 5938 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5939 | break; |
| 5940 | |
| 5941 | case NFA_EOF: |
| 5942 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5943 | { |
| 5944 | add_here = TRUE; |
| 5945 | add_state = t->state->out; |
| 5946 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5947 | break; |
| 5948 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5949 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5950 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5951 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5952 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5953 | int len = 0; |
| 5954 | nfa_state_T *end; |
| 5955 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5956 | int cchars[MAX_MCO]; |
| 5957 | int ccount = 0; |
| 5958 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5959 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5960 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5961 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5962 | if (utf_iscomposing(sta->c)) |
| 5963 | { |
| 5964 | /* Only match composing character(s), ignore base |
| 5965 | * character. Used for ".{composing}" and "{composing}" |
| 5966 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5967 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5968 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 5969 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5970 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5971 | /* If \Z was present, then ignore composing characters. |
| 5972 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5973 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5974 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5975 | else |
| 5976 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5977 | while (sta->c != NFA_END_COMPOSING) |
| 5978 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5979 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5980 | |
| 5981 | /* Check base character matches first, unless ignored. */ |
| 5982 | else if (len > 0 || mc == sta->c) |
| 5983 | { |
| 5984 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5985 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5986 | len += mb_char2len(mc); |
| 5987 | sta = sta->out; |
| 5988 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5989 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5990 | /* We don't care about the order of composing characters. |
| 5991 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5992 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5993 | { |
| 5994 | mc = mb_ptr2char(reginput + len); |
| 5995 | cchars[ccount++] = mc; |
| 5996 | len += mb_char2len(mc); |
| 5997 | if (ccount == MAX_MCO) |
| 5998 | break; |
| 5999 | } |
| 6000 | |
| 6001 | /* Check that each composing char in the pattern matches a |
| 6002 | * composing char in the text. We do not check if all |
| 6003 | * composing chars are matched. */ |
| 6004 | result = OK; |
| 6005 | while (sta->c != NFA_END_COMPOSING) |
| 6006 | { |
| 6007 | for (j = 0; j < ccount; ++j) |
| 6008 | if (cchars[j] == sta->c) |
| 6009 | break; |
| 6010 | if (j == ccount) |
| 6011 | { |
| 6012 | result = FAIL; |
| 6013 | break; |
| 6014 | } |
| 6015 | sta = sta->out; |
| 6016 | } |
| 6017 | } |
| 6018 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 6019 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 6020 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6021 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6022 | ADD_STATE_IF_MATCH(end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6023 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6024 | } |
| 6025 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6026 | |
| 6027 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6028 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 6029 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6030 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6031 | go_to_nextline = TRUE; |
| 6032 | /* Pass -1 for the offset, which means taking the position |
| 6033 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6034 | add_state = t->state->out; |
| 6035 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6036 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6037 | else if (curc == '\n' && reg_line_lbr) |
| 6038 | { |
| 6039 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6040 | add_state = t->state->out; |
| 6041 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6042 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6043 | break; |
| 6044 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6045 | case NFA_START_COLL: |
| 6046 | case NFA_START_NEG_COLL: |
| 6047 | { |
| 6048 | /* What follows is a list of characters, until NFA_END_COLL. |
| 6049 | * One of them must match or none of them must match. */ |
| 6050 | nfa_state_T *state; |
| 6051 | int result_if_matched; |
| 6052 | int c1, c2; |
| 6053 | |
| 6054 | /* Never match EOL. If it's part of the collection it is added |
| 6055 | * as a separate state with an OR. */ |
| 6056 | if (curc == NUL) |
| 6057 | break; |
| 6058 | |
| 6059 | state = t->state->out; |
| 6060 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 6061 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6062 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6063 | if (state->c == NFA_END_COLL) |
| 6064 | { |
| 6065 | result = !result_if_matched; |
| 6066 | break; |
| 6067 | } |
| 6068 | if (state->c == NFA_RANGE_MIN) |
| 6069 | { |
| 6070 | c1 = state->val; |
| 6071 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 6072 | c2 = state->val; |
| 6073 | #ifdef ENABLE_LOG |
| 6074 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 6075 | curc, c1, c2); |
| 6076 | #endif |
| 6077 | if (curc >= c1 && curc <= c2) |
| 6078 | { |
| 6079 | result = result_if_matched; |
| 6080 | break; |
| 6081 | } |
| 6082 | if (ireg_ic) |
| 6083 | { |
| 6084 | int curc_low = MB_TOLOWER(curc); |
| 6085 | int done = FALSE; |
| 6086 | |
| 6087 | for ( ; c1 <= c2; ++c1) |
| 6088 | if (MB_TOLOWER(c1) == curc_low) |
| 6089 | { |
| 6090 | result = result_if_matched; |
| 6091 | done = TRUE; |
| 6092 | break; |
| 6093 | } |
| 6094 | if (done) |
| 6095 | break; |
| 6096 | } |
| 6097 | } |
| 6098 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 6099 | : (curc == state->c |
| 6100 | || (ireg_ic && MB_TOLOWER(curc) |
| 6101 | == MB_TOLOWER(state->c)))) |
| 6102 | { |
| 6103 | result = result_if_matched; |
| 6104 | break; |
| 6105 | } |
| 6106 | state = state->out; |
| 6107 | } |
| 6108 | if (result) |
| 6109 | { |
| 6110 | /* next state is in out of the NFA_END_COLL, out1 of |
| 6111 | * START points to the END state */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6112 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6113 | add_off = clen; |
| 6114 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6115 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6116 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6117 | |
| 6118 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6119 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6120 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6121 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6122 | add_state = t->state->out; |
| 6123 | add_off = clen; |
| 6124 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6125 | break; |
| 6126 | |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 6127 | case NFA_ANY_COMPOSING: |
| 6128 | /* On a composing character skip over it. Otherwise do |
| 6129 | * nothing. Always matches. */ |
| 6130 | #ifdef FEAT_MBYTE |
| 6131 | if (enc_utf8 && utf_iscomposing(curc)) |
| 6132 | { |
| 6133 | add_off = clen; |
| 6134 | } |
| 6135 | else |
| 6136 | #endif |
| 6137 | { |
| 6138 | add_here = TRUE; |
| 6139 | add_off = 0; |
| 6140 | } |
| 6141 | add_state = t->state->out; |
| 6142 | break; |
| 6143 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6144 | /* |
| 6145 | * Character classes like \a for alpha, \d for digit etc. |
| 6146 | */ |
| 6147 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6148 | result = vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6149 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6150 | break; |
| 6151 | |
| 6152 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6153 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6154 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6155 | break; |
| 6156 | |
| 6157 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 6158 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6159 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6160 | break; |
| 6161 | |
| 6162 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6163 | result = !VIM_ISDIGIT(curc) |
| 6164 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6165 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6166 | break; |
| 6167 | |
| 6168 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6169 | result = vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6170 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6171 | break; |
| 6172 | |
| 6173 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6174 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6175 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6176 | break; |
| 6177 | |
| 6178 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6179 | result = vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6180 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6181 | break; |
| 6182 | |
| 6183 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6184 | result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6185 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6186 | break; |
| 6187 | |
| 6188 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6189 | result = vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6190 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6191 | break; |
| 6192 | |
| 6193 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6194 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6195 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6196 | break; |
| 6197 | |
| 6198 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6199 | result = ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6200 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6201 | break; |
| 6202 | |
| 6203 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6204 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6205 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6206 | break; |
| 6207 | |
| 6208 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6209 | result = ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6210 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6211 | break; |
| 6212 | |
| 6213 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6214 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6215 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6216 | break; |
| 6217 | |
| 6218 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6219 | result = ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6220 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6221 | break; |
| 6222 | |
| 6223 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6224 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6225 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6226 | break; |
| 6227 | |
| 6228 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6229 | result = ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6230 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6231 | break; |
| 6232 | |
| 6233 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6234 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6235 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6236 | break; |
| 6237 | |
| 6238 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6239 | result = ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6240 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6241 | break; |
| 6242 | |
| 6243 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6244 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6245 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6246 | break; |
| 6247 | |
| 6248 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6249 | result = ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6250 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6251 | break; |
| 6252 | |
| 6253 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6254 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6255 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6256 | break; |
| 6257 | |
| 6258 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6259 | result = ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6260 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6261 | break; |
| 6262 | |
| 6263 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6264 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6265 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6266 | break; |
| 6267 | |
| 6268 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6269 | result = ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6270 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6271 | break; |
| 6272 | |
| 6273 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6274 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6275 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6276 | break; |
| 6277 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 6278 | case NFA_LOWER_IC: /* [a-z] */ |
| 6279 | result = ri_lower(curc) || (ireg_ic && ri_upper(curc)); |
| 6280 | ADD_STATE_IF_MATCH(t->state); |
| 6281 | break; |
| 6282 | |
| 6283 | case NFA_NLOWER_IC: /* [^a-z] */ |
| 6284 | result = curc != NUL |
| 6285 | && !(ri_lower(curc) || (ireg_ic && ri_upper(curc))); |
| 6286 | ADD_STATE_IF_MATCH(t->state); |
| 6287 | break; |
| 6288 | |
| 6289 | case NFA_UPPER_IC: /* [A-Z] */ |
| 6290 | result = ri_upper(curc) || (ireg_ic && ri_lower(curc)); |
| 6291 | ADD_STATE_IF_MATCH(t->state); |
| 6292 | break; |
| 6293 | |
| 6294 | case NFA_NUPPER_IC: /* ^[A-Z] */ |
| 6295 | result = curc != NUL |
| 6296 | && !(ri_upper(curc) || (ireg_ic && ri_lower(curc))); |
| 6297 | ADD_STATE_IF_MATCH(t->state); |
| 6298 | break; |
| 6299 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6300 | case NFA_BACKREF1: |
| 6301 | case NFA_BACKREF2: |
| 6302 | case NFA_BACKREF3: |
| 6303 | case NFA_BACKREF4: |
| 6304 | case NFA_BACKREF5: |
| 6305 | case NFA_BACKREF6: |
| 6306 | case NFA_BACKREF7: |
| 6307 | case NFA_BACKREF8: |
| 6308 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6309 | #ifdef FEAT_SYN_HL |
| 6310 | case NFA_ZREF1: |
| 6311 | case NFA_ZREF2: |
| 6312 | case NFA_ZREF3: |
| 6313 | case NFA_ZREF4: |
| 6314 | case NFA_ZREF5: |
| 6315 | case NFA_ZREF6: |
| 6316 | case NFA_ZREF7: |
| 6317 | case NFA_ZREF8: |
| 6318 | case NFA_ZREF9: |
| 6319 | #endif |
| 6320 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6321 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6322 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6323 | int bytelen; |
| 6324 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6325 | if (t->state->c <= NFA_BACKREF9) |
| 6326 | { |
| 6327 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 6328 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 6329 | } |
| 6330 | #ifdef FEAT_SYN_HL |
| 6331 | else |
| 6332 | { |
| 6333 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 6334 | result = match_zref(subidx, &bytelen); |
| 6335 | } |
| 6336 | #endif |
| 6337 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6338 | if (result) |
| 6339 | { |
| 6340 | if (bytelen == 0) |
| 6341 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 6342 | /* empty match always works, output of NFA_SKIP to be |
| 6343 | * used next */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6344 | add_here = TRUE; |
| 6345 | add_state = t->state->out->out; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6346 | } |
| 6347 | else if (bytelen <= clen) |
| 6348 | { |
| 6349 | /* match current character, jump ahead to out of |
| 6350 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6351 | add_state = t->state->out->out; |
| 6352 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6353 | } |
| 6354 | else |
| 6355 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 6356 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6357 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6358 | add_state = t->state->out; |
| 6359 | add_off = bytelen; |
| 6360 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6361 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6362 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6363 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6364 | } |
| 6365 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 6366 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6367 | if (t->count - clen <= 0) |
| 6368 | { |
| 6369 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6370 | add_state = t->state->out; |
| 6371 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6372 | } |
| 6373 | else |
| 6374 | { |
| 6375 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6376 | add_state = t->state; |
| 6377 | add_off = 0; |
| 6378 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6379 | } |
| 6380 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6381 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6382 | case NFA_LNUM: |
| 6383 | case NFA_LNUM_GT: |
| 6384 | case NFA_LNUM_LT: |
| 6385 | result = (REG_MULTI && |
| 6386 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 6387 | (long_u)(reglnum + reg_firstlnum))); |
| 6388 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6389 | { |
| 6390 | add_here = TRUE; |
| 6391 | add_state = t->state->out; |
| 6392 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6393 | break; |
| 6394 | |
| 6395 | case NFA_COL: |
| 6396 | case NFA_COL_GT: |
| 6397 | case NFA_COL_LT: |
| 6398 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 6399 | (long_u)(reginput - regline) + 1); |
| 6400 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6401 | { |
| 6402 | add_here = TRUE; |
| 6403 | add_state = t->state->out; |
| 6404 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6405 | break; |
| 6406 | |
| 6407 | case NFA_VCOL: |
| 6408 | case NFA_VCOL_GT: |
| 6409 | case NFA_VCOL_LT: |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6410 | { |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6411 | int op = t->state->c - NFA_VCOL; |
| 6412 | colnr_T col = (colnr_T)(reginput - regline); |
Bram Moolenaar | ef795d1 | 2015-01-18 16:46:32 +0100 | [diff] [blame] | 6413 | win_T *wp = reg_win == NULL ? curwin : reg_win; |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6414 | |
| 6415 | /* Bail out quickly when there can't be a match, avoid the |
| 6416 | * overhead of win_linetabsize() on long lines. */ |
Bram Moolenaar | 4f36dc3 | 2015-03-05 17:16:06 +0100 | [diff] [blame] | 6417 | if (op != 1 && col > t->state->val |
| 6418 | #ifdef FEAT_MBYTE |
| 6419 | * (has_mbyte ? MB_MAXBYTES : 1) |
| 6420 | #endif |
| 6421 | ) |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6422 | break; |
Bram Moolenaar | ef795d1 | 2015-01-18 16:46:32 +0100 | [diff] [blame] | 6423 | result = FALSE; |
| 6424 | if (op == 1 && col - 1 > t->state->val && col > 100) |
| 6425 | { |
| 6426 | int ts = wp->w_buffer->b_p_ts; |
| 6427 | |
| 6428 | /* Guess that a character won't use more columns than |
| 6429 | * 'tabstop', with a minimum of 4. */ |
| 6430 | if (ts < 4) |
| 6431 | ts = 4; |
| 6432 | result = col > t->state->val * ts; |
| 6433 | } |
| 6434 | if (!result) |
| 6435 | result = nfa_re_num_cmp(t->state->val, op, |
| 6436 | (long_u)win_linetabsize(wp, regline, col) + 1); |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6437 | if (result) |
| 6438 | { |
| 6439 | add_here = TRUE; |
| 6440 | add_state = t->state->out; |
| 6441 | } |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6442 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6443 | break; |
| 6444 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6445 | case NFA_MARK: |
| 6446 | case NFA_MARK_GT: |
| 6447 | case NFA_MARK_LT: |
| 6448 | { |
| 6449 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 6450 | |
| 6451 | /* Compare the mark position to the match position. */ |
| 6452 | result = (pos != NULL /* mark doesn't exist */ |
| 6453 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 6454 | && (pos->lnum == reglnum + reg_firstlnum |
| 6455 | ? (pos->col == (colnr_T)(reginput - regline) |
| 6456 | ? t->state->c == NFA_MARK |
| 6457 | : (pos->col < (colnr_T)(reginput - regline) |
| 6458 | ? t->state->c == NFA_MARK_GT |
| 6459 | : t->state->c == NFA_MARK_LT)) |
| 6460 | : (pos->lnum < reglnum + reg_firstlnum |
| 6461 | ? t->state->c == NFA_MARK_GT |
| 6462 | : t->state->c == NFA_MARK_LT))); |
| 6463 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6464 | { |
| 6465 | add_here = TRUE; |
| 6466 | add_state = t->state->out; |
| 6467 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6468 | break; |
| 6469 | } |
| 6470 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6471 | case NFA_CURSOR: |
| 6472 | result = (reg_win != NULL |
| 6473 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 6474 | && ((colnr_T)(reginput - regline) |
| 6475 | == reg_win->w_cursor.col)); |
| 6476 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6477 | { |
| 6478 | add_here = TRUE; |
| 6479 | add_state = t->state->out; |
| 6480 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6481 | break; |
| 6482 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6483 | case NFA_VISUAL: |
| 6484 | result = reg_match_visual(); |
| 6485 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6486 | { |
| 6487 | add_here = TRUE; |
| 6488 | add_state = t->state->out; |
| 6489 | } |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 6490 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6491 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6492 | case NFA_MOPEN1: |
| 6493 | case NFA_MOPEN2: |
| 6494 | case NFA_MOPEN3: |
| 6495 | case NFA_MOPEN4: |
| 6496 | case NFA_MOPEN5: |
| 6497 | case NFA_MOPEN6: |
| 6498 | case NFA_MOPEN7: |
| 6499 | case NFA_MOPEN8: |
| 6500 | case NFA_MOPEN9: |
| 6501 | #ifdef FEAT_SYN_HL |
| 6502 | case NFA_ZOPEN: |
| 6503 | case NFA_ZOPEN1: |
| 6504 | case NFA_ZOPEN2: |
| 6505 | case NFA_ZOPEN3: |
| 6506 | case NFA_ZOPEN4: |
| 6507 | case NFA_ZOPEN5: |
| 6508 | case NFA_ZOPEN6: |
| 6509 | case NFA_ZOPEN7: |
| 6510 | case NFA_ZOPEN8: |
| 6511 | case NFA_ZOPEN9: |
| 6512 | #endif |
| 6513 | case NFA_NOPEN: |
| 6514 | case NFA_ZSTART: |
| 6515 | /* These states are only added to be able to bail out when |
| 6516 | * they are added again, nothing is to be done. */ |
| 6517 | break; |
| 6518 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6519 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6520 | { |
| 6521 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6522 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6523 | #ifdef DEBUG |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6524 | if (c < 0) |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6525 | EMSGN("INTERNAL: Negative state char: %ld", c); |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6526 | #endif |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6527 | result = (c == curc); |
| 6528 | |
| 6529 | if (!result && ireg_ic) |
| 6530 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6531 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 8df5acf | 2014-05-13 19:37:29 +0200 | [diff] [blame] | 6532 | /* If ireg_icombine is not set only skip over the character |
| 6533 | * itself. When it is set skip over composing characters. */ |
| 6534 | if (result && enc_utf8 && !ireg_icombine) |
Bram Moolenaar | 2186ffa | 2015-05-04 10:33:15 +0200 | [diff] [blame] | 6535 | clen = utf_ptr2len(reginput); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6536 | #endif |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6537 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6538 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6539 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6540 | |
| 6541 | } /* switch (t->state->c) */ |
| 6542 | |
| 6543 | if (add_state != NULL) |
| 6544 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6545 | nfa_pim_T *pim; |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6546 | nfa_pim_T pim_copy; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6547 | |
| 6548 | if (t->pim.result == NFA_PIM_UNUSED) |
| 6549 | pim = NULL; |
| 6550 | else |
| 6551 | pim = &t->pim; |
| 6552 | |
| 6553 | /* Handle the postponed invisible match if the match might end |
| 6554 | * without advancing and before the end of the line. */ |
| 6555 | if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6556 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6557 | if (pim->result == NFA_PIM_TODO) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6558 | { |
| 6559 | #ifdef ENABLE_LOG |
| 6560 | fprintf(log_fd, "\n"); |
| 6561 | fprintf(log_fd, "==================================\n"); |
| 6562 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 6563 | fprintf(log_fd, "\n"); |
| 6564 | #endif |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6565 | result = recursive_regmatch(pim->state, pim, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6566 | prog, submatch, m, &listids); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6567 | pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6568 | /* for \@! and \@<! it is a match when the result is |
| 6569 | * FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6570 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6571 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6572 | || pim->state->c |
| 6573 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6574 | || pim->state->c |
| 6575 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6576 | { |
| 6577 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6578 | copy_sub_off(&pim->subs.norm, &m->norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6579 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6580 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6581 | copy_sub_off(&pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6582 | #endif |
| 6583 | } |
| 6584 | } |
| 6585 | else |
| 6586 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6587 | result = (pim->result == NFA_PIM_MATCH); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6588 | #ifdef ENABLE_LOG |
| 6589 | fprintf(log_fd, "\n"); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6590 | 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] | 6591 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 6592 | fprintf(log_fd, "\n"); |
| 6593 | #endif |
| 6594 | } |
| 6595 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6596 | /* for \@! and \@<! it is a match when result is FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6597 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6598 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6599 | || pim->state->c |
| 6600 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6601 | || pim->state->c |
| 6602 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6603 | { |
| 6604 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6605 | copy_sub_off(&t->subs.norm, &pim->subs.norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6606 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6607 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6608 | copy_sub_off(&t->subs.synt, &pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6609 | #endif |
| 6610 | } |
| 6611 | else |
| 6612 | /* look-behind match failed, don't add the state */ |
| 6613 | continue; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6614 | |
| 6615 | /* Postponed invisible match was handled, don't add it to |
| 6616 | * following states. */ |
| 6617 | pim = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6618 | } |
| 6619 | |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6620 | /* If "pim" points into l->t it will become invalid when |
| 6621 | * adding the state causes the list to be reallocated. Make a |
| 6622 | * local copy to avoid that. */ |
| 6623 | if (pim == &t->pim) |
| 6624 | { |
| 6625 | copy_pim(&pim_copy, pim); |
| 6626 | pim = &pim_copy; |
| 6627 | } |
| 6628 | |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6629 | if (add_here) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6630 | addstate_here(thislist, add_state, &t->subs, pim, &listidx); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6631 | else |
| 6632 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6633 | addstate(nextlist, add_state, &t->subs, pim, add_off); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6634 | if (add_count > 0) |
| 6635 | nextlist->t[nextlist->n - 1].count = add_count; |
| 6636 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6637 | } |
| 6638 | |
| 6639 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 6640 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6641 | /* Look for the start of a match in the current position by adding the |
| 6642 | * start state to the list of states. |
| 6643 | * The first found match is the leftmost one, thus the order of states |
| 6644 | * matters! |
| 6645 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 6646 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6647 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6648 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6649 | if (nfa_match == FALSE |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6650 | && ((toplevel |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6651 | && reglnum == 0 |
| 6652 | && clen != 0 |
| 6653 | && (ireg_maxcol == 0 |
| 6654 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6655 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6656 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6657 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 6658 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6659 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6660 | < nfa_endp->se_u.pos.col)) |
| 6661 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6662 | { |
| 6663 | #ifdef ENABLE_LOG |
| 6664 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 6665 | #endif |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6666 | /* Inline optimized code for addstate() if we know the state is |
| 6667 | * the first MOPEN. */ |
| 6668 | if (toplevel) |
| 6669 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6670 | int add = TRUE; |
| 6671 | int c; |
| 6672 | |
| 6673 | if (prog->regstart != NUL && clen != 0) |
| 6674 | { |
| 6675 | if (nextlist->n == 0) |
| 6676 | { |
| 6677 | colnr_T col = (colnr_T)(reginput - regline) + clen; |
| 6678 | |
| 6679 | /* Nextlist is empty, we can skip ahead to the |
| 6680 | * character that must appear at the start. */ |
| 6681 | if (skip_to_start(prog->regstart, &col) == FAIL) |
| 6682 | break; |
| 6683 | #ifdef ENABLE_LOG |
| 6684 | fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
| 6685 | col - ((colnr_T)(reginput - regline) + clen)); |
| 6686 | #endif |
| 6687 | reginput = regline + col - clen; |
| 6688 | } |
| 6689 | else |
| 6690 | { |
| 6691 | /* Checking if the required start character matches is |
| 6692 | * cheaper than adding a state that won't match. */ |
| 6693 | c = PTR2CHAR(reginput + clen); |
| 6694 | if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c) |
| 6695 | != MB_TOLOWER(prog->regstart))) |
| 6696 | { |
| 6697 | #ifdef ENABLE_LOG |
| 6698 | fprintf(log_fd, " Skipping start state, regstart does not match\n"); |
| 6699 | #endif |
| 6700 | add = FALSE; |
| 6701 | } |
| 6702 | } |
| 6703 | } |
| 6704 | |
| 6705 | if (add) |
| 6706 | { |
| 6707 | if (REG_MULTI) |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6708 | m->norm.list.multi[0].start_col = |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6709 | (colnr_T)(reginput - regline) + clen; |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6710 | else |
| 6711 | m->norm.list.line[0].start = reginput + clen; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6712 | addstate(nextlist, start->out, m, NULL, clen); |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6713 | } |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6714 | } |
| 6715 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6716 | addstate(nextlist, start, m, NULL, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6717 | } |
| 6718 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6719 | #ifdef ENABLE_LOG |
| 6720 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6721 | { |
| 6722 | int i; |
| 6723 | |
| 6724 | for (i = 0; i < thislist->n; i++) |
| 6725 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 6726 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6727 | fprintf(log_fd, "\n"); |
| 6728 | #endif |
| 6729 | |
| 6730 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6731 | /* Advance to the next character, or advance to the next line, or |
| 6732 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6733 | if (clen != 0) |
| 6734 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6735 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 6736 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6737 | reg_nextline(); |
| 6738 | else |
| 6739 | break; |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6740 | |
| 6741 | /* Allow interrupting with CTRL-C. */ |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6742 | line_breakcheck(); |
Bram Moolenaar | a20bcad | 2015-01-14 18:40:28 +0100 | [diff] [blame] | 6743 | if (got_int) |
| 6744 | break; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6745 | #ifdef FEAT_RELTIME |
| 6746 | /* Check for timeout once in a twenty times to avoid overhead. */ |
| 6747 | if (nfa_time_limit != NULL && ++nfa_time_count == 20) |
| 6748 | { |
| 6749 | nfa_time_count = 0; |
| 6750 | if (profile_passed_limit(nfa_time_limit)) |
| 6751 | break; |
| 6752 | } |
| 6753 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6754 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6755 | |
| 6756 | #ifdef ENABLE_LOG |
| 6757 | if (log_fd != stderr) |
| 6758 | fclose(log_fd); |
| 6759 | log_fd = NULL; |
| 6760 | #endif |
| 6761 | |
| 6762 | theend: |
| 6763 | /* Free memory */ |
| 6764 | vim_free(list[0].t); |
| 6765 | vim_free(list[1].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6766 | vim_free(listids); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6767 | #undef ADD_STATE_IF_MATCH |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 6768 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6769 | fclose(debug); |
| 6770 | #endif |
| 6771 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6772 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6773 | } |
| 6774 | |
| 6775 | /* |
| 6776 | * Try match of "prog" with at regline["col"]. |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 6777 | * Returns <= 0 for failure, number of lines contained in the match otherwise. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6778 | */ |
| 6779 | static long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6780 | nfa_regtry( |
| 6781 | nfa_regprog_T *prog, |
| 6782 | colnr_T col, |
| 6783 | proftime_T *tm UNUSED) /* timeout limit or NULL */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6784 | { |
| 6785 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6786 | regsubs_T subs, m; |
| 6787 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 6788 | int result; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6789 | #ifdef ENABLE_LOG |
| 6790 | FILE *f; |
| 6791 | #endif |
| 6792 | |
| 6793 | reginput = regline + col; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 6794 | #ifdef FEAT_RELTIME |
| 6795 | nfa_time_limit = tm; |
| 6796 | nfa_time_count = 0; |
| 6797 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6798 | |
| 6799 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6800 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6801 | if (f != NULL) |
| 6802 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6803 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6804 | #ifdef DEBUG |
| 6805 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 6806 | #endif |
| 6807 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6808 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 6809 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6810 | fprintf(f, "\n\n"); |
| 6811 | fclose(f); |
| 6812 | } |
| 6813 | else |
| 6814 | EMSG(_("Could not open temporary log file for writing ")); |
| 6815 | #endif |
| 6816 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6817 | clear_sub(&subs.norm); |
| 6818 | clear_sub(&m.norm); |
| 6819 | #ifdef FEAT_SYN_HL |
| 6820 | clear_sub(&subs.synt); |
| 6821 | clear_sub(&m.synt); |
| 6822 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6823 | |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 6824 | result = nfa_regmatch(prog, start, &subs, &m); |
| 6825 | if (result == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6826 | return 0; |
Bram Moolenaar | fda3729 | 2014-11-05 14:27:36 +0100 | [diff] [blame] | 6827 | else if (result == NFA_TOO_EXPENSIVE) |
| 6828 | return result; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6829 | |
| 6830 | cleanup_subexpr(); |
| 6831 | if (REG_MULTI) |
| 6832 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6833 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6834 | { |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6835 | reg_startpos[i].lnum = subs.norm.list.multi[i].start_lnum; |
| 6836 | reg_startpos[i].col = subs.norm.list.multi[i].start_col; |
| 6837 | |
| 6838 | reg_endpos[i].lnum = subs.norm.list.multi[i].end_lnum; |
| 6839 | reg_endpos[i].col = subs.norm.list.multi[i].end_col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | if (reg_startpos[0].lnum < 0) |
| 6843 | { |
| 6844 | reg_startpos[0].lnum = 0; |
| 6845 | reg_startpos[0].col = col; |
| 6846 | } |
| 6847 | if (reg_endpos[0].lnum < 0) |
| 6848 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 6849 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6850 | reg_endpos[0].lnum = reglnum; |
| 6851 | reg_endpos[0].col = (int)(reginput - regline); |
| 6852 | } |
| 6853 | else |
| 6854 | /* Use line number of "\ze". */ |
| 6855 | reglnum = reg_endpos[0].lnum; |
| 6856 | } |
| 6857 | else |
| 6858 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6859 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6860 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6861 | reg_startp[i] = subs.norm.list.line[i].start; |
| 6862 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6863 | } |
| 6864 | |
| 6865 | if (reg_startp[0] == NULL) |
| 6866 | reg_startp[0] = regline + col; |
| 6867 | if (reg_endp[0] == NULL) |
| 6868 | reg_endp[0] = reginput; |
| 6869 | } |
| 6870 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6871 | #ifdef FEAT_SYN_HL |
| 6872 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 6873 | unref_extmatch(re_extmatch_out); |
| 6874 | re_extmatch_out = NULL; |
| 6875 | |
| 6876 | if (prog->reghasz == REX_SET) |
| 6877 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6878 | cleanup_zsubexpr(); |
| 6879 | re_extmatch_out = make_extmatch(); |
Bram Moolenaar | 5ad075c | 2015-11-24 15:18:32 +0100 | [diff] [blame] | 6880 | /* Loop over \z1, \z2, etc. There is no \z0. */ |
| 6881 | for (i = 1; i < subs.synt.in_use; i++) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6882 | { |
| 6883 | if (REG_MULTI) |
| 6884 | { |
| 6885 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 6886 | |
Bram Moolenaar | 5a4e160 | 2014-04-06 21:34:04 +0200 | [diff] [blame] | 6887 | /* Only accept single line matches that are valid. */ |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6888 | if (mpos->start_lnum >= 0 |
| 6889 | && mpos->start_lnum == mpos->end_lnum |
| 6890 | && mpos->end_col >= mpos->start_col) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6891 | re_extmatch_out->matches[i] = |
Bram Moolenaar | 0cd040b | 2015-01-27 14:54:11 +0100 | [diff] [blame] | 6892 | vim_strnsave(reg_getline(mpos->start_lnum) |
| 6893 | + mpos->start_col, |
| 6894 | mpos->end_col - mpos->start_col); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6895 | } |
| 6896 | else |
| 6897 | { |
| 6898 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 6899 | |
| 6900 | if (lpos->start != NULL && lpos->end != NULL) |
| 6901 | re_extmatch_out->matches[i] = |
| 6902 | vim_strnsave(lpos->start, |
| 6903 | (int)(lpos->end - lpos->start)); |
| 6904 | } |
| 6905 | } |
| 6906 | } |
| 6907 | #endif |
| 6908 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6909 | return 1 + reglnum; |
| 6910 | } |
| 6911 | |
| 6912 | /* |
| 6913 | * Match a regexp against a string ("line" points to the string) or multiple |
| 6914 | * lines ("line" is NULL, use reg_getline()). |
| 6915 | * |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 6916 | * Returns <= 0 for failure, number of lines contained in the match otherwise. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6917 | */ |
| 6918 | static long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 6919 | nfa_regexec_both( |
| 6920 | char_u *line, |
| 6921 | colnr_T startcol, /* column to start looking for match */ |
| 6922 | proftime_T *tm) /* timeout limit or NULL */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6923 | { |
| 6924 | nfa_regprog_T *prog; |
| 6925 | long retval = 0L; |
| 6926 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6927 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6928 | |
| 6929 | if (REG_MULTI) |
| 6930 | { |
| 6931 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 6932 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 6933 | reg_startpos = reg_mmatch->startpos; |
| 6934 | reg_endpos = reg_mmatch->endpos; |
| 6935 | } |
| 6936 | else |
| 6937 | { |
| 6938 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 6939 | reg_startp = reg_match->startp; |
| 6940 | reg_endp = reg_match->endp; |
| 6941 | } |
| 6942 | |
| 6943 | /* Be paranoid... */ |
| 6944 | if (prog == NULL || line == NULL) |
| 6945 | { |
| 6946 | EMSG(_(e_null)); |
| 6947 | goto theend; |
| 6948 | } |
| 6949 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6950 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 6951 | if (prog->regflags & RF_ICASE) |
| 6952 | ireg_ic = TRUE; |
| 6953 | else if (prog->regflags & RF_NOICASE) |
| 6954 | ireg_ic = FALSE; |
| 6955 | |
| 6956 | #ifdef FEAT_MBYTE |
| 6957 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 6958 | if (prog->regflags & RF_ICOMBINE) |
| 6959 | ireg_icombine = TRUE; |
| 6960 | #endif |
| 6961 | |
| 6962 | regline = line; |
| 6963 | reglnum = 0; /* relative to line */ |
| 6964 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6965 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6966 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6967 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6968 | nfa_listid = 1; |
| 6969 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6970 | nfa_regengine.expr = prog->pattern; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6971 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6972 | if (prog->reganch && col > 0) |
| 6973 | return 0L; |
| 6974 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6975 | need_clear_subexpr = TRUE; |
| 6976 | #ifdef FEAT_SYN_HL |
| 6977 | /* Clear the external match subpointers if necessary. */ |
| 6978 | if (prog->reghasz == REX_SET) |
| 6979 | { |
| 6980 | nfa_has_zsubexpr = TRUE; |
| 6981 | need_clear_zsubexpr = TRUE; |
| 6982 | } |
| 6983 | else |
| 6984 | nfa_has_zsubexpr = FALSE; |
| 6985 | #endif |
| 6986 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6987 | if (prog->regstart != NUL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6988 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6989 | /* Skip ahead until a character we know the match must start with. |
| 6990 | * When there is none there is no match. */ |
| 6991 | if (skip_to_start(prog->regstart, &col) == FAIL) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6992 | return 0L; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6993 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6994 | /* If match_text is set it contains the full text that must match. |
| 6995 | * Nothing else to try. Doesn't handle combining chars well. */ |
Bram Moolenaar | a940aa6 | 2013-06-08 23:30:04 +0200 | [diff] [blame] | 6996 | if (prog->match_text != NULL |
| 6997 | #ifdef FEAT_MBYTE |
| 6998 | && !ireg_icombine |
| 6999 | #endif |
| 7000 | ) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7001 | return find_match_text(col, prog->regstart, prog->match_text); |
| 7002 | } |
| 7003 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7004 | /* If the start column is past the maximum column: no need to try. */ |
| 7005 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 7006 | goto theend; |
| 7007 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 7008 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7009 | for (i = 0; i < nstate; ++i) |
| 7010 | { |
| 7011 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 7012 | prog->state[i].lastlist[0] = 0; |
| 7013 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7014 | } |
| 7015 | |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 7016 | retval = nfa_regtry(prog, col, tm); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7017 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7018 | nfa_regengine.expr = NULL; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7019 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7020 | theend: |
| 7021 | return retval; |
| 7022 | } |
| 7023 | |
| 7024 | /* |
| 7025 | * Compile a regular expression into internal code for the NFA matcher. |
| 7026 | * Returns the program in allocated space. Returns NULL for an error. |
| 7027 | */ |
| 7028 | static regprog_T * |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7029 | nfa_regcomp(char_u *expr, int re_flags) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7030 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7031 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 7032 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7033 | int *postfix; |
| 7034 | |
| 7035 | if (expr == NULL) |
| 7036 | return NULL; |
| 7037 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7038 | nfa_regengine.expr = expr; |
Bram Moolenaar | e0ad365 | 2015-01-27 12:59:55 +0100 | [diff] [blame] | 7039 | nfa_re_flags = re_flags; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7040 | |
| 7041 | init_class_tab(); |
| 7042 | |
| 7043 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 7044 | return NULL; |
| 7045 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7046 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7047 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7048 | postfix = re2post(); |
| 7049 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 7050 | { |
| 7051 | /* TODO: only give this error for debugging? */ |
| 7052 | if (post_ptr >= post_end) |
| 7053 | 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] | 7054 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 7055 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7056 | |
| 7057 | /* |
| 7058 | * In order to build the NFA, we parse the input regexp twice: |
| 7059 | * 1. first pass to count size (so we can allocate space) |
| 7060 | * 2. second to emit code |
| 7061 | */ |
| 7062 | #ifdef ENABLE_LOG |
| 7063 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 7064 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7065 | |
| 7066 | if (f != NULL) |
| 7067 | { |
| 7068 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 7069 | fclose(f); |
| 7070 | } |
| 7071 | } |
| 7072 | #endif |
| 7073 | |
| 7074 | /* |
| 7075 | * PASS 1 |
| 7076 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 7077 | */ |
| 7078 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7079 | |
Bram Moolenaar | 16619a2 | 2013-06-11 18:42:36 +0200 | [diff] [blame] | 7080 | /* allocate the regprog with space for the compiled regexp */ |
| 7081 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 7082 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 7083 | if (prog == NULL) |
| 7084 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7085 | state_ptr = prog->state; |
| 7086 | |
| 7087 | /* |
| 7088 | * PASS 2 |
| 7089 | * Build the NFA |
| 7090 | */ |
| 7091 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 7092 | if (prog->start == NULL) |
| 7093 | goto fail; |
| 7094 | |
| 7095 | prog->regflags = regflags; |
| 7096 | prog->engine = &nfa_regengine; |
| 7097 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 7098 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 7099 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 7100 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7101 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 7102 | nfa_postprocess(prog); |
| 7103 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7104 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 7105 | prog->regstart = nfa_get_regstart(prog->start, 0); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7106 | prog->match_text = nfa_get_match_text(prog->start); |
| 7107 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7108 | #ifdef ENABLE_LOG |
| 7109 | nfa_postfix_dump(expr, OK); |
| 7110 | nfa_dump(prog); |
| 7111 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 7112 | #ifdef FEAT_SYN_HL |
| 7113 | /* Remember whether this pattern has any \z specials in it. */ |
| 7114 | prog->reghasz = re_has_z; |
| 7115 | #endif |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7116 | prog->pattern = vim_strsave(expr); |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7117 | nfa_regengine.expr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7118 | |
| 7119 | out: |
| 7120 | vim_free(post_start); |
| 7121 | post_start = post_ptr = post_end = NULL; |
| 7122 | state_ptr = NULL; |
| 7123 | return (regprog_T *)prog; |
| 7124 | |
| 7125 | fail: |
| 7126 | vim_free(prog); |
| 7127 | prog = NULL; |
| 7128 | #ifdef ENABLE_LOG |
| 7129 | nfa_postfix_dump(expr, FAIL); |
| 7130 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7131 | nfa_regengine.expr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7132 | goto out; |
| 7133 | } |
| 7134 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7135 | /* |
| 7136 | * Free a compiled regexp program, returned by nfa_regcomp(). |
| 7137 | */ |
| 7138 | static void |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7139 | nfa_regfree(regprog_T *prog) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7140 | { |
| 7141 | if (prog != NULL) |
| 7142 | { |
| 7143 | vim_free(((nfa_regprog_T *)prog)->match_text); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7144 | vim_free(((nfa_regprog_T *)prog)->pattern); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7145 | vim_free(prog); |
| 7146 | } |
| 7147 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7148 | |
| 7149 | /* |
| 7150 | * Match a regexp against a string. |
| 7151 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 7152 | * Uses curbuf for line count and 'iskeyword'. |
Bram Moolenaar | 2af78a1 | 2014-04-23 19:06:37 +0200 | [diff] [blame] | 7153 | * 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] | 7154 | * |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 7155 | * Returns <= 0 for failure, number of lines contained in the match otherwise. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7156 | */ |
| 7157 | static int |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7158 | nfa_regexec_nl( |
| 7159 | regmatch_T *rmp, |
| 7160 | char_u *line, /* string to match against */ |
| 7161 | colnr_T col, /* column to start looking for match */ |
| 7162 | int line_lbr) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7163 | { |
| 7164 | reg_match = rmp; |
| 7165 | reg_mmatch = NULL; |
| 7166 | reg_maxline = 0; |
Bram Moolenaar | 2af78a1 | 2014-04-23 19:06:37 +0200 | [diff] [blame] | 7167 | reg_line_lbr = line_lbr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7168 | reg_buf = curbuf; |
| 7169 | reg_win = NULL; |
| 7170 | ireg_ic = rmp->rm_ic; |
| 7171 | #ifdef FEAT_MBYTE |
| 7172 | ireg_icombine = FALSE; |
| 7173 | #endif |
| 7174 | ireg_maxcol = 0; |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 7175 | return nfa_regexec_both(line, col, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7176 | } |
| 7177 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7178 | |
| 7179 | /* |
| 7180 | * Match a regexp against multiple lines. |
| 7181 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 7182 | * Uses curbuf for line count and 'iskeyword'. |
| 7183 | * |
Bram Moolenaar | 8c73150 | 2014-11-23 15:57:49 +0100 | [diff] [blame] | 7184 | * 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] | 7185 | * match otherwise. |
| 7186 | * |
| 7187 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 7188 | * |
| 7189 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 7190 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 7191 | * |
| 7192 | * +-------------------------+ |
| 7193 | * |a | |
| 7194 | * |b | |
| 7195 | * |c | |
| 7196 | * | | |
| 7197 | * +-------------------------+ |
| 7198 | * |
| 7199 | * then nfa_regexec_multi() returns 3. while the original |
| 7200 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 7201 | * |
| 7202 | * FIXME if this behavior is not compatible. |
| 7203 | */ |
| 7204 | static long |
Bram Moolenaar | 0554097 | 2016-01-30 20:31:25 +0100 | [diff] [blame] | 7205 | nfa_regexec_multi( |
| 7206 | regmmatch_T *rmp, |
| 7207 | win_T *win, /* window in which to search or NULL */ |
| 7208 | buf_T *buf, /* buffer in which to search */ |
| 7209 | linenr_T lnum, /* nr of line to start looking for match */ |
| 7210 | colnr_T col, /* column to start looking for match */ |
| 7211 | proftime_T *tm) /* timeout limit or NULL */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7212 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7213 | reg_match = NULL; |
| 7214 | reg_mmatch = rmp; |
| 7215 | reg_buf = buf; |
| 7216 | reg_win = win; |
| 7217 | reg_firstlnum = lnum; |
| 7218 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 7219 | reg_line_lbr = FALSE; |
| 7220 | ireg_ic = rmp->rmm_ic; |
| 7221 | #ifdef FEAT_MBYTE |
| 7222 | ireg_icombine = FALSE; |
| 7223 | #endif |
| 7224 | ireg_maxcol = rmp->rmm_maxcol; |
| 7225 | |
Bram Moolenaar | 70781ee | 2015-02-03 16:49:24 +0100 | [diff] [blame] | 7226 | return nfa_regexec_both(NULL, col, tm); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7227 | } |
| 7228 | |
| 7229 | #ifdef DEBUG |
| 7230 | # undef ENABLE_LOG |
| 7231 | #endif |