Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * NFA regular expression implementation. |
| 4 | * |
| 5 | * This file is included in "regexp.c". |
| 6 | */ |
| 7 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 8 | /* |
| 9 | * Logging of NFA engine. |
| 10 | * |
| 11 | * The NFA engine can write four log files: |
| 12 | * - Error log: Contains NFA engine's fatal errors. |
| 13 | * - Dump log: Contains compiled NFA state machine's information. |
| 14 | * - Run log: Contains information of matching procedure. |
| 15 | * - Debug log: Contains detailed information of matching procedure. Can be |
| 16 | * disabled by undefining NFA_REGEXP_DEBUG_LOG. |
| 17 | * The first one can also be used without debug mode. |
| 18 | * The last three are enabled when compiled as debug mode and individually |
| 19 | * disabled by commenting them out. |
| 20 | * The log files can get quite big! |
| 21 | * Do disable all of this when compiling Vim for debugging, undefine DEBUG in |
| 22 | * regexp.c |
| 23 | */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 24 | #ifdef DEBUG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 25 | # define NFA_REGEXP_ERROR_LOG "nfa_regexp_error.log" |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 26 | # define ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 27 | # define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log" |
| 28 | # define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log" |
| 29 | # define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log" |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 30 | #endif |
| 31 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 32 | /* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */ |
| 33 | #define NFA_ADD_NL 31 |
| 34 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 35 | enum |
| 36 | { |
| 37 | NFA_SPLIT = -1024, |
| 38 | NFA_MATCH, |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 39 | NFA_EMPTY, /* matches 0-length */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 40 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 41 | NFA_START_COLL, /* [abc] start */ |
| 42 | NFA_END_COLL, /* [abc] end */ |
| 43 | NFA_START_NEG_COLL, /* [^abc] start */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 44 | NFA_END_NEG_COLL, /* [^abc] end (postfix only) */ |
| 45 | NFA_RANGE, /* range of the two previous items |
| 46 | * (postfix only) */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 47 | NFA_RANGE_MIN, /* low end of a range */ |
| 48 | NFA_RANGE_MAX, /* high end of a range */ |
| 49 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 50 | NFA_CONCAT, /* concatenate two previous items (postfix |
| 51 | * only) */ |
| 52 | NFA_OR, /* \| (postfix only) */ |
| 53 | NFA_STAR, /* greedy * (posfix only) */ |
| 54 | NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */ |
| 55 | NFA_QUEST, /* greedy \? (postfix only) */ |
| 56 | NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 57 | |
| 58 | NFA_BOL, /* ^ Begin line */ |
| 59 | NFA_EOL, /* $ End line */ |
| 60 | NFA_BOW, /* \< Begin word */ |
| 61 | NFA_EOW, /* \> End word */ |
| 62 | NFA_BOF, /* \%^ Begin file */ |
| 63 | NFA_EOF, /* \%$ End file */ |
| 64 | NFA_NEWL, |
| 65 | NFA_ZSTART, /* Used for \zs */ |
| 66 | NFA_ZEND, /* Used for \ze */ |
| 67 | NFA_NOPEN, /* Start of subexpression marked with \%( */ |
| 68 | NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ |
| 69 | NFA_START_INVISIBLE, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 70 | NFA_START_INVISIBLE_FIRST, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 71 | NFA_START_INVISIBLE_NEG, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 72 | NFA_START_INVISIBLE_NEG_FIRST, |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 73 | NFA_START_INVISIBLE_BEFORE, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 74 | NFA_START_INVISIBLE_BEFORE_FIRST, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 75 | NFA_START_INVISIBLE_BEFORE_NEG, |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 76 | NFA_START_INVISIBLE_BEFORE_NEG_FIRST, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 77 | NFA_START_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 78 | NFA_END_INVISIBLE, |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 79 | NFA_END_INVISIBLE_NEG, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 80 | NFA_END_PATTERN, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 81 | NFA_COMPOSING, /* Next nodes in NFA are part of the |
| 82 | composing multibyte char */ |
| 83 | NFA_END_COMPOSING, /* End of a composing char in the NFA */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 84 | NFA_OPT_CHARS, /* \%[abc] */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 85 | |
| 86 | /* The following are used only in the postfix form, not in the NFA */ |
| 87 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 88 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 89 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 90 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 91 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 92 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 93 | NFA_BACKREF1, /* \1 */ |
| 94 | NFA_BACKREF2, /* \2 */ |
| 95 | NFA_BACKREF3, /* \3 */ |
| 96 | NFA_BACKREF4, /* \4 */ |
| 97 | NFA_BACKREF5, /* \5 */ |
| 98 | NFA_BACKREF6, /* \6 */ |
| 99 | NFA_BACKREF7, /* \7 */ |
| 100 | NFA_BACKREF8, /* \8 */ |
| 101 | NFA_BACKREF9, /* \9 */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 102 | #ifdef FEAT_SYN_HL |
| 103 | NFA_ZREF1, /* \z1 */ |
| 104 | NFA_ZREF2, /* \z2 */ |
| 105 | NFA_ZREF3, /* \z3 */ |
| 106 | NFA_ZREF4, /* \z4 */ |
| 107 | NFA_ZREF5, /* \z5 */ |
| 108 | NFA_ZREF6, /* \z6 */ |
| 109 | NFA_ZREF7, /* \z7 */ |
| 110 | NFA_ZREF8, /* \z8 */ |
| 111 | NFA_ZREF9, /* \z9 */ |
| 112 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 113 | NFA_SKIP, /* Skip characters */ |
| 114 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 115 | NFA_MOPEN, |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 116 | NFA_MOPEN1, |
| 117 | NFA_MOPEN2, |
| 118 | NFA_MOPEN3, |
| 119 | NFA_MOPEN4, |
| 120 | NFA_MOPEN5, |
| 121 | NFA_MOPEN6, |
| 122 | NFA_MOPEN7, |
| 123 | NFA_MOPEN8, |
| 124 | NFA_MOPEN9, |
| 125 | |
| 126 | NFA_MCLOSE, |
| 127 | NFA_MCLOSE1, |
| 128 | NFA_MCLOSE2, |
| 129 | NFA_MCLOSE3, |
| 130 | NFA_MCLOSE4, |
| 131 | NFA_MCLOSE5, |
| 132 | NFA_MCLOSE6, |
| 133 | NFA_MCLOSE7, |
| 134 | NFA_MCLOSE8, |
| 135 | NFA_MCLOSE9, |
| 136 | |
| 137 | #ifdef FEAT_SYN_HL |
| 138 | NFA_ZOPEN, |
| 139 | NFA_ZOPEN1, |
| 140 | NFA_ZOPEN2, |
| 141 | NFA_ZOPEN3, |
| 142 | NFA_ZOPEN4, |
| 143 | NFA_ZOPEN5, |
| 144 | NFA_ZOPEN6, |
| 145 | NFA_ZOPEN7, |
| 146 | NFA_ZOPEN8, |
| 147 | NFA_ZOPEN9, |
| 148 | |
| 149 | NFA_ZCLOSE, |
| 150 | NFA_ZCLOSE1, |
| 151 | NFA_ZCLOSE2, |
| 152 | NFA_ZCLOSE3, |
| 153 | NFA_ZCLOSE4, |
| 154 | NFA_ZCLOSE5, |
| 155 | NFA_ZCLOSE6, |
| 156 | NFA_ZCLOSE7, |
| 157 | NFA_ZCLOSE8, |
| 158 | NFA_ZCLOSE9, |
| 159 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 160 | |
| 161 | /* NFA_FIRST_NL */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 162 | NFA_ANY, /* Match any one character. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 163 | NFA_IDENT, /* Match identifier char */ |
| 164 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 165 | NFA_KWORD, /* Match keyword char */ |
| 166 | NFA_SKWORD, /* Match word char but no digit */ |
| 167 | NFA_FNAME, /* Match file name char */ |
| 168 | NFA_SFNAME, /* Match file name char but no digit */ |
| 169 | NFA_PRINT, /* Match printable char */ |
| 170 | NFA_SPRINT, /* Match printable char but no digit */ |
| 171 | NFA_WHITE, /* Match whitespace char */ |
| 172 | NFA_NWHITE, /* Match non-whitespace char */ |
| 173 | NFA_DIGIT, /* Match digit char */ |
| 174 | NFA_NDIGIT, /* Match non-digit char */ |
| 175 | NFA_HEX, /* Match hex char */ |
| 176 | NFA_NHEX, /* Match non-hex char */ |
| 177 | NFA_OCTAL, /* Match octal char */ |
| 178 | NFA_NOCTAL, /* Match non-octal char */ |
| 179 | NFA_WORD, /* Match word char */ |
| 180 | NFA_NWORD, /* Match non-word char */ |
| 181 | NFA_HEAD, /* Match head char */ |
| 182 | NFA_NHEAD, /* Match non-head char */ |
| 183 | NFA_ALPHA, /* Match alpha char */ |
| 184 | NFA_NALPHA, /* Match non-alpha char */ |
| 185 | NFA_LOWER, /* Match lowercase char */ |
| 186 | NFA_NLOWER, /* Match non-lowercase char */ |
| 187 | NFA_UPPER, /* Match uppercase char */ |
| 188 | NFA_NUPPER, /* Match non-uppercase char */ |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 189 | NFA_LOWER_IC, /* Match [a-z] */ |
| 190 | NFA_NLOWER_IC, /* Match [^a-z] */ |
| 191 | NFA_UPPER_IC, /* Match [A-Z] */ |
| 192 | NFA_NUPPER_IC, /* Match [^A-Z] */ |
| 193 | |
| 194 | NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL, |
| 195 | NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL, |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 196 | |
| 197 | NFA_CURSOR, /* Match cursor pos */ |
| 198 | NFA_LNUM, /* Match line number */ |
| 199 | NFA_LNUM_GT, /* Match > line number */ |
| 200 | NFA_LNUM_LT, /* Match < line number */ |
| 201 | NFA_COL, /* Match cursor column */ |
| 202 | NFA_COL_GT, /* Match > cursor column */ |
| 203 | NFA_COL_LT, /* Match < cursor column */ |
| 204 | NFA_VCOL, /* Match cursor virtual column */ |
| 205 | NFA_VCOL_GT, /* Match > cursor virtual column */ |
| 206 | NFA_VCOL_LT, /* Match < cursor virtual column */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 207 | NFA_MARK, /* Match mark */ |
| 208 | NFA_MARK_GT, /* Match > mark */ |
| 209 | NFA_MARK_LT, /* Match < mark */ |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 210 | NFA_VISUAL, /* Match Visual area */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 211 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 212 | /* Character classes [:alnum:] etc */ |
| 213 | NFA_CLASS_ALNUM, |
| 214 | NFA_CLASS_ALPHA, |
| 215 | NFA_CLASS_BLANK, |
| 216 | NFA_CLASS_CNTRL, |
| 217 | NFA_CLASS_DIGIT, |
| 218 | NFA_CLASS_GRAPH, |
| 219 | NFA_CLASS_LOWER, |
| 220 | NFA_CLASS_PRINT, |
| 221 | NFA_CLASS_PUNCT, |
| 222 | NFA_CLASS_SPACE, |
| 223 | NFA_CLASS_UPPER, |
| 224 | NFA_CLASS_XDIGIT, |
| 225 | NFA_CLASS_TAB, |
| 226 | NFA_CLASS_RETURN, |
| 227 | NFA_CLASS_BACKSPACE, |
| 228 | NFA_CLASS_ESCAPE |
| 229 | }; |
| 230 | |
| 231 | /* Keep in sync with classchars. */ |
| 232 | static int nfa_classcodes[] = { |
| 233 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 234 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 235 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 236 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 237 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 238 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 239 | NFA_UPPER, NFA_NUPPER |
| 240 | }; |
| 241 | |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 242 | 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] | 243 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 244 | 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] | 245 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 246 | /* NFA regexp \ze operator encountered. */ |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 247 | static int nfa_has_zend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 248 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 249 | /* NFA regexp \1 .. \9 encountered. */ |
| 250 | static int nfa_has_backref; |
| 251 | |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 252 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 253 | /* NFA regexp has \z( ), set zsubexpr. */ |
| 254 | static int nfa_has_zsubexpr; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 255 | #endif |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 256 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 257 | /* Number of sub expressions actually being used during execution. 1 if only |
| 258 | * the whole match (subexpr 0) is used. */ |
| 259 | static int nfa_nsubexpr; |
| 260 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 261 | static int *post_start; /* holds the postfix form of r.e. */ |
| 262 | static int *post_end; |
| 263 | static int *post_ptr; |
| 264 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 265 | static int nstate; /* Number of states in the NFA. Also used when |
| 266 | * executing. */ |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 267 | static int istate; /* Index in the state vector, used in alloc_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 268 | |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 269 | /* If not NULL match must end at this position */ |
| 270 | static save_se_T *nfa_endp = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 271 | |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 272 | /* listid is global, so that it increases on recursive calls to |
| 273 | * nfa_regmatch(), which means we don't have to clear the lastlist field of |
| 274 | * all the states. */ |
| 275 | static int nfa_listid; |
| 276 | static int nfa_alt_listid; |
| 277 | |
| 278 | /* 0 for first call to nfa_regmatch(), 1 for recursive call. */ |
| 279 | static int nfa_ll_index = 0; |
| 280 | |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 281 | static int nfa_regcomp_start __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 282 | static int nfa_get_reganch __ARGS((nfa_state_T *start, int depth)); |
| 283 | static int nfa_get_regstart __ARGS((nfa_state_T *start, int depth)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 284 | static char_u *nfa_get_match_text __ARGS((nfa_state_T *start)); |
Bram Moolenaar | 01b626c | 2013-06-16 22:49:14 +0200 | [diff] [blame] | 285 | static int realloc_post_list __ARGS((void)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 286 | static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 287 | static int nfa_emit_equi_class __ARGS((int c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 288 | static int nfa_regatom __ARGS((void)); |
| 289 | static int nfa_regpiece __ARGS((void)); |
| 290 | static int nfa_regconcat __ARGS((void)); |
| 291 | static int nfa_regbranch __ARGS((void)); |
| 292 | static int nfa_reg __ARGS((int paren)); |
| 293 | #ifdef DEBUG |
| 294 | static void nfa_set_code __ARGS((int c)); |
| 295 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 296 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 297 | static void nfa_print_state2 __ARGS((FILE *debugf, nfa_state_T *state, garray_T *indent)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 298 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 299 | #endif |
| 300 | static int *re2post __ARGS((void)); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 301 | static nfa_state_T *alloc_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 302 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
| 303 | static int nfa_max_width __ARGS((nfa_state_T *startstate, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 304 | static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size)); |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 305 | static void nfa_postprocess __ARGS((nfa_regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 306 | static int check_char_class __ARGS((int class, int c)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 307 | static void nfa_save_listids __ARGS((nfa_regprog_T *prog, int *list)); |
| 308 | static void nfa_restore_listids __ARGS((nfa_regprog_T *prog, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 309 | static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 310 | static long nfa_regtry __ARGS((nfa_regprog_T *prog, colnr_T col)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 311 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 312 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 313 | static void nfa_regfree __ARGS((regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 314 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 315 | static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 316 | static int match_follows __ARGS((nfa_state_T *startstate, int depth)); |
| 317 | static int failure_chance __ARGS((nfa_state_T *state, int depth)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 318 | |
| 319 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 320 | #define EMIT(c) do { \ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 321 | if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 322 | return FAIL; \ |
| 323 | *post_ptr++ = c; \ |
| 324 | } while (0) |
| 325 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 326 | /* |
| 327 | * Initialize internal variables before NFA compilation. |
| 328 | * Return OK on success, FAIL otherwise. |
| 329 | */ |
| 330 | static int |
| 331 | nfa_regcomp_start(expr, re_flags) |
| 332 | char_u *expr; |
| 333 | int re_flags; /* see vim_regcomp() */ |
| 334 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 335 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 336 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 337 | |
| 338 | nstate = 0; |
| 339 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 340 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 341 | nstate_max = (int)(STRLEN(expr) + 1) * 25; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 342 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 343 | /* 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] | 344 | * When it is still not enough realloc_post_list() will be used. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 345 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 346 | |
| 347 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 348 | postfix_size = sizeof(int) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 349 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 350 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 351 | if (post_start == NULL) |
| 352 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 353 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 354 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 355 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 356 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 357 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 358 | /* shared with BT engine */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 359 | regcomp_start(expr, re_flags); |
| 360 | |
| 361 | return OK; |
| 362 | } |
| 363 | |
| 364 | /* |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 365 | * Figure out if the NFA state list starts with an anchor, must match at start |
| 366 | * of the line. |
| 367 | */ |
| 368 | static int |
| 369 | nfa_get_reganch(start, depth) |
| 370 | nfa_state_T *start; |
| 371 | int depth; |
| 372 | { |
| 373 | nfa_state_T *p = start; |
| 374 | |
| 375 | if (depth > 4) |
| 376 | return 0; |
| 377 | |
| 378 | while (p != NULL) |
| 379 | { |
| 380 | switch (p->c) |
| 381 | { |
| 382 | case NFA_BOL: |
| 383 | case NFA_BOF: |
| 384 | return 1; /* yes! */ |
| 385 | |
| 386 | case NFA_ZSTART: |
| 387 | case NFA_ZEND: |
| 388 | case NFA_CURSOR: |
| 389 | case NFA_VISUAL: |
| 390 | |
| 391 | case NFA_MOPEN: |
| 392 | case NFA_MOPEN1: |
| 393 | case NFA_MOPEN2: |
| 394 | case NFA_MOPEN3: |
| 395 | case NFA_MOPEN4: |
| 396 | case NFA_MOPEN5: |
| 397 | case NFA_MOPEN6: |
| 398 | case NFA_MOPEN7: |
| 399 | case NFA_MOPEN8: |
| 400 | case NFA_MOPEN9: |
| 401 | case NFA_NOPEN: |
| 402 | #ifdef FEAT_SYN_HL |
| 403 | case NFA_ZOPEN: |
| 404 | case NFA_ZOPEN1: |
| 405 | case NFA_ZOPEN2: |
| 406 | case NFA_ZOPEN3: |
| 407 | case NFA_ZOPEN4: |
| 408 | case NFA_ZOPEN5: |
| 409 | case NFA_ZOPEN6: |
| 410 | case NFA_ZOPEN7: |
| 411 | case NFA_ZOPEN8: |
| 412 | case NFA_ZOPEN9: |
| 413 | #endif |
| 414 | p = p->out; |
| 415 | break; |
| 416 | |
| 417 | case NFA_SPLIT: |
| 418 | return nfa_get_reganch(p->out, depth + 1) |
| 419 | && nfa_get_reganch(p->out1, depth + 1); |
| 420 | |
| 421 | default: |
| 422 | return 0; /* noooo */ |
| 423 | } |
| 424 | } |
| 425 | return 0; |
| 426 | } |
| 427 | |
| 428 | /* |
| 429 | * Figure out if the NFA state list starts with a character which must match |
| 430 | * at start of the match. |
| 431 | */ |
| 432 | static int |
| 433 | nfa_get_regstart(start, depth) |
| 434 | nfa_state_T *start; |
| 435 | int depth; |
| 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 * |
| 519 | nfa_get_match_text(start) |
| 520 | nfa_state_T *start; |
| 521 | { |
| 522 | nfa_state_T *p = start; |
| 523 | int len = 0; |
| 524 | char_u *ret; |
| 525 | char_u *s; |
| 526 | |
| 527 | if (p->c != NFA_MOPEN) |
| 528 | return NULL; /* just in case */ |
| 529 | p = p->out; |
| 530 | while (p->c > 0) |
| 531 | { |
| 532 | len += MB_CHAR2LEN(p->c); |
| 533 | p = p->out; |
| 534 | } |
| 535 | if (p->c != NFA_MCLOSE || p->out->c != NFA_MATCH) |
| 536 | return NULL; |
| 537 | |
| 538 | ret = alloc(len); |
| 539 | if (ret != NULL) |
| 540 | { |
| 541 | len = 0; |
| 542 | p = start->out->out; /* skip first char, it goes into regstart */ |
| 543 | s = ret; |
| 544 | while (p->c > 0) |
| 545 | { |
| 546 | #ifdef FEAT_MBYTE |
| 547 | if (has_mbyte) |
| 548 | s += (*mb_char2bytes)(p->c, s); |
| 549 | else |
| 550 | #endif |
| 551 | *s++ = p->c; |
| 552 | p = p->out; |
| 553 | } |
| 554 | *s = NUL; |
| 555 | } |
| 556 | return ret; |
| 557 | } |
| 558 | |
| 559 | /* |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 560 | * Allocate more space for post_start. Called when |
| 561 | * running above the estimated number of states. |
| 562 | */ |
| 563 | static int |
| 564 | realloc_post_list() |
| 565 | { |
Bram Moolenaar | 99dc19d | 2013-05-31 20:49:31 +0200 | [diff] [blame] | 566 | int nstate_max = (int)(post_end - post_start); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 567 | int new_max = nstate_max + 1000; |
| 568 | int *new_start; |
| 569 | int *old_start; |
| 570 | |
| 571 | new_start = (int *)lalloc(new_max * sizeof(int), TRUE); |
| 572 | if (new_start == NULL) |
| 573 | return FAIL; |
| 574 | mch_memmove(new_start, post_start, nstate_max * sizeof(int)); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 575 | old_start = post_start; |
| 576 | post_start = new_start; |
| 577 | post_ptr = new_start + (post_ptr - old_start); |
| 578 | post_end = post_start + new_max; |
| 579 | vim_free(old_start); |
| 580 | return OK; |
| 581 | } |
| 582 | |
| 583 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 584 | * Search between "start" and "end" and try to recognize a |
| 585 | * character class in expanded form. For example [0-9]. |
| 586 | * On success, return the id the character class to be emitted. |
| 587 | * On failure, return 0 (=FAIL) |
| 588 | * Start points to the first char of the range, while end should point |
| 589 | * to the closing brace. |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 590 | * Keep in mind that 'ignorecase' applies at execution time, thus [a-z] may |
| 591 | * need to be interpreted as [a-zA-Z]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 592 | */ |
| 593 | static int |
| 594 | nfa_recognize_char_class(start, end, extra_newl) |
| 595 | char_u *start; |
| 596 | char_u *end; |
| 597 | int extra_newl; |
| 598 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 599 | # define CLASS_not 0x80 |
| 600 | # define CLASS_af 0x40 |
| 601 | # define CLASS_AF 0x20 |
| 602 | # define CLASS_az 0x10 |
| 603 | # define CLASS_AZ 0x08 |
| 604 | # define CLASS_o7 0x04 |
| 605 | # define CLASS_o9 0x02 |
| 606 | # define CLASS_underscore 0x01 |
| 607 | |
| 608 | int newl = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 609 | char_u *p; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 610 | int config = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 611 | |
| 612 | if (extra_newl == TRUE) |
| 613 | newl = TRUE; |
| 614 | |
| 615 | if (*end != ']') |
| 616 | return FAIL; |
| 617 | p = start; |
| 618 | if (*p == '^') |
| 619 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 620 | config |= CLASS_not; |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 621 | p++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | while (p < end) |
| 625 | { |
| 626 | if (p + 2 < end && *(p + 1) == '-') |
| 627 | { |
| 628 | switch (*p) |
| 629 | { |
| 630 | case '0': |
| 631 | if (*(p + 2) == '9') |
| 632 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 633 | config |= CLASS_o9; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 634 | break; |
| 635 | } |
| 636 | else |
| 637 | if (*(p + 2) == '7') |
| 638 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 639 | config |= CLASS_o7; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 640 | break; |
| 641 | } |
| 642 | case 'a': |
| 643 | if (*(p + 2) == 'z') |
| 644 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 645 | config |= CLASS_az; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 646 | break; |
| 647 | } |
| 648 | else |
| 649 | if (*(p + 2) == 'f') |
| 650 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 651 | config |= CLASS_af; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | case 'A': |
| 655 | if (*(p + 2) == 'Z') |
| 656 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 657 | config |= CLASS_AZ; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 658 | break; |
| 659 | } |
| 660 | else |
| 661 | if (*(p + 2) == 'F') |
| 662 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 663 | config |= CLASS_AF; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 664 | break; |
| 665 | } |
| 666 | /* FALLTHROUGH */ |
| 667 | default: |
| 668 | return FAIL; |
| 669 | } |
| 670 | p += 3; |
| 671 | } |
| 672 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 673 | { |
| 674 | newl = TRUE; |
| 675 | p += 2; |
| 676 | } |
| 677 | else if (*p == '_') |
| 678 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 679 | config |= CLASS_underscore; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 680 | p ++; |
| 681 | } |
| 682 | else if (*p == '\n') |
| 683 | { |
| 684 | newl = TRUE; |
| 685 | p ++; |
| 686 | } |
| 687 | else |
| 688 | return FAIL; |
| 689 | } /* while (p < end) */ |
| 690 | |
| 691 | if (p != end) |
| 692 | return FAIL; |
| 693 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 694 | if (newl == TRUE) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 695 | extra_newl = NFA_ADD_NL; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 696 | |
| 697 | switch (config) |
| 698 | { |
| 699 | case CLASS_o9: |
| 700 | return extra_newl + NFA_DIGIT; |
| 701 | case CLASS_not | CLASS_o9: |
| 702 | return extra_newl + NFA_NDIGIT; |
| 703 | case CLASS_af | CLASS_AF | CLASS_o9: |
| 704 | return extra_newl + NFA_HEX; |
| 705 | case CLASS_not | CLASS_af | CLASS_AF | CLASS_o9: |
| 706 | return extra_newl + NFA_NHEX; |
| 707 | case CLASS_o7: |
| 708 | return extra_newl + NFA_OCTAL; |
| 709 | case CLASS_not | CLASS_o7: |
| 710 | return extra_newl + NFA_NOCTAL; |
| 711 | case CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 712 | return extra_newl + NFA_WORD; |
| 713 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_o9 | CLASS_underscore: |
| 714 | return extra_newl + NFA_NWORD; |
| 715 | case CLASS_az | CLASS_AZ | CLASS_underscore: |
| 716 | return extra_newl + NFA_HEAD; |
| 717 | case CLASS_not | CLASS_az | CLASS_AZ | CLASS_underscore: |
| 718 | return extra_newl + NFA_NHEAD; |
| 719 | case CLASS_az | CLASS_AZ: |
| 720 | return extra_newl + NFA_ALPHA; |
| 721 | case CLASS_not | CLASS_az | CLASS_AZ: |
| 722 | return extra_newl + NFA_NALPHA; |
| 723 | case CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 724 | return extra_newl + NFA_LOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 725 | case CLASS_not | CLASS_az: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 726 | return extra_newl + NFA_NLOWER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 727 | case CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 728 | return extra_newl + NFA_UPPER_IC; |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 729 | case CLASS_not | CLASS_AZ: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 730 | return extra_newl + NFA_NUPPER_IC; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 731 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 732 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* |
| 736 | * Produce the bytes for equivalence class "c". |
| 737 | * Currently only handles latin1, latin9 and utf-8. |
| 738 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 739 | * equivalent to 'a OR b OR c' |
| 740 | * |
| 741 | * NOTE! When changing this function, also update reg_equi_class() |
| 742 | */ |
| 743 | static int |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 744 | nfa_emit_equi_class(c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 745 | int c; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 746 | { |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 747 | #define EMIT2(c) EMIT(c); EMIT(NFA_CONCAT); |
| 748 | #ifdef FEAT_MBYTE |
| 749 | # define EMITMBC(c) EMIT(c); EMIT(NFA_CONCAT); |
| 750 | #else |
| 751 | # define EMITMBC(c) |
| 752 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 753 | |
| 754 | #ifdef FEAT_MBYTE |
| 755 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 756 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 757 | #endif |
| 758 | { |
| 759 | switch (c) |
| 760 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 761 | case 'A': case 0300: case 0301: case 0302: |
| 762 | case 0303: case 0304: case 0305: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 763 | CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd) |
| 764 | CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2) |
| 765 | EMIT2('A'); EMIT2(0300); EMIT2(0301); EMIT2(0302); |
| 766 | EMIT2(0303); EMIT2(0304); EMIT2(0305); |
| 767 | EMITMBC(0x100) EMITMBC(0x102) EMITMBC(0x104) |
| 768 | EMITMBC(0x1cd) EMITMBC(0x1de) EMITMBC(0x1e0) |
| 769 | EMITMBC(0x1ea2) |
| 770 | return OK; |
| 771 | |
| 772 | case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06) |
| 773 | EMIT2('B'); EMITMBC(0x1e02) EMITMBC(0x1e06) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 774 | return OK; |
| 775 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 776 | case 'C': case 0307: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 777 | CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c) |
| 778 | EMIT2('C'); EMIT2(0307); EMITMBC(0x106) EMITMBC(0x108) |
| 779 | EMITMBC(0x10a) EMITMBC(0x10c) |
| 780 | return OK; |
| 781 | |
| 782 | case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a) |
| 783 | CASEMBC(0x1e0e) CASEMBC(0x1e10) |
| 784 | EMIT2('D'); EMITMBC(0x10e) EMITMBC(0x110) EMITMBC(0x1e0a) |
| 785 | EMITMBC(0x1e0e) EMITMBC(0x1e10) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 786 | return OK; |
| 787 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 788 | case 'E': case 0310: case 0311: case 0312: case 0313: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 789 | CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118) |
| 790 | CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc) |
| 791 | EMIT2('E'); EMIT2(0310); EMIT2(0311); EMIT2(0312); |
| 792 | EMIT2(0313); |
| 793 | EMITMBC(0x112) EMITMBC(0x114) EMITMBC(0x116) |
| 794 | EMITMBC(0x118) EMITMBC(0x11a) EMITMBC(0x1eba) |
| 795 | EMITMBC(0x1ebc) |
| 796 | return OK; |
| 797 | |
| 798 | case 'F': CASEMBC(0x1e1e) |
| 799 | EMIT2('F'); EMITMBC(0x1e1e) |
| 800 | return OK; |
| 801 | |
| 802 | case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120) |
| 803 | CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4) |
| 804 | CASEMBC(0x1e20) |
| 805 | EMIT2('G'); EMITMBC(0x11c) EMITMBC(0x11e) EMITMBC(0x120) |
| 806 | EMITMBC(0x122) EMITMBC(0x1e4) EMITMBC(0x1e6) |
| 807 | EMITMBC(0x1f4) EMITMBC(0x1e20) |
| 808 | return OK; |
| 809 | |
| 810 | case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22) |
| 811 | CASEMBC(0x1e26) CASEMBC(0x1e28) |
| 812 | EMIT2('H'); EMITMBC(0x124) EMITMBC(0x126) EMITMBC(0x1e22) |
| 813 | EMITMBC(0x1e26) EMITMBC(0x1e28) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 814 | return OK; |
| 815 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 816 | case 'I': case 0314: case 0315: case 0316: case 0317: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 817 | CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e) |
| 818 | CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8) |
| 819 | EMIT2('I'); EMIT2(0314); EMIT2(0315); EMIT2(0316); |
| 820 | EMIT2(0317); EMITMBC(0x128) EMITMBC(0x12a) |
| 821 | EMITMBC(0x12c) EMITMBC(0x12e) EMITMBC(0x130) |
| 822 | EMITMBC(0x1cf) EMITMBC(0x1ec8) |
| 823 | return OK; |
| 824 | |
| 825 | case 'J': CASEMBC(0x134) |
| 826 | EMIT2('J'); EMITMBC(0x134) |
| 827 | return OK; |
| 828 | |
| 829 | case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30) |
| 830 | CASEMBC(0x1e34) |
| 831 | EMIT2('K'); EMITMBC(0x136) EMITMBC(0x1e8) EMITMBC(0x1e30) |
| 832 | EMITMBC(0x1e34) |
| 833 | return OK; |
| 834 | |
| 835 | case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d) |
| 836 | CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a) |
| 837 | EMIT2('L'); EMITMBC(0x139) EMITMBC(0x13b) EMITMBC(0x13d) |
| 838 | EMITMBC(0x13f) EMITMBC(0x141) EMITMBC(0x1e3a) |
| 839 | return OK; |
| 840 | |
| 841 | case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40) |
| 842 | EMIT2('M'); EMITMBC(0x1e3e) EMITMBC(0x1e40) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 843 | return OK; |
| 844 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 845 | case 'N': case 0321: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 846 | CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44) |
| 847 | CASEMBC(0x1e48) |
| 848 | EMIT2('N'); EMIT2(0321); EMITMBC(0x143) EMITMBC(0x145) |
| 849 | EMITMBC(0x147) EMITMBC(0x1e44) EMITMBC(0x1e48) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 850 | return OK; |
| 851 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 852 | case 'O': case 0322: case 0323: case 0324: case 0325: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 853 | case 0326: case 0330: |
| 854 | CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0) |
| 855 | CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece) |
| 856 | EMIT2('O'); EMIT2(0322); EMIT2(0323); EMIT2(0324); |
| 857 | EMIT2(0325); EMIT2(0326); EMIT2(0330); |
| 858 | EMITMBC(0x14c) EMITMBC(0x14e) EMITMBC(0x150) |
| 859 | EMITMBC(0x1a0) EMITMBC(0x1d1) EMITMBC(0x1ea) |
| 860 | EMITMBC(0x1ec) EMITMBC(0x1ece) |
| 861 | return OK; |
| 862 | |
| 863 | case 'P': case 0x1e54: case 0x1e56: |
| 864 | EMIT2('P'); EMITMBC(0x1e54) EMITMBC(0x1e56) |
| 865 | return OK; |
| 866 | |
| 867 | case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158) |
| 868 | CASEMBC(0x1e58) CASEMBC(0x1e5e) |
| 869 | EMIT2('R'); EMITMBC(0x154) EMITMBC(0x156) EMITMBC(0x158) |
| 870 | EMITMBC(0x1e58) EMITMBC(0x1e5e) |
| 871 | return OK; |
| 872 | |
| 873 | case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e) |
| 874 | CASEMBC(0x160) CASEMBC(0x1e60) |
| 875 | EMIT2('S'); EMITMBC(0x15a) EMITMBC(0x15c) EMITMBC(0x15e) |
| 876 | EMITMBC(0x160) EMITMBC(0x1e60) |
| 877 | return OK; |
| 878 | |
| 879 | case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166) |
| 880 | CASEMBC(0x1e6a) CASEMBC(0x1e6e) |
| 881 | EMIT2('T'); EMITMBC(0x162) EMITMBC(0x164) EMITMBC(0x166) |
| 882 | EMITMBC(0x1e6a) EMITMBC(0x1e6e) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 883 | return OK; |
| 884 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 885 | case 'U': case 0331: case 0332: case 0333: case 0334: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 886 | CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e) |
| 887 | CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3) |
| 888 | CASEMBC(0x1ee6) |
| 889 | EMIT2('U'); EMIT2(0331); EMIT2(0332); EMIT2(0333); |
| 890 | EMIT2(0334); EMITMBC(0x168) EMITMBC(0x16a) |
| 891 | EMITMBC(0x16c) EMITMBC(0x16e) EMITMBC(0x170) |
| 892 | EMITMBC(0x172) EMITMBC(0x1af) EMITMBC(0x1d3) |
| 893 | EMITMBC(0x1ee6) |
| 894 | return OK; |
| 895 | |
| 896 | case 'V': CASEMBC(0x1e7c) |
| 897 | EMIT2('V'); EMITMBC(0x1e7c) |
| 898 | return OK; |
| 899 | |
| 900 | case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82) |
| 901 | CASEMBC(0x1e84) CASEMBC(0x1e86) |
| 902 | EMIT2('W'); EMITMBC(0x174) EMITMBC(0x1e80) EMITMBC(0x1e82) |
| 903 | EMITMBC(0x1e84) EMITMBC(0x1e86) |
| 904 | return OK; |
| 905 | |
| 906 | case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c) |
| 907 | EMIT2('X'); EMITMBC(0x1e8a) EMITMBC(0x1e8c) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 908 | return OK; |
| 909 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 910 | case 'Y': case 0335: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 911 | CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2) |
| 912 | CASEMBC(0x1ef6) CASEMBC(0x1ef8) |
| 913 | EMIT2('Y'); EMIT2(0335); EMITMBC(0x176) EMITMBC(0x178) |
| 914 | EMITMBC(0x1e8e) EMITMBC(0x1ef2) EMITMBC(0x1ef6) |
| 915 | EMITMBC(0x1ef8) |
| 916 | return OK; |
| 917 | |
| 918 | case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d) |
| 919 | CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94) |
| 920 | EMIT2('Z'); EMITMBC(0x179) EMITMBC(0x17b) EMITMBC(0x17d) |
| 921 | EMITMBC(0x1b5) EMITMBC(0x1e90) EMITMBC(0x1e94) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 922 | return OK; |
| 923 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 924 | case 'a': case 0340: case 0341: case 0342: |
| 925 | case 0343: case 0344: case 0345: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 926 | CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce) |
| 927 | CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3) |
| 928 | EMIT2('a'); EMIT2(0340); EMIT2(0341); EMIT2(0342); |
| 929 | EMIT2(0343); EMIT2(0344); EMIT2(0345); |
| 930 | EMITMBC(0x101) EMITMBC(0x103) EMITMBC(0x105) |
| 931 | EMITMBC(0x1ce) EMITMBC(0x1df) EMITMBC(0x1e1) |
| 932 | EMITMBC(0x1ea3) |
| 933 | return OK; |
| 934 | |
| 935 | case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07) |
| 936 | EMIT2('b'); EMITMBC(0x1e03) EMITMBC(0x1e07) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 937 | return OK; |
| 938 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 939 | case 'c': case 0347: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 940 | CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d) |
| 941 | EMIT2('c'); EMIT2(0347); EMITMBC(0x107) EMITMBC(0x109) |
| 942 | EMITMBC(0x10b) EMITMBC(0x10d) |
| 943 | return OK; |
| 944 | |
| 945 | case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b) |
| 946 | CASEMBC(0x1e11) |
| 947 | EMIT2('d'); EMITMBC(0x10f) EMITMBC(0x111) EMITMBC(0x1e0b) |
| 948 | EMITMBC(0x01e0f) EMITMBC(0x1e11) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 949 | return OK; |
| 950 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 951 | case 'e': case 0350: case 0351: case 0352: case 0353: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 952 | CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119) |
| 953 | CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd) |
| 954 | EMIT2('e'); EMIT2(0350); EMIT2(0351); EMIT2(0352); |
| 955 | EMIT2(0353); EMITMBC(0x113) EMITMBC(0x115) |
| 956 | EMITMBC(0x117) EMITMBC(0x119) EMITMBC(0x11b) |
| 957 | EMITMBC(0x1ebb) EMITMBC(0x1ebd) |
| 958 | return OK; |
| 959 | |
| 960 | case 'f': CASEMBC(0x1e1f) |
| 961 | EMIT2('f'); EMITMBC(0x1e1f) |
| 962 | return OK; |
| 963 | |
| 964 | case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121) |
| 965 | CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5) |
| 966 | CASEMBC(0x1e21) |
| 967 | EMIT2('g'); EMITMBC(0x11d) EMITMBC(0x11f) EMITMBC(0x121) |
| 968 | EMITMBC(0x123) EMITMBC(0x1e5) EMITMBC(0x1e7) |
| 969 | EMITMBC(0x1f5) EMITMBC(0x1e21) |
| 970 | return OK; |
| 971 | |
| 972 | case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23) |
| 973 | CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96) |
| 974 | EMIT2('h'); EMITMBC(0x125) EMITMBC(0x127) EMITMBC(0x1e23) |
| 975 | EMITMBC(0x1e27) EMITMBC(0x1e29) EMITMBC(0x1e96) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 976 | return OK; |
| 977 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 978 | case 'i': case 0354: case 0355: case 0356: case 0357: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 979 | CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f) |
| 980 | CASEMBC(0x1d0) CASEMBC(0x1ec9) |
| 981 | EMIT2('i'); EMIT2(0354); EMIT2(0355); EMIT2(0356); |
| 982 | EMIT2(0357); EMITMBC(0x129) EMITMBC(0x12b) |
| 983 | EMITMBC(0x12d) EMITMBC(0x12f) EMITMBC(0x1d0) |
| 984 | EMITMBC(0x1ec9) |
| 985 | return OK; |
| 986 | |
| 987 | case 'j': CASEMBC(0x135) CASEMBC(0x1f0) |
| 988 | EMIT2('j'); EMITMBC(0x135) EMITMBC(0x1f0) |
| 989 | return OK; |
| 990 | |
| 991 | case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31) |
| 992 | CASEMBC(0x1e35) |
| 993 | EMIT2('k'); EMITMBC(0x137) EMITMBC(0x1e9) EMITMBC(0x1e31) |
| 994 | EMITMBC(0x1e35) |
| 995 | return OK; |
| 996 | |
| 997 | case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e) |
| 998 | CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b) |
| 999 | EMIT2('l'); EMITMBC(0x13a) EMITMBC(0x13c) EMITMBC(0x13e) |
| 1000 | EMITMBC(0x140) EMITMBC(0x142) EMITMBC(0x1e3b) |
| 1001 | return OK; |
| 1002 | |
| 1003 | case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41) |
| 1004 | EMIT2('m'); EMITMBC(0x1e3f) EMITMBC(0x1e41) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1005 | return OK; |
| 1006 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1007 | case 'n': case 0361: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1008 | CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149) |
| 1009 | CASEMBC(0x1e45) CASEMBC(0x1e49) |
| 1010 | EMIT2('n'); EMIT2(0361); EMITMBC(0x144) EMITMBC(0x146) |
| 1011 | EMITMBC(0x148) EMITMBC(0x149) EMITMBC(0x1e45) |
| 1012 | EMITMBC(0x1e49) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1013 | return OK; |
| 1014 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1015 | case 'o': case 0362: case 0363: case 0364: case 0365: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1016 | case 0366: case 0370: |
| 1017 | CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1) |
| 1018 | CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf) |
| 1019 | EMIT2('o'); EMIT2(0362); EMIT2(0363); EMIT2(0364); |
| 1020 | EMIT2(0365); EMIT2(0366); EMIT2(0370); |
| 1021 | EMITMBC(0x14d) EMITMBC(0x14f) EMITMBC(0x151) |
| 1022 | EMITMBC(0x1a1) EMITMBC(0x1d2) EMITMBC(0x1eb) |
| 1023 | EMITMBC(0x1ed) EMITMBC(0x1ecf) |
| 1024 | return OK; |
| 1025 | |
| 1026 | case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57) |
| 1027 | EMIT2('p'); EMITMBC(0x1e55) EMITMBC(0x1e57) |
| 1028 | return OK; |
| 1029 | |
| 1030 | case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159) |
| 1031 | CASEMBC(0x1e59) CASEMBC(0x1e5f) |
| 1032 | EMIT2('r'); EMITMBC(0x155) EMITMBC(0x157) EMITMBC(0x159) |
| 1033 | EMITMBC(0x1e59) EMITMBC(0x1e5f) |
| 1034 | return OK; |
| 1035 | |
| 1036 | case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f) |
| 1037 | CASEMBC(0x161) CASEMBC(0x1e61) |
| 1038 | EMIT2('s'); EMITMBC(0x15b) EMITMBC(0x15d) EMITMBC(0x15f) |
| 1039 | EMITMBC(0x161) EMITMBC(0x1e61) |
| 1040 | return OK; |
| 1041 | |
| 1042 | case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167) |
| 1043 | CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97) |
| 1044 | EMIT2('t'); EMITMBC(0x163) EMITMBC(0x165) EMITMBC(0x167) |
| 1045 | EMITMBC(0x1e6b) EMITMBC(0x1e6f) EMITMBC(0x1e97) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1046 | return OK; |
| 1047 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1048 | case 'u': case 0371: case 0372: case 0373: case 0374: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1049 | CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f) |
| 1050 | CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4) |
| 1051 | CASEMBC(0x1ee7) |
| 1052 | EMIT2('u'); EMIT2(0371); EMIT2(0372); EMIT2(0373); |
| 1053 | EMIT2(0374); EMITMBC(0x169) EMITMBC(0x16b) |
| 1054 | EMITMBC(0x16d) EMITMBC(0x16f) EMITMBC(0x171) |
| 1055 | EMITMBC(0x173) EMITMBC(0x1b0) EMITMBC(0x1d4) |
| 1056 | EMITMBC(0x1ee7) |
| 1057 | return OK; |
| 1058 | |
| 1059 | case 'v': CASEMBC(0x1e7d) |
| 1060 | EMIT2('v'); EMITMBC(0x1e7d) |
| 1061 | return OK; |
| 1062 | |
| 1063 | case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83) |
| 1064 | CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98) |
| 1065 | EMIT2('w'); EMITMBC(0x175) EMITMBC(0x1e81) EMITMBC(0x1e83) |
| 1066 | EMITMBC(0x1e85) EMITMBC(0x1e87) EMITMBC(0x1e98) |
| 1067 | return OK; |
| 1068 | |
| 1069 | case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d) |
| 1070 | EMIT2('x'); EMITMBC(0x1e8b) EMITMBC(0x1e8d) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1071 | return OK; |
| 1072 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1073 | case 'y': case 0375: case 0377: |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1074 | CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99) |
| 1075 | CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9) |
| 1076 | EMIT2('y'); EMIT2(0375); EMIT2(0377); EMITMBC(0x177) |
| 1077 | EMITMBC(0x1e8f) EMITMBC(0x1e99) EMITMBC(0x1ef3) |
| 1078 | EMITMBC(0x1ef7) EMITMBC(0x1ef9) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1079 | return OK; |
| 1080 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1081 | case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e) |
| 1082 | CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) |
| 1083 | EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e) |
| 1084 | EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95) |
| 1085 | return OK; |
| 1086 | |
| 1087 | /* default: character itself */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1091 | EMIT2(c); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1092 | return OK; |
| 1093 | #undef EMIT2 |
Bram Moolenaar | e6a2fa6 | 2013-09-19 17:00:20 +0200 | [diff] [blame] | 1094 | #undef EMITMBC |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /* |
| 1098 | * Code to parse regular expression. |
| 1099 | * |
| 1100 | * We try to reuse parsing functions in regexp.c to |
| 1101 | * minimize surprise and keep the syntax consistent. |
| 1102 | */ |
| 1103 | |
| 1104 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1105 | * Parse the lowest level. |
| 1106 | * |
| 1107 | * An atom can be one of a long list of items. Many atoms match one character |
| 1108 | * in the text. It is often an ordinary character or a character class. |
| 1109 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 1110 | * is only for syntax highlighting. |
| 1111 | * |
| 1112 | * atom ::= ordinary-atom |
| 1113 | * or \( pattern \) |
| 1114 | * or \%( pattern \) |
| 1115 | * or \z( pattern \) |
| 1116 | */ |
| 1117 | static int |
| 1118 | nfa_regatom() |
| 1119 | { |
| 1120 | int c; |
| 1121 | int charclass; |
| 1122 | int equiclass; |
| 1123 | int collclass; |
| 1124 | int got_coll_char; |
| 1125 | char_u *p; |
| 1126 | char_u *endp; |
| 1127 | #ifdef FEAT_MBYTE |
| 1128 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1129 | #endif |
| 1130 | int extra = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1131 | int emit_range; |
| 1132 | int negated; |
| 1133 | int result; |
| 1134 | int startc = -1; |
| 1135 | int endc = -1; |
| 1136 | int oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1137 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1138 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1139 | switch (c) |
| 1140 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1141 | case NUL: |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1142 | EMSG_RET_FAIL(_(e_nul_found)); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1143 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1144 | case Magic('^'): |
| 1145 | EMIT(NFA_BOL); |
| 1146 | break; |
| 1147 | |
| 1148 | case Magic('$'): |
| 1149 | EMIT(NFA_EOL); |
| 1150 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1151 | had_eol = TRUE; |
| 1152 | #endif |
| 1153 | break; |
| 1154 | |
| 1155 | case Magic('<'): |
| 1156 | EMIT(NFA_BOW); |
| 1157 | break; |
| 1158 | |
| 1159 | case Magic('>'): |
| 1160 | EMIT(NFA_EOW); |
| 1161 | break; |
| 1162 | |
| 1163 | case Magic('_'): |
| 1164 | c = no_Magic(getchr()); |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1165 | if (c == NUL) |
| 1166 | EMSG_RET_FAIL(_(e_nul_found)); |
| 1167 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1168 | if (c == '^') /* "\_^" is start-of-line */ |
| 1169 | { |
| 1170 | EMIT(NFA_BOL); |
| 1171 | break; |
| 1172 | } |
| 1173 | if (c == '$') /* "\_$" is end-of-line */ |
| 1174 | { |
| 1175 | EMIT(NFA_EOL); |
| 1176 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1177 | had_eol = TRUE; |
| 1178 | #endif |
| 1179 | break; |
| 1180 | } |
| 1181 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1182 | extra = NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1183 | |
| 1184 | /* "\_[" is collection plus newline */ |
| 1185 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1186 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1187 | |
| 1188 | /* "\_x" is character class plus newline */ |
| 1189 | /*FALLTHROUGH*/ |
| 1190 | |
| 1191 | /* |
| 1192 | * Character classes. |
| 1193 | */ |
| 1194 | case Magic('.'): |
| 1195 | case Magic('i'): |
| 1196 | case Magic('I'): |
| 1197 | case Magic('k'): |
| 1198 | case Magic('K'): |
| 1199 | case Magic('f'): |
| 1200 | case Magic('F'): |
| 1201 | case Magic('p'): |
| 1202 | case Magic('P'): |
| 1203 | case Magic('s'): |
| 1204 | case Magic('S'): |
| 1205 | case Magic('d'): |
| 1206 | case Magic('D'): |
| 1207 | case Magic('x'): |
| 1208 | case Magic('X'): |
| 1209 | case Magic('o'): |
| 1210 | case Magic('O'): |
| 1211 | case Magic('w'): |
| 1212 | case Magic('W'): |
| 1213 | case Magic('h'): |
| 1214 | case Magic('H'): |
| 1215 | case Magic('a'): |
| 1216 | case Magic('A'): |
| 1217 | case Magic('l'): |
| 1218 | case Magic('L'): |
| 1219 | case Magic('u'): |
| 1220 | case Magic('U'): |
| 1221 | p = vim_strchr(classchars, no_Magic(c)); |
| 1222 | if (p == NULL) |
| 1223 | { |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 1224 | if (extra == NFA_ADD_NL) |
| 1225 | { |
| 1226 | EMSGN(_(e_ill_char_class), c); |
| 1227 | rc_did_emsg = TRUE; |
| 1228 | return FAIL; |
| 1229 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1230 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 1231 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1232 | } |
| 1233 | #ifdef FEAT_MBYTE |
| 1234 | /* When '.' is followed by a composing char ignore the dot, so that |
| 1235 | * the composing char is matched here. */ |
| 1236 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 1237 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1238 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1239 | c = getchr(); |
| 1240 | goto nfa_do_multibyte; |
| 1241 | } |
| 1242 | #endif |
| 1243 | EMIT(nfa_classcodes[p - classchars]); |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1244 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1245 | { |
| 1246 | EMIT(NFA_NEWL); |
| 1247 | EMIT(NFA_OR); |
| 1248 | regflags |= RF_HASNL; |
| 1249 | } |
| 1250 | break; |
| 1251 | |
| 1252 | case Magic('n'): |
| 1253 | if (reg_string) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1254 | /* In a string "\n" matches a newline character. */ |
| 1255 | EMIT(NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1256 | else |
| 1257 | { |
| 1258 | /* In buffer text "\n" matches the end of a line. */ |
| 1259 | EMIT(NFA_NEWL); |
| 1260 | regflags |= RF_HASNL; |
| 1261 | } |
| 1262 | break; |
| 1263 | |
| 1264 | case Magic('('): |
| 1265 | if (nfa_reg(REG_PAREN) == FAIL) |
| 1266 | return FAIL; /* cascaded error */ |
| 1267 | break; |
| 1268 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1269 | case Magic('|'): |
| 1270 | case Magic('&'): |
| 1271 | case Magic(')'): |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1272 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1273 | return FAIL; |
| 1274 | |
| 1275 | case Magic('='): |
| 1276 | case Magic('?'): |
| 1277 | case Magic('+'): |
| 1278 | case Magic('@'): |
| 1279 | case Magic('*'): |
| 1280 | case Magic('{'): |
| 1281 | /* these should follow an atom, not form an atom */ |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1282 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1283 | return FAIL; |
| 1284 | |
Bram Moolenaar | f18fb7a | 2013-06-02 22:08:03 +0200 | [diff] [blame] | 1285 | case Magic('~'): |
| 1286 | { |
| 1287 | char_u *lp; |
| 1288 | |
| 1289 | /* Previous substitute pattern. |
| 1290 | * Generated as "\%(pattern\)". */ |
| 1291 | if (reg_prev_sub == NULL) |
| 1292 | { |
| 1293 | EMSG(_(e_nopresub)); |
| 1294 | return FAIL; |
| 1295 | } |
| 1296 | for (lp = reg_prev_sub; *lp != NUL; mb_cptr_adv(lp)) |
| 1297 | { |
| 1298 | EMIT(PTR2CHAR(lp)); |
| 1299 | if (lp != reg_prev_sub) |
| 1300 | EMIT(NFA_CONCAT); |
| 1301 | } |
| 1302 | EMIT(NFA_NOPEN); |
| 1303 | break; |
| 1304 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1305 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 1306 | case Magic('1'): |
| 1307 | case Magic('2'): |
| 1308 | case Magic('3'): |
| 1309 | case Magic('4'): |
| 1310 | case Magic('5'): |
| 1311 | case Magic('6'): |
| 1312 | case Magic('7'): |
| 1313 | case Magic('8'): |
| 1314 | case Magic('9'): |
| 1315 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 1316 | nfa_has_backref = TRUE; |
| 1317 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1318 | |
| 1319 | case Magic('z'): |
| 1320 | c = no_Magic(getchr()); |
| 1321 | switch (c) |
| 1322 | { |
| 1323 | case 's': |
| 1324 | EMIT(NFA_ZSTART); |
| 1325 | break; |
| 1326 | case 'e': |
| 1327 | EMIT(NFA_ZEND); |
| 1328 | nfa_has_zend = TRUE; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 1329 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1330 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1331 | case '1': |
| 1332 | case '2': |
| 1333 | case '3': |
| 1334 | case '4': |
| 1335 | case '5': |
| 1336 | case '6': |
| 1337 | case '7': |
| 1338 | case '8': |
| 1339 | case '9': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1340 | /* \z1...\z9 */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1341 | if (reg_do_extmatch != REX_USE) |
| 1342 | EMSG_RET_FAIL(_(e_z1_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1343 | EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
| 1344 | /* No need to set nfa_has_backref, the sub-matches don't |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 1345 | * change when \z1 .. \z9 matches or not. */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1346 | re_has_z = REX_USE; |
| 1347 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1348 | case '(': |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1349 | /* \z( */ |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 1350 | if (reg_do_extmatch != REX_SET) |
| 1351 | EMSG_RET_FAIL(_(e_z_not_allowed)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 1352 | if (nfa_reg(REG_ZPAREN) == FAIL) |
| 1353 | return FAIL; /* cascaded error */ |
| 1354 | re_has_z = REX_SET; |
| 1355 | break; |
| 1356 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1357 | default: |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1358 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1359 | no_Magic(c)); |
| 1360 | return FAIL; |
| 1361 | } |
| 1362 | break; |
| 1363 | |
| 1364 | case Magic('%'): |
| 1365 | c = no_Magic(getchr()); |
| 1366 | switch (c) |
| 1367 | { |
| 1368 | /* () without a back reference */ |
| 1369 | case '(': |
| 1370 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 1371 | return FAIL; |
| 1372 | EMIT(NFA_NOPEN); |
| 1373 | break; |
| 1374 | |
| 1375 | case 'd': /* %d123 decimal */ |
| 1376 | case 'o': /* %o123 octal */ |
| 1377 | case 'x': /* %xab hex 2 */ |
| 1378 | case 'u': /* %uabcd hex 4 */ |
| 1379 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1380 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1381 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1382 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1383 | switch (c) |
| 1384 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1385 | case 'd': nr = getdecchrs(); break; |
| 1386 | case 'o': nr = getoctchrs(); break; |
| 1387 | case 'x': nr = gethexchrs(2); break; |
| 1388 | case 'u': nr = gethexchrs(4); break; |
| 1389 | case 'U': nr = gethexchrs(8); break; |
| 1390 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1391 | } |
| 1392 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1393 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1394 | EMSG2_RET_FAIL( |
| 1395 | _("E678: Invalid character after %s%%[dxouU]"), |
| 1396 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1397 | /* A NUL is stored in the text as NL */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1398 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 595cad2 | 2013-09-22 13:57:24 +0200 | [diff] [blame] | 1399 | EMIT(nr == 0 ? 0x0a : nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1400 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1401 | break; |
| 1402 | |
| 1403 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 1404 | * pattern -- regardless of whether or not it makes sense. */ |
| 1405 | case '^': |
| 1406 | EMIT(NFA_BOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1407 | break; |
| 1408 | |
| 1409 | case '$': |
| 1410 | EMIT(NFA_EOF); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1411 | break; |
| 1412 | |
| 1413 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1414 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1415 | break; |
| 1416 | |
| 1417 | case 'V': |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 1418 | EMIT(NFA_VISUAL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1419 | break; |
| 1420 | |
| 1421 | case '[': |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1422 | { |
| 1423 | int n; |
| 1424 | |
| 1425 | /* \%[abc] */ |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1426 | for (n = 0; (c = peekchr()) != ']'; ++n) |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1427 | { |
| 1428 | if (c == NUL) |
| 1429 | EMSG2_RET_FAIL(_(e_missing_sb), |
| 1430 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1431 | /* recursive call! */ |
| 1432 | if (nfa_regatom() == FAIL) |
| 1433 | return FAIL; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1434 | } |
Bram Moolenaar | d798625 | 2013-06-17 21:33:41 +0200 | [diff] [blame] | 1435 | getchr(); /* get the ] */ |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 1436 | if (n == 0) |
| 1437 | EMSG2_RET_FAIL(_(e_empty_sb), |
| 1438 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1439 | EMIT(NFA_OPT_CHARS); |
| 1440 | EMIT(n); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1441 | |
| 1442 | /* Emit as "\%(\%[abc]\)" to be able to handle |
| 1443 | * "\%[abc]*" which would cause the empty string to be |
| 1444 | * matched an unlimited number of times. NFA_NOPEN is |
| 1445 | * added only once at a position, while NFA_SPLIT is |
| 1446 | * added multiple times. This is more efficient than |
| 1447 | * not allowsing NFA_SPLIT multiple times, it is used |
| 1448 | * a lot. */ |
| 1449 | EMIT(NFA_NOPEN); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1450 | break; |
| 1451 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1452 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1453 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1454 | { |
Bram Moolenaar | 021e147 | 2013-05-30 19:18:31 +0200 | [diff] [blame] | 1455 | int n = 0; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1456 | int cmp = c; |
| 1457 | |
| 1458 | if (c == '<' || c == '>') |
| 1459 | c = getchr(); |
| 1460 | while (VIM_ISDIGIT(c)) |
| 1461 | { |
| 1462 | n = n * 10 + (c - '0'); |
| 1463 | c = getchr(); |
| 1464 | } |
| 1465 | if (c == 'l' || c == 'c' || c == 'v') |
| 1466 | { |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1467 | if (c == 'l') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1468 | /* \%{n}l \%{n}<l \%{n}>l */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1469 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1470 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1471 | else if (c == 'c') |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1472 | /* \%{n}c \%{n}<c \%{n}>c */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1473 | EMIT(cmp == '<' ? NFA_COL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1474 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1475 | else |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1476 | /* \%{n}v \%{n}<v \%{n}>v */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1477 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1478 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1479 | EMIT(n); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1480 | break; |
| 1481 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1482 | else if (c == '\'' && n == 0) |
| 1483 | { |
| 1484 | /* \%'m \%<'m \%>'m */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1485 | EMIT(cmp == '<' ? NFA_MARK_LT : |
| 1486 | cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 1487 | EMIT(getchr()); |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 1488 | break; |
| 1489 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1490 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1491 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 1492 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1493 | return FAIL; |
| 1494 | } |
| 1495 | break; |
| 1496 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1497 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 1498 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1499 | /* |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1500 | * [abc] uses NFA_START_COLL - NFA_END_COLL |
| 1501 | * [^abc] uses NFA_START_NEG_COLL - NFA_END_NEG_COLL |
| 1502 | * Each character is produced as a regular state, using |
| 1503 | * NFA_CONCAT to bind them together. |
| 1504 | * Besides normal characters there can be: |
| 1505 | * - character classes NFA_CLASS_* |
| 1506 | * - ranges, two characters followed by NFA_RANGE. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1507 | */ |
| 1508 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1509 | p = regparse; |
| 1510 | endp = skip_anyof(p); |
| 1511 | if (*endp == ']') |
| 1512 | { |
| 1513 | /* |
| 1514 | * Try to reverse engineer character classes. For example, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1515 | * 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] | 1516 | * and perform the necessary substitutions in the NFA. |
| 1517 | */ |
| 1518 | result = nfa_recognize_char_class(regparse, endp, |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1519 | extra == NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1520 | if (result != FAIL) |
| 1521 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1522 | if (result >= NFA_FIRST_NL && result <= NFA_LAST_NL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1523 | { |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1524 | EMIT(result - NFA_ADD_NL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1525 | EMIT(NFA_NEWL); |
| 1526 | EMIT(NFA_OR); |
| 1527 | } |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1528 | else |
| 1529 | EMIT(result); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1530 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1531 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1532 | return OK; |
| 1533 | } |
| 1534 | /* |
| 1535 | * Failed to recognize a character class. Use the simple |
| 1536 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 1537 | */ |
| 1538 | startc = endc = oldstartc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1539 | negated = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1540 | if (*regparse == '^') /* negated range */ |
| 1541 | { |
| 1542 | negated = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1543 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1544 | EMIT(NFA_START_NEG_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1545 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1546 | else |
| 1547 | EMIT(NFA_START_COLL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1548 | if (*regparse == '-') |
| 1549 | { |
| 1550 | startc = '-'; |
| 1551 | EMIT(startc); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1552 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1553 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1554 | } |
| 1555 | /* Emit the OR branches for each character in the [] */ |
| 1556 | emit_range = FALSE; |
| 1557 | while (regparse < endp) |
| 1558 | { |
| 1559 | oldstartc = startc; |
| 1560 | startc = -1; |
| 1561 | got_coll_char = FALSE; |
| 1562 | if (*regparse == '[') |
| 1563 | { |
| 1564 | /* Check for [: :], [= =], [. .] */ |
| 1565 | equiclass = collclass = 0; |
| 1566 | charclass = get_char_class(®parse); |
| 1567 | if (charclass == CLASS_NONE) |
| 1568 | { |
| 1569 | equiclass = get_equi_class(®parse); |
| 1570 | if (equiclass == 0) |
| 1571 | collclass = get_coll_element(®parse); |
| 1572 | } |
| 1573 | |
| 1574 | /* Character class like [:alpha:] */ |
| 1575 | if (charclass != CLASS_NONE) |
| 1576 | { |
| 1577 | switch (charclass) |
| 1578 | { |
| 1579 | case CLASS_ALNUM: |
| 1580 | EMIT(NFA_CLASS_ALNUM); |
| 1581 | break; |
| 1582 | case CLASS_ALPHA: |
| 1583 | EMIT(NFA_CLASS_ALPHA); |
| 1584 | break; |
| 1585 | case CLASS_BLANK: |
| 1586 | EMIT(NFA_CLASS_BLANK); |
| 1587 | break; |
| 1588 | case CLASS_CNTRL: |
| 1589 | EMIT(NFA_CLASS_CNTRL); |
| 1590 | break; |
| 1591 | case CLASS_DIGIT: |
| 1592 | EMIT(NFA_CLASS_DIGIT); |
| 1593 | break; |
| 1594 | case CLASS_GRAPH: |
| 1595 | EMIT(NFA_CLASS_GRAPH); |
| 1596 | break; |
| 1597 | case CLASS_LOWER: |
| 1598 | EMIT(NFA_CLASS_LOWER); |
| 1599 | break; |
| 1600 | case CLASS_PRINT: |
| 1601 | EMIT(NFA_CLASS_PRINT); |
| 1602 | break; |
| 1603 | case CLASS_PUNCT: |
| 1604 | EMIT(NFA_CLASS_PUNCT); |
| 1605 | break; |
| 1606 | case CLASS_SPACE: |
| 1607 | EMIT(NFA_CLASS_SPACE); |
| 1608 | break; |
| 1609 | case CLASS_UPPER: |
| 1610 | EMIT(NFA_CLASS_UPPER); |
| 1611 | break; |
| 1612 | case CLASS_XDIGIT: |
| 1613 | EMIT(NFA_CLASS_XDIGIT); |
| 1614 | break; |
| 1615 | case CLASS_TAB: |
| 1616 | EMIT(NFA_CLASS_TAB); |
| 1617 | break; |
| 1618 | case CLASS_RETURN: |
| 1619 | EMIT(NFA_CLASS_RETURN); |
| 1620 | break; |
| 1621 | case CLASS_BACKSPACE: |
| 1622 | EMIT(NFA_CLASS_BACKSPACE); |
| 1623 | break; |
| 1624 | case CLASS_ESCAPE: |
| 1625 | EMIT(NFA_CLASS_ESCAPE); |
| 1626 | break; |
| 1627 | } |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1628 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1629 | continue; |
| 1630 | } |
| 1631 | /* Try equivalence class [=a=] and the like */ |
| 1632 | if (equiclass != 0) |
| 1633 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1634 | result = nfa_emit_equi_class(equiclass); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1635 | if (result == FAIL) |
| 1636 | { |
| 1637 | /* should never happen */ |
| 1638 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1639 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1640 | continue; |
| 1641 | } |
| 1642 | /* Try collating class like [. .] */ |
| 1643 | if (collclass != 0) |
| 1644 | { |
| 1645 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1646 | /* Will emit the proper atom at the end of the |
| 1647 | * while loop. */ |
| 1648 | } |
| 1649 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1650 | /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
| 1651 | * start character. */ |
| 1652 | if (*regparse == '-' && oldstartc != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1653 | { |
| 1654 | emit_range = TRUE; |
| 1655 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1656 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1657 | continue; /* reading the end of the range */ |
| 1658 | } |
| 1659 | |
| 1660 | /* Now handle simple and escaped characters. |
| 1661 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1662 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1663 | * 'cpoptions' is not included. |
| 1664 | * Posix doesn't recognize backslash at all. |
| 1665 | */ |
| 1666 | if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1667 | && !reg_cpo_bsl |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1668 | && regparse + 1 <= endp |
| 1669 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1670 | || (!reg_cpo_lit |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1671 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1672 | != NULL) |
| 1673 | ) |
| 1674 | ) |
| 1675 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1676 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1677 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1678 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1679 | startc = reg_string ? NL : NFA_NEWL; |
| 1680 | else |
| 1681 | if (*regparse == 'd' |
| 1682 | || *regparse == 'o' |
| 1683 | || *regparse == 'x' |
| 1684 | || *regparse == 'u' |
| 1685 | || *regparse == 'U' |
| 1686 | ) |
| 1687 | { |
| 1688 | /* TODO(RE) This needs more testing */ |
| 1689 | startc = coll_get_char(); |
| 1690 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1691 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1692 | } |
| 1693 | else |
| 1694 | { |
| 1695 | /* \r,\t,\e,\b */ |
| 1696 | startc = backslash_trans(*regparse); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | /* Normal printable char */ |
| 1701 | if (startc == -1) |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1702 | startc = PTR2CHAR(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* Previous char was '-', so this char is end of range. */ |
| 1705 | if (emit_range) |
| 1706 | { |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1707 | endc = startc; |
| 1708 | startc = oldstartc; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1709 | if (startc > endc) |
| 1710 | EMSG_RET_FAIL(_(e_invrange)); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1711 | |
| 1712 | if (endc > startc + 2) |
| 1713 | { |
| 1714 | /* Emit a range instead of the sequence of |
| 1715 | * individual characters. */ |
| 1716 | if (startc == 0) |
| 1717 | /* \x00 is translated to \x0a, start at \x01. */ |
| 1718 | EMIT(1); |
| 1719 | else |
| 1720 | --post_ptr; /* remove NFA_CONCAT */ |
| 1721 | EMIT(endc); |
| 1722 | EMIT(NFA_RANGE); |
| 1723 | EMIT(NFA_CONCAT); |
| 1724 | } |
| 1725 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1726 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1727 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1728 | || (*mb_char2len)(endc) > 1)) |
| 1729 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1730 | /* Emit the characters in the range. |
| 1731 | * "startc" was already emitted, so skip it. |
| 1732 | * */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1733 | for (c = startc + 1; c <= endc; c++) |
| 1734 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1735 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1736 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1737 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1738 | } |
| 1739 | else |
| 1740 | #endif |
| 1741 | { |
| 1742 | #ifdef EBCDIC |
| 1743 | int alpha_only = FALSE; |
| 1744 | |
| 1745 | /* for alphabetical range skip the gaps |
| 1746 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1747 | if (isalpha(startc) && isalpha(endc)) |
| 1748 | alpha_only = TRUE; |
| 1749 | #endif |
| 1750 | /* Emit the range. "startc" was already emitted, so |
| 1751 | * skip it. */ |
| 1752 | for (c = startc + 1; c <= endc; c++) |
| 1753 | #ifdef EBCDIC |
| 1754 | if (!alpha_only || isalpha(startc)) |
| 1755 | #endif |
| 1756 | { |
| 1757 | EMIT(c); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1758 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1759 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1760 | } |
Bram Moolenaar | 75d7a06 | 2013-06-01 13:24:24 +0200 | [diff] [blame] | 1761 | emit_range = FALSE; |
| 1762 | startc = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1763 | } |
| 1764 | else |
| 1765 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1766 | /* This char (startc) is not part of a range. Just |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1767 | * emit it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1768 | * Normally, simply emit startc. But if we get char |
| 1769 | * code=0 from a collating char, then replace it with |
| 1770 | * 0x0a. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1771 | * This is needed to completely mimic the behaviour of |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1772 | * the backtracking engine. */ |
| 1773 | if (startc == NFA_NEWL) |
| 1774 | { |
| 1775 | /* Line break can't be matched as part of the |
| 1776 | * collection, add an OR below. But not for negated |
| 1777 | * range. */ |
| 1778 | if (!negated) |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1779 | extra = NFA_ADD_NL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1780 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1781 | else |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1782 | { |
| 1783 | if (got_coll_char == TRUE && startc == 0) |
| 1784 | EMIT(0x0a); |
| 1785 | else |
| 1786 | EMIT(startc); |
| 1787 | EMIT(NFA_CONCAT); |
| 1788 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1789 | } |
| 1790 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1791 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1792 | } /* while (p < endp) */ |
| 1793 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1794 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1795 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1796 | { |
| 1797 | EMIT('-'); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1798 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1799 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1800 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1801 | /* skip the trailing ] */ |
| 1802 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1803 | mb_ptr_adv(regparse); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1804 | |
| 1805 | /* Mark end of the collection. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1806 | if (negated == TRUE) |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 1807 | EMIT(NFA_END_NEG_COLL); |
| 1808 | else |
| 1809 | EMIT(NFA_END_COLL); |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1810 | |
| 1811 | /* \_[] also matches \n but it's not negated */ |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 1812 | if (extra == NFA_ADD_NL) |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1813 | { |
| 1814 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1815 | EMIT(NFA_OR); |
| 1816 | } |
| 1817 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1818 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1819 | } /* if exists closing ] */ |
| 1820 | |
| 1821 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1822 | EMSG_RET_FAIL(_(e_missingbracket)); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1823 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1824 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1825 | default: |
| 1826 | { |
| 1827 | #ifdef FEAT_MBYTE |
| 1828 | int plen; |
| 1829 | |
| 1830 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1831 | /* plen is length of current char with composing chars */ |
| 1832 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1833 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1834 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1835 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1836 | int i = 0; |
| 1837 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1838 | /* A base character plus composing characters, or just one |
| 1839 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1840 | * This requires creating a separate atom as if enclosing |
| 1841 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1842 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1843 | * building the postfix form, not the NFA itself; |
| 1844 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1845 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1846 | for (;;) |
| 1847 | { |
| 1848 | EMIT(c); |
| 1849 | if (i > 0) |
| 1850 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1851 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1852 | break; |
| 1853 | c = utf_ptr2char(old_regparse + i); |
| 1854 | } |
| 1855 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1856 | regparse = old_regparse + plen; |
| 1857 | } |
| 1858 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1859 | #endif |
| 1860 | { |
| 1861 | c = no_Magic(c); |
| 1862 | EMIT(c); |
| 1863 | } |
| 1864 | return OK; |
| 1865 | } |
| 1866 | } |
| 1867 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1868 | return OK; |
| 1869 | } |
| 1870 | |
| 1871 | /* |
| 1872 | * Parse something followed by possible [*+=]. |
| 1873 | * |
| 1874 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1875 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1876 | * characters: "", "a", "aa", etc. |
| 1877 | * |
| 1878 | * piece ::= atom |
| 1879 | * or atom multi |
| 1880 | */ |
| 1881 | static int |
| 1882 | nfa_regpiece() |
| 1883 | { |
| 1884 | int i; |
| 1885 | int op; |
| 1886 | int ret; |
| 1887 | long minval, maxval; |
| 1888 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1889 | parse_state_T old_state; |
| 1890 | parse_state_T new_state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1891 | int c2; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1892 | int old_post_pos; |
| 1893 | int my_post_start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1894 | int quest; |
| 1895 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1896 | /* Save the current parse state, so that we can use it if <atom>{m,n} is |
| 1897 | * next. */ |
| 1898 | save_parse_state(&old_state); |
| 1899 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1900 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 1901 | my_post_start = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1902 | |
| 1903 | ret = nfa_regatom(); |
| 1904 | if (ret == FAIL) |
| 1905 | return FAIL; /* cascaded error */ |
| 1906 | |
| 1907 | op = peekchr(); |
| 1908 | if (re_multi_type(op) == NOT_MULTI) |
| 1909 | return OK; |
| 1910 | |
| 1911 | skipchr(); |
| 1912 | switch (op) |
| 1913 | { |
| 1914 | case Magic('*'): |
| 1915 | EMIT(NFA_STAR); |
| 1916 | break; |
| 1917 | |
| 1918 | case Magic('+'): |
| 1919 | /* |
| 1920 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1921 | * first and only submatch would be "aaa". But the backtracking |
| 1922 | * engine interprets the plus as "try matching one more time", and |
| 1923 | * a* matches a second time at the end of the input, the empty |
| 1924 | * string. |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 1925 | * The submatch will be the empty string. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1926 | * |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 1927 | * In order to be consistent with the old engine, we replace |
| 1928 | * <atom>+ with <atom><atom>* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1929 | */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 1930 | restore_parse_state(&old_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1931 | curchr = -1; |
| 1932 | if (nfa_regatom() == FAIL) |
| 1933 | return FAIL; |
| 1934 | EMIT(NFA_STAR); |
| 1935 | EMIT(NFA_CONCAT); |
| 1936 | skipchr(); /* skip the \+ */ |
| 1937 | break; |
| 1938 | |
| 1939 | case Magic('@'): |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1940 | c2 = getdecchrs(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1941 | op = no_Magic(getchr()); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1942 | i = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1943 | switch(op) |
| 1944 | { |
| 1945 | case '=': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1946 | /* \@= */ |
| 1947 | i = NFA_PREV_ATOM_NO_WIDTH; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1948 | break; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1949 | case '!': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1950 | /* \@! */ |
| 1951 | i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 1952 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1953 | case '<': |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1954 | op = no_Magic(getchr()); |
| 1955 | if (op == '=') |
| 1956 | /* \@<= */ |
| 1957 | i = NFA_PREV_ATOM_JUST_BEFORE; |
| 1958 | else if (op == '!') |
| 1959 | /* \@<! */ |
| 1960 | i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
| 1961 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1962 | case '>': |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 1963 | /* \@> */ |
| 1964 | i = NFA_PREV_ATOM_LIKE_PATTERN; |
| 1965 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1966 | } |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1967 | if (i == 0) |
| 1968 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 1969 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
| 1970 | return FAIL; |
| 1971 | } |
| 1972 | EMIT(i); |
| 1973 | if (i == NFA_PREV_ATOM_JUST_BEFORE |
| 1974 | || i == NFA_PREV_ATOM_JUST_BEFORE_NEG) |
| 1975 | EMIT(c2); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1976 | break; |
| 1977 | |
| 1978 | case Magic('?'): |
| 1979 | case Magic('='): |
| 1980 | EMIT(NFA_QUEST); |
| 1981 | break; |
| 1982 | |
| 1983 | case Magic('{'): |
| 1984 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1985 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1986 | * version of '?' |
| 1987 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1988 | * parenthesis have the same id |
| 1989 | */ |
| 1990 | |
| 1991 | greedy = TRUE; |
| 1992 | c2 = peekchr(); |
| 1993 | if (c2 == '-' || c2 == Magic('-')) |
| 1994 | { |
| 1995 | skipchr(); |
| 1996 | greedy = FALSE; |
| 1997 | } |
| 1998 | if (!read_limits(&minval, &maxval)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1999 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 2000 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2001 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 2002 | * <atom>* */ |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2003 | if (minval == 0 && maxval == MAX_LIMIT) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2004 | { |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2005 | if (greedy) |
| 2006 | /* \{}, \{0,} */ |
| 2007 | EMIT(NFA_STAR); |
| 2008 | else |
| 2009 | /* \{-}, \{-0,} */ |
| 2010 | EMIT(NFA_STAR_NONGREEDY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2011 | break; |
| 2012 | } |
| 2013 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2014 | /* Special case: x{0} or x{-0} */ |
| 2015 | if (maxval == 0) |
| 2016 | { |
| 2017 | /* Ignore result of previous call to nfa_regatom() */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2018 | post_ptr = post_start + my_post_start; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2019 | /* NFA_EMPTY is 0-length and works everywhere */ |
| 2020 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2021 | return OK; |
| 2022 | } |
| 2023 | |
| 2024 | /* Ignore 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 | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2026 | /* Save parse state after the repeated atom and the \{} */ |
| 2027 | save_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2028 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2029 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 2030 | for (i = 0; i < maxval; i++) |
| 2031 | { |
| 2032 | /* Goto beginning of the repeated atom */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2033 | restore_parse_state(&old_state); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2034 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2035 | if (nfa_regatom() == FAIL) |
| 2036 | return FAIL; |
| 2037 | /* after "minval" times, atoms are optional */ |
| 2038 | if (i + 1 > minval) |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2039 | { |
| 2040 | if (maxval == MAX_LIMIT) |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2041 | { |
| 2042 | if (greedy) |
| 2043 | EMIT(NFA_STAR); |
| 2044 | else |
| 2045 | EMIT(NFA_STAR_NONGREEDY); |
| 2046 | } |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2047 | else |
| 2048 | EMIT(quest); |
| 2049 | } |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2050 | if (old_post_pos != my_post_start) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2051 | EMIT(NFA_CONCAT); |
Bram Moolenaar | 54dafde | 2013-05-31 23:18:00 +0200 | [diff] [blame] | 2052 | if (i + 1 > minval && maxval == MAX_LIMIT) |
| 2053 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | /* Go to just after the repeated atom and the \{} */ |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2057 | restore_parse_state(&new_state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2058 | curchr = -1; |
| 2059 | |
| 2060 | break; |
| 2061 | |
| 2062 | |
| 2063 | default: |
| 2064 | break; |
| 2065 | } /* end switch */ |
| 2066 | |
| 2067 | if (re_multi_type(peekchr()) != NOT_MULTI) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2068 | /* Can't have a multi follow a multi. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2069 | 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] | 2070 | |
| 2071 | return OK; |
| 2072 | } |
| 2073 | |
| 2074 | /* |
| 2075 | * Parse one or more pieces, concatenated. It matches a match for the |
| 2076 | * first piece, followed by a match for the second piece, etc. Example: |
| 2077 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 2078 | * |
| 2079 | * concat ::= piece |
| 2080 | * or piece piece |
| 2081 | * or piece piece piece |
| 2082 | * etc. |
| 2083 | */ |
| 2084 | static int |
| 2085 | nfa_regconcat() |
| 2086 | { |
| 2087 | int cont = TRUE; |
| 2088 | int first = TRUE; |
| 2089 | |
| 2090 | while (cont) |
| 2091 | { |
| 2092 | switch (peekchr()) |
| 2093 | { |
| 2094 | case NUL: |
| 2095 | case Magic('|'): |
| 2096 | case Magic('&'): |
| 2097 | case Magic(')'): |
| 2098 | cont = FALSE; |
| 2099 | break; |
| 2100 | |
| 2101 | case Magic('Z'): |
| 2102 | #ifdef FEAT_MBYTE |
| 2103 | regflags |= RF_ICOMBINE; |
| 2104 | #endif |
| 2105 | skipchr_keepstart(); |
| 2106 | break; |
| 2107 | case Magic('c'): |
| 2108 | regflags |= RF_ICASE; |
| 2109 | skipchr_keepstart(); |
| 2110 | break; |
| 2111 | case Magic('C'): |
| 2112 | regflags |= RF_NOICASE; |
| 2113 | skipchr_keepstart(); |
| 2114 | break; |
| 2115 | case Magic('v'): |
| 2116 | reg_magic = MAGIC_ALL; |
| 2117 | skipchr_keepstart(); |
| 2118 | curchr = -1; |
| 2119 | break; |
| 2120 | case Magic('m'): |
| 2121 | reg_magic = MAGIC_ON; |
| 2122 | skipchr_keepstart(); |
| 2123 | curchr = -1; |
| 2124 | break; |
| 2125 | case Magic('M'): |
| 2126 | reg_magic = MAGIC_OFF; |
| 2127 | skipchr_keepstart(); |
| 2128 | curchr = -1; |
| 2129 | break; |
| 2130 | case Magic('V'): |
| 2131 | reg_magic = MAGIC_NONE; |
| 2132 | skipchr_keepstart(); |
| 2133 | curchr = -1; |
| 2134 | break; |
| 2135 | |
| 2136 | default: |
| 2137 | if (nfa_regpiece() == FAIL) |
| 2138 | return FAIL; |
| 2139 | if (first == FALSE) |
| 2140 | EMIT(NFA_CONCAT); |
| 2141 | else |
| 2142 | first = FALSE; |
| 2143 | break; |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | return OK; |
| 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 2152 | * last concat, but only if all the preceding concats also match at the same |
| 2153 | * position. Examples: |
| 2154 | * "foobeep\&..." matches "foo" in "foobeep". |
| 2155 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 2156 | * |
| 2157 | * branch ::= concat |
| 2158 | * or concat \& concat |
| 2159 | * or concat \& concat \& concat |
| 2160 | * etc. |
| 2161 | */ |
| 2162 | static int |
| 2163 | nfa_regbranch() |
| 2164 | { |
| 2165 | int ch; |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2166 | int old_post_pos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2167 | |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2168 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2169 | |
| 2170 | /* First branch, possibly the only one */ |
| 2171 | if (nfa_regconcat() == FAIL) |
| 2172 | return FAIL; |
| 2173 | |
| 2174 | ch = peekchr(); |
| 2175 | /* Try next concats */ |
| 2176 | while (ch == Magic('&')) |
| 2177 | { |
| 2178 | skipchr(); |
| 2179 | EMIT(NFA_NOPEN); |
| 2180 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2181 | old_post_pos = (int)(post_ptr - post_start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2182 | if (nfa_regconcat() == FAIL) |
| 2183 | return FAIL; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2184 | /* if concat is empty do emit a node */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2185 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2186 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2187 | EMIT(NFA_CONCAT); |
| 2188 | ch = peekchr(); |
| 2189 | } |
| 2190 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2191 | /* if a branch is empty, emit one node for it */ |
Bram Moolenaar | 16299b5 | 2013-05-30 18:45:23 +0200 | [diff] [blame] | 2192 | if (old_post_pos == (int)(post_ptr - post_start)) |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2193 | EMIT(NFA_EMPTY); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2194 | |
| 2195 | return OK; |
| 2196 | } |
| 2197 | |
| 2198 | /* |
| 2199 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 2200 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 2201 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 2202 | * is used. |
| 2203 | * |
| 2204 | * pattern ::= branch |
| 2205 | * or branch \| branch |
| 2206 | * or branch \| branch \| branch |
| 2207 | * etc. |
| 2208 | */ |
| 2209 | static int |
| 2210 | nfa_reg(paren) |
| 2211 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 2212 | { |
| 2213 | int parno = 0; |
| 2214 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2215 | if (paren == REG_PAREN) |
| 2216 | { |
| 2217 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2218 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2219 | parno = regnpar++; |
| 2220 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2221 | #ifdef FEAT_SYN_HL |
| 2222 | else if (paren == REG_ZPAREN) |
| 2223 | { |
| 2224 | /* Make a ZOPEN node. */ |
| 2225 | if (regnzpar >= NSUBEXP) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2226 | EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2227 | parno = regnzpar++; |
| 2228 | } |
| 2229 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2230 | |
| 2231 | if (nfa_regbranch() == FAIL) |
| 2232 | return FAIL; /* cascaded error */ |
| 2233 | |
| 2234 | while (peekchr() == Magic('|')) |
| 2235 | { |
| 2236 | skipchr(); |
| 2237 | if (nfa_regbranch() == FAIL) |
| 2238 | return FAIL; /* cascaded error */ |
| 2239 | EMIT(NFA_OR); |
| 2240 | } |
| 2241 | |
| 2242 | /* Check for proper termination. */ |
| 2243 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 2244 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2245 | if (paren == REG_NPAREN) |
| 2246 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 2247 | else |
| 2248 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 2249 | } |
| 2250 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 2251 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2252 | if (peekchr() == Magic(')')) |
| 2253 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 2254 | else |
| 2255 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 2256 | } |
| 2257 | /* |
| 2258 | * Here we set the flag allowing back references to this set of |
| 2259 | * parentheses. |
| 2260 | */ |
| 2261 | if (paren == REG_PAREN) |
| 2262 | { |
| 2263 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 2264 | EMIT(NFA_MOPEN + parno); |
| 2265 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2266 | #ifdef FEAT_SYN_HL |
| 2267 | else if (paren == REG_ZPAREN) |
| 2268 | EMIT(NFA_ZOPEN + parno); |
| 2269 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2270 | |
| 2271 | return OK; |
| 2272 | } |
| 2273 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2274 | #ifdef DEBUG |
| 2275 | static char_u code[50]; |
| 2276 | |
| 2277 | static void |
| 2278 | nfa_set_code(c) |
| 2279 | int c; |
| 2280 | { |
| 2281 | int addnl = FALSE; |
| 2282 | |
| 2283 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 2284 | { |
| 2285 | addnl = TRUE; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2286 | c -= NFA_ADD_NL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | STRCPY(code, ""); |
| 2290 | switch (c) |
| 2291 | { |
| 2292 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 2293 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 2294 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 2295 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 2296 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 2297 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 2298 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2299 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 2300 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 2301 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 2302 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 2303 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 2304 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 2305 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 2306 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 2307 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2308 | #ifdef FEAT_SYN_HL |
| 2309 | case NFA_ZREF1: STRCPY(code, "NFA_ZREF1"); break; |
| 2310 | case NFA_ZREF2: STRCPY(code, "NFA_ZREF2"); break; |
| 2311 | case NFA_ZREF3: STRCPY(code, "NFA_ZREF3"); break; |
| 2312 | case NFA_ZREF4: STRCPY(code, "NFA_ZREF4"); break; |
| 2313 | case NFA_ZREF5: STRCPY(code, "NFA_ZREF5"); break; |
| 2314 | case NFA_ZREF6: STRCPY(code, "NFA_ZREF6"); break; |
| 2315 | case NFA_ZREF7: STRCPY(code, "NFA_ZREF7"); break; |
| 2316 | case NFA_ZREF8: STRCPY(code, "NFA_ZREF8"); break; |
| 2317 | case NFA_ZREF9: STRCPY(code, "NFA_ZREF9"); break; |
| 2318 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2319 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 2320 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2321 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2322 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2323 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 2324 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2325 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 2326 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE"); break; |
| 2327 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 2328 | STRCPY(code, "NFA_PREV_ATOM_JUST_BEFORE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2329 | case NFA_PREV_ATOM_LIKE_PATTERN: |
| 2330 | STRCPY(code, "NFA_PREV_ATOM_LIKE_PATTERN"); break; |
| 2331 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 2332 | case NFA_NOPEN: STRCPY(code, "NFA_NOPEN"); break; |
| 2333 | case NFA_NCLOSE: STRCPY(code, "NFA_NCLOSE"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2334 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2335 | case NFA_START_INVISIBLE_FIRST: |
| 2336 | STRCPY(code, "NFA_START_INVISIBLE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2337 | case NFA_START_INVISIBLE_NEG: |
| 2338 | STRCPY(code, "NFA_START_INVISIBLE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2339 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 2340 | STRCPY(code, "NFA_START_INVISIBLE_NEG_FIRST"); break; |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 2341 | case NFA_START_INVISIBLE_BEFORE: |
| 2342 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2343 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 2344 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_FIRST"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2345 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2346 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG"); break; |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 2347 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 2348 | STRCPY(code, "NFA_START_INVISIBLE_BEFORE_NEG_FIRST"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2349 | case NFA_START_PATTERN: STRCPY(code, "NFA_START_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2350 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2351 | case NFA_END_INVISIBLE_NEG: STRCPY(code, "NFA_END_INVISIBLE_NEG"); break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 2352 | case NFA_END_PATTERN: STRCPY(code, "NFA_END_PATTERN"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2353 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2354 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 2355 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 2356 | case NFA_OPT_CHARS: STRCPY(code, "NFA_OPT_CHARS"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2357 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2358 | case NFA_MOPEN: |
| 2359 | case NFA_MOPEN1: |
| 2360 | case NFA_MOPEN2: |
| 2361 | case NFA_MOPEN3: |
| 2362 | case NFA_MOPEN4: |
| 2363 | case NFA_MOPEN5: |
| 2364 | case NFA_MOPEN6: |
| 2365 | case NFA_MOPEN7: |
| 2366 | case NFA_MOPEN8: |
| 2367 | case NFA_MOPEN9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2368 | STRCPY(code, "NFA_MOPEN(x)"); |
| 2369 | code[10] = c - NFA_MOPEN + '0'; |
| 2370 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2371 | case NFA_MCLOSE: |
| 2372 | case NFA_MCLOSE1: |
| 2373 | case NFA_MCLOSE2: |
| 2374 | case NFA_MCLOSE3: |
| 2375 | case NFA_MCLOSE4: |
| 2376 | case NFA_MCLOSE5: |
| 2377 | case NFA_MCLOSE6: |
| 2378 | case NFA_MCLOSE7: |
| 2379 | case NFA_MCLOSE8: |
| 2380 | case NFA_MCLOSE9: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2381 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 2382 | code[11] = c - NFA_MCLOSE + '0'; |
| 2383 | break; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 2384 | #ifdef FEAT_SYN_HL |
| 2385 | case NFA_ZOPEN: |
| 2386 | case NFA_ZOPEN1: |
| 2387 | case NFA_ZOPEN2: |
| 2388 | case NFA_ZOPEN3: |
| 2389 | case NFA_ZOPEN4: |
| 2390 | case NFA_ZOPEN5: |
| 2391 | case NFA_ZOPEN6: |
| 2392 | case NFA_ZOPEN7: |
| 2393 | case NFA_ZOPEN8: |
| 2394 | case NFA_ZOPEN9: |
| 2395 | STRCPY(code, "NFA_ZOPEN(x)"); |
| 2396 | code[10] = c - NFA_ZOPEN + '0'; |
| 2397 | break; |
| 2398 | case NFA_ZCLOSE: |
| 2399 | case NFA_ZCLOSE1: |
| 2400 | case NFA_ZCLOSE2: |
| 2401 | case NFA_ZCLOSE3: |
| 2402 | case NFA_ZCLOSE4: |
| 2403 | case NFA_ZCLOSE5: |
| 2404 | case NFA_ZCLOSE6: |
| 2405 | case NFA_ZCLOSE7: |
| 2406 | case NFA_ZCLOSE8: |
| 2407 | case NFA_ZCLOSE9: |
| 2408 | STRCPY(code, "NFA_ZCLOSE(x)"); |
| 2409 | code[11] = c - NFA_ZCLOSE + '0'; |
| 2410 | break; |
| 2411 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2412 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 2413 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 2414 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 2415 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 2416 | case NFA_EOF: STRCPY(code, "NFA_EOF "); break; |
| 2417 | case NFA_BOF: STRCPY(code, "NFA_BOF "); break; |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 2418 | case NFA_LNUM: STRCPY(code, "NFA_LNUM "); break; |
| 2419 | case NFA_LNUM_GT: STRCPY(code, "NFA_LNUM_GT "); break; |
| 2420 | case NFA_LNUM_LT: STRCPY(code, "NFA_LNUM_LT "); break; |
| 2421 | case NFA_COL: STRCPY(code, "NFA_COL "); break; |
| 2422 | case NFA_COL_GT: STRCPY(code, "NFA_COL_GT "); break; |
| 2423 | case NFA_COL_LT: STRCPY(code, "NFA_COL_LT "); break; |
| 2424 | case NFA_VCOL: STRCPY(code, "NFA_VCOL "); break; |
| 2425 | case NFA_VCOL_GT: STRCPY(code, "NFA_VCOL_GT "); break; |
| 2426 | case NFA_VCOL_LT: STRCPY(code, "NFA_VCOL_LT "); break; |
| 2427 | case NFA_MARK: STRCPY(code, "NFA_MARK "); break; |
| 2428 | case NFA_MARK_GT: STRCPY(code, "NFA_MARK_GT "); break; |
| 2429 | case NFA_MARK_LT: STRCPY(code, "NFA_MARK_LT "); break; |
| 2430 | case NFA_CURSOR: STRCPY(code, "NFA_CURSOR "); break; |
| 2431 | case NFA_VISUAL: STRCPY(code, "NFA_VISUAL "); break; |
| 2432 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2433 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 2434 | case NFA_STAR_NONGREEDY: STRCPY(code, "NFA_STAR_NONGREEDY "); break; |
| 2435 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 2436 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 2437 | case NFA_EMPTY: STRCPY(code, "NFA_EMPTY"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2438 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2439 | |
| 2440 | case NFA_START_COLL: STRCPY(code, "NFA_START_COLL"); break; |
| 2441 | case NFA_END_COLL: STRCPY(code, "NFA_END_COLL"); break; |
| 2442 | case NFA_START_NEG_COLL: STRCPY(code, "NFA_START_NEG_COLL"); break; |
| 2443 | case NFA_END_NEG_COLL: STRCPY(code, "NFA_END_NEG_COLL"); break; |
| 2444 | case NFA_RANGE: STRCPY(code, "NFA_RANGE"); break; |
| 2445 | case NFA_RANGE_MIN: STRCPY(code, "NFA_RANGE_MIN"); break; |
| 2446 | case NFA_RANGE_MAX: STRCPY(code, "NFA_RANGE_MAX"); break; |
| 2447 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2448 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 2449 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 2450 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 2451 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 2452 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 2453 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 2454 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 2455 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 2456 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 2457 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 2458 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 2459 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 2460 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 2461 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 2462 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 2463 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 2464 | |
| 2465 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 2466 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 2467 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 2468 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 2469 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 2470 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 2471 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 2472 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 2473 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 2474 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 2475 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 2476 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 2477 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 2478 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 2479 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 2480 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 2481 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 2482 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 2483 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 2484 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 2485 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 2486 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 2487 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 2488 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 2489 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 2490 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 2491 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2492 | case NFA_LOWER_IC: STRCPY(code, "NFA_LOWER_IC"); break; |
| 2493 | case NFA_NLOWER_IC: STRCPY(code, "NFA_NLOWER_IC"); break; |
| 2494 | case NFA_UPPER_IC: STRCPY(code, "NFA_UPPER_IC"); break; |
| 2495 | case NFA_NUPPER_IC: STRCPY(code, "NFA_NUPPER_IC"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2496 | |
| 2497 | default: |
| 2498 | STRCPY(code, "CHAR(x)"); |
| 2499 | code[5] = c; |
| 2500 | } |
| 2501 | |
| 2502 | if (addnl == TRUE) |
| 2503 | STRCAT(code, " + NEWLINE "); |
| 2504 | |
| 2505 | } |
| 2506 | |
| 2507 | #ifdef ENABLE_LOG |
| 2508 | static FILE *log_fd; |
| 2509 | |
| 2510 | /* |
| 2511 | * Print the postfix notation of the current regexp. |
| 2512 | */ |
| 2513 | static void |
| 2514 | nfa_postfix_dump(expr, retval) |
| 2515 | char_u *expr; |
| 2516 | int retval; |
| 2517 | { |
| 2518 | int *p; |
| 2519 | FILE *f; |
| 2520 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2521 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2522 | if (f != NULL) |
| 2523 | { |
| 2524 | fprintf(f, "\n-------------------------\n"); |
| 2525 | if (retval == FAIL) |
| 2526 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 2527 | else if (retval == OK) |
| 2528 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 2529 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2530 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2531 | { |
| 2532 | nfa_set_code(*p); |
| 2533 | fprintf(f, "%s, ", code); |
| 2534 | } |
| 2535 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 2536 | for (p = post_start; *p && p < post_ptr; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2537 | fprintf(f, "%d ", *p); |
| 2538 | fprintf(f, "\n\n"); |
| 2539 | fclose(f); |
| 2540 | } |
| 2541 | } |
| 2542 | |
| 2543 | /* |
| 2544 | * Print the NFA starting with a root node "state". |
| 2545 | */ |
| 2546 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2547 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2548 | FILE *debugf; |
| 2549 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2550 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2551 | garray_T indent; |
| 2552 | |
| 2553 | ga_init2(&indent, 1, 64); |
| 2554 | ga_append(&indent, '\0'); |
| 2555 | nfa_print_state2(debugf, state, &indent); |
| 2556 | ga_clear(&indent); |
| 2557 | } |
| 2558 | |
| 2559 | static void |
| 2560 | nfa_print_state2(debugf, state, indent) |
| 2561 | FILE *debugf; |
| 2562 | nfa_state_T *state; |
| 2563 | garray_T *indent; |
| 2564 | { |
| 2565 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2566 | |
| 2567 | if (state == NULL) |
| 2568 | return; |
| 2569 | |
| 2570 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2571 | |
| 2572 | /* Output indent */ |
| 2573 | p = (char_u *)indent->ga_data; |
| 2574 | if (indent->ga_len >= 3) |
| 2575 | { |
| 2576 | int last = indent->ga_len - 3; |
| 2577 | char_u save[2]; |
| 2578 | |
| 2579 | STRNCPY(save, &p[last], 2); |
| 2580 | STRNCPY(&p[last], "+-", 2); |
| 2581 | fprintf(debugf, " %s", p); |
| 2582 | STRNCPY(&p[last], save, 2); |
| 2583 | } |
| 2584 | else |
| 2585 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2586 | |
| 2587 | nfa_set_code(state->c); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 2588 | fprintf(debugf, "%s (%d) (id=%d) val=%d\n", |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2589 | code, |
| 2590 | state->c, |
| 2591 | abs(state->id), |
| 2592 | state->val); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2593 | if (state->id < 0) |
| 2594 | return; |
| 2595 | |
| 2596 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2597 | |
| 2598 | /* grow indent for state->out */ |
| 2599 | indent->ga_len -= 1; |
| 2600 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2601 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2602 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 2603 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2604 | ga_append(indent, '\0'); |
| 2605 | |
| 2606 | nfa_print_state2(debugf, state->out, indent); |
| 2607 | |
| 2608 | /* replace last part of indent for state->out1 */ |
| 2609 | indent->ga_len -= 3; |
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->out1, indent); |
| 2614 | |
| 2615 | /* shrink indent */ |
| 2616 | indent->ga_len -= 3; |
| 2617 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | /* |
| 2621 | * Print the NFA state machine. |
| 2622 | */ |
| 2623 | static void |
| 2624 | nfa_dump(prog) |
| 2625 | nfa_regprog_T *prog; |
| 2626 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2627 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2628 | |
| 2629 | if (debugf != NULL) |
| 2630 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 2631 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2632 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 2633 | if (prog->reganch) |
| 2634 | fprintf(debugf, "reganch: %d\n", prog->reganch); |
| 2635 | if (prog->regstart != NUL) |
| 2636 | fprintf(debugf, "regstart: %c (decimal: %d)\n", |
| 2637 | prog->regstart, prog->regstart); |
| 2638 | if (prog->match_text != NULL) |
| 2639 | fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 2640 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2641 | fclose(debugf); |
| 2642 | } |
| 2643 | } |
| 2644 | #endif /* ENABLE_LOG */ |
| 2645 | #endif /* DEBUG */ |
| 2646 | |
| 2647 | /* |
| 2648 | * Parse r.e. @expr and convert it into postfix form. |
| 2649 | * Return the postfix string on success, NULL otherwise. |
| 2650 | */ |
| 2651 | static int * |
| 2652 | re2post() |
| 2653 | { |
| 2654 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 2655 | return NULL; |
| 2656 | EMIT(NFA_MOPEN); |
| 2657 | return post_start; |
| 2658 | } |
| 2659 | |
| 2660 | /* NB. Some of the code below is inspired by Russ's. */ |
| 2661 | |
| 2662 | /* |
| 2663 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 2664 | * if c == MATCH, no arrows out; matching state. |
| 2665 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 2666 | * If c < 256, labeled arrow with character c to out. |
| 2667 | */ |
| 2668 | |
| 2669 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 2670 | |
| 2671 | /* |
| 2672 | * Allocate and initialize nfa_state_T. |
| 2673 | */ |
| 2674 | static nfa_state_T * |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 2675 | alloc_state(c, out, out1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2676 | int c; |
| 2677 | nfa_state_T *out; |
| 2678 | nfa_state_T *out1; |
| 2679 | { |
| 2680 | nfa_state_T *s; |
| 2681 | |
| 2682 | if (istate >= nstate) |
| 2683 | return NULL; |
| 2684 | |
| 2685 | s = &state_ptr[istate++]; |
| 2686 | |
| 2687 | s->c = c; |
| 2688 | s->out = out; |
| 2689 | s->out1 = out1; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 2690 | s->val = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2691 | |
| 2692 | s->id = istate; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 2693 | s->lastlist[0] = 0; |
| 2694 | s->lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2695 | |
| 2696 | return s; |
| 2697 | } |
| 2698 | |
| 2699 | /* |
| 2700 | * A partially built NFA without the matching state filled in. |
| 2701 | * Frag_T.start points at the start state. |
| 2702 | * Frag_T.out is a list of places that need to be set to the |
| 2703 | * next state for this fragment. |
| 2704 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2705 | |
| 2706 | /* Since the out pointers in the list are always |
| 2707 | * uninitialized, we use the pointers themselves |
| 2708 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2709 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2710 | union Ptrlist |
| 2711 | { |
| 2712 | Ptrlist *next; |
| 2713 | nfa_state_T *s; |
| 2714 | }; |
| 2715 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2716 | struct Frag |
| 2717 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2718 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2719 | Ptrlist *out; |
| 2720 | }; |
| 2721 | typedef struct Frag Frag_T; |
| 2722 | |
| 2723 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2724 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2725 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2726 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2727 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2728 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2729 | |
| 2730 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2731 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2732 | */ |
| 2733 | static Frag_T |
| 2734 | frag(start, out) |
| 2735 | nfa_state_T *start; |
| 2736 | Ptrlist *out; |
| 2737 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2738 | Frag_T n; |
| 2739 | |
| 2740 | n.start = start; |
| 2741 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2742 | return n; |
| 2743 | } |
| 2744 | |
| 2745 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2746 | * Create singleton list containing just outp. |
| 2747 | */ |
| 2748 | static Ptrlist * |
| 2749 | list1(outp) |
| 2750 | nfa_state_T **outp; |
| 2751 | { |
| 2752 | Ptrlist *l; |
| 2753 | |
| 2754 | l = (Ptrlist *)outp; |
| 2755 | l->next = NULL; |
| 2756 | return l; |
| 2757 | } |
| 2758 | |
| 2759 | /* |
| 2760 | * Patch the list of states at out to point to start. |
| 2761 | */ |
| 2762 | static void |
| 2763 | patch(l, s) |
| 2764 | Ptrlist *l; |
| 2765 | nfa_state_T *s; |
| 2766 | { |
| 2767 | Ptrlist *next; |
| 2768 | |
| 2769 | for (; l; l = next) |
| 2770 | { |
| 2771 | next = l->next; |
| 2772 | l->s = s; |
| 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | |
| 2777 | /* |
| 2778 | * Join the two lists l1 and l2, returning the combination. |
| 2779 | */ |
| 2780 | static Ptrlist * |
| 2781 | append(l1, l2) |
| 2782 | Ptrlist *l1; |
| 2783 | Ptrlist *l2; |
| 2784 | { |
| 2785 | Ptrlist *oldl1; |
| 2786 | |
| 2787 | oldl1 = l1; |
| 2788 | while (l1->next) |
| 2789 | l1 = l1->next; |
| 2790 | l1->next = l2; |
| 2791 | return oldl1; |
| 2792 | } |
| 2793 | |
| 2794 | /* |
| 2795 | * Stack used for transforming postfix form into NFA. |
| 2796 | */ |
| 2797 | static Frag_T empty; |
| 2798 | |
| 2799 | static void |
| 2800 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2801 | int *postfix UNUSED; |
| 2802 | int *end UNUSED; |
| 2803 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2804 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2805 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2806 | FILE *df; |
| 2807 | int *p2; |
| 2808 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2809 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2810 | if (df) |
| 2811 | { |
| 2812 | fprintf(df, "Error popping the stack!\n"); |
| 2813 | #ifdef DEBUG |
| 2814 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2815 | #endif |
| 2816 | fprintf(df, "Postfix form is: "); |
| 2817 | #ifdef DEBUG |
| 2818 | for (p2 = postfix; p2 < end; p2++) |
| 2819 | { |
| 2820 | nfa_set_code(*p2); |
| 2821 | fprintf(df, "%s, ", code); |
| 2822 | } |
| 2823 | nfa_set_code(*p); |
| 2824 | fprintf(df, "\nCurrent position is: "); |
| 2825 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2826 | { |
| 2827 | nfa_set_code(*p2); |
| 2828 | fprintf(df, "%s, ", code); |
| 2829 | } |
| 2830 | #else |
| 2831 | for (p2 = postfix; p2 < end; p2++) |
| 2832 | { |
| 2833 | fprintf(df, "%d, ", *p2); |
| 2834 | } |
| 2835 | fprintf(df, "\nCurrent position is: "); |
| 2836 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2837 | { |
| 2838 | fprintf(df, "%d, ", *p2); |
| 2839 | } |
| 2840 | #endif |
| 2841 | fprintf(df, "\n--------------------------\n"); |
| 2842 | fclose(df); |
| 2843 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2844 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2845 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2846 | } |
| 2847 | |
| 2848 | /* |
| 2849 | * Push an item onto the stack. |
| 2850 | */ |
| 2851 | static void |
| 2852 | st_push(s, p, stack_end) |
| 2853 | Frag_T s; |
| 2854 | Frag_T **p; |
| 2855 | Frag_T *stack_end; |
| 2856 | { |
| 2857 | Frag_T *stackp = *p; |
| 2858 | |
| 2859 | if (stackp >= stack_end) |
| 2860 | return; |
| 2861 | *stackp = s; |
| 2862 | *p = *p + 1; |
| 2863 | } |
| 2864 | |
| 2865 | /* |
| 2866 | * Pop an item from the stack. |
| 2867 | */ |
| 2868 | static Frag_T |
| 2869 | st_pop(p, stack) |
| 2870 | Frag_T **p; |
| 2871 | Frag_T *stack; |
| 2872 | { |
| 2873 | Frag_T *stackp; |
| 2874 | |
| 2875 | *p = *p - 1; |
| 2876 | stackp = *p; |
| 2877 | if (stackp < stack) |
| 2878 | return empty; |
| 2879 | return **p; |
| 2880 | } |
| 2881 | |
| 2882 | /* |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2883 | * Estimate the maximum byte length of anything matching "state". |
| 2884 | * When unknown or unlimited return -1. |
| 2885 | */ |
| 2886 | static int |
| 2887 | nfa_max_width(startstate, depth) |
| 2888 | nfa_state_T *startstate; |
| 2889 | int depth; |
| 2890 | { |
| 2891 | int l, r; |
| 2892 | nfa_state_T *state = startstate; |
| 2893 | int len = 0; |
| 2894 | |
| 2895 | /* detect looping in a NFA_SPLIT */ |
| 2896 | if (depth > 4) |
| 2897 | return -1; |
| 2898 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 2899 | while (state != NULL) |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2900 | { |
| 2901 | switch (state->c) |
| 2902 | { |
| 2903 | case NFA_END_INVISIBLE: |
| 2904 | case NFA_END_INVISIBLE_NEG: |
| 2905 | /* the end, return what we have */ |
| 2906 | return len; |
| 2907 | |
| 2908 | case NFA_SPLIT: |
| 2909 | /* two alternatives, use the maximum */ |
| 2910 | l = nfa_max_width(state->out, depth + 1); |
| 2911 | r = nfa_max_width(state->out1, depth + 1); |
| 2912 | if (l < 0 || r < 0) |
| 2913 | return -1; |
| 2914 | return len + (l > r ? l : r); |
| 2915 | |
| 2916 | case NFA_ANY: |
| 2917 | case NFA_START_COLL: |
| 2918 | case NFA_START_NEG_COLL: |
| 2919 | /* matches some character, including composing chars */ |
| 2920 | #ifdef FEAT_MBYTE |
| 2921 | if (enc_utf8) |
| 2922 | len += MB_MAXBYTES; |
| 2923 | else if (has_mbyte) |
| 2924 | len += 2; |
| 2925 | else |
| 2926 | #endif |
| 2927 | ++len; |
| 2928 | if (state->c != NFA_ANY) |
| 2929 | { |
| 2930 | /* skip over the characters */ |
| 2931 | state = state->out1->out; |
| 2932 | continue; |
| 2933 | } |
| 2934 | break; |
| 2935 | |
| 2936 | case NFA_DIGIT: |
| 2937 | case NFA_WHITE: |
| 2938 | case NFA_HEX: |
| 2939 | case NFA_OCTAL: |
| 2940 | /* ascii */ |
| 2941 | ++len; |
| 2942 | break; |
| 2943 | |
| 2944 | case NFA_IDENT: |
| 2945 | case NFA_SIDENT: |
| 2946 | case NFA_KWORD: |
| 2947 | case NFA_SKWORD: |
| 2948 | case NFA_FNAME: |
| 2949 | case NFA_SFNAME: |
| 2950 | case NFA_PRINT: |
| 2951 | case NFA_SPRINT: |
| 2952 | case NFA_NWHITE: |
| 2953 | case NFA_NDIGIT: |
| 2954 | case NFA_NHEX: |
| 2955 | case NFA_NOCTAL: |
| 2956 | case NFA_WORD: |
| 2957 | case NFA_NWORD: |
| 2958 | case NFA_HEAD: |
| 2959 | case NFA_NHEAD: |
| 2960 | case NFA_ALPHA: |
| 2961 | case NFA_NALPHA: |
| 2962 | case NFA_LOWER: |
| 2963 | case NFA_NLOWER: |
| 2964 | case NFA_UPPER: |
| 2965 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 2966 | case NFA_LOWER_IC: |
| 2967 | case NFA_NLOWER_IC: |
| 2968 | case NFA_UPPER_IC: |
| 2969 | case NFA_NUPPER_IC: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 2970 | /* possibly non-ascii */ |
| 2971 | #ifdef FEAT_MBYTE |
| 2972 | if (has_mbyte) |
| 2973 | len += 3; |
| 2974 | else |
| 2975 | #endif |
| 2976 | ++len; |
| 2977 | break; |
| 2978 | |
| 2979 | case NFA_START_INVISIBLE: |
| 2980 | case NFA_START_INVISIBLE_NEG: |
| 2981 | case NFA_START_INVISIBLE_BEFORE: |
| 2982 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 2983 | /* zero-width, out1 points to the END state */ |
| 2984 | state = state->out1->out; |
| 2985 | continue; |
| 2986 | |
| 2987 | case NFA_BACKREF1: |
| 2988 | case NFA_BACKREF2: |
| 2989 | case NFA_BACKREF3: |
| 2990 | case NFA_BACKREF4: |
| 2991 | case NFA_BACKREF5: |
| 2992 | case NFA_BACKREF6: |
| 2993 | case NFA_BACKREF7: |
| 2994 | case NFA_BACKREF8: |
| 2995 | case NFA_BACKREF9: |
| 2996 | #ifdef FEAT_SYN_HL |
| 2997 | case NFA_ZREF1: |
| 2998 | case NFA_ZREF2: |
| 2999 | case NFA_ZREF3: |
| 3000 | case NFA_ZREF4: |
| 3001 | case NFA_ZREF5: |
| 3002 | case NFA_ZREF6: |
| 3003 | case NFA_ZREF7: |
| 3004 | case NFA_ZREF8: |
| 3005 | case NFA_ZREF9: |
| 3006 | #endif |
| 3007 | case NFA_NEWL: |
| 3008 | case NFA_SKIP: |
| 3009 | /* unknown width */ |
| 3010 | return -1; |
| 3011 | |
| 3012 | case NFA_BOL: |
| 3013 | case NFA_EOL: |
| 3014 | case NFA_BOF: |
| 3015 | case NFA_EOF: |
| 3016 | case NFA_BOW: |
| 3017 | case NFA_EOW: |
| 3018 | case NFA_MOPEN: |
| 3019 | case NFA_MOPEN1: |
| 3020 | case NFA_MOPEN2: |
| 3021 | case NFA_MOPEN3: |
| 3022 | case NFA_MOPEN4: |
| 3023 | case NFA_MOPEN5: |
| 3024 | case NFA_MOPEN6: |
| 3025 | case NFA_MOPEN7: |
| 3026 | case NFA_MOPEN8: |
| 3027 | case NFA_MOPEN9: |
| 3028 | #ifdef FEAT_SYN_HL |
| 3029 | case NFA_ZOPEN: |
| 3030 | case NFA_ZOPEN1: |
| 3031 | case NFA_ZOPEN2: |
| 3032 | case NFA_ZOPEN3: |
| 3033 | case NFA_ZOPEN4: |
| 3034 | case NFA_ZOPEN5: |
| 3035 | case NFA_ZOPEN6: |
| 3036 | case NFA_ZOPEN7: |
| 3037 | case NFA_ZOPEN8: |
| 3038 | case NFA_ZOPEN9: |
| 3039 | case NFA_ZCLOSE: |
| 3040 | case NFA_ZCLOSE1: |
| 3041 | case NFA_ZCLOSE2: |
| 3042 | case NFA_ZCLOSE3: |
| 3043 | case NFA_ZCLOSE4: |
| 3044 | case NFA_ZCLOSE5: |
| 3045 | case NFA_ZCLOSE6: |
| 3046 | case NFA_ZCLOSE7: |
| 3047 | case NFA_ZCLOSE8: |
| 3048 | case NFA_ZCLOSE9: |
| 3049 | #endif |
| 3050 | case NFA_MCLOSE: |
| 3051 | case NFA_MCLOSE1: |
| 3052 | case NFA_MCLOSE2: |
| 3053 | case NFA_MCLOSE3: |
| 3054 | case NFA_MCLOSE4: |
| 3055 | case NFA_MCLOSE5: |
| 3056 | case NFA_MCLOSE6: |
| 3057 | case NFA_MCLOSE7: |
| 3058 | case NFA_MCLOSE8: |
| 3059 | case NFA_MCLOSE9: |
| 3060 | case NFA_NOPEN: |
| 3061 | case NFA_NCLOSE: |
| 3062 | |
| 3063 | case NFA_LNUM_GT: |
| 3064 | case NFA_LNUM_LT: |
| 3065 | case NFA_COL_GT: |
| 3066 | case NFA_COL_LT: |
| 3067 | case NFA_VCOL_GT: |
| 3068 | case NFA_VCOL_LT: |
| 3069 | case NFA_MARK_GT: |
| 3070 | case NFA_MARK_LT: |
| 3071 | case NFA_VISUAL: |
| 3072 | case NFA_LNUM: |
| 3073 | case NFA_CURSOR: |
| 3074 | case NFA_COL: |
| 3075 | case NFA_VCOL: |
| 3076 | case NFA_MARK: |
| 3077 | |
| 3078 | case NFA_ZSTART: |
| 3079 | case NFA_ZEND: |
| 3080 | case NFA_OPT_CHARS: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3081 | case NFA_EMPTY: |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3082 | case NFA_START_PATTERN: |
| 3083 | case NFA_END_PATTERN: |
| 3084 | case NFA_COMPOSING: |
| 3085 | case NFA_END_COMPOSING: |
| 3086 | /* zero-width */ |
| 3087 | break; |
| 3088 | |
| 3089 | default: |
| 3090 | if (state->c < 0) |
| 3091 | /* don't know what this is */ |
| 3092 | return -1; |
| 3093 | /* normal character */ |
| 3094 | len += MB_CHAR2LEN(state->c); |
| 3095 | break; |
| 3096 | } |
| 3097 | |
| 3098 | /* normal way to continue */ |
| 3099 | state = state->out; |
| 3100 | } |
| 3101 | |
Bram Moolenaar | fe70acb | 2013-06-21 18:31:23 +0200 | [diff] [blame] | 3102 | /* unrecognized, "cannot happen" */ |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3103 | return -1; |
| 3104 | } |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 3105 | |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3106 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3107 | * Convert a postfix form into its equivalent NFA. |
| 3108 | * Return the NFA start state on success, NULL otherwise. |
| 3109 | */ |
| 3110 | static nfa_state_T * |
| 3111 | post2nfa(postfix, end, nfa_calc_size) |
| 3112 | int *postfix; |
| 3113 | int *end; |
| 3114 | int nfa_calc_size; |
| 3115 | { |
| 3116 | int *p; |
| 3117 | int mopen; |
| 3118 | int mclose; |
| 3119 | Frag_T *stack = NULL; |
| 3120 | Frag_T *stackp = NULL; |
| 3121 | Frag_T *stack_end = NULL; |
| 3122 | Frag_T e1; |
| 3123 | Frag_T e2; |
| 3124 | Frag_T e; |
| 3125 | nfa_state_T *s; |
| 3126 | nfa_state_T *s1; |
| 3127 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3128 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3129 | |
| 3130 | if (postfix == NULL) |
| 3131 | return NULL; |
| 3132 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 3133 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3134 | #define POP() st_pop(&stackp, stack); \ |
| 3135 | if (stackp < stack) \ |
| 3136 | { \ |
| 3137 | st_error(postfix, end, p); \ |
| 3138 | return NULL; \ |
| 3139 | } |
| 3140 | |
| 3141 | if (nfa_calc_size == FALSE) |
| 3142 | { |
| 3143 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3144 | stack = (Frag_T *)lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3145 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 3146 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | for (p = postfix; p < end; ++p) |
| 3150 | { |
| 3151 | switch (*p) |
| 3152 | { |
| 3153 | case NFA_CONCAT: |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3154 | /* Concatenation. |
| 3155 | * Pay attention: this operator does not exist in the r.e. itself |
| 3156 | * (it is implicit, really). It is added when r.e. is translated |
| 3157 | * to postfix form in re2post(). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3158 | if (nfa_calc_size == TRUE) |
| 3159 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3160 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3161 | break; |
| 3162 | } |
| 3163 | e2 = POP(); |
| 3164 | e1 = POP(); |
| 3165 | patch(e1.out, e2.start); |
| 3166 | PUSH(frag(e1.start, e2.out)); |
| 3167 | break; |
| 3168 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3169 | case NFA_OR: |
| 3170 | /* Alternation */ |
| 3171 | if (nfa_calc_size == TRUE) |
| 3172 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3173 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3174 | break; |
| 3175 | } |
| 3176 | e2 = POP(); |
| 3177 | e1 = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3178 | s = alloc_state(NFA_SPLIT, e1.start, e2.start); |
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 | PUSH(frag(s, append(e1.out, e2.out))); |
| 3182 | break; |
| 3183 | |
| 3184 | case NFA_STAR: |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3185 | /* Zero or more, prefer more */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3186 | if (nfa_calc_size == TRUE) |
| 3187 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3188 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3189 | break; |
| 3190 | } |
| 3191 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3192 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3193 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3194 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3195 | patch(e.out, s); |
| 3196 | PUSH(frag(s, list1(&s->out1))); |
| 3197 | break; |
| 3198 | |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3199 | case NFA_STAR_NONGREEDY: |
| 3200 | /* Zero or more, prefer zero */ |
| 3201 | if (nfa_calc_size == TRUE) |
| 3202 | { |
| 3203 | nstate++; |
| 3204 | break; |
| 3205 | } |
| 3206 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3207 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | 36b3a01 | 2013-06-01 12:40:20 +0200 | [diff] [blame] | 3208 | if (s == NULL) |
| 3209 | goto theend; |
| 3210 | patch(e.out, s); |
| 3211 | PUSH(frag(s, list1(&s->out))); |
| 3212 | break; |
| 3213 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3214 | case NFA_QUEST: |
| 3215 | /* one or zero atoms=> 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, e.start, NULL); |
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->out1)))); |
| 3226 | break; |
| 3227 | |
| 3228 | case NFA_QUEST_NONGREEDY: |
| 3229 | /* zero or one atoms => non-greedy match */ |
| 3230 | if (nfa_calc_size == TRUE) |
| 3231 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3232 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3233 | break; |
| 3234 | } |
| 3235 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3236 | s = alloc_state(NFA_SPLIT, NULL, e.start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3237 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3238 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3239 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 3240 | break; |
| 3241 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3242 | case NFA_END_COLL: |
| 3243 | case NFA_END_NEG_COLL: |
| 3244 | /* On the stack is the sequence starting with NFA_START_COLL or |
| 3245 | * NFA_START_NEG_COLL and all possible characters. Patch it to |
| 3246 | * add the output to the start. */ |
| 3247 | if (nfa_calc_size == TRUE) |
| 3248 | { |
| 3249 | nstate++; |
| 3250 | break; |
| 3251 | } |
| 3252 | e = POP(); |
| 3253 | s = alloc_state(NFA_END_COLL, NULL, NULL); |
| 3254 | if (s == NULL) |
| 3255 | goto theend; |
| 3256 | patch(e.out, s); |
| 3257 | e.start->out1 = s; |
| 3258 | PUSH(frag(e.start, list1(&s->out))); |
| 3259 | break; |
| 3260 | |
| 3261 | case NFA_RANGE: |
| 3262 | /* Before this are two characters, the low and high end of a |
| 3263 | * range. Turn them into two states with MIN and MAX. */ |
| 3264 | if (nfa_calc_size == TRUE) |
| 3265 | { |
| 3266 | /* nstate += 0; */ |
| 3267 | break; |
| 3268 | } |
| 3269 | e2 = POP(); |
| 3270 | e1 = POP(); |
| 3271 | e2.start->val = e2.start->c; |
| 3272 | e2.start->c = NFA_RANGE_MAX; |
| 3273 | e1.start->val = e1.start->c; |
| 3274 | e1.start->c = NFA_RANGE_MIN; |
| 3275 | patch(e1.out, e2.start); |
| 3276 | PUSH(frag(e1.start, e2.out)); |
| 3277 | break; |
| 3278 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3279 | case NFA_EMPTY: |
| 3280 | /* 0-length, used in a repetition with max/min count of 0 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3281 | if (nfa_calc_size == TRUE) |
| 3282 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3283 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3284 | break; |
| 3285 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 3286 | s = alloc_state(NFA_EMPTY, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3287 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3288 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3289 | PUSH(frag(s, list1(&s->out))); |
| 3290 | break; |
| 3291 | |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3292 | case NFA_OPT_CHARS: |
| 3293 | { |
| 3294 | int n; |
| 3295 | |
Bram Moolenaar | eec3e1e | 2013-08-01 18:38:26 +0200 | [diff] [blame] | 3296 | /* \%[abc] implemented as: |
| 3297 | * NFA_SPLIT |
| 3298 | * +-CHAR(a) |
| 3299 | * | +-NFA_SPLIT |
| 3300 | * | +-CHAR(b) |
| 3301 | * | | +-NFA_SPLIT |
| 3302 | * | | +-CHAR(c) |
| 3303 | * | | | +-next |
| 3304 | * | | +- next |
| 3305 | * | +- next |
| 3306 | * +- next |
| 3307 | */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3308 | n = *++p; /* get number of characters */ |
| 3309 | if (nfa_calc_size == TRUE) |
| 3310 | { |
| 3311 | nstate += n; |
| 3312 | break; |
| 3313 | } |
Bram Moolenaar | c19b4b5 | 2013-06-05 21:23:39 +0200 | [diff] [blame] | 3314 | s = NULL; /* avoid compiler warning */ |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3315 | e1.out = NULL; /* stores list with out1's */ |
| 3316 | s1 = NULL; /* previous NFA_SPLIT to connect to */ |
| 3317 | while (n-- > 0) |
| 3318 | { |
| 3319 | e = POP(); /* get character */ |
| 3320 | s = alloc_state(NFA_SPLIT, e.start, NULL); |
| 3321 | if (s == NULL) |
| 3322 | goto theend; |
| 3323 | if (e1.out == NULL) |
| 3324 | e1 = e; |
| 3325 | patch(e.out, s1); |
| 3326 | append(e1.out, list1(&s->out1)); |
| 3327 | s1 = s; |
| 3328 | } |
| 3329 | PUSH(frag(s, e1.out)); |
| 3330 | break; |
| 3331 | } |
| 3332 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3333 | case NFA_PREV_ATOM_NO_WIDTH: |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3334 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3335 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3336 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3337 | case NFA_PREV_ATOM_LIKE_PATTERN: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3338 | { |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3339 | int before = (*p == NFA_PREV_ATOM_JUST_BEFORE |
| 3340 | || *p == NFA_PREV_ATOM_JUST_BEFORE_NEG); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3341 | int pattern = (*p == NFA_PREV_ATOM_LIKE_PATTERN); |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3342 | int start_state; |
| 3343 | int end_state; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3344 | int n = 0; |
| 3345 | nfa_state_T *zend; |
| 3346 | nfa_state_T *skip; |
| 3347 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3348 | switch (*p) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3349 | { |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3350 | case NFA_PREV_ATOM_NO_WIDTH: |
| 3351 | start_state = NFA_START_INVISIBLE; |
| 3352 | end_state = NFA_END_INVISIBLE; |
| 3353 | break; |
| 3354 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 3355 | start_state = NFA_START_INVISIBLE_NEG; |
| 3356 | end_state = NFA_END_INVISIBLE_NEG; |
| 3357 | break; |
| 3358 | case NFA_PREV_ATOM_JUST_BEFORE: |
| 3359 | start_state = NFA_START_INVISIBLE_BEFORE; |
| 3360 | end_state = NFA_END_INVISIBLE; |
| 3361 | break; |
| 3362 | case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
| 3363 | start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
| 3364 | end_state = NFA_END_INVISIBLE_NEG; |
| 3365 | break; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 3366 | default: /* NFA_PREV_ATOM_LIKE_PATTERN: */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 3367 | start_state = NFA_START_PATTERN; |
| 3368 | end_state = NFA_END_PATTERN; |
| 3369 | break; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3370 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3371 | |
| 3372 | if (before) |
| 3373 | n = *++p; /* get the count */ |
| 3374 | |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3375 | /* The \@= operator: match the preceding atom with zero width. |
Bram Moolenaar | b06e20e | 2013-05-30 22:44:02 +0200 | [diff] [blame] | 3376 | * The \@! operator: no match for the preceding atom. |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3377 | * The \@<= operator: match for the preceding atom. |
| 3378 | * The \@<! operator: no match for the preceding atom. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3379 | * Surrounds the preceding atom with START_INVISIBLE and |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 3380 | * END_INVISIBLE, similarly to MOPEN. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3381 | |
| 3382 | if (nfa_calc_size == TRUE) |
| 3383 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3384 | nstate += pattern ? 4 : 2; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3385 | break; |
| 3386 | } |
| 3387 | e = POP(); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3388 | s1 = alloc_state(end_state, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3389 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3390 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3391 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3392 | s = alloc_state(start_state, e.start, s1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3393 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3394 | goto theend; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3395 | if (pattern) |
| 3396 | { |
| 3397 | /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ |
| 3398 | skip = alloc_state(NFA_SKIP, NULL, NULL); |
| 3399 | zend = alloc_state(NFA_ZEND, s1, NULL); |
| 3400 | s1->out= skip; |
| 3401 | patch(e.out, zend); |
| 3402 | PUSH(frag(s, list1(&skip->out))); |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 3403 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3404 | else |
| 3405 | { |
| 3406 | patch(e.out, s1); |
| 3407 | PUSH(frag(s, list1(&s1->out))); |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 3408 | if (before) |
| 3409 | { |
| 3410 | if (n <= 0) |
| 3411 | /* See if we can guess the maximum width, it avoids a |
| 3412 | * lot of pointless tries. */ |
| 3413 | n = nfa_max_width(e.start, 0); |
| 3414 | s->val = n; /* store the count */ |
| 3415 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3416 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3417 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3418 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3419 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3420 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3421 | case NFA_COMPOSING: /* char with composing char */ |
| 3422 | #if 0 |
| 3423 | /* TODO */ |
| 3424 | if (regflags & RF_ICOMBINE) |
| 3425 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3426 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3427 | } |
| 3428 | #endif |
| 3429 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3430 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3431 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3432 | case NFA_MOPEN: /* \( \) Submatch */ |
| 3433 | case NFA_MOPEN1: |
| 3434 | case NFA_MOPEN2: |
| 3435 | case NFA_MOPEN3: |
| 3436 | case NFA_MOPEN4: |
| 3437 | case NFA_MOPEN5: |
| 3438 | case NFA_MOPEN6: |
| 3439 | case NFA_MOPEN7: |
| 3440 | case NFA_MOPEN8: |
| 3441 | case NFA_MOPEN9: |
| 3442 | #ifdef FEAT_SYN_HL |
| 3443 | case NFA_ZOPEN: /* \z( \) Submatch */ |
| 3444 | case NFA_ZOPEN1: |
| 3445 | case NFA_ZOPEN2: |
| 3446 | case NFA_ZOPEN3: |
| 3447 | case NFA_ZOPEN4: |
| 3448 | case NFA_ZOPEN5: |
| 3449 | case NFA_ZOPEN6: |
| 3450 | case NFA_ZOPEN7: |
| 3451 | case NFA_ZOPEN8: |
| 3452 | case NFA_ZOPEN9: |
| 3453 | #endif |
| 3454 | case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3455 | if (nfa_calc_size == TRUE) |
| 3456 | { |
| 3457 | nstate += 2; |
| 3458 | break; |
| 3459 | } |
| 3460 | |
| 3461 | mopen = *p; |
| 3462 | switch (*p) |
| 3463 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3464 | case NFA_NOPEN: mclose = NFA_NCLOSE; break; |
| 3465 | #ifdef FEAT_SYN_HL |
| 3466 | case NFA_ZOPEN: mclose = NFA_ZCLOSE; break; |
| 3467 | case NFA_ZOPEN1: mclose = NFA_ZCLOSE1; break; |
| 3468 | case NFA_ZOPEN2: mclose = NFA_ZCLOSE2; break; |
| 3469 | case NFA_ZOPEN3: mclose = NFA_ZCLOSE3; break; |
| 3470 | case NFA_ZOPEN4: mclose = NFA_ZCLOSE4; break; |
| 3471 | case NFA_ZOPEN5: mclose = NFA_ZCLOSE5; break; |
| 3472 | case NFA_ZOPEN6: mclose = NFA_ZCLOSE6; break; |
| 3473 | case NFA_ZOPEN7: mclose = NFA_ZCLOSE7; break; |
| 3474 | case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
| 3475 | case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
| 3476 | #endif |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3477 | #ifdef FEAT_MBYTE |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3478 | case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3479 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3480 | default: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3481 | /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3482 | mclose = *p + NSUBEXP; |
| 3483 | break; |
| 3484 | } |
| 3485 | |
| 3486 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 3487 | * the empty regexp "". In this case, the NFA will be |
| 3488 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 3489 | * empty groups of parenthesis, and empty mbyte chars */ |
| 3490 | if (stackp == stack) |
| 3491 | { |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3492 | s = alloc_state(mopen, NULL, 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 | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3495 | s1 = alloc_state(mclose, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3496 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3497 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3498 | patch(list1(&s->out), s1); |
| 3499 | PUSH(frag(s, list1(&s1->out))); |
| 3500 | break; |
| 3501 | } |
| 3502 | |
| 3503 | /* At least one node was emitted before NFA_MOPEN, so |
| 3504 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 3505 | e = POP(); |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3506 | s = alloc_state(mopen, e.start, NULL); /* `(' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3507 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3508 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3509 | |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3510 | s1 = alloc_state(mclose, NULL, NULL); /* `)' */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3511 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3512 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3513 | patch(e.out, s1); |
| 3514 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3515 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3516 | if (mopen == NFA_COMPOSING) |
| 3517 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3518 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3519 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3520 | |
| 3521 | PUSH(frag(s, list1(&s1->out))); |
| 3522 | break; |
| 3523 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3524 | case NFA_BACKREF1: |
| 3525 | case NFA_BACKREF2: |
| 3526 | case NFA_BACKREF3: |
| 3527 | case NFA_BACKREF4: |
| 3528 | case NFA_BACKREF5: |
| 3529 | case NFA_BACKREF6: |
| 3530 | case NFA_BACKREF7: |
| 3531 | case NFA_BACKREF8: |
| 3532 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3533 | #ifdef FEAT_SYN_HL |
| 3534 | case NFA_ZREF1: |
| 3535 | case NFA_ZREF2: |
| 3536 | case NFA_ZREF3: |
| 3537 | case NFA_ZREF4: |
| 3538 | case NFA_ZREF5: |
| 3539 | case NFA_ZREF6: |
| 3540 | case NFA_ZREF7: |
| 3541 | case NFA_ZREF8: |
| 3542 | case NFA_ZREF9: |
| 3543 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3544 | if (nfa_calc_size == TRUE) |
| 3545 | { |
| 3546 | nstate += 2; |
| 3547 | break; |
| 3548 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3549 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3550 | if (s == NULL) |
| 3551 | goto theend; |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3552 | s1 = alloc_state(NFA_SKIP, NULL, NULL); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3553 | if (s1 == NULL) |
| 3554 | goto theend; |
| 3555 | patch(list1(&s->out), s1); |
| 3556 | PUSH(frag(s, list1(&s1->out))); |
| 3557 | break; |
| 3558 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3559 | case NFA_LNUM: |
| 3560 | case NFA_LNUM_GT: |
| 3561 | case NFA_LNUM_LT: |
| 3562 | case NFA_VCOL: |
| 3563 | case NFA_VCOL_GT: |
| 3564 | case NFA_VCOL_LT: |
| 3565 | case NFA_COL: |
| 3566 | case NFA_COL_GT: |
| 3567 | case NFA_COL_LT: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 3568 | case NFA_MARK: |
| 3569 | case NFA_MARK_GT: |
| 3570 | case NFA_MARK_LT: |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3571 | { |
| 3572 | int n = *++p; /* lnum, col or mark name */ |
| 3573 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3574 | if (nfa_calc_size == TRUE) |
| 3575 | { |
| 3576 | nstate += 1; |
| 3577 | break; |
| 3578 | } |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3579 | s = alloc_state(p[-1], NULL, NULL); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3580 | if (s == NULL) |
| 3581 | goto theend; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3582 | s->val = n; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3583 | PUSH(frag(s, list1(&s->out))); |
| 3584 | break; |
Bram Moolenaar | d75799ab7 | 2013-06-05 11:05:17 +0200 | [diff] [blame] | 3585 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3586 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3587 | case NFA_ZSTART: |
| 3588 | case NFA_ZEND: |
| 3589 | default: |
| 3590 | /* Operands */ |
| 3591 | if (nfa_calc_size == TRUE) |
| 3592 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3593 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3594 | break; |
| 3595 | } |
Bram Moolenaar | 525666f | 2013-06-02 16:40:55 +0200 | [diff] [blame] | 3596 | s = alloc_state(*p, NULL, NULL); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3597 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3598 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3599 | PUSH(frag(s, list1(&s->out))); |
| 3600 | break; |
| 3601 | |
| 3602 | } /* switch(*p) */ |
| 3603 | |
| 3604 | } /* for(p = postfix; *p; ++p) */ |
| 3605 | |
| 3606 | if (nfa_calc_size == TRUE) |
| 3607 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 3608 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3609 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3610 | } |
| 3611 | |
| 3612 | e = POP(); |
| 3613 | if (stackp != stack) |
| 3614 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 3615 | |
| 3616 | if (istate >= nstate) |
| 3617 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 3618 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3619 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 3620 | matchstate->c = NFA_MATCH; |
| 3621 | matchstate->out = matchstate->out1 = NULL; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 3622 | matchstate->id = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3623 | |
| 3624 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 3625 | ret = e.start; |
| 3626 | |
| 3627 | theend: |
| 3628 | vim_free(stack); |
| 3629 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3630 | |
| 3631 | #undef POP1 |
| 3632 | #undef PUSH1 |
| 3633 | #undef POP2 |
| 3634 | #undef PUSH2 |
| 3635 | #undef POP |
| 3636 | #undef PUSH |
| 3637 | } |
| 3638 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 3639 | /* |
| 3640 | * After building the NFA program, inspect it to add optimization hints. |
| 3641 | */ |
| 3642 | static void |
| 3643 | nfa_postprocess(prog) |
| 3644 | nfa_regprog_T *prog; |
| 3645 | { |
| 3646 | int i; |
| 3647 | int c; |
| 3648 | |
| 3649 | for (i = 0; i < prog->nstate; ++i) |
| 3650 | { |
| 3651 | c = prog->state[i].c; |
| 3652 | if (c == NFA_START_INVISIBLE |
| 3653 | || c == NFA_START_INVISIBLE_NEG |
| 3654 | || c == NFA_START_INVISIBLE_BEFORE |
| 3655 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3656 | { |
| 3657 | int directly; |
| 3658 | |
| 3659 | /* Do it directly when what follows is possibly the end of the |
| 3660 | * match. */ |
| 3661 | if (match_follows(prog->state[i].out1->out, 0)) |
| 3662 | directly = TRUE; |
| 3663 | else |
| 3664 | { |
| 3665 | int ch_invisible = failure_chance(prog->state[i].out, 0); |
| 3666 | int ch_follows = failure_chance(prog->state[i].out1->out, 0); |
| 3667 | |
| 3668 | /* Postpone when the invisible match is expensive or has a |
| 3669 | * lower chance of failing. */ |
| 3670 | if (c == NFA_START_INVISIBLE_BEFORE |
| 3671 | || c == NFA_START_INVISIBLE_BEFORE_NEG) |
| 3672 | { |
| 3673 | /* "before" matches are very expensive when |
| 3674 | * unbounded, always prefer what follows then, |
| 3675 | * unless what follows will always match. |
| 3676 | * Otherwise strongly prefer what follows. */ |
| 3677 | if (prog->state[i].val <= 0 && ch_follows > 0) |
| 3678 | directly = FALSE; |
| 3679 | else |
| 3680 | directly = ch_follows * 10 < ch_invisible; |
| 3681 | } |
| 3682 | else |
| 3683 | { |
| 3684 | /* normal invisible, first do the one with the |
| 3685 | * highest failure chance */ |
| 3686 | directly = ch_follows < ch_invisible; |
| 3687 | } |
| 3688 | } |
| 3689 | if (directly) |
| 3690 | /* switch to the _FIRST state */ |
| 3691 | ++prog->state[i].c; |
| 3692 | } |
| 3693 | } |
| 3694 | } |
| 3695 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3696 | /**************************************************************** |
| 3697 | * NFA execution code. |
| 3698 | ****************************************************************/ |
| 3699 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3700 | typedef struct |
| 3701 | { |
| 3702 | int in_use; /* number of subexpr with useful info */ |
| 3703 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3704 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3705 | union |
| 3706 | { |
| 3707 | struct multipos |
| 3708 | { |
| 3709 | lpos_T start; |
| 3710 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3711 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3712 | struct linepos |
| 3713 | { |
| 3714 | char_u *start; |
| 3715 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3716 | } line[NSUBEXP]; |
| 3717 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3718 | } regsub_T; |
| 3719 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3720 | typedef struct |
| 3721 | { |
| 3722 | regsub_T norm; /* \( .. \) matches */ |
| 3723 | #ifdef FEAT_SYN_HL |
| 3724 | regsub_T synt; /* \z( .. \) matches */ |
| 3725 | #endif |
| 3726 | } regsubs_T; |
| 3727 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3728 | /* nfa_pim_T stores a Postponed Invisible Match. */ |
| 3729 | typedef struct nfa_pim_S nfa_pim_T; |
| 3730 | struct nfa_pim_S |
| 3731 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3732 | int result; /* NFA_PIM_*, see below */ |
| 3733 | nfa_state_T *state; /* the invisible match start state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3734 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3735 | union |
| 3736 | { |
| 3737 | lpos_T pos; |
| 3738 | char_u *ptr; |
| 3739 | } end; /* where the match must end */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3740 | }; |
| 3741 | |
| 3742 | /* Values for done in nfa_pim_T. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3743 | #define NFA_PIM_UNUSED 0 /* pim not used */ |
| 3744 | #define NFA_PIM_TODO 1 /* pim not done yet */ |
| 3745 | #define NFA_PIM_MATCH 2 /* pim executed, matches */ |
| 3746 | #define NFA_PIM_NOMATCH 3 /* pim executed, no match */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3747 | |
| 3748 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3749 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3750 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3751 | { |
| 3752 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3753 | int count; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3754 | nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed |
| 3755 | * invisible match */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3756 | regsubs_T subs; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3757 | } nfa_thread_T; |
| 3758 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3759 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3760 | typedef struct |
| 3761 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3762 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3763 | int n; /* nr of states currently in "t" */ |
| 3764 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3765 | int id; /* ID of the list */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 3766 | int has_pim; /* TRUE when any state has a PIM */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3767 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3768 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3769 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3770 | static void log_subsexpr __ARGS((regsubs_T *subs)); |
| 3771 | static void log_subexpr __ARGS((regsub_T *sub)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3772 | static char *pim_info __ARGS((nfa_pim_T *pim)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3773 | |
| 3774 | static void |
| 3775 | log_subsexpr(subs) |
| 3776 | regsubs_T *subs; |
| 3777 | { |
| 3778 | log_subexpr(&subs->norm); |
| 3779 | # ifdef FEAT_SYN_HL |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 3780 | if (nfa_has_zsubexpr) |
| 3781 | log_subexpr(&subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3782 | # endif |
| 3783 | } |
| 3784 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3785 | static void |
| 3786 | log_subexpr(sub) |
| 3787 | regsub_T *sub; |
| 3788 | { |
| 3789 | int j; |
| 3790 | |
| 3791 | for (j = 0; j < sub->in_use; j++) |
| 3792 | if (REG_MULTI) |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3793 | 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] | 3794 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3795 | sub->list.multi[j].start.col, |
| 3796 | (int)sub->list.multi[j].start.lnum, |
| 3797 | sub->list.multi[j].end.col, |
| 3798 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3799 | else |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3800 | { |
| 3801 | char *s = (char *)sub->list.line[j].start; |
| 3802 | char *e = (char *)sub->list.line[j].end; |
| 3803 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 3804 | fprintf(log_fd, "*** group %d, start: \"%s\", end: \"%s\"\n", |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3805 | j, |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 3806 | s == NULL ? "NULL" : s, |
| 3807 | e == NULL ? "NULL" : e); |
| 3808 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3809 | } |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3810 | |
| 3811 | static char * |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3812 | pim_info(pim) |
| 3813 | nfa_pim_T *pim; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3814 | { |
| 3815 | static char buf[30]; |
| 3816 | |
| 3817 | if (pim == NULL || pim->result == NFA_PIM_UNUSED) |
| 3818 | buf[0] = NUL; |
| 3819 | else |
| 3820 | { |
| 3821 | sprintf(buf, " PIM col %d", REG_MULTI ? (int)pim->end.pos.col |
| 3822 | : (int)(pim->end.ptr - reginput)); |
| 3823 | } |
| 3824 | return buf; |
| 3825 | } |
| 3826 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3827 | #endif |
| 3828 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3829 | /* Used during execution: whether a match has been found. */ |
| 3830 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3831 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3832 | static void copy_pim __ARGS((nfa_pim_T *to, nfa_pim_T *from)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3833 | static void clear_sub __ARGS((regsub_T *sub)); |
| 3834 | static void copy_sub __ARGS((regsub_T *to, regsub_T *from)); |
| 3835 | static void copy_sub_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3836 | static void copy_ze_off __ARGS((regsub_T *to, regsub_T *from)); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3837 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 3838 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 3839 | static int has_state_with_pos __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim)); |
| 3840 | static int pim_equal __ARGS((nfa_pim_T *one, nfa_pim_T *two)); |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 3841 | static int state_in_list __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs)); |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 3842 | static regsubs_T *addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs_arg, nfa_pim_T *pim, int off)); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 3843 | static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsubs_T *subs, nfa_pim_T *pim, int *ip)); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3844 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 3845 | /* |
| 3846 | * Copy postponed invisible match info from "from" to "to". |
| 3847 | */ |
| 3848 | static void |
| 3849 | copy_pim(to, from) |
| 3850 | nfa_pim_T *to; |
| 3851 | nfa_pim_T *from; |
| 3852 | { |
| 3853 | to->result = from->result; |
| 3854 | to->state = from->state; |
| 3855 | copy_sub(&to->subs.norm, &from->subs.norm); |
| 3856 | #ifdef FEAT_SYN_HL |
| 3857 | if (nfa_has_zsubexpr) |
| 3858 | copy_sub(&to->subs.synt, &from->subs.synt); |
| 3859 | #endif |
| 3860 | to->end = from->end; |
| 3861 | } |
| 3862 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 3863 | static void |
| 3864 | clear_sub(sub) |
| 3865 | regsub_T *sub; |
| 3866 | { |
| 3867 | if (REG_MULTI) |
| 3868 | /* Use 0xff to set lnum to -1 */ |
| 3869 | vim_memset(sub->list.multi, 0xff, |
| 3870 | sizeof(struct multipos) * nfa_nsubexpr); |
| 3871 | else |
| 3872 | vim_memset(sub->list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 3873 | sub->in_use = 0; |
| 3874 | } |
| 3875 | |
| 3876 | /* |
| 3877 | * Copy the submatches from "from" to "to". |
| 3878 | */ |
| 3879 | static void |
| 3880 | copy_sub(to, from) |
| 3881 | regsub_T *to; |
| 3882 | regsub_T *from; |
| 3883 | { |
| 3884 | to->in_use = from->in_use; |
| 3885 | if (from->in_use > 0) |
| 3886 | { |
| 3887 | /* Copy the match start and end positions. */ |
| 3888 | if (REG_MULTI) |
| 3889 | mch_memmove(&to->list.multi[0], |
| 3890 | &from->list.multi[0], |
| 3891 | sizeof(struct multipos) * from->in_use); |
| 3892 | else |
| 3893 | mch_memmove(&to->list.line[0], |
| 3894 | &from->list.line[0], |
| 3895 | sizeof(struct linepos) * from->in_use); |
| 3896 | } |
| 3897 | } |
| 3898 | |
| 3899 | /* |
| 3900 | * Like copy_sub() but exclude the main match. |
| 3901 | */ |
| 3902 | static void |
| 3903 | copy_sub_off(to, from) |
| 3904 | regsub_T *to; |
| 3905 | regsub_T *from; |
| 3906 | { |
| 3907 | if (to->in_use < from->in_use) |
| 3908 | to->in_use = from->in_use; |
| 3909 | if (from->in_use > 1) |
| 3910 | { |
| 3911 | /* Copy the match start and end positions. */ |
| 3912 | if (REG_MULTI) |
| 3913 | mch_memmove(&to->list.multi[1], |
| 3914 | &from->list.multi[1], |
| 3915 | sizeof(struct multipos) * (from->in_use - 1)); |
| 3916 | else |
| 3917 | mch_memmove(&to->list.line[1], |
| 3918 | &from->list.line[1], |
| 3919 | sizeof(struct linepos) * (from->in_use - 1)); |
| 3920 | } |
| 3921 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3922 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3923 | /* |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 3924 | * Like copy_sub() but only do the end of the main match if \ze is present. |
| 3925 | */ |
| 3926 | static void |
| 3927 | copy_ze_off(to, from) |
| 3928 | regsub_T *to; |
| 3929 | regsub_T *from; |
| 3930 | { |
| 3931 | if (nfa_has_zend) |
| 3932 | { |
| 3933 | if (REG_MULTI) |
| 3934 | { |
| 3935 | if (from->list.multi[0].end.lnum >= 0) |
| 3936 | to->list.multi[0].end = from->list.multi[0].end; |
| 3937 | } |
| 3938 | else |
| 3939 | { |
| 3940 | if (from->list.line[0].end != NULL) |
| 3941 | to->list.line[0].end = from->list.line[0].end; |
| 3942 | } |
| 3943 | } |
| 3944 | } |
| 3945 | |
| 3946 | /* |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3947 | * Return TRUE if "sub1" and "sub2" have the same start positions. |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3948 | */ |
| 3949 | static int |
| 3950 | sub_equal(sub1, sub2) |
| 3951 | regsub_T *sub1; |
| 3952 | regsub_T *sub2; |
| 3953 | { |
| 3954 | int i; |
| 3955 | int todo; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3956 | linenr_T s1; |
| 3957 | linenr_T s2; |
| 3958 | char_u *sp1; |
| 3959 | char_u *sp2; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3960 | |
| 3961 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 3962 | if (REG_MULTI) |
| 3963 | { |
| 3964 | for (i = 0; i < todo; ++i) |
| 3965 | { |
| 3966 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3967 | s1 = sub1->list.multi[i].start.lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3968 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3969 | s1 = -1; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3970 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3971 | s2 = sub2->list.multi[i].start.lnum; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3972 | else |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3973 | s2 = -1; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3974 | if (s1 != s2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3975 | return FALSE; |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 3976 | if (s1 != -1 && sub1->list.multi[i].start.col |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3977 | != sub2->list.multi[i].start.col) |
| 3978 | return FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3979 | } |
| 3980 | } |
| 3981 | else |
| 3982 | { |
| 3983 | for (i = 0; i < todo; ++i) |
| 3984 | { |
| 3985 | if (i < sub1->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3986 | sp1 = sub1->list.line[i].start; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3987 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3988 | sp1 = NULL; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3989 | if (i < sub2->in_use) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3990 | sp2 = sub2->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 | sp2 = NULL; |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 3993 | if (sp1 != sp2) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3994 | return FALSE; |
| 3995 | } |
| 3996 | } |
| 3997 | |
| 3998 | return TRUE; |
| 3999 | } |
| 4000 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4001 | #ifdef ENABLE_LOG |
| 4002 | static void |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4003 | report_state(char *action, |
| 4004 | regsub_T *sub, |
| 4005 | nfa_state_T *state, |
| 4006 | int lid, |
| 4007 | nfa_pim_T *pim) |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4008 | { |
| 4009 | int col; |
| 4010 | |
| 4011 | if (sub->in_use <= 0) |
| 4012 | col = -1; |
| 4013 | else if (REG_MULTI) |
| 4014 | col = sub->list.multi[0].start.col; |
| 4015 | else |
| 4016 | col = (int)(sub->list.line[0].start - regline); |
| 4017 | nfa_set_code(state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4018 | fprintf(log_fd, "> %s state %d to list %d. char %d: %s (start col %d)%s\n", |
| 4019 | action, abs(state->id), lid, state->c, code, col, |
| 4020 | pim_info(pim)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4021 | } |
| 4022 | #endif |
| 4023 | |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4024 | /* |
| 4025 | * Return TRUE if the same state is already in list "l" with the same |
| 4026 | * positions as "subs". |
| 4027 | */ |
| 4028 | static int |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4029 | has_state_with_pos(l, state, subs, pim) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4030 | nfa_list_T *l; /* runtime state list */ |
| 4031 | nfa_state_T *state; /* state to update */ |
| 4032 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4033 | nfa_pim_T *pim; /* postponed match or NULL */ |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4034 | { |
| 4035 | nfa_thread_T *thread; |
| 4036 | int i; |
| 4037 | |
| 4038 | for (i = 0; i < l->n; ++i) |
| 4039 | { |
| 4040 | thread = &l->t[i]; |
| 4041 | if (thread->state->id == state->id |
| 4042 | && sub_equal(&thread->subs.norm, &subs->norm) |
| 4043 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | c5089bb | 2013-06-14 21:15:25 +0200 | [diff] [blame] | 4044 | && (!nfa_has_zsubexpr |
| 4045 | || sub_equal(&thread->subs.synt, &subs->synt)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4046 | #endif |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4047 | && pim_equal(&thread->pim, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4048 | return TRUE; |
| 4049 | } |
| 4050 | return FALSE; |
| 4051 | } |
| 4052 | |
| 4053 | /* |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4054 | * Return TRUE if "one" and "two" are equal. That includes when both are not |
| 4055 | * set. |
| 4056 | */ |
| 4057 | static int |
| 4058 | pim_equal(one, two) |
| 4059 | nfa_pim_T *one; |
| 4060 | nfa_pim_T *two; |
| 4061 | { |
| 4062 | int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED); |
| 4063 | int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED); |
| 4064 | |
| 4065 | if (one_unused) |
| 4066 | /* one is unused: equal when two is also unused */ |
| 4067 | return two_unused; |
| 4068 | if (two_unused) |
| 4069 | /* one is used and two is not: not equal */ |
| 4070 | return FALSE; |
Bram Moolenaar | 3f0df06 | 2013-08-14 13:34:25 +0200 | [diff] [blame] | 4071 | /* compare the state id */ |
| 4072 | if (one->state->id != two->state->id) |
| 4073 | return FALSE; |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4074 | /* compare the position */ |
| 4075 | if (REG_MULTI) |
| 4076 | return one->end.pos.lnum == two->end.pos.lnum |
| 4077 | && one->end.pos.col == two->end.pos.col; |
| 4078 | return one->end.ptr == two->end.ptr; |
| 4079 | } |
| 4080 | |
| 4081 | /* |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4082 | * Return TRUE if "state" leads to a NFA_MATCH without advancing the input. |
| 4083 | */ |
| 4084 | static int |
| 4085 | match_follows(startstate, depth) |
| 4086 | nfa_state_T *startstate; |
| 4087 | int depth; |
| 4088 | { |
| 4089 | nfa_state_T *state = startstate; |
| 4090 | |
| 4091 | /* avoid too much recursion */ |
| 4092 | if (depth > 10) |
| 4093 | return FALSE; |
| 4094 | |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4095 | while (state != NULL) |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4096 | { |
| 4097 | switch (state->c) |
| 4098 | { |
| 4099 | case NFA_MATCH: |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4100 | case NFA_MCLOSE: |
| 4101 | case NFA_END_INVISIBLE: |
| 4102 | case NFA_END_INVISIBLE_NEG: |
| 4103 | case NFA_END_PATTERN: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4104 | return TRUE; |
| 4105 | |
| 4106 | case NFA_SPLIT: |
| 4107 | return match_follows(state->out, depth + 1) |
| 4108 | || match_follows(state->out1, depth + 1); |
| 4109 | |
| 4110 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4111 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4112 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4113 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4114 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4115 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4116 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4117 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4118 | case NFA_COMPOSING: |
| 4119 | /* skip ahead to next state */ |
| 4120 | state = state->out1->out; |
Bram Moolenaar | 690ae9c | 2013-07-13 20:58:11 +0200 | [diff] [blame] | 4121 | continue; |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4122 | |
| 4123 | case NFA_ANY: |
| 4124 | case NFA_IDENT: |
| 4125 | case NFA_SIDENT: |
| 4126 | case NFA_KWORD: |
| 4127 | case NFA_SKWORD: |
| 4128 | case NFA_FNAME: |
| 4129 | case NFA_SFNAME: |
| 4130 | case NFA_PRINT: |
| 4131 | case NFA_SPRINT: |
| 4132 | case NFA_WHITE: |
| 4133 | case NFA_NWHITE: |
| 4134 | case NFA_DIGIT: |
| 4135 | case NFA_NDIGIT: |
| 4136 | case NFA_HEX: |
| 4137 | case NFA_NHEX: |
| 4138 | case NFA_OCTAL: |
| 4139 | case NFA_NOCTAL: |
| 4140 | case NFA_WORD: |
| 4141 | case NFA_NWORD: |
| 4142 | case NFA_HEAD: |
| 4143 | case NFA_NHEAD: |
| 4144 | case NFA_ALPHA: |
| 4145 | case NFA_NALPHA: |
| 4146 | case NFA_LOWER: |
| 4147 | case NFA_NLOWER: |
| 4148 | case NFA_UPPER: |
| 4149 | case NFA_NUPPER: |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 4150 | case NFA_LOWER_IC: |
| 4151 | case NFA_NLOWER_IC: |
| 4152 | case NFA_UPPER_IC: |
| 4153 | case NFA_NUPPER_IC: |
Bram Moolenaar | 1e02e66 | 2013-06-08 23:26:27 +0200 | [diff] [blame] | 4154 | case NFA_START_COLL: |
| 4155 | case NFA_START_NEG_COLL: |
| 4156 | case NFA_NEWL: |
| 4157 | /* state will advance input */ |
| 4158 | return FALSE; |
| 4159 | |
| 4160 | default: |
| 4161 | if (state->c > 0) |
| 4162 | /* state will advance input */ |
| 4163 | return FALSE; |
| 4164 | |
| 4165 | /* Others: zero-width or possibly zero-width, might still find |
| 4166 | * a match at the same position, keep looking. */ |
| 4167 | break; |
| 4168 | } |
| 4169 | state = state->out; |
| 4170 | } |
| 4171 | return FALSE; |
| 4172 | } |
| 4173 | |
| 4174 | |
| 4175 | /* |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4176 | * Return TRUE if "state" is already in list "l". |
| 4177 | */ |
| 4178 | static int |
| 4179 | state_in_list(l, state, subs) |
| 4180 | nfa_list_T *l; /* runtime state list */ |
| 4181 | nfa_state_T *state; /* state to update */ |
| 4182 | regsubs_T *subs; /* pointers to subexpressions */ |
| 4183 | { |
| 4184 | if (state->lastlist[nfa_ll_index] == l->id) |
| 4185 | { |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4186 | if (!nfa_has_backref || has_state_with_pos(l, state, subs, NULL)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4187 | return TRUE; |
| 4188 | } |
| 4189 | return FALSE; |
| 4190 | } |
| 4191 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4192 | /* |
| 4193 | * Add "state" and possibly what follows to state list ".". |
| 4194 | * Returns "subs_arg", possibly copied into temp_subs. |
| 4195 | */ |
| 4196 | |
| 4197 | static regsubs_T * |
| 4198 | addstate(l, state, subs_arg, pim, off) |
| 4199 | nfa_list_T *l; /* runtime state list */ |
| 4200 | nfa_state_T *state; /* state to update */ |
| 4201 | regsubs_T *subs_arg; /* pointers to subexpressions */ |
| 4202 | nfa_pim_T *pim; /* postponed look-behind match */ |
| 4203 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4204 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4205 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4206 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4207 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4208 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4209 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4210 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4211 | regsub_T *sub; |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4212 | regsubs_T *subs = subs_arg; |
| 4213 | static regsubs_T temp_subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4214 | #ifdef ENABLE_LOG |
| 4215 | int did_print = FALSE; |
| 4216 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4217 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4218 | switch (state->c) |
| 4219 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4220 | case NFA_NCLOSE: |
| 4221 | case NFA_MCLOSE: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4222 | case NFA_MCLOSE1: |
| 4223 | case NFA_MCLOSE2: |
| 4224 | case NFA_MCLOSE3: |
| 4225 | case NFA_MCLOSE4: |
| 4226 | case NFA_MCLOSE5: |
| 4227 | case NFA_MCLOSE6: |
| 4228 | case NFA_MCLOSE7: |
| 4229 | case NFA_MCLOSE8: |
| 4230 | case NFA_MCLOSE9: |
| 4231 | #ifdef FEAT_SYN_HL |
| 4232 | case NFA_ZCLOSE: |
| 4233 | case NFA_ZCLOSE1: |
| 4234 | case NFA_ZCLOSE2: |
| 4235 | case NFA_ZCLOSE3: |
| 4236 | case NFA_ZCLOSE4: |
| 4237 | case NFA_ZCLOSE5: |
| 4238 | case NFA_ZCLOSE6: |
| 4239 | case NFA_ZCLOSE7: |
| 4240 | case NFA_ZCLOSE8: |
| 4241 | case NFA_ZCLOSE9: |
| 4242 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4243 | case NFA_MOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4244 | case NFA_ZEND: |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4245 | case NFA_SPLIT: |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4246 | case NFA_EMPTY: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4247 | /* These nodes are not added themselves but their "out" and/or |
| 4248 | * "out1" may be added below. */ |
| 4249 | break; |
| 4250 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4251 | case NFA_BOL: |
| 4252 | case NFA_BOF: |
| 4253 | /* "^" won't match past end-of-line, don't bother trying. |
| 4254 | * Except when at the end of the line, or when we are going to the |
| 4255 | * next line for a look-behind match. */ |
| 4256 | if (reginput > regline |
| 4257 | && *reginput != NUL |
| 4258 | && (nfa_endp == NULL |
| 4259 | || !REG_MULTI |
| 4260 | || reglnum == nfa_endp->se_u.pos.lnum)) |
| 4261 | goto skip_add; |
| 4262 | /* FALLTHROUGH */ |
| 4263 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4264 | case NFA_MOPEN1: |
| 4265 | case NFA_MOPEN2: |
| 4266 | case NFA_MOPEN3: |
| 4267 | case NFA_MOPEN4: |
| 4268 | case NFA_MOPEN5: |
| 4269 | case NFA_MOPEN6: |
| 4270 | case NFA_MOPEN7: |
| 4271 | case NFA_MOPEN8: |
| 4272 | case NFA_MOPEN9: |
| 4273 | #ifdef FEAT_SYN_HL |
| 4274 | case NFA_ZOPEN: |
| 4275 | case NFA_ZOPEN1: |
| 4276 | case NFA_ZOPEN2: |
| 4277 | case NFA_ZOPEN3: |
| 4278 | case NFA_ZOPEN4: |
| 4279 | case NFA_ZOPEN5: |
| 4280 | case NFA_ZOPEN6: |
| 4281 | case NFA_ZOPEN7: |
| 4282 | case NFA_ZOPEN8: |
| 4283 | case NFA_ZOPEN9: |
| 4284 | #endif |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4285 | case NFA_NOPEN: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 4286 | case NFA_ZSTART: |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 4287 | /* These nodes need to be added so that we can bail out when it |
| 4288 | * was added to this list before at the same position to avoid an |
| 4289 | * endless loop for "\(\)*" */ |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 4290 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4291 | default: |
Bram Moolenaar | 272fb58 | 2013-11-21 16:03:40 +0100 | [diff] [blame] | 4292 | if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4293 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4294 | /* This state is already in the list, don't add it again, |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4295 | * unless it is an MOPEN that is used for a backreference or |
| 4296 | * when there is a PIM. */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4297 | if (!nfa_has_backref && pim == NULL && !l->has_pim) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4298 | { |
| 4299 | skip_add: |
| 4300 | #ifdef ENABLE_LOG |
| 4301 | nfa_set_code(state->c); |
| 4302 | fprintf(log_fd, "> Not adding state %d to list %d. char %d: %s\n", |
| 4303 | abs(state->id), l->id, state->c, code); |
| 4304 | #endif |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4305 | return subs; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4306 | } |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4307 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4308 | /* Do not add the state again when it exists with the same |
| 4309 | * positions. */ |
Bram Moolenaar | 69b5245 | 2013-07-17 21:10:51 +0200 | [diff] [blame] | 4310 | if (has_state_with_pos(l, state, subs, pim)) |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 4311 | goto skip_add; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4312 | } |
| 4313 | |
Bram Moolenaar | a016912 | 2013-06-26 18:16:58 +0200 | [diff] [blame] | 4314 | /* When there are backreferences or PIMs the number of states may |
| 4315 | * be (a lot) bigger than anticipated. */ |
| 4316 | if (l->n == l->len) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4317 | { |
| 4318 | int newlen = l->len * 3 / 2 + 50; |
| 4319 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4320 | if (subs != &temp_subs) |
| 4321 | { |
| 4322 | /* "subs" may point into the current array, need to make a |
| 4323 | * copy before it becomes invalid. */ |
| 4324 | copy_sub(&temp_subs.norm, &subs->norm); |
| 4325 | #ifdef FEAT_SYN_HL |
| 4326 | if (nfa_has_zsubexpr) |
| 4327 | copy_sub(&temp_subs.synt, &subs->synt); |
| 4328 | #endif |
| 4329 | subs = &temp_subs; |
| 4330 | } |
| 4331 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4332 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 4333 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4334 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4335 | |
| 4336 | /* add the state to the list */ |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4337 | state->lastlist[nfa_ll_index] = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4338 | thread = &l->t[l->n++]; |
| 4339 | thread->state = state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4340 | if (pim == NULL) |
| 4341 | thread->pim.result = NFA_PIM_UNUSED; |
| 4342 | else |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4343 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4344 | copy_pim(&thread->pim, pim); |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 4345 | l->has_pim = TRUE; |
| 4346 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4347 | copy_sub(&thread->subs.norm, &subs->norm); |
| 4348 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4349 | if (nfa_has_zsubexpr) |
| 4350 | copy_sub(&thread->subs.synt, &subs->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4351 | #endif |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4352 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4353 | report_state("Adding", &thread->subs.norm, state, l->id, pim); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4354 | did_print = TRUE; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4355 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4356 | } |
| 4357 | |
| 4358 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 4359 | if (!did_print) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4360 | report_state("Processing", &subs->norm, state, l->id, pim); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4361 | #endif |
| 4362 | switch (state->c) |
| 4363 | { |
| 4364 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4365 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4366 | break; |
| 4367 | |
| 4368 | case NFA_SPLIT: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4369 | /* order matters here */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4370 | subs = addstate(l, state->out, subs, pim, off); |
| 4371 | subs = addstate(l, state->out1, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4372 | break; |
| 4373 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 4374 | case NFA_EMPTY: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4375 | case NFA_NOPEN: |
| 4376 | case NFA_NCLOSE: |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4377 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4378 | break; |
| 4379 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4380 | case NFA_MOPEN: |
| 4381 | case NFA_MOPEN1: |
| 4382 | case NFA_MOPEN2: |
| 4383 | case NFA_MOPEN3: |
| 4384 | case NFA_MOPEN4: |
| 4385 | case NFA_MOPEN5: |
| 4386 | case NFA_MOPEN6: |
| 4387 | case NFA_MOPEN7: |
| 4388 | case NFA_MOPEN8: |
| 4389 | case NFA_MOPEN9: |
| 4390 | #ifdef FEAT_SYN_HL |
| 4391 | case NFA_ZOPEN: |
| 4392 | case NFA_ZOPEN1: |
| 4393 | case NFA_ZOPEN2: |
| 4394 | case NFA_ZOPEN3: |
| 4395 | case NFA_ZOPEN4: |
| 4396 | case NFA_ZOPEN5: |
| 4397 | case NFA_ZOPEN6: |
| 4398 | case NFA_ZOPEN7: |
| 4399 | case NFA_ZOPEN8: |
| 4400 | case NFA_ZOPEN9: |
| 4401 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4402 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4403 | if (state->c == NFA_ZSTART) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4404 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4405 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4406 | sub = &subs->norm; |
| 4407 | } |
| 4408 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4409 | else if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4410 | { |
| 4411 | subidx = state->c - NFA_ZOPEN; |
| 4412 | sub = &subs->synt; |
| 4413 | } |
| 4414 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4415 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4416 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4417 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4418 | sub = &subs->norm; |
| 4419 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4420 | |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4421 | /* avoid compiler warnings */ |
| 4422 | save_ptr = NULL; |
| 4423 | save_lpos.lnum = 0; |
| 4424 | save_lpos.col = 0; |
| 4425 | |
Bram Moolenaar | 927d4a1 | 2013-06-09 17:25:34 +0200 | [diff] [blame] | 4426 | /* Set the position (with "off" added) in the subexpression. Save |
| 4427 | * and restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4428 | if (REG_MULTI) |
| 4429 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4430 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4431 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4432 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4433 | save_in_use = -1; |
| 4434 | } |
| 4435 | else |
| 4436 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4437 | save_in_use = sub->in_use; |
| 4438 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4439 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4440 | sub->list.multi[i].start.lnum = -1; |
| 4441 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4442 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4443 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4444 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4445 | if (off == -1) |
| 4446 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4447 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 4448 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4449 | } |
| 4450 | else |
| 4451 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4452 | sub->list.multi[subidx].start.lnum = reglnum; |
| 4453 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4454 | (colnr_T)(reginput - regline + off); |
| 4455 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4456 | } |
| 4457 | else |
| 4458 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4459 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4460 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4461 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4462 | save_in_use = -1; |
| 4463 | } |
| 4464 | else |
| 4465 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4466 | save_in_use = sub->in_use; |
| 4467 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4468 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4469 | sub->list.line[i].start = NULL; |
| 4470 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4471 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4472 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4473 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4474 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4475 | } |
| 4476 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4477 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4478 | /* "subs" may have changed, need to set "sub" again */ |
| 4479 | #ifdef FEAT_SYN_HL |
| 4480 | if (state->c >= NFA_ZOPEN && state->c <= NFA_ZOPEN9) |
| 4481 | sub = &subs->synt; |
| 4482 | else |
| 4483 | #endif |
| 4484 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4485 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4486 | if (save_in_use == -1) |
| 4487 | { |
| 4488 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4489 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4490 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4491 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4492 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4493 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4494 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4495 | break; |
| 4496 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4497 | case NFA_MCLOSE: |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4498 | if (nfa_has_zend && (REG_MULTI |
| 4499 | ? subs->norm.list.multi[0].end.lnum >= 0 |
| 4500 | : subs->norm.list.line[0].end != NULL)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4501 | { |
Bram Moolenaar | 9be4481 | 2013-09-05 21:15:44 +0200 | [diff] [blame] | 4502 | /* Do not overwrite the position set by \ze. */ |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4503 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4504 | break; |
| 4505 | } |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4506 | case NFA_MCLOSE1: |
| 4507 | case NFA_MCLOSE2: |
| 4508 | case NFA_MCLOSE3: |
| 4509 | case NFA_MCLOSE4: |
| 4510 | case NFA_MCLOSE5: |
| 4511 | case NFA_MCLOSE6: |
| 4512 | case NFA_MCLOSE7: |
| 4513 | case NFA_MCLOSE8: |
| 4514 | case NFA_MCLOSE9: |
| 4515 | #ifdef FEAT_SYN_HL |
| 4516 | case NFA_ZCLOSE: |
| 4517 | case NFA_ZCLOSE1: |
| 4518 | case NFA_ZCLOSE2: |
| 4519 | case NFA_ZCLOSE3: |
| 4520 | case NFA_ZCLOSE4: |
| 4521 | case NFA_ZCLOSE5: |
| 4522 | case NFA_ZCLOSE6: |
| 4523 | case NFA_ZCLOSE7: |
| 4524 | case NFA_ZCLOSE8: |
| 4525 | case NFA_ZCLOSE9: |
| 4526 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4527 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4528 | if (state->c == NFA_ZEND) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4529 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4530 | subidx = 0; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4531 | sub = &subs->norm; |
| 4532 | } |
| 4533 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4534 | else if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4535 | { |
| 4536 | subidx = state->c - NFA_ZCLOSE; |
| 4537 | sub = &subs->synt; |
| 4538 | } |
| 4539 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4540 | else |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4541 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4542 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4543 | sub = &subs->norm; |
| 4544 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4545 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4546 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 4547 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4548 | save_in_use = sub->in_use; |
| 4549 | if (sub->in_use <= subidx) |
| 4550 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4551 | if (REG_MULTI) |
| 4552 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4553 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4554 | if (off == -1) |
| 4555 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4556 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 4557 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4558 | } |
| 4559 | else |
| 4560 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4561 | sub->list.multi[subidx].end.lnum = reglnum; |
| 4562 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4563 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4564 | } |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4565 | /* avoid compiler warnings */ |
| 4566 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4567 | } |
| 4568 | else |
| 4569 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4570 | save_ptr = sub->list.line[subidx].end; |
| 4571 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | de9149e | 2013-07-17 19:22:13 +0200 | [diff] [blame] | 4572 | /* avoid compiler warnings */ |
| 4573 | save_lpos.lnum = 0; |
| 4574 | save_lpos.col = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4575 | } |
| 4576 | |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4577 | subs = addstate(l, state->out, subs, pim, off); |
Bram Moolenaar | ebefd99 | 2013-08-14 14:18:40 +0200 | [diff] [blame] | 4578 | /* "subs" may have changed, need to set "sub" again */ |
| 4579 | #ifdef FEAT_SYN_HL |
| 4580 | if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
| 4581 | sub = &subs->synt; |
| 4582 | else |
| 4583 | #endif |
| 4584 | sub = &subs->norm; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4585 | |
| 4586 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4587 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4588 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4589 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4590 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4591 | break; |
| 4592 | } |
Bram Moolenaar | d05bf56 | 2013-06-30 23:24:08 +0200 | [diff] [blame] | 4593 | return subs; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4594 | } |
| 4595 | |
| 4596 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4597 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 4598 | * Used for zero-width matches, next state to use is the added one. |
| 4599 | * This makes sure the order of states to be tried does not change, which |
| 4600 | * matters for alternatives. |
| 4601 | */ |
| 4602 | static void |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4603 | addstate_here(l, state, subs, pim, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4604 | nfa_list_T *l; /* runtime state list */ |
| 4605 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4606 | regsubs_T *subs; /* pointers to subexpressions */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4607 | nfa_pim_T *pim; /* postponed look-behind match */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4608 | int *ip; |
| 4609 | { |
| 4610 | int tlen = l->n; |
| 4611 | int count; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4612 | int listidx = *ip; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4613 | |
| 4614 | /* 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] | 4615 | addstate(l, state, subs, pim, 0); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4616 | |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4617 | /* when "*ip" was at the end of the list, nothing to do */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4618 | if (listidx + 1 == tlen) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4619 | return; |
| 4620 | |
| 4621 | /* re-order to put the new state at the current position */ |
| 4622 | count = l->n - tlen; |
Bram Moolenaar | a50d02d | 2013-06-16 15:43:50 +0200 | [diff] [blame] | 4623 | if (count == 0) |
| 4624 | return; /* no state got added */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4625 | if (count == 1) |
| 4626 | { |
| 4627 | /* overwrite the current state */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4628 | l->t[listidx] = l->t[l->n - 1]; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4629 | } |
| 4630 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4631 | { |
Bram Moolenaar | 55480dc | 2013-06-30 13:17:24 +0200 | [diff] [blame] | 4632 | if (l->n + count - 1 >= l->len) |
| 4633 | { |
| 4634 | /* not enough space to move the new states, reallocate the list |
| 4635 | * and move the states to the right position */ |
| 4636 | nfa_thread_T *newl; |
| 4637 | |
| 4638 | l->len = l->len * 3 / 2 + 50; |
| 4639 | newl = (nfa_thread_T *)alloc(l->len * sizeof(nfa_thread_T)); |
| 4640 | if (newl == NULL) |
| 4641 | return; |
| 4642 | mch_memmove(&(newl[0]), |
| 4643 | &(l->t[0]), |
| 4644 | sizeof(nfa_thread_T) * listidx); |
| 4645 | mch_memmove(&(newl[listidx]), |
| 4646 | &(l->t[l->n - count]), |
| 4647 | sizeof(nfa_thread_T) * count); |
| 4648 | mch_memmove(&(newl[listidx + count]), |
| 4649 | &(l->t[listidx + 1]), |
| 4650 | sizeof(nfa_thread_T) * (l->n - count - listidx - 1)); |
| 4651 | vim_free(l->t); |
| 4652 | l->t = newl; |
| 4653 | } |
| 4654 | else |
| 4655 | { |
| 4656 | /* make space for new states, then move them from the |
| 4657 | * end to the current position */ |
| 4658 | mch_memmove(&(l->t[listidx + count]), |
| 4659 | &(l->t[listidx + 1]), |
| 4660 | sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
| 4661 | mch_memmove(&(l->t[listidx]), |
| 4662 | &(l->t[l->n - 1]), |
| 4663 | sizeof(nfa_thread_T) * count); |
| 4664 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4665 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4666 | --l->n; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 4667 | *ip = listidx - 1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 4668 | } |
| 4669 | |
| 4670 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4671 | * Check character class "class" against current character c. |
| 4672 | */ |
| 4673 | static int |
| 4674 | check_char_class(class, c) |
| 4675 | int class; |
| 4676 | int c; |
| 4677 | { |
| 4678 | switch (class) |
| 4679 | { |
| 4680 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4681 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4682 | return OK; |
| 4683 | break; |
| 4684 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4685 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4686 | return OK; |
| 4687 | break; |
| 4688 | case NFA_CLASS_BLANK: |
| 4689 | if (c == ' ' || c == '\t') |
| 4690 | return OK; |
| 4691 | break; |
| 4692 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4693 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4694 | return OK; |
| 4695 | break; |
| 4696 | case NFA_CLASS_DIGIT: |
| 4697 | if (VIM_ISDIGIT(c)) |
| 4698 | return OK; |
| 4699 | break; |
| 4700 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4701 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4702 | return OK; |
| 4703 | break; |
| 4704 | case NFA_CLASS_LOWER: |
| 4705 | if (MB_ISLOWER(c)) |
| 4706 | return OK; |
| 4707 | break; |
| 4708 | case NFA_CLASS_PRINT: |
| 4709 | if (vim_isprintc(c)) |
| 4710 | return OK; |
| 4711 | break; |
| 4712 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 4713 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4714 | return OK; |
| 4715 | break; |
| 4716 | case NFA_CLASS_SPACE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4717 | if ((c >= 9 && c <= 13) || (c == ' ')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4718 | return OK; |
| 4719 | break; |
| 4720 | case NFA_CLASS_UPPER: |
| 4721 | if (MB_ISUPPER(c)) |
| 4722 | return OK; |
| 4723 | break; |
| 4724 | case NFA_CLASS_XDIGIT: |
| 4725 | if (vim_isxdigit(c)) |
| 4726 | return OK; |
| 4727 | break; |
| 4728 | case NFA_CLASS_TAB: |
| 4729 | if (c == '\t') |
| 4730 | return OK; |
| 4731 | break; |
| 4732 | case NFA_CLASS_RETURN: |
| 4733 | if (c == '\r') |
| 4734 | return OK; |
| 4735 | break; |
| 4736 | case NFA_CLASS_BACKSPACE: |
| 4737 | if (c == '\b') |
| 4738 | return OK; |
| 4739 | break; |
| 4740 | case NFA_CLASS_ESCAPE: |
| 4741 | if (c == '\033') |
| 4742 | return OK; |
| 4743 | break; |
| 4744 | |
| 4745 | default: |
| 4746 | /* should not be here :P */ |
Bram Moolenaar | 174a848 | 2013-11-28 14:20:17 +0100 | [diff] [blame] | 4747 | EMSGN(_(e_ill_char_class), class); |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 4748 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4749 | } |
| 4750 | return FAIL; |
| 4751 | } |
| 4752 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4753 | /* |
| 4754 | * Check for a match with subexpression "subidx". |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4755 | * Return TRUE if it matches. |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4756 | */ |
| 4757 | static int |
| 4758 | match_backref(sub, subidx, bytelen) |
| 4759 | regsub_T *sub; /* pointers to subexpressions */ |
| 4760 | int subidx; |
| 4761 | int *bytelen; /* out: length of match in bytes */ |
| 4762 | { |
| 4763 | int len; |
| 4764 | |
| 4765 | if (sub->in_use <= subidx) |
| 4766 | { |
| 4767 | retempty: |
| 4768 | /* backref was not set, match an empty string */ |
| 4769 | *bytelen = 0; |
| 4770 | return TRUE; |
| 4771 | } |
| 4772 | |
| 4773 | if (REG_MULTI) |
| 4774 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4775 | if (sub->list.multi[subidx].start.lnum < 0 |
| 4776 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4777 | goto retempty; |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4778 | if (sub->list.multi[subidx].start.lnum == reglnum |
| 4779 | && sub->list.multi[subidx].end.lnum == reglnum) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4780 | { |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 4781 | len = sub->list.multi[subidx].end.col |
| 4782 | - sub->list.multi[subidx].start.col; |
| 4783 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
| 4784 | reginput, &len) == 0) |
| 4785 | { |
| 4786 | *bytelen = len; |
| 4787 | return TRUE; |
| 4788 | } |
| 4789 | } |
| 4790 | else |
| 4791 | { |
| 4792 | if (match_with_backref( |
| 4793 | sub->list.multi[subidx].start.lnum, |
| 4794 | sub->list.multi[subidx].start.col, |
| 4795 | sub->list.multi[subidx].end.lnum, |
| 4796 | sub->list.multi[subidx].end.col, |
| 4797 | bytelen) == RA_MATCH) |
| 4798 | return TRUE; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4799 | } |
| 4800 | } |
| 4801 | else |
| 4802 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4803 | if (sub->list.line[subidx].start == NULL |
| 4804 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4805 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4806 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 4807 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4808 | { |
| 4809 | *bytelen = len; |
| 4810 | return TRUE; |
| 4811 | } |
| 4812 | } |
| 4813 | return FALSE; |
| 4814 | } |
| 4815 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 4816 | #ifdef FEAT_SYN_HL |
| 4817 | |
| 4818 | static int match_zref __ARGS((int subidx, int *bytelen)); |
| 4819 | |
| 4820 | /* |
| 4821 | * Check for a match with \z subexpression "subidx". |
| 4822 | * Return TRUE if it matches. |
| 4823 | */ |
| 4824 | static int |
| 4825 | match_zref(subidx, bytelen) |
| 4826 | int subidx; |
| 4827 | int *bytelen; /* out: length of match in bytes */ |
| 4828 | { |
| 4829 | int len; |
| 4830 | |
| 4831 | cleanup_zsubexpr(); |
| 4832 | if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
| 4833 | { |
| 4834 | /* backref was not set, match an empty string */ |
| 4835 | *bytelen = 0; |
| 4836 | return TRUE; |
| 4837 | } |
| 4838 | |
| 4839 | len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
| 4840 | if (cstrncmp(re_extmatch_in->matches[subidx], reginput, &len) == 0) |
| 4841 | { |
| 4842 | *bytelen = len; |
| 4843 | return TRUE; |
| 4844 | } |
| 4845 | return FALSE; |
| 4846 | } |
| 4847 | #endif |
| 4848 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4849 | /* |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4850 | * Save list IDs for all NFA states of "prog" into "list". |
| 4851 | * Also reset the IDs to zero. |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4852 | * Only used for the recursive value lastlist[1]. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4853 | */ |
| 4854 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4855 | nfa_save_listids(prog, list) |
| 4856 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4857 | int *list; |
| 4858 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4859 | int i; |
| 4860 | nfa_state_T *p; |
| 4861 | |
| 4862 | /* Order in the list is reverse, it's a bit faster that way. */ |
| 4863 | p = &prog->state[0]; |
| 4864 | for (i = prog->nstate; --i >= 0; ) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4865 | { |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4866 | list[i] = p->lastlist[1]; |
| 4867 | p->lastlist[1] = 0; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4868 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4869 | } |
| 4870 | } |
| 4871 | |
| 4872 | /* |
| 4873 | * Restore list IDs from "list" to all NFA states. |
| 4874 | */ |
| 4875 | static void |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4876 | nfa_restore_listids(prog, list) |
| 4877 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4878 | int *list; |
| 4879 | { |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4880 | int i; |
| 4881 | nfa_state_T *p; |
| 4882 | |
| 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 | p->lastlist[1] = list[i]; |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4887 | ++p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4888 | } |
| 4889 | } |
| 4890 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 4891 | static int |
| 4892 | nfa_re_num_cmp(val, op, pos) |
| 4893 | long_u val; |
| 4894 | int op; |
| 4895 | long_u pos; |
| 4896 | { |
| 4897 | if (op == 1) return pos > val; |
| 4898 | if (op == 2) return pos < val; |
| 4899 | return val == pos; |
| 4900 | } |
| 4901 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4902 | static int recursive_regmatch __ARGS((nfa_state_T *state, nfa_pim_T *pim, nfa_regprog_T *prog, regsubs_T *submatch, regsubs_T *m, int **listids)); |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 4903 | static int nfa_regmatch __ARGS((nfa_regprog_T *prog, nfa_state_T *start, regsubs_T *submatch, regsubs_T *m)); |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4904 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4905 | /* |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4906 | * Recursively call nfa_regmatch() |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4907 | * "pim" is NULL or contains info about a Postponed Invisible Match (start |
| 4908 | * position). |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4909 | */ |
| 4910 | static int |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4911 | recursive_regmatch(state, pim, prog, submatch, m, listids) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4912 | nfa_state_T *state; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4913 | nfa_pim_T *pim; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4914 | nfa_regprog_T *prog; |
| 4915 | regsubs_T *submatch; |
| 4916 | regsubs_T *m; |
| 4917 | int **listids; |
| 4918 | { |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 4919 | int save_reginput_col = (int)(reginput - regline); |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4920 | int save_reglnum = reglnum; |
| 4921 | int save_nfa_match = nfa_match; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4922 | int save_nfa_listid = nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4923 | save_se_T *save_nfa_endp = nfa_endp; |
| 4924 | save_se_T endpos; |
| 4925 | save_se_T *endposp = NULL; |
| 4926 | int result; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 4927 | int need_restore = FALSE; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4928 | |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4929 | if (pim != NULL) |
| 4930 | { |
| 4931 | /* start at the position where the postponed match was */ |
| 4932 | if (REG_MULTI) |
| 4933 | reginput = regline + pim->end.pos.col; |
| 4934 | else |
| 4935 | reginput = pim->end.ptr; |
| 4936 | } |
| 4937 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 4938 | if (state->c == NFA_START_INVISIBLE_BEFORE |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 4939 | || state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 4940 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG |
| 4941 | || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4942 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4943 | /* The recursive match must end at the current position. When "pim" is |
| 4944 | * not NULL it specifies the current position. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4945 | endposp = &endpos; |
| 4946 | if (REG_MULTI) |
| 4947 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4948 | if (pim == NULL) |
| 4949 | { |
| 4950 | endpos.se_u.pos.col = (int)(reginput - regline); |
| 4951 | endpos.se_u.pos.lnum = reglnum; |
| 4952 | } |
| 4953 | else |
| 4954 | endpos.se_u.pos = pim->end.pos; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4955 | } |
| 4956 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 4957 | { |
| 4958 | if (pim == NULL) |
| 4959 | endpos.se_u.ptr = reginput; |
| 4960 | else |
| 4961 | endpos.se_u.ptr = pim->end.ptr; |
| 4962 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4963 | |
| 4964 | /* Go back the specified number of bytes, or as far as the |
| 4965 | * start of the previous line, to try matching "\@<=" or |
Bram Moolenaar | 3fb14bc | 2013-07-14 12:34:56 +0200 | [diff] [blame] | 4966 | * not matching "\@<!". This is very inefficient, limit the number of |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 4967 | * bytes if possible. */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 4968 | if (state->val <= 0) |
| 4969 | { |
| 4970 | if (REG_MULTI) |
| 4971 | { |
| 4972 | regline = reg_getline(--reglnum); |
| 4973 | if (regline == NULL) |
| 4974 | /* can't go before the first line */ |
| 4975 | regline = reg_getline(++reglnum); |
| 4976 | } |
| 4977 | reginput = regline; |
| 4978 | } |
| 4979 | else |
| 4980 | { |
| 4981 | if (REG_MULTI && (int)(reginput - regline) < state->val) |
| 4982 | { |
| 4983 | /* Not enough bytes in this line, go to end of |
| 4984 | * previous line. */ |
| 4985 | regline = reg_getline(--reglnum); |
| 4986 | if (regline == NULL) |
| 4987 | { |
| 4988 | /* can't go before the first line */ |
| 4989 | regline = reg_getline(++reglnum); |
| 4990 | reginput = regline; |
| 4991 | } |
| 4992 | else |
| 4993 | reginput = regline + STRLEN(regline); |
| 4994 | } |
| 4995 | if ((int)(reginput - regline) >= state->val) |
| 4996 | { |
| 4997 | reginput -= state->val; |
| 4998 | #ifdef FEAT_MBYTE |
| 4999 | if (has_mbyte) |
| 5000 | reginput -= mb_head_off(regline, reginput); |
| 5001 | #endif |
| 5002 | } |
| 5003 | else |
| 5004 | reginput = regline; |
| 5005 | } |
| 5006 | } |
| 5007 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5008 | #ifdef ENABLE_LOG |
| 5009 | if (log_fd != stderr) |
| 5010 | fclose(log_fd); |
| 5011 | log_fd = NULL; |
| 5012 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5013 | /* Have to clear the lastlist field of the NFA nodes, so that |
| 5014 | * nfa_regmatch() and addstate() can run properly after recursion. */ |
| 5015 | if (nfa_ll_index == 1) |
| 5016 | { |
| 5017 | /* Already calling nfa_regmatch() recursively. Save the lastlist[1] |
| 5018 | * values and clear them. */ |
| 5019 | if (*listids == NULL) |
| 5020 | { |
| 5021 | *listids = (int *)lalloc(sizeof(int) * nstate, TRUE); |
| 5022 | if (*listids == NULL) |
| 5023 | { |
| 5024 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 5025 | return 0; |
| 5026 | } |
| 5027 | } |
| 5028 | nfa_save_listids(prog, *listids); |
| 5029 | need_restore = TRUE; |
| 5030 | /* any value of nfa_listid will do */ |
| 5031 | } |
| 5032 | else |
| 5033 | { |
| 5034 | /* First recursive nfa_regmatch() call, switch to the second lastlist |
| 5035 | * entry. Make sure nfa_listid is different from a previous recursive |
| 5036 | * call, because some states may still have this ID. */ |
| 5037 | ++nfa_ll_index; |
| 5038 | if (nfa_listid <= nfa_alt_listid) |
| 5039 | nfa_listid = nfa_alt_listid; |
| 5040 | } |
| 5041 | |
| 5042 | /* Call nfa_regmatch() to check if the current concat matches at this |
| 5043 | * position. The concat ends with the node NFA_END_INVISIBLE */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5044 | nfa_endp = endposp; |
| 5045 | result = nfa_regmatch(prog, state->out, submatch, m); |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5046 | |
| 5047 | if (need_restore) |
| 5048 | nfa_restore_listids(prog, *listids); |
| 5049 | else |
| 5050 | { |
| 5051 | --nfa_ll_index; |
| 5052 | nfa_alt_listid = nfa_listid; |
| 5053 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5054 | |
| 5055 | /* restore position in input text */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5056 | reglnum = save_reglnum; |
Bram Moolenaar | 484d241 | 2013-06-13 19:47:07 +0200 | [diff] [blame] | 5057 | if (REG_MULTI) |
| 5058 | regline = reg_getline(reglnum); |
Bram Moolenaar | 4c46b5e | 2013-06-13 22:59:30 +0200 | [diff] [blame] | 5059 | reginput = regline + save_reginput_col; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5060 | nfa_match = save_nfa_match; |
| 5061 | nfa_endp = save_nfa_endp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5062 | nfa_listid = save_nfa_listid; |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5063 | |
| 5064 | #ifdef ENABLE_LOG |
| 5065 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
| 5066 | if (log_fd != NULL) |
| 5067 | { |
| 5068 | fprintf(log_fd, "****************************\n"); |
| 5069 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 5070 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 5071 | fprintf(log_fd, "****************************\n"); |
| 5072 | } |
| 5073 | else |
| 5074 | { |
| 5075 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5076 | log_fd = stderr; |
| 5077 | } |
| 5078 | #endif |
| 5079 | |
| 5080 | return result; |
| 5081 | } |
| 5082 | |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5083 | static int skip_to_start __ARGS((int c, colnr_T *colp)); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5084 | static long find_match_text __ARGS((colnr_T startcol, int regstart, char_u *match_text)); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5085 | |
| 5086 | /* |
| 5087 | * Estimate the chance of a match with "state" failing. |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5088 | * empty match: 0 |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5089 | * NFA_ANY: 1 |
| 5090 | * specific character: 99 |
| 5091 | */ |
| 5092 | static int |
| 5093 | failure_chance(state, depth) |
| 5094 | nfa_state_T *state; |
| 5095 | int depth; |
| 5096 | { |
| 5097 | int c = state->c; |
| 5098 | int l, r; |
| 5099 | |
| 5100 | /* detect looping */ |
| 5101 | if (depth > 4) |
| 5102 | return 1; |
| 5103 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5104 | switch (c) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5105 | { |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5106 | case NFA_SPLIT: |
| 5107 | if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
| 5108 | /* avoid recursive stuff */ |
| 5109 | return 1; |
| 5110 | /* two alternatives, use the lowest failure chance */ |
| 5111 | l = failure_chance(state->out, depth + 1); |
| 5112 | r = failure_chance(state->out1, depth + 1); |
| 5113 | return l < r ? l : r; |
| 5114 | |
| 5115 | case NFA_ANY: |
| 5116 | /* matches anything, unlikely to fail */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5117 | return 1; |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5118 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5119 | case NFA_MATCH: |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5120 | case NFA_MCLOSE: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5121 | /* empty match works always */ |
| 5122 | return 0; |
| 5123 | |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5124 | case NFA_START_INVISIBLE: |
| 5125 | case NFA_START_INVISIBLE_FIRST: |
| 5126 | case NFA_START_INVISIBLE_NEG: |
| 5127 | case NFA_START_INVISIBLE_NEG_FIRST: |
| 5128 | case NFA_START_INVISIBLE_BEFORE: |
| 5129 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
| 5130 | case NFA_START_INVISIBLE_BEFORE_NEG: |
| 5131 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
| 5132 | case NFA_START_PATTERN: |
| 5133 | /* recursive regmatch is expensive, use low failure chance */ |
| 5134 | return 5; |
| 5135 | |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5136 | case NFA_BOL: |
| 5137 | case NFA_EOL: |
| 5138 | case NFA_BOF: |
| 5139 | case NFA_EOF: |
| 5140 | case NFA_NEWL: |
| 5141 | return 99; |
| 5142 | |
| 5143 | case NFA_BOW: |
| 5144 | case NFA_EOW: |
| 5145 | return 90; |
| 5146 | |
| 5147 | case NFA_MOPEN: |
| 5148 | case NFA_MOPEN1: |
| 5149 | case NFA_MOPEN2: |
| 5150 | case NFA_MOPEN3: |
| 5151 | case NFA_MOPEN4: |
| 5152 | case NFA_MOPEN5: |
| 5153 | case NFA_MOPEN6: |
| 5154 | case NFA_MOPEN7: |
| 5155 | case NFA_MOPEN8: |
| 5156 | case NFA_MOPEN9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5157 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5158 | case NFA_ZOPEN: |
| 5159 | case NFA_ZOPEN1: |
| 5160 | case NFA_ZOPEN2: |
| 5161 | case NFA_ZOPEN3: |
| 5162 | case NFA_ZOPEN4: |
| 5163 | case NFA_ZOPEN5: |
| 5164 | case NFA_ZOPEN6: |
| 5165 | case NFA_ZOPEN7: |
| 5166 | case NFA_ZOPEN8: |
| 5167 | case NFA_ZOPEN9: |
| 5168 | case NFA_ZCLOSE: |
| 5169 | case NFA_ZCLOSE1: |
| 5170 | case NFA_ZCLOSE2: |
| 5171 | case NFA_ZCLOSE3: |
| 5172 | case NFA_ZCLOSE4: |
| 5173 | case NFA_ZCLOSE5: |
| 5174 | case NFA_ZCLOSE6: |
| 5175 | case NFA_ZCLOSE7: |
| 5176 | case NFA_ZCLOSE8: |
| 5177 | case NFA_ZCLOSE9: |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5178 | #endif |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5179 | case NFA_NOPEN: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5180 | case NFA_MCLOSE1: |
| 5181 | case NFA_MCLOSE2: |
| 5182 | case NFA_MCLOSE3: |
| 5183 | case NFA_MCLOSE4: |
| 5184 | case NFA_MCLOSE5: |
| 5185 | case NFA_MCLOSE6: |
| 5186 | case NFA_MCLOSE7: |
| 5187 | case NFA_MCLOSE8: |
| 5188 | case NFA_MCLOSE9: |
| 5189 | case NFA_NCLOSE: |
| 5190 | return failure_chance(state->out, depth + 1); |
| 5191 | |
| 5192 | case NFA_BACKREF1: |
| 5193 | case NFA_BACKREF2: |
| 5194 | case NFA_BACKREF3: |
| 5195 | case NFA_BACKREF4: |
| 5196 | case NFA_BACKREF5: |
| 5197 | case NFA_BACKREF6: |
| 5198 | case NFA_BACKREF7: |
| 5199 | case NFA_BACKREF8: |
| 5200 | case NFA_BACKREF9: |
| 5201 | #ifdef FEAT_SYN_HL |
| 5202 | case NFA_ZREF1: |
| 5203 | case NFA_ZREF2: |
| 5204 | case NFA_ZREF3: |
| 5205 | case NFA_ZREF4: |
| 5206 | case NFA_ZREF5: |
| 5207 | case NFA_ZREF6: |
| 5208 | case NFA_ZREF7: |
| 5209 | case NFA_ZREF8: |
| 5210 | case NFA_ZREF9: |
| 5211 | #endif |
| 5212 | /* backreferences don't match in many places */ |
| 5213 | return 94; |
| 5214 | |
| 5215 | case NFA_LNUM_GT: |
| 5216 | case NFA_LNUM_LT: |
| 5217 | case NFA_COL_GT: |
| 5218 | case NFA_COL_LT: |
| 5219 | case NFA_VCOL_GT: |
| 5220 | case NFA_VCOL_LT: |
| 5221 | case NFA_MARK_GT: |
| 5222 | case NFA_MARK_LT: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5223 | case NFA_VISUAL: |
Bram Moolenaar | e2b8cb3 | 2013-06-05 11:46:25 +0200 | [diff] [blame] | 5224 | /* before/after positions don't match very often */ |
| 5225 | return 85; |
| 5226 | |
| 5227 | case NFA_LNUM: |
| 5228 | return 90; |
| 5229 | |
| 5230 | case NFA_CURSOR: |
| 5231 | case NFA_COL: |
| 5232 | case NFA_VCOL: |
| 5233 | case NFA_MARK: |
| 5234 | /* specific positions rarely match */ |
| 5235 | return 98; |
| 5236 | |
| 5237 | case NFA_COMPOSING: |
| 5238 | return 95; |
| 5239 | |
| 5240 | default: |
| 5241 | if (c > 0) |
| 5242 | /* character match fails often */ |
| 5243 | return 95; |
| 5244 | } |
| 5245 | |
| 5246 | /* something else, includes character classes */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5247 | return 50; |
| 5248 | } |
| 5249 | |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5250 | /* |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 5251 | * Skip until the char "c" we know a match must start with. |
| 5252 | */ |
| 5253 | static int |
| 5254 | skip_to_start(c, colp) |
| 5255 | int c; |
| 5256 | colnr_T *colp; |
| 5257 | { |
| 5258 | char_u *s; |
| 5259 | |
| 5260 | /* Used often, do some work to avoid call overhead. */ |
| 5261 | if (!ireg_ic |
| 5262 | #ifdef FEAT_MBYTE |
| 5263 | && !has_mbyte |
| 5264 | #endif |
| 5265 | ) |
| 5266 | s = vim_strbyte(regline + *colp, c); |
| 5267 | else |
| 5268 | s = cstrchr(regline + *colp, c); |
| 5269 | if (s == NULL) |
| 5270 | return FAIL; |
| 5271 | *colp = (int)(s - regline); |
| 5272 | return OK; |
| 5273 | } |
| 5274 | |
| 5275 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5276 | * Check for a match with match_text. |
Bram Moolenaar | e7766ee | 2013-06-08 22:30:03 +0200 | [diff] [blame] | 5277 | * Called after skip_to_start() has found regstart. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5278 | * Returns zero for no match, 1 for a match. |
| 5279 | */ |
| 5280 | static long |
| 5281 | find_match_text(startcol, regstart, match_text) |
| 5282 | colnr_T startcol; |
| 5283 | int regstart; |
| 5284 | char_u *match_text; |
| 5285 | { |
| 5286 | colnr_T col = startcol; |
| 5287 | int c1, c2; |
| 5288 | int len1, len2; |
| 5289 | int match; |
| 5290 | |
| 5291 | for (;;) |
| 5292 | { |
| 5293 | match = TRUE; |
| 5294 | len2 = MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5295 | for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
| 5296 | { |
| 5297 | c1 = PTR2CHAR(match_text + len1); |
| 5298 | c2 = PTR2CHAR(regline + col + len2); |
| 5299 | if (c1 != c2 && (!ireg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
| 5300 | { |
| 5301 | match = FALSE; |
| 5302 | break; |
| 5303 | } |
| 5304 | len2 += MB_CHAR2LEN(c2); |
| 5305 | } |
| 5306 | if (match |
| 5307 | #ifdef FEAT_MBYTE |
| 5308 | /* check that no composing char follows */ |
| 5309 | && !(enc_utf8 |
| 5310 | && utf_iscomposing(PTR2CHAR(regline + col + len2))) |
| 5311 | #endif |
| 5312 | ) |
| 5313 | { |
| 5314 | cleanup_subexpr(); |
| 5315 | if (REG_MULTI) |
| 5316 | { |
| 5317 | reg_startpos[0].lnum = reglnum; |
| 5318 | reg_startpos[0].col = col; |
| 5319 | reg_endpos[0].lnum = reglnum; |
| 5320 | reg_endpos[0].col = col + len2; |
| 5321 | } |
| 5322 | else |
| 5323 | { |
| 5324 | reg_startp[0] = regline + col; |
| 5325 | reg_endp[0] = regline + col + len2; |
| 5326 | } |
| 5327 | return 1L; |
| 5328 | } |
| 5329 | |
| 5330 | /* Try finding regstart after the current match. */ |
| 5331 | col += MB_CHAR2LEN(regstart); /* skip regstart */ |
| 5332 | if (skip_to_start(regstart, &col) == FAIL) |
| 5333 | break; |
| 5334 | } |
| 5335 | return 0L; |
| 5336 | } |
| 5337 | |
| 5338 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5339 | * Main matching routine. |
| 5340 | * |
| 5341 | * Run NFA to determine whether it matches reginput. |
| 5342 | * |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 5343 | * 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] | 5344 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5345 | * Return TRUE if there is a match, FALSE otherwise. |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5346 | * When there is a match "submatch" contains the positions. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5347 | * Note: Caller must ensure that: start != NULL. |
| 5348 | */ |
| 5349 | static int |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5350 | nfa_regmatch(prog, start, submatch, m) |
| 5351 | nfa_regprog_T *prog; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5352 | nfa_state_T *start; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5353 | regsubs_T *submatch; |
| 5354 | regsubs_T *m; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5355 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5356 | int result; |
| 5357 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5358 | int flag = 0; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5359 | int go_to_nextline = FALSE; |
| 5360 | nfa_thread_T *t; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5361 | nfa_list_T list[2]; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5362 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5363 | nfa_list_T *thislist; |
| 5364 | nfa_list_T *nextlist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5365 | int *listids = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5366 | nfa_state_T *add_state; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5367 | int add_here; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5368 | int add_count; |
Bram Moolenaar | 4380d1e | 2013-06-09 20:51:00 +0200 | [diff] [blame] | 5369 | int add_off = 0; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5370 | int toplevel = start->c == NFA_MOPEN; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5371 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5372 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5373 | |
| 5374 | if (debug == NULL) |
| 5375 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5376 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5377 | return FALSE; |
| 5378 | } |
| 5379 | #endif |
Bram Moolenaar | 41f1205 | 2013-08-25 17:01:42 +0200 | [diff] [blame] | 5380 | /* Some patterns may take a long time to match, especially when using |
| 5381 | * recursive_regmatch(). Allow interrupting them with CTRL-C. */ |
| 5382 | fast_breakcheck(); |
| 5383 | if (got_int) |
| 5384 | return FALSE; |
| 5385 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5386 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5387 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5388 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 5389 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5390 | list[0].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5391 | list[0].len = nstate + 1; |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5392 | list[1].t = (nfa_thread_T *)lalloc(size, TRUE); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 5393 | list[1].len = nstate + 1; |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5394 | if (list[0].t == NULL || list[1].t == NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5395 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5396 | |
| 5397 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5398 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5399 | if (log_fd != NULL) |
| 5400 | { |
| 5401 | fprintf(log_fd, "**********************************\n"); |
| 5402 | nfa_set_code(start->c); |
| 5403 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 5404 | abs(start->id), code); |
| 5405 | fprintf(log_fd, "**********************************\n"); |
| 5406 | } |
| 5407 | else |
| 5408 | { |
| 5409 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 5410 | log_fd = stderr; |
| 5411 | } |
| 5412 | #endif |
| 5413 | |
| 5414 | thislist = &list[0]; |
| 5415 | thislist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5416 | thislist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5417 | nextlist = &list[1]; |
| 5418 | nextlist->n = 0; |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5419 | nextlist->has_pim = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5420 | #ifdef ENABLE_LOG |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5421 | fprintf(log_fd, "(---) STARTSTATE first\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5422 | #endif |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5423 | thislist->id = nfa_listid + 1; |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5424 | |
| 5425 | /* Inline optimized code for addstate(thislist, start, m, 0) if we know |
| 5426 | * it's the first MOPEN. */ |
| 5427 | if (toplevel) |
| 5428 | { |
| 5429 | if (REG_MULTI) |
| 5430 | { |
| 5431 | m->norm.list.multi[0].start.lnum = reglnum; |
| 5432 | m->norm.list.multi[0].start.col = (colnr_T)(reginput - regline); |
| 5433 | } |
| 5434 | else |
| 5435 | m->norm.list.line[0].start = reginput; |
| 5436 | m->norm.in_use = 1; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5437 | addstate(thislist, start->out, m, NULL, 0); |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 5438 | } |
| 5439 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5440 | addstate(thislist, start, m, NULL, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5441 | |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5442 | #define ADD_STATE_IF_MATCH(state) \ |
| 5443 | if (result) { \ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5444 | add_state = state->out; \ |
| 5445 | add_off = clen; \ |
| 5446 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5447 | |
| 5448 | /* |
| 5449 | * Run for each character. |
| 5450 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5451 | for (;;) |
| 5452 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5453 | int curc; |
| 5454 | int clen; |
| 5455 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5456 | #ifdef FEAT_MBYTE |
| 5457 | if (has_mbyte) |
| 5458 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5459 | curc = (*mb_ptr2char)(reginput); |
| 5460 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5461 | } |
| 5462 | else |
| 5463 | #endif |
| 5464 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5465 | curc = *reginput; |
| 5466 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5467 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5468 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5469 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5470 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5471 | go_to_nextline = FALSE; |
| 5472 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5473 | |
| 5474 | /* swap lists */ |
| 5475 | thislist = &list[flag]; |
| 5476 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 5477 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | 196ed14 | 2013-07-21 18:59:24 +0200 | [diff] [blame] | 5478 | nextlist->has_pim = FALSE; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 5479 | ++nfa_listid; |
| 5480 | thislist->id = nfa_listid; |
| 5481 | nextlist->id = nfa_listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5482 | |
| 5483 | #ifdef ENABLE_LOG |
| 5484 | fprintf(log_fd, "------------------------------------------\n"); |
| 5485 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5486 | 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] | 5487 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5488 | { |
| 5489 | int i; |
| 5490 | |
| 5491 | for (i = 0; i < thislist->n; i++) |
| 5492 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 5493 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5494 | fprintf(log_fd, "\n"); |
| 5495 | #endif |
| 5496 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5497 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5498 | fprintf(debug, "\n-------------------\n"); |
| 5499 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5500 | /* |
| 5501 | * If the state lists are empty we can stop. |
| 5502 | */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5503 | if (thislist->n == 0) |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 5504 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5505 | |
| 5506 | /* compute nextlist */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5507 | for (listidx = 0; listidx < thislist->n; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5508 | { |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5509 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5510 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 5511 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5512 | nfa_set_code(t->state->c); |
| 5513 | fprintf(debug, "%s, ", code); |
| 5514 | #endif |
| 5515 | #ifdef ENABLE_LOG |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5516 | { |
| 5517 | int col; |
| 5518 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5519 | if (t->subs.norm.in_use <= 0) |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5520 | col = -1; |
| 5521 | else if (REG_MULTI) |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5522 | col = t->subs.norm.list.multi[0].start.col; |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5523 | else |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 5524 | col = (int)(t->subs.norm.list.line[0].start - regline); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5525 | nfa_set_code(t->state->c); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5526 | fprintf(log_fd, "(%d) char %d %s (start col %d)%s ... \n", |
| 5527 | abs(t->state->id), (int)t->state->c, code, col, |
| 5528 | pim_info(&t->pim)); |
Bram Moolenaar | 2d5e112 | 2013-05-30 21:42:13 +0200 | [diff] [blame] | 5529 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5530 | #endif |
| 5531 | |
| 5532 | /* |
| 5533 | * Handle the possible codes of the current state. |
| 5534 | * The most important is NFA_MATCH. |
| 5535 | */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5536 | add_state = NULL; |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5537 | add_here = FALSE; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5538 | add_count = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5539 | switch (t->state->c) |
| 5540 | { |
| 5541 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5542 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 5543 | nfa_match = TRUE; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5544 | copy_sub(&submatch->norm, &t->subs.norm); |
| 5545 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 5546 | if (nfa_has_zsubexpr) |
| 5547 | copy_sub(&submatch->synt, &t->subs.synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5548 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5549 | #ifdef ENABLE_LOG |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5550 | log_subsexpr(&t->subs); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5551 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5552 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5553 | * states at this position. When the list of states is going |
| 5554 | * to be empty quit without advancing, so that "reginput" is |
| 5555 | * correct. */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5556 | if (nextlist->n == 0) |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 5557 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5558 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 5559 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5560 | |
| 5561 | case NFA_END_INVISIBLE: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5562 | case NFA_END_INVISIBLE_NEG: |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5563 | case NFA_END_PATTERN: |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5564 | /* |
| 5565 | * This is only encountered after a NFA_START_INVISIBLE or |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5566 | * NFA_START_INVISIBLE_BEFORE node. |
| 5567 | * They surround a zero-width group, used with "\@=", "\&", |
| 5568 | * "\@!", "\@<=" and "\@<!". |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5569 | * If we got here, it means that the current "invisible" group |
| 5570 | * finished successfully, so return control to the parent |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5571 | * nfa_regmatch(). For a look-behind match only when it ends |
| 5572 | * in the position in "nfa_endp". |
| 5573 | * Submatches are stored in *m, and used in the parent call. |
| 5574 | */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5575 | #ifdef ENABLE_LOG |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5576 | if (nfa_endp != NULL) |
| 5577 | { |
| 5578 | if (REG_MULTI) |
| 5579 | fprintf(log_fd, "Current lnum: %d, endp lnum: %d; current col: %d, endp col: %d\n", |
| 5580 | (int)reglnum, |
| 5581 | (int)nfa_endp->se_u.pos.lnum, |
| 5582 | (int)(reginput - regline), |
| 5583 | nfa_endp->se_u.pos.col); |
| 5584 | else |
| 5585 | fprintf(log_fd, "Current col: %d, endp col: %d\n", |
| 5586 | (int)(reginput - regline), |
| 5587 | (int)(nfa_endp->se_u.ptr - reginput)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5588 | } |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5589 | #endif |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5590 | /* If "nfa_endp" is set it's only a match if it ends at |
| 5591 | * "nfa_endp" */ |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5592 | if (nfa_endp != NULL && (REG_MULTI |
| 5593 | ? (reglnum != nfa_endp->se_u.pos.lnum |
| 5594 | || (int)(reginput - regline) |
| 5595 | != nfa_endp->se_u.pos.col) |
| 5596 | : reginput != nfa_endp->se_u.ptr)) |
| 5597 | break; |
| 5598 | |
| 5599 | /* do not set submatches for \@! */ |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5600 | if (t->state->c != NFA_END_INVISIBLE_NEG) |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5601 | { |
| 5602 | copy_sub(&m->norm, &t->subs.norm); |
| 5603 | #ifdef FEAT_SYN_HL |
| 5604 | if (nfa_has_zsubexpr) |
| 5605 | copy_sub(&m->synt, &t->subs.synt); |
| 5606 | #endif |
| 5607 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5608 | #ifdef ENABLE_LOG |
| 5609 | fprintf(log_fd, "Match found:\n"); |
| 5610 | log_subsexpr(m); |
| 5611 | #endif |
Bram Moolenaar | f46da70 | 2013-06-02 22:37:42 +0200 | [diff] [blame] | 5612 | nfa_match = TRUE; |
Bram Moolenaar | 78c93e4 | 2013-09-05 16:05:36 +0200 | [diff] [blame] | 5613 | /* See comment above at "goto nextchar". */ |
| 5614 | if (nextlist->n == 0) |
| 5615 | clen = 0; |
| 5616 | goto nextchar; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5617 | |
| 5618 | case NFA_START_INVISIBLE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5619 | case NFA_START_INVISIBLE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5620 | case NFA_START_INVISIBLE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5621 | case NFA_START_INVISIBLE_NEG_FIRST: |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5622 | case NFA_START_INVISIBLE_BEFORE: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5623 | case NFA_START_INVISIBLE_BEFORE_FIRST: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5624 | case NFA_START_INVISIBLE_BEFORE_NEG: |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5625 | case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5626 | { |
Bram Moolenaar | bcf4d17 | 2013-06-10 16:35:18 +0200 | [diff] [blame] | 5627 | #ifdef ENABLE_LOG |
| 5628 | fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", |
| 5629 | failure_chance(t->state->out, 0), |
| 5630 | failure_chance(t->state->out1->out, 0)); |
Bram Moolenaar | b76591e | 2013-06-04 21:42:22 +0200 | [diff] [blame] | 5631 | #endif |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5632 | /* Do it directly if there already is a PIM or when |
| 5633 | * nfa_postprocess() detected it will work better. */ |
| 5634 | if (t->pim.result != NFA_PIM_UNUSED |
| 5635 | || t->state->c == NFA_START_INVISIBLE_FIRST |
| 5636 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5637 | || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
| 5638 | || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5639 | { |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5640 | int in_use = m->norm.in_use; |
| 5641 | |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5642 | /* Copy submatch info for the recursive call, opposite |
| 5643 | * of what happens on success below. */ |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5644 | copy_sub_off(&m->norm, &t->subs.norm); |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5645 | #ifdef FEAT_SYN_HL |
| 5646 | if (nfa_has_zsubexpr) |
| 5647 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5648 | #endif |
Bram Moolenaar | f86c0b0 | 2013-06-26 12:42:44 +0200 | [diff] [blame] | 5649 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5650 | /* |
| 5651 | * First try matching the invisible match, then what |
| 5652 | * follows. |
| 5653 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5654 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5655 | submatch, m, &listids); |
| 5656 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5657 | /* for \@! and \@<! it is a match when the result is |
| 5658 | * FALSE */ |
| 5659 | if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 5660 | || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 5661 | || t->state->c |
| 5662 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 5663 | || t->state->c |
| 5664 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5665 | { |
| 5666 | /* Copy submatch info from the recursive call */ |
| 5667 | copy_sub_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5668 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5669 | if (nfa_has_zsubexpr) |
| 5670 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 5671 | #endif |
Bram Moolenaar | f211884 | 2013-09-25 18:16:38 +0200 | [diff] [blame] | 5672 | /* If the pattern has \ze and it matched in the |
| 5673 | * sub pattern, use it. */ |
| 5674 | copy_ze_off(&t->subs.norm, &m->norm); |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 5675 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5676 | /* t->state->out1 is the corresponding |
| 5677 | * END_INVISIBLE node; Add its out to the current |
| 5678 | * list (zero-width match). */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5679 | add_here = TRUE; |
| 5680 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5681 | } |
Bram Moolenaar | 4d9ae21 | 2013-06-28 23:04:42 +0200 | [diff] [blame] | 5682 | m->norm.in_use = in_use; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5683 | } |
| 5684 | else |
| 5685 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5686 | nfa_pim_T pim; |
| 5687 | |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5688 | /* |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5689 | * First try matching what follows. Only if a match |
| 5690 | * is found verify the invisible match matches. Add a |
| 5691 | * nfa_pim_T to the following states, it contains info |
| 5692 | * about the invisible match. |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5693 | */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5694 | pim.state = t->state; |
| 5695 | pim.result = NFA_PIM_TODO; |
| 5696 | pim.subs.norm.in_use = 0; |
| 5697 | #ifdef FEAT_SYN_HL |
| 5698 | pim.subs.synt.in_use = 0; |
| 5699 | #endif |
| 5700 | if (REG_MULTI) |
| 5701 | { |
| 5702 | pim.end.pos.col = (int)(reginput - regline); |
| 5703 | pim.end.pos.lnum = reglnum; |
| 5704 | } |
| 5705 | else |
| 5706 | pim.end.ptr = reginput; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5707 | |
| 5708 | /* t->state->out1 is the corresponding END_INVISIBLE |
| 5709 | * node; Add its out to the current list (zero-width |
| 5710 | * match). */ |
| 5711 | addstate_here(thislist, t->state->out1->out, &t->subs, |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5712 | &pim, &listidx); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5713 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5714 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5715 | break; |
| 5716 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5717 | case NFA_START_PATTERN: |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5718 | { |
| 5719 | nfa_state_T *skip = NULL; |
| 5720 | #ifdef ENABLE_LOG |
| 5721 | int skip_lid = 0; |
| 5722 | #endif |
| 5723 | |
| 5724 | /* There is no point in trying to match the pattern if the |
| 5725 | * output state is not going to be added to the list. */ |
| 5726 | if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
| 5727 | { |
| 5728 | skip = t->state->out1->out; |
| 5729 | #ifdef ENABLE_LOG |
| 5730 | skip_lid = nextlist->id; |
| 5731 | #endif |
| 5732 | } |
| 5733 | else if (state_in_list(nextlist, |
| 5734 | t->state->out1->out->out, &t->subs)) |
| 5735 | { |
| 5736 | skip = t->state->out1->out->out; |
| 5737 | #ifdef ENABLE_LOG |
| 5738 | skip_lid = nextlist->id; |
| 5739 | #endif |
| 5740 | } |
Bram Moolenaar | 44c71db | 2013-06-14 22:33:51 +0200 | [diff] [blame] | 5741 | else if (state_in_list(thislist, |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5742 | t->state->out1->out->out, &t->subs)) |
| 5743 | { |
| 5744 | skip = t->state->out1->out->out; |
| 5745 | #ifdef ENABLE_LOG |
| 5746 | skip_lid = thislist->id; |
| 5747 | #endif |
| 5748 | } |
| 5749 | if (skip != NULL) |
| 5750 | { |
| 5751 | #ifdef ENABLE_LOG |
| 5752 | nfa_set_code(skip->c); |
| 5753 | fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
| 5754 | abs(skip->id), skip_lid, skip->c, code); |
| 5755 | #endif |
| 5756 | break; |
| 5757 | } |
Bram Moolenaar | 699c120 | 2013-09-25 16:41:54 +0200 | [diff] [blame] | 5758 | /* Copy submatch info to the recursive call, opposite of what |
| 5759 | * happens afterwards. */ |
| 5760 | copy_sub_off(&m->norm, &t->subs.norm); |
| 5761 | #ifdef FEAT_SYN_HL |
| 5762 | if (nfa_has_zsubexpr) |
| 5763 | copy_sub_off(&m->synt, &t->subs.synt); |
| 5764 | #endif |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5765 | |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5766 | /* First try matching the pattern. */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 5767 | result = recursive_regmatch(t->state, NULL, prog, |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5768 | submatch, m, &listids); |
| 5769 | if (result) |
| 5770 | { |
| 5771 | int bytelen; |
| 5772 | |
| 5773 | #ifdef ENABLE_LOG |
| 5774 | fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
| 5775 | log_subsexpr(m); |
| 5776 | #endif |
| 5777 | /* Copy submatch info from the recursive call */ |
| 5778 | copy_sub_off(&t->subs.norm, &m->norm); |
| 5779 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 5780 | if (nfa_has_zsubexpr) |
| 5781 | copy_sub_off(&t->subs.synt, &m->synt); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5782 | #endif |
| 5783 | /* Now we need to skip over the matched text and then |
| 5784 | * continue with what follows. */ |
| 5785 | if (REG_MULTI) |
| 5786 | /* TODO: multi-line match */ |
| 5787 | bytelen = m->norm.list.multi[0].end.col |
| 5788 | - (int)(reginput - regline); |
| 5789 | else |
| 5790 | bytelen = (int)(m->norm.list.line[0].end - reginput); |
| 5791 | |
| 5792 | #ifdef ENABLE_LOG |
| 5793 | fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
| 5794 | #endif |
| 5795 | if (bytelen == 0) |
| 5796 | { |
| 5797 | /* empty match, output of corresponding |
| 5798 | * NFA_END_PATTERN/NFA_SKIP to be used at current |
| 5799 | * position */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5800 | add_here = TRUE; |
| 5801 | add_state = t->state->out1->out->out; |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5802 | } |
| 5803 | else if (bytelen <= clen) |
| 5804 | { |
| 5805 | /* match current character, output of corresponding |
| 5806 | * NFA_END_PATTERN to be used at next position. */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5807 | add_state = t->state->out1->out->out; |
| 5808 | add_off = clen; |
| 5809 | } |
| 5810 | else |
| 5811 | { |
| 5812 | /* skip over the matched characters, set character |
| 5813 | * count in NFA_SKIP */ |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5814 | add_state = t->state->out1->out; |
| 5815 | add_off = bytelen; |
| 5816 | add_count = bytelen - clen; |
| 5817 | } |
| 5818 | } |
| 5819 | break; |
Bram Moolenaar | 43e0298 | 2013-06-07 17:31:29 +0200 | [diff] [blame] | 5820 | } |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 5821 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5822 | case NFA_BOL: |
| 5823 | if (reginput == regline) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5824 | { |
| 5825 | add_here = TRUE; |
| 5826 | add_state = t->state->out; |
| 5827 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5828 | break; |
| 5829 | |
| 5830 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5831 | if (curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5832 | { |
| 5833 | add_here = TRUE; |
| 5834 | add_state = t->state->out; |
| 5835 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5836 | break; |
| 5837 | |
| 5838 | case NFA_BOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5839 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5840 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5841 | if (curc == NUL) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5842 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5843 | #ifdef FEAT_MBYTE |
| 5844 | else if (has_mbyte) |
| 5845 | { |
| 5846 | int this_class; |
| 5847 | |
| 5848 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5849 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5850 | if (this_class <= 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5851 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5852 | else if (reg_prev_class() == this_class) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5853 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5854 | } |
| 5855 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5856 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5857 | || (reginput > regline |
| 5858 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5859 | result = FALSE; |
| 5860 | if (result) |
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; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5866 | |
| 5867 | case NFA_EOW: |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5868 | result = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5869 | if (reginput == regline) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5870 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5871 | #ifdef FEAT_MBYTE |
| 5872 | else if (has_mbyte) |
| 5873 | { |
| 5874 | int this_class, prev_class; |
| 5875 | |
| 5876 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5877 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5878 | prev_class = reg_prev_class(); |
| 5879 | if (this_class == prev_class |
| 5880 | || prev_class == 0 || prev_class == 1) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5881 | result = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5882 | } |
| 5883 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 5884 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5885 | || (reginput[0] != NUL |
| 5886 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 5887 | result = FALSE; |
| 5888 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5889 | { |
| 5890 | add_here = TRUE; |
| 5891 | add_state = t->state->out; |
| 5892 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5893 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5894 | |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5895 | case NFA_BOF: |
| 5896 | if (reglnum == 0 && reginput == regline |
| 5897 | && (!REG_MULTI || reg_firstlnum == 1)) |
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 | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5902 | break; |
| 5903 | |
| 5904 | case NFA_EOF: |
| 5905 | if (reglnum == reg_maxline && curc == NUL) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 5906 | { |
| 5907 | add_here = TRUE; |
| 5908 | add_state = t->state->out; |
| 5909 | } |
Bram Moolenaar | 4b78063 | 2013-05-31 22:14:52 +0200 | [diff] [blame] | 5910 | break; |
| 5911 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5912 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5913 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5914 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5915 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 5916 | int len = 0; |
| 5917 | nfa_state_T *end; |
| 5918 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5919 | int cchars[MAX_MCO]; |
| 5920 | int ccount = 0; |
| 5921 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5922 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5923 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5924 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5925 | if (utf_iscomposing(sta->c)) |
| 5926 | { |
| 5927 | /* Only match composing character(s), ignore base |
| 5928 | * character. Used for ".{composing}" and "{composing}" |
| 5929 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5930 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5931 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 5932 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5933 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 5934 | /* If \Z was present, then ignore composing characters. |
| 5935 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5936 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5937 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5938 | else |
| 5939 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5940 | while (sta->c != NFA_END_COMPOSING) |
| 5941 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5942 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5943 | |
| 5944 | /* Check base character matches first, unless ignored. */ |
| 5945 | else if (len > 0 || mc == sta->c) |
| 5946 | { |
| 5947 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5948 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 5949 | len += mb_char2len(mc); |
| 5950 | sta = sta->out; |
| 5951 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5952 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5953 | /* We don't care about the order of composing characters. |
| 5954 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 5955 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5956 | { |
| 5957 | mc = mb_ptr2char(reginput + len); |
| 5958 | cchars[ccount++] = mc; |
| 5959 | len += mb_char2len(mc); |
| 5960 | if (ccount == MAX_MCO) |
| 5961 | break; |
| 5962 | } |
| 5963 | |
| 5964 | /* Check that each composing char in the pattern matches a |
| 5965 | * composing char in the text. We do not check if all |
| 5966 | * composing chars are matched. */ |
| 5967 | result = OK; |
| 5968 | while (sta->c != NFA_END_COMPOSING) |
| 5969 | { |
| 5970 | for (j = 0; j < ccount; ++j) |
| 5971 | if (cchars[j] == sta->c) |
| 5972 | break; |
| 5973 | if (j == ccount) |
| 5974 | { |
| 5975 | result = FAIL; |
| 5976 | break; |
| 5977 | } |
| 5978 | sta = sta->out; |
| 5979 | } |
| 5980 | } |
| 5981 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 5982 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 5983 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5984 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 5985 | ADD_STATE_IF_MATCH(end); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5986 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 5987 | } |
| 5988 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5989 | |
| 5990 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 5991 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 5992 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5993 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 5994 | go_to_nextline = TRUE; |
| 5995 | /* Pass -1 for the offset, which means taking the position |
| 5996 | * at the start of the next line. */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 5997 | add_state = t->state->out; |
| 5998 | add_off = -1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 5999 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6000 | else if (curc == '\n' && reg_line_lbr) |
| 6001 | { |
| 6002 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6003 | add_state = t->state->out; |
| 6004 | add_off = 1; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6005 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6006 | break; |
| 6007 | |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6008 | case NFA_START_COLL: |
| 6009 | case NFA_START_NEG_COLL: |
| 6010 | { |
| 6011 | /* What follows is a list of characters, until NFA_END_COLL. |
| 6012 | * One of them must match or none of them must match. */ |
| 6013 | nfa_state_T *state; |
| 6014 | int result_if_matched; |
| 6015 | int c1, c2; |
| 6016 | |
| 6017 | /* Never match EOL. If it's part of the collection it is added |
| 6018 | * as a separate state with an OR. */ |
| 6019 | if (curc == NUL) |
| 6020 | break; |
| 6021 | |
| 6022 | state = t->state->out; |
| 6023 | result_if_matched = (t->state->c == NFA_START_COLL); |
| 6024 | for (;;) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6025 | { |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6026 | if (state->c == NFA_END_COLL) |
| 6027 | { |
| 6028 | result = !result_if_matched; |
| 6029 | break; |
| 6030 | } |
| 6031 | if (state->c == NFA_RANGE_MIN) |
| 6032 | { |
| 6033 | c1 = state->val; |
| 6034 | state = state->out; /* advance to NFA_RANGE_MAX */ |
| 6035 | c2 = state->val; |
| 6036 | #ifdef ENABLE_LOG |
| 6037 | fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
| 6038 | curc, c1, c2); |
| 6039 | #endif |
| 6040 | if (curc >= c1 && curc <= c2) |
| 6041 | { |
| 6042 | result = result_if_matched; |
| 6043 | break; |
| 6044 | } |
| 6045 | if (ireg_ic) |
| 6046 | { |
| 6047 | int curc_low = MB_TOLOWER(curc); |
| 6048 | int done = FALSE; |
| 6049 | |
| 6050 | for ( ; c1 <= c2; ++c1) |
| 6051 | if (MB_TOLOWER(c1) == curc_low) |
| 6052 | { |
| 6053 | result = result_if_matched; |
| 6054 | done = TRUE; |
| 6055 | break; |
| 6056 | } |
| 6057 | if (done) |
| 6058 | break; |
| 6059 | } |
| 6060 | } |
| 6061 | else if (state->c < 0 ? check_char_class(state->c, curc) |
| 6062 | : (curc == state->c |
| 6063 | || (ireg_ic && MB_TOLOWER(curc) |
| 6064 | == MB_TOLOWER(state->c)))) |
| 6065 | { |
| 6066 | result = result_if_matched; |
| 6067 | break; |
| 6068 | } |
| 6069 | state = state->out; |
| 6070 | } |
| 6071 | if (result) |
| 6072 | { |
| 6073 | /* next state is in out of the NFA_END_COLL, out1 of |
| 6074 | * START points to the END state */ |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6075 | add_state = t->state->out1->out; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6076 | add_off = clen; |
| 6077 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6078 | break; |
Bram Moolenaar | 417bad2 | 2013-06-07 14:08:30 +0200 | [diff] [blame] | 6079 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6080 | |
| 6081 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6082 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6083 | if (curc > 0) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6084 | { |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6085 | add_state = t->state->out; |
| 6086 | add_off = clen; |
| 6087 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6088 | break; |
| 6089 | |
| 6090 | /* |
| 6091 | * Character classes like \a for alpha, \d for digit etc. |
| 6092 | */ |
| 6093 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6094 | result = vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6095 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6096 | break; |
| 6097 | |
| 6098 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6099 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6100 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6101 | break; |
| 6102 | |
| 6103 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 6104 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6105 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6106 | break; |
| 6107 | |
| 6108 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6109 | result = !VIM_ISDIGIT(curc) |
| 6110 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6111 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6112 | break; |
| 6113 | |
| 6114 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6115 | result = vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6116 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6117 | break; |
| 6118 | |
| 6119 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6120 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6121 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6122 | break; |
| 6123 | |
| 6124 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6125 | result = vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6126 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6127 | break; |
| 6128 | |
| 6129 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 6130 | result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(reginput)); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6131 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6132 | break; |
| 6133 | |
| 6134 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6135 | result = vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6136 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6137 | break; |
| 6138 | |
| 6139 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6140 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6141 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6142 | break; |
| 6143 | |
| 6144 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6145 | result = ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6146 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6147 | break; |
| 6148 | |
| 6149 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6150 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6151 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6152 | break; |
| 6153 | |
| 6154 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6155 | result = ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6156 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6157 | break; |
| 6158 | |
| 6159 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6160 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6161 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6162 | break; |
| 6163 | |
| 6164 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6165 | result = ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6166 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6167 | break; |
| 6168 | |
| 6169 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6170 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6171 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6172 | break; |
| 6173 | |
| 6174 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6175 | result = ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6176 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6177 | break; |
| 6178 | |
| 6179 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6180 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6181 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6182 | break; |
| 6183 | |
| 6184 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6185 | result = ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6186 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6187 | break; |
| 6188 | |
| 6189 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6190 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6191 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6192 | break; |
| 6193 | |
| 6194 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6195 | result = ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6196 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6197 | break; |
| 6198 | |
| 6199 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6200 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6201 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6202 | break; |
| 6203 | |
| 6204 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6205 | result = ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6206 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6207 | break; |
| 6208 | |
| 6209 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6210 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6211 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6212 | break; |
| 6213 | |
| 6214 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6215 | result = ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6216 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6217 | break; |
| 6218 | |
| 6219 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6220 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6221 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6222 | break; |
| 6223 | |
Bram Moolenaar | 1cfad52 | 2013-08-14 12:06:49 +0200 | [diff] [blame] | 6224 | case NFA_LOWER_IC: /* [a-z] */ |
| 6225 | result = ri_lower(curc) || (ireg_ic && ri_upper(curc)); |
| 6226 | ADD_STATE_IF_MATCH(t->state); |
| 6227 | break; |
| 6228 | |
| 6229 | case NFA_NLOWER_IC: /* [^a-z] */ |
| 6230 | result = curc != NUL |
| 6231 | && !(ri_lower(curc) || (ireg_ic && ri_upper(curc))); |
| 6232 | ADD_STATE_IF_MATCH(t->state); |
| 6233 | break; |
| 6234 | |
| 6235 | case NFA_UPPER_IC: /* [A-Z] */ |
| 6236 | result = ri_upper(curc) || (ireg_ic && ri_lower(curc)); |
| 6237 | ADD_STATE_IF_MATCH(t->state); |
| 6238 | break; |
| 6239 | |
| 6240 | case NFA_NUPPER_IC: /* ^[A-Z] */ |
| 6241 | result = curc != NUL |
| 6242 | && !(ri_upper(curc) || (ireg_ic && ri_lower(curc))); |
| 6243 | ADD_STATE_IF_MATCH(t->state); |
| 6244 | break; |
| 6245 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6246 | case NFA_BACKREF1: |
| 6247 | case NFA_BACKREF2: |
| 6248 | case NFA_BACKREF3: |
| 6249 | case NFA_BACKREF4: |
| 6250 | case NFA_BACKREF5: |
| 6251 | case NFA_BACKREF6: |
| 6252 | case NFA_BACKREF7: |
| 6253 | case NFA_BACKREF8: |
| 6254 | case NFA_BACKREF9: |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6255 | #ifdef FEAT_SYN_HL |
| 6256 | case NFA_ZREF1: |
| 6257 | case NFA_ZREF2: |
| 6258 | case NFA_ZREF3: |
| 6259 | case NFA_ZREF4: |
| 6260 | case NFA_ZREF5: |
| 6261 | case NFA_ZREF6: |
| 6262 | case NFA_ZREF7: |
| 6263 | case NFA_ZREF8: |
| 6264 | case NFA_ZREF9: |
| 6265 | #endif |
| 6266 | /* \1 .. \9 \z1 .. \z9 */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6267 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6268 | int subidx; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6269 | int bytelen; |
| 6270 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6271 | if (t->state->c <= NFA_BACKREF9) |
| 6272 | { |
| 6273 | subidx = t->state->c - NFA_BACKREF1 + 1; |
| 6274 | result = match_backref(&t->subs.norm, subidx, &bytelen); |
| 6275 | } |
| 6276 | #ifdef FEAT_SYN_HL |
| 6277 | else |
| 6278 | { |
| 6279 | subidx = t->state->c - NFA_ZREF1 + 1; |
| 6280 | result = match_zref(subidx, &bytelen); |
| 6281 | } |
| 6282 | #endif |
| 6283 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6284 | if (result) |
| 6285 | { |
| 6286 | if (bytelen == 0) |
| 6287 | { |
Bram Moolenaar | b122e97 | 2013-06-02 16:07:10 +0200 | [diff] [blame] | 6288 | /* empty match always works, output of NFA_SKIP to be |
| 6289 | * used next */ |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6290 | add_here = TRUE; |
| 6291 | add_state = t->state->out->out; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6292 | } |
| 6293 | else if (bytelen <= clen) |
| 6294 | { |
| 6295 | /* match current character, jump ahead to out of |
| 6296 | * NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6297 | add_state = t->state->out->out; |
| 6298 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6299 | } |
| 6300 | else |
| 6301 | { |
Bram Moolenaar | f811509 | 2013-06-04 17:47:05 +0200 | [diff] [blame] | 6302 | /* skip over the matched characters, set character |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6303 | * count in NFA_SKIP */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6304 | add_state = t->state->out; |
| 6305 | add_off = bytelen; |
| 6306 | add_count = bytelen - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6307 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6308 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6309 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6310 | } |
| 6311 | case NFA_SKIP: |
Bram Moolenaar | 67604ae | 2013-06-05 16:51:57 +0200 | [diff] [blame] | 6312 | /* character of previous matching \1 .. \9 or \@> */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6313 | if (t->count - clen <= 0) |
| 6314 | { |
| 6315 | /* end of match, go to what follows */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6316 | add_state = t->state->out; |
| 6317 | add_off = clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6318 | } |
| 6319 | else |
| 6320 | { |
| 6321 | /* add state again with decremented count */ |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6322 | add_state = t->state; |
| 6323 | add_off = 0; |
| 6324 | add_count = t->count - clen; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 6325 | } |
| 6326 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6327 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6328 | case NFA_LNUM: |
| 6329 | case NFA_LNUM_GT: |
| 6330 | case NFA_LNUM_LT: |
| 6331 | result = (REG_MULTI && |
| 6332 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 6333 | (long_u)(reglnum + reg_firstlnum))); |
| 6334 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6335 | { |
| 6336 | add_here = TRUE; |
| 6337 | add_state = t->state->out; |
| 6338 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6339 | break; |
| 6340 | |
| 6341 | case NFA_COL: |
| 6342 | case NFA_COL_GT: |
| 6343 | case NFA_COL_LT: |
| 6344 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 6345 | (long_u)(reginput - regline) + 1); |
| 6346 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6347 | { |
| 6348 | add_here = TRUE; |
| 6349 | add_state = t->state->out; |
| 6350 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6351 | break; |
| 6352 | |
| 6353 | case NFA_VCOL: |
| 6354 | case NFA_VCOL_GT: |
| 6355 | case NFA_VCOL_LT: |
| 6356 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 6357 | (long_u)win_linetabsize( |
| 6358 | reg_win == NULL ? curwin : reg_win, |
| 6359 | regline, (colnr_T)(reginput - regline)) + 1); |
| 6360 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6361 | { |
| 6362 | add_here = TRUE; |
| 6363 | add_state = t->state->out; |
| 6364 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6365 | break; |
| 6366 | |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6367 | case NFA_MARK: |
| 6368 | case NFA_MARK_GT: |
| 6369 | case NFA_MARK_LT: |
| 6370 | { |
| 6371 | pos_T *pos = getmark_buf(reg_buf, t->state->val, FALSE); |
| 6372 | |
| 6373 | /* Compare the mark position to the match position. */ |
| 6374 | result = (pos != NULL /* mark doesn't exist */ |
| 6375 | && pos->lnum > 0 /* mark isn't set in reg_buf */ |
| 6376 | && (pos->lnum == reglnum + reg_firstlnum |
| 6377 | ? (pos->col == (colnr_T)(reginput - regline) |
| 6378 | ? t->state->c == NFA_MARK |
| 6379 | : (pos->col < (colnr_T)(reginput - regline) |
| 6380 | ? t->state->c == NFA_MARK_GT |
| 6381 | : t->state->c == NFA_MARK_LT)) |
| 6382 | : (pos->lnum < reglnum + reg_firstlnum |
| 6383 | ? t->state->c == NFA_MARK_GT |
| 6384 | : t->state->c == NFA_MARK_LT))); |
| 6385 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6386 | { |
| 6387 | add_here = TRUE; |
| 6388 | add_state = t->state->out; |
| 6389 | } |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 6390 | break; |
| 6391 | } |
| 6392 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6393 | case NFA_CURSOR: |
| 6394 | result = (reg_win != NULL |
| 6395 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 6396 | && ((colnr_T)(reginput - regline) |
| 6397 | == reg_win->w_cursor.col)); |
| 6398 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6399 | { |
| 6400 | add_here = TRUE; |
| 6401 | add_state = t->state->out; |
| 6402 | } |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 6403 | break; |
| 6404 | |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6405 | case NFA_VISUAL: |
| 6406 | result = reg_match_visual(); |
| 6407 | if (result) |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6408 | { |
| 6409 | add_here = TRUE; |
| 6410 | add_state = t->state->out; |
| 6411 | } |
Bram Moolenaar | 973fced | 2013-06-05 21:10:59 +0200 | [diff] [blame] | 6412 | break; |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 6413 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6414 | case NFA_MOPEN1: |
| 6415 | case NFA_MOPEN2: |
| 6416 | case NFA_MOPEN3: |
| 6417 | case NFA_MOPEN4: |
| 6418 | case NFA_MOPEN5: |
| 6419 | case NFA_MOPEN6: |
| 6420 | case NFA_MOPEN7: |
| 6421 | case NFA_MOPEN8: |
| 6422 | case NFA_MOPEN9: |
| 6423 | #ifdef FEAT_SYN_HL |
| 6424 | case NFA_ZOPEN: |
| 6425 | case NFA_ZOPEN1: |
| 6426 | case NFA_ZOPEN2: |
| 6427 | case NFA_ZOPEN3: |
| 6428 | case NFA_ZOPEN4: |
| 6429 | case NFA_ZOPEN5: |
| 6430 | case NFA_ZOPEN6: |
| 6431 | case NFA_ZOPEN7: |
| 6432 | case NFA_ZOPEN8: |
| 6433 | case NFA_ZOPEN9: |
| 6434 | #endif |
| 6435 | case NFA_NOPEN: |
| 6436 | case NFA_ZSTART: |
| 6437 | /* These states are only added to be able to bail out when |
| 6438 | * they are added again, nothing is to be done. */ |
| 6439 | break; |
| 6440 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6441 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6442 | { |
| 6443 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 6444 | |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6445 | #ifdef DEBUG |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6446 | if (c < 0) |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6447 | EMSGN("INTERNAL: Negative state char: %ld", c); |
Bram Moolenaar | 398d53d | 2013-08-01 15:45:52 +0200 | [diff] [blame] | 6448 | #endif |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6449 | result = (c == curc); |
| 6450 | |
| 6451 | if (!result && ireg_ic) |
| 6452 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6453 | #ifdef FEAT_MBYTE |
| 6454 | /* If there is a composing character which is not being |
| 6455 | * ignored there can be no match. Match with composing |
| 6456 | * character uses NFA_COMPOSING above. */ |
| 6457 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6458 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 6459 | result = FALSE; |
| 6460 | #endif |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6461 | ADD_STATE_IF_MATCH(t->state); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6462 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 6463 | } |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6464 | |
| 6465 | } /* switch (t->state->c) */ |
| 6466 | |
| 6467 | if (add_state != NULL) |
| 6468 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6469 | nfa_pim_T *pim; |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6470 | nfa_pim_T pim_copy; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6471 | |
| 6472 | if (t->pim.result == NFA_PIM_UNUSED) |
| 6473 | pim = NULL; |
| 6474 | else |
| 6475 | pim = &t->pim; |
| 6476 | |
| 6477 | /* Handle the postponed invisible match if the match might end |
| 6478 | * without advancing and before the end of the line. */ |
| 6479 | if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6480 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6481 | if (pim->result == NFA_PIM_TODO) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6482 | { |
| 6483 | #ifdef ENABLE_LOG |
| 6484 | fprintf(log_fd, "\n"); |
| 6485 | fprintf(log_fd, "==================================\n"); |
| 6486 | fprintf(log_fd, "Postponed recursive nfa_regmatch()\n"); |
| 6487 | fprintf(log_fd, "\n"); |
| 6488 | #endif |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6489 | result = recursive_regmatch(pim->state, pim, |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6490 | prog, submatch, m, &listids); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6491 | pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6492 | /* for \@! and \@<! it is a match when the result is |
| 6493 | * FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6494 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6495 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6496 | || pim->state->c |
| 6497 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6498 | || pim->state->c |
| 6499 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6500 | { |
| 6501 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6502 | copy_sub_off(&pim->subs.norm, &m->norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6503 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6504 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6505 | copy_sub_off(&pim->subs.synt, &m->synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6506 | #endif |
| 6507 | } |
| 6508 | } |
| 6509 | else |
| 6510 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6511 | result = (pim->result == NFA_PIM_MATCH); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6512 | #ifdef ENABLE_LOG |
| 6513 | fprintf(log_fd, "\n"); |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6514 | 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] | 6515 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 6516 | fprintf(log_fd, "\n"); |
| 6517 | #endif |
| 6518 | } |
| 6519 | |
Bram Moolenaar | decd954 | 2013-06-07 16:31:50 +0200 | [diff] [blame] | 6520 | /* for \@! and \@<! it is a match when result is FALSE */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6521 | if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 6522 | || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
| 6523 | || pim->state->c |
| 6524 | == NFA_START_INVISIBLE_BEFORE_NEG |
| 6525 | || pim->state->c |
| 6526 | == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6527 | { |
| 6528 | /* Copy submatch info from the recursive call */ |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6529 | copy_sub_off(&t->subs.norm, &pim->subs.norm); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6530 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 188c57b | 2013-06-06 16:22:06 +0200 | [diff] [blame] | 6531 | if (nfa_has_zsubexpr) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6532 | copy_sub_off(&t->subs.synt, &pim->subs.synt); |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6533 | #endif |
| 6534 | } |
| 6535 | else |
| 6536 | /* look-behind match failed, don't add the state */ |
| 6537 | continue; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6538 | |
| 6539 | /* Postponed invisible match was handled, don't add it to |
| 6540 | * following states. */ |
| 6541 | pim = NULL; |
Bram Moolenaar | a2d9510 | 2013-06-04 14:23:05 +0200 | [diff] [blame] | 6542 | } |
| 6543 | |
Bram Moolenaar | a951e35 | 2013-10-06 15:46:11 +0200 | [diff] [blame] | 6544 | /* If "pim" points into l->t it will become invalid when |
| 6545 | * adding the state causes the list to be reallocated. Make a |
| 6546 | * local copy to avoid that. */ |
| 6547 | if (pim == &t->pim) |
| 6548 | { |
| 6549 | copy_pim(&pim_copy, pim); |
| 6550 | pim = &pim_copy; |
| 6551 | } |
| 6552 | |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6553 | if (add_here) |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6554 | addstate_here(thislist, add_state, &t->subs, pim, &listidx); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6555 | else |
| 6556 | { |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6557 | addstate(nextlist, add_state, &t->subs, pim, add_off); |
Bram Moolenaar | b1b284f | 2013-06-08 13:33:37 +0200 | [diff] [blame] | 6558 | if (add_count > 0) |
| 6559 | nextlist->t[nextlist->n - 1].count = add_count; |
| 6560 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6561 | } |
| 6562 | |
| 6563 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 6564 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6565 | /* Look for the start of a match in the current position by adding the |
| 6566 | * start state to the list of states. |
| 6567 | * The first found match is the leftmost one, thus the order of states |
| 6568 | * matters! |
| 6569 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 6570 | * because recursive calls should only start in the first position. |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6571 | * Unless "nfa_endp" is not NULL, then we match the end position. |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 6572 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6573 | if (nfa_match == FALSE |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6574 | && ((toplevel |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6575 | && reglnum == 0 |
| 6576 | && clen != 0 |
| 6577 | && (ireg_maxcol == 0 |
| 6578 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6579 | || (nfa_endp != NULL |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6580 | && (REG_MULTI |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6581 | ? (reglnum < nfa_endp->se_u.pos.lnum |
| 6582 | || (reglnum == nfa_endp->se_u.pos.lnum |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 6583 | && (int)(reginput - regline) |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6584 | < nfa_endp->se_u.pos.col)) |
| 6585 | : reginput < nfa_endp->se_u.ptr)))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6586 | { |
| 6587 | #ifdef ENABLE_LOG |
| 6588 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 6589 | #endif |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6590 | /* Inline optimized code for addstate() if we know the state is |
| 6591 | * the first MOPEN. */ |
| 6592 | if (toplevel) |
| 6593 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6594 | int add = TRUE; |
| 6595 | int c; |
| 6596 | |
| 6597 | if (prog->regstart != NUL && clen != 0) |
| 6598 | { |
| 6599 | if (nextlist->n == 0) |
| 6600 | { |
| 6601 | colnr_T col = (colnr_T)(reginput - regline) + clen; |
| 6602 | |
| 6603 | /* Nextlist is empty, we can skip ahead to the |
| 6604 | * character that must appear at the start. */ |
| 6605 | if (skip_to_start(prog->regstart, &col) == FAIL) |
| 6606 | break; |
| 6607 | #ifdef ENABLE_LOG |
| 6608 | fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
| 6609 | col - ((colnr_T)(reginput - regline) + clen)); |
| 6610 | #endif |
| 6611 | reginput = regline + col - clen; |
| 6612 | } |
| 6613 | else |
| 6614 | { |
| 6615 | /* Checking if the required start character matches is |
| 6616 | * cheaper than adding a state that won't match. */ |
| 6617 | c = PTR2CHAR(reginput + clen); |
| 6618 | if (c != prog->regstart && (!ireg_ic || MB_TOLOWER(c) |
| 6619 | != MB_TOLOWER(prog->regstart))) |
| 6620 | { |
| 6621 | #ifdef ENABLE_LOG |
| 6622 | fprintf(log_fd, " Skipping start state, regstart does not match\n"); |
| 6623 | #endif |
| 6624 | add = FALSE; |
| 6625 | } |
| 6626 | } |
| 6627 | } |
| 6628 | |
| 6629 | if (add) |
| 6630 | { |
| 6631 | if (REG_MULTI) |
| 6632 | m->norm.list.multi[0].start.col = |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6633 | (colnr_T)(reginput - regline) + clen; |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6634 | else |
| 6635 | m->norm.list.line[0].start = reginput + clen; |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6636 | addstate(nextlist, start->out, m, NULL, clen); |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6637 | } |
Bram Moolenaar | f96d109 | 2013-06-07 22:39:40 +0200 | [diff] [blame] | 6638 | } |
| 6639 | else |
Bram Moolenaar | 2a4e98a | 2013-06-09 16:24:45 +0200 | [diff] [blame] | 6640 | addstate(nextlist, start, m, NULL, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6641 | } |
| 6642 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6643 | #ifdef ENABLE_LOG |
| 6644 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6645 | { |
| 6646 | int i; |
| 6647 | |
| 6648 | for (i = 0; i < thislist->n; i++) |
| 6649 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 6650 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6651 | fprintf(log_fd, "\n"); |
| 6652 | #endif |
| 6653 | |
| 6654 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6655 | /* Advance to the next character, or advance to the next line, or |
| 6656 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 6657 | if (clen != 0) |
| 6658 | reginput += clen; |
Bram Moolenaar | 307aa16 | 2013-06-02 16:34:21 +0200 | [diff] [blame] | 6659 | else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
| 6660 | && reglnum < nfa_endp->se_u.pos.lnum)) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 6661 | reg_nextline(); |
| 6662 | else |
| 6663 | break; |
| 6664 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6665 | |
| 6666 | #ifdef ENABLE_LOG |
| 6667 | if (log_fd != stderr) |
| 6668 | fclose(log_fd); |
| 6669 | log_fd = NULL; |
| 6670 | #endif |
| 6671 | |
| 6672 | theend: |
| 6673 | /* Free memory */ |
| 6674 | vim_free(list[0].t); |
| 6675 | vim_free(list[1].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6676 | vim_free(listids); |
Bram Moolenaar | 8aca2e9 | 2013-06-07 14:59:18 +0200 | [diff] [blame] | 6677 | #undef ADD_STATE_IF_MATCH |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 6678 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6679 | fclose(debug); |
| 6680 | #endif |
| 6681 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6682 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6683 | } |
| 6684 | |
| 6685 | /* |
| 6686 | * Try match of "prog" with at regline["col"]. |
| 6687 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6688 | */ |
| 6689 | static long |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6690 | nfa_regtry(prog, col) |
| 6691 | nfa_regprog_T *prog; |
| 6692 | colnr_T col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6693 | { |
| 6694 | int i; |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6695 | regsubs_T subs, m; |
| 6696 | nfa_state_T *start = prog->start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6697 | #ifdef ENABLE_LOG |
| 6698 | FILE *f; |
| 6699 | #endif |
| 6700 | |
| 6701 | reginput = regline + col; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6702 | |
| 6703 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6704 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6705 | if (f != NULL) |
| 6706 | { |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6707 | fprintf(f, "\n\n\t=======================================================\n"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6708 | #ifdef DEBUG |
| 6709 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 6710 | #endif |
| 6711 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
Bram Moolenaar | 8795374 | 2013-06-05 18:52:40 +0200 | [diff] [blame] | 6712 | fprintf(f, "\t=======================================================\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 6713 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6714 | fprintf(f, "\n\n"); |
| 6715 | fclose(f); |
| 6716 | } |
| 6717 | else |
| 6718 | EMSG(_("Could not open temporary log file for writing ")); |
| 6719 | #endif |
| 6720 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6721 | clear_sub(&subs.norm); |
| 6722 | clear_sub(&m.norm); |
| 6723 | #ifdef FEAT_SYN_HL |
| 6724 | clear_sub(&subs.synt); |
| 6725 | clear_sub(&m.synt); |
| 6726 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6727 | |
Bram Moolenaar | f6de032 | 2013-06-02 21:30:04 +0200 | [diff] [blame] | 6728 | if (nfa_regmatch(prog, start, &subs, &m) == FALSE) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6729 | return 0; |
| 6730 | |
| 6731 | cleanup_subexpr(); |
| 6732 | if (REG_MULTI) |
| 6733 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6734 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6735 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6736 | reg_startpos[i] = subs.norm.list.multi[i].start; |
| 6737 | reg_endpos[i] = subs.norm.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6738 | } |
| 6739 | |
| 6740 | if (reg_startpos[0].lnum < 0) |
| 6741 | { |
| 6742 | reg_startpos[0].lnum = 0; |
| 6743 | reg_startpos[0].col = col; |
| 6744 | } |
| 6745 | if (reg_endpos[0].lnum < 0) |
| 6746 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 6747 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6748 | reg_endpos[0].lnum = reglnum; |
| 6749 | reg_endpos[0].col = (int)(reginput - regline); |
| 6750 | } |
| 6751 | else |
| 6752 | /* Use line number of "\ze". */ |
| 6753 | reglnum = reg_endpos[0].lnum; |
| 6754 | } |
| 6755 | else |
| 6756 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6757 | for (i = 0; i < subs.norm.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6758 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6759 | reg_startp[i] = subs.norm.list.line[i].start; |
| 6760 | reg_endp[i] = subs.norm.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | if (reg_startp[0] == NULL) |
| 6764 | reg_startp[0] = regline + col; |
| 6765 | if (reg_endp[0] == NULL) |
| 6766 | reg_endp[0] = reginput; |
| 6767 | } |
| 6768 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6769 | #ifdef FEAT_SYN_HL |
| 6770 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 6771 | unref_extmatch(re_extmatch_out); |
| 6772 | re_extmatch_out = NULL; |
| 6773 | |
| 6774 | if (prog->reghasz == REX_SET) |
| 6775 | { |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6776 | cleanup_zsubexpr(); |
| 6777 | re_extmatch_out = make_extmatch(); |
| 6778 | for (i = 0; i < subs.synt.in_use; i++) |
| 6779 | { |
| 6780 | if (REG_MULTI) |
| 6781 | { |
| 6782 | struct multipos *mpos = &subs.synt.list.multi[i]; |
| 6783 | |
Bram Moolenaar | 5a4e160 | 2014-04-06 21:34:04 +0200 | [diff] [blame] | 6784 | /* Only accept single line matches that are valid. */ |
| 6785 | if (mpos->start.lnum >= 0 |
| 6786 | && mpos->start.lnum == mpos->end.lnum |
| 6787 | && mpos->end.col >= mpos->start.col) |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6788 | re_extmatch_out->matches[i] = |
| 6789 | vim_strnsave(reg_getline(mpos->start.lnum) |
| 6790 | + mpos->start.col, |
| 6791 | mpos->end.col - mpos->start.col); |
| 6792 | } |
| 6793 | else |
| 6794 | { |
| 6795 | struct linepos *lpos = &subs.synt.list.line[i]; |
| 6796 | |
| 6797 | if (lpos->start != NULL && lpos->end != NULL) |
| 6798 | re_extmatch_out->matches[i] = |
| 6799 | vim_strnsave(lpos->start, |
| 6800 | (int)(lpos->end - lpos->start)); |
| 6801 | } |
| 6802 | } |
| 6803 | } |
| 6804 | #endif |
| 6805 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6806 | return 1 + reglnum; |
| 6807 | } |
| 6808 | |
| 6809 | /* |
| 6810 | * Match a regexp against a string ("line" points to the string) or multiple |
| 6811 | * lines ("line" is NULL, use reg_getline()). |
| 6812 | * |
| 6813 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 6814 | */ |
| 6815 | static long |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6816 | nfa_regexec_both(line, startcol) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6817 | char_u *line; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6818 | colnr_T startcol; /* column to start looking for match */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6819 | { |
| 6820 | nfa_regprog_T *prog; |
| 6821 | long retval = 0L; |
| 6822 | int i; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6823 | colnr_T col = startcol; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6824 | |
| 6825 | if (REG_MULTI) |
| 6826 | { |
| 6827 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 6828 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 6829 | reg_startpos = reg_mmatch->startpos; |
| 6830 | reg_endpos = reg_mmatch->endpos; |
| 6831 | } |
| 6832 | else |
| 6833 | { |
| 6834 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 6835 | reg_startp = reg_match->startp; |
| 6836 | reg_endp = reg_match->endp; |
| 6837 | } |
| 6838 | |
| 6839 | /* Be paranoid... */ |
| 6840 | if (prog == NULL || line == NULL) |
| 6841 | { |
| 6842 | EMSG(_(e_null)); |
| 6843 | goto theend; |
| 6844 | } |
| 6845 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6846 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 6847 | if (prog->regflags & RF_ICASE) |
| 6848 | ireg_ic = TRUE; |
| 6849 | else if (prog->regflags & RF_NOICASE) |
| 6850 | ireg_ic = FALSE; |
| 6851 | |
| 6852 | #ifdef FEAT_MBYTE |
| 6853 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 6854 | if (prog->regflags & RF_ICOMBINE) |
| 6855 | ireg_icombine = TRUE; |
| 6856 | #endif |
| 6857 | |
| 6858 | regline = line; |
| 6859 | reglnum = 0; /* relative to line */ |
| 6860 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6861 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 6862 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 6863 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6864 | nfa_listid = 1; |
| 6865 | nfa_alt_listid = 2; |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6866 | #ifdef DEBUG |
| 6867 | nfa_regengine.expr = prog->pattern; |
| 6868 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6869 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6870 | if (prog->reganch && col > 0) |
| 6871 | return 0L; |
| 6872 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6873 | need_clear_subexpr = TRUE; |
| 6874 | #ifdef FEAT_SYN_HL |
| 6875 | /* Clear the external match subpointers if necessary. */ |
| 6876 | if (prog->reghasz == REX_SET) |
| 6877 | { |
| 6878 | nfa_has_zsubexpr = TRUE; |
| 6879 | need_clear_zsubexpr = TRUE; |
| 6880 | } |
| 6881 | else |
| 6882 | nfa_has_zsubexpr = FALSE; |
| 6883 | #endif |
| 6884 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6885 | if (prog->regstart != NUL) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6886 | { |
Bram Moolenaar | 87f764a | 2013-06-08 14:38:27 +0200 | [diff] [blame] | 6887 | /* Skip ahead until a character we know the match must start with. |
| 6888 | * When there is none there is no match. */ |
| 6889 | if (skip_to_start(prog->regstart, &col) == FAIL) |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6890 | return 0L; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6891 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6892 | /* If match_text is set it contains the full text that must match. |
| 6893 | * Nothing else to try. Doesn't handle combining chars well. */ |
Bram Moolenaar | a940aa6 | 2013-06-08 23:30:04 +0200 | [diff] [blame] | 6894 | if (prog->match_text != NULL |
| 6895 | #ifdef FEAT_MBYTE |
| 6896 | && !ireg_icombine |
| 6897 | #endif |
| 6898 | ) |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6899 | return find_match_text(col, prog->regstart, prog->match_text); |
| 6900 | } |
| 6901 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 6902 | /* If the start column is past the maximum column: no need to try. */ |
| 6903 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 6904 | goto theend; |
| 6905 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 6906 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6907 | for (i = 0; i < nstate; ++i) |
| 6908 | { |
| 6909 | prog->state[i].id = i; |
Bram Moolenaar | dd2ccdf | 2013-06-03 12:17:04 +0200 | [diff] [blame] | 6910 | prog->state[i].lastlist[0] = 0; |
| 6911 | prog->state[i].lastlist[1] = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6912 | } |
| 6913 | |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 6914 | retval = nfa_regtry(prog, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6915 | |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 6916 | #ifdef DEBUG |
| 6917 | nfa_regengine.expr = NULL; |
| 6918 | #endif |
| 6919 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6920 | theend: |
| 6921 | return retval; |
| 6922 | } |
| 6923 | |
| 6924 | /* |
| 6925 | * Compile a regular expression into internal code for the NFA matcher. |
| 6926 | * Returns the program in allocated space. Returns NULL for an error. |
| 6927 | */ |
| 6928 | static regprog_T * |
| 6929 | nfa_regcomp(expr, re_flags) |
| 6930 | char_u *expr; |
| 6931 | int re_flags; |
| 6932 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6933 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 6934 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6935 | int *postfix; |
| 6936 | |
| 6937 | if (expr == NULL) |
| 6938 | return NULL; |
| 6939 | |
| 6940 | #ifdef DEBUG |
| 6941 | nfa_regengine.expr = expr; |
| 6942 | #endif |
| 6943 | |
| 6944 | init_class_tab(); |
| 6945 | |
| 6946 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 6947 | return NULL; |
| 6948 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6949 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6950 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6951 | postfix = re2post(); |
| 6952 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6953 | { |
| 6954 | /* TODO: only give this error for debugging? */ |
| 6955 | if (post_ptr >= post_end) |
| 6956 | 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] | 6957 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 6958 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6959 | |
| 6960 | /* |
| 6961 | * In order to build the NFA, we parse the input regexp twice: |
| 6962 | * 1. first pass to count size (so we can allocate space) |
| 6963 | * 2. second to emit code |
| 6964 | */ |
| 6965 | #ifdef ENABLE_LOG |
| 6966 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 6967 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6968 | |
| 6969 | if (f != NULL) |
| 6970 | { |
| 6971 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 6972 | fclose(f); |
| 6973 | } |
| 6974 | } |
| 6975 | #endif |
| 6976 | |
| 6977 | /* |
| 6978 | * PASS 1 |
| 6979 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 6980 | */ |
| 6981 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6982 | |
Bram Moolenaar | 16619a2 | 2013-06-11 18:42:36 +0200 | [diff] [blame] | 6983 | /* allocate the regprog with space for the compiled regexp */ |
| 6984 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 6985 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 6986 | if (prog == NULL) |
| 6987 | goto fail; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6988 | state_ptr = prog->state; |
| 6989 | |
| 6990 | /* |
| 6991 | * PASS 2 |
| 6992 | * Build the NFA |
| 6993 | */ |
| 6994 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 6995 | if (prog->start == NULL) |
| 6996 | goto fail; |
| 6997 | |
| 6998 | prog->regflags = regflags; |
| 6999 | prog->engine = &nfa_regengine; |
| 7000 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 7001 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 7002 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 7003 | prog->nsubexp = regnpar; |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7004 | |
Bram Moolenaar | a2947e2 | 2013-06-11 22:44:09 +0200 | [diff] [blame] | 7005 | nfa_postprocess(prog); |
| 7006 | |
Bram Moolenaar | d89616e | 2013-06-06 18:46:06 +0200 | [diff] [blame] | 7007 | prog->reganch = nfa_get_reganch(prog->start, 0); |
| 7008 | prog->regstart = nfa_get_regstart(prog->start, 0); |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7009 | prog->match_text = nfa_get_match_text(prog->start); |
| 7010 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7011 | #ifdef ENABLE_LOG |
| 7012 | nfa_postfix_dump(expr, OK); |
| 7013 | nfa_dump(prog); |
| 7014 | #endif |
Bram Moolenaar | efb23f2 | 2013-06-01 23:02:54 +0200 | [diff] [blame] | 7015 | #ifdef FEAT_SYN_HL |
| 7016 | /* Remember whether this pattern has any \z specials in it. */ |
| 7017 | prog->reghasz = re_has_z; |
| 7018 | #endif |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7019 | #ifdef DEBUG |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7020 | prog->pattern = vim_strsave(expr); |
Bram Moolenaar | 69afb7b | 2013-06-02 15:55:55 +0200 | [diff] [blame] | 7021 | nfa_regengine.expr = NULL; |
| 7022 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7023 | |
| 7024 | out: |
| 7025 | vim_free(post_start); |
| 7026 | post_start = post_ptr = post_end = NULL; |
| 7027 | state_ptr = NULL; |
| 7028 | return (regprog_T *)prog; |
| 7029 | |
| 7030 | fail: |
| 7031 | vim_free(prog); |
| 7032 | prog = NULL; |
| 7033 | #ifdef ENABLE_LOG |
| 7034 | nfa_postfix_dump(expr, FAIL); |
| 7035 | #endif |
| 7036 | #ifdef DEBUG |
| 7037 | nfa_regengine.expr = NULL; |
| 7038 | #endif |
| 7039 | goto out; |
| 7040 | } |
| 7041 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7042 | /* |
| 7043 | * Free a compiled regexp program, returned by nfa_regcomp(). |
| 7044 | */ |
| 7045 | static void |
| 7046 | nfa_regfree(prog) |
| 7047 | regprog_T *prog; |
| 7048 | { |
| 7049 | if (prog != NULL) |
| 7050 | { |
| 7051 | vim_free(((nfa_regprog_T *)prog)->match_text); |
| 7052 | #ifdef DEBUG |
| 7053 | vim_free(((nfa_regprog_T *)prog)->pattern); |
| 7054 | #endif |
| 7055 | vim_free(prog); |
| 7056 | } |
| 7057 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7058 | |
| 7059 | /* |
| 7060 | * Match a regexp against a string. |
| 7061 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 7062 | * Uses curbuf for line count and 'iskeyword'. |
| 7063 | * |
| 7064 | * Return TRUE if there is a match, FALSE if not. |
| 7065 | */ |
| 7066 | static int |
| 7067 | nfa_regexec(rmp, line, col) |
| 7068 | regmatch_T *rmp; |
| 7069 | char_u *line; /* string to match against */ |
| 7070 | colnr_T col; /* column to start looking for match */ |
| 7071 | { |
| 7072 | reg_match = rmp; |
| 7073 | reg_mmatch = NULL; |
| 7074 | reg_maxline = 0; |
| 7075 | reg_line_lbr = FALSE; |
| 7076 | reg_buf = curbuf; |
| 7077 | reg_win = NULL; |
| 7078 | ireg_ic = rmp->rm_ic; |
| 7079 | #ifdef FEAT_MBYTE |
| 7080 | ireg_icombine = FALSE; |
| 7081 | #endif |
| 7082 | ireg_maxcol = 0; |
| 7083 | return (nfa_regexec_both(line, col) != 0); |
| 7084 | } |
| 7085 | |
| 7086 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 7087 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 7088 | |
| 7089 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 7090 | |
| 7091 | /* |
| 7092 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 7093 | */ |
| 7094 | static int |
| 7095 | nfa_regexec_nl(rmp, line, col) |
| 7096 | regmatch_T *rmp; |
| 7097 | char_u *line; /* string to match against */ |
| 7098 | colnr_T col; /* column to start looking for match */ |
| 7099 | { |
| 7100 | reg_match = rmp; |
| 7101 | reg_mmatch = NULL; |
| 7102 | reg_maxline = 0; |
| 7103 | reg_line_lbr = TRUE; |
| 7104 | reg_buf = curbuf; |
| 7105 | reg_win = NULL; |
| 7106 | ireg_ic = rmp->rm_ic; |
| 7107 | #ifdef FEAT_MBYTE |
| 7108 | ireg_icombine = FALSE; |
| 7109 | #endif |
| 7110 | ireg_maxcol = 0; |
| 7111 | return (nfa_regexec_both(line, col) != 0); |
| 7112 | } |
| 7113 | #endif |
| 7114 | |
| 7115 | |
| 7116 | /* |
| 7117 | * Match a regexp against multiple lines. |
| 7118 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 7119 | * Uses curbuf for line count and 'iskeyword'. |
| 7120 | * |
| 7121 | * Return zero if there is no match. Return number of lines contained in the |
| 7122 | * match otherwise. |
| 7123 | * |
| 7124 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 7125 | * |
| 7126 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 7127 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 7128 | * |
| 7129 | * +-------------------------+ |
| 7130 | * |a | |
| 7131 | * |b | |
| 7132 | * |c | |
| 7133 | * | | |
| 7134 | * +-------------------------+ |
| 7135 | * |
| 7136 | * then nfa_regexec_multi() returns 3. while the original |
| 7137 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 7138 | * |
| 7139 | * FIXME if this behavior is not compatible. |
| 7140 | */ |
| 7141 | static long |
| 7142 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 7143 | regmmatch_T *rmp; |
| 7144 | win_T *win; /* window in which to search or NULL */ |
| 7145 | buf_T *buf; /* buffer in which to search */ |
| 7146 | linenr_T lnum; /* nr of line to start looking for match */ |
| 7147 | colnr_T col; /* column to start looking for match */ |
| 7148 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 7149 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7150 | reg_match = NULL; |
| 7151 | reg_mmatch = rmp; |
| 7152 | reg_buf = buf; |
| 7153 | reg_win = win; |
| 7154 | reg_firstlnum = lnum; |
| 7155 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 7156 | reg_line_lbr = FALSE; |
| 7157 | ireg_ic = rmp->rmm_ic; |
| 7158 | #ifdef FEAT_MBYTE |
| 7159 | ireg_icombine = FALSE; |
| 7160 | #endif |
| 7161 | ireg_maxcol = rmp->rmm_maxcol; |
| 7162 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 7163 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7164 | } |
| 7165 | |
| 7166 | #ifdef DEBUG |
| 7167 | # undef ENABLE_LOG |
| 7168 | #endif |