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 | |
| 32 | /* Upper limit allowed for {m,n} repetitions handled by NFA */ |
| 33 | #define NFA_BRACES_MAXLIMIT 10 |
| 34 | /* For allocating space for the postfix representation */ |
| 35 | #define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2 |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 36 | |
| 37 | enum |
| 38 | { |
| 39 | NFA_SPLIT = -1024, |
| 40 | NFA_MATCH, |
| 41 | NFA_SKIP_CHAR, /* matches a 0-length char */ |
| 42 | NFA_END_NEG_RANGE, /* Used when expanding [^ab] */ |
| 43 | |
| 44 | NFA_CONCAT, |
| 45 | NFA_OR, |
| 46 | NFA_STAR, |
| 47 | NFA_PLUS, |
| 48 | NFA_QUEST, |
| 49 | NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */ |
| 50 | NFA_NOT, /* used for [^ab] negated char ranges */ |
| 51 | |
| 52 | NFA_BOL, /* ^ Begin line */ |
| 53 | NFA_EOL, /* $ End line */ |
| 54 | NFA_BOW, /* \< Begin word */ |
| 55 | NFA_EOW, /* \> End word */ |
| 56 | NFA_BOF, /* \%^ Begin file */ |
| 57 | NFA_EOF, /* \%$ End file */ |
| 58 | NFA_NEWL, |
| 59 | NFA_ZSTART, /* Used for \zs */ |
| 60 | NFA_ZEND, /* Used for \ze */ |
| 61 | NFA_NOPEN, /* Start of subexpression marked with \%( */ |
| 62 | NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ |
| 63 | NFA_START_INVISIBLE, |
| 64 | NFA_END_INVISIBLE, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 65 | NFA_COMPOSING, /* Next nodes in NFA are part of the |
| 66 | composing multibyte char */ |
| 67 | NFA_END_COMPOSING, /* End of a composing char in the NFA */ |
| 68 | |
| 69 | /* The following are used only in the postfix form, not in the NFA */ |
| 70 | NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ |
| 71 | NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ |
| 72 | NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ |
| 73 | NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ |
| 74 | NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ |
| 75 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 76 | NFA_BACKREF1, /* \1 */ |
| 77 | NFA_BACKREF2, /* \2 */ |
| 78 | NFA_BACKREF3, /* \3 */ |
| 79 | NFA_BACKREF4, /* \4 */ |
| 80 | NFA_BACKREF5, /* \5 */ |
| 81 | NFA_BACKREF6, /* \6 */ |
| 82 | NFA_BACKREF7, /* \7 */ |
| 83 | NFA_BACKREF8, /* \8 */ |
| 84 | NFA_BACKREF9, /* \9 */ |
| 85 | NFA_SKIP, /* Skip characters */ |
| 86 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 87 | NFA_MOPEN, |
| 88 | NFA_MCLOSE = NFA_MOPEN + NSUBEXP, |
| 89 | |
| 90 | /* NFA_FIRST_NL */ |
| 91 | NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */ |
| 92 | NFA_ANYOF, /* Match any character in this string. */ |
| 93 | NFA_ANYBUT, /* Match any character not in this string. */ |
| 94 | NFA_IDENT, /* Match identifier char */ |
| 95 | NFA_SIDENT, /* Match identifier char but no digit */ |
| 96 | NFA_KWORD, /* Match keyword char */ |
| 97 | NFA_SKWORD, /* Match word char but no digit */ |
| 98 | NFA_FNAME, /* Match file name char */ |
| 99 | NFA_SFNAME, /* Match file name char but no digit */ |
| 100 | NFA_PRINT, /* Match printable char */ |
| 101 | NFA_SPRINT, /* Match printable char but no digit */ |
| 102 | NFA_WHITE, /* Match whitespace char */ |
| 103 | NFA_NWHITE, /* Match non-whitespace char */ |
| 104 | NFA_DIGIT, /* Match digit char */ |
| 105 | NFA_NDIGIT, /* Match non-digit char */ |
| 106 | NFA_HEX, /* Match hex char */ |
| 107 | NFA_NHEX, /* Match non-hex char */ |
| 108 | NFA_OCTAL, /* Match octal char */ |
| 109 | NFA_NOCTAL, /* Match non-octal char */ |
| 110 | NFA_WORD, /* Match word char */ |
| 111 | NFA_NWORD, /* Match non-word char */ |
| 112 | NFA_HEAD, /* Match head char */ |
| 113 | NFA_NHEAD, /* Match non-head char */ |
| 114 | NFA_ALPHA, /* Match alpha char */ |
| 115 | NFA_NALPHA, /* Match non-alpha char */ |
| 116 | NFA_LOWER, /* Match lowercase char */ |
| 117 | NFA_NLOWER, /* Match non-lowercase char */ |
| 118 | NFA_UPPER, /* Match uppercase char */ |
| 119 | NFA_NUPPER, /* Match non-uppercase char */ |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 120 | |
| 121 | NFA_CURSOR, /* Match cursor pos */ |
| 122 | NFA_LNUM, /* Match line number */ |
| 123 | NFA_LNUM_GT, /* Match > line number */ |
| 124 | NFA_LNUM_LT, /* Match < line number */ |
| 125 | NFA_COL, /* Match cursor column */ |
| 126 | NFA_COL_GT, /* Match > cursor column */ |
| 127 | NFA_COL_LT, /* Match < cursor column */ |
| 128 | NFA_VCOL, /* Match cursor virtual column */ |
| 129 | NFA_VCOL_GT, /* Match > cursor virtual column */ |
| 130 | NFA_VCOL_LT, /* Match < cursor virtual column */ |
| 131 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 132 | NFA_FIRST_NL = NFA_ANY + ADD_NL, |
| 133 | NFA_LAST_NL = NFA_NUPPER + ADD_NL, |
| 134 | |
| 135 | /* Character classes [:alnum:] etc */ |
| 136 | NFA_CLASS_ALNUM, |
| 137 | NFA_CLASS_ALPHA, |
| 138 | NFA_CLASS_BLANK, |
| 139 | NFA_CLASS_CNTRL, |
| 140 | NFA_CLASS_DIGIT, |
| 141 | NFA_CLASS_GRAPH, |
| 142 | NFA_CLASS_LOWER, |
| 143 | NFA_CLASS_PRINT, |
| 144 | NFA_CLASS_PUNCT, |
| 145 | NFA_CLASS_SPACE, |
| 146 | NFA_CLASS_UPPER, |
| 147 | NFA_CLASS_XDIGIT, |
| 148 | NFA_CLASS_TAB, |
| 149 | NFA_CLASS_RETURN, |
| 150 | NFA_CLASS_BACKSPACE, |
| 151 | NFA_CLASS_ESCAPE |
| 152 | }; |
| 153 | |
| 154 | /* Keep in sync with classchars. */ |
| 155 | static int nfa_classcodes[] = { |
| 156 | NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
| 157 | NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
| 158 | NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
| 159 | NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
| 160 | NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, |
| 161 | NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, |
| 162 | NFA_UPPER, NFA_NUPPER |
| 163 | }; |
| 164 | |
| 165 | static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); |
| 166 | |
| 167 | /* |
| 168 | * NFA errors can be of 3 types: |
| 169 | * *** NFA runtime errors, when something unknown goes wrong. The NFA fails |
| 170 | * silently and revert the to backtracking engine. |
| 171 | * syntax_error = FALSE; |
| 172 | * *** Regexp syntax errors, when the input regexp is not syntactically correct. |
| 173 | * The NFA engine displays an error message, and nothing else happens. |
| 174 | * syntax_error = TRUE |
| 175 | * *** Unsupported features, when the input regexp uses an operator that is not |
| 176 | * implemented in the NFA. The NFA engine fails silently, and reverts to the |
| 177 | * old backtracking engine. |
| 178 | * syntax_error = FALSE |
| 179 | * "The NFA fails" means that "compiling the regexp with the NFA fails": |
| 180 | * nfa_regcomp() returns FAIL. |
| 181 | */ |
| 182 | static int syntax_error = FALSE; |
| 183 | |
| 184 | /* NFA regexp \ze operator encountered. */ |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 185 | static int nfa_has_zend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 186 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 187 | /* NFA regexp \1 .. \9 encountered. */ |
| 188 | static int nfa_has_backref; |
| 189 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 190 | /* Number of sub expressions actually being used during execution. 1 if only |
| 191 | * the whole match (subexpr 0) is used. */ |
| 192 | static int nfa_nsubexpr; |
| 193 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 194 | static int *post_start; /* holds the postfix form of r.e. */ |
| 195 | static int *post_end; |
| 196 | static int *post_ptr; |
| 197 | |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 198 | static int nstate; /* Number of states in the NFA. Also used when |
| 199 | * executing. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 200 | static int istate; /* Index in the state vector, used in new_state() */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 201 | |
| 202 | |
| 203 | static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags)); |
| 204 | static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl)); |
| 205 | static int nfa_emit_equi_class __ARGS((int c, int neg)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 206 | static int nfa_regatom __ARGS((void)); |
| 207 | static int nfa_regpiece __ARGS((void)); |
| 208 | static int nfa_regconcat __ARGS((void)); |
| 209 | static int nfa_regbranch __ARGS((void)); |
| 210 | static int nfa_reg __ARGS((int paren)); |
| 211 | #ifdef DEBUG |
| 212 | static void nfa_set_code __ARGS((int c)); |
| 213 | static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 214 | static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state)); |
| 215 | 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] | 216 | static void nfa_dump __ARGS((nfa_regprog_T *prog)); |
| 217 | #endif |
| 218 | static int *re2post __ARGS((void)); |
| 219 | static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1)); |
| 220 | static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size)); |
| 221 | static int check_char_class __ARGS((int class, int c)); |
| 222 | static void st_error __ARGS((int *postfix, int *end, int *p)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 223 | static void nfa_set_neg_listids __ARGS((nfa_state_T *start)); |
| 224 | static void nfa_set_null_listids __ARGS((nfa_state_T *start)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 225 | static void nfa_save_listids __ARGS((nfa_state_T *start, int *list)); |
| 226 | static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list)); |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 227 | static int nfa_re_num_cmp __ARGS((long_u val, int op, long_u pos)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 228 | static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col)); |
| 229 | static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); |
| 230 | static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); |
| 231 | static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 232 | static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
| 233 | |
| 234 | /* helper functions used when doing re2post() ... regatom() parsing */ |
| 235 | #define EMIT(c) do { \ |
| 236 | if (post_ptr >= post_end) \ |
| 237 | return FAIL; \ |
| 238 | *post_ptr++ = c; \ |
| 239 | } while (0) |
| 240 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 241 | /* |
| 242 | * Initialize internal variables before NFA compilation. |
| 243 | * Return OK on success, FAIL otherwise. |
| 244 | */ |
| 245 | static int |
| 246 | nfa_regcomp_start(expr, re_flags) |
| 247 | char_u *expr; |
| 248 | int re_flags; /* see vim_regcomp() */ |
| 249 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 250 | size_t postfix_size; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 251 | int nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 252 | |
| 253 | nstate = 0; |
| 254 | istate = 0; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 255 | /* A reasonable estimation for maximum size */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 256 | nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 257 | |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 258 | /* Some items blow up in size, such as [A-z]. Add more space for that. |
| 259 | * TODO: some patterns may still fail. */ |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 260 | nstate_max += 1000; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 261 | |
| 262 | /* Size for postfix representation of expr. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 263 | postfix_size = sizeof(*post_start) * nstate_max; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 264 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 265 | post_start = (int *)lalloc(postfix_size, TRUE); |
| 266 | if (post_start == NULL) |
| 267 | return FAIL; |
| 268 | vim_memset(post_start, 0, postfix_size); |
| 269 | post_ptr = post_start; |
Bram Moolenaar | bc0ea8f | 2013-05-20 13:44:29 +0200 | [diff] [blame] | 270 | post_end = post_start + nstate_max; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 271 | nfa_has_zend = FALSE; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 272 | nfa_has_backref = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 273 | |
| 274 | regcomp_start(expr, re_flags); |
| 275 | |
| 276 | return OK; |
| 277 | } |
| 278 | |
| 279 | /* |
| 280 | * Search between "start" and "end" and try to recognize a |
| 281 | * character class in expanded form. For example [0-9]. |
| 282 | * On success, return the id the character class to be emitted. |
| 283 | * On failure, return 0 (=FAIL) |
| 284 | * Start points to the first char of the range, while end should point |
| 285 | * to the closing brace. |
| 286 | */ |
| 287 | static int |
| 288 | nfa_recognize_char_class(start, end, extra_newl) |
| 289 | char_u *start; |
| 290 | char_u *end; |
| 291 | int extra_newl; |
| 292 | { |
| 293 | int i; |
| 294 | /* Each of these variables takes up a char in "config[]", |
| 295 | * in the order they are here. */ |
| 296 | int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE, |
| 297 | o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE; |
| 298 | char_u *p; |
| 299 | #define NCONFIGS 16 |
| 300 | int classid[NCONFIGS] = { |
| 301 | NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX, |
| 302 | NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD, |
| 303 | NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA, |
| 304 | NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER |
| 305 | }; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 306 | char_u myconfig[10]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 307 | char_u config[NCONFIGS][9] = { |
| 308 | "000000100", /* digit */ |
| 309 | "100000100", /* non digit */ |
| 310 | "011000100", /* hex-digit */ |
| 311 | "111000100", /* non hex-digit */ |
| 312 | "000001000", /* octal-digit */ |
| 313 | "100001000", /* [^0-7] */ |
| 314 | "000110110", /* [0-9A-Za-z_] */ |
| 315 | "100110110", /* [^0-9A-Za-z_] */ |
| 316 | "000110010", /* head of word */ |
| 317 | "100110010", /* not head of word */ |
| 318 | "000110000", /* alphabetic char a-z */ |
| 319 | "100110000", /* non alphabetic char */ |
| 320 | "000100000", /* lowercase letter */ |
| 321 | "100100000", /* non lowercase */ |
| 322 | "000010000", /* uppercase */ |
| 323 | "100010000" /* non uppercase */ |
| 324 | }; |
| 325 | |
| 326 | if (extra_newl == TRUE) |
| 327 | newl = TRUE; |
| 328 | |
| 329 | if (*end != ']') |
| 330 | return FAIL; |
| 331 | p = start; |
| 332 | if (*p == '^') |
| 333 | { |
| 334 | not = TRUE; |
| 335 | p ++; |
| 336 | } |
| 337 | |
| 338 | while (p < end) |
| 339 | { |
| 340 | if (p + 2 < end && *(p + 1) == '-') |
| 341 | { |
| 342 | switch (*p) |
| 343 | { |
| 344 | case '0': |
| 345 | if (*(p + 2) == '9') |
| 346 | { |
| 347 | o9 = TRUE; |
| 348 | break; |
| 349 | } |
| 350 | else |
| 351 | if (*(p + 2) == '7') |
| 352 | { |
| 353 | o7 = TRUE; |
| 354 | break; |
| 355 | } |
| 356 | case 'a': |
| 357 | if (*(p + 2) == 'z') |
| 358 | { |
| 359 | az = TRUE; |
| 360 | break; |
| 361 | } |
| 362 | else |
| 363 | if (*(p + 2) == 'f') |
| 364 | { |
| 365 | af = TRUE; |
| 366 | break; |
| 367 | } |
| 368 | case 'A': |
| 369 | if (*(p + 2) == 'Z') |
| 370 | { |
| 371 | AZ = TRUE; |
| 372 | break; |
| 373 | } |
| 374 | else |
| 375 | if (*(p + 2) == 'F') |
| 376 | { |
| 377 | AF = TRUE; |
| 378 | break; |
| 379 | } |
| 380 | /* FALLTHROUGH */ |
| 381 | default: |
| 382 | return FAIL; |
| 383 | } |
| 384 | p += 3; |
| 385 | } |
| 386 | else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') |
| 387 | { |
| 388 | newl = TRUE; |
| 389 | p += 2; |
| 390 | } |
| 391 | else if (*p == '_') |
| 392 | { |
| 393 | underscore = TRUE; |
| 394 | p ++; |
| 395 | } |
| 396 | else if (*p == '\n') |
| 397 | { |
| 398 | newl = TRUE; |
| 399 | p ++; |
| 400 | } |
| 401 | else |
| 402 | return FAIL; |
| 403 | } /* while (p < end) */ |
| 404 | |
| 405 | if (p != end) |
| 406 | return FAIL; |
| 407 | |
| 408 | /* build the config that represents the ranges we gathered */ |
| 409 | STRCPY(myconfig, "000000000"); |
| 410 | if (not == TRUE) |
| 411 | myconfig[0] = '1'; |
| 412 | if (af == TRUE) |
| 413 | myconfig[1] = '1'; |
| 414 | if (AF == TRUE) |
| 415 | myconfig[2] = '1'; |
| 416 | if (az == TRUE) |
| 417 | myconfig[3] = '1'; |
| 418 | if (AZ == TRUE) |
| 419 | myconfig[4] = '1'; |
| 420 | if (o7 == TRUE) |
| 421 | myconfig[5] = '1'; |
| 422 | if (o9 == TRUE) |
| 423 | myconfig[6] = '1'; |
| 424 | if (underscore == TRUE) |
| 425 | myconfig[7] = '1'; |
| 426 | if (newl == TRUE) |
| 427 | { |
| 428 | myconfig[8] = '1'; |
| 429 | extra_newl = ADD_NL; |
| 430 | } |
| 431 | /* try to recognize character classes */ |
| 432 | for (i = 0; i < NCONFIGS; i++) |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 433 | if (STRNCMP(myconfig, config[i], 8) == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 434 | return classid[i] + extra_newl; |
| 435 | |
| 436 | /* fallthrough => no success so far */ |
| 437 | return FAIL; |
| 438 | |
| 439 | #undef NCONFIGS |
| 440 | } |
| 441 | |
| 442 | /* |
| 443 | * Produce the bytes for equivalence class "c". |
| 444 | * Currently only handles latin1, latin9 and utf-8. |
| 445 | * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is |
| 446 | * equivalent to 'a OR b OR c' |
| 447 | * |
| 448 | * NOTE! When changing this function, also update reg_equi_class() |
| 449 | */ |
| 450 | static int |
| 451 | nfa_emit_equi_class(c, neg) |
| 452 | int c; |
| 453 | int neg; |
| 454 | { |
| 455 | int first = TRUE; |
| 456 | int glue = neg == TRUE ? NFA_CONCAT : NFA_OR; |
| 457 | #define EMIT2(c) \ |
| 458 | EMIT(c); \ |
| 459 | if (neg == TRUE) { \ |
| 460 | EMIT(NFA_NOT); \ |
| 461 | } \ |
| 462 | if (first == FALSE) \ |
| 463 | EMIT(glue); \ |
| 464 | else \ |
| 465 | first = FALSE; \ |
| 466 | |
| 467 | #ifdef FEAT_MBYTE |
| 468 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
| 469 | || STRCMP(p_enc, "iso-8859-15") == 0) |
| 470 | #endif |
| 471 | { |
| 472 | switch (c) |
| 473 | { |
| 474 | case 'A': case '\300': case '\301': case '\302': |
| 475 | case '\303': case '\304': case '\305': |
| 476 | EMIT2('A'); EMIT2('\300'); EMIT2('\301'); |
| 477 | EMIT2('\302'); EMIT2('\303'); EMIT2('\304'); |
| 478 | EMIT2('\305'); |
| 479 | return OK; |
| 480 | |
| 481 | case 'C': case '\307': |
| 482 | EMIT2('C'); EMIT2('\307'); |
| 483 | return OK; |
| 484 | |
| 485 | case 'E': case '\310': case '\311': case '\312': case '\313': |
| 486 | EMIT2('E'); EMIT2('\310'); EMIT2('\311'); |
| 487 | EMIT2('\312'); EMIT2('\313'); |
| 488 | return OK; |
| 489 | |
| 490 | case 'I': case '\314': case '\315': case '\316': case '\317': |
| 491 | EMIT2('I'); EMIT2('\314'); EMIT2('\315'); |
| 492 | EMIT2('\316'); EMIT2('\317'); |
| 493 | return OK; |
| 494 | |
| 495 | case 'N': case '\321': |
| 496 | EMIT2('N'); EMIT2('\321'); |
| 497 | return OK; |
| 498 | |
| 499 | case 'O': case '\322': case '\323': case '\324': case '\325': |
| 500 | case '\326': |
| 501 | EMIT2('O'); EMIT2('\322'); EMIT2('\323'); |
| 502 | EMIT2('\324'); EMIT2('\325'); EMIT2('\326'); |
| 503 | return OK; |
| 504 | |
| 505 | case 'U': case '\331': case '\332': case '\333': case '\334': |
| 506 | EMIT2('U'); EMIT2('\331'); EMIT2('\332'); |
| 507 | EMIT2('\333'); EMIT2('\334'); |
| 508 | return OK; |
| 509 | |
| 510 | case 'Y': case '\335': |
| 511 | EMIT2('Y'); EMIT2('\335'); |
| 512 | return OK; |
| 513 | |
| 514 | case 'a': case '\340': case '\341': case '\342': |
| 515 | case '\343': case '\344': case '\345': |
| 516 | EMIT2('a'); EMIT2('\340'); EMIT2('\341'); |
| 517 | EMIT2('\342'); EMIT2('\343'); EMIT2('\344'); |
| 518 | EMIT2('\345'); |
| 519 | return OK; |
| 520 | |
| 521 | case 'c': case '\347': |
| 522 | EMIT2('c'); EMIT2('\347'); |
| 523 | return OK; |
| 524 | |
| 525 | case 'e': case '\350': case '\351': case '\352': case '\353': |
| 526 | EMIT2('e'); EMIT2('\350'); EMIT2('\351'); |
| 527 | EMIT2('\352'); EMIT2('\353'); |
| 528 | return OK; |
| 529 | |
| 530 | case 'i': case '\354': case '\355': case '\356': case '\357': |
| 531 | EMIT2('i'); EMIT2('\354'); EMIT2('\355'); |
| 532 | EMIT2('\356'); EMIT2('\357'); |
| 533 | return OK; |
| 534 | |
| 535 | case 'n': case '\361': |
| 536 | EMIT2('n'); EMIT2('\361'); |
| 537 | return OK; |
| 538 | |
| 539 | case 'o': case '\362': case '\363': case '\364': case '\365': |
| 540 | case '\366': |
| 541 | EMIT2('o'); EMIT2('\362'); EMIT2('\363'); |
| 542 | EMIT2('\364'); EMIT2('\365'); EMIT2('\366'); |
| 543 | return OK; |
| 544 | |
| 545 | case 'u': case '\371': case '\372': case '\373': case '\374': |
| 546 | EMIT2('u'); EMIT2('\371'); EMIT2('\372'); |
| 547 | EMIT2('\373'); EMIT2('\374'); |
| 548 | return OK; |
| 549 | |
| 550 | case 'y': case '\375': case '\377': |
| 551 | EMIT2('y'); EMIT2('\375'); EMIT2('\377'); |
| 552 | return OK; |
| 553 | |
| 554 | default: |
| 555 | return FAIL; |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | EMIT(c); |
| 560 | return OK; |
| 561 | #undef EMIT2 |
| 562 | } |
| 563 | |
| 564 | /* |
| 565 | * Code to parse regular expression. |
| 566 | * |
| 567 | * We try to reuse parsing functions in regexp.c to |
| 568 | * minimize surprise and keep the syntax consistent. |
| 569 | */ |
| 570 | |
| 571 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 572 | * Parse the lowest level. |
| 573 | * |
| 574 | * An atom can be one of a long list of items. Many atoms match one character |
| 575 | * in the text. It is often an ordinary character or a character class. |
| 576 | * Braces can be used to make a pattern into an atom. The "\z(\)" construct |
| 577 | * is only for syntax highlighting. |
| 578 | * |
| 579 | * atom ::= ordinary-atom |
| 580 | * or \( pattern \) |
| 581 | * or \%( pattern \) |
| 582 | * or \z( pattern \) |
| 583 | */ |
| 584 | static int |
| 585 | nfa_regatom() |
| 586 | { |
| 587 | int c; |
| 588 | int charclass; |
| 589 | int equiclass; |
| 590 | int collclass; |
| 591 | int got_coll_char; |
| 592 | char_u *p; |
| 593 | char_u *endp; |
| 594 | #ifdef FEAT_MBYTE |
| 595 | char_u *old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 596 | #endif |
| 597 | int extra = 0; |
| 598 | int first; |
| 599 | int emit_range; |
| 600 | int negated; |
| 601 | int result; |
| 602 | int startc = -1; |
| 603 | int endc = -1; |
| 604 | int oldstartc = -1; |
| 605 | int cpo_lit; /* 'cpoptions' contains 'l' flag */ |
| 606 | int cpo_bsl; /* 'cpoptions' contains '\' flag */ |
| 607 | int glue; /* ID that will "glue" nodes together */ |
| 608 | |
| 609 | cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL; |
| 610 | cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL; |
| 611 | |
| 612 | c = getchr(); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 613 | switch (c) |
| 614 | { |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 615 | case NUL: |
| 616 | syntax_error = TRUE; |
| 617 | EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); |
| 618 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 619 | case Magic('^'): |
| 620 | EMIT(NFA_BOL); |
| 621 | break; |
| 622 | |
| 623 | case Magic('$'): |
| 624 | EMIT(NFA_EOL); |
| 625 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 626 | had_eol = TRUE; |
| 627 | #endif |
| 628 | break; |
| 629 | |
| 630 | case Magic('<'): |
| 631 | EMIT(NFA_BOW); |
| 632 | break; |
| 633 | |
| 634 | case Magic('>'): |
| 635 | EMIT(NFA_EOW); |
| 636 | break; |
| 637 | |
| 638 | case Magic('_'): |
| 639 | c = no_Magic(getchr()); |
| 640 | if (c == '^') /* "\_^" is start-of-line */ |
| 641 | { |
| 642 | EMIT(NFA_BOL); |
| 643 | break; |
| 644 | } |
| 645 | if (c == '$') /* "\_$" is end-of-line */ |
| 646 | { |
| 647 | EMIT(NFA_EOL); |
| 648 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 649 | had_eol = TRUE; |
| 650 | #endif |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | extra = ADD_NL; |
| 655 | |
| 656 | /* "\_[" is collection plus newline */ |
| 657 | if (c == '[') |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 658 | goto collection; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 659 | |
| 660 | /* "\_x" is character class plus newline */ |
| 661 | /*FALLTHROUGH*/ |
| 662 | |
| 663 | /* |
| 664 | * Character classes. |
| 665 | */ |
| 666 | case Magic('.'): |
| 667 | case Magic('i'): |
| 668 | case Magic('I'): |
| 669 | case Magic('k'): |
| 670 | case Magic('K'): |
| 671 | case Magic('f'): |
| 672 | case Magic('F'): |
| 673 | case Magic('p'): |
| 674 | case Magic('P'): |
| 675 | case Magic('s'): |
| 676 | case Magic('S'): |
| 677 | case Magic('d'): |
| 678 | case Magic('D'): |
| 679 | case Magic('x'): |
| 680 | case Magic('X'): |
| 681 | case Magic('o'): |
| 682 | case Magic('O'): |
| 683 | case Magic('w'): |
| 684 | case Magic('W'): |
| 685 | case Magic('h'): |
| 686 | case Magic('H'): |
| 687 | case Magic('a'): |
| 688 | case Magic('A'): |
| 689 | case Magic('l'): |
| 690 | case Magic('L'): |
| 691 | case Magic('u'): |
| 692 | case Magic('U'): |
| 693 | p = vim_strchr(classchars, no_Magic(c)); |
| 694 | if (p == NULL) |
| 695 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 696 | EMSGN("INTERNAL: Unknown character class char: %ld", c); |
| 697 | return FAIL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 698 | } |
| 699 | #ifdef FEAT_MBYTE |
| 700 | /* When '.' is followed by a composing char ignore the dot, so that |
| 701 | * the composing char is matched here. */ |
| 702 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 703 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 704 | old_regparse = regparse; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 705 | c = getchr(); |
| 706 | goto nfa_do_multibyte; |
| 707 | } |
| 708 | #endif |
| 709 | EMIT(nfa_classcodes[p - classchars]); |
| 710 | if (extra == ADD_NL) |
| 711 | { |
| 712 | EMIT(NFA_NEWL); |
| 713 | EMIT(NFA_OR); |
| 714 | regflags |= RF_HASNL; |
| 715 | } |
| 716 | break; |
| 717 | |
| 718 | case Magic('n'): |
| 719 | if (reg_string) |
| 720 | /* In a string "\n" matches a newline character. */ |
| 721 | EMIT(NL); |
| 722 | else |
| 723 | { |
| 724 | /* In buffer text "\n" matches the end of a line. */ |
| 725 | EMIT(NFA_NEWL); |
| 726 | regflags |= RF_HASNL; |
| 727 | } |
| 728 | break; |
| 729 | |
| 730 | case Magic('('): |
| 731 | if (nfa_reg(REG_PAREN) == FAIL) |
| 732 | return FAIL; /* cascaded error */ |
| 733 | break; |
| 734 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 735 | case Magic('|'): |
| 736 | case Magic('&'): |
| 737 | case Magic(')'): |
| 738 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 739 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 740 | return FAIL; |
| 741 | |
| 742 | case Magic('='): |
| 743 | case Magic('?'): |
| 744 | case Magic('+'): |
| 745 | case Magic('@'): |
| 746 | case Magic('*'): |
| 747 | case Magic('{'): |
| 748 | /* these should follow an atom, not form an atom */ |
| 749 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 750 | EMSGN(_(e_misplaced), no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 751 | return FAIL; |
| 752 | |
| 753 | case Magic('~'): /* previous substitute pattern */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 754 | /* TODO: Not supported yet */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 755 | return FAIL; |
| 756 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 757 | case Magic('1'): |
| 758 | case Magic('2'): |
| 759 | case Magic('3'): |
| 760 | case Magic('4'): |
| 761 | case Magic('5'): |
| 762 | case Magic('6'): |
| 763 | case Magic('7'): |
| 764 | case Magic('8'): |
| 765 | case Magic('9'): |
| 766 | EMIT(NFA_BACKREF1 + (no_Magic(c) - '1')); |
| 767 | nfa_has_backref = TRUE; |
| 768 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 769 | |
| 770 | case Magic('z'): |
| 771 | c = no_Magic(getchr()); |
| 772 | switch (c) |
| 773 | { |
| 774 | case 's': |
| 775 | EMIT(NFA_ZSTART); |
| 776 | break; |
| 777 | case 'e': |
| 778 | EMIT(NFA_ZEND); |
| 779 | nfa_has_zend = TRUE; |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 780 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 781 | case '1': |
| 782 | case '2': |
| 783 | case '3': |
| 784 | case '4': |
| 785 | case '5': |
| 786 | case '6': |
| 787 | case '7': |
| 788 | case '8': |
| 789 | case '9': |
| 790 | case '(': |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 791 | /* TODO: \z1...\z9 and \z( not yet supported */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 792 | return FAIL; |
| 793 | default: |
| 794 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 795 | EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 796 | no_Magic(c)); |
| 797 | return FAIL; |
| 798 | } |
| 799 | break; |
| 800 | |
| 801 | case Magic('%'): |
| 802 | c = no_Magic(getchr()); |
| 803 | switch (c) |
| 804 | { |
| 805 | /* () without a back reference */ |
| 806 | case '(': |
| 807 | if (nfa_reg(REG_NPAREN) == FAIL) |
| 808 | return FAIL; |
| 809 | EMIT(NFA_NOPEN); |
| 810 | break; |
| 811 | |
| 812 | case 'd': /* %d123 decimal */ |
| 813 | case 'o': /* %o123 octal */ |
| 814 | case 'x': /* %xab hex 2 */ |
| 815 | case 'u': /* %uabcd hex 4 */ |
| 816 | case 'U': /* %U1234abcd hex 8 */ |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 817 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 818 | int nr; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 819 | |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 820 | switch (c) |
| 821 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 822 | case 'd': nr = getdecchrs(); break; |
| 823 | case 'o': nr = getoctchrs(); break; |
| 824 | case 'x': nr = gethexchrs(2); break; |
| 825 | case 'u': nr = gethexchrs(4); break; |
| 826 | case 'U': nr = gethexchrs(8); break; |
| 827 | default: nr = -1; break; |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 828 | } |
| 829 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 830 | if (nr < 0) |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 831 | EMSG2_RET_FAIL( |
| 832 | _("E678: Invalid character after %s%%[dxouU]"), |
| 833 | reg_magic == MAGIC_ALL); |
| 834 | /* TODO: what if a composing character follows? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 835 | EMIT(nr); |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 836 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 837 | break; |
| 838 | |
| 839 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 840 | * pattern -- regardless of whether or not it makes sense. */ |
| 841 | case '^': |
| 842 | EMIT(NFA_BOF); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 843 | /* TODO: Not yet supported */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 844 | return FAIL; |
| 845 | break; |
| 846 | |
| 847 | case '$': |
| 848 | EMIT(NFA_EOF); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 849 | /* TODO: Not yet supported */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 850 | return FAIL; |
| 851 | break; |
| 852 | |
| 853 | case '#': |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 854 | EMIT(NFA_CURSOR); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 855 | break; |
| 856 | |
| 857 | case 'V': |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 858 | /* TODO: not supported yet */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 859 | return FAIL; |
| 860 | break; |
| 861 | |
| 862 | case '[': |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 863 | /* TODO: \%[abc] not supported yet */ |
| 864 | return FAIL; |
| 865 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 866 | default: |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 867 | { |
| 868 | long_u n = 0; |
| 869 | int cmp = c; |
| 870 | |
| 871 | if (c == '<' || c == '>') |
| 872 | c = getchr(); |
| 873 | while (VIM_ISDIGIT(c)) |
| 874 | { |
| 875 | n = n * 10 + (c - '0'); |
| 876 | c = getchr(); |
| 877 | } |
| 878 | if (c == 'l' || c == 'c' || c == 'v') |
| 879 | { |
| 880 | EMIT(n); |
| 881 | if (c == 'l') |
| 882 | EMIT(cmp == '<' ? NFA_LNUM_LT : |
| 883 | cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
| 884 | else if (c == 'c') |
| 885 | EMIT(cmp == '<' ? NFA_COL_LT : |
| 886 | cmp == '>' ? NFA_COL_GT : NFA_COL); |
| 887 | else |
| 888 | EMIT(cmp == '<' ? NFA_VCOL_LT : |
| 889 | cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
| 890 | break; |
| 891 | } |
| 892 | else if (c == '\'') |
| 893 | /* TODO: \%'m not supported yet */ |
| 894 | return FAIL; |
| 895 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 896 | syntax_error = TRUE; |
| 897 | EMSGN(_("E867: (NFA) Unknown operator '\\%%%c'"), |
| 898 | no_Magic(c)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 899 | return FAIL; |
| 900 | } |
| 901 | break; |
| 902 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 903 | case Magic('['): |
Bram Moolenaar | 307d10a | 2013-05-23 22:25:15 +0200 | [diff] [blame] | 904 | collection: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 905 | /* |
| 906 | * Glue is emitted between several atoms from the []. |
| 907 | * It is either NFA_OR, or NFA_CONCAT. |
| 908 | * |
| 909 | * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation) |
| 910 | * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT |
| 911 | * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix |
| 912 | * notation) |
| 913 | * |
| 914 | */ |
| 915 | |
| 916 | |
| 917 | /* Emit negation atoms, if needed. |
| 918 | * The CONCAT below merges the NOT with the previous node. */ |
| 919 | #define TRY_NEG() \ |
| 920 | if (negated == TRUE) \ |
| 921 | { \ |
| 922 | EMIT(NFA_NOT); \ |
| 923 | } |
| 924 | |
| 925 | /* Emit glue between important nodes : CONCAT or OR. */ |
| 926 | #define EMIT_GLUE() \ |
| 927 | if (first == FALSE) \ |
| 928 | EMIT(glue); \ |
| 929 | else \ |
| 930 | first = FALSE; |
| 931 | |
| 932 | p = regparse; |
| 933 | endp = skip_anyof(p); |
| 934 | if (*endp == ']') |
| 935 | { |
| 936 | /* |
| 937 | * Try to reverse engineer character classes. For example, |
| 938 | * recognize that [0-9] stands for \d and [A-Za-z_] with \h, |
| 939 | * and perform the necessary substitutions in the NFA. |
| 940 | */ |
| 941 | result = nfa_recognize_char_class(regparse, endp, |
| 942 | extra == ADD_NL); |
| 943 | if (result != FAIL) |
| 944 | { |
| 945 | if (result >= NFA_DIGIT && result <= NFA_NUPPER) |
| 946 | EMIT(result); |
| 947 | else /* must be char class + newline */ |
| 948 | { |
| 949 | EMIT(result - ADD_NL); |
| 950 | EMIT(NFA_NEWL); |
| 951 | EMIT(NFA_OR); |
| 952 | } |
| 953 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 954 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 955 | return OK; |
| 956 | } |
| 957 | /* |
| 958 | * Failed to recognize a character class. Use the simple |
| 959 | * version that turns [abc] into 'a' OR 'b' OR 'c' |
| 960 | */ |
| 961 | startc = endc = oldstartc = -1; |
| 962 | first = TRUE; /* Emitting first atom in this sequence? */ |
| 963 | negated = FALSE; |
| 964 | glue = NFA_OR; |
| 965 | if (*regparse == '^') /* negated range */ |
| 966 | { |
| 967 | negated = TRUE; |
| 968 | glue = NFA_CONCAT; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 969 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 970 | } |
| 971 | if (*regparse == '-') |
| 972 | { |
| 973 | startc = '-'; |
| 974 | EMIT(startc); |
| 975 | TRY_NEG(); |
| 976 | EMIT_GLUE(); |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 977 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 978 | } |
| 979 | /* Emit the OR branches for each character in the [] */ |
| 980 | emit_range = FALSE; |
| 981 | while (regparse < endp) |
| 982 | { |
| 983 | oldstartc = startc; |
| 984 | startc = -1; |
| 985 | got_coll_char = FALSE; |
| 986 | if (*regparse == '[') |
| 987 | { |
| 988 | /* Check for [: :], [= =], [. .] */ |
| 989 | equiclass = collclass = 0; |
| 990 | charclass = get_char_class(®parse); |
| 991 | if (charclass == CLASS_NONE) |
| 992 | { |
| 993 | equiclass = get_equi_class(®parse); |
| 994 | if (equiclass == 0) |
| 995 | collclass = get_coll_element(®parse); |
| 996 | } |
| 997 | |
| 998 | /* Character class like [:alpha:] */ |
| 999 | if (charclass != CLASS_NONE) |
| 1000 | { |
| 1001 | switch (charclass) |
| 1002 | { |
| 1003 | case CLASS_ALNUM: |
| 1004 | EMIT(NFA_CLASS_ALNUM); |
| 1005 | break; |
| 1006 | case CLASS_ALPHA: |
| 1007 | EMIT(NFA_CLASS_ALPHA); |
| 1008 | break; |
| 1009 | case CLASS_BLANK: |
| 1010 | EMIT(NFA_CLASS_BLANK); |
| 1011 | break; |
| 1012 | case CLASS_CNTRL: |
| 1013 | EMIT(NFA_CLASS_CNTRL); |
| 1014 | break; |
| 1015 | case CLASS_DIGIT: |
| 1016 | EMIT(NFA_CLASS_DIGIT); |
| 1017 | break; |
| 1018 | case CLASS_GRAPH: |
| 1019 | EMIT(NFA_CLASS_GRAPH); |
| 1020 | break; |
| 1021 | case CLASS_LOWER: |
| 1022 | EMIT(NFA_CLASS_LOWER); |
| 1023 | break; |
| 1024 | case CLASS_PRINT: |
| 1025 | EMIT(NFA_CLASS_PRINT); |
| 1026 | break; |
| 1027 | case CLASS_PUNCT: |
| 1028 | EMIT(NFA_CLASS_PUNCT); |
| 1029 | break; |
| 1030 | case CLASS_SPACE: |
| 1031 | EMIT(NFA_CLASS_SPACE); |
| 1032 | break; |
| 1033 | case CLASS_UPPER: |
| 1034 | EMIT(NFA_CLASS_UPPER); |
| 1035 | break; |
| 1036 | case CLASS_XDIGIT: |
| 1037 | EMIT(NFA_CLASS_XDIGIT); |
| 1038 | break; |
| 1039 | case CLASS_TAB: |
| 1040 | EMIT(NFA_CLASS_TAB); |
| 1041 | break; |
| 1042 | case CLASS_RETURN: |
| 1043 | EMIT(NFA_CLASS_RETURN); |
| 1044 | break; |
| 1045 | case CLASS_BACKSPACE: |
| 1046 | EMIT(NFA_CLASS_BACKSPACE); |
| 1047 | break; |
| 1048 | case CLASS_ESCAPE: |
| 1049 | EMIT(NFA_CLASS_ESCAPE); |
| 1050 | break; |
| 1051 | } |
| 1052 | TRY_NEG(); |
| 1053 | EMIT_GLUE(); |
| 1054 | continue; |
| 1055 | } |
| 1056 | /* Try equivalence class [=a=] and the like */ |
| 1057 | if (equiclass != 0) |
| 1058 | { |
| 1059 | result = nfa_emit_equi_class(equiclass, negated); |
| 1060 | if (result == FAIL) |
| 1061 | { |
| 1062 | /* should never happen */ |
| 1063 | EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
| 1064 | } |
| 1065 | EMIT_GLUE(); |
| 1066 | continue; |
| 1067 | } |
| 1068 | /* Try collating class like [. .] */ |
| 1069 | if (collclass != 0) |
| 1070 | { |
| 1071 | startc = collclass; /* allow [.a.]-x as a range */ |
| 1072 | /* Will emit the proper atom at the end of the |
| 1073 | * while loop. */ |
| 1074 | } |
| 1075 | } |
| 1076 | /* Try a range like 'a-x' or '\t-z' */ |
| 1077 | if (*regparse == '-') |
| 1078 | { |
| 1079 | emit_range = TRUE; |
| 1080 | startc = oldstartc; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1081 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1082 | continue; /* reading the end of the range */ |
| 1083 | } |
| 1084 | |
| 1085 | /* Now handle simple and escaped characters. |
| 1086 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 1087 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 1088 | * 'cpoptions' is not included. |
| 1089 | * Posix doesn't recognize backslash at all. |
| 1090 | */ |
| 1091 | if (*regparse == '\\' |
| 1092 | && !cpo_bsl |
| 1093 | && regparse + 1 <= endp |
| 1094 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
| 1095 | || (!cpo_lit |
| 1096 | && vim_strchr(REGEXP_ABBR, regparse[1]) |
| 1097 | != NULL) |
| 1098 | ) |
| 1099 | ) |
| 1100 | { |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1101 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1102 | |
Bram Moolenaar | 673af4d | 2013-05-21 22:00:51 +0200 | [diff] [blame] | 1103 | if (*regparse == 'n') |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1104 | startc = reg_string ? NL : NFA_NEWL; |
| 1105 | else |
| 1106 | if (*regparse == 'd' |
| 1107 | || *regparse == 'o' |
| 1108 | || *regparse == 'x' |
| 1109 | || *regparse == 'u' |
| 1110 | || *regparse == 'U' |
| 1111 | ) |
| 1112 | { |
| 1113 | /* TODO(RE) This needs more testing */ |
| 1114 | startc = coll_get_char(); |
| 1115 | got_coll_char = TRUE; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1116 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1117 | } |
| 1118 | else |
| 1119 | { |
| 1120 | /* \r,\t,\e,\b */ |
| 1121 | startc = backslash_trans(*regparse); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | /* Normal printable char */ |
| 1126 | if (startc == -1) |
| 1127 | #ifdef FEAT_MBYTE |
| 1128 | startc = (*mb_ptr2char)(regparse); |
| 1129 | #else |
| 1130 | startc = *regparse; |
| 1131 | #endif |
| 1132 | |
| 1133 | /* Previous char was '-', so this char is end of range. */ |
| 1134 | if (emit_range) |
| 1135 | { |
| 1136 | endc = startc; startc = oldstartc; |
| 1137 | if (startc > endc) |
| 1138 | EMSG_RET_FAIL(_(e_invrange)); |
| 1139 | #ifdef FEAT_MBYTE |
| 1140 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
| 1141 | || (*mb_char2len)(endc) > 1)) |
| 1142 | { |
| 1143 | if (endc > startc + 256) |
| 1144 | EMSG_RET_FAIL(_(e_invrange)); |
| 1145 | /* Emit the range. "startc" was already emitted, so |
| 1146 | * skip it. */ |
| 1147 | for (c = startc + 1; c <= endc; c++) |
| 1148 | { |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1149 | EMIT(c); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1150 | TRY_NEG(); |
| 1151 | EMIT_GLUE(); |
| 1152 | } |
| 1153 | emit_range = FALSE; |
| 1154 | } |
| 1155 | else |
| 1156 | #endif |
| 1157 | { |
| 1158 | #ifdef EBCDIC |
| 1159 | int alpha_only = FALSE; |
| 1160 | |
| 1161 | /* for alphabetical range skip the gaps |
| 1162 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 1163 | if (isalpha(startc) && isalpha(endc)) |
| 1164 | alpha_only = TRUE; |
| 1165 | #endif |
| 1166 | /* Emit the range. "startc" was already emitted, so |
| 1167 | * skip it. */ |
| 1168 | for (c = startc + 1; c <= endc; c++) |
| 1169 | #ifdef EBCDIC |
| 1170 | if (!alpha_only || isalpha(startc)) |
| 1171 | #endif |
| 1172 | { |
| 1173 | EMIT(c); |
| 1174 | TRY_NEG(); |
| 1175 | EMIT_GLUE(); |
| 1176 | } |
| 1177 | emit_range = FALSE; |
| 1178 | } |
| 1179 | } |
| 1180 | else |
| 1181 | { |
| 1182 | /* |
| 1183 | * This char (startc) is not part of a range. Just |
| 1184 | * emit it. |
| 1185 | * |
| 1186 | * Normally, simply emit startc. But if we get char |
| 1187 | * code=0 from a collating char, then replace it with |
| 1188 | * 0x0a. |
| 1189 | * |
| 1190 | * This is needed to completely mimic the behaviour of |
| 1191 | * the backtracking engine. |
| 1192 | */ |
| 1193 | if (got_coll_char == TRUE && startc == 0) |
| 1194 | EMIT(0x0a); |
| 1195 | else |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1196 | EMIT(startc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1197 | TRY_NEG(); |
| 1198 | EMIT_GLUE(); |
| 1199 | } |
| 1200 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1201 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1202 | } /* while (p < endp) */ |
| 1203 | |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1204 | mb_ptr_back(old_regparse, regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1205 | if (*regparse == '-') /* if last, '-' is just a char */ |
| 1206 | { |
| 1207 | EMIT('-'); |
| 1208 | TRY_NEG(); |
| 1209 | EMIT_GLUE(); |
| 1210 | } |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1211 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1212 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1213 | /* skip the trailing ] */ |
| 1214 | regparse = endp; |
Bram Moolenaar | 51a2983 | 2013-05-28 22:30:35 +0200 | [diff] [blame] | 1215 | mb_ptr_adv(regparse); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1216 | if (negated == TRUE) |
| 1217 | { |
| 1218 | /* Mark end of negated char range */ |
| 1219 | EMIT(NFA_END_NEG_RANGE); |
| 1220 | EMIT(NFA_CONCAT); |
| 1221 | } |
Bram Moolenaar | bad704f | 2013-05-30 11:51:08 +0200 | [diff] [blame] | 1222 | |
| 1223 | /* \_[] also matches \n but it's not negated */ |
| 1224 | if (extra == ADD_NL) |
| 1225 | { |
| 1226 | EMIT(reg_string ? NL : NFA_NEWL); |
| 1227 | EMIT(NFA_OR); |
| 1228 | } |
| 1229 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1230 | return OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1231 | } /* if exists closing ] */ |
| 1232 | |
| 1233 | if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1234 | { |
| 1235 | syntax_error = TRUE; |
| 1236 | EMSG_RET_FAIL(_(e_missingbracket)); |
| 1237 | } |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1238 | /* FALLTHROUGH */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1239 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1240 | default: |
| 1241 | { |
| 1242 | #ifdef FEAT_MBYTE |
| 1243 | int plen; |
| 1244 | |
| 1245 | nfa_do_multibyte: |
Bram Moolenaar | 4719658 | 2013-05-25 22:04:23 +0200 | [diff] [blame] | 1246 | /* plen is length of current char with composing chars */ |
| 1247 | if (enc_utf8 && ((*mb_char2len)(c) |
| 1248 | != (plen = (*mb_ptr2len)(old_regparse)) |
| 1249 | || utf_iscomposing(c))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1250 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 1251 | int i = 0; |
| 1252 | |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 1253 | /* A base character plus composing characters, or just one |
| 1254 | * or more composing characters. |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1255 | * This requires creating a separate atom as if enclosing |
| 1256 | * the characters in (), where NFA_COMPOSING is the ( and |
| 1257 | * NFA_END_COMPOSING is the ). Note that right now we are |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1258 | * building the postfix form, not the NFA itself; |
| 1259 | * a composing char could be: a, b, c, NFA_COMPOSING |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1260 | * where 'b' and 'c' are chars with codes > 256. */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1261 | for (;;) |
| 1262 | { |
| 1263 | EMIT(c); |
| 1264 | if (i > 0) |
| 1265 | EMIT(NFA_CONCAT); |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 1266 | if ((i += utf_char2len(c)) >= plen) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 1267 | break; |
| 1268 | c = utf_ptr2char(old_regparse + i); |
| 1269 | } |
| 1270 | EMIT(NFA_COMPOSING); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1271 | regparse = old_regparse + plen; |
| 1272 | } |
| 1273 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1274 | #endif |
| 1275 | { |
| 1276 | c = no_Magic(c); |
| 1277 | EMIT(c); |
| 1278 | } |
| 1279 | return OK; |
| 1280 | } |
| 1281 | } |
| 1282 | |
| 1283 | #undef TRY_NEG |
| 1284 | #undef EMIT_GLUE |
| 1285 | |
| 1286 | return OK; |
| 1287 | } |
| 1288 | |
| 1289 | /* |
| 1290 | * Parse something followed by possible [*+=]. |
| 1291 | * |
| 1292 | * A piece is an atom, possibly followed by a multi, an indication of how many |
| 1293 | * times the atom can be matched. Example: "a*" matches any sequence of "a" |
| 1294 | * characters: "", "a", "aa", etc. |
| 1295 | * |
| 1296 | * piece ::= atom |
| 1297 | * or atom multi |
| 1298 | */ |
| 1299 | static int |
| 1300 | nfa_regpiece() |
| 1301 | { |
| 1302 | int i; |
| 1303 | int op; |
| 1304 | int ret; |
| 1305 | long minval, maxval; |
| 1306 | int greedy = TRUE; /* Braces are prefixed with '-' ? */ |
| 1307 | char_u *old_regparse, *new_regparse; |
| 1308 | int c2; |
| 1309 | int *old_post_ptr, *my_post_start; |
| 1310 | int old_regnpar; |
| 1311 | int quest; |
| 1312 | |
| 1313 | /* Save the current position in the regexp, so that we can use it if |
| 1314 | * <atom>{m,n} is next. */ |
| 1315 | old_regparse = regparse; |
| 1316 | /* Save current number of open parenthesis, so we can use it if |
| 1317 | * <atom>{m,n} is next */ |
| 1318 | old_regnpar = regnpar; |
| 1319 | /* store current pos in the postfix form, for \{m,n} involving 0s */ |
| 1320 | my_post_start = post_ptr; |
| 1321 | |
| 1322 | ret = nfa_regatom(); |
| 1323 | if (ret == FAIL) |
| 1324 | return FAIL; /* cascaded error */ |
| 1325 | |
| 1326 | op = peekchr(); |
| 1327 | if (re_multi_type(op) == NOT_MULTI) |
| 1328 | return OK; |
| 1329 | |
| 1330 | skipchr(); |
| 1331 | switch (op) |
| 1332 | { |
| 1333 | case Magic('*'): |
| 1334 | EMIT(NFA_STAR); |
| 1335 | break; |
| 1336 | |
| 1337 | case Magic('+'): |
| 1338 | /* |
| 1339 | * Trick: Normally, (a*)\+ would match the whole input "aaa". The |
| 1340 | * first and only submatch would be "aaa". But the backtracking |
| 1341 | * engine interprets the plus as "try matching one more time", and |
| 1342 | * a* matches a second time at the end of the input, the empty |
| 1343 | * string. |
| 1344 | * The submatch will the empty string. |
| 1345 | * |
| 1346 | * In order to be consistent with the old engine, we disable |
| 1347 | * NFA_PLUS, and replace <atom>+ with <atom><atom>* |
| 1348 | */ |
| 1349 | /* EMIT(NFA_PLUS); */ |
| 1350 | regnpar = old_regnpar; |
| 1351 | regparse = old_regparse; |
| 1352 | curchr = -1; |
| 1353 | if (nfa_regatom() == FAIL) |
| 1354 | return FAIL; |
| 1355 | EMIT(NFA_STAR); |
| 1356 | EMIT(NFA_CONCAT); |
| 1357 | skipchr(); /* skip the \+ */ |
| 1358 | break; |
| 1359 | |
| 1360 | case Magic('@'): |
| 1361 | op = no_Magic(getchr()); |
| 1362 | switch(op) |
| 1363 | { |
| 1364 | case '=': |
| 1365 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
| 1366 | break; |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 1367 | case '0': |
| 1368 | case '1': |
| 1369 | case '2': |
| 1370 | case '3': |
| 1371 | case '4': |
| 1372 | case '5': |
| 1373 | case '6': |
| 1374 | case '7': |
| 1375 | case '8': |
| 1376 | case '9': |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1377 | case '!': |
| 1378 | case '<': |
| 1379 | case '>': |
| 1380 | /* Not supported yet */ |
| 1381 | return FAIL; |
| 1382 | default: |
| 1383 | syntax_error = TRUE; |
Bram Moolenaar | ba40447 | 2013-05-19 22:31:18 +0200 | [diff] [blame] | 1384 | EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1385 | return FAIL; |
| 1386 | } |
| 1387 | break; |
| 1388 | |
| 1389 | case Magic('?'): |
| 1390 | case Magic('='): |
| 1391 | EMIT(NFA_QUEST); |
| 1392 | break; |
| 1393 | |
| 1394 | case Magic('{'): |
| 1395 | /* a{2,5} will expand to 'aaa?a?a?' |
| 1396 | * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
| 1397 | * version of '?' |
| 1398 | * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
| 1399 | * parenthesis have the same id |
| 1400 | */ |
| 1401 | |
| 1402 | greedy = TRUE; |
| 1403 | c2 = peekchr(); |
| 1404 | if (c2 == '-' || c2 == Magic('-')) |
| 1405 | { |
| 1406 | skipchr(); |
| 1407 | greedy = FALSE; |
| 1408 | } |
| 1409 | if (!read_limits(&minval, &maxval)) |
| 1410 | { |
| 1411 | syntax_error = TRUE; |
| 1412 | EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
| 1413 | } |
| 1414 | /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
| 1415 | * <atom>* */ |
| 1416 | if (minval == 0 && maxval == MAX_LIMIT && greedy) |
| 1417 | { |
| 1418 | EMIT(NFA_STAR); |
| 1419 | break; |
| 1420 | } |
| 1421 | |
| 1422 | if (maxval > NFA_BRACES_MAXLIMIT) |
| 1423 | { |
| 1424 | /* This would yield a huge automaton and use too much memory. |
| 1425 | * Revert to old engine */ |
| 1426 | return FAIL; |
| 1427 | } |
| 1428 | |
| 1429 | /* Special case: x{0} or x{-0} */ |
| 1430 | if (maxval == 0) |
| 1431 | { |
| 1432 | /* Ignore result of previous call to nfa_regatom() */ |
| 1433 | post_ptr = my_post_start; |
| 1434 | /* NFA_SKIP_CHAR has 0-length and works everywhere */ |
| 1435 | EMIT(NFA_SKIP_CHAR); |
| 1436 | return OK; |
| 1437 | } |
| 1438 | |
| 1439 | /* Ignore previous call to nfa_regatom() */ |
| 1440 | post_ptr = my_post_start; |
| 1441 | /* Save pos after the repeated atom and the \{} */ |
| 1442 | new_regparse = regparse; |
| 1443 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1444 | quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
| 1445 | for (i = 0; i < maxval; i++) |
| 1446 | { |
| 1447 | /* Goto beginning of the repeated atom */ |
| 1448 | regparse = old_regparse; |
| 1449 | curchr = -1; |
| 1450 | /* Restore count of parenthesis */ |
| 1451 | regnpar = old_regnpar; |
| 1452 | old_post_ptr = post_ptr; |
| 1453 | if (nfa_regatom() == FAIL) |
| 1454 | return FAIL; |
| 1455 | /* after "minval" times, atoms are optional */ |
| 1456 | if (i + 1 > minval) |
| 1457 | EMIT(quest); |
| 1458 | if (old_post_ptr != my_post_start) |
| 1459 | EMIT(NFA_CONCAT); |
| 1460 | } |
| 1461 | |
| 1462 | /* Go to just after the repeated atom and the \{} */ |
| 1463 | regparse = new_regparse; |
| 1464 | curchr = -1; |
| 1465 | |
| 1466 | break; |
| 1467 | |
| 1468 | |
| 1469 | default: |
| 1470 | break; |
| 1471 | } /* end switch */ |
| 1472 | |
| 1473 | if (re_multi_type(peekchr()) != NOT_MULTI) |
| 1474 | { |
| 1475 | /* Can't have a multi follow a multi. */ |
| 1476 | syntax_error = TRUE; |
| 1477 | EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !")); |
| 1478 | } |
| 1479 | |
| 1480 | return OK; |
| 1481 | } |
| 1482 | |
| 1483 | /* |
| 1484 | * Parse one or more pieces, concatenated. It matches a match for the |
| 1485 | * first piece, followed by a match for the second piece, etc. Example: |
| 1486 | * "f[0-9]b", first matches "f", then a digit and then "b". |
| 1487 | * |
| 1488 | * concat ::= piece |
| 1489 | * or piece piece |
| 1490 | * or piece piece piece |
| 1491 | * etc. |
| 1492 | */ |
| 1493 | static int |
| 1494 | nfa_regconcat() |
| 1495 | { |
| 1496 | int cont = TRUE; |
| 1497 | int first = TRUE; |
| 1498 | |
| 1499 | while (cont) |
| 1500 | { |
| 1501 | switch (peekchr()) |
| 1502 | { |
| 1503 | case NUL: |
| 1504 | case Magic('|'): |
| 1505 | case Magic('&'): |
| 1506 | case Magic(')'): |
| 1507 | cont = FALSE; |
| 1508 | break; |
| 1509 | |
| 1510 | case Magic('Z'): |
| 1511 | #ifdef FEAT_MBYTE |
| 1512 | regflags |= RF_ICOMBINE; |
| 1513 | #endif |
| 1514 | skipchr_keepstart(); |
| 1515 | break; |
| 1516 | case Magic('c'): |
| 1517 | regflags |= RF_ICASE; |
| 1518 | skipchr_keepstart(); |
| 1519 | break; |
| 1520 | case Magic('C'): |
| 1521 | regflags |= RF_NOICASE; |
| 1522 | skipchr_keepstart(); |
| 1523 | break; |
| 1524 | case Magic('v'): |
| 1525 | reg_magic = MAGIC_ALL; |
| 1526 | skipchr_keepstart(); |
| 1527 | curchr = -1; |
| 1528 | break; |
| 1529 | case Magic('m'): |
| 1530 | reg_magic = MAGIC_ON; |
| 1531 | skipchr_keepstart(); |
| 1532 | curchr = -1; |
| 1533 | break; |
| 1534 | case Magic('M'): |
| 1535 | reg_magic = MAGIC_OFF; |
| 1536 | skipchr_keepstart(); |
| 1537 | curchr = -1; |
| 1538 | break; |
| 1539 | case Magic('V'): |
| 1540 | reg_magic = MAGIC_NONE; |
| 1541 | skipchr_keepstart(); |
| 1542 | curchr = -1; |
| 1543 | break; |
| 1544 | |
| 1545 | default: |
| 1546 | if (nfa_regpiece() == FAIL) |
| 1547 | return FAIL; |
| 1548 | if (first == FALSE) |
| 1549 | EMIT(NFA_CONCAT); |
| 1550 | else |
| 1551 | first = FALSE; |
| 1552 | break; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | return OK; |
| 1557 | } |
| 1558 | |
| 1559 | /* |
| 1560 | * Parse a branch, one or more concats, separated by "\&". It matches the |
| 1561 | * last concat, but only if all the preceding concats also match at the same |
| 1562 | * position. Examples: |
| 1563 | * "foobeep\&..." matches "foo" in "foobeep". |
| 1564 | * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" |
| 1565 | * |
| 1566 | * branch ::= concat |
| 1567 | * or concat \& concat |
| 1568 | * or concat \& concat \& concat |
| 1569 | * etc. |
| 1570 | */ |
| 1571 | static int |
| 1572 | nfa_regbranch() |
| 1573 | { |
| 1574 | int ch; |
| 1575 | int *old_post_ptr; |
| 1576 | |
| 1577 | old_post_ptr = post_ptr; |
| 1578 | |
| 1579 | /* First branch, possibly the only one */ |
| 1580 | if (nfa_regconcat() == FAIL) |
| 1581 | return FAIL; |
| 1582 | |
| 1583 | ch = peekchr(); |
| 1584 | /* Try next concats */ |
| 1585 | while (ch == Magic('&')) |
| 1586 | { |
| 1587 | skipchr(); |
| 1588 | EMIT(NFA_NOPEN); |
| 1589 | EMIT(NFA_PREV_ATOM_NO_WIDTH); |
| 1590 | old_post_ptr = post_ptr; |
| 1591 | if (nfa_regconcat() == FAIL) |
| 1592 | return FAIL; |
| 1593 | /* if concat is empty, skip a input char. But do emit a node */ |
| 1594 | if (old_post_ptr == post_ptr) |
| 1595 | EMIT(NFA_SKIP_CHAR); |
| 1596 | EMIT(NFA_CONCAT); |
| 1597 | ch = peekchr(); |
| 1598 | } |
| 1599 | |
| 1600 | /* Even if a branch is empty, emit one node for it */ |
| 1601 | if (old_post_ptr == post_ptr) |
| 1602 | EMIT(NFA_SKIP_CHAR); |
| 1603 | |
| 1604 | return OK; |
| 1605 | } |
| 1606 | |
| 1607 | /* |
| 1608 | * Parse a pattern, one or more branches, separated by "\|". It matches |
| 1609 | * anything that matches one of the branches. Example: "foo\|beep" matches |
| 1610 | * "foo" and matches "beep". If more than one branch matches, the first one |
| 1611 | * is used. |
| 1612 | * |
| 1613 | * pattern ::= branch |
| 1614 | * or branch \| branch |
| 1615 | * or branch \| branch \| branch |
| 1616 | * etc. |
| 1617 | */ |
| 1618 | static int |
| 1619 | nfa_reg(paren) |
| 1620 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 1621 | { |
| 1622 | int parno = 0; |
| 1623 | |
| 1624 | #ifdef FEAT_SYN_HL |
| 1625 | #endif |
| 1626 | if (paren == REG_PAREN) |
| 1627 | { |
| 1628 | if (regnpar >= NSUBEXP) /* Too many `(' */ |
| 1629 | { |
| 1630 | syntax_error = TRUE; |
| 1631 | EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
| 1632 | } |
| 1633 | parno = regnpar++; |
| 1634 | } |
| 1635 | |
| 1636 | if (nfa_regbranch() == FAIL) |
| 1637 | return FAIL; /* cascaded error */ |
| 1638 | |
| 1639 | while (peekchr() == Magic('|')) |
| 1640 | { |
| 1641 | skipchr(); |
| 1642 | if (nfa_regbranch() == FAIL) |
| 1643 | return FAIL; /* cascaded error */ |
| 1644 | EMIT(NFA_OR); |
| 1645 | } |
| 1646 | |
| 1647 | /* Check for proper termination. */ |
| 1648 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 1649 | { |
| 1650 | syntax_error = TRUE; |
| 1651 | if (paren == REG_NPAREN) |
| 1652 | EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
| 1653 | else |
| 1654 | EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
| 1655 | } |
| 1656 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 1657 | { |
| 1658 | syntax_error = TRUE; |
| 1659 | if (peekchr() == Magic(')')) |
| 1660 | EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
| 1661 | else |
| 1662 | EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); |
| 1663 | } |
| 1664 | /* |
| 1665 | * Here we set the flag allowing back references to this set of |
| 1666 | * parentheses. |
| 1667 | */ |
| 1668 | if (paren == REG_PAREN) |
| 1669 | { |
| 1670 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 1671 | EMIT(NFA_MOPEN + parno); |
| 1672 | } |
| 1673 | |
| 1674 | return OK; |
| 1675 | } |
| 1676 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1677 | #ifdef DEBUG |
| 1678 | static char_u code[50]; |
| 1679 | |
| 1680 | static void |
| 1681 | nfa_set_code(c) |
| 1682 | int c; |
| 1683 | { |
| 1684 | int addnl = FALSE; |
| 1685 | |
| 1686 | if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) |
| 1687 | { |
| 1688 | addnl = TRUE; |
| 1689 | c -= ADD_NL; |
| 1690 | } |
| 1691 | |
| 1692 | STRCPY(code, ""); |
| 1693 | switch (c) |
| 1694 | { |
| 1695 | case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; |
| 1696 | case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; |
| 1697 | case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; |
| 1698 | case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; |
| 1699 | case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; |
| 1700 | case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; |
| 1701 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 1702 | case NFA_BACKREF1: STRCPY(code, "NFA_BACKREF1"); break; |
| 1703 | case NFA_BACKREF2: STRCPY(code, "NFA_BACKREF2"); break; |
| 1704 | case NFA_BACKREF3: STRCPY(code, "NFA_BACKREF3"); break; |
| 1705 | case NFA_BACKREF4: STRCPY(code, "NFA_BACKREF4"); break; |
| 1706 | case NFA_BACKREF5: STRCPY(code, "NFA_BACKREF5"); break; |
| 1707 | case NFA_BACKREF6: STRCPY(code, "NFA_BACKREF6"); break; |
| 1708 | case NFA_BACKREF7: STRCPY(code, "NFA_BACKREF7"); break; |
| 1709 | case NFA_BACKREF8: STRCPY(code, "NFA_BACKREF8"); break; |
| 1710 | case NFA_BACKREF9: STRCPY(code, "NFA_BACKREF9"); break; |
| 1711 | case NFA_SKIP: STRCPY(code, "NFA_SKIP"); break; |
| 1712 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1713 | case NFA_PREV_ATOM_NO_WIDTH: |
| 1714 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 1715 | case NFA_PREV_ATOM_NO_WIDTH_NEG: |
| 1716 | STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH_NEG"); break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1717 | case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break; |
| 1718 | case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break; |
| 1719 | case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; |
| 1720 | case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; |
| 1721 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1722 | case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; |
| 1723 | case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; |
| 1724 | |
| 1725 | case NFA_MOPEN + 0: |
| 1726 | case NFA_MOPEN + 1: |
| 1727 | case NFA_MOPEN + 2: |
| 1728 | case NFA_MOPEN + 3: |
| 1729 | case NFA_MOPEN + 4: |
| 1730 | case NFA_MOPEN + 5: |
| 1731 | case NFA_MOPEN + 6: |
| 1732 | case NFA_MOPEN + 7: |
| 1733 | case NFA_MOPEN + 8: |
| 1734 | case NFA_MOPEN + 9: |
| 1735 | STRCPY(code, "NFA_MOPEN(x)"); |
| 1736 | code[10] = c - NFA_MOPEN + '0'; |
| 1737 | break; |
| 1738 | case NFA_MCLOSE + 0: |
| 1739 | case NFA_MCLOSE + 1: |
| 1740 | case NFA_MCLOSE + 2: |
| 1741 | case NFA_MCLOSE + 3: |
| 1742 | case NFA_MCLOSE + 4: |
| 1743 | case NFA_MCLOSE + 5: |
| 1744 | case NFA_MCLOSE + 6: |
| 1745 | case NFA_MCLOSE + 7: |
| 1746 | case NFA_MCLOSE + 8: |
| 1747 | case NFA_MCLOSE + 9: |
| 1748 | STRCPY(code, "NFA_MCLOSE(x)"); |
| 1749 | code[11] = c - NFA_MCLOSE + '0'; |
| 1750 | break; |
| 1751 | case NFA_EOL: STRCPY(code, "NFA_EOL "); break; |
| 1752 | case NFA_BOL: STRCPY(code, "NFA_BOL "); break; |
| 1753 | case NFA_EOW: STRCPY(code, "NFA_EOW "); break; |
| 1754 | case NFA_BOW: STRCPY(code, "NFA_BOW "); break; |
| 1755 | case NFA_STAR: STRCPY(code, "NFA_STAR "); break; |
| 1756 | case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break; |
| 1757 | case NFA_NOT: STRCPY(code, "NFA_NOT "); break; |
| 1758 | case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; |
| 1759 | case NFA_OR: STRCPY(code, "NFA_OR"); break; |
| 1760 | case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; |
| 1761 | case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; |
| 1762 | case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break; |
| 1763 | case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; |
| 1764 | case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; |
| 1765 | case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; |
| 1766 | case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; |
| 1767 | case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; |
| 1768 | case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; |
| 1769 | case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; |
| 1770 | case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; |
| 1771 | case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; |
| 1772 | case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; |
| 1773 | case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; |
| 1774 | case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; |
| 1775 | case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; |
| 1776 | case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; |
| 1777 | case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; |
| 1778 | case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; |
| 1779 | |
| 1780 | case NFA_ANY: STRCPY(code, "NFA_ANY"); break; |
| 1781 | case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; |
| 1782 | case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; |
| 1783 | case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; |
| 1784 | case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; |
| 1785 | case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; |
| 1786 | case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; |
| 1787 | case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; |
| 1788 | case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; |
| 1789 | case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; |
| 1790 | case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; |
| 1791 | case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; |
| 1792 | case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; |
| 1793 | case NFA_HEX: STRCPY(code, "NFA_HEX"); break; |
| 1794 | case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; |
| 1795 | case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; |
| 1796 | case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; |
| 1797 | case NFA_WORD: STRCPY(code, "NFA_WORD"); break; |
| 1798 | case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; |
| 1799 | case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; |
| 1800 | case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; |
| 1801 | case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; |
| 1802 | case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; |
| 1803 | case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; |
| 1804 | case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; |
| 1805 | case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; |
| 1806 | case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; |
| 1807 | |
| 1808 | default: |
| 1809 | STRCPY(code, "CHAR(x)"); |
| 1810 | code[5] = c; |
| 1811 | } |
| 1812 | |
| 1813 | if (addnl == TRUE) |
| 1814 | STRCAT(code, " + NEWLINE "); |
| 1815 | |
| 1816 | } |
| 1817 | |
| 1818 | #ifdef ENABLE_LOG |
| 1819 | static FILE *log_fd; |
| 1820 | |
| 1821 | /* |
| 1822 | * Print the postfix notation of the current regexp. |
| 1823 | */ |
| 1824 | static void |
| 1825 | nfa_postfix_dump(expr, retval) |
| 1826 | char_u *expr; |
| 1827 | int retval; |
| 1828 | { |
| 1829 | int *p; |
| 1830 | FILE *f; |
| 1831 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 1832 | f = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1833 | if (f != NULL) |
| 1834 | { |
| 1835 | fprintf(f, "\n-------------------------\n"); |
| 1836 | if (retval == FAIL) |
| 1837 | fprintf(f, ">>> NFA engine failed ... \n"); |
| 1838 | else if (retval == OK) |
| 1839 | fprintf(f, ">>> NFA engine succeeded !\n"); |
| 1840 | fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 1841 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1842 | { |
| 1843 | nfa_set_code(*p); |
| 1844 | fprintf(f, "%s, ", code); |
| 1845 | } |
| 1846 | fprintf(f, "\"\nPostfix notation (int): "); |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 1847 | for (p = post_start; *p && p < post_end; p++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1848 | fprintf(f, "%d ", *p); |
| 1849 | fprintf(f, "\n\n"); |
| 1850 | fclose(f); |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | /* |
| 1855 | * Print the NFA starting with a root node "state". |
| 1856 | */ |
| 1857 | static void |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1858 | nfa_print_state(debugf, state) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1859 | FILE *debugf; |
| 1860 | nfa_state_T *state; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1861 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1862 | garray_T indent; |
| 1863 | |
| 1864 | ga_init2(&indent, 1, 64); |
| 1865 | ga_append(&indent, '\0'); |
| 1866 | nfa_print_state2(debugf, state, &indent); |
| 1867 | ga_clear(&indent); |
| 1868 | } |
| 1869 | |
| 1870 | static void |
| 1871 | nfa_print_state2(debugf, state, indent) |
| 1872 | FILE *debugf; |
| 1873 | nfa_state_T *state; |
| 1874 | garray_T *indent; |
| 1875 | { |
| 1876 | char_u *p; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1877 | |
| 1878 | if (state == NULL) |
| 1879 | return; |
| 1880 | |
| 1881 | fprintf(debugf, "(%2d)", abs(state->id)); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1882 | |
| 1883 | /* Output indent */ |
| 1884 | p = (char_u *)indent->ga_data; |
| 1885 | if (indent->ga_len >= 3) |
| 1886 | { |
| 1887 | int last = indent->ga_len - 3; |
| 1888 | char_u save[2]; |
| 1889 | |
| 1890 | STRNCPY(save, &p[last], 2); |
| 1891 | STRNCPY(&p[last], "+-", 2); |
| 1892 | fprintf(debugf, " %s", p); |
| 1893 | STRNCPY(&p[last], save, 2); |
| 1894 | } |
| 1895 | else |
| 1896 | fprintf(debugf, " %s", p); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1897 | |
| 1898 | nfa_set_code(state->c); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1899 | fprintf(debugf, "%s%s (%d) (id=%d)\n", |
| 1900 | state->negated ? "NOT " : "", code, state->c, abs(state->id)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1901 | if (state->id < 0) |
| 1902 | return; |
| 1903 | |
| 1904 | state->id = abs(state->id) * -1; |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1905 | |
| 1906 | /* grow indent for state->out */ |
| 1907 | indent->ga_len -= 1; |
| 1908 | if (state->out1) |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 1909 | ga_concat(indent, (char_u *)"| "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1910 | else |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 1911 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1912 | ga_append(indent, '\0'); |
| 1913 | |
| 1914 | nfa_print_state2(debugf, state->out, indent); |
| 1915 | |
| 1916 | /* replace last part of indent for state->out1 */ |
| 1917 | indent->ga_len -= 3; |
Bram Moolenaar | f47ca63 | 2013-05-25 15:31:05 +0200 | [diff] [blame] | 1918 | ga_concat(indent, (char_u *)" "); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1919 | ga_append(indent, '\0'); |
| 1920 | |
| 1921 | nfa_print_state2(debugf, state->out1, indent); |
| 1922 | |
| 1923 | /* shrink indent */ |
| 1924 | indent->ga_len -= 3; |
| 1925 | ga_append(indent, '\0'); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * Print the NFA state machine. |
| 1930 | */ |
| 1931 | static void |
| 1932 | nfa_dump(prog) |
| 1933 | nfa_regprog_T *prog; |
| 1934 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 1935 | FILE *debugf = fopen(NFA_REGEXP_DUMP_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1936 | |
| 1937 | if (debugf != NULL) |
| 1938 | { |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 1939 | nfa_print_state(debugf, prog->start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1940 | fclose(debugf); |
| 1941 | } |
| 1942 | } |
| 1943 | #endif /* ENABLE_LOG */ |
| 1944 | #endif /* DEBUG */ |
| 1945 | |
| 1946 | /* |
| 1947 | * Parse r.e. @expr and convert it into postfix form. |
| 1948 | * Return the postfix string on success, NULL otherwise. |
| 1949 | */ |
| 1950 | static int * |
| 1951 | re2post() |
| 1952 | { |
| 1953 | if (nfa_reg(REG_NOPAREN) == FAIL) |
| 1954 | return NULL; |
| 1955 | EMIT(NFA_MOPEN); |
| 1956 | return post_start; |
| 1957 | } |
| 1958 | |
| 1959 | /* NB. Some of the code below is inspired by Russ's. */ |
| 1960 | |
| 1961 | /* |
| 1962 | * Represents an NFA state plus zero or one or two arrows exiting. |
| 1963 | * if c == MATCH, no arrows out; matching state. |
| 1964 | * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
| 1965 | * If c < 256, labeled arrow with character c to out. |
| 1966 | */ |
| 1967 | |
| 1968 | static nfa_state_T *state_ptr; /* points to nfa_prog->state */ |
| 1969 | |
| 1970 | /* |
| 1971 | * Allocate and initialize nfa_state_T. |
| 1972 | */ |
| 1973 | static nfa_state_T * |
| 1974 | new_state(c, out, out1) |
| 1975 | int c; |
| 1976 | nfa_state_T *out; |
| 1977 | nfa_state_T *out1; |
| 1978 | { |
| 1979 | nfa_state_T *s; |
| 1980 | |
| 1981 | if (istate >= nstate) |
| 1982 | return NULL; |
| 1983 | |
| 1984 | s = &state_ptr[istate++]; |
| 1985 | |
| 1986 | s->c = c; |
| 1987 | s->out = out; |
| 1988 | s->out1 = out1; |
| 1989 | |
| 1990 | s->id = istate; |
| 1991 | s->lastlist = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1992 | s->negated = FALSE; |
| 1993 | |
| 1994 | return s; |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * A partially built NFA without the matching state filled in. |
| 1999 | * Frag_T.start points at the start state. |
| 2000 | * Frag_T.out is a list of places that need to be set to the |
| 2001 | * next state for this fragment. |
| 2002 | */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* Since the out pointers in the list are always |
| 2005 | * uninitialized, we use the pointers themselves |
| 2006 | * as storage for the Ptrlists. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2007 | typedef union Ptrlist Ptrlist; |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2008 | union Ptrlist |
| 2009 | { |
| 2010 | Ptrlist *next; |
| 2011 | nfa_state_T *s; |
| 2012 | }; |
| 2013 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2014 | struct Frag |
| 2015 | { |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 2016 | nfa_state_T *start; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2017 | Ptrlist *out; |
| 2018 | }; |
| 2019 | typedef struct Frag Frag_T; |
| 2020 | |
| 2021 | static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); |
| 2022 | static Ptrlist *list1 __ARGS((nfa_state_T **outp)); |
| 2023 | static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); |
| 2024 | static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); |
| 2025 | static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); |
| 2026 | static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); |
| 2027 | |
| 2028 | /* |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2029 | * Initialize a Frag_T struct and return it. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2030 | */ |
| 2031 | static Frag_T |
| 2032 | frag(start, out) |
| 2033 | nfa_state_T *start; |
| 2034 | Ptrlist *out; |
| 2035 | { |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2036 | Frag_T n; |
| 2037 | |
| 2038 | n.start = start; |
| 2039 | n.out = out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2040 | return n; |
| 2041 | } |
| 2042 | |
| 2043 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2044 | * Create singleton list containing just outp. |
| 2045 | */ |
| 2046 | static Ptrlist * |
| 2047 | list1(outp) |
| 2048 | nfa_state_T **outp; |
| 2049 | { |
| 2050 | Ptrlist *l; |
| 2051 | |
| 2052 | l = (Ptrlist *)outp; |
| 2053 | l->next = NULL; |
| 2054 | return l; |
| 2055 | } |
| 2056 | |
| 2057 | /* |
| 2058 | * Patch the list of states at out to point to start. |
| 2059 | */ |
| 2060 | static void |
| 2061 | patch(l, s) |
| 2062 | Ptrlist *l; |
| 2063 | nfa_state_T *s; |
| 2064 | { |
| 2065 | Ptrlist *next; |
| 2066 | |
| 2067 | for (; l; l = next) |
| 2068 | { |
| 2069 | next = l->next; |
| 2070 | l->s = s; |
| 2071 | } |
| 2072 | } |
| 2073 | |
| 2074 | |
| 2075 | /* |
| 2076 | * Join the two lists l1 and l2, returning the combination. |
| 2077 | */ |
| 2078 | static Ptrlist * |
| 2079 | append(l1, l2) |
| 2080 | Ptrlist *l1; |
| 2081 | Ptrlist *l2; |
| 2082 | { |
| 2083 | Ptrlist *oldl1; |
| 2084 | |
| 2085 | oldl1 = l1; |
| 2086 | while (l1->next) |
| 2087 | l1 = l1->next; |
| 2088 | l1->next = l2; |
| 2089 | return oldl1; |
| 2090 | } |
| 2091 | |
| 2092 | /* |
| 2093 | * Stack used for transforming postfix form into NFA. |
| 2094 | */ |
| 2095 | static Frag_T empty; |
| 2096 | |
| 2097 | static void |
| 2098 | st_error(postfix, end, p) |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2099 | int *postfix UNUSED; |
| 2100 | int *end UNUSED; |
| 2101 | int *p UNUSED; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2102 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2103 | #ifdef NFA_REGEXP_ERROR_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2104 | FILE *df; |
| 2105 | int *p2; |
| 2106 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2107 | df = fopen(NFA_REGEXP_ERROR_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2108 | if (df) |
| 2109 | { |
| 2110 | fprintf(df, "Error popping the stack!\n"); |
| 2111 | #ifdef DEBUG |
| 2112 | fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); |
| 2113 | #endif |
| 2114 | fprintf(df, "Postfix form is: "); |
| 2115 | #ifdef DEBUG |
| 2116 | for (p2 = postfix; p2 < end; p2++) |
| 2117 | { |
| 2118 | nfa_set_code(*p2); |
| 2119 | fprintf(df, "%s, ", code); |
| 2120 | } |
| 2121 | nfa_set_code(*p); |
| 2122 | fprintf(df, "\nCurrent position is: "); |
| 2123 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2124 | { |
| 2125 | nfa_set_code(*p2); |
| 2126 | fprintf(df, "%s, ", code); |
| 2127 | } |
| 2128 | #else |
| 2129 | for (p2 = postfix; p2 < end; p2++) |
| 2130 | { |
| 2131 | fprintf(df, "%d, ", *p2); |
| 2132 | } |
| 2133 | fprintf(df, "\nCurrent position is: "); |
| 2134 | for (p2 = postfix; p2 <= p; p2 ++) |
| 2135 | { |
| 2136 | fprintf(df, "%d, ", *p2); |
| 2137 | } |
| 2138 | #endif |
| 2139 | fprintf(df, "\n--------------------------\n"); |
| 2140 | fclose(df); |
| 2141 | } |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2142 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2143 | EMSG(_("E874: (NFA) Could not pop the stack !")); |
| 2144 | } |
| 2145 | |
| 2146 | /* |
| 2147 | * Push an item onto the stack. |
| 2148 | */ |
| 2149 | static void |
| 2150 | st_push(s, p, stack_end) |
| 2151 | Frag_T s; |
| 2152 | Frag_T **p; |
| 2153 | Frag_T *stack_end; |
| 2154 | { |
| 2155 | Frag_T *stackp = *p; |
| 2156 | |
| 2157 | if (stackp >= stack_end) |
| 2158 | return; |
| 2159 | *stackp = s; |
| 2160 | *p = *p + 1; |
| 2161 | } |
| 2162 | |
| 2163 | /* |
| 2164 | * Pop an item from the stack. |
| 2165 | */ |
| 2166 | static Frag_T |
| 2167 | st_pop(p, stack) |
| 2168 | Frag_T **p; |
| 2169 | Frag_T *stack; |
| 2170 | { |
| 2171 | Frag_T *stackp; |
| 2172 | |
| 2173 | *p = *p - 1; |
| 2174 | stackp = *p; |
| 2175 | if (stackp < stack) |
| 2176 | return empty; |
| 2177 | return **p; |
| 2178 | } |
| 2179 | |
| 2180 | /* |
| 2181 | * Convert a postfix form into its equivalent NFA. |
| 2182 | * Return the NFA start state on success, NULL otherwise. |
| 2183 | */ |
| 2184 | static nfa_state_T * |
| 2185 | post2nfa(postfix, end, nfa_calc_size) |
| 2186 | int *postfix; |
| 2187 | int *end; |
| 2188 | int nfa_calc_size; |
| 2189 | { |
| 2190 | int *p; |
| 2191 | int mopen; |
| 2192 | int mclose; |
| 2193 | Frag_T *stack = NULL; |
| 2194 | Frag_T *stackp = NULL; |
| 2195 | Frag_T *stack_end = NULL; |
| 2196 | Frag_T e1; |
| 2197 | Frag_T e2; |
| 2198 | Frag_T e; |
| 2199 | nfa_state_T *s; |
| 2200 | nfa_state_T *s1; |
| 2201 | nfa_state_T *matchstate; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2202 | nfa_state_T *ret = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2203 | |
| 2204 | if (postfix == NULL) |
| 2205 | return NULL; |
| 2206 | |
Bram Moolenaar | 053bb60 | 2013-05-20 13:55:21 +0200 | [diff] [blame] | 2207 | #define PUSH(s) st_push((s), &stackp, stack_end) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2208 | #define POP() st_pop(&stackp, stack); \ |
| 2209 | if (stackp < stack) \ |
| 2210 | { \ |
| 2211 | st_error(postfix, end, p); \ |
| 2212 | return NULL; \ |
| 2213 | } |
| 2214 | |
| 2215 | if (nfa_calc_size == FALSE) |
| 2216 | { |
| 2217 | /* Allocate space for the stack. Max states on the stack : nstate */ |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2218 | stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2219 | stackp = stack; |
Bram Moolenaar | e3c7b86 | 2013-05-20 21:57:03 +0200 | [diff] [blame] | 2220 | stack_end = stack + (nstate + 1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | for (p = postfix; p < end; ++p) |
| 2224 | { |
| 2225 | switch (*p) |
| 2226 | { |
| 2227 | case NFA_CONCAT: |
| 2228 | /* Catenation. |
| 2229 | * Pay attention: this operator does not exist |
| 2230 | * in the r.e. itself (it is implicit, really). |
| 2231 | * It is added when r.e. is translated to postfix |
| 2232 | * form in re2post(). |
| 2233 | * |
| 2234 | * No new state added here. */ |
| 2235 | if (nfa_calc_size == TRUE) |
| 2236 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2237 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2238 | break; |
| 2239 | } |
| 2240 | e2 = POP(); |
| 2241 | e1 = POP(); |
| 2242 | patch(e1.out, e2.start); |
| 2243 | PUSH(frag(e1.start, e2.out)); |
| 2244 | break; |
| 2245 | |
| 2246 | case NFA_NOT: |
| 2247 | /* Negation of a character */ |
| 2248 | if (nfa_calc_size == TRUE) |
| 2249 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2250 | /* nstate += 0; */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2251 | break; |
| 2252 | } |
| 2253 | e1 = POP(); |
| 2254 | e1.start->negated = TRUE; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2255 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2256 | if (e1.start->c == NFA_COMPOSING) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2257 | e1.start->out1->negated = TRUE; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2258 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2259 | PUSH(e1); |
| 2260 | break; |
| 2261 | |
| 2262 | case NFA_OR: |
| 2263 | /* Alternation */ |
| 2264 | if (nfa_calc_size == TRUE) |
| 2265 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2266 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2267 | break; |
| 2268 | } |
| 2269 | e2 = POP(); |
| 2270 | e1 = POP(); |
| 2271 | s = new_state(NFA_SPLIT, e1.start, e2.start); |
| 2272 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2273 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2274 | PUSH(frag(s, append(e1.out, e2.out))); |
| 2275 | break; |
| 2276 | |
| 2277 | case NFA_STAR: |
| 2278 | /* Zero or more */ |
| 2279 | if (nfa_calc_size == TRUE) |
| 2280 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2281 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2282 | break; |
| 2283 | } |
| 2284 | e = POP(); |
| 2285 | s = new_state(NFA_SPLIT, e.start, NULL); |
| 2286 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2287 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2288 | patch(e.out, s); |
| 2289 | PUSH(frag(s, list1(&s->out1))); |
| 2290 | break; |
| 2291 | |
| 2292 | case NFA_QUEST: |
| 2293 | /* one or zero atoms=> greedy match */ |
| 2294 | if (nfa_calc_size == TRUE) |
| 2295 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2296 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2297 | break; |
| 2298 | } |
| 2299 | e = POP(); |
| 2300 | s = new_state(NFA_SPLIT, e.start, NULL); |
| 2301 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2302 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2303 | PUSH(frag(s, append(e.out, list1(&s->out1)))); |
| 2304 | break; |
| 2305 | |
| 2306 | case NFA_QUEST_NONGREEDY: |
| 2307 | /* zero or one atoms => non-greedy match */ |
| 2308 | if (nfa_calc_size == TRUE) |
| 2309 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2310 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2311 | break; |
| 2312 | } |
| 2313 | e = POP(); |
| 2314 | s = new_state(NFA_SPLIT, NULL, e.start); |
| 2315 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2316 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2317 | PUSH(frag(s, append(e.out, list1(&s->out)))); |
| 2318 | break; |
| 2319 | |
| 2320 | case NFA_PLUS: |
| 2321 | /* One or more */ |
| 2322 | if (nfa_calc_size == TRUE) |
| 2323 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2324 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2325 | break; |
| 2326 | } |
| 2327 | e = POP(); |
| 2328 | s = new_state(NFA_SPLIT, e.start, NULL); |
| 2329 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2330 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2331 | patch(e.out, s); |
| 2332 | PUSH(frag(e.start, list1(&s->out1))); |
| 2333 | break; |
| 2334 | |
| 2335 | case NFA_SKIP_CHAR: |
| 2336 | /* Symbol of 0-length, Used in a repetition |
| 2337 | * with max/min count of 0 */ |
| 2338 | if (nfa_calc_size == TRUE) |
| 2339 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2340 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2341 | break; |
| 2342 | } |
| 2343 | s = new_state(NFA_SKIP_CHAR, NULL, NULL); |
| 2344 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2345 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2346 | PUSH(frag(s, list1(&s->out))); |
| 2347 | break; |
| 2348 | |
| 2349 | case NFA_PREV_ATOM_NO_WIDTH: |
| 2350 | /* The \@= operator: match the preceding atom with 0 width. |
| 2351 | * Surrounds the preceding atom with START_INVISIBLE and |
| 2352 | * END_INVISIBLE, similarly to MOPEN. |
| 2353 | */ |
| 2354 | /* TODO: Maybe this drops the speed? */ |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2355 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2356 | |
| 2357 | if (nfa_calc_size == TRUE) |
| 2358 | { |
| 2359 | nstate += 2; |
| 2360 | break; |
| 2361 | } |
| 2362 | e = POP(); |
| 2363 | s1 = new_state(NFA_END_INVISIBLE, NULL, NULL); |
| 2364 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2365 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2366 | patch(e.out, s1); |
| 2367 | |
| 2368 | s = new_state(NFA_START_INVISIBLE, e.start, s1); |
| 2369 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2370 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2371 | PUSH(frag(s, list1(&s1->out))); |
| 2372 | break; |
| 2373 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2374 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2375 | case NFA_COMPOSING: /* char with composing char */ |
| 2376 | #if 0 |
| 2377 | /* TODO */ |
| 2378 | if (regflags & RF_ICOMBINE) |
| 2379 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 2380 | /* use the base character only */ |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2381 | } |
| 2382 | #endif |
| 2383 | /* FALLTHROUGH */ |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2384 | #endif |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2385 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2386 | case NFA_MOPEN + 0: /* Submatch */ |
| 2387 | case NFA_MOPEN + 1: |
| 2388 | case NFA_MOPEN + 2: |
| 2389 | case NFA_MOPEN + 3: |
| 2390 | case NFA_MOPEN + 4: |
| 2391 | case NFA_MOPEN + 5: |
| 2392 | case NFA_MOPEN + 6: |
| 2393 | case NFA_MOPEN + 7: |
| 2394 | case NFA_MOPEN + 8: |
| 2395 | case NFA_MOPEN + 9: |
| 2396 | case NFA_NOPEN: /* \%( "Invisible Submatch" */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2397 | if (nfa_calc_size == TRUE) |
| 2398 | { |
| 2399 | nstate += 2; |
| 2400 | break; |
| 2401 | } |
| 2402 | |
| 2403 | mopen = *p; |
| 2404 | switch (*p) |
| 2405 | { |
| 2406 | case NFA_NOPEN: |
| 2407 | mclose = NFA_NCLOSE; |
| 2408 | break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2409 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2410 | case NFA_COMPOSING: |
| 2411 | mclose = NFA_END_COMPOSING; |
| 2412 | break; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2413 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2414 | default: |
| 2415 | /* NFA_MOPEN(0) ... NFA_MOPEN(9) */ |
| 2416 | mclose = *p + NSUBEXP; |
| 2417 | break; |
| 2418 | } |
| 2419 | |
| 2420 | /* Allow "NFA_MOPEN" as a valid postfix representation for |
| 2421 | * the empty regexp "". In this case, the NFA will be |
| 2422 | * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
| 2423 | * empty groups of parenthesis, and empty mbyte chars */ |
| 2424 | if (stackp == stack) |
| 2425 | { |
| 2426 | s = new_state(mopen, NULL, NULL); |
| 2427 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2428 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2429 | s1 = new_state(mclose, NULL, NULL); |
| 2430 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2431 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2432 | patch(list1(&s->out), s1); |
| 2433 | PUSH(frag(s, list1(&s1->out))); |
| 2434 | break; |
| 2435 | } |
| 2436 | |
| 2437 | /* At least one node was emitted before NFA_MOPEN, so |
| 2438 | * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ |
| 2439 | e = POP(); |
| 2440 | s = new_state(mopen, e.start, NULL); /* `(' */ |
| 2441 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2442 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2443 | |
| 2444 | s1 = new_state(mclose, NULL, NULL); /* `)' */ |
| 2445 | if (s1 == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2446 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2447 | patch(e.out, s1); |
| 2448 | |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2449 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 2450 | if (mopen == NFA_COMPOSING) |
| 2451 | /* COMPOSING->out1 = END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2452 | patch(list1(&s->out1), s1); |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 2453 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2454 | |
| 2455 | PUSH(frag(s, list1(&s1->out))); |
| 2456 | break; |
| 2457 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2458 | case NFA_BACKREF1: |
| 2459 | case NFA_BACKREF2: |
| 2460 | case NFA_BACKREF3: |
| 2461 | case NFA_BACKREF4: |
| 2462 | case NFA_BACKREF5: |
| 2463 | case NFA_BACKREF6: |
| 2464 | case NFA_BACKREF7: |
| 2465 | case NFA_BACKREF8: |
| 2466 | case NFA_BACKREF9: |
| 2467 | if (nfa_calc_size == TRUE) |
| 2468 | { |
| 2469 | nstate += 2; |
| 2470 | break; |
| 2471 | } |
| 2472 | s = new_state(*p, NULL, NULL); |
| 2473 | if (s == NULL) |
| 2474 | goto theend; |
| 2475 | s1 = new_state(NFA_SKIP, NULL, NULL); |
| 2476 | if (s1 == NULL) |
| 2477 | goto theend; |
| 2478 | patch(list1(&s->out), s1); |
| 2479 | PUSH(frag(s, list1(&s1->out))); |
| 2480 | break; |
| 2481 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 2482 | case NFA_LNUM: |
| 2483 | case NFA_LNUM_GT: |
| 2484 | case NFA_LNUM_LT: |
| 2485 | case NFA_VCOL: |
| 2486 | case NFA_VCOL_GT: |
| 2487 | case NFA_VCOL_LT: |
| 2488 | case NFA_COL: |
| 2489 | case NFA_COL_GT: |
| 2490 | case NFA_COL_LT: |
| 2491 | if (nfa_calc_size == TRUE) |
| 2492 | { |
| 2493 | nstate += 1; |
| 2494 | break; |
| 2495 | } |
| 2496 | e1 = POP(); |
| 2497 | s = new_state(*p, NULL, NULL); |
| 2498 | if (s == NULL) |
| 2499 | goto theend; |
| 2500 | s->val = e1.start->c; |
| 2501 | PUSH(frag(s, list1(&s->out))); |
| 2502 | break; |
| 2503 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2504 | case NFA_ZSTART: |
| 2505 | case NFA_ZEND: |
| 2506 | default: |
| 2507 | /* Operands */ |
| 2508 | if (nfa_calc_size == TRUE) |
| 2509 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2510 | nstate++; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2511 | break; |
| 2512 | } |
| 2513 | s = new_state(*p, NULL, NULL); |
| 2514 | if (s == NULL) |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2515 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2516 | PUSH(frag(s, list1(&s->out))); |
| 2517 | break; |
| 2518 | |
| 2519 | } /* switch(*p) */ |
| 2520 | |
| 2521 | } /* for(p = postfix; *p; ++p) */ |
| 2522 | |
| 2523 | if (nfa_calc_size == TRUE) |
| 2524 | { |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 2525 | nstate++; |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2526 | goto theend; /* Return value when counting size is ignored anyway */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2527 | } |
| 2528 | |
| 2529 | e = POP(); |
| 2530 | if (stackp != stack) |
| 2531 | EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); |
| 2532 | |
| 2533 | if (istate >= nstate) |
| 2534 | EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
| 2535 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2536 | matchstate = &state_ptr[istate++]; /* the match state */ |
| 2537 | matchstate->c = NFA_MATCH; |
| 2538 | matchstate->out = matchstate->out1 = NULL; |
| 2539 | |
| 2540 | patch(e.out, matchstate); |
Bram Moolenaar | b09d983 | 2013-05-21 16:28:11 +0200 | [diff] [blame] | 2541 | ret = e.start; |
| 2542 | |
| 2543 | theend: |
| 2544 | vim_free(stack); |
| 2545 | return ret; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2546 | |
| 2547 | #undef POP1 |
| 2548 | #undef PUSH1 |
| 2549 | #undef POP2 |
| 2550 | #undef PUSH2 |
| 2551 | #undef POP |
| 2552 | #undef PUSH |
| 2553 | } |
| 2554 | |
| 2555 | /**************************************************************** |
| 2556 | * NFA execution code. |
| 2557 | ****************************************************************/ |
| 2558 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2559 | typedef struct |
| 2560 | { |
| 2561 | int in_use; /* number of subexpr with useful info */ |
| 2562 | |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2563 | /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2564 | union |
| 2565 | { |
| 2566 | struct multipos |
| 2567 | { |
| 2568 | lpos_T start; |
| 2569 | lpos_T end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2570 | } multi[NSUBEXP]; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2571 | struct linepos |
| 2572 | { |
| 2573 | char_u *start; |
| 2574 | char_u *end; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2575 | } line[NSUBEXP]; |
| 2576 | } list; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2577 | } regsub_T; |
| 2578 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2579 | /* nfa_thread_T contains execution information of a NFA state */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2580 | typedef struct |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2581 | { |
| 2582 | nfa_state_T *state; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2583 | int count; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2584 | regsub_T sub; /* submatch info, only party used */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2585 | } nfa_thread_T; |
| 2586 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2587 | /* nfa_list_T contains the alternative NFA execution states. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2588 | typedef struct |
| 2589 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2590 | nfa_thread_T *t; /* allocated array of states */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2591 | int n; /* nr of states currently in "t" */ |
| 2592 | int len; /* max nr of states in "t" */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2593 | int id; /* ID of the list */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2594 | } nfa_list_T; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2595 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2596 | #ifdef ENABLE_LOG |
| 2597 | static void |
| 2598 | log_subexpr(sub) |
| 2599 | regsub_T *sub; |
| 2600 | { |
| 2601 | int j; |
| 2602 | |
| 2603 | for (j = 0; j < sub->in_use; j++) |
| 2604 | if (REG_MULTI) |
| 2605 | fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d", |
| 2606 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2607 | sub->list.multi[j].start.col, |
| 2608 | (int)sub->list.multi[j].start.lnum, |
| 2609 | sub->list.multi[j].end.col, |
| 2610 | (int)sub->list.multi[j].end.lnum); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2611 | else |
| 2612 | fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"", |
| 2613 | j, |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2614 | (char *)sub->list.line[j].start, |
| 2615 | (char *)sub->list.line[j].end); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2616 | fprintf(log_fd, "\n"); |
| 2617 | } |
| 2618 | #endif |
| 2619 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2620 | /* Used during execution: whether a match has been found. */ |
| 2621 | static int nfa_match; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2622 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2623 | static int sub_equal __ARGS((regsub_T *sub1, regsub_T *sub2)); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2624 | static void addstate __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int off)); |
| 2625 | static void addstate_here __ARGS((nfa_list_T *l, nfa_state_T *state, regsub_T *sub, int *ip)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2626 | |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2627 | /* |
| 2628 | * Return TRUE if "sub1" and "sub2" have the same positions. |
| 2629 | */ |
| 2630 | static int |
| 2631 | sub_equal(sub1, sub2) |
| 2632 | regsub_T *sub1; |
| 2633 | regsub_T *sub2; |
| 2634 | { |
| 2635 | int i; |
| 2636 | int todo; |
| 2637 | linenr_T s1, e1; |
| 2638 | linenr_T s2, e2; |
| 2639 | char_u *sp1, *ep1; |
| 2640 | char_u *sp2, *ep2; |
| 2641 | |
| 2642 | todo = sub1->in_use > sub2->in_use ? sub1->in_use : sub2->in_use; |
| 2643 | if (REG_MULTI) |
| 2644 | { |
| 2645 | for (i = 0; i < todo; ++i) |
| 2646 | { |
| 2647 | if (i < sub1->in_use) |
| 2648 | { |
| 2649 | s1 = sub1->list.multi[i].start.lnum; |
| 2650 | e1 = sub1->list.multi[i].end.lnum; |
| 2651 | } |
| 2652 | else |
| 2653 | { |
| 2654 | s1 = 0; |
| 2655 | e1 = 0; |
| 2656 | } |
| 2657 | if (i < sub2->in_use) |
| 2658 | { |
| 2659 | s2 = sub2->list.multi[i].start.lnum; |
| 2660 | e2 = sub2->list.multi[i].end.lnum; |
| 2661 | } |
| 2662 | else |
| 2663 | { |
| 2664 | s2 = 0; |
| 2665 | e2 = 0; |
| 2666 | } |
| 2667 | if (s1 != s2 || e1 != e2) |
| 2668 | return FALSE; |
| 2669 | if (s1 != 0 && sub1->list.multi[i].start.col |
| 2670 | != sub2->list.multi[i].start.col) |
| 2671 | return FALSE; |
| 2672 | if (e1 != 0 && sub1->list.multi[i].end.col |
| 2673 | != sub2->list.multi[i].end.col) |
| 2674 | return FALSE; |
| 2675 | } |
| 2676 | } |
| 2677 | else |
| 2678 | { |
| 2679 | for (i = 0; i < todo; ++i) |
| 2680 | { |
| 2681 | if (i < sub1->in_use) |
| 2682 | { |
| 2683 | sp1 = sub1->list.line[i].start; |
| 2684 | ep1 = sub1->list.line[i].end; |
| 2685 | } |
| 2686 | else |
| 2687 | { |
| 2688 | sp1 = NULL; |
| 2689 | ep1 = NULL; |
| 2690 | } |
| 2691 | if (i < sub2->in_use) |
| 2692 | { |
| 2693 | sp2 = sub2->list.line[i].start; |
| 2694 | ep2 = sub2->list.line[i].end; |
| 2695 | } |
| 2696 | else |
| 2697 | { |
| 2698 | sp2 = NULL; |
| 2699 | ep2 = NULL; |
| 2700 | } |
| 2701 | if (sp1 != sp2 || ep1 != ep2) |
| 2702 | return FALSE; |
| 2703 | } |
| 2704 | } |
| 2705 | |
| 2706 | return TRUE; |
| 2707 | } |
| 2708 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2709 | static void |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2710 | addstate(l, state, sub, off) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 2711 | nfa_list_T *l; /* runtime state list */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2712 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2713 | regsub_T *sub; /* pointers to subexpressions */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2714 | int off; /* byte offset, when -1 go to next line */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2715 | { |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2716 | int subidx; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2717 | nfa_thread_T *thread; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2718 | lpos_T save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2719 | int save_in_use; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2720 | char_u *save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2721 | int i; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2722 | |
| 2723 | if (l == NULL || state == NULL) |
| 2724 | return; |
| 2725 | |
| 2726 | switch (state->c) |
| 2727 | { |
| 2728 | case NFA_SPLIT: |
| 2729 | case NFA_NOT: |
| 2730 | case NFA_NOPEN: |
| 2731 | case NFA_NCLOSE: |
| 2732 | case NFA_MCLOSE: |
| 2733 | case NFA_MCLOSE + 1: |
| 2734 | case NFA_MCLOSE + 2: |
| 2735 | case NFA_MCLOSE + 3: |
| 2736 | case NFA_MCLOSE + 4: |
| 2737 | case NFA_MCLOSE + 5: |
| 2738 | case NFA_MCLOSE + 6: |
| 2739 | case NFA_MCLOSE + 7: |
| 2740 | case NFA_MCLOSE + 8: |
| 2741 | case NFA_MCLOSE + 9: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2742 | /* These nodes are not added themselves but their "out" and/or |
| 2743 | * "out1" may be added below. */ |
| 2744 | break; |
| 2745 | |
| 2746 | case NFA_MOPEN: |
| 2747 | case NFA_MOPEN + 1: |
| 2748 | case NFA_MOPEN + 2: |
| 2749 | case NFA_MOPEN + 3: |
| 2750 | case NFA_MOPEN + 4: |
| 2751 | case NFA_MOPEN + 5: |
| 2752 | case NFA_MOPEN + 6: |
| 2753 | case NFA_MOPEN + 7: |
| 2754 | case NFA_MOPEN + 8: |
| 2755 | case NFA_MOPEN + 9: |
| 2756 | /* These nodes do not need to be added, but we need to bail out |
| 2757 | * when it was tried to be added to this list before. */ |
| 2758 | if (state->lastlist == l->id) |
| 2759 | return; |
| 2760 | state->lastlist = l->id; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2761 | break; |
| 2762 | |
| 2763 | default: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2764 | if (state->lastlist == l->id) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2765 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2766 | /* This state is already in the list, don't add it again, |
| 2767 | * unless it is an MOPEN that is used for a backreference. */ |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2768 | if (!nfa_has_backref) |
| 2769 | return; |
| 2770 | |
| 2771 | /* See if the same state is already in the list with the same |
| 2772 | * positions. */ |
| 2773 | for (i = 0; i < l->n; ++i) |
| 2774 | { |
| 2775 | thread = &l->t[i]; |
| 2776 | if (thread->state->id == state->id |
| 2777 | && sub_equal(&thread->sub, sub)) |
| 2778 | return; |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | /* when there are backreferences the number of states may be (a |
| 2783 | * lot) bigger */ |
| 2784 | if (nfa_has_backref && l->n == l->len) |
| 2785 | { |
| 2786 | int newlen = l->len * 3 / 2 + 50; |
| 2787 | |
| 2788 | l->t = vim_realloc(l->t, newlen * sizeof(nfa_thread_T)); |
| 2789 | l->len = newlen; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2790 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2791 | |
| 2792 | /* add the state to the list */ |
| 2793 | state->lastlist = l->id; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2794 | thread = &l->t[l->n++]; |
| 2795 | thread->state = state; |
| 2796 | thread->sub.in_use = sub->in_use; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2797 | if (sub->in_use > 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2798 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2799 | /* Copy the match start and end positions. */ |
| 2800 | if (REG_MULTI) |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2801 | mch_memmove(&thread->sub.list.multi[0], |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2802 | &sub->list.multi[0], |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2803 | sizeof(struct multipos) * sub->in_use); |
| 2804 | else |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 2805 | mch_memmove(&thread->sub.list.line[0], |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2806 | &sub->list.line[0], |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2807 | sizeof(struct linepos) * sub->in_use); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2808 | } |
| 2809 | } |
| 2810 | |
| 2811 | #ifdef ENABLE_LOG |
| 2812 | nfa_set_code(state->c); |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2813 | fprintf(log_fd, "> Adding state %d to list. Character %d: %s\n", |
| 2814 | abs(state->id), state->c, code); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2815 | #endif |
| 2816 | switch (state->c) |
| 2817 | { |
| 2818 | case NFA_MATCH: |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2819 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2820 | break; |
| 2821 | |
| 2822 | case NFA_SPLIT: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2823 | addstate(l, state->out, sub, off); |
| 2824 | addstate(l, state->out1, sub, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2825 | break; |
| 2826 | |
| 2827 | #if 0 |
| 2828 | case NFA_END_NEG_RANGE: |
| 2829 | /* Nothing to handle here. nfa_regmatch() will take care of it */ |
| 2830 | break; |
| 2831 | |
| 2832 | case NFA_NOT: |
| 2833 | EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !")); |
| 2834 | #ifdef ENABLE_LOG |
| 2835 | fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n"); |
| 2836 | #endif |
| 2837 | break; |
| 2838 | |
| 2839 | case NFA_COMPOSING: |
| 2840 | /* nfa_regmatch() will match all the bytes of this composing char. */ |
| 2841 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2842 | #endif |
| 2843 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2844 | case NFA_SKIP_CHAR: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2845 | case NFA_NOPEN: |
| 2846 | case NFA_NCLOSE: |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2847 | addstate(l, state->out, sub, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2848 | break; |
| 2849 | |
| 2850 | /* If this state is reached, then a recursive call of nfa_regmatch() |
| 2851 | * succeeded. the next call saves the found submatches in the |
| 2852 | * first state after the "invisible" branch. */ |
| 2853 | #if 0 |
| 2854 | case NFA_END_INVISIBLE: |
| 2855 | break; |
| 2856 | #endif |
| 2857 | |
| 2858 | case NFA_MOPEN + 0: |
| 2859 | case NFA_MOPEN + 1: |
| 2860 | case NFA_MOPEN + 2: |
| 2861 | case NFA_MOPEN + 3: |
| 2862 | case NFA_MOPEN + 4: |
| 2863 | case NFA_MOPEN + 5: |
| 2864 | case NFA_MOPEN + 6: |
| 2865 | case NFA_MOPEN + 7: |
| 2866 | case NFA_MOPEN + 8: |
| 2867 | case NFA_MOPEN + 9: |
| 2868 | case NFA_ZSTART: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2869 | if (state->c == NFA_ZSTART) |
| 2870 | subidx = 0; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2871 | else |
| 2872 | subidx = state->c - NFA_MOPEN; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2873 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2874 | /* Set the position (with "off") in the subexpression. Save and |
| 2875 | * restore it when it was in use. Otherwise fill any gap. */ |
Bram Moolenaar | 4b6ebe6 | 2013-05-30 17:49:24 +0200 | [diff] [blame^] | 2876 | save_ptr = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2877 | if (REG_MULTI) |
| 2878 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2879 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2880 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2881 | save_lpos = sub->list.multi[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2882 | save_in_use = -1; |
| 2883 | } |
| 2884 | else |
| 2885 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2886 | save_in_use = sub->in_use; |
| 2887 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2888 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2889 | sub->list.multi[i].start.lnum = -1; |
| 2890 | sub->list.multi[i].end.lnum = -1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2891 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2892 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2893 | } |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2894 | if (off == -1) |
| 2895 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2896 | sub->list.multi[subidx].start.lnum = reglnum + 1; |
| 2897 | sub->list.multi[subidx].start.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2898 | } |
| 2899 | else |
| 2900 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2901 | sub->list.multi[subidx].start.lnum = reglnum; |
| 2902 | sub->list.multi[subidx].start.col = |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2903 | (colnr_T)(reginput - regline + off); |
| 2904 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2905 | } |
| 2906 | else |
| 2907 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2908 | if (subidx < sub->in_use) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2909 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2910 | save_ptr = sub->list.line[subidx].start; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2911 | save_in_use = -1; |
| 2912 | } |
| 2913 | else |
| 2914 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2915 | save_in_use = sub->in_use; |
| 2916 | for (i = sub->in_use; i < subidx; ++i) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2917 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2918 | sub->list.line[i].start = NULL; |
| 2919 | sub->list.line[i].end = NULL; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2920 | } |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2921 | sub->in_use = subidx + 1; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2922 | } |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2923 | sub->list.line[subidx].start = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2924 | } |
| 2925 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2926 | addstate(l, state->out, sub, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2927 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2928 | if (save_in_use == -1) |
| 2929 | { |
| 2930 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2931 | sub->list.multi[subidx].start = save_lpos; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2932 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2933 | sub->list.line[subidx].start = save_ptr; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2934 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2935 | else |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2936 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2937 | break; |
| 2938 | |
| 2939 | case NFA_MCLOSE + 0: |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 2940 | if (nfa_has_zend) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2941 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 2942 | /* Do not overwrite the position set by \ze. If no \ze |
| 2943 | * encountered end will be set in nfa_regtry(). */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2944 | addstate(l, state->out, sub, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2945 | break; |
| 2946 | } |
| 2947 | case NFA_MCLOSE + 1: |
| 2948 | case NFA_MCLOSE + 2: |
| 2949 | case NFA_MCLOSE + 3: |
| 2950 | case NFA_MCLOSE + 4: |
| 2951 | case NFA_MCLOSE + 5: |
| 2952 | case NFA_MCLOSE + 6: |
| 2953 | case NFA_MCLOSE + 7: |
| 2954 | case NFA_MCLOSE + 8: |
| 2955 | case NFA_MCLOSE + 9: |
| 2956 | case NFA_ZEND: |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2957 | if (state->c == NFA_ZEND) |
| 2958 | subidx = 0; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2959 | else |
| 2960 | subidx = state->c - NFA_MCLOSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2961 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 2962 | /* We don't fill in gaps here, there must have been an MOPEN that |
| 2963 | * has done that. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2964 | save_in_use = sub->in_use; |
| 2965 | if (sub->in_use <= subidx) |
| 2966 | sub->in_use = subidx + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2967 | if (REG_MULTI) |
| 2968 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2969 | save_lpos = sub->list.multi[subidx].end; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2970 | if (off == -1) |
| 2971 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2972 | sub->list.multi[subidx].end.lnum = reglnum + 1; |
| 2973 | sub->list.multi[subidx].end.col = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2974 | } |
| 2975 | else |
| 2976 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2977 | sub->list.multi[subidx].end.lnum = reglnum; |
| 2978 | sub->list.multi[subidx].end.col = |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 2979 | (colnr_T)(reginput - regline + off); |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 2980 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2981 | } |
| 2982 | else |
| 2983 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2984 | save_ptr = sub->list.line[subidx].end; |
| 2985 | sub->list.line[subidx].end = reginput + off; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2986 | } |
| 2987 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2988 | addstate(l, state->out, sub, off); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2989 | |
| 2990 | if (REG_MULTI) |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2991 | sub->list.multi[subidx].end = save_lpos; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2992 | else |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 2993 | sub->list.line[subidx].end = save_ptr; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 2994 | sub->in_use = save_in_use; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2995 | break; |
| 2996 | } |
| 2997 | } |
| 2998 | |
| 2999 | /* |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3000 | * Like addstate(), but the new state(s) are put at position "*ip". |
| 3001 | * Used for zero-width matches, next state to use is the added one. |
| 3002 | * This makes sure the order of states to be tried does not change, which |
| 3003 | * matters for alternatives. |
| 3004 | */ |
| 3005 | static void |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3006 | addstate_here(l, state, sub, ip) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3007 | nfa_list_T *l; /* runtime state list */ |
| 3008 | nfa_state_T *state; /* state to update */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3009 | regsub_T *sub; /* pointers to subexpressions */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3010 | int *ip; |
| 3011 | { |
| 3012 | int tlen = l->n; |
| 3013 | int count; |
| 3014 | int i = *ip; |
| 3015 | |
| 3016 | /* first add the state(s) at the end, so that we know how many there are */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3017 | addstate(l, state, sub, 0); |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3018 | |
| 3019 | /* when "*ip" was at the end of the list, nothing to do */ |
| 3020 | if (i + 1 == tlen) |
| 3021 | return; |
| 3022 | |
| 3023 | /* re-order to put the new state at the current position */ |
| 3024 | count = l->n - tlen; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3025 | if (count == 1) |
| 3026 | { |
| 3027 | /* overwrite the current state */ |
| 3028 | l->t[i] = l->t[l->n - 1]; |
| 3029 | } |
| 3030 | else if (count > 1) |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3031 | { |
| 3032 | /* make space for new states, then move them from the |
| 3033 | * end to the current position */ |
| 3034 | mch_memmove(&(l->t[i + count]), |
| 3035 | &(l->t[i + 1]), |
| 3036 | sizeof(nfa_thread_T) * (l->n - i - 1)); |
| 3037 | mch_memmove(&(l->t[i]), |
| 3038 | &(l->t[l->n - 1]), |
| 3039 | sizeof(nfa_thread_T) * count); |
| 3040 | } |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3041 | --l->n; |
| 3042 | *ip = i - 1; |
| 3043 | } |
| 3044 | |
| 3045 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3046 | * Check character class "class" against current character c. |
| 3047 | */ |
| 3048 | static int |
| 3049 | check_char_class(class, c) |
| 3050 | int class; |
| 3051 | int c; |
| 3052 | { |
| 3053 | switch (class) |
| 3054 | { |
| 3055 | case NFA_CLASS_ALNUM: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3056 | if (c >= 1 && c <= 255 && isalnum(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3057 | return OK; |
| 3058 | break; |
| 3059 | case NFA_CLASS_ALPHA: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3060 | if (c >= 1 && c <= 255 && isalpha(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3061 | return OK; |
| 3062 | break; |
| 3063 | case NFA_CLASS_BLANK: |
| 3064 | if (c == ' ' || c == '\t') |
| 3065 | return OK; |
| 3066 | break; |
| 3067 | case NFA_CLASS_CNTRL: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3068 | if (c >= 1 && c <= 255 && iscntrl(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3069 | return OK; |
| 3070 | break; |
| 3071 | case NFA_CLASS_DIGIT: |
| 3072 | if (VIM_ISDIGIT(c)) |
| 3073 | return OK; |
| 3074 | break; |
| 3075 | case NFA_CLASS_GRAPH: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3076 | if (c >= 1 && c <= 255 && isgraph(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3077 | return OK; |
| 3078 | break; |
| 3079 | case NFA_CLASS_LOWER: |
| 3080 | if (MB_ISLOWER(c)) |
| 3081 | return OK; |
| 3082 | break; |
| 3083 | case NFA_CLASS_PRINT: |
| 3084 | if (vim_isprintc(c)) |
| 3085 | return OK; |
| 3086 | break; |
| 3087 | case NFA_CLASS_PUNCT: |
Bram Moolenaar | 745fc02 | 2013-05-20 22:20:02 +0200 | [diff] [blame] | 3088 | if (c >= 1 && c <= 255 && ispunct(c)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3089 | return OK; |
| 3090 | break; |
| 3091 | case NFA_CLASS_SPACE: |
| 3092 | if ((c >=9 && c <= 13) || (c == ' ')) |
| 3093 | return OK; |
| 3094 | break; |
| 3095 | case NFA_CLASS_UPPER: |
| 3096 | if (MB_ISUPPER(c)) |
| 3097 | return OK; |
| 3098 | break; |
| 3099 | case NFA_CLASS_XDIGIT: |
| 3100 | if (vim_isxdigit(c)) |
| 3101 | return OK; |
| 3102 | break; |
| 3103 | case NFA_CLASS_TAB: |
| 3104 | if (c == '\t') |
| 3105 | return OK; |
| 3106 | break; |
| 3107 | case NFA_CLASS_RETURN: |
| 3108 | if (c == '\r') |
| 3109 | return OK; |
| 3110 | break; |
| 3111 | case NFA_CLASS_BACKSPACE: |
| 3112 | if (c == '\b') |
| 3113 | return OK; |
| 3114 | break; |
| 3115 | case NFA_CLASS_ESCAPE: |
| 3116 | if (c == '\033') |
| 3117 | return OK; |
| 3118 | break; |
| 3119 | |
| 3120 | default: |
| 3121 | /* should not be here :P */ |
| 3122 | EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class ")); |
| 3123 | } |
| 3124 | return FAIL; |
| 3125 | } |
| 3126 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3127 | static int match_backref __ARGS((regsub_T *sub, int subidx, int *bytelen)); |
| 3128 | |
| 3129 | /* |
| 3130 | * Check for a match with subexpression "subidx". |
| 3131 | * return TRUE if it matches. |
| 3132 | */ |
| 3133 | static int |
| 3134 | match_backref(sub, subidx, bytelen) |
| 3135 | regsub_T *sub; /* pointers to subexpressions */ |
| 3136 | int subidx; |
| 3137 | int *bytelen; /* out: length of match in bytes */ |
| 3138 | { |
| 3139 | int len; |
| 3140 | |
| 3141 | if (sub->in_use <= subidx) |
| 3142 | { |
| 3143 | retempty: |
| 3144 | /* backref was not set, match an empty string */ |
| 3145 | *bytelen = 0; |
| 3146 | return TRUE; |
| 3147 | } |
| 3148 | |
| 3149 | if (REG_MULTI) |
| 3150 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3151 | if (sub->list.multi[subidx].start.lnum < 0 |
| 3152 | || sub->list.multi[subidx].end.lnum < 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3153 | goto retempty; |
| 3154 | /* TODO: line breaks */ |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3155 | len = sub->list.multi[subidx].end.col |
| 3156 | - sub->list.multi[subidx].start.col; |
| 3157 | if (cstrncmp(regline + sub->list.multi[subidx].start.col, |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3158 | reginput, &len) == 0) |
| 3159 | { |
| 3160 | *bytelen = len; |
| 3161 | return TRUE; |
| 3162 | } |
| 3163 | } |
| 3164 | else |
| 3165 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3166 | if (sub->list.line[subidx].start == NULL |
| 3167 | || sub->list.line[subidx].end == NULL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3168 | goto retempty; |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3169 | len = (int)(sub->list.line[subidx].end - sub->list.line[subidx].start); |
| 3170 | if (cstrncmp(sub->list.line[subidx].start, reginput, &len) == 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3171 | { |
| 3172 | *bytelen = len; |
| 3173 | return TRUE; |
| 3174 | } |
| 3175 | } |
| 3176 | return FALSE; |
| 3177 | } |
| 3178 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3179 | /* |
| 3180 | * Set all NFA nodes' list ID equal to -1. |
| 3181 | */ |
| 3182 | static void |
| 3183 | nfa_set_neg_listids(start) |
| 3184 | nfa_state_T *start; |
| 3185 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3186 | if (start != NULL && start->lastlist >= 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3187 | { |
| 3188 | start->lastlist = -1; |
| 3189 | nfa_set_neg_listids(start->out); |
| 3190 | nfa_set_neg_listids(start->out1); |
| 3191 | } |
| 3192 | } |
| 3193 | |
| 3194 | /* |
| 3195 | * Set all NFA nodes' list ID equal to 0. |
| 3196 | */ |
| 3197 | static void |
| 3198 | nfa_set_null_listids(start) |
| 3199 | nfa_state_T *start; |
| 3200 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3201 | if (start != NULL && start->lastlist == -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3202 | { |
| 3203 | start->lastlist = 0; |
| 3204 | nfa_set_null_listids(start->out); |
| 3205 | nfa_set_null_listids(start->out1); |
| 3206 | } |
| 3207 | } |
| 3208 | |
| 3209 | /* |
| 3210 | * Save list IDs for all NFA states in "list". |
| 3211 | */ |
| 3212 | static void |
| 3213 | nfa_save_listids(start, list) |
| 3214 | nfa_state_T *start; |
| 3215 | int *list; |
| 3216 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3217 | if (start != NULL && start->lastlist != -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3218 | { |
| 3219 | list[abs(start->id)] = start->lastlist; |
| 3220 | start->lastlist = -1; |
| 3221 | nfa_save_listids(start->out, list); |
| 3222 | nfa_save_listids(start->out1, list); |
| 3223 | } |
| 3224 | } |
| 3225 | |
| 3226 | /* |
| 3227 | * Restore list IDs from "list" to all NFA states. |
| 3228 | */ |
| 3229 | static void |
| 3230 | nfa_restore_listids(start, list) |
| 3231 | nfa_state_T *start; |
| 3232 | int *list; |
| 3233 | { |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3234 | if (start != NULL && start->lastlist == -1) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3235 | { |
| 3236 | start->lastlist = list[abs(start->id)]; |
| 3237 | nfa_restore_listids(start->out, list); |
| 3238 | nfa_restore_listids(start->out1, list); |
| 3239 | } |
| 3240 | } |
| 3241 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3242 | static int |
| 3243 | nfa_re_num_cmp(val, op, pos) |
| 3244 | long_u val; |
| 3245 | int op; |
| 3246 | long_u pos; |
| 3247 | { |
| 3248 | if (op == 1) return pos > val; |
| 3249 | if (op == 2) return pos < val; |
| 3250 | return val == pos; |
| 3251 | } |
| 3252 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3253 | static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m)); |
| 3254 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3255 | /* |
| 3256 | * Main matching routine. |
| 3257 | * |
| 3258 | * Run NFA to determine whether it matches reginput. |
| 3259 | * |
| 3260 | * Return TRUE if there is a match, FALSE otherwise. |
| 3261 | * Note: Caller must ensure that: start != NULL. |
| 3262 | */ |
| 3263 | static int |
| 3264 | nfa_regmatch(start, submatch, m) |
| 3265 | nfa_state_T *start; |
| 3266 | regsub_T *submatch; |
| 3267 | regsub_T *m; |
| 3268 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3269 | int result; |
| 3270 | int size = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3271 | int flag = 0; |
| 3272 | int old_reglnum = -1; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3273 | int go_to_nextline = FALSE; |
| 3274 | nfa_thread_T *t; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3275 | char_u *old_reginput = NULL; |
| 3276 | char_u *old_regline = NULL; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3277 | nfa_list_T list[3]; |
| 3278 | nfa_list_T *listtbl[2][2]; |
| 3279 | nfa_list_T *ll; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3280 | int listid = 1; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3281 | int listidx; |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3282 | nfa_list_T *thislist; |
| 3283 | nfa_list_T *nextlist; |
| 3284 | nfa_list_T *neglist; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3285 | int *listids = NULL; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 3286 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3287 | FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3288 | |
| 3289 | if (debug == NULL) |
| 3290 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3291 | EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3292 | return FALSE; |
| 3293 | } |
| 3294 | #endif |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3295 | nfa_match = FALSE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3296 | |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3297 | /* Allocate memory for the lists of nodes. */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3298 | size = (nstate + 1) * sizeof(nfa_thread_T); |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 3299 | list[0].t = (nfa_thread_T *)lalloc_clear(size, TRUE); |
| 3300 | list[0].len = nstate + 1; |
| 3301 | list[1].t = (nfa_thread_T *)lalloc_clear(size, TRUE); |
| 3302 | list[1].len = nstate + 1; |
| 3303 | list[2].t = (nfa_thread_T *)lalloc_clear(size, TRUE); |
| 3304 | list[2].len = nstate + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3305 | if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL) |
| 3306 | goto theend; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3307 | |
| 3308 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3309 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3310 | if (log_fd != NULL) |
| 3311 | { |
| 3312 | fprintf(log_fd, "**********************************\n"); |
| 3313 | nfa_set_code(start->c); |
| 3314 | fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", |
| 3315 | abs(start->id), code); |
| 3316 | fprintf(log_fd, "**********************************\n"); |
| 3317 | } |
| 3318 | else |
| 3319 | { |
| 3320 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 3321 | log_fd = stderr; |
| 3322 | } |
| 3323 | #endif |
| 3324 | |
| 3325 | thislist = &list[0]; |
| 3326 | thislist->n = 0; |
| 3327 | nextlist = &list[1]; |
| 3328 | nextlist->n = 0; |
| 3329 | neglist = &list[2]; |
| 3330 | neglist->n = 0; |
| 3331 | #ifdef ENABLE_LOG |
| 3332 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 3333 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3334 | thislist->id = listid; |
| 3335 | addstate(thislist, start, m, 0); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3336 | |
| 3337 | /* There are two cases when the NFA advances: 1. input char matches the |
| 3338 | * NFA node and 2. input char does not match the NFA node, but the next |
| 3339 | * node is NFA_NOT. The following macro calls addstate() according to |
| 3340 | * these rules. It is used A LOT, so use the "listtbl" table for speed */ |
| 3341 | listtbl[0][0] = NULL; |
| 3342 | listtbl[0][1] = neglist; |
| 3343 | listtbl[1][0] = nextlist; |
| 3344 | listtbl[1][1] = NULL; |
| 3345 | #define ADD_POS_NEG_STATE(node) \ |
| 3346 | ll = listtbl[result ? 1 : 0][node->negated]; \ |
| 3347 | if (ll != NULL) \ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3348 | addstate(ll, node->out , &t->sub, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3349 | |
| 3350 | |
| 3351 | /* |
| 3352 | * Run for each character. |
| 3353 | */ |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3354 | for (;;) |
| 3355 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3356 | int curc; |
| 3357 | int clen; |
| 3358 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3359 | #ifdef FEAT_MBYTE |
| 3360 | if (has_mbyte) |
| 3361 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3362 | curc = (*mb_ptr2char)(reginput); |
| 3363 | clen = (*mb_ptr2len)(reginput); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3364 | } |
| 3365 | else |
| 3366 | #endif |
| 3367 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3368 | curc = *reginput; |
| 3369 | clen = 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3370 | } |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3371 | if (curc == NUL) |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3372 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3373 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3374 | go_to_nextline = FALSE; |
| 3375 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3376 | |
| 3377 | /* swap lists */ |
| 3378 | thislist = &list[flag]; |
| 3379 | nextlist = &list[flag ^= 1]; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3380 | nextlist->n = 0; /* clear nextlist */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3381 | listtbl[1][0] = nextlist; |
| 3382 | ++listid; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3383 | thislist->id = listid; |
| 3384 | nextlist->id = listid + 1; |
| 3385 | neglist->id = listid + 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3386 | |
| 3387 | #ifdef ENABLE_LOG |
| 3388 | fprintf(log_fd, "------------------------------------------\n"); |
| 3389 | fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3390 | 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] | 3391 | fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3392 | { |
| 3393 | int i; |
| 3394 | |
| 3395 | for (i = 0; i < thislist->n; i++) |
| 3396 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 3397 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3398 | fprintf(log_fd, "\n"); |
| 3399 | #endif |
| 3400 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 3401 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3402 | fprintf(debug, "\n-------------------\n"); |
| 3403 | #endif |
Bram Moolenaar | 66e83d7 | 2013-05-21 14:03:00 +0200 | [diff] [blame] | 3404 | /* |
| 3405 | * If the state lists are empty we can stop. |
| 3406 | */ |
| 3407 | if (thislist->n == 0 && neglist->n == 0) |
| 3408 | break; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3409 | |
| 3410 | /* compute nextlist */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3411 | for (listidx = 0; listidx < thislist->n || neglist->n > 0; ++listidx) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3412 | { |
| 3413 | if (neglist->n > 0) |
| 3414 | { |
| 3415 | t = &neglist->t[0]; |
Bram Moolenaar | 0fabe3f | 2013-05-21 12:34:17 +0200 | [diff] [blame] | 3416 | neglist->n--; |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3417 | listidx--; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3418 | } |
| 3419 | else |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3420 | t = &thislist->t[listidx]; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3421 | |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 3422 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3423 | nfa_set_code(t->state->c); |
| 3424 | fprintf(debug, "%s, ", code); |
| 3425 | #endif |
| 3426 | #ifdef ENABLE_LOG |
| 3427 | nfa_set_code(t->state->c); |
| 3428 | fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id), |
| 3429 | code, (int)t->state->c); |
| 3430 | #endif |
| 3431 | |
| 3432 | /* |
| 3433 | * Handle the possible codes of the current state. |
| 3434 | * The most important is NFA_MATCH. |
| 3435 | */ |
| 3436 | switch (t->state->c) |
| 3437 | { |
| 3438 | case NFA_MATCH: |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 3439 | { |
| 3440 | int j; |
| 3441 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3442 | nfa_match = TRUE; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3443 | submatch->in_use = t->sub.in_use; |
| 3444 | if (REG_MULTI) |
| 3445 | for (j = 0; j < submatch->in_use; j++) |
| 3446 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3447 | submatch->list.multi[j].start = |
| 3448 | t->sub.list.multi[j].start; |
| 3449 | submatch->list.multi[j].end = t->sub.list.multi[j].end; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3450 | } |
| 3451 | else |
| 3452 | for (j = 0; j < submatch->in_use; j++) |
| 3453 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3454 | submatch->list.line[j].start = |
| 3455 | t->sub.list.line[j].start; |
| 3456 | submatch->list.line[j].end = t->sub.list.line[j].end; |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3457 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3458 | #ifdef ENABLE_LOG |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3459 | log_subexpr(&t->sub); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3460 | #endif |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3461 | /* Found the left-most longest match, do not look at any other |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 3462 | * states at this position. When the list of states is going |
| 3463 | * to be empty quit without advancing, so that "reginput" is |
| 3464 | * correct. */ |
| 3465 | if (nextlist->n == 0 && neglist->n == 0) |
| 3466 | clen = 0; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3467 | goto nextchar; |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 3468 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3469 | |
| 3470 | case NFA_END_INVISIBLE: |
| 3471 | /* This is only encountered after a NFA_START_INVISIBLE node. |
| 3472 | * They surround a zero-width group, used with "\@=" and "\&". |
| 3473 | * If we got here, it means that the current "invisible" group |
| 3474 | * finished successfully, so return control to the parent |
| 3475 | * nfa_regmatch(). Submatches are stored in *m, and used in |
| 3476 | * the parent call. */ |
| 3477 | if (start->c == NFA_MOPEN + 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3478 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3479 | else |
| 3480 | { |
| 3481 | *m = t->sub; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 3482 | nfa_match = TRUE; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3483 | } |
| 3484 | break; |
| 3485 | |
| 3486 | case NFA_START_INVISIBLE: |
| 3487 | /* Save global variables, and call nfa_regmatch() to check if |
| 3488 | * the current concat matches at this position. The concat |
| 3489 | * ends with the node NFA_END_INVISIBLE */ |
| 3490 | old_reginput = reginput; |
| 3491 | old_regline = regline; |
| 3492 | old_reglnum = reglnum; |
| 3493 | if (listids == NULL) |
| 3494 | { |
| 3495 | listids = (int *) lalloc(sizeof(int) * nstate, TRUE); |
| 3496 | if (listids == NULL) |
| 3497 | { |
| 3498 | EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); |
| 3499 | return 0; |
| 3500 | } |
| 3501 | } |
| 3502 | #ifdef ENABLE_LOG |
| 3503 | if (log_fd != stderr) |
| 3504 | fclose(log_fd); |
| 3505 | log_fd = NULL; |
| 3506 | #endif |
| 3507 | /* Have to clear the listid field of the NFA nodes, so that |
| 3508 | * nfa_regmatch() and addstate() can run properly after |
| 3509 | * recursion. */ |
| 3510 | nfa_save_listids(start, listids); |
| 3511 | nfa_set_null_listids(start); |
| 3512 | result = nfa_regmatch(t->state->out, submatch, m); |
| 3513 | nfa_set_neg_listids(start); |
| 3514 | nfa_restore_listids(start, listids); |
| 3515 | |
| 3516 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3517 | log_fd = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3518 | if (log_fd != NULL) |
| 3519 | { |
| 3520 | fprintf(log_fd, "****************************\n"); |
| 3521 | fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); |
| 3522 | fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
| 3523 | fprintf(log_fd, "****************************\n"); |
| 3524 | } |
| 3525 | else |
| 3526 | { |
| 3527 | EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); |
| 3528 | log_fd = stderr; |
| 3529 | } |
| 3530 | #endif |
| 3531 | if (result == TRUE) |
| 3532 | { |
Bram Moolenaar | eb3ecae | 2013-05-27 11:22:04 +0200 | [diff] [blame] | 3533 | int j; |
| 3534 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3535 | /* Restore position in input text */ |
| 3536 | reginput = old_reginput; |
| 3537 | regline = old_regline; |
| 3538 | reglnum = old_reglnum; |
| 3539 | /* Copy submatch info from the recursive call */ |
| 3540 | if (REG_MULTI) |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3541 | for (j = 1; j < m->in_use; j++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3542 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3543 | t->sub.list.multi[j].start = m->list.multi[j].start; |
| 3544 | t->sub.list.multi[j].end = m->list.multi[j].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3545 | } |
| 3546 | else |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3547 | for (j = 1; j < m->in_use; j++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3548 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 3549 | t->sub.list.line[j].start = m->list.line[j].start; |
| 3550 | t->sub.list.line[j].end = m->list.line[j].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3551 | } |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 3552 | t->sub.in_use = m->in_use; |
| 3553 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3554 | /* t->state->out1 is the corresponding END_INVISIBLE node */ |
Bram Moolenaar | 4b41706 | 2013-05-25 20:19:50 +0200 | [diff] [blame] | 3555 | addstate_here(thislist, t->state->out1->out, &t->sub, |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3556 | &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3557 | } |
| 3558 | else |
| 3559 | { |
| 3560 | /* continue with next input char */ |
| 3561 | reginput = old_reginput; |
| 3562 | } |
| 3563 | break; |
| 3564 | |
| 3565 | case NFA_BOL: |
| 3566 | if (reginput == regline) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3567 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3568 | break; |
| 3569 | |
| 3570 | case NFA_EOL: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3571 | if (curc == NUL) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3572 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3573 | break; |
| 3574 | |
| 3575 | case NFA_BOW: |
| 3576 | { |
| 3577 | int bow = TRUE; |
| 3578 | |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3579 | if (curc == NUL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3580 | bow = FALSE; |
| 3581 | #ifdef FEAT_MBYTE |
| 3582 | else if (has_mbyte) |
| 3583 | { |
| 3584 | int this_class; |
| 3585 | |
| 3586 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3587 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3588 | if (this_class <= 1) |
| 3589 | bow = FALSE; |
| 3590 | else if (reg_prev_class() == this_class) |
| 3591 | bow = FALSE; |
| 3592 | } |
| 3593 | #endif |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3594 | else if (!vim_iswordc_buf(curc, reg_buf) |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3595 | || (reginput > regline |
| 3596 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3597 | bow = FALSE; |
| 3598 | if (bow) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3599 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3600 | break; |
| 3601 | } |
| 3602 | |
| 3603 | case NFA_EOW: |
| 3604 | { |
| 3605 | int eow = TRUE; |
| 3606 | |
| 3607 | if (reginput == regline) |
| 3608 | eow = FALSE; |
| 3609 | #ifdef FEAT_MBYTE |
| 3610 | else if (has_mbyte) |
| 3611 | { |
| 3612 | int this_class, prev_class; |
| 3613 | |
| 3614 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3615 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3616 | prev_class = reg_prev_class(); |
| 3617 | if (this_class == prev_class |
| 3618 | || prev_class == 0 || prev_class == 1) |
| 3619 | eow = FALSE; |
| 3620 | } |
| 3621 | #endif |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3622 | else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3623 | || (reginput[0] != NUL |
| 3624 | && vim_iswordc_buf(curc, reg_buf))) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3625 | eow = FALSE; |
| 3626 | if (eow) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3627 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3628 | break; |
| 3629 | } |
| 3630 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3631 | #ifdef FEAT_MBYTE |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3632 | case NFA_COMPOSING: |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3633 | { |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3634 | int mc = curc; |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 3635 | int len = 0; |
| 3636 | nfa_state_T *end; |
| 3637 | nfa_state_T *sta; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3638 | int cchars[MAX_MCO]; |
| 3639 | int ccount = 0; |
| 3640 | int j; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3641 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3642 | sta = t->state->out; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3643 | len = 0; |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 3644 | if (utf_iscomposing(sta->c)) |
| 3645 | { |
| 3646 | /* Only match composing character(s), ignore base |
| 3647 | * character. Used for ".{composing}" and "{composing}" |
| 3648 | * (no preceding character). */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3649 | len += mb_char2len(mc); |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 3650 | } |
Bram Moolenaar | 3451d66 | 2013-05-26 15:14:55 +0200 | [diff] [blame] | 3651 | if (ireg_icombine && len == 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3652 | { |
Bram Moolenaar | 56d58d5 | 2013-05-25 14:42:03 +0200 | [diff] [blame] | 3653 | /* If \Z was present, then ignore composing characters. |
| 3654 | * When ignoring the base character this always matches. */ |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3655 | /* TODO: How about negated? */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3656 | if (len == 0 && sta->c != curc) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3657 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3658 | else |
| 3659 | result = OK; |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3660 | while (sta->c != NFA_END_COMPOSING) |
| 3661 | sta = sta->out; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3662 | } |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3663 | |
| 3664 | /* Check base character matches first, unless ignored. */ |
| 3665 | else if (len > 0 || mc == sta->c) |
| 3666 | { |
| 3667 | if (len == 0) |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3668 | { |
Bram Moolenaar | fad8de0 | 2013-05-24 23:10:50 +0200 | [diff] [blame] | 3669 | len += mb_char2len(mc); |
| 3670 | sta = sta->out; |
| 3671 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3672 | |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3673 | /* We don't care about the order of composing characters. |
| 3674 | * Get them into cchars[] first. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3675 | while (len < clen) |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3676 | { |
| 3677 | mc = mb_ptr2char(reginput + len); |
| 3678 | cchars[ccount++] = mc; |
| 3679 | len += mb_char2len(mc); |
| 3680 | if (ccount == MAX_MCO) |
| 3681 | break; |
| 3682 | } |
| 3683 | |
| 3684 | /* Check that each composing char in the pattern matches a |
| 3685 | * composing char in the text. We do not check if all |
| 3686 | * composing chars are matched. */ |
| 3687 | result = OK; |
| 3688 | while (sta->c != NFA_END_COMPOSING) |
| 3689 | { |
| 3690 | for (j = 0; j < ccount; ++j) |
| 3691 | if (cchars[j] == sta->c) |
| 3692 | break; |
| 3693 | if (j == ccount) |
| 3694 | { |
| 3695 | result = FAIL; |
| 3696 | break; |
| 3697 | } |
| 3698 | sta = sta->out; |
| 3699 | } |
| 3700 | } |
| 3701 | else |
Bram Moolenaar | 1d81475 | 2013-05-24 20:25:33 +0200 | [diff] [blame] | 3702 | result = FAIL; |
Bram Moolenaar | 3f1682e | 2013-05-26 14:32:05 +0200 | [diff] [blame] | 3703 | |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3704 | end = t->state->out1; /* NFA_END_COMPOSING */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3705 | ADD_POS_NEG_STATE(end); |
| 3706 | break; |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 3707 | } |
| 3708 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3709 | |
| 3710 | case NFA_NEWL: |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3711 | if (curc == NUL && !reg_line_lbr && REG_MULTI |
| 3712 | && reglnum <= reg_maxline) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3713 | { |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3714 | go_to_nextline = TRUE; |
| 3715 | /* Pass -1 for the offset, which means taking the position |
| 3716 | * at the start of the next line. */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3717 | addstate(nextlist, t->state->out, &t->sub, -1); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3718 | } |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3719 | else if (curc == '\n' && reg_line_lbr) |
| 3720 | { |
| 3721 | /* match \n as if it is an ordinary character */ |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3722 | addstate(nextlist, t->state->out, &t->sub, 1); |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 3723 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3724 | break; |
| 3725 | |
| 3726 | case NFA_CLASS_ALNUM: |
| 3727 | case NFA_CLASS_ALPHA: |
| 3728 | case NFA_CLASS_BLANK: |
| 3729 | case NFA_CLASS_CNTRL: |
| 3730 | case NFA_CLASS_DIGIT: |
| 3731 | case NFA_CLASS_GRAPH: |
| 3732 | case NFA_CLASS_LOWER: |
| 3733 | case NFA_CLASS_PRINT: |
| 3734 | case NFA_CLASS_PUNCT: |
| 3735 | case NFA_CLASS_SPACE: |
| 3736 | case NFA_CLASS_UPPER: |
| 3737 | case NFA_CLASS_XDIGIT: |
| 3738 | case NFA_CLASS_TAB: |
| 3739 | case NFA_CLASS_RETURN: |
| 3740 | case NFA_CLASS_BACKSPACE: |
| 3741 | case NFA_CLASS_ESCAPE: |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3742 | result = check_char_class(t->state->c, curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3743 | ADD_POS_NEG_STATE(t->state); |
| 3744 | break; |
| 3745 | |
| 3746 | case NFA_END_NEG_RANGE: |
| 3747 | /* This follows a series of negated nodes, like: |
| 3748 | * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3749 | if (curc > 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3750 | addstate(nextlist, t->state->out, &t->sub, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3751 | break; |
| 3752 | |
| 3753 | case NFA_ANY: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 3754 | /* Any char except '\0', (end of input) does not match. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3755 | if (curc > 0) |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3756 | addstate(nextlist, t->state->out, &t->sub, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3757 | break; |
| 3758 | |
| 3759 | /* |
| 3760 | * Character classes like \a for alpha, \d for digit etc. |
| 3761 | */ |
| 3762 | case NFA_IDENT: /* \i */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3763 | result = vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3764 | ADD_POS_NEG_STATE(t->state); |
| 3765 | break; |
| 3766 | |
| 3767 | case NFA_SIDENT: /* \I */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3768 | result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3769 | ADD_POS_NEG_STATE(t->state); |
| 3770 | break; |
| 3771 | |
| 3772 | case NFA_KWORD: /* \k */ |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 3773 | result = vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3774 | ADD_POS_NEG_STATE(t->state); |
| 3775 | break; |
| 3776 | |
| 3777 | case NFA_SKWORD: /* \K */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3778 | result = !VIM_ISDIGIT(curc) |
| 3779 | && vim_iswordp_buf(reginput, reg_buf); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3780 | ADD_POS_NEG_STATE(t->state); |
| 3781 | break; |
| 3782 | |
| 3783 | case NFA_FNAME: /* \f */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3784 | result = vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3785 | ADD_POS_NEG_STATE(t->state); |
| 3786 | break; |
| 3787 | |
| 3788 | case NFA_SFNAME: /* \F */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3789 | result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3790 | ADD_POS_NEG_STATE(t->state); |
| 3791 | break; |
| 3792 | |
| 3793 | case NFA_PRINT: /* \p */ |
Bram Moolenaar | 0805049 | 2013-05-21 12:43:56 +0200 | [diff] [blame] | 3794 | result = ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3795 | ADD_POS_NEG_STATE(t->state); |
| 3796 | break; |
| 3797 | |
| 3798 | case NFA_SPRINT: /* \P */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3799 | result = !VIM_ISDIGIT(curc) && ptr2cells(reginput) == 1; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3800 | ADD_POS_NEG_STATE(t->state); |
| 3801 | break; |
| 3802 | |
| 3803 | case NFA_WHITE: /* \s */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3804 | result = vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3805 | ADD_POS_NEG_STATE(t->state); |
| 3806 | break; |
| 3807 | |
| 3808 | case NFA_NWHITE: /* \S */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3809 | result = curc != NUL && !vim_iswhite(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3810 | ADD_POS_NEG_STATE(t->state); |
| 3811 | break; |
| 3812 | |
| 3813 | case NFA_DIGIT: /* \d */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3814 | result = ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3815 | ADD_POS_NEG_STATE(t->state); |
| 3816 | break; |
| 3817 | |
| 3818 | case NFA_NDIGIT: /* \D */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3819 | result = curc != NUL && !ri_digit(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3820 | ADD_POS_NEG_STATE(t->state); |
| 3821 | break; |
| 3822 | |
| 3823 | case NFA_HEX: /* \x */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3824 | result = ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3825 | ADD_POS_NEG_STATE(t->state); |
| 3826 | break; |
| 3827 | |
| 3828 | case NFA_NHEX: /* \X */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3829 | result = curc != NUL && !ri_hex(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3830 | ADD_POS_NEG_STATE(t->state); |
| 3831 | break; |
| 3832 | |
| 3833 | case NFA_OCTAL: /* \o */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3834 | result = ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3835 | ADD_POS_NEG_STATE(t->state); |
| 3836 | break; |
| 3837 | |
| 3838 | case NFA_NOCTAL: /* \O */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3839 | result = curc != NUL && !ri_octal(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3840 | ADD_POS_NEG_STATE(t->state); |
| 3841 | break; |
| 3842 | |
| 3843 | case NFA_WORD: /* \w */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3844 | result = ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3845 | ADD_POS_NEG_STATE(t->state); |
| 3846 | break; |
| 3847 | |
| 3848 | case NFA_NWORD: /* \W */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3849 | result = curc != NUL && !ri_word(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3850 | ADD_POS_NEG_STATE(t->state); |
| 3851 | break; |
| 3852 | |
| 3853 | case NFA_HEAD: /* \h */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3854 | result = ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3855 | ADD_POS_NEG_STATE(t->state); |
| 3856 | break; |
| 3857 | |
| 3858 | case NFA_NHEAD: /* \H */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3859 | result = curc != NUL && !ri_head(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3860 | ADD_POS_NEG_STATE(t->state); |
| 3861 | break; |
| 3862 | |
| 3863 | case NFA_ALPHA: /* \a */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3864 | result = ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3865 | ADD_POS_NEG_STATE(t->state); |
| 3866 | break; |
| 3867 | |
| 3868 | case NFA_NALPHA: /* \A */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3869 | result = curc != NUL && !ri_alpha(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3870 | ADD_POS_NEG_STATE(t->state); |
| 3871 | break; |
| 3872 | |
| 3873 | case NFA_LOWER: /* \l */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3874 | result = ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3875 | ADD_POS_NEG_STATE(t->state); |
| 3876 | break; |
| 3877 | |
| 3878 | case NFA_NLOWER: /* \L */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3879 | result = curc != NUL && !ri_lower(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3880 | ADD_POS_NEG_STATE(t->state); |
| 3881 | break; |
| 3882 | |
| 3883 | case NFA_UPPER: /* \u */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3884 | result = ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3885 | ADD_POS_NEG_STATE(t->state); |
| 3886 | break; |
| 3887 | |
| 3888 | case NFA_NUPPER: /* \U */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 3889 | result = curc != NUL && !ri_upper(curc); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3890 | ADD_POS_NEG_STATE(t->state); |
| 3891 | break; |
| 3892 | |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3893 | case NFA_BACKREF1: |
| 3894 | case NFA_BACKREF2: |
| 3895 | case NFA_BACKREF3: |
| 3896 | case NFA_BACKREF4: |
| 3897 | case NFA_BACKREF5: |
| 3898 | case NFA_BACKREF6: |
| 3899 | case NFA_BACKREF7: |
| 3900 | case NFA_BACKREF8: |
| 3901 | case NFA_BACKREF9: |
| 3902 | /* \1 .. \9 */ |
| 3903 | { |
| 3904 | int subidx = t->state->c - NFA_BACKREF1 + 1; |
| 3905 | int bytelen; |
| 3906 | |
| 3907 | result = match_backref(&t->sub, subidx, &bytelen); |
| 3908 | if (result) |
| 3909 | { |
| 3910 | if (bytelen == 0) |
| 3911 | { |
| 3912 | /* empty match always works, add NFA_SKIP with zero to |
| 3913 | * be used next */ |
| 3914 | addstate_here(thislist, t->state->out, &t->sub, |
| 3915 | &listidx); |
| 3916 | thislist->t[listidx + 1].count = 0; |
| 3917 | } |
| 3918 | else if (bytelen <= clen) |
| 3919 | { |
| 3920 | /* match current character, jump ahead to out of |
| 3921 | * NFA_SKIP */ |
| 3922 | addstate(nextlist, t->state->out->out, &t->sub, clen); |
| 3923 | #ifdef ENABLE_LOG |
| 3924 | log_subexpr(&nextlist->t[nextlist->n - 1].sub); |
| 3925 | #endif |
| 3926 | } |
| 3927 | else |
| 3928 | { |
| 3929 | /* skip ofer the matched characters, set character |
| 3930 | * count in NFA_SKIP */ |
| 3931 | addstate(nextlist, t->state->out, &t->sub, bytelen); |
| 3932 | nextlist->t[nextlist->n - 1].count = bytelen - clen; |
| 3933 | #ifdef ENABLE_LOG |
| 3934 | log_subexpr(&nextlist->t[nextlist->n - 1].sub); |
| 3935 | #endif |
| 3936 | } |
| 3937 | |
| 3938 | } |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 3939 | break; |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 3940 | } |
| 3941 | case NFA_SKIP: |
| 3942 | /* charater of previous matching \1 .. \9 */ |
| 3943 | if (t->count - clen <= 0) |
| 3944 | { |
| 3945 | /* end of match, go to what follows */ |
| 3946 | addstate(nextlist, t->state->out, &t->sub, clen); |
| 3947 | #ifdef ENABLE_LOG |
| 3948 | log_subexpr(&nextlist->t[nextlist->n - 1].sub); |
| 3949 | #endif |
| 3950 | } |
| 3951 | else |
| 3952 | { |
| 3953 | /* add state again with decremented count */ |
| 3954 | addstate(nextlist, t->state, &t->sub, 0); |
| 3955 | nextlist->t[nextlist->n - 1].count = t->count - clen; |
| 3956 | #ifdef ENABLE_LOG |
| 3957 | log_subexpr(&nextlist->t[nextlist->n - 1].sub); |
| 3958 | #endif |
| 3959 | } |
| 3960 | break; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 3961 | |
| 3962 | case NFA_SKIP_CHAR: |
| 3963 | case NFA_ZSTART: |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 3964 | case NFA_ZEND: |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 3965 | /* TODO: should not happen? */ |
| 3966 | break; |
| 3967 | |
Bram Moolenaar | 423532e | 2013-05-29 21:14:42 +0200 | [diff] [blame] | 3968 | case NFA_LNUM: |
| 3969 | case NFA_LNUM_GT: |
| 3970 | case NFA_LNUM_LT: |
| 3971 | result = (REG_MULTI && |
| 3972 | nfa_re_num_cmp(t->state->val, t->state->c - NFA_LNUM, |
| 3973 | (long_u)(reglnum + reg_firstlnum))); |
| 3974 | if (result) |
| 3975 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
| 3976 | break; |
| 3977 | |
| 3978 | case NFA_COL: |
| 3979 | case NFA_COL_GT: |
| 3980 | case NFA_COL_LT: |
| 3981 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_COL, |
| 3982 | (long_u)(reginput - regline) + 1); |
| 3983 | if (result) |
| 3984 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
| 3985 | break; |
| 3986 | |
| 3987 | case NFA_VCOL: |
| 3988 | case NFA_VCOL_GT: |
| 3989 | case NFA_VCOL_LT: |
| 3990 | result = nfa_re_num_cmp(t->state->val, t->state->c - NFA_VCOL, |
| 3991 | (long_u)win_linetabsize( |
| 3992 | reg_win == NULL ? curwin : reg_win, |
| 3993 | regline, (colnr_T)(reginput - regline)) + 1); |
| 3994 | if (result) |
| 3995 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
| 3996 | break; |
| 3997 | |
| 3998 | case NFA_CURSOR: |
| 3999 | result = (reg_win != NULL |
| 4000 | && (reglnum + reg_firstlnum == reg_win->w_cursor.lnum) |
| 4001 | && ((colnr_T)(reginput - regline) |
| 4002 | == reg_win->w_cursor.col)); |
| 4003 | if (result) |
| 4004 | addstate_here(thislist, t->state->out, &t->sub, &listidx); |
| 4005 | break; |
| 4006 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4007 | default: /* regular character */ |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 4008 | { |
| 4009 | int c = t->state->c; |
Bram Moolenaar | 12e4014 | 2013-05-21 15:33:41 +0200 | [diff] [blame] | 4010 | |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 4011 | /* TODO: put this in #ifdef later */ |
| 4012 | if (c < -256) |
| 4013 | EMSGN("INTERNAL: Negative state char: %ld", c); |
| 4014 | if (is_Magic(c)) |
| 4015 | c = un_Magic(c); |
| 4016 | result = (c == curc); |
| 4017 | |
| 4018 | if (!result && ireg_ic) |
| 4019 | result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4020 | #ifdef FEAT_MBYTE |
| 4021 | /* If there is a composing character which is not being |
| 4022 | * ignored there can be no match. Match with composing |
| 4023 | * character uses NFA_COMPOSING above. */ |
| 4024 | if (result && enc_utf8 && !ireg_icombine |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4025 | && clen != utf_char2len(curc)) |
Bram Moolenaar | 3c577f2 | 2013-05-24 21:59:54 +0200 | [diff] [blame] | 4026 | result = FALSE; |
| 4027 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4028 | ADD_POS_NEG_STATE(t->state); |
| 4029 | break; |
Bram Moolenaar | c4912e5 | 2013-05-26 19:19:52 +0200 | [diff] [blame] | 4030 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4031 | } |
| 4032 | |
| 4033 | } /* for (thislist = thislist; thislist->state; thislist++) */ |
| 4034 | |
Bram Moolenaar | e23febd | 2013-05-26 18:40:14 +0200 | [diff] [blame] | 4035 | /* Look for the start of a match in the current position by adding the |
| 4036 | * start state to the list of states. |
| 4037 | * The first found match is the leftmost one, thus the order of states |
| 4038 | * matters! |
| 4039 | * Do not add the start state in recursive calls of nfa_regmatch(), |
| 4040 | * because recursive calls should only start in the first position. |
| 4041 | * Also don't start a match past the first line. */ |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4042 | if (nfa_match == FALSE && start->c == NFA_MOPEN + 0 |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 4043 | && reglnum == 0 && clen != 0 |
| 4044 | && (ireg_maxcol == 0 |
| 4045 | || (colnr_T)(reginput - regline) < ireg_maxcol)) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4046 | { |
| 4047 | #ifdef ENABLE_LOG |
| 4048 | fprintf(log_fd, "(---) STARTSTATE\n"); |
| 4049 | #endif |
Bram Moolenaar | 5714b80 | 2013-05-28 22:03:20 +0200 | [diff] [blame] | 4050 | addstate(nextlist, start, m, clen); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4051 | } |
| 4052 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4053 | #ifdef ENABLE_LOG |
| 4054 | fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4055 | { |
| 4056 | int i; |
| 4057 | |
| 4058 | for (i = 0; i < thislist->n; i++) |
| 4059 | fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); |
| 4060 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4061 | fprintf(log_fd, "\n"); |
| 4062 | #endif |
| 4063 | |
| 4064 | nextchar: |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4065 | /* Advance to the next character, or advance to the next line, or |
| 4066 | * finish. */ |
Bram Moolenaar | 7cd4d9c | 2013-05-26 14:54:12 +0200 | [diff] [blame] | 4067 | if (clen != 0) |
| 4068 | reginput += clen; |
Bram Moolenaar | 35b2386 | 2013-05-22 23:00:40 +0200 | [diff] [blame] | 4069 | else if (go_to_nextline) |
| 4070 | reg_nextline(); |
| 4071 | else |
| 4072 | break; |
| 4073 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4074 | |
| 4075 | #ifdef ENABLE_LOG |
| 4076 | if (log_fd != stderr) |
| 4077 | fclose(log_fd); |
| 4078 | log_fd = NULL; |
| 4079 | #endif |
| 4080 | |
| 4081 | theend: |
| 4082 | /* Free memory */ |
| 4083 | vim_free(list[0].t); |
| 4084 | vim_free(list[1].t); |
| 4085 | vim_free(list[2].t); |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4086 | vim_free(listids); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4087 | #undef ADD_POS_NEG_STATE |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 4088 | #ifdef NFA_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4089 | fclose(debug); |
| 4090 | #endif |
| 4091 | |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4092 | return nfa_match; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4093 | } |
| 4094 | |
| 4095 | /* |
| 4096 | * Try match of "prog" with at regline["col"]. |
| 4097 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 4098 | */ |
| 4099 | static long |
| 4100 | nfa_regtry(start, col) |
| 4101 | nfa_state_T *start; |
| 4102 | colnr_T col; |
| 4103 | { |
| 4104 | int i; |
| 4105 | regsub_T sub, m; |
| 4106 | #ifdef ENABLE_LOG |
| 4107 | FILE *f; |
| 4108 | #endif |
| 4109 | |
| 4110 | reginput = regline + col; |
| 4111 | need_clear_subexpr = TRUE; |
| 4112 | |
| 4113 | #ifdef ENABLE_LOG |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4114 | f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4115 | if (f != NULL) |
| 4116 | { |
| 4117 | fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n"); |
| 4118 | fprintf(f, " =======================================================\n"); |
| 4119 | #ifdef DEBUG |
| 4120 | fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); |
| 4121 | #endif |
| 4122 | fprintf(f, "\tInput text is \"%s\" \n", reginput); |
| 4123 | fprintf(f, " =======================================================\n\n\n\n\n\n\n"); |
Bram Moolenaar | 152e789 | 2013-05-25 12:28:11 +0200 | [diff] [blame] | 4124 | nfa_print_state(f, start); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4125 | fprintf(f, "\n\n"); |
| 4126 | fclose(f); |
| 4127 | } |
| 4128 | else |
| 4129 | EMSG(_("Could not open temporary log file for writing ")); |
| 4130 | #endif |
| 4131 | |
| 4132 | if (REG_MULTI) |
| 4133 | { |
| 4134 | /* Use 0xff to set lnum to -1 */ |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4135 | vim_memset(sub.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr); |
| 4136 | vim_memset(m.list.multi, 0xff, sizeof(struct multipos) * nfa_nsubexpr); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4137 | } |
| 4138 | else |
| 4139 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4140 | vim_memset(sub.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
| 4141 | vim_memset(m.list.line, 0, sizeof(struct linepos) * nfa_nsubexpr); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4142 | } |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4143 | sub.in_use = 0; |
| 4144 | m.in_use = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4145 | |
| 4146 | if (nfa_regmatch(start, &sub, &m) == FALSE) |
| 4147 | return 0; |
| 4148 | |
| 4149 | cleanup_subexpr(); |
| 4150 | if (REG_MULTI) |
| 4151 | { |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4152 | for (i = 0; i < sub.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4153 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4154 | reg_startpos[i] = sub.list.multi[i].start; |
| 4155 | reg_endpos[i] = sub.list.multi[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4156 | } |
| 4157 | |
| 4158 | if (reg_startpos[0].lnum < 0) |
| 4159 | { |
| 4160 | reg_startpos[0].lnum = 0; |
| 4161 | reg_startpos[0].col = col; |
| 4162 | } |
| 4163 | if (reg_endpos[0].lnum < 0) |
| 4164 | { |
Bram Moolenaar | e0fea9c | 2013-05-27 20:10:50 +0200 | [diff] [blame] | 4165 | /* pattern has a \ze but it didn't match, use current end */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4166 | reg_endpos[0].lnum = reglnum; |
| 4167 | reg_endpos[0].col = (int)(reginput - regline); |
| 4168 | } |
| 4169 | else |
| 4170 | /* Use line number of "\ze". */ |
| 4171 | reglnum = reg_endpos[0].lnum; |
| 4172 | } |
| 4173 | else |
| 4174 | { |
Bram Moolenaar | 26c2f3f | 2013-05-26 22:56:19 +0200 | [diff] [blame] | 4175 | for (i = 0; i < sub.in_use; i++) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4176 | { |
Bram Moolenaar | f9e56b2 | 2013-05-28 22:52:16 +0200 | [diff] [blame] | 4177 | reg_startp[i] = sub.list.line[i].start; |
| 4178 | reg_endp[i] = sub.list.line[i].end; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | if (reg_startp[0] == NULL) |
| 4182 | reg_startp[0] = regline + col; |
| 4183 | if (reg_endp[0] == NULL) |
| 4184 | reg_endp[0] = reginput; |
| 4185 | } |
| 4186 | |
| 4187 | return 1 + reglnum; |
| 4188 | } |
| 4189 | |
| 4190 | /* |
| 4191 | * Match a regexp against a string ("line" points to the string) or multiple |
| 4192 | * lines ("line" is NULL, use reg_getline()). |
| 4193 | * |
| 4194 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 4195 | */ |
| 4196 | static long |
| 4197 | nfa_regexec_both(line, col) |
| 4198 | char_u *line; |
| 4199 | colnr_T col; /* column to start looking for match */ |
| 4200 | { |
| 4201 | nfa_regprog_T *prog; |
| 4202 | long retval = 0L; |
| 4203 | int i; |
| 4204 | |
| 4205 | if (REG_MULTI) |
| 4206 | { |
| 4207 | prog = (nfa_regprog_T *)reg_mmatch->regprog; |
| 4208 | line = reg_getline((linenr_T)0); /* relative to the cursor */ |
| 4209 | reg_startpos = reg_mmatch->startpos; |
| 4210 | reg_endpos = reg_mmatch->endpos; |
| 4211 | } |
| 4212 | else |
| 4213 | { |
| 4214 | prog = (nfa_regprog_T *)reg_match->regprog; |
| 4215 | reg_startp = reg_match->startp; |
| 4216 | reg_endp = reg_match->endp; |
| 4217 | } |
| 4218 | |
| 4219 | /* Be paranoid... */ |
| 4220 | if (prog == NULL || line == NULL) |
| 4221 | { |
| 4222 | EMSG(_(e_null)); |
| 4223 | goto theend; |
| 4224 | } |
| 4225 | |
| 4226 | /* If the start column is past the maximum column: no need to try. */ |
| 4227 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 4228 | goto theend; |
| 4229 | |
| 4230 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 4231 | if (prog->regflags & RF_ICASE) |
| 4232 | ireg_ic = TRUE; |
| 4233 | else if (prog->regflags & RF_NOICASE) |
| 4234 | ireg_ic = FALSE; |
| 4235 | |
| 4236 | #ifdef FEAT_MBYTE |
| 4237 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 4238 | if (prog->regflags & RF_ICOMBINE) |
| 4239 | ireg_icombine = TRUE; |
| 4240 | #endif |
| 4241 | |
| 4242 | regline = line; |
| 4243 | reglnum = 0; /* relative to line */ |
| 4244 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4245 | nfa_has_zend = prog->has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4246 | nfa_has_backref = prog->has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4247 | nfa_nsubexpr = prog->nsubexp; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4248 | |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4249 | nstate = prog->nstate; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4250 | for (i = 0; i < nstate; ++i) |
| 4251 | { |
| 4252 | prog->state[i].id = i; |
| 4253 | prog->state[i].lastlist = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4254 | } |
| 4255 | |
| 4256 | retval = nfa_regtry(prog->start, col); |
| 4257 | |
| 4258 | theend: |
| 4259 | return retval; |
| 4260 | } |
| 4261 | |
| 4262 | /* |
| 4263 | * Compile a regular expression into internal code for the NFA matcher. |
| 4264 | * Returns the program in allocated space. Returns NULL for an error. |
| 4265 | */ |
| 4266 | static regprog_T * |
| 4267 | nfa_regcomp(expr, re_flags) |
| 4268 | char_u *expr; |
| 4269 | int re_flags; |
| 4270 | { |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 4271 | nfa_regprog_T *prog = NULL; |
Bram Moolenaar | ca12d7c | 2013-05-20 21:26:33 +0200 | [diff] [blame] | 4272 | size_t prog_size; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4273 | int *postfix; |
| 4274 | |
| 4275 | if (expr == NULL) |
| 4276 | return NULL; |
| 4277 | |
| 4278 | #ifdef DEBUG |
| 4279 | nfa_regengine.expr = expr; |
| 4280 | #endif |
| 4281 | |
| 4282 | init_class_tab(); |
| 4283 | |
| 4284 | if (nfa_regcomp_start(expr, re_flags) == FAIL) |
| 4285 | return NULL; |
| 4286 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4287 | /* Build postfix form of the regexp. Needed to build the NFA |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 4288 | * (and count its size). */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4289 | postfix = re2post(); |
| 4290 | if (postfix == NULL) |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4291 | { |
| 4292 | /* TODO: only give this error for debugging? */ |
| 4293 | if (post_ptr >= post_end) |
| 4294 | 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] | 4295 | goto fail; /* Cascaded (syntax?) error */ |
Bram Moolenaar | 61db8b5 | 2013-05-26 17:45:49 +0200 | [diff] [blame] | 4296 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4297 | |
| 4298 | /* |
| 4299 | * In order to build the NFA, we parse the input regexp twice: |
| 4300 | * 1. first pass to count size (so we can allocate space) |
| 4301 | * 2. second to emit code |
| 4302 | */ |
| 4303 | #ifdef ENABLE_LOG |
| 4304 | { |
Bram Moolenaar | d6c11cb | 2013-05-25 12:18:39 +0200 | [diff] [blame] | 4305 | FILE *f = fopen(NFA_REGEXP_RUN_LOG, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4306 | |
| 4307 | if (f != NULL) |
| 4308 | { |
| 4309 | fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); |
| 4310 | fclose(f); |
| 4311 | } |
| 4312 | } |
| 4313 | #endif |
| 4314 | |
| 4315 | /* |
| 4316 | * PASS 1 |
| 4317 | * Count number of NFA states in "nstate". Do not build the NFA. |
| 4318 | */ |
| 4319 | post2nfa(postfix, post_ptr, TRUE); |
Bram Moolenaar | aae4883 | 2013-05-25 21:18:34 +0200 | [diff] [blame] | 4320 | |
| 4321 | /* Space for compiled regexp */ |
| 4322 | prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate; |
| 4323 | prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); |
| 4324 | if (prog == NULL) |
| 4325 | goto fail; |
| 4326 | vim_memset(prog, 0, prog_size); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4327 | state_ptr = prog->state; |
| 4328 | |
| 4329 | /* |
| 4330 | * PASS 2 |
| 4331 | * Build the NFA |
| 4332 | */ |
| 4333 | prog->start = post2nfa(postfix, post_ptr, FALSE); |
| 4334 | if (prog->start == NULL) |
| 4335 | goto fail; |
| 4336 | |
| 4337 | prog->regflags = regflags; |
| 4338 | prog->engine = &nfa_regengine; |
| 4339 | prog->nstate = nstate; |
Bram Moolenaar | 57a285b | 2013-05-26 16:57:28 +0200 | [diff] [blame] | 4340 | prog->has_zend = nfa_has_zend; |
Bram Moolenaar | 428e987 | 2013-05-30 17:05:39 +0200 | [diff] [blame] | 4341 | prog->has_backref = nfa_has_backref; |
Bram Moolenaar | 963fee2 | 2013-05-26 21:47:28 +0200 | [diff] [blame] | 4342 | prog->nsubexp = regnpar; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4343 | #ifdef ENABLE_LOG |
| 4344 | nfa_postfix_dump(expr, OK); |
| 4345 | nfa_dump(prog); |
| 4346 | #endif |
| 4347 | |
| 4348 | out: |
| 4349 | vim_free(post_start); |
| 4350 | post_start = post_ptr = post_end = NULL; |
| 4351 | state_ptr = NULL; |
| 4352 | return (regprog_T *)prog; |
| 4353 | |
| 4354 | fail: |
| 4355 | vim_free(prog); |
| 4356 | prog = NULL; |
| 4357 | #ifdef ENABLE_LOG |
| 4358 | nfa_postfix_dump(expr, FAIL); |
| 4359 | #endif |
| 4360 | #ifdef DEBUG |
| 4361 | nfa_regengine.expr = NULL; |
| 4362 | #endif |
| 4363 | goto out; |
| 4364 | } |
| 4365 | |
| 4366 | |
| 4367 | /* |
| 4368 | * Match a regexp against a string. |
| 4369 | * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). |
| 4370 | * Uses curbuf for line count and 'iskeyword'. |
| 4371 | * |
| 4372 | * Return TRUE if there is a match, FALSE if not. |
| 4373 | */ |
| 4374 | static int |
| 4375 | nfa_regexec(rmp, line, col) |
| 4376 | regmatch_T *rmp; |
| 4377 | char_u *line; /* string to match against */ |
| 4378 | colnr_T col; /* column to start looking for match */ |
| 4379 | { |
| 4380 | reg_match = rmp; |
| 4381 | reg_mmatch = NULL; |
| 4382 | reg_maxline = 0; |
| 4383 | reg_line_lbr = FALSE; |
| 4384 | reg_buf = curbuf; |
| 4385 | reg_win = NULL; |
| 4386 | ireg_ic = rmp->rm_ic; |
| 4387 | #ifdef FEAT_MBYTE |
| 4388 | ireg_icombine = FALSE; |
| 4389 | #endif |
| 4390 | ireg_maxcol = 0; |
| 4391 | return (nfa_regexec_both(line, col) != 0); |
| 4392 | } |
| 4393 | |
| 4394 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 4395 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 4396 | |
| 4397 | static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 4398 | |
| 4399 | /* |
| 4400 | * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. |
| 4401 | */ |
| 4402 | static int |
| 4403 | nfa_regexec_nl(rmp, line, col) |
| 4404 | regmatch_T *rmp; |
| 4405 | char_u *line; /* string to match against */ |
| 4406 | colnr_T col; /* column to start looking for match */ |
| 4407 | { |
| 4408 | reg_match = rmp; |
| 4409 | reg_mmatch = NULL; |
| 4410 | reg_maxline = 0; |
| 4411 | reg_line_lbr = TRUE; |
| 4412 | reg_buf = curbuf; |
| 4413 | reg_win = NULL; |
| 4414 | ireg_ic = rmp->rm_ic; |
| 4415 | #ifdef FEAT_MBYTE |
| 4416 | ireg_icombine = FALSE; |
| 4417 | #endif |
| 4418 | ireg_maxcol = 0; |
| 4419 | return (nfa_regexec_both(line, col) != 0); |
| 4420 | } |
| 4421 | #endif |
| 4422 | |
| 4423 | |
| 4424 | /* |
| 4425 | * Match a regexp against multiple lines. |
| 4426 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 4427 | * Uses curbuf for line count and 'iskeyword'. |
| 4428 | * |
| 4429 | * Return zero if there is no match. Return number of lines contained in the |
| 4430 | * match otherwise. |
| 4431 | * |
| 4432 | * Note: the body is the same as bt_regexec() except for nfa_regexec_both() |
| 4433 | * |
| 4434 | * ! Also NOTE : match may actually be in another line. e.g.: |
| 4435 | * when r.e. is \nc, cursor is at 'a' and the text buffer looks like |
| 4436 | * |
| 4437 | * +-------------------------+ |
| 4438 | * |a | |
| 4439 | * |b | |
| 4440 | * |c | |
| 4441 | * | | |
| 4442 | * +-------------------------+ |
| 4443 | * |
| 4444 | * then nfa_regexec_multi() returns 3. while the original |
| 4445 | * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. |
| 4446 | * |
| 4447 | * FIXME if this behavior is not compatible. |
| 4448 | */ |
| 4449 | static long |
| 4450 | nfa_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 4451 | regmmatch_T *rmp; |
| 4452 | win_T *win; /* window in which to search or NULL */ |
| 4453 | buf_T *buf; /* buffer in which to search */ |
| 4454 | linenr_T lnum; /* nr of line to start looking for match */ |
| 4455 | colnr_T col; /* column to start looking for match */ |
| 4456 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
| 4457 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4458 | reg_match = NULL; |
| 4459 | reg_mmatch = rmp; |
| 4460 | reg_buf = buf; |
| 4461 | reg_win = win; |
| 4462 | reg_firstlnum = lnum; |
| 4463 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 4464 | reg_line_lbr = FALSE; |
| 4465 | ireg_ic = rmp->rmm_ic; |
| 4466 | #ifdef FEAT_MBYTE |
| 4467 | ireg_icombine = FALSE; |
| 4468 | #endif |
| 4469 | ireg_maxcol = rmp->rmm_maxcol; |
| 4470 | |
Bram Moolenaar | f878bf0 | 2013-05-21 21:20:20 +0200 | [diff] [blame] | 4471 | return nfa_regexec_both(NULL, col); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4472 | } |
| 4473 | |
| 4474 | #ifdef DEBUG |
| 4475 | # undef ENABLE_LOG |
| 4476 | #endif |