Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 2 | * |
| 3 | * Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub() |
| 4 | * |
| 5 | * NOTICE: |
| 6 | * |
| 7 | * This is NOT the original regular expression code as written by Henry |
| 8 | * Spencer. This code has been modified specifically for use with the VIM |
| 9 | * editor, and should not be used separately from Vim. If you want a good |
| 10 | * regular expression library, get the original code. The copyright notice |
| 11 | * that follows is from the original. |
| 12 | * |
| 13 | * END NOTICE |
| 14 | * |
| 15 | * Copyright (c) 1986 by University of Toronto. |
| 16 | * Written by Henry Spencer. Not derived from licensed software. |
| 17 | * |
| 18 | * Permission is granted to anyone to use this software for any |
| 19 | * purpose on any computer system, and to redistribute it freely, |
| 20 | * subject to the following restrictions: |
| 21 | * |
| 22 | * 1. The author is not responsible for the consequences of use of |
| 23 | * this software, no matter how awful, even if they arise |
| 24 | * from defects in it. |
| 25 | * |
| 26 | * 2. The origin of this software must not be misrepresented, either |
| 27 | * by explicit claim or by omission. |
| 28 | * |
| 29 | * 3. Altered versions must be plainly marked as such, and must not |
| 30 | * be misrepresented as being the original software. |
| 31 | * |
| 32 | * Beware that some of this code is subtly aware of the way operator |
| 33 | * precedence is structured in regular expressions. Serious changes in |
| 34 | * regular-expression syntax might require a total rethink. |
| 35 | * |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 36 | * Changes have been made by Tony Andrews, Olaf 'Rhialto' Seibert, Robert |
| 37 | * Webb, Ciaran McCreesh and Bram Moolenaar. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 38 | * Named character class support added by Walter Briscoe (1998 Jul 01) |
| 39 | */ |
| 40 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 41 | /* Uncomment the first if you do not want to see debugging logs or files |
| 42 | * related to regular expressions, even when compiling with -DDEBUG. |
| 43 | * Uncomment the second to get the regexp debugging. */ |
| 44 | /* #undef DEBUG */ |
| 45 | /* #define DEBUG */ |
| 46 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 47 | #include "vim.h" |
| 48 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 49 | #ifdef DEBUG |
| 50 | /* show/save debugging data when BT engine is used */ |
| 51 | # define BT_REGEXP_DUMP |
| 52 | /* save the debugging data to a file instead of displaying it */ |
| 53 | # define BT_REGEXP_LOG |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 54 | # define BT_REGEXP_DEBUG_LOG |
| 55 | # define BT_REGEXP_DEBUG_LOG_NAME "bt_regexp_debug.log" |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 56 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 57 | |
| 58 | /* |
| 59 | * The "internal use only" fields in regexp.h are present to pass info from |
| 60 | * compile to execute that permits the execute phase to run lots faster on |
| 61 | * simple cases. They are: |
| 62 | * |
| 63 | * regstart char that must begin a match; NUL if none obvious; Can be a |
| 64 | * multi-byte character. |
| 65 | * reganch is the match anchored (at beginning-of-line only)? |
| 66 | * regmust string (pointer into program) that match must include, or NULL |
| 67 | * regmlen length of regmust string |
| 68 | * regflags RF_ values or'ed together |
| 69 | * |
| 70 | * Regstart and reganch permit very fast decisions on suitable starting points |
| 71 | * for a match, cutting down the work a lot. Regmust permits fast rejection |
| 72 | * of lines that cannot possibly match. The regmust tests are costly enough |
| 73 | * that vim_regcomp() supplies a regmust only if the r.e. contains something |
| 74 | * potentially expensive (at present, the only such thing detected is * or + |
| 75 | * at the start of the r.e., which can involve a lot of backup). Regmlen is |
| 76 | * supplied because the test in vim_regexec() needs it and vim_regcomp() is |
| 77 | * computing it anyway. |
| 78 | */ |
| 79 | |
| 80 | /* |
| 81 | * Structure for regexp "program". This is essentially a linear encoding |
| 82 | * of a nondeterministic finite-state machine (aka syntax charts or |
| 83 | * "railroad normal form" in parsing technology). Each node is an opcode |
| 84 | * plus a "next" pointer, possibly plus an operand. "Next" pointers of |
| 85 | * all nodes except BRANCH and BRACES_COMPLEX implement concatenation; a "next" |
| 86 | * pointer with a BRANCH on both ends of it is connecting two alternatives. |
| 87 | * (Here we have one of the subtle syntax dependencies: an individual BRANCH |
| 88 | * (as opposed to a collection of them) is never concatenated with anything |
| 89 | * because of operator precedence). The "next" pointer of a BRACES_COMPLEX |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 90 | * node points to the node after the stuff to be repeated. |
| 91 | * The operand of some types of node is a literal string; for others, it is a |
| 92 | * node leading into a sub-FSM. In particular, the operand of a BRANCH node |
| 93 | * is the first node of the branch. |
| 94 | * (NB this is *not* a tree structure: the tail of the branch connects to the |
| 95 | * thing following the set of BRANCHes.) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 96 | * |
| 97 | * pattern is coded like: |
| 98 | * |
| 99 | * +-----------------+ |
| 100 | * | V |
| 101 | * <aa>\|<bb> BRANCH <aa> BRANCH <bb> --> END |
| 102 | * | ^ | ^ |
| 103 | * +------+ +----------+ |
| 104 | * |
| 105 | * |
| 106 | * +------------------+ |
| 107 | * V | |
| 108 | * <aa>* BRANCH BRANCH <aa> --> BACK BRANCH --> NOTHING --> END |
| 109 | * | | ^ ^ |
| 110 | * | +---------------+ | |
| 111 | * +---------------------------------------------+ |
| 112 | * |
| 113 | * |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 114 | * +----------------------+ |
| 115 | * V | |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 116 | * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 117 | * | | ^ ^ |
| 118 | * | +-----------+ | |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 119 | * +--------------------------------------------------+ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 120 | * |
| 121 | * |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 122 | * +-------------------------+ |
| 123 | * V | |
| 124 | * <aa>\{} BRANCH BRACE_LIMITS --> BRACE_COMPLEX <aa> --> BACK END |
| 125 | * | | ^ |
| 126 | * | +----------------+ |
| 127 | * +-----------------------------------------------+ |
| 128 | * |
| 129 | * |
| 130 | * <aa>\@!<bb> BRANCH NOMATCH <aa> --> END <bb> --> END |
| 131 | * | | ^ ^ |
| 132 | * | +----------------+ | |
| 133 | * +--------------------------------+ |
| 134 | * |
| 135 | * +---------+ |
| 136 | * | V |
| 137 | * \z[abc] BRANCH BRANCH a BRANCH b BRANCH c BRANCH NOTHING --> END |
| 138 | * | | | | ^ ^ |
| 139 | * | | | +-----+ | |
| 140 | * | | +----------------+ | |
| 141 | * | +---------------------------+ | |
| 142 | * +------------------------------------------------------+ |
| 143 | * |
Bram Moolenaar | 7aa9f6a | 2007-05-10 18:00:30 +0000 | [diff] [blame] | 144 | * They all start with a BRANCH for "\|" alternatives, even when there is only |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 145 | * one alternative. |
| 146 | */ |
| 147 | |
| 148 | /* |
| 149 | * The opcodes are: |
| 150 | */ |
| 151 | |
| 152 | /* definition number opnd? meaning */ |
| 153 | #define END 0 /* End of program or NOMATCH operand. */ |
| 154 | #define BOL 1 /* Match "" at beginning of line. */ |
| 155 | #define EOL 2 /* Match "" at end of line. */ |
| 156 | #define BRANCH 3 /* node Match this alternative, or the |
| 157 | * next... */ |
| 158 | #define BACK 4 /* Match "", "next" ptr points backward. */ |
| 159 | #define EXACTLY 5 /* str Match this string. */ |
| 160 | #define NOTHING 6 /* Match empty string. */ |
| 161 | #define STAR 7 /* node Match this (simple) thing 0 or more |
| 162 | * times. */ |
| 163 | #define PLUS 8 /* node Match this (simple) thing 1 or more |
| 164 | * times. */ |
| 165 | #define MATCH 9 /* node match the operand zero-width */ |
| 166 | #define NOMATCH 10 /* node check for no match with operand */ |
| 167 | #define BEHIND 11 /* node look behind for a match with operand */ |
| 168 | #define NOBEHIND 12 /* node look behind for no match with operand */ |
| 169 | #define SUBPAT 13 /* node match the operand here */ |
| 170 | #define BRACE_SIMPLE 14 /* node Match this (simple) thing between m and |
| 171 | * n times (\{m,n\}). */ |
| 172 | #define BOW 15 /* Match "" after [^a-zA-Z0-9_] */ |
| 173 | #define EOW 16 /* Match "" at [^a-zA-Z0-9_] */ |
| 174 | #define BRACE_LIMITS 17 /* nr nr define the min & max for BRACE_SIMPLE |
| 175 | * and BRACE_COMPLEX. */ |
| 176 | #define NEWL 18 /* Match line-break */ |
| 177 | #define BHPOS 19 /* End position for BEHIND or NOBEHIND */ |
| 178 | |
| 179 | |
| 180 | /* character classes: 20-48 normal, 50-78 include a line-break */ |
| 181 | #define ADD_NL 30 |
| 182 | #define FIRST_NL ANY + ADD_NL |
| 183 | #define ANY 20 /* Match any one character. */ |
| 184 | #define ANYOF 21 /* str Match any character in this string. */ |
| 185 | #define ANYBUT 22 /* str Match any character not in this |
| 186 | * string. */ |
| 187 | #define IDENT 23 /* Match identifier char */ |
| 188 | #define SIDENT 24 /* Match identifier char but no digit */ |
| 189 | #define KWORD 25 /* Match keyword char */ |
| 190 | #define SKWORD 26 /* Match word char but no digit */ |
| 191 | #define FNAME 27 /* Match file name char */ |
| 192 | #define SFNAME 28 /* Match file name char but no digit */ |
| 193 | #define PRINT 29 /* Match printable char */ |
| 194 | #define SPRINT 30 /* Match printable char but no digit */ |
| 195 | #define WHITE 31 /* Match whitespace char */ |
| 196 | #define NWHITE 32 /* Match non-whitespace char */ |
| 197 | #define DIGIT 33 /* Match digit char */ |
| 198 | #define NDIGIT 34 /* Match non-digit char */ |
| 199 | #define HEX 35 /* Match hex char */ |
| 200 | #define NHEX 36 /* Match non-hex char */ |
| 201 | #define OCTAL 37 /* Match octal char */ |
| 202 | #define NOCTAL 38 /* Match non-octal char */ |
| 203 | #define WORD 39 /* Match word char */ |
| 204 | #define NWORD 40 /* Match non-word char */ |
| 205 | #define HEAD 41 /* Match head char */ |
| 206 | #define NHEAD 42 /* Match non-head char */ |
| 207 | #define ALPHA 43 /* Match alpha char */ |
| 208 | #define NALPHA 44 /* Match non-alpha char */ |
| 209 | #define LOWER 45 /* Match lowercase char */ |
| 210 | #define NLOWER 46 /* Match non-lowercase char */ |
| 211 | #define UPPER 47 /* Match uppercase char */ |
| 212 | #define NUPPER 48 /* Match non-uppercase char */ |
| 213 | #define LAST_NL NUPPER + ADD_NL |
| 214 | #define WITH_NL(op) ((op) >= FIRST_NL && (op) <= LAST_NL) |
| 215 | |
| 216 | #define MOPEN 80 /* -89 Mark this point in input as start of |
| 217 | * \( subexpr. MOPEN + 0 marks start of |
| 218 | * match. */ |
| 219 | #define MCLOSE 90 /* -99 Analogous to MOPEN. MCLOSE + 0 marks |
| 220 | * end of match. */ |
| 221 | #define BACKREF 100 /* -109 node Match same string again \1-\9 */ |
| 222 | |
| 223 | #ifdef FEAT_SYN_HL |
| 224 | # define ZOPEN 110 /* -119 Mark this point in input as start of |
| 225 | * \z( subexpr. */ |
| 226 | # define ZCLOSE 120 /* -129 Analogous to ZOPEN. */ |
| 227 | # define ZREF 130 /* -139 node Match external submatch \z1-\z9 */ |
| 228 | #endif |
| 229 | |
| 230 | #define BRACE_COMPLEX 140 /* -149 node Match nodes between m & n times */ |
| 231 | |
| 232 | #define NOPEN 150 /* Mark this point in input as start of |
| 233 | \%( subexpr. */ |
| 234 | #define NCLOSE 151 /* Analogous to NOPEN. */ |
| 235 | |
| 236 | #define MULTIBYTECODE 200 /* mbc Match one multi-byte character */ |
| 237 | #define RE_BOF 201 /* Match "" at beginning of file. */ |
| 238 | #define RE_EOF 202 /* Match "" at end of file. */ |
| 239 | #define CURSOR 203 /* Match location of cursor. */ |
| 240 | |
| 241 | #define RE_LNUM 204 /* nr cmp Match line number */ |
| 242 | #define RE_COL 205 /* nr cmp Match column number */ |
| 243 | #define RE_VCOL 206 /* nr cmp Match virtual column number */ |
| 244 | |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 245 | #define RE_MARK 207 /* mark cmp Match mark position */ |
| 246 | #define RE_VISUAL 208 /* Match Visual area */ |
| 247 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 248 | /* |
| 249 | * Magic characters have a special meaning, they don't match literally. |
| 250 | * Magic characters are negative. This separates them from literal characters |
| 251 | * (possibly multi-byte). Only ASCII characters can be Magic. |
| 252 | */ |
| 253 | #define Magic(x) ((int)(x) - 256) |
| 254 | #define un_Magic(x) ((x) + 256) |
| 255 | #define is_Magic(x) ((x) < 0) |
| 256 | |
| 257 | static int no_Magic __ARGS((int x)); |
| 258 | static int toggle_Magic __ARGS((int x)); |
| 259 | |
| 260 | static int |
| 261 | no_Magic(x) |
| 262 | int x; |
| 263 | { |
| 264 | if (is_Magic(x)) |
| 265 | return un_Magic(x); |
| 266 | return x; |
| 267 | } |
| 268 | |
| 269 | static int |
| 270 | toggle_Magic(x) |
| 271 | int x; |
| 272 | { |
| 273 | if (is_Magic(x)) |
| 274 | return un_Magic(x); |
| 275 | return Magic(x); |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * The first byte of the regexp internal "program" is actually this magic |
| 280 | * number; the start node begins in the second byte. It's used to catch the |
| 281 | * most severe mutilation of the program by the caller. |
| 282 | */ |
| 283 | |
| 284 | #define REGMAGIC 0234 |
| 285 | |
| 286 | /* |
| 287 | * Opcode notes: |
| 288 | * |
| 289 | * BRANCH The set of branches constituting a single choice are hooked |
| 290 | * together with their "next" pointers, since precedence prevents |
| 291 | * anything being concatenated to any individual branch. The |
| 292 | * "next" pointer of the last BRANCH in a choice points to the |
| 293 | * thing following the whole choice. This is also where the |
| 294 | * final "next" pointer of each individual branch points; each |
| 295 | * branch starts with the operand node of a BRANCH node. |
| 296 | * |
| 297 | * BACK Normal "next" pointers all implicitly point forward; BACK |
| 298 | * exists to make loop structures possible. |
| 299 | * |
| 300 | * STAR,PLUS '=', and complex '*' and '+', are implemented as circular |
| 301 | * BRANCH structures using BACK. Simple cases (one character |
| 302 | * per match) are implemented with STAR and PLUS for speed |
| 303 | * and to minimize recursive plunges. |
| 304 | * |
| 305 | * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX |
| 306 | * node, and defines the min and max limits to be used for that |
| 307 | * node. |
| 308 | * |
| 309 | * MOPEN,MCLOSE ...are numbered at compile time. |
| 310 | * ZOPEN,ZCLOSE ...ditto |
| 311 | */ |
| 312 | |
| 313 | /* |
| 314 | * A node is one char of opcode followed by two chars of "next" pointer. |
| 315 | * "Next" pointers are stored as two 8-bit bytes, high order first. The |
| 316 | * value is a positive offset from the opcode of the node containing it. |
| 317 | * An operand, if any, simply follows the node. (Note that much of the |
| 318 | * code generation knows about this implicit relationship.) |
| 319 | * |
| 320 | * Using two bytes for the "next" pointer is vast overkill for most things, |
| 321 | * but allows patterns to get big without disasters. |
| 322 | */ |
| 323 | #define OP(p) ((int)*(p)) |
| 324 | #define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377)) |
| 325 | #define OPERAND(p) ((p) + 3) |
| 326 | /* Obtain an operand that was stored as four bytes, MSB first. */ |
| 327 | #define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \ |
| 328 | + ((long)(p)[5] << 8) + (long)(p)[6]) |
| 329 | /* Obtain a second operand stored as four bytes. */ |
| 330 | #define OPERAND_MAX(p) OPERAND_MIN((p) + 4) |
| 331 | /* Obtain a second single-byte operand stored after a four bytes operand. */ |
| 332 | #define OPERAND_CMP(p) (p)[7] |
| 333 | |
| 334 | /* |
| 335 | * Utility definitions. |
| 336 | */ |
| 337 | #define UCHARAT(p) ((int)*(char_u *)(p)) |
| 338 | |
| 339 | /* Used for an error (down from) vim_regcomp(): give the error message, set |
| 340 | * rc_did_emsg and return NULL */ |
Bram Moolenaar | 9869207 | 2006-02-04 00:57:42 +0000 | [diff] [blame] | 341 | #define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL) |
Bram Moolenaar | 45eeb13 | 2005-06-06 21:59:07 +0000 | [diff] [blame] | 342 | #define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 343 | #define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL) |
| 344 | #define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL) |
| 345 | #define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 346 | |
| 347 | #define MAX_LIMIT (32767L << 16L) |
| 348 | |
| 349 | static int re_multi_type __ARGS((int)); |
| 350 | static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n)); |
| 351 | static char_u *cstrchr __ARGS((char_u *, int)); |
| 352 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 353 | #ifdef BT_REGEXP_DUMP |
| 354 | static void regdump __ARGS((char_u *, bt_regprog_T *)); |
| 355 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 356 | #ifdef DEBUG |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 357 | static char_u *regprop __ARGS((char_u *)); |
| 358 | #endif |
| 359 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 360 | static char_u e_missingbracket[] = N_("E769: Missing ] after %s["); |
| 361 | static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%("); |
| 362 | static char_u e_unmatchedp[] = N_("E54: Unmatched %s("); |
| 363 | static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)"); |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 364 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 365 | static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here"); |
| 366 | static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here"); |
Bram Moolenaar | 01d89dd | 2013-06-03 19:41:06 +0200 | [diff] [blame] | 367 | #endif |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 368 | static char_u e_missing_sb[] = N_("E69: Missing ] after %s%%["); |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 369 | static char_u e_empty_sb[] = N_("E70: Empty %s%%[]"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 370 | #define NOT_MULTI 0 |
| 371 | #define MULTI_ONE 1 |
| 372 | #define MULTI_MULT 2 |
| 373 | /* |
| 374 | * Return NOT_MULTI if c is not a "multi" operator. |
| 375 | * Return MULTI_ONE if c is a single "multi" operator. |
| 376 | * Return MULTI_MULT if c is a multi "multi" operator. |
| 377 | */ |
| 378 | static int |
| 379 | re_multi_type(c) |
| 380 | int c; |
| 381 | { |
| 382 | if (c == Magic('@') || c == Magic('=') || c == Magic('?')) |
| 383 | return MULTI_ONE; |
| 384 | if (c == Magic('*') || c == Magic('+') || c == Magic('{')) |
| 385 | return MULTI_MULT; |
| 386 | return NOT_MULTI; |
| 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Flags to be passed up and down. |
| 391 | */ |
| 392 | #define HASWIDTH 0x1 /* Known never to match null string. */ |
| 393 | #define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */ |
| 394 | #define SPSTART 0x4 /* Starts with * or +. */ |
| 395 | #define HASNL 0x8 /* Contains some \n. */ |
| 396 | #define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */ |
| 397 | #define WORST 0 /* Worst case. */ |
| 398 | |
| 399 | /* |
| 400 | * When regcode is set to this value, code is not emitted and size is computed |
| 401 | * instead. |
| 402 | */ |
| 403 | #define JUST_CALC_SIZE ((char_u *) -1) |
| 404 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 405 | static char_u *reg_prev_sub = NULL; |
| 406 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 407 | /* |
| 408 | * REGEXP_INRANGE contains all characters which are always special in a [] |
| 409 | * range after '\'. |
| 410 | * REGEXP_ABBR contains all characters which act as abbreviations after '\'. |
| 411 | * These are: |
| 412 | * \n - New line (NL). |
| 413 | * \r - Carriage Return (CR). |
| 414 | * \t - Tab (TAB). |
| 415 | * \e - Escape (ESC). |
| 416 | * \b - Backspace (Ctrl_H). |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 417 | * \d - Character code in decimal, eg \d123 |
| 418 | * \o - Character code in octal, eg \o80 |
| 419 | * \x - Character code in hex, eg \x4a |
| 420 | * \u - Multibyte character code, eg \u20ac |
| 421 | * \U - Long multibyte character code, eg \U12345678 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 422 | */ |
| 423 | static char_u REGEXP_INRANGE[] = "]^-n\\"; |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 424 | static char_u REGEXP_ABBR[] = "nrtebdoxuU"; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 425 | |
| 426 | static int backslash_trans __ARGS((int c)); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 427 | static int get_char_class __ARGS((char_u **pp)); |
| 428 | static int get_equi_class __ARGS((char_u **pp)); |
| 429 | static void reg_equi_class __ARGS((int c)); |
| 430 | static int get_coll_element __ARGS((char_u **pp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 431 | static char_u *skip_anyof __ARGS((char_u *p)); |
| 432 | static void init_class_tab __ARGS((void)); |
| 433 | |
| 434 | /* |
| 435 | * Translate '\x' to its control character, except "\n", which is Magic. |
| 436 | */ |
| 437 | static int |
| 438 | backslash_trans(c) |
| 439 | int c; |
| 440 | { |
| 441 | switch (c) |
| 442 | { |
| 443 | case 'r': return CAR; |
| 444 | case 't': return TAB; |
| 445 | case 'e': return ESC; |
| 446 | case 'b': return BS; |
| 447 | } |
| 448 | return c; |
| 449 | } |
| 450 | |
| 451 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 452 | * Check for a character class name "[:name:]". "pp" points to the '['. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | * Returns one of the CLASS_ items. CLASS_NONE means that no item was |
| 454 | * recognized. Otherwise "pp" is advanced to after the item. |
| 455 | */ |
| 456 | static int |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 457 | get_char_class(pp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 458 | char_u **pp; |
| 459 | { |
| 460 | static const char *(class_names[]) = |
| 461 | { |
| 462 | "alnum:]", |
| 463 | #define CLASS_ALNUM 0 |
| 464 | "alpha:]", |
| 465 | #define CLASS_ALPHA 1 |
| 466 | "blank:]", |
| 467 | #define CLASS_BLANK 2 |
| 468 | "cntrl:]", |
| 469 | #define CLASS_CNTRL 3 |
| 470 | "digit:]", |
| 471 | #define CLASS_DIGIT 4 |
| 472 | "graph:]", |
| 473 | #define CLASS_GRAPH 5 |
| 474 | "lower:]", |
| 475 | #define CLASS_LOWER 6 |
| 476 | "print:]", |
| 477 | #define CLASS_PRINT 7 |
| 478 | "punct:]", |
| 479 | #define CLASS_PUNCT 8 |
| 480 | "space:]", |
| 481 | #define CLASS_SPACE 9 |
| 482 | "upper:]", |
| 483 | #define CLASS_UPPER 10 |
| 484 | "xdigit:]", |
| 485 | #define CLASS_XDIGIT 11 |
| 486 | "tab:]", |
| 487 | #define CLASS_TAB 12 |
| 488 | "return:]", |
| 489 | #define CLASS_RETURN 13 |
| 490 | "backspace:]", |
| 491 | #define CLASS_BACKSPACE 14 |
| 492 | "escape:]", |
| 493 | #define CLASS_ESCAPE 15 |
| 494 | }; |
| 495 | #define CLASS_NONE 99 |
| 496 | int i; |
| 497 | |
| 498 | if ((*pp)[1] == ':') |
| 499 | { |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 500 | for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0) |
| 502 | { |
| 503 | *pp += STRLEN(class_names[i]) + 2; |
| 504 | return i; |
| 505 | } |
| 506 | } |
| 507 | return CLASS_NONE; |
| 508 | } |
| 509 | |
| 510 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 511 | * Specific version of character class functions. |
| 512 | * Using a table to keep this fast. |
| 513 | */ |
| 514 | static short class_tab[256]; |
| 515 | |
| 516 | #define RI_DIGIT 0x01 |
| 517 | #define RI_HEX 0x02 |
| 518 | #define RI_OCTAL 0x04 |
| 519 | #define RI_WORD 0x08 |
| 520 | #define RI_HEAD 0x10 |
| 521 | #define RI_ALPHA 0x20 |
| 522 | #define RI_LOWER 0x40 |
| 523 | #define RI_UPPER 0x80 |
| 524 | #define RI_WHITE 0x100 |
| 525 | |
| 526 | static void |
| 527 | init_class_tab() |
| 528 | { |
| 529 | int i; |
| 530 | static int done = FALSE; |
| 531 | |
| 532 | if (done) |
| 533 | return; |
| 534 | |
| 535 | for (i = 0; i < 256; ++i) |
| 536 | { |
| 537 | if (i >= '0' && i <= '7') |
| 538 | class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD; |
| 539 | else if (i >= '8' && i <= '9') |
| 540 | class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD; |
| 541 | else if (i >= 'a' && i <= 'f') |
| 542 | class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER; |
| 543 | #ifdef EBCDIC |
| 544 | else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r') |
| 545 | || (i >= 's' && i <= 'z')) |
| 546 | #else |
| 547 | else if (i >= 'g' && i <= 'z') |
| 548 | #endif |
| 549 | class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER; |
| 550 | else if (i >= 'A' && i <= 'F') |
| 551 | class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER; |
| 552 | #ifdef EBCDIC |
| 553 | else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R') |
| 554 | || (i >= 'S' && i <= 'Z')) |
| 555 | #else |
| 556 | else if (i >= 'G' && i <= 'Z') |
| 557 | #endif |
| 558 | class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER; |
| 559 | else if (i == '_') |
| 560 | class_tab[i] = RI_WORD + RI_HEAD; |
| 561 | else |
| 562 | class_tab[i] = 0; |
| 563 | } |
| 564 | class_tab[' '] |= RI_WHITE; |
| 565 | class_tab['\t'] |= RI_WHITE; |
| 566 | done = TRUE; |
| 567 | } |
| 568 | |
| 569 | #ifdef FEAT_MBYTE |
| 570 | # define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT)) |
| 571 | # define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX)) |
| 572 | # define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL)) |
| 573 | # define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD)) |
| 574 | # define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD)) |
| 575 | # define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA)) |
| 576 | # define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER)) |
| 577 | # define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER)) |
| 578 | # define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE)) |
| 579 | #else |
| 580 | # define ri_digit(c) (class_tab[c] & RI_DIGIT) |
| 581 | # define ri_hex(c) (class_tab[c] & RI_HEX) |
| 582 | # define ri_octal(c) (class_tab[c] & RI_OCTAL) |
| 583 | # define ri_word(c) (class_tab[c] & RI_WORD) |
| 584 | # define ri_head(c) (class_tab[c] & RI_HEAD) |
| 585 | # define ri_alpha(c) (class_tab[c] & RI_ALPHA) |
| 586 | # define ri_lower(c) (class_tab[c] & RI_LOWER) |
| 587 | # define ri_upper(c) (class_tab[c] & RI_UPPER) |
| 588 | # define ri_white(c) (class_tab[c] & RI_WHITE) |
| 589 | #endif |
| 590 | |
| 591 | /* flags for regflags */ |
| 592 | #define RF_ICASE 1 /* ignore case */ |
| 593 | #define RF_NOICASE 2 /* don't ignore case */ |
| 594 | #define RF_HASNL 4 /* can match a NL */ |
| 595 | #define RF_ICOMBINE 8 /* ignore combining characters */ |
| 596 | #define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */ |
| 597 | |
| 598 | /* |
| 599 | * Global work variables for vim_regcomp(). |
| 600 | */ |
| 601 | |
| 602 | static char_u *regparse; /* Input-scan pointer. */ |
| 603 | static int prevchr_len; /* byte length of previous char */ |
| 604 | static int num_complex_braces; /* Complex \{...} count */ |
| 605 | static int regnpar; /* () count. */ |
| 606 | #ifdef FEAT_SYN_HL |
| 607 | static int regnzpar; /* \z() count. */ |
| 608 | static int re_has_z; /* \z item detected */ |
| 609 | #endif |
| 610 | static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */ |
| 611 | static long regsize; /* Code size. */ |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 612 | static int reg_toolong; /* TRUE when offset out of range */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 613 | static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */ |
| 614 | static unsigned regflags; /* RF_ flags for prog */ |
| 615 | static long brace_min[10]; /* Minimums for complex brace repeats */ |
| 616 | static long brace_max[10]; /* Maximums for complex brace repeats */ |
| 617 | static int brace_count[10]; /* Current counts for complex brace repeats */ |
| 618 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 619 | static int had_eol; /* TRUE when EOL found by vim_regcomp() */ |
| 620 | #endif |
| 621 | static int one_exactly = FALSE; /* only do one char for EXACTLY */ |
| 622 | |
| 623 | static int reg_magic; /* magicness of the pattern: */ |
| 624 | #define MAGIC_NONE 1 /* "\V" very unmagic */ |
| 625 | #define MAGIC_OFF 2 /* "\M" or 'magic' off */ |
| 626 | #define MAGIC_ON 3 /* "\m" or 'magic' */ |
| 627 | #define MAGIC_ALL 4 /* "\v" very magic */ |
| 628 | |
| 629 | static int reg_string; /* matching with a string instead of a buffer |
| 630 | line */ |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 631 | static int reg_strict; /* "[abc" is illegal */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 632 | |
| 633 | /* |
| 634 | * META contains all characters that may be magic, except '^' and '$'. |
| 635 | */ |
| 636 | |
| 637 | #ifdef EBCDIC |
| 638 | static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~"; |
| 639 | #else |
| 640 | /* META[] is used often enough to justify turning it into a table. */ |
| 641 | static char_u META_flags[] = { |
| 642 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 643 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 644 | /* % & ( ) * + . */ |
| 645 | 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, |
| 646 | /* 1 2 3 4 5 6 7 8 9 < = > ? */ |
| 647 | 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, |
| 648 | /* @ A C D F H I K L M O */ |
| 649 | 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, |
| 650 | /* P S U V W X Z [ _ */ |
| 651 | 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1, |
| 652 | /* a c d f h i k l m n o */ |
| 653 | 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, |
| 654 | /* p s u v w x z { | ~ */ |
| 655 | 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1 |
| 656 | }; |
| 657 | #endif |
| 658 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 659 | static int curchr; /* currently parsed character */ |
| 660 | /* Previous character. Note: prevchr is sometimes -1 when we are not at the |
| 661 | * start, eg in /[ ^I]^ the pattern was never found even if it existed, |
| 662 | * because ^ was taken to be magic -- webb */ |
| 663 | static int prevchr; |
| 664 | static int prevprevchr; /* previous-previous character */ |
| 665 | static int nextchr; /* used for ungetchr() */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | |
| 667 | /* arguments for reg() */ |
| 668 | #define REG_NOPAREN 0 /* toplevel reg() */ |
| 669 | #define REG_PAREN 1 /* \(\) */ |
| 670 | #define REG_ZPAREN 2 /* \z(\) */ |
| 671 | #define REG_NPAREN 3 /* \%(\) */ |
| 672 | |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 673 | typedef struct |
| 674 | { |
| 675 | char_u *regparse; |
| 676 | int prevchr_len; |
| 677 | int curchr; |
| 678 | int prevchr; |
| 679 | int prevprevchr; |
| 680 | int nextchr; |
| 681 | int at_start; |
| 682 | int prev_at_start; |
| 683 | int regnpar; |
| 684 | } parse_state_T; |
| 685 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 686 | /* |
| 687 | * Forward declarations for vim_regcomp()'s friends. |
| 688 | */ |
| 689 | static void initchr __ARGS((char_u *)); |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 690 | static void save_parse_state __ARGS((parse_state_T *ps)); |
| 691 | static void restore_parse_state __ARGS((parse_state_T *ps)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 692 | static int getchr __ARGS((void)); |
| 693 | static void skipchr_keepstart __ARGS((void)); |
| 694 | static int peekchr __ARGS((void)); |
| 695 | static void skipchr __ARGS((void)); |
| 696 | static void ungetchr __ARGS((void)); |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 697 | static int gethexchrs __ARGS((int maxinputlen)); |
| 698 | static int getoctchrs __ARGS((void)); |
| 699 | static int getdecchrs __ARGS((void)); |
| 700 | static int coll_get_char __ARGS((void)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 701 | static void regcomp_start __ARGS((char_u *expr, int flags)); |
| 702 | static char_u *reg __ARGS((int, int *)); |
| 703 | static char_u *regbranch __ARGS((int *flagp)); |
| 704 | static char_u *regconcat __ARGS((int *flagp)); |
| 705 | static char_u *regpiece __ARGS((int *)); |
| 706 | static char_u *regatom __ARGS((int *)); |
| 707 | static char_u *regnode __ARGS((int)); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 708 | #ifdef FEAT_MBYTE |
| 709 | static int use_multibytecode __ARGS((int c)); |
| 710 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 711 | static int prog_magic_wrong __ARGS((void)); |
| 712 | static char_u *regnext __ARGS((char_u *)); |
| 713 | static void regc __ARGS((int b)); |
| 714 | #ifdef FEAT_MBYTE |
| 715 | static void regmbc __ARGS((int c)); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 716 | # define REGMBC(x) regmbc(x); |
| 717 | # define CASEMBC(x) case x: |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 718 | #else |
| 719 | # define regmbc(c) regc(c) |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 720 | # define REGMBC(x) |
| 721 | # define CASEMBC(x) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 722 | #endif |
| 723 | static void reginsert __ARGS((int, char_u *)); |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 724 | static void reginsert_nr __ARGS((int op, long val, char_u *opnd)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 725 | static void reginsert_limits __ARGS((int, long, long, char_u *)); |
| 726 | static char_u *re_put_long __ARGS((char_u *pr, long_u val)); |
| 727 | static int read_limits __ARGS((long *, long *)); |
| 728 | static void regtail __ARGS((char_u *, char_u *)); |
| 729 | static void regoptail __ARGS((char_u *, char_u *)); |
| 730 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 731 | static regengine_T bt_regengine; |
| 732 | static regengine_T nfa_regengine; |
| 733 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | /* |
| 735 | * Return TRUE if compiled regular expression "prog" can match a line break. |
| 736 | */ |
| 737 | int |
| 738 | re_multiline(prog) |
| 739 | regprog_T *prog; |
| 740 | { |
| 741 | return (prog->regflags & RF_HASNL); |
| 742 | } |
| 743 | |
| 744 | /* |
| 745 | * Return TRUE if compiled regular expression "prog" looks before the start |
| 746 | * position (pattern contains "\@<=" or "\@<!"). |
| 747 | */ |
| 748 | int |
| 749 | re_lookbehind(prog) |
| 750 | regprog_T *prog; |
| 751 | { |
| 752 | return (prog->regflags & RF_LOOKBH); |
| 753 | } |
| 754 | |
| 755 | /* |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 756 | * Check for an equivalence class name "[=a=]". "pp" points to the '['. |
| 757 | * Returns a character representing the class. Zero means that no item was |
| 758 | * recognized. Otherwise "pp" is advanced to after the item. |
| 759 | */ |
| 760 | static int |
| 761 | get_equi_class(pp) |
| 762 | char_u **pp; |
| 763 | { |
| 764 | int c; |
| 765 | int l = 1; |
| 766 | char_u *p = *pp; |
| 767 | |
| 768 | if (p[1] == '=') |
| 769 | { |
| 770 | #ifdef FEAT_MBYTE |
| 771 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 772 | l = (*mb_ptr2len)(p + 2); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 773 | #endif |
| 774 | if (p[l + 2] == '=' && p[l + 3] == ']') |
| 775 | { |
| 776 | #ifdef FEAT_MBYTE |
| 777 | if (has_mbyte) |
| 778 | c = mb_ptr2char(p + 2); |
| 779 | else |
| 780 | #endif |
| 781 | c = p[2]; |
| 782 | *pp += l + 4; |
| 783 | return c; |
| 784 | } |
| 785 | } |
| 786 | return 0; |
| 787 | } |
| 788 | |
Bram Moolenaar | 2c704a7 | 2010-06-03 21:17:25 +0200 | [diff] [blame] | 789 | #ifdef EBCDIC |
| 790 | /* |
| 791 | * Table for equivalence class "c". (IBM-1047) |
| 792 | */ |
| 793 | char *EQUIVAL_CLASS_C[16] = { |
| 794 | "A\x62\x63\x64\x65\x66\x67", |
| 795 | "C\x68", |
| 796 | "E\x71\x72\x73\x74", |
| 797 | "I\x75\x76\x77\x78", |
| 798 | "N\x69", |
| 799 | "O\xEB\xEC\xED\xEE\xEF", |
| 800 | "U\xFB\xFC\xFD\xFE", |
| 801 | "Y\xBA", |
| 802 | "a\x42\x43\x44\x45\x46\x47", |
| 803 | "c\x48", |
| 804 | "e\x51\x52\x53\x54", |
| 805 | "i\x55\x56\x57\x58", |
| 806 | "n\x49", |
| 807 | "o\xCB\xCC\xCD\xCE\xCF", |
| 808 | "u\xDB\xDC\xDD\xDE", |
| 809 | "y\x8D\xDF", |
| 810 | }; |
| 811 | #endif |
| 812 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 813 | /* |
| 814 | * Produce the bytes for equivalence class "c". |
| 815 | * Currently only handles latin1, latin9 and utf-8. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 816 | * NOTE: When changing this function, also change nfa_emit_equi_class() |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 817 | */ |
| 818 | static void |
| 819 | reg_equi_class(c) |
| 820 | int c; |
| 821 | { |
| 822 | #ifdef FEAT_MBYTE |
| 823 | if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 |
Bram Moolenaar | 7862282 | 2005-08-23 21:00:13 +0000 | [diff] [blame] | 824 | || STRCMP(p_enc, "iso-8859-15") == 0) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 825 | #endif |
| 826 | { |
Bram Moolenaar | 2c704a7 | 2010-06-03 21:17:25 +0200 | [diff] [blame] | 827 | #ifdef EBCDIC |
| 828 | int i; |
| 829 | |
| 830 | /* This might be slower than switch/case below. */ |
| 831 | for (i = 0; i < 16; i++) |
| 832 | { |
| 833 | if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL) |
| 834 | { |
| 835 | char *p = EQUIVAL_CLASS_C[i]; |
| 836 | |
| 837 | while (*p != 0) |
| 838 | regmbc(*p++); |
| 839 | return; |
| 840 | } |
| 841 | } |
| 842 | #else |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 843 | switch (c) |
| 844 | { |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 845 | case 'A': case '\300': case '\301': case '\302': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 846 | CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd) |
| 847 | CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 848 | case '\303': case '\304': case '\305': |
| 849 | regmbc('A'); regmbc('\300'); regmbc('\301'); |
| 850 | regmbc('\302'); regmbc('\303'); regmbc('\304'); |
| 851 | regmbc('\305'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 852 | REGMBC(0x100) REGMBC(0x102) REGMBC(0x104) |
| 853 | REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0) |
| 854 | REGMBC(0x1ea2) |
| 855 | return; |
| 856 | case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06) |
| 857 | regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 858 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 859 | case 'C': case '\307': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 860 | CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 861 | regmbc('C'); regmbc('\307'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 862 | REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a) |
| 863 | REGMBC(0x10c) |
| 864 | return; |
| 865 | case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a) |
| 866 | CASEMBC(0x1e0e) CASEMBC(0x1e10) |
| 867 | regmbc('D'); REGMBC(0x10e) REGMBC(0x110) |
| 868 | REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 869 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 870 | case 'E': case '\310': case '\311': case '\312': case '\313': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 871 | CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118) |
| 872 | CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 873 | regmbc('E'); regmbc('\310'); regmbc('\311'); |
| 874 | regmbc('\312'); regmbc('\313'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 875 | REGMBC(0x112) REGMBC(0x114) REGMBC(0x116) |
| 876 | REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba) |
| 877 | REGMBC(0x1ebc) |
| 878 | return; |
| 879 | case 'F': CASEMBC(0x1e1e) |
| 880 | regmbc('F'); REGMBC(0x1e1e) |
| 881 | return; |
| 882 | case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120) |
| 883 | CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4) |
| 884 | CASEMBC(0x1e20) |
| 885 | regmbc('G'); REGMBC(0x11c) REGMBC(0x11e) |
| 886 | REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4) |
| 887 | REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20) |
| 888 | return; |
| 889 | case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22) |
| 890 | CASEMBC(0x1e26) CASEMBC(0x1e28) |
| 891 | regmbc('H'); REGMBC(0x124) REGMBC(0x126) |
| 892 | REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 893 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 894 | case 'I': case '\314': case '\315': case '\316': case '\317': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 895 | CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e) |
| 896 | CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 897 | regmbc('I'); regmbc('\314'); regmbc('\315'); |
| 898 | regmbc('\316'); regmbc('\317'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 899 | REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c) |
| 900 | REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf) |
| 901 | REGMBC(0x1ec8) |
| 902 | return; |
| 903 | case 'J': CASEMBC(0x134) |
| 904 | regmbc('J'); REGMBC(0x134) |
| 905 | return; |
| 906 | case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30) |
| 907 | CASEMBC(0x1e34) |
| 908 | regmbc('K'); REGMBC(0x136) REGMBC(0x1e8) |
| 909 | REGMBC(0x1e30) REGMBC(0x1e34) |
| 910 | return; |
| 911 | case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d) |
| 912 | CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a) |
| 913 | regmbc('L'); REGMBC(0x139) REGMBC(0x13b) |
| 914 | REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141) |
| 915 | REGMBC(0x1e3a) |
| 916 | return; |
| 917 | case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40) |
| 918 | regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 919 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 920 | case 'N': case '\321': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 921 | CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44) |
| 922 | CASEMBC(0x1e48) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 923 | regmbc('N'); regmbc('\321'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 924 | REGMBC(0x143) REGMBC(0x145) REGMBC(0x147) |
| 925 | REGMBC(0x1e44) REGMBC(0x1e48) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 926 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 927 | case 'O': case '\322': case '\323': case '\324': case '\325': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 928 | case '\326': case '\330': |
| 929 | CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0) |
| 930 | CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 931 | regmbc('O'); regmbc('\322'); regmbc('\323'); |
| 932 | regmbc('\324'); regmbc('\325'); regmbc('\326'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 933 | regmbc('\330'); |
| 934 | REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150) |
| 935 | REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea) |
| 936 | REGMBC(0x1ec) REGMBC(0x1ece) |
| 937 | return; |
| 938 | case 'P': case 0x1e54: case 0x1e56: |
| 939 | regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56) |
| 940 | return; |
| 941 | case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158) |
| 942 | CASEMBC(0x1e58) CASEMBC(0x1e5e) |
| 943 | regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158) |
| 944 | REGMBC(0x1e58) REGMBC(0x1e5e) |
| 945 | return; |
| 946 | case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e) |
| 947 | CASEMBC(0x160) CASEMBC(0x1e60) |
| 948 | regmbc('S'); REGMBC(0x15a) REGMBC(0x15c) |
| 949 | REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60) |
| 950 | return; |
| 951 | case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166) |
| 952 | CASEMBC(0x1e6a) CASEMBC(0x1e6e) |
| 953 | regmbc('T'); REGMBC(0x162) REGMBC(0x164) |
| 954 | REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 955 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 956 | case 'U': case '\331': case '\332': case '\333': case '\334': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 957 | CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e) |
| 958 | CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3) |
| 959 | CASEMBC(0x1ee6) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 960 | regmbc('U'); regmbc('\331'); regmbc('\332'); |
| 961 | regmbc('\333'); regmbc('\334'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 962 | REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c) |
| 963 | REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172) |
| 964 | REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6) |
| 965 | return; |
| 966 | case 'V': CASEMBC(0x1e7c) |
| 967 | regmbc('V'); REGMBC(0x1e7c) |
| 968 | return; |
| 969 | case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82) |
| 970 | CASEMBC(0x1e84) CASEMBC(0x1e86) |
| 971 | regmbc('W'); REGMBC(0x174) REGMBC(0x1e80) |
| 972 | REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86) |
| 973 | return; |
| 974 | case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c) |
| 975 | regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 976 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 977 | case 'Y': case '\335': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 978 | CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2) |
| 979 | CASEMBC(0x1ef6) CASEMBC(0x1ef8) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 980 | regmbc('Y'); regmbc('\335'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 981 | REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e) |
| 982 | REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8) |
| 983 | return; |
| 984 | case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d) |
| 985 | CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94) |
| 986 | regmbc('Z'); REGMBC(0x179) REGMBC(0x17b) |
| 987 | REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90) |
| 988 | REGMBC(0x1e94) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 989 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 990 | case 'a': case '\340': case '\341': case '\342': |
| 991 | case '\343': case '\344': case '\345': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 992 | CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce) |
| 993 | CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 994 | regmbc('a'); regmbc('\340'); regmbc('\341'); |
| 995 | regmbc('\342'); regmbc('\343'); regmbc('\344'); |
| 996 | regmbc('\345'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 997 | REGMBC(0x101) REGMBC(0x103) REGMBC(0x105) |
| 998 | REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1) |
| 999 | REGMBC(0x1ea3) |
| 1000 | return; |
| 1001 | case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07) |
| 1002 | regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1003 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1004 | case 'c': case '\347': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1005 | CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1006 | regmbc('c'); regmbc('\347'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1007 | REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b) |
| 1008 | REGMBC(0x10d) |
| 1009 | return; |
| 1010 | case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b) |
| 1011 | CASEMBC(0x1e11) |
| 1012 | regmbc('d'); REGMBC(0x10f) REGMBC(0x111) |
| 1013 | REGMBC(0x1e0b) REGMBC(0x01e0f) REGMBC(0x1e11) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1014 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1015 | case 'e': case '\350': case '\351': case '\352': case '\353': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1016 | CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119) |
| 1017 | CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1018 | regmbc('e'); regmbc('\350'); regmbc('\351'); |
| 1019 | regmbc('\352'); regmbc('\353'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1020 | REGMBC(0x113) REGMBC(0x115) REGMBC(0x117) |
| 1021 | REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb) |
| 1022 | REGMBC(0x1ebd) |
| 1023 | return; |
| 1024 | case 'f': CASEMBC(0x1e1f) |
| 1025 | regmbc('f'); REGMBC(0x1e1f) |
| 1026 | return; |
| 1027 | case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121) |
| 1028 | CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5) |
| 1029 | CASEMBC(0x1e21) |
| 1030 | regmbc('g'); REGMBC(0x11d) REGMBC(0x11f) |
| 1031 | REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5) |
| 1032 | REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21) |
| 1033 | return; |
| 1034 | case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23) |
| 1035 | CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96) |
| 1036 | regmbc('h'); REGMBC(0x125) REGMBC(0x127) |
| 1037 | REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29) |
| 1038 | REGMBC(0x1e96) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1039 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1040 | case 'i': case '\354': case '\355': case '\356': case '\357': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1041 | CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f) |
| 1042 | CASEMBC(0x1d0) CASEMBC(0x1ec9) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1043 | regmbc('i'); regmbc('\354'); regmbc('\355'); |
| 1044 | regmbc('\356'); regmbc('\357'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1045 | REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d) |
| 1046 | REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9) |
| 1047 | return; |
| 1048 | case 'j': CASEMBC(0x135) CASEMBC(0x1f0) |
| 1049 | regmbc('j'); REGMBC(0x135) REGMBC(0x1f0) |
| 1050 | return; |
| 1051 | case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31) |
| 1052 | CASEMBC(0x1e35) |
| 1053 | regmbc('k'); REGMBC(0x137) REGMBC(0x1e9) |
| 1054 | REGMBC(0x1e31) REGMBC(0x1e35) |
| 1055 | return; |
| 1056 | case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e) |
| 1057 | CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b) |
| 1058 | regmbc('l'); REGMBC(0x13a) REGMBC(0x13c) |
| 1059 | REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142) |
| 1060 | REGMBC(0x1e3b) |
| 1061 | return; |
| 1062 | case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41) |
| 1063 | regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1064 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1065 | case 'n': case '\361': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1066 | CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149) |
| 1067 | CASEMBC(0x1e45) CASEMBC(0x1e49) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1068 | regmbc('n'); regmbc('\361'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1069 | REGMBC(0x144) REGMBC(0x146) REGMBC(0x148) |
| 1070 | REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1071 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1072 | case 'o': case '\362': case '\363': case '\364': case '\365': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1073 | case '\366': case '\370': |
| 1074 | CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1) |
| 1075 | CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1076 | regmbc('o'); regmbc('\362'); regmbc('\363'); |
| 1077 | regmbc('\364'); regmbc('\365'); regmbc('\366'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1078 | regmbc('\370'); |
| 1079 | REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151) |
| 1080 | REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb) |
| 1081 | REGMBC(0x1ed) REGMBC(0x1ecf) |
| 1082 | return; |
| 1083 | case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57) |
| 1084 | regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57) |
| 1085 | return; |
| 1086 | case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159) |
| 1087 | CASEMBC(0x1e59) CASEMBC(0x1e5f) |
| 1088 | regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159) |
| 1089 | REGMBC(0x1e59) REGMBC(0x1e5f) |
| 1090 | return; |
| 1091 | case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f) |
| 1092 | CASEMBC(0x161) CASEMBC(0x1e61) |
| 1093 | regmbc('s'); REGMBC(0x15b) REGMBC(0x15d) |
| 1094 | REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61) |
| 1095 | return; |
| 1096 | case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167) |
| 1097 | CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97) |
| 1098 | regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167) |
| 1099 | REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1100 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1101 | case 'u': case '\371': case '\372': case '\373': case '\374': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1102 | CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f) |
| 1103 | CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4) |
| 1104 | CASEMBC(0x1ee7) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1105 | regmbc('u'); regmbc('\371'); regmbc('\372'); |
| 1106 | regmbc('\373'); regmbc('\374'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1107 | REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d) |
| 1108 | REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173) |
| 1109 | REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7) |
| 1110 | return; |
| 1111 | case 'v': CASEMBC(0x1e7d) |
| 1112 | regmbc('v'); REGMBC(0x1e7d) |
| 1113 | return; |
| 1114 | case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83) |
| 1115 | CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98) |
| 1116 | regmbc('w'); REGMBC(0x175) REGMBC(0x1e81) |
| 1117 | REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87) |
| 1118 | REGMBC(0x1e98) |
| 1119 | return; |
| 1120 | case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d) |
| 1121 | regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1122 | return; |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1123 | case 'y': case '\375': case '\377': |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1124 | CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99) |
| 1125 | CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9) |
Bram Moolenaar | 402d2fe | 2005-04-15 21:00:38 +0000 | [diff] [blame] | 1126 | regmbc('y'); regmbc('\375'); regmbc('\377'); |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 1127 | REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99) |
| 1128 | REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9) |
| 1129 | return; |
| 1130 | case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e) |
| 1131 | CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) |
| 1132 | regmbc('z'); REGMBC(0x17a) REGMBC(0x17c) |
| 1133 | REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91) |
| 1134 | REGMBC(0x1e95) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1135 | return; |
| 1136 | } |
Bram Moolenaar | 2c704a7 | 2010-06-03 21:17:25 +0200 | [diff] [blame] | 1137 | #endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1138 | } |
| 1139 | regmbc(c); |
| 1140 | } |
| 1141 | |
| 1142 | /* |
| 1143 | * Check for a collating element "[.a.]". "pp" points to the '['. |
| 1144 | * Returns a character. Zero means that no item was recognized. Otherwise |
| 1145 | * "pp" is advanced to after the item. |
| 1146 | * Currently only single characters are recognized! |
| 1147 | */ |
| 1148 | static int |
| 1149 | get_coll_element(pp) |
| 1150 | char_u **pp; |
| 1151 | { |
| 1152 | int c; |
| 1153 | int l = 1; |
| 1154 | char_u *p = *pp; |
| 1155 | |
| 1156 | if (p[1] == '.') |
| 1157 | { |
| 1158 | #ifdef FEAT_MBYTE |
| 1159 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1160 | l = (*mb_ptr2len)(p + 2); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1161 | #endif |
| 1162 | if (p[l + 2] == '.' && p[l + 3] == ']') |
| 1163 | { |
| 1164 | #ifdef FEAT_MBYTE |
| 1165 | if (has_mbyte) |
| 1166 | c = mb_ptr2char(p + 2); |
| 1167 | else |
| 1168 | #endif |
| 1169 | c = p[2]; |
| 1170 | *pp += l + 4; |
| 1171 | return c; |
| 1172 | } |
| 1173 | } |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1177 | static void get_cpo_flags __ARGS((void)); |
| 1178 | static int reg_cpo_lit; /* 'cpoptions' contains 'l' flag */ |
| 1179 | static int reg_cpo_bsl; /* 'cpoptions' contains '\' flag */ |
| 1180 | |
| 1181 | static void |
| 1182 | get_cpo_flags() |
| 1183 | { |
| 1184 | reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL; |
| 1185 | reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL; |
| 1186 | } |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1187 | |
| 1188 | /* |
| 1189 | * Skip over a "[]" range. |
| 1190 | * "p" must point to the character after the '['. |
| 1191 | * The returned pointer is on the matching ']', or the terminating NUL. |
| 1192 | */ |
| 1193 | static char_u * |
| 1194 | skip_anyof(p) |
| 1195 | char_u *p; |
| 1196 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1197 | #ifdef FEAT_MBYTE |
| 1198 | int l; |
| 1199 | #endif |
| 1200 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1201 | if (*p == '^') /* Complement of range. */ |
| 1202 | ++p; |
| 1203 | if (*p == ']' || *p == '-') |
| 1204 | ++p; |
| 1205 | while (*p != NUL && *p != ']') |
| 1206 | { |
| 1207 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1208 | if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1209 | p += l; |
| 1210 | else |
| 1211 | #endif |
| 1212 | if (*p == '-') |
| 1213 | { |
| 1214 | ++p; |
| 1215 | if (*p != ']' && *p != NUL) |
| 1216 | mb_ptr_adv(p); |
| 1217 | } |
| 1218 | else if (*p == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1219 | && !reg_cpo_bsl |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1220 | && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1221 | || (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL))) |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 1222 | p += 2; |
| 1223 | else if (*p == '[') |
| 1224 | { |
| 1225 | if (get_char_class(&p) == CLASS_NONE |
| 1226 | && get_equi_class(&p) == 0 |
| 1227 | && get_coll_element(&p) == 0) |
| 1228 | ++p; /* It was not a class name */ |
| 1229 | } |
| 1230 | else |
| 1231 | ++p; |
| 1232 | } |
| 1233 | |
| 1234 | return p; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1238 | * Skip past regular expression. |
Bram Moolenaar | 748bf03 | 2005-02-02 23:04:36 +0000 | [diff] [blame] | 1239 | * Stop at end of "startp" or where "dirc" is found ('/', '?', etc). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1240 | * Take care of characters with a backslash in front of it. |
| 1241 | * Skip strings inside [ and ]. |
| 1242 | * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the |
| 1243 | * expression and change "\?" to "?". If "*newp" is not NULL the expression |
| 1244 | * is changed in-place. |
| 1245 | */ |
| 1246 | char_u * |
| 1247 | skip_regexp(startp, dirc, magic, newp) |
| 1248 | char_u *startp; |
| 1249 | int dirc; |
| 1250 | int magic; |
| 1251 | char_u **newp; |
| 1252 | { |
| 1253 | int mymagic; |
| 1254 | char_u *p = startp; |
| 1255 | |
| 1256 | if (magic) |
| 1257 | mymagic = MAGIC_ON; |
| 1258 | else |
| 1259 | mymagic = MAGIC_OFF; |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1260 | get_cpo_flags(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1261 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 1262 | for (; p[0] != NUL; mb_ptr_adv(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1263 | { |
| 1264 | if (p[0] == dirc) /* found end of regexp */ |
| 1265 | break; |
| 1266 | if ((p[0] == '[' && mymagic >= MAGIC_ON) |
| 1267 | || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF)) |
| 1268 | { |
| 1269 | p = skip_anyof(p + 1); |
| 1270 | if (p[0] == NUL) |
| 1271 | break; |
| 1272 | } |
| 1273 | else if (p[0] == '\\' && p[1] != NUL) |
| 1274 | { |
| 1275 | if (dirc == '?' && newp != NULL && p[1] == '?') |
| 1276 | { |
| 1277 | /* change "\?" to "?", make a copy first. */ |
| 1278 | if (*newp == NULL) |
| 1279 | { |
| 1280 | *newp = vim_strsave(startp); |
| 1281 | if (*newp != NULL) |
| 1282 | p = *newp + (p - startp); |
| 1283 | } |
| 1284 | if (*newp != NULL) |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 1285 | STRMOVE(p, p + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1286 | else |
| 1287 | ++p; |
| 1288 | } |
| 1289 | else |
| 1290 | ++p; /* skip next character */ |
| 1291 | if (*p == 'v') |
| 1292 | mymagic = MAGIC_ALL; |
| 1293 | else if (*p == 'V') |
| 1294 | mymagic = MAGIC_NONE; |
| 1295 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1296 | } |
| 1297 | return p; |
| 1298 | } |
| 1299 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 1300 | static regprog_T *bt_regcomp __ARGS((char_u *expr, int re_flags)); |
| 1301 | static void bt_regfree __ARGS((regprog_T *prog)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1302 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1304 | * bt_regcomp() - compile a regular expression into internal code for the |
| 1305 | * traditional back track matcher. |
Bram Moolenaar | 86b6835 | 2004-12-27 21:59:20 +0000 | [diff] [blame] | 1306 | * Returns the program in allocated space. Returns NULL for an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1307 | * |
| 1308 | * We can't allocate space until we know how big the compiled form will be, |
| 1309 | * but we can't compile it (and thus know how big it is) until we've got a |
| 1310 | * place to put the code. So we cheat: we compile it twice, once with code |
| 1311 | * generation turned off and size counting turned on, and once "for real". |
| 1312 | * This also means that we don't allocate space until we are sure that the |
| 1313 | * thing really will compile successfully, and we never have to move the |
| 1314 | * code and thus invalidate pointers into it. (Note that it has to be in |
| 1315 | * one piece because vim_free() must be able to free it all.) |
| 1316 | * |
| 1317 | * Whether upper/lower case is to be ignored is decided when executing the |
| 1318 | * program, it does not matter here. |
| 1319 | * |
| 1320 | * Beware that the optimization-preparation code in here knows about some |
| 1321 | * of the structure of the compiled regexp. |
| 1322 | * "re_flags": RE_MAGIC and/or RE_STRING. |
| 1323 | */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1324 | static regprog_T * |
| 1325 | bt_regcomp(expr, re_flags) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1326 | char_u *expr; |
| 1327 | int re_flags; |
| 1328 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1329 | bt_regprog_T *r; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1330 | char_u *scan; |
| 1331 | char_u *longest; |
| 1332 | int len; |
| 1333 | int flags; |
| 1334 | |
| 1335 | if (expr == NULL) |
| 1336 | EMSG_RET_NULL(_(e_null)); |
| 1337 | |
| 1338 | init_class_tab(); |
| 1339 | |
| 1340 | /* |
| 1341 | * First pass: determine size, legality. |
| 1342 | */ |
| 1343 | regcomp_start(expr, re_flags); |
| 1344 | regcode = JUST_CALC_SIZE; |
| 1345 | regc(REGMAGIC); |
| 1346 | if (reg(REG_NOPAREN, &flags) == NULL) |
| 1347 | return NULL; |
| 1348 | |
| 1349 | /* Small enough for pointer-storage convention? */ |
| 1350 | #ifdef SMALL_MALLOC /* 16 bit storage allocation */ |
| 1351 | if (regsize >= 65536L - 256L) |
| 1352 | EMSG_RET_NULL(_("E339: Pattern too long")); |
| 1353 | #endif |
| 1354 | |
| 1355 | /* Allocate space. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1356 | r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1357 | if (r == NULL) |
| 1358 | return NULL; |
| 1359 | |
| 1360 | /* |
| 1361 | * Second pass: emit code. |
| 1362 | */ |
| 1363 | regcomp_start(expr, re_flags); |
| 1364 | regcode = r->program; |
| 1365 | regc(REGMAGIC); |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 1366 | if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1367 | { |
| 1368 | vim_free(r); |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 1369 | if (reg_toolong) |
| 1370 | EMSG_RET_NULL(_("E339: Pattern too long")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1371 | return NULL; |
| 1372 | } |
| 1373 | |
| 1374 | /* Dig out information for optimizations. */ |
| 1375 | r->regstart = NUL; /* Worst-case defaults. */ |
| 1376 | r->reganch = 0; |
| 1377 | r->regmust = NULL; |
| 1378 | r->regmlen = 0; |
| 1379 | r->regflags = regflags; |
| 1380 | if (flags & HASNL) |
| 1381 | r->regflags |= RF_HASNL; |
| 1382 | if (flags & HASLOOKBH) |
| 1383 | r->regflags |= RF_LOOKBH; |
| 1384 | #ifdef FEAT_SYN_HL |
| 1385 | /* Remember whether this pattern has any \z specials in it. */ |
| 1386 | r->reghasz = re_has_z; |
| 1387 | #endif |
| 1388 | scan = r->program + 1; /* First BRANCH. */ |
| 1389 | if (OP(regnext(scan)) == END) /* Only one top-level choice. */ |
| 1390 | { |
| 1391 | scan = OPERAND(scan); |
| 1392 | |
| 1393 | /* Starting-point info. */ |
| 1394 | if (OP(scan) == BOL || OP(scan) == RE_BOF) |
| 1395 | { |
| 1396 | r->reganch++; |
| 1397 | scan = regnext(scan); |
| 1398 | } |
| 1399 | |
| 1400 | if (OP(scan) == EXACTLY) |
| 1401 | { |
| 1402 | #ifdef FEAT_MBYTE |
| 1403 | if (has_mbyte) |
| 1404 | r->regstart = (*mb_ptr2char)(OPERAND(scan)); |
| 1405 | else |
| 1406 | #endif |
| 1407 | r->regstart = *OPERAND(scan); |
| 1408 | } |
| 1409 | else if ((OP(scan) == BOW |
| 1410 | || OP(scan) == EOW |
| 1411 | || OP(scan) == NOTHING |
| 1412 | || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN |
| 1413 | || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE) |
| 1414 | && OP(regnext(scan)) == EXACTLY) |
| 1415 | { |
| 1416 | #ifdef FEAT_MBYTE |
| 1417 | if (has_mbyte) |
| 1418 | r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan))); |
| 1419 | else |
| 1420 | #endif |
| 1421 | r->regstart = *OPERAND(regnext(scan)); |
| 1422 | } |
| 1423 | |
| 1424 | /* |
| 1425 | * If there's something expensive in the r.e., find the longest |
| 1426 | * literal string that must appear and make it the regmust. Resolve |
| 1427 | * ties in favor of later strings, since the regstart check works |
| 1428 | * with the beginning of the r.e. and avoiding duplication |
| 1429 | * strengthens checking. Not a strong reason, but sufficient in the |
| 1430 | * absence of others. |
| 1431 | */ |
| 1432 | /* |
| 1433 | * When the r.e. starts with BOW, it is faster to look for a regmust |
| 1434 | * first. Used a lot for "#" and "*" commands. (Added by mool). |
| 1435 | */ |
| 1436 | if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW) |
| 1437 | && !(flags & HASNL)) |
| 1438 | { |
| 1439 | longest = NULL; |
| 1440 | len = 0; |
| 1441 | for (; scan != NULL; scan = regnext(scan)) |
| 1442 | if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len) |
| 1443 | { |
| 1444 | longest = OPERAND(scan); |
| 1445 | len = (int)STRLEN(OPERAND(scan)); |
| 1446 | } |
| 1447 | r->regmust = longest; |
| 1448 | r->regmlen = len; |
| 1449 | } |
| 1450 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1451 | #ifdef BT_REGEXP_DUMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1452 | regdump(expr, r); |
| 1453 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1454 | r->engine = &bt_regengine; |
| 1455 | return (regprog_T *)r; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 1459 | * Free a compiled regexp program, returned by bt_regcomp(). |
| 1460 | */ |
| 1461 | static void |
| 1462 | bt_regfree(prog) |
| 1463 | regprog_T *prog; |
| 1464 | { |
| 1465 | vim_free(prog); |
| 1466 | } |
| 1467 | |
| 1468 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1469 | * Setup to parse the regexp. Used once to get the length and once to do it. |
| 1470 | */ |
| 1471 | static void |
| 1472 | regcomp_start(expr, re_flags) |
| 1473 | char_u *expr; |
| 1474 | int re_flags; /* see vim_regcomp() */ |
| 1475 | { |
| 1476 | initchr(expr); |
| 1477 | if (re_flags & RE_MAGIC) |
| 1478 | reg_magic = MAGIC_ON; |
| 1479 | else |
| 1480 | reg_magic = MAGIC_OFF; |
| 1481 | reg_string = (re_flags & RE_STRING); |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1482 | reg_strict = (re_flags & RE_STRICT); |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 1483 | get_cpo_flags(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1484 | |
| 1485 | num_complex_braces = 0; |
| 1486 | regnpar = 1; |
| 1487 | vim_memset(had_endbrace, 0, sizeof(had_endbrace)); |
| 1488 | #ifdef FEAT_SYN_HL |
| 1489 | regnzpar = 1; |
| 1490 | re_has_z = 0; |
| 1491 | #endif |
| 1492 | regsize = 0L; |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 1493 | reg_toolong = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1494 | regflags = 0; |
| 1495 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1496 | had_eol = FALSE; |
| 1497 | #endif |
| 1498 | } |
| 1499 | |
| 1500 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1501 | /* |
| 1502 | * Check if during the previous call to vim_regcomp the EOL item "$" has been |
| 1503 | * found. This is messy, but it works fine. |
| 1504 | */ |
| 1505 | int |
| 1506 | vim_regcomp_had_eol() |
| 1507 | { |
| 1508 | return had_eol; |
| 1509 | } |
| 1510 | #endif |
| 1511 | |
| 1512 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1513 | * Parse regular expression, i.e. main body or parenthesized thing. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1514 | * |
| 1515 | * Caller must absorb opening parenthesis. |
| 1516 | * |
| 1517 | * Combining parenthesis handling with the base level of regular expression |
| 1518 | * is a trifle forced, but the need to tie the tails of the branches to what |
| 1519 | * follows makes it hard to avoid. |
| 1520 | */ |
| 1521 | static char_u * |
| 1522 | reg(paren, flagp) |
| 1523 | int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ |
| 1524 | int *flagp; |
| 1525 | { |
| 1526 | char_u *ret; |
| 1527 | char_u *br; |
| 1528 | char_u *ender; |
| 1529 | int parno = 0; |
| 1530 | int flags; |
| 1531 | |
| 1532 | *flagp = HASWIDTH; /* Tentatively. */ |
| 1533 | |
| 1534 | #ifdef FEAT_SYN_HL |
| 1535 | if (paren == REG_ZPAREN) |
| 1536 | { |
| 1537 | /* Make a ZOPEN node. */ |
| 1538 | if (regnzpar >= NSUBEXP) |
| 1539 | EMSG_RET_NULL(_("E50: Too many \\z(")); |
| 1540 | parno = regnzpar; |
| 1541 | regnzpar++; |
| 1542 | ret = regnode(ZOPEN + parno); |
| 1543 | } |
| 1544 | else |
| 1545 | #endif |
| 1546 | if (paren == REG_PAREN) |
| 1547 | { |
| 1548 | /* Make a MOPEN node. */ |
| 1549 | if (regnpar >= NSUBEXP) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1550 | EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1551 | parno = regnpar; |
| 1552 | ++regnpar; |
| 1553 | ret = regnode(MOPEN + parno); |
| 1554 | } |
| 1555 | else if (paren == REG_NPAREN) |
| 1556 | { |
| 1557 | /* Make a NOPEN node. */ |
| 1558 | ret = regnode(NOPEN); |
| 1559 | } |
| 1560 | else |
| 1561 | ret = NULL; |
| 1562 | |
| 1563 | /* Pick up the branches, linking them together. */ |
| 1564 | br = regbranch(&flags); |
| 1565 | if (br == NULL) |
| 1566 | return NULL; |
| 1567 | if (ret != NULL) |
| 1568 | regtail(ret, br); /* [MZ]OPEN -> first. */ |
| 1569 | else |
| 1570 | ret = br; |
| 1571 | /* If one of the branches can be zero-width, the whole thing can. |
| 1572 | * If one of the branches has * at start or matches a line-break, the |
| 1573 | * whole thing can. */ |
| 1574 | if (!(flags & HASWIDTH)) |
| 1575 | *flagp &= ~HASWIDTH; |
| 1576 | *flagp |= flags & (SPSTART | HASNL | HASLOOKBH); |
| 1577 | while (peekchr() == Magic('|')) |
| 1578 | { |
| 1579 | skipchr(); |
| 1580 | br = regbranch(&flags); |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 1581 | if (br == NULL || reg_toolong) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1582 | return NULL; |
| 1583 | regtail(ret, br); /* BRANCH -> BRANCH. */ |
| 1584 | if (!(flags & HASWIDTH)) |
| 1585 | *flagp &= ~HASWIDTH; |
| 1586 | *flagp |= flags & (SPSTART | HASNL | HASLOOKBH); |
| 1587 | } |
| 1588 | |
| 1589 | /* Make a closing node, and hook it on the end. */ |
| 1590 | ender = regnode( |
| 1591 | #ifdef FEAT_SYN_HL |
| 1592 | paren == REG_ZPAREN ? ZCLOSE + parno : |
| 1593 | #endif |
| 1594 | paren == REG_PAREN ? MCLOSE + parno : |
| 1595 | paren == REG_NPAREN ? NCLOSE : END); |
| 1596 | regtail(ret, ender); |
| 1597 | |
| 1598 | /* Hook the tails of the branches to the closing node. */ |
| 1599 | for (br = ret; br != NULL; br = regnext(br)) |
| 1600 | regoptail(br, ender); |
| 1601 | |
| 1602 | /* Check for proper termination. */ |
| 1603 | if (paren != REG_NOPAREN && getchr() != Magic(')')) |
| 1604 | { |
| 1605 | #ifdef FEAT_SYN_HL |
| 1606 | if (paren == REG_ZPAREN) |
Bram Moolenaar | 45eeb13 | 2005-06-06 21:59:07 +0000 | [diff] [blame] | 1607 | EMSG_RET_NULL(_("E52: Unmatched \\z(")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1608 | else |
| 1609 | #endif |
| 1610 | if (paren == REG_NPAREN) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1611 | EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1612 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1613 | EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1614 | } |
| 1615 | else if (paren == REG_NOPAREN && peekchr() != NUL) |
| 1616 | { |
| 1617 | if (curchr == Magic(')')) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1618 | EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1619 | else |
Bram Moolenaar | 45eeb13 | 2005-06-06 21:59:07 +0000 | [diff] [blame] | 1620 | EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1621 | /* NOTREACHED */ |
| 1622 | } |
| 1623 | /* |
| 1624 | * Here we set the flag allowing back references to this set of |
| 1625 | * parentheses. |
| 1626 | */ |
| 1627 | if (paren == REG_PAREN) |
| 1628 | had_endbrace[parno] = TRUE; /* have seen the close paren */ |
| 1629 | return ret; |
| 1630 | } |
| 1631 | |
| 1632 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1633 | * Parse one alternative of an | operator. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1634 | * Implements the & operator. |
| 1635 | */ |
| 1636 | static char_u * |
| 1637 | regbranch(flagp) |
| 1638 | int *flagp; |
| 1639 | { |
| 1640 | char_u *ret; |
| 1641 | char_u *chain = NULL; |
| 1642 | char_u *latest; |
| 1643 | int flags; |
| 1644 | |
| 1645 | *flagp = WORST | HASNL; /* Tentatively. */ |
| 1646 | |
| 1647 | ret = regnode(BRANCH); |
| 1648 | for (;;) |
| 1649 | { |
| 1650 | latest = regconcat(&flags); |
| 1651 | if (latest == NULL) |
| 1652 | return NULL; |
| 1653 | /* If one of the branches has width, the whole thing has. If one of |
| 1654 | * the branches anchors at start-of-line, the whole thing does. |
| 1655 | * If one of the branches uses look-behind, the whole thing does. */ |
| 1656 | *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH); |
| 1657 | /* If one of the branches doesn't match a line-break, the whole thing |
| 1658 | * doesn't. */ |
| 1659 | *flagp &= ~HASNL | (flags & HASNL); |
| 1660 | if (chain != NULL) |
| 1661 | regtail(chain, latest); |
| 1662 | if (peekchr() != Magic('&')) |
| 1663 | break; |
| 1664 | skipchr(); |
| 1665 | regtail(latest, regnode(END)); /* operand ends */ |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 1666 | if (reg_toolong) |
| 1667 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1668 | reginsert(MATCH, latest); |
| 1669 | chain = latest; |
| 1670 | } |
| 1671 | |
| 1672 | return ret; |
| 1673 | } |
| 1674 | |
| 1675 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1676 | * Parse one alternative of an | or & operator. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1677 | * Implements the concatenation operator. |
| 1678 | */ |
| 1679 | static char_u * |
| 1680 | regconcat(flagp) |
| 1681 | int *flagp; |
| 1682 | { |
| 1683 | char_u *first = NULL; |
| 1684 | char_u *chain = NULL; |
| 1685 | char_u *latest; |
| 1686 | int flags; |
| 1687 | int cont = TRUE; |
| 1688 | |
| 1689 | *flagp = WORST; /* Tentatively. */ |
| 1690 | |
| 1691 | while (cont) |
| 1692 | { |
| 1693 | switch (peekchr()) |
| 1694 | { |
| 1695 | case NUL: |
| 1696 | case Magic('|'): |
| 1697 | case Magic('&'): |
| 1698 | case Magic(')'): |
| 1699 | cont = FALSE; |
| 1700 | break; |
| 1701 | case Magic('Z'): |
| 1702 | #ifdef FEAT_MBYTE |
| 1703 | regflags |= RF_ICOMBINE; |
| 1704 | #endif |
| 1705 | skipchr_keepstart(); |
| 1706 | break; |
| 1707 | case Magic('c'): |
| 1708 | regflags |= RF_ICASE; |
| 1709 | skipchr_keepstart(); |
| 1710 | break; |
| 1711 | case Magic('C'): |
| 1712 | regflags |= RF_NOICASE; |
| 1713 | skipchr_keepstart(); |
| 1714 | break; |
| 1715 | case Magic('v'): |
| 1716 | reg_magic = MAGIC_ALL; |
| 1717 | skipchr_keepstart(); |
| 1718 | curchr = -1; |
| 1719 | break; |
| 1720 | case Magic('m'): |
| 1721 | reg_magic = MAGIC_ON; |
| 1722 | skipchr_keepstart(); |
| 1723 | curchr = -1; |
| 1724 | break; |
| 1725 | case Magic('M'): |
| 1726 | reg_magic = MAGIC_OFF; |
| 1727 | skipchr_keepstart(); |
| 1728 | curchr = -1; |
| 1729 | break; |
| 1730 | case Magic('V'): |
| 1731 | reg_magic = MAGIC_NONE; |
| 1732 | skipchr_keepstart(); |
| 1733 | curchr = -1; |
| 1734 | break; |
| 1735 | default: |
| 1736 | latest = regpiece(&flags); |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 1737 | if (latest == NULL || reg_toolong) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1738 | return NULL; |
| 1739 | *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH); |
| 1740 | if (chain == NULL) /* First piece. */ |
| 1741 | *flagp |= flags & SPSTART; |
| 1742 | else |
| 1743 | regtail(chain, latest); |
| 1744 | chain = latest; |
| 1745 | if (first == NULL) |
| 1746 | first = latest; |
| 1747 | break; |
| 1748 | } |
| 1749 | } |
| 1750 | if (first == NULL) /* Loop ran zero times. */ |
| 1751 | first = regnode(NOTHING); |
| 1752 | return first; |
| 1753 | } |
| 1754 | |
| 1755 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1756 | * Parse something followed by possible [*+=]. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1757 | * |
| 1758 | * Note that the branching code sequences used for = and the general cases |
| 1759 | * of * and + are somewhat optimized: they use the same NOTHING node as |
| 1760 | * both the endmarker for their branch list and the body of the last branch. |
| 1761 | * It might seem that this node could be dispensed with entirely, but the |
| 1762 | * endmarker role is not redundant. |
| 1763 | */ |
| 1764 | static char_u * |
| 1765 | regpiece(flagp) |
| 1766 | int *flagp; |
| 1767 | { |
| 1768 | char_u *ret; |
| 1769 | int op; |
| 1770 | char_u *next; |
| 1771 | int flags; |
| 1772 | long minval; |
| 1773 | long maxval; |
| 1774 | |
| 1775 | ret = regatom(&flags); |
| 1776 | if (ret == NULL) |
| 1777 | return NULL; |
| 1778 | |
| 1779 | op = peekchr(); |
| 1780 | if (re_multi_type(op) == NOT_MULTI) |
| 1781 | { |
| 1782 | *flagp = flags; |
| 1783 | return ret; |
| 1784 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1785 | /* default flags */ |
| 1786 | *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH))); |
| 1787 | |
| 1788 | skipchr(); |
| 1789 | switch (op) |
| 1790 | { |
| 1791 | case Magic('*'): |
| 1792 | if (flags & SIMPLE) |
| 1793 | reginsert(STAR, ret); |
| 1794 | else |
| 1795 | { |
| 1796 | /* Emit x* as (x&|), where & means "self". */ |
| 1797 | reginsert(BRANCH, ret); /* Either x */ |
| 1798 | regoptail(ret, regnode(BACK)); /* and loop */ |
| 1799 | regoptail(ret, ret); /* back */ |
| 1800 | regtail(ret, regnode(BRANCH)); /* or */ |
| 1801 | regtail(ret, regnode(NOTHING)); /* null. */ |
| 1802 | } |
| 1803 | break; |
| 1804 | |
| 1805 | case Magic('+'): |
| 1806 | if (flags & SIMPLE) |
| 1807 | reginsert(PLUS, ret); |
| 1808 | else |
| 1809 | { |
| 1810 | /* Emit x+ as x(&|), where & means "self". */ |
| 1811 | next = regnode(BRANCH); /* Either */ |
| 1812 | regtail(ret, next); |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 1813 | regtail(regnode(BACK), ret); /* loop back */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1814 | regtail(next, regnode(BRANCH)); /* or */ |
| 1815 | regtail(ret, regnode(NOTHING)); /* null. */ |
| 1816 | } |
| 1817 | *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH))); |
| 1818 | break; |
| 1819 | |
| 1820 | case Magic('@'): |
| 1821 | { |
| 1822 | int lop = END; |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 1823 | int nr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 1825 | nr = getdecchrs(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1826 | switch (no_Magic(getchr())) |
| 1827 | { |
| 1828 | case '=': lop = MATCH; break; /* \@= */ |
| 1829 | case '!': lop = NOMATCH; break; /* \@! */ |
| 1830 | case '>': lop = SUBPAT; break; /* \@> */ |
| 1831 | case '<': switch (no_Magic(getchr())) |
| 1832 | { |
| 1833 | case '=': lop = BEHIND; break; /* \@<= */ |
| 1834 | case '!': lop = NOBEHIND; break; /* \@<! */ |
| 1835 | } |
| 1836 | } |
| 1837 | if (lop == END) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1838 | EMSG2_RET_NULL(_("E59: invalid character after %s@"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1839 | reg_magic == MAGIC_ALL); |
| 1840 | /* Look behind must match with behind_pos. */ |
| 1841 | if (lop == BEHIND || lop == NOBEHIND) |
| 1842 | { |
| 1843 | regtail(ret, regnode(BHPOS)); |
| 1844 | *flagp |= HASLOOKBH; |
| 1845 | } |
| 1846 | regtail(ret, regnode(END)); /* operand ends */ |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 1847 | if (lop == BEHIND || lop == NOBEHIND) |
| 1848 | { |
| 1849 | if (nr < 0) |
| 1850 | nr = 0; /* no limit is same as zero limit */ |
| 1851 | reginsert_nr(lop, nr, ret); |
| 1852 | } |
| 1853 | else |
| 1854 | reginsert(lop, ret); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1855 | break; |
| 1856 | } |
| 1857 | |
| 1858 | case Magic('?'): |
| 1859 | case Magic('='): |
| 1860 | /* Emit x= as (x|) */ |
| 1861 | reginsert(BRANCH, ret); /* Either x */ |
| 1862 | regtail(ret, regnode(BRANCH)); /* or */ |
| 1863 | next = regnode(NOTHING); /* null. */ |
| 1864 | regtail(ret, next); |
| 1865 | regoptail(ret, next); |
| 1866 | break; |
| 1867 | |
| 1868 | case Magic('{'): |
| 1869 | if (!read_limits(&minval, &maxval)) |
| 1870 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1871 | if (flags & SIMPLE) |
| 1872 | { |
| 1873 | reginsert(BRACE_SIMPLE, ret); |
| 1874 | reginsert_limits(BRACE_LIMITS, minval, maxval, ret); |
| 1875 | } |
| 1876 | else |
| 1877 | { |
| 1878 | if (num_complex_braces >= 10) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1879 | EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1880 | reg_magic == MAGIC_ALL); |
| 1881 | reginsert(BRACE_COMPLEX + num_complex_braces, ret); |
| 1882 | regoptail(ret, regnode(BACK)); |
| 1883 | regoptail(ret, ret); |
| 1884 | reginsert_limits(BRACE_LIMITS, minval, maxval, ret); |
| 1885 | ++num_complex_braces; |
| 1886 | } |
| 1887 | if (minval > 0 && maxval > 0) |
| 1888 | *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH))); |
| 1889 | break; |
| 1890 | } |
| 1891 | if (re_multi_type(peekchr()) != NOT_MULTI) |
| 1892 | { |
| 1893 | /* Can't have a multi follow a multi. */ |
| 1894 | if (peekchr() == Magic('*')) |
| 1895 | sprintf((char *)IObuff, _("E61: Nested %s*"), |
| 1896 | reg_magic >= MAGIC_ON ? "" : "\\"); |
| 1897 | else |
| 1898 | sprintf((char *)IObuff, _("E62: Nested %s%c"), |
| 1899 | reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr())); |
| 1900 | EMSG_RET_NULL(IObuff); |
| 1901 | } |
| 1902 | |
| 1903 | return ret; |
| 1904 | } |
| 1905 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1906 | /* When making changes to classchars also change nfa_classcodes. */ |
| 1907 | static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU"; |
| 1908 | static int classcodes[] = { |
| 1909 | ANY, IDENT, SIDENT, KWORD, SKWORD, |
| 1910 | FNAME, SFNAME, PRINT, SPRINT, |
| 1911 | WHITE, NWHITE, DIGIT, NDIGIT, |
| 1912 | HEX, NHEX, OCTAL, NOCTAL, |
| 1913 | WORD, NWORD, HEAD, NHEAD, |
| 1914 | ALPHA, NALPHA, LOWER, NLOWER, |
| 1915 | UPPER, NUPPER |
| 1916 | }; |
| 1917 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1918 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 1919 | * Parse the lowest level. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1920 | * |
| 1921 | * Optimization: gobbles an entire sequence of ordinary characters so that |
| 1922 | * it can turn them into a single node, which is smaller to store and |
| 1923 | * faster to run. Don't do this when one_exactly is set. |
| 1924 | */ |
| 1925 | static char_u * |
| 1926 | regatom(flagp) |
| 1927 | int *flagp; |
| 1928 | { |
| 1929 | char_u *ret; |
| 1930 | int flags; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1931 | int c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | char_u *p; |
| 1933 | int extra = 0; |
| 1934 | |
| 1935 | *flagp = WORST; /* Tentatively. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1936 | |
| 1937 | c = getchr(); |
| 1938 | switch (c) |
| 1939 | { |
| 1940 | case Magic('^'): |
| 1941 | ret = regnode(BOL); |
| 1942 | break; |
| 1943 | |
| 1944 | case Magic('$'): |
| 1945 | ret = regnode(EOL); |
| 1946 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1947 | had_eol = TRUE; |
| 1948 | #endif |
| 1949 | break; |
| 1950 | |
| 1951 | case Magic('<'): |
| 1952 | ret = regnode(BOW); |
| 1953 | break; |
| 1954 | |
| 1955 | case Magic('>'): |
| 1956 | ret = regnode(EOW); |
| 1957 | break; |
| 1958 | |
| 1959 | case Magic('_'): |
| 1960 | c = no_Magic(getchr()); |
| 1961 | if (c == '^') /* "\_^" is start-of-line */ |
| 1962 | { |
| 1963 | ret = regnode(BOL); |
| 1964 | break; |
| 1965 | } |
| 1966 | if (c == '$') /* "\_$" is end-of-line */ |
| 1967 | { |
| 1968 | ret = regnode(EOL); |
| 1969 | #if defined(FEAT_SYN_HL) || defined(PROTO) |
| 1970 | had_eol = TRUE; |
| 1971 | #endif |
| 1972 | break; |
| 1973 | } |
| 1974 | |
| 1975 | extra = ADD_NL; |
| 1976 | *flagp |= HASNL; |
| 1977 | |
| 1978 | /* "\_[" is character range plus newline */ |
| 1979 | if (c == '[') |
| 1980 | goto collection; |
| 1981 | |
| 1982 | /* "\_x" is character class plus newline */ |
| 1983 | /*FALLTHROUGH*/ |
| 1984 | |
| 1985 | /* |
| 1986 | * Character classes. |
| 1987 | */ |
| 1988 | case Magic('.'): |
| 1989 | case Magic('i'): |
| 1990 | case Magic('I'): |
| 1991 | case Magic('k'): |
| 1992 | case Magic('K'): |
| 1993 | case Magic('f'): |
| 1994 | case Magic('F'): |
| 1995 | case Magic('p'): |
| 1996 | case Magic('P'): |
| 1997 | case Magic('s'): |
| 1998 | case Magic('S'): |
| 1999 | case Magic('d'): |
| 2000 | case Magic('D'): |
| 2001 | case Magic('x'): |
| 2002 | case Magic('X'): |
| 2003 | case Magic('o'): |
| 2004 | case Magic('O'): |
| 2005 | case Magic('w'): |
| 2006 | case Magic('W'): |
| 2007 | case Magic('h'): |
| 2008 | case Magic('H'): |
| 2009 | case Magic('a'): |
| 2010 | case Magic('A'): |
| 2011 | case Magic('l'): |
| 2012 | case Magic('L'): |
| 2013 | case Magic('u'): |
| 2014 | case Magic('U'): |
| 2015 | p = vim_strchr(classchars, no_Magic(c)); |
| 2016 | if (p == NULL) |
| 2017 | EMSG_RET_NULL(_("E63: invalid use of \\_")); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2018 | #ifdef FEAT_MBYTE |
| 2019 | /* When '.' is followed by a composing char ignore the dot, so that |
| 2020 | * the composing char is matched here. */ |
| 2021 | if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
| 2022 | { |
| 2023 | c = getchr(); |
| 2024 | goto do_multibyte; |
| 2025 | } |
| 2026 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2027 | ret = regnode(classcodes[p - classchars] + extra); |
| 2028 | *flagp |= HASWIDTH | SIMPLE; |
| 2029 | break; |
| 2030 | |
| 2031 | case Magic('n'): |
| 2032 | if (reg_string) |
| 2033 | { |
| 2034 | /* In a string "\n" matches a newline character. */ |
| 2035 | ret = regnode(EXACTLY); |
| 2036 | regc(NL); |
| 2037 | regc(NUL); |
| 2038 | *flagp |= HASWIDTH | SIMPLE; |
| 2039 | } |
| 2040 | else |
| 2041 | { |
| 2042 | /* In buffer text "\n" matches the end of a line. */ |
| 2043 | ret = regnode(NEWL); |
| 2044 | *flagp |= HASWIDTH | HASNL; |
| 2045 | } |
| 2046 | break; |
| 2047 | |
| 2048 | case Magic('('): |
| 2049 | if (one_exactly) |
| 2050 | EMSG_ONE_RET_NULL; |
| 2051 | ret = reg(REG_PAREN, &flags); |
| 2052 | if (ret == NULL) |
| 2053 | return NULL; |
| 2054 | *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH); |
| 2055 | break; |
| 2056 | |
| 2057 | case NUL: |
| 2058 | case Magic('|'): |
| 2059 | case Magic('&'): |
| 2060 | case Magic(')'): |
Bram Moolenaar | d421077 | 2008-01-02 14:35:30 +0000 | [diff] [blame] | 2061 | if (one_exactly) |
| 2062 | EMSG_ONE_RET_NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */ |
| 2064 | /* NOTREACHED */ |
| 2065 | |
| 2066 | case Magic('='): |
| 2067 | case Magic('?'): |
| 2068 | case Magic('+'): |
| 2069 | case Magic('@'): |
| 2070 | case Magic('{'): |
| 2071 | case Magic('*'): |
| 2072 | c = no_Magic(c); |
| 2073 | sprintf((char *)IObuff, _("E64: %s%c follows nothing"), |
| 2074 | (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL) |
| 2075 | ? "" : "\\", c); |
| 2076 | EMSG_RET_NULL(IObuff); |
| 2077 | /* NOTREACHED */ |
| 2078 | |
| 2079 | case Magic('~'): /* previous substitute pattern */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2080 | if (reg_prev_sub != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2081 | { |
| 2082 | char_u *lp; |
| 2083 | |
| 2084 | ret = regnode(EXACTLY); |
| 2085 | lp = reg_prev_sub; |
| 2086 | while (*lp != NUL) |
| 2087 | regc(*lp++); |
| 2088 | regc(NUL); |
| 2089 | if (*reg_prev_sub != NUL) |
| 2090 | { |
| 2091 | *flagp |= HASWIDTH; |
| 2092 | if ((lp - reg_prev_sub) == 1) |
| 2093 | *flagp |= SIMPLE; |
| 2094 | } |
| 2095 | } |
| 2096 | else |
| 2097 | EMSG_RET_NULL(_(e_nopresub)); |
| 2098 | break; |
| 2099 | |
| 2100 | case Magic('1'): |
| 2101 | case Magic('2'): |
| 2102 | case Magic('3'): |
| 2103 | case Magic('4'): |
| 2104 | case Magic('5'): |
| 2105 | case Magic('6'): |
| 2106 | case Magic('7'): |
| 2107 | case Magic('8'): |
| 2108 | case Magic('9'): |
| 2109 | { |
| 2110 | int refnum; |
| 2111 | |
| 2112 | refnum = c - Magic('0'); |
| 2113 | /* |
| 2114 | * Check if the back reference is legal. We must have seen the |
| 2115 | * close brace. |
| 2116 | * TODO: Should also check that we don't refer to something |
| 2117 | * that is repeated (+*=): what instance of the repetition |
| 2118 | * should we match? |
| 2119 | */ |
| 2120 | if (!had_endbrace[refnum]) |
| 2121 | { |
| 2122 | /* Trick: check if "@<=" or "@<!" follows, in which case |
| 2123 | * the \1 can appear before the referenced match. */ |
| 2124 | for (p = regparse; *p != NUL; ++p) |
| 2125 | if (p[0] == '@' && p[1] == '<' |
| 2126 | && (p[2] == '!' || p[2] == '=')) |
| 2127 | break; |
| 2128 | if (*p == NUL) |
| 2129 | EMSG_RET_NULL(_("E65: Illegal back reference")); |
| 2130 | } |
| 2131 | ret = regnode(BACKREF + refnum); |
| 2132 | } |
| 2133 | break; |
| 2134 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2135 | case Magic('z'): |
| 2136 | { |
| 2137 | c = no_Magic(getchr()); |
| 2138 | switch (c) |
| 2139 | { |
Bram Moolenaar | c4956c8 | 2006-03-12 21:58:43 +0000 | [diff] [blame] | 2140 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2141 | case '(': if (reg_do_extmatch != REX_SET) |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 2142 | EMSG_RET_NULL(_(e_z_not_allowed)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2143 | if (one_exactly) |
| 2144 | EMSG_ONE_RET_NULL; |
| 2145 | ret = reg(REG_ZPAREN, &flags); |
| 2146 | if (ret == NULL) |
| 2147 | return NULL; |
| 2148 | *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH); |
| 2149 | re_has_z = REX_SET; |
| 2150 | break; |
| 2151 | |
| 2152 | case '1': |
| 2153 | case '2': |
| 2154 | case '3': |
| 2155 | case '4': |
| 2156 | case '5': |
| 2157 | case '6': |
| 2158 | case '7': |
| 2159 | case '8': |
| 2160 | case '9': if (reg_do_extmatch != REX_USE) |
Bram Moolenaar | 5de820b | 2013-06-02 15:01:57 +0200 | [diff] [blame] | 2161 | EMSG_RET_NULL(_(e_z1_not_allowed)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2162 | ret = regnode(ZREF + c - '0'); |
| 2163 | re_has_z = REX_USE; |
| 2164 | break; |
Bram Moolenaar | c4956c8 | 2006-03-12 21:58:43 +0000 | [diff] [blame] | 2165 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2166 | |
| 2167 | case 's': ret = regnode(MOPEN + 0); |
| 2168 | break; |
| 2169 | |
| 2170 | case 'e': ret = regnode(MCLOSE + 0); |
| 2171 | break; |
| 2172 | |
| 2173 | default: EMSG_RET_NULL(_("E68: Invalid character after \\z")); |
| 2174 | } |
| 2175 | } |
| 2176 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2177 | |
| 2178 | case Magic('%'): |
| 2179 | { |
| 2180 | c = no_Magic(getchr()); |
| 2181 | switch (c) |
| 2182 | { |
| 2183 | /* () without a back reference */ |
| 2184 | case '(': |
| 2185 | if (one_exactly) |
| 2186 | EMSG_ONE_RET_NULL; |
| 2187 | ret = reg(REG_NPAREN, &flags); |
| 2188 | if (ret == NULL) |
| 2189 | return NULL; |
| 2190 | *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH); |
| 2191 | break; |
| 2192 | |
| 2193 | /* Catch \%^ and \%$ regardless of where they appear in the |
| 2194 | * pattern -- regardless of whether or not it makes sense. */ |
| 2195 | case '^': |
| 2196 | ret = regnode(RE_BOF); |
| 2197 | break; |
| 2198 | |
| 2199 | case '$': |
| 2200 | ret = regnode(RE_EOF); |
| 2201 | break; |
| 2202 | |
| 2203 | case '#': |
| 2204 | ret = regnode(CURSOR); |
| 2205 | break; |
| 2206 | |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 2207 | case 'V': |
| 2208 | ret = regnode(RE_VISUAL); |
| 2209 | break; |
| 2210 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2211 | /* \%[abc]: Emit as a list of branches, all ending at the last |
| 2212 | * branch which matches nothing. */ |
| 2213 | case '[': |
| 2214 | if (one_exactly) /* doesn't nest */ |
| 2215 | EMSG_ONE_RET_NULL; |
| 2216 | { |
| 2217 | char_u *lastbranch; |
| 2218 | char_u *lastnode = NULL; |
| 2219 | char_u *br; |
| 2220 | |
| 2221 | ret = NULL; |
| 2222 | while ((c = getchr()) != ']') |
| 2223 | { |
| 2224 | if (c == NUL) |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 2225 | EMSG2_RET_NULL(_(e_missing_sb), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2226 | reg_magic == MAGIC_ALL); |
| 2227 | br = regnode(BRANCH); |
| 2228 | if (ret == NULL) |
| 2229 | ret = br; |
| 2230 | else |
| 2231 | regtail(lastnode, br); |
| 2232 | |
| 2233 | ungetchr(); |
| 2234 | one_exactly = TRUE; |
| 2235 | lastnode = regatom(flagp); |
| 2236 | one_exactly = FALSE; |
| 2237 | if (lastnode == NULL) |
| 2238 | return NULL; |
| 2239 | } |
| 2240 | if (ret == NULL) |
Bram Moolenaar | 2976c02 | 2013-06-05 21:30:37 +0200 | [diff] [blame] | 2241 | EMSG2_RET_NULL(_(e_empty_sb), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2242 | reg_magic == MAGIC_ALL); |
| 2243 | lastbranch = regnode(BRANCH); |
| 2244 | br = regnode(NOTHING); |
| 2245 | if (ret != JUST_CALC_SIZE) |
| 2246 | { |
| 2247 | regtail(lastnode, br); |
| 2248 | regtail(lastbranch, br); |
| 2249 | /* connect all branches to the NOTHING |
| 2250 | * branch at the end */ |
| 2251 | for (br = ret; br != lastnode; ) |
| 2252 | { |
| 2253 | if (OP(br) == BRANCH) |
| 2254 | { |
| 2255 | regtail(br, lastbranch); |
| 2256 | br = OPERAND(br); |
| 2257 | } |
| 2258 | else |
| 2259 | br = regnext(br); |
| 2260 | } |
| 2261 | } |
Bram Moolenaar | a6404a4 | 2008-08-08 11:45:39 +0000 | [diff] [blame] | 2262 | *flagp &= ~(HASWIDTH | SIMPLE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2263 | break; |
| 2264 | } |
| 2265 | |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 2266 | case 'd': /* %d123 decimal */ |
| 2267 | case 'o': /* %o123 octal */ |
| 2268 | case 'x': /* %xab hex 2 */ |
| 2269 | case 'u': /* %uabcd hex 4 */ |
| 2270 | case 'U': /* %U1234abcd hex 8 */ |
| 2271 | { |
| 2272 | int i; |
| 2273 | |
| 2274 | switch (c) |
| 2275 | { |
| 2276 | case 'd': i = getdecchrs(); break; |
| 2277 | case 'o': i = getoctchrs(); break; |
| 2278 | case 'x': i = gethexchrs(2); break; |
| 2279 | case 'u': i = gethexchrs(4); break; |
| 2280 | case 'U': i = gethexchrs(8); break; |
| 2281 | default: i = -1; break; |
| 2282 | } |
| 2283 | |
| 2284 | if (i < 0) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2285 | EMSG2_RET_NULL( |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 2286 | _("E678: Invalid character after %s%%[dxouU]"), |
| 2287 | reg_magic == MAGIC_ALL); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2288 | #ifdef FEAT_MBYTE |
| 2289 | if (use_multibytecode(i)) |
| 2290 | ret = regnode(MULTIBYTECODE); |
| 2291 | else |
| 2292 | #endif |
| 2293 | ret = regnode(EXACTLY); |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 2294 | if (i == 0) |
| 2295 | regc(0x0a); |
| 2296 | else |
| 2297 | #ifdef FEAT_MBYTE |
| 2298 | regmbc(i); |
| 2299 | #else |
| 2300 | regc(i); |
| 2301 | #endif |
| 2302 | regc(NUL); |
| 2303 | *flagp |= HASWIDTH; |
| 2304 | break; |
| 2305 | } |
| 2306 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2307 | default: |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 2308 | if (VIM_ISDIGIT(c) || c == '<' || c == '>' |
| 2309 | || c == '\'') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2310 | { |
| 2311 | long_u n = 0; |
| 2312 | int cmp; |
| 2313 | |
| 2314 | cmp = c; |
| 2315 | if (cmp == '<' || cmp == '>') |
| 2316 | c = getchr(); |
| 2317 | while (VIM_ISDIGIT(c)) |
| 2318 | { |
| 2319 | n = n * 10 + (c - '0'); |
| 2320 | c = getchr(); |
| 2321 | } |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 2322 | if (c == '\'' && n == 0) |
| 2323 | { |
| 2324 | /* "\%'m", "\%<'m" and "\%>'m": Mark */ |
| 2325 | c = getchr(); |
| 2326 | ret = regnode(RE_MARK); |
| 2327 | if (ret == JUST_CALC_SIZE) |
| 2328 | regsize += 2; |
| 2329 | else |
| 2330 | { |
| 2331 | *regcode++ = c; |
| 2332 | *regcode++ = cmp; |
| 2333 | } |
| 2334 | break; |
| 2335 | } |
| 2336 | else if (c == 'l' || c == 'c' || c == 'v') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | { |
| 2338 | if (c == 'l') |
| 2339 | ret = regnode(RE_LNUM); |
| 2340 | else if (c == 'c') |
| 2341 | ret = regnode(RE_COL); |
| 2342 | else |
| 2343 | ret = regnode(RE_VCOL); |
| 2344 | if (ret == JUST_CALC_SIZE) |
| 2345 | regsize += 5; |
| 2346 | else |
| 2347 | { |
| 2348 | /* put the number and the optional |
| 2349 | * comparator after the opcode */ |
| 2350 | regcode = re_put_long(regcode, n); |
| 2351 | *regcode++ = cmp; |
| 2352 | } |
| 2353 | break; |
| 2354 | } |
| 2355 | } |
| 2356 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2357 | EMSG2_RET_NULL(_("E71: Invalid character after %s%%"), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2358 | reg_magic == MAGIC_ALL); |
| 2359 | } |
| 2360 | } |
| 2361 | break; |
| 2362 | |
| 2363 | case Magic('['): |
| 2364 | collection: |
| 2365 | { |
| 2366 | char_u *lp; |
| 2367 | |
| 2368 | /* |
| 2369 | * If there is no matching ']', we assume the '[' is a normal |
| 2370 | * character. This makes 'incsearch' and ":help [" work. |
| 2371 | */ |
| 2372 | lp = skip_anyof(regparse); |
| 2373 | if (*lp == ']') /* there is a matching ']' */ |
| 2374 | { |
| 2375 | int startc = -1; /* > 0 when next '-' is a range */ |
| 2376 | int endc; |
| 2377 | |
| 2378 | /* |
| 2379 | * In a character class, different parsing rules apply. |
| 2380 | * Not even \ is special anymore, nothing is. |
| 2381 | */ |
| 2382 | if (*regparse == '^') /* Complement of range. */ |
| 2383 | { |
| 2384 | ret = regnode(ANYBUT + extra); |
| 2385 | regparse++; |
| 2386 | } |
| 2387 | else |
| 2388 | ret = regnode(ANYOF + extra); |
| 2389 | |
| 2390 | /* At the start ']' and '-' mean the literal character. */ |
| 2391 | if (*regparse == ']' || *regparse == '-') |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2392 | { |
| 2393 | startc = *regparse; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2394 | regc(*regparse++); |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2395 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2396 | |
| 2397 | while (*regparse != NUL && *regparse != ']') |
| 2398 | { |
| 2399 | if (*regparse == '-') |
| 2400 | { |
| 2401 | ++regparse; |
| 2402 | /* The '-' is not used for a range at the end and |
| 2403 | * after or before a '\n'. */ |
| 2404 | if (*regparse == ']' || *regparse == NUL |
| 2405 | || startc == -1 |
| 2406 | || (regparse[0] == '\\' && regparse[1] == 'n')) |
| 2407 | { |
| 2408 | regc('-'); |
| 2409 | startc = '-'; /* [--x] is a range */ |
| 2410 | } |
| 2411 | else |
| 2412 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2413 | /* Also accept "a-[.z.]" */ |
| 2414 | endc = 0; |
| 2415 | if (*regparse == '[') |
| 2416 | endc = get_coll_element(®parse); |
| 2417 | if (endc == 0) |
| 2418 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2419 | #ifdef FEAT_MBYTE |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2420 | if (has_mbyte) |
| 2421 | endc = mb_ptr2char_adv(®parse); |
| 2422 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | #endif |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2424 | endc = *regparse++; |
| 2425 | } |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 2426 | |
| 2427 | /* Handle \o40, \x20 and \u20AC style sequences */ |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 2428 | if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl) |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 2429 | endc = coll_get_char(); |
| 2430 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | if (startc > endc) |
| 2432 | EMSG_RET_NULL(_(e_invrange)); |
| 2433 | #ifdef FEAT_MBYTE |
| 2434 | if (has_mbyte && ((*mb_char2len)(startc) > 1 |
| 2435 | || (*mb_char2len)(endc) > 1)) |
| 2436 | { |
| 2437 | /* Limit to a range of 256 chars */ |
| 2438 | if (endc > startc + 256) |
| 2439 | EMSG_RET_NULL(_(e_invrange)); |
| 2440 | while (++startc <= endc) |
| 2441 | regmbc(startc); |
| 2442 | } |
| 2443 | else |
| 2444 | #endif |
| 2445 | { |
| 2446 | #ifdef EBCDIC |
| 2447 | int alpha_only = FALSE; |
| 2448 | |
| 2449 | /* for alphabetical range skip the gaps |
| 2450 | * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ |
| 2451 | if (isalpha(startc) && isalpha(endc)) |
| 2452 | alpha_only = TRUE; |
| 2453 | #endif |
| 2454 | while (++startc <= endc) |
| 2455 | #ifdef EBCDIC |
| 2456 | if (!alpha_only || isalpha(startc)) |
| 2457 | #endif |
| 2458 | regc(startc); |
| 2459 | } |
| 2460 | startc = -1; |
| 2461 | } |
| 2462 | } |
| 2463 | /* |
| 2464 | * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
| 2465 | * accepts "\t", "\e", etc., but only when the 'l' flag in |
| 2466 | * 'cpoptions' is not included. |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2467 | * Posix doesn't recognize backslash at all. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2468 | */ |
| 2469 | else if (*regparse == '\\' |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 2470 | && !reg_cpo_bsl |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2471 | && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
Bram Moolenaar | 1cd3f2c | 2013-06-05 12:43:09 +0200 | [diff] [blame] | 2472 | || (!reg_cpo_lit |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2473 | && vim_strchr(REGEXP_ABBR, |
| 2474 | regparse[1]) != NULL))) |
| 2475 | { |
| 2476 | regparse++; |
| 2477 | if (*regparse == 'n') |
| 2478 | { |
| 2479 | /* '\n' in range: also match NL */ |
| 2480 | if (ret != JUST_CALC_SIZE) |
| 2481 | { |
Bram Moolenaar | e337e5f | 2013-01-30 18:21:51 +0100 | [diff] [blame] | 2482 | /* Using \n inside [^] does not change what |
| 2483 | * matches. "[^\n]" is the same as ".". */ |
| 2484 | if (*ret == ANYOF) |
| 2485 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | *ret = ANYOF + ADD_NL; |
Bram Moolenaar | e337e5f | 2013-01-30 18:21:51 +0100 | [diff] [blame] | 2487 | *flagp |= HASNL; |
| 2488 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2489 | /* else: must have had a \n already */ |
| 2490 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | regparse++; |
| 2492 | startc = -1; |
| 2493 | } |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 2494 | else if (*regparse == 'd' |
| 2495 | || *regparse == 'o' |
| 2496 | || *regparse == 'x' |
| 2497 | || *regparse == 'u' |
| 2498 | || *regparse == 'U') |
| 2499 | { |
| 2500 | startc = coll_get_char(); |
| 2501 | if (startc == 0) |
| 2502 | regc(0x0a); |
| 2503 | else |
| 2504 | #ifdef FEAT_MBYTE |
| 2505 | regmbc(startc); |
| 2506 | #else |
| 2507 | regc(startc); |
| 2508 | #endif |
| 2509 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2510 | else |
| 2511 | { |
| 2512 | startc = backslash_trans(*regparse++); |
| 2513 | regc(startc); |
| 2514 | } |
| 2515 | } |
| 2516 | else if (*regparse == '[') |
| 2517 | { |
| 2518 | int c_class; |
| 2519 | int cu; |
| 2520 | |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2521 | c_class = get_char_class(®parse); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2522 | startc = -1; |
| 2523 | /* Characters assumed to be 8 bits! */ |
| 2524 | switch (c_class) |
| 2525 | { |
| 2526 | case CLASS_NONE: |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 2527 | c_class = get_equi_class(®parse); |
| 2528 | if (c_class != 0) |
| 2529 | { |
| 2530 | /* produce equivalence class */ |
| 2531 | reg_equi_class(c_class); |
| 2532 | } |
| 2533 | else if ((c_class = |
| 2534 | get_coll_element(®parse)) != 0) |
| 2535 | { |
| 2536 | /* produce a collating element */ |
| 2537 | regmbc(c_class); |
| 2538 | } |
| 2539 | else |
| 2540 | { |
| 2541 | /* literal '[', allow [[-x] as a range */ |
| 2542 | startc = *regparse++; |
| 2543 | regc(startc); |
| 2544 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2545 | break; |
| 2546 | case CLASS_ALNUM: |
| 2547 | for (cu = 1; cu <= 255; cu++) |
| 2548 | if (isalnum(cu)) |
| 2549 | regc(cu); |
| 2550 | break; |
| 2551 | case CLASS_ALPHA: |
| 2552 | for (cu = 1; cu <= 255; cu++) |
| 2553 | if (isalpha(cu)) |
| 2554 | regc(cu); |
| 2555 | break; |
| 2556 | case CLASS_BLANK: |
| 2557 | regc(' '); |
| 2558 | regc('\t'); |
| 2559 | break; |
| 2560 | case CLASS_CNTRL: |
| 2561 | for (cu = 1; cu <= 255; cu++) |
| 2562 | if (iscntrl(cu)) |
| 2563 | regc(cu); |
| 2564 | break; |
| 2565 | case CLASS_DIGIT: |
| 2566 | for (cu = 1; cu <= 255; cu++) |
| 2567 | if (VIM_ISDIGIT(cu)) |
| 2568 | regc(cu); |
| 2569 | break; |
| 2570 | case CLASS_GRAPH: |
| 2571 | for (cu = 1; cu <= 255; cu++) |
| 2572 | if (isgraph(cu)) |
| 2573 | regc(cu); |
| 2574 | break; |
| 2575 | case CLASS_LOWER: |
| 2576 | for (cu = 1; cu <= 255; cu++) |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 2577 | if (MB_ISLOWER(cu)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2578 | regc(cu); |
| 2579 | break; |
| 2580 | case CLASS_PRINT: |
| 2581 | for (cu = 1; cu <= 255; cu++) |
| 2582 | if (vim_isprintc(cu)) |
| 2583 | regc(cu); |
| 2584 | break; |
| 2585 | case CLASS_PUNCT: |
| 2586 | for (cu = 1; cu <= 255; cu++) |
| 2587 | if (ispunct(cu)) |
| 2588 | regc(cu); |
| 2589 | break; |
| 2590 | case CLASS_SPACE: |
| 2591 | for (cu = 9; cu <= 13; cu++) |
| 2592 | regc(cu); |
| 2593 | regc(' '); |
| 2594 | break; |
| 2595 | case CLASS_UPPER: |
| 2596 | for (cu = 1; cu <= 255; cu++) |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 2597 | if (MB_ISUPPER(cu)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2598 | regc(cu); |
| 2599 | break; |
| 2600 | case CLASS_XDIGIT: |
| 2601 | for (cu = 1; cu <= 255; cu++) |
| 2602 | if (vim_isxdigit(cu)) |
| 2603 | regc(cu); |
| 2604 | break; |
| 2605 | case CLASS_TAB: |
| 2606 | regc('\t'); |
| 2607 | break; |
| 2608 | case CLASS_RETURN: |
| 2609 | regc('\r'); |
| 2610 | break; |
| 2611 | case CLASS_BACKSPACE: |
| 2612 | regc('\b'); |
| 2613 | break; |
| 2614 | case CLASS_ESCAPE: |
| 2615 | regc('\033'); |
| 2616 | break; |
| 2617 | } |
| 2618 | } |
| 2619 | else |
| 2620 | { |
| 2621 | #ifdef FEAT_MBYTE |
| 2622 | if (has_mbyte) |
| 2623 | { |
| 2624 | int len; |
| 2625 | |
| 2626 | /* produce a multibyte character, including any |
| 2627 | * following composing characters */ |
| 2628 | startc = mb_ptr2char(regparse); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2629 | len = (*mb_ptr2len)(regparse); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2630 | if (enc_utf8 && utf_char2len(startc) != len) |
| 2631 | startc = -1; /* composing chars */ |
| 2632 | while (--len >= 0) |
| 2633 | regc(*regparse++); |
| 2634 | } |
| 2635 | else |
| 2636 | #endif |
| 2637 | { |
| 2638 | startc = *regparse++; |
| 2639 | regc(startc); |
| 2640 | } |
| 2641 | } |
| 2642 | } |
| 2643 | regc(NUL); |
| 2644 | prevchr_len = 1; /* last char was the ']' */ |
| 2645 | if (*regparse != ']') |
| 2646 | EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */ |
| 2647 | skipchr(); /* let's be friends with the lexer again */ |
| 2648 | *flagp |= HASWIDTH | SIMPLE; |
| 2649 | break; |
| 2650 | } |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2651 | else if (reg_strict) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2652 | EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2653 | } |
| 2654 | /* FALLTHROUGH */ |
| 2655 | |
| 2656 | default: |
| 2657 | { |
| 2658 | int len; |
| 2659 | |
| 2660 | #ifdef FEAT_MBYTE |
| 2661 | /* A multi-byte character is handled as a separate atom if it's |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2662 | * before a multi and when it's a composing char. */ |
| 2663 | if (use_multibytecode(c)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2665 | do_multibyte: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2666 | ret = regnode(MULTIBYTECODE); |
| 2667 | regmbc(c); |
| 2668 | *flagp |= HASWIDTH | SIMPLE; |
| 2669 | break; |
| 2670 | } |
| 2671 | #endif |
| 2672 | |
| 2673 | ret = regnode(EXACTLY); |
| 2674 | |
| 2675 | /* |
| 2676 | * Append characters as long as: |
| 2677 | * - there is no following multi, we then need the character in |
| 2678 | * front of it as a single character operand |
| 2679 | * - not running into a Magic character |
| 2680 | * - "one_exactly" is not set |
| 2681 | * But always emit at least one character. Might be a Multi, |
| 2682 | * e.g., a "[" without matching "]". |
| 2683 | */ |
| 2684 | for (len = 0; c != NUL && (len == 0 |
| 2685 | || (re_multi_type(peekchr()) == NOT_MULTI |
| 2686 | && !one_exactly |
| 2687 | && !is_Magic(c))); ++len) |
| 2688 | { |
| 2689 | c = no_Magic(c); |
| 2690 | #ifdef FEAT_MBYTE |
| 2691 | if (has_mbyte) |
| 2692 | { |
| 2693 | regmbc(c); |
| 2694 | if (enc_utf8) |
| 2695 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2696 | int l; |
| 2697 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2698 | /* Need to get composing character too. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2699 | for (;;) |
| 2700 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2701 | l = utf_ptr2len(regparse); |
| 2702 | if (!UTF_COMPOSINGLIKE(regparse, regparse + l)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2703 | break; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2704 | regmbc(utf_ptr2char(regparse)); |
| 2705 | skipchr(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2706 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2707 | } |
| 2708 | } |
| 2709 | else |
| 2710 | #endif |
| 2711 | regc(c); |
| 2712 | c = getchr(); |
| 2713 | } |
| 2714 | ungetchr(); |
| 2715 | |
| 2716 | regc(NUL); |
| 2717 | *flagp |= HASWIDTH; |
| 2718 | if (len == 1) |
| 2719 | *flagp |= SIMPLE; |
| 2720 | } |
| 2721 | break; |
| 2722 | } |
| 2723 | |
| 2724 | return ret; |
| 2725 | } |
| 2726 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2727 | #ifdef FEAT_MBYTE |
| 2728 | /* |
| 2729 | * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for |
| 2730 | * character "c". |
| 2731 | */ |
| 2732 | static int |
| 2733 | use_multibytecode(c) |
| 2734 | int c; |
| 2735 | { |
| 2736 | return has_mbyte && (*mb_char2len)(c) > 1 |
| 2737 | && (re_multi_type(peekchr()) != NOT_MULTI |
| 2738 | || (enc_utf8 && utf_iscomposing(c))); |
| 2739 | } |
| 2740 | #endif |
| 2741 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2742 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2743 | * Emit a node. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2744 | * Return pointer to generated code. |
| 2745 | */ |
| 2746 | static char_u * |
| 2747 | regnode(op) |
| 2748 | int op; |
| 2749 | { |
| 2750 | char_u *ret; |
| 2751 | |
| 2752 | ret = regcode; |
| 2753 | if (ret == JUST_CALC_SIZE) |
| 2754 | regsize += 3; |
| 2755 | else |
| 2756 | { |
| 2757 | *regcode++ = op; |
| 2758 | *regcode++ = NUL; /* Null "next" pointer. */ |
| 2759 | *regcode++ = NUL; |
| 2760 | } |
| 2761 | return ret; |
| 2762 | } |
| 2763 | |
| 2764 | /* |
| 2765 | * Emit (if appropriate) a byte of code |
| 2766 | */ |
| 2767 | static void |
| 2768 | regc(b) |
| 2769 | int b; |
| 2770 | { |
| 2771 | if (regcode == JUST_CALC_SIZE) |
| 2772 | regsize++; |
| 2773 | else |
| 2774 | *regcode++ = b; |
| 2775 | } |
| 2776 | |
| 2777 | #ifdef FEAT_MBYTE |
| 2778 | /* |
| 2779 | * Emit (if appropriate) a multi-byte character of code |
| 2780 | */ |
| 2781 | static void |
| 2782 | regmbc(c) |
| 2783 | int c; |
| 2784 | { |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 2785 | if (!has_mbyte && c > 0xff) |
| 2786 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2787 | if (regcode == JUST_CALC_SIZE) |
| 2788 | regsize += (*mb_char2len)(c); |
| 2789 | else |
| 2790 | regcode += (*mb_char2bytes)(c, regcode); |
| 2791 | } |
| 2792 | #endif |
| 2793 | |
| 2794 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2795 | * Insert an operator in front of already-emitted operand |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2796 | * |
| 2797 | * Means relocating the operand. |
| 2798 | */ |
| 2799 | static void |
| 2800 | reginsert(op, opnd) |
| 2801 | int op; |
| 2802 | char_u *opnd; |
| 2803 | { |
| 2804 | char_u *src; |
| 2805 | char_u *dst; |
| 2806 | char_u *place; |
| 2807 | |
| 2808 | if (regcode == JUST_CALC_SIZE) |
| 2809 | { |
| 2810 | regsize += 3; |
| 2811 | return; |
| 2812 | } |
| 2813 | src = regcode; |
| 2814 | regcode += 3; |
| 2815 | dst = regcode; |
| 2816 | while (src > opnd) |
| 2817 | *--dst = *--src; |
| 2818 | |
| 2819 | place = opnd; /* Op node, where operand used to be. */ |
| 2820 | *place++ = op; |
| 2821 | *place++ = NUL; |
| 2822 | *place = NUL; |
| 2823 | } |
| 2824 | |
| 2825 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2826 | * Insert an operator in front of already-emitted operand. |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 2827 | * Add a number to the operator. |
| 2828 | */ |
| 2829 | static void |
| 2830 | reginsert_nr(op, val, opnd) |
| 2831 | int op; |
| 2832 | long val; |
| 2833 | char_u *opnd; |
| 2834 | { |
| 2835 | char_u *src; |
| 2836 | char_u *dst; |
| 2837 | char_u *place; |
| 2838 | |
| 2839 | if (regcode == JUST_CALC_SIZE) |
| 2840 | { |
| 2841 | regsize += 7; |
| 2842 | return; |
| 2843 | } |
| 2844 | src = regcode; |
| 2845 | regcode += 7; |
| 2846 | dst = regcode; |
| 2847 | while (src > opnd) |
| 2848 | *--dst = *--src; |
| 2849 | |
| 2850 | place = opnd; /* Op node, where operand used to be. */ |
| 2851 | *place++ = op; |
| 2852 | *place++ = NUL; |
| 2853 | *place++ = NUL; |
| 2854 | place = re_put_long(place, (long_u)val); |
| 2855 | } |
| 2856 | |
| 2857 | /* |
| 2858 | * Insert an operator in front of already-emitted operand. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | * The operator has the given limit values as operands. Also set next pointer. |
| 2860 | * |
| 2861 | * Means relocating the operand. |
| 2862 | */ |
| 2863 | static void |
| 2864 | reginsert_limits(op, minval, maxval, opnd) |
| 2865 | int op; |
| 2866 | long minval; |
| 2867 | long maxval; |
| 2868 | char_u *opnd; |
| 2869 | { |
| 2870 | char_u *src; |
| 2871 | char_u *dst; |
| 2872 | char_u *place; |
| 2873 | |
| 2874 | if (regcode == JUST_CALC_SIZE) |
| 2875 | { |
| 2876 | regsize += 11; |
| 2877 | return; |
| 2878 | } |
| 2879 | src = regcode; |
| 2880 | regcode += 11; |
| 2881 | dst = regcode; |
| 2882 | while (src > opnd) |
| 2883 | *--dst = *--src; |
| 2884 | |
| 2885 | place = opnd; /* Op node, where operand used to be. */ |
| 2886 | *place++ = op; |
| 2887 | *place++ = NUL; |
| 2888 | *place++ = NUL; |
| 2889 | place = re_put_long(place, (long_u)minval); |
| 2890 | place = re_put_long(place, (long_u)maxval); |
| 2891 | regtail(opnd, place); |
| 2892 | } |
| 2893 | |
| 2894 | /* |
| 2895 | * Write a long as four bytes at "p" and return pointer to the next char. |
| 2896 | */ |
| 2897 | static char_u * |
| 2898 | re_put_long(p, val) |
| 2899 | char_u *p; |
| 2900 | long_u val; |
| 2901 | { |
| 2902 | *p++ = (char_u) ((val >> 24) & 0377); |
| 2903 | *p++ = (char_u) ((val >> 16) & 0377); |
| 2904 | *p++ = (char_u) ((val >> 8) & 0377); |
| 2905 | *p++ = (char_u) (val & 0377); |
| 2906 | return p; |
| 2907 | } |
| 2908 | |
| 2909 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2910 | * Set the next-pointer at the end of a node chain. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2911 | */ |
| 2912 | static void |
| 2913 | regtail(p, val) |
| 2914 | char_u *p; |
| 2915 | char_u *val; |
| 2916 | { |
| 2917 | char_u *scan; |
| 2918 | char_u *temp; |
| 2919 | int offset; |
| 2920 | |
| 2921 | if (p == JUST_CALC_SIZE) |
| 2922 | return; |
| 2923 | |
| 2924 | /* Find last node. */ |
| 2925 | scan = p; |
| 2926 | for (;;) |
| 2927 | { |
| 2928 | temp = regnext(scan); |
| 2929 | if (temp == NULL) |
| 2930 | break; |
| 2931 | scan = temp; |
| 2932 | } |
| 2933 | |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 2934 | if (OP(scan) == BACK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2935 | offset = (int)(scan - val); |
| 2936 | else |
| 2937 | offset = (int)(val - scan); |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 2938 | /* When the offset uses more than 16 bits it can no longer fit in the two |
Bram Moolenaar | 522f9ae | 2011-07-20 17:58:20 +0200 | [diff] [blame] | 2939 | * bytes available. Use a global flag to avoid having to check return |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 2940 | * values in too many places. */ |
| 2941 | if (offset > 0xffff) |
| 2942 | reg_toolong = TRUE; |
| 2943 | else |
| 2944 | { |
| 2945 | *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377); |
| 2946 | *(scan + 2) = (char_u) (offset & 0377); |
| 2947 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
| 2950 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2951 | * Like regtail, on item after a BRANCH; nop if none. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2952 | */ |
| 2953 | static void |
| 2954 | regoptail(p, val) |
| 2955 | char_u *p; |
| 2956 | char_u *val; |
| 2957 | { |
| 2958 | /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */ |
| 2959 | if (p == NULL || p == JUST_CALC_SIZE |
| 2960 | || (OP(p) != BRANCH |
| 2961 | && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9))) |
| 2962 | return; |
| 2963 | regtail(OPERAND(p), val); |
| 2964 | } |
| 2965 | |
| 2966 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2967 | * Functions for getting characters from the regexp input. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2968 | */ |
| 2969 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2970 | static int at_start; /* True when on the first character */ |
| 2971 | static int prev_at_start; /* True when on the second character */ |
| 2972 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2973 | /* |
| 2974 | * Start parsing at "str". |
| 2975 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | static void |
| 2977 | initchr(str) |
| 2978 | char_u *str; |
| 2979 | { |
| 2980 | regparse = str; |
| 2981 | prevchr_len = 0; |
| 2982 | curchr = prevprevchr = prevchr = nextchr = -1; |
| 2983 | at_start = TRUE; |
| 2984 | prev_at_start = FALSE; |
| 2985 | } |
| 2986 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 2987 | /* |
Bram Moolenaar | 3737fc1 | 2013-06-01 14:42:56 +0200 | [diff] [blame] | 2988 | * Save the current parse state, so that it can be restored and parsing |
| 2989 | * starts in the same state again. |
| 2990 | */ |
| 2991 | static void |
| 2992 | save_parse_state(ps) |
| 2993 | parse_state_T *ps; |
| 2994 | { |
| 2995 | ps->regparse = regparse; |
| 2996 | ps->prevchr_len = prevchr_len; |
| 2997 | ps->curchr = curchr; |
| 2998 | ps->prevchr = prevchr; |
| 2999 | ps->prevprevchr = prevprevchr; |
| 3000 | ps->nextchr = nextchr; |
| 3001 | ps->at_start = at_start; |
| 3002 | ps->prev_at_start = prev_at_start; |
| 3003 | ps->regnpar = regnpar; |
| 3004 | } |
| 3005 | |
| 3006 | /* |
| 3007 | * Restore a previously saved parse state. |
| 3008 | */ |
| 3009 | static void |
| 3010 | restore_parse_state(ps) |
| 3011 | parse_state_T *ps; |
| 3012 | { |
| 3013 | regparse = ps->regparse; |
| 3014 | prevchr_len = ps->prevchr_len; |
| 3015 | curchr = ps->curchr; |
| 3016 | prevchr = ps->prevchr; |
| 3017 | prevprevchr = ps->prevprevchr; |
| 3018 | nextchr = ps->nextchr; |
| 3019 | at_start = ps->at_start; |
| 3020 | prev_at_start = ps->prev_at_start; |
| 3021 | regnpar = ps->regnpar; |
| 3022 | } |
| 3023 | |
| 3024 | |
| 3025 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3026 | * Get the next character without advancing. |
| 3027 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3028 | static int |
| 3029 | peekchr() |
| 3030 | { |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3031 | static int after_slash = FALSE; |
| 3032 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3033 | if (curchr == -1) |
| 3034 | { |
| 3035 | switch (curchr = regparse[0]) |
| 3036 | { |
| 3037 | case '.': |
| 3038 | case '[': |
| 3039 | case '~': |
| 3040 | /* magic when 'magic' is on */ |
| 3041 | if (reg_magic >= MAGIC_ON) |
| 3042 | curchr = Magic(curchr); |
| 3043 | break; |
| 3044 | case '(': |
| 3045 | case ')': |
| 3046 | case '{': |
| 3047 | case '%': |
| 3048 | case '+': |
| 3049 | case '=': |
| 3050 | case '?': |
| 3051 | case '@': |
| 3052 | case '!': |
| 3053 | case '&': |
| 3054 | case '|': |
| 3055 | case '<': |
| 3056 | case '>': |
| 3057 | case '#': /* future ext. */ |
| 3058 | case '"': /* future ext. */ |
| 3059 | case '\'': /* future ext. */ |
| 3060 | case ',': /* future ext. */ |
| 3061 | case '-': /* future ext. */ |
| 3062 | case ':': /* future ext. */ |
| 3063 | case ';': /* future ext. */ |
| 3064 | case '`': /* future ext. */ |
| 3065 | case '/': /* Can't be used in / command */ |
| 3066 | /* magic only after "\v" */ |
| 3067 | if (reg_magic == MAGIC_ALL) |
| 3068 | curchr = Magic(curchr); |
| 3069 | break; |
| 3070 | case '*': |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3071 | /* * is not magic as the very first character, eg "?*ptr", when |
| 3072 | * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But |
| 3073 | * "\(\*" is not magic, thus must be magic if "after_slash" */ |
| 3074 | if (reg_magic >= MAGIC_ON |
| 3075 | && !at_start |
| 3076 | && !(prev_at_start && prevchr == Magic('^')) |
| 3077 | && (after_slash |
| 3078 | || (prevchr != Magic('(') |
| 3079 | && prevchr != Magic('&') |
| 3080 | && prevchr != Magic('|')))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3081 | curchr = Magic('*'); |
| 3082 | break; |
| 3083 | case '^': |
| 3084 | /* '^' is only magic as the very first character and if it's after |
| 3085 | * "\(", "\|", "\&' or "\n" */ |
| 3086 | if (reg_magic >= MAGIC_OFF |
| 3087 | && (at_start |
| 3088 | || reg_magic == MAGIC_ALL |
| 3089 | || prevchr == Magic('(') |
| 3090 | || prevchr == Magic('|') |
| 3091 | || prevchr == Magic('&') |
| 3092 | || prevchr == Magic('n') |
| 3093 | || (no_Magic(prevchr) == '(' |
| 3094 | && prevprevchr == Magic('%')))) |
| 3095 | { |
| 3096 | curchr = Magic('^'); |
| 3097 | at_start = TRUE; |
| 3098 | prev_at_start = FALSE; |
| 3099 | } |
| 3100 | break; |
| 3101 | case '$': |
| 3102 | /* '$' is only magic as the very last char and if it's in front of |
| 3103 | * either "\|", "\)", "\&", or "\n" */ |
| 3104 | if (reg_magic >= MAGIC_OFF) |
| 3105 | { |
| 3106 | char_u *p = regparse + 1; |
| 3107 | |
| 3108 | /* ignore \c \C \m and \M after '$' */ |
| 3109 | while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C' |
| 3110 | || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z')) |
| 3111 | p += 2; |
| 3112 | if (p[0] == NUL |
| 3113 | || (p[0] == '\\' |
| 3114 | && (p[1] == '|' || p[1] == '&' || p[1] == ')' |
| 3115 | || p[1] == 'n')) |
| 3116 | || reg_magic == MAGIC_ALL) |
| 3117 | curchr = Magic('$'); |
| 3118 | } |
| 3119 | break; |
| 3120 | case '\\': |
| 3121 | { |
| 3122 | int c = regparse[1]; |
| 3123 | |
| 3124 | if (c == NUL) |
| 3125 | curchr = '\\'; /* trailing '\' */ |
| 3126 | else if ( |
| 3127 | #ifdef EBCDIC |
| 3128 | vim_strchr(META, c) |
| 3129 | #else |
| 3130 | c <= '~' && META_flags[c] |
| 3131 | #endif |
| 3132 | ) |
| 3133 | { |
| 3134 | /* |
| 3135 | * META contains everything that may be magic sometimes, |
| 3136 | * except ^ and $ ("\^" and "\$" are only magic after |
| 3137 | * "\v"). We now fetch the next character and toggle its |
| 3138 | * magicness. Therefore, \ is so meta-magic that it is |
| 3139 | * not in META. |
| 3140 | */ |
| 3141 | curchr = -1; |
| 3142 | prev_at_start = at_start; |
| 3143 | at_start = FALSE; /* be able to say "/\*ptr" */ |
| 3144 | ++regparse; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3145 | ++after_slash; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3146 | peekchr(); |
| 3147 | --regparse; |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3148 | --after_slash; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3149 | curchr = toggle_Magic(curchr); |
| 3150 | } |
| 3151 | else if (vim_strchr(REGEXP_ABBR, c)) |
| 3152 | { |
| 3153 | /* |
| 3154 | * Handle abbreviations, like "\t" for TAB -- webb |
| 3155 | */ |
| 3156 | curchr = backslash_trans(c); |
| 3157 | } |
| 3158 | else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^')) |
| 3159 | curchr = toggle_Magic(c); |
| 3160 | else |
| 3161 | { |
| 3162 | /* |
| 3163 | * Next character can never be (made) magic? |
| 3164 | * Then backslashing it won't do anything. |
| 3165 | */ |
| 3166 | #ifdef FEAT_MBYTE |
| 3167 | if (has_mbyte) |
| 3168 | curchr = (*mb_ptr2char)(regparse + 1); |
| 3169 | else |
| 3170 | #endif |
| 3171 | curchr = c; |
| 3172 | } |
| 3173 | break; |
| 3174 | } |
| 3175 | |
| 3176 | #ifdef FEAT_MBYTE |
| 3177 | default: |
| 3178 | if (has_mbyte) |
| 3179 | curchr = (*mb_ptr2char)(regparse); |
| 3180 | #endif |
| 3181 | } |
| 3182 | } |
| 3183 | |
| 3184 | return curchr; |
| 3185 | } |
| 3186 | |
| 3187 | /* |
| 3188 | * Eat one lexed character. Do this in a way that we can undo it. |
| 3189 | */ |
| 3190 | static void |
| 3191 | skipchr() |
| 3192 | { |
| 3193 | /* peekchr() eats a backslash, do the same here */ |
| 3194 | if (*regparse == '\\') |
| 3195 | prevchr_len = 1; |
| 3196 | else |
| 3197 | prevchr_len = 0; |
| 3198 | if (regparse[prevchr_len] != NUL) |
| 3199 | { |
| 3200 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3201 | if (enc_utf8) |
Bram Moolenaar | 8f5c578 | 2007-11-29 20:27:21 +0000 | [diff] [blame] | 3202 | /* exclude composing chars that mb_ptr2len does include */ |
| 3203 | prevchr_len += utf_ptr2len(regparse + prevchr_len); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3204 | else if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3205 | prevchr_len += (*mb_ptr2len)(regparse + prevchr_len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3206 | else |
| 3207 | #endif |
| 3208 | ++prevchr_len; |
| 3209 | } |
| 3210 | regparse += prevchr_len; |
| 3211 | prev_at_start = at_start; |
| 3212 | at_start = FALSE; |
| 3213 | prevprevchr = prevchr; |
| 3214 | prevchr = curchr; |
| 3215 | curchr = nextchr; /* use previously unget char, or -1 */ |
| 3216 | nextchr = -1; |
| 3217 | } |
| 3218 | |
| 3219 | /* |
| 3220 | * Skip a character while keeping the value of prev_at_start for at_start. |
| 3221 | * prevchr and prevprevchr are also kept. |
| 3222 | */ |
| 3223 | static void |
| 3224 | skipchr_keepstart() |
| 3225 | { |
| 3226 | int as = prev_at_start; |
| 3227 | int pr = prevchr; |
| 3228 | int prpr = prevprevchr; |
| 3229 | |
| 3230 | skipchr(); |
| 3231 | at_start = as; |
| 3232 | prevchr = pr; |
| 3233 | prevprevchr = prpr; |
| 3234 | } |
| 3235 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3236 | /* |
| 3237 | * Get the next character from the pattern. We know about magic and such, so |
| 3238 | * therefore we need a lexical analyzer. |
| 3239 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3240 | static int |
| 3241 | getchr() |
| 3242 | { |
| 3243 | int chr = peekchr(); |
| 3244 | |
| 3245 | skipchr(); |
| 3246 | return chr; |
| 3247 | } |
| 3248 | |
| 3249 | /* |
| 3250 | * put character back. Works only once! |
| 3251 | */ |
| 3252 | static void |
| 3253 | ungetchr() |
| 3254 | { |
| 3255 | nextchr = curchr; |
| 3256 | curchr = prevchr; |
| 3257 | prevchr = prevprevchr; |
| 3258 | at_start = prev_at_start; |
| 3259 | prev_at_start = FALSE; |
| 3260 | |
| 3261 | /* Backup regparse, so that it's at the same position as before the |
| 3262 | * getchr(). */ |
| 3263 | regparse -= prevchr_len; |
| 3264 | } |
| 3265 | |
| 3266 | /* |
Bram Moolenaar | 7b0294c | 2004-10-11 10:16:09 +0000 | [diff] [blame] | 3267 | * Get and return the value of the hex string at the current position. |
| 3268 | * Return -1 if there is no valid hex number. |
| 3269 | * The position is updated: |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 3270 | * blahblah\%x20asdf |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3271 | * before-^ ^-after |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 3272 | * The parameter controls the maximum number of input characters. This will be |
| 3273 | * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence. |
| 3274 | */ |
| 3275 | static int |
| 3276 | gethexchrs(maxinputlen) |
| 3277 | int maxinputlen; |
| 3278 | { |
| 3279 | int nr = 0; |
| 3280 | int c; |
| 3281 | int i; |
| 3282 | |
| 3283 | for (i = 0; i < maxinputlen; ++i) |
| 3284 | { |
| 3285 | c = regparse[0]; |
| 3286 | if (!vim_isxdigit(c)) |
| 3287 | break; |
| 3288 | nr <<= 4; |
| 3289 | nr |= hex2nr(c); |
| 3290 | ++regparse; |
| 3291 | } |
| 3292 | |
| 3293 | if (i == 0) |
| 3294 | return -1; |
| 3295 | return nr; |
| 3296 | } |
| 3297 | |
| 3298 | /* |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 3299 | * Get and return the value of the decimal string immediately after the |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 3300 | * current position. Return -1 for invalid. Consumes all digits. |
| 3301 | */ |
| 3302 | static int |
| 3303 | getdecchrs() |
| 3304 | { |
| 3305 | int nr = 0; |
| 3306 | int c; |
| 3307 | int i; |
| 3308 | |
| 3309 | for (i = 0; ; ++i) |
| 3310 | { |
| 3311 | c = regparse[0]; |
| 3312 | if (c < '0' || c > '9') |
| 3313 | break; |
| 3314 | nr *= 10; |
| 3315 | nr += c - '0'; |
| 3316 | ++regparse; |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 3317 | curchr = -1; /* no longer valid */ |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | if (i == 0) |
| 3321 | return -1; |
| 3322 | return nr; |
| 3323 | } |
| 3324 | |
| 3325 | /* |
| 3326 | * get and return the value of the octal string immediately after the current |
| 3327 | * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle |
| 3328 | * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't |
| 3329 | * treat 8 or 9 as recognised characters. Position is updated: |
| 3330 | * blahblah\%o210asdf |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 3331 | * before-^ ^-after |
Bram Moolenaar | c0197e2 | 2004-09-13 20:26:32 +0000 | [diff] [blame] | 3332 | */ |
| 3333 | static int |
| 3334 | getoctchrs() |
| 3335 | { |
| 3336 | int nr = 0; |
| 3337 | int c; |
| 3338 | int i; |
| 3339 | |
| 3340 | for (i = 0; i < 3 && nr < 040; ++i) |
| 3341 | { |
| 3342 | c = regparse[0]; |
| 3343 | if (c < '0' || c > '7') |
| 3344 | break; |
| 3345 | nr <<= 3; |
| 3346 | nr |= hex2nr(c); |
| 3347 | ++regparse; |
| 3348 | } |
| 3349 | |
| 3350 | if (i == 0) |
| 3351 | return -1; |
| 3352 | return nr; |
| 3353 | } |
| 3354 | |
| 3355 | /* |
| 3356 | * Get a number after a backslash that is inside []. |
| 3357 | * When nothing is recognized return a backslash. |
| 3358 | */ |
| 3359 | static int |
| 3360 | coll_get_char() |
| 3361 | { |
| 3362 | int nr = -1; |
| 3363 | |
| 3364 | switch (*regparse++) |
| 3365 | { |
| 3366 | case 'd': nr = getdecchrs(); break; |
| 3367 | case 'o': nr = getoctchrs(); break; |
| 3368 | case 'x': nr = gethexchrs(2); break; |
| 3369 | case 'u': nr = gethexchrs(4); break; |
| 3370 | case 'U': nr = gethexchrs(8); break; |
| 3371 | } |
| 3372 | if (nr < 0) |
| 3373 | { |
| 3374 | /* If getting the number fails be backwards compatible: the character |
| 3375 | * is a backslash. */ |
| 3376 | --regparse; |
| 3377 | nr = '\\'; |
| 3378 | } |
| 3379 | return nr; |
| 3380 | } |
| 3381 | |
| 3382 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3383 | * read_limits - Read two integers to be taken as a minimum and maximum. |
| 3384 | * If the first character is '-', then the range is reversed. |
| 3385 | * Should end with 'end'. If minval is missing, zero is default, if maxval is |
| 3386 | * missing, a very big number is the default. |
| 3387 | */ |
| 3388 | static int |
| 3389 | read_limits(minval, maxval) |
| 3390 | long *minval; |
| 3391 | long *maxval; |
| 3392 | { |
| 3393 | int reverse = FALSE; |
| 3394 | char_u *first_char; |
| 3395 | long tmp; |
| 3396 | |
| 3397 | if (*regparse == '-') |
| 3398 | { |
| 3399 | /* Starts with '-', so reverse the range later */ |
| 3400 | regparse++; |
| 3401 | reverse = TRUE; |
| 3402 | } |
| 3403 | first_char = regparse; |
| 3404 | *minval = getdigits(®parse); |
| 3405 | if (*regparse == ',') /* There is a comma */ |
| 3406 | { |
| 3407 | if (vim_isdigit(*++regparse)) |
| 3408 | *maxval = getdigits(®parse); |
| 3409 | else |
| 3410 | *maxval = MAX_LIMIT; |
| 3411 | } |
| 3412 | else if (VIM_ISDIGIT(*first_char)) |
| 3413 | *maxval = *minval; /* It was \{n} or \{-n} */ |
| 3414 | else |
| 3415 | *maxval = MAX_LIMIT; /* It was \{} or \{-} */ |
| 3416 | if (*regparse == '\\') |
| 3417 | regparse++; /* Allow either \{...} or \{...\} */ |
Bram Moolenaar | df177f6 | 2005-02-22 08:39:57 +0000 | [diff] [blame] | 3418 | if (*regparse != '}') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3419 | { |
| 3420 | sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"), |
| 3421 | reg_magic == MAGIC_ALL ? "" : "\\"); |
| 3422 | EMSG_RET_FAIL(IObuff); |
| 3423 | } |
| 3424 | |
| 3425 | /* |
| 3426 | * Reverse the range if there was a '-', or make sure it is in the right |
| 3427 | * order otherwise. |
| 3428 | */ |
| 3429 | if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval)) |
| 3430 | { |
| 3431 | tmp = *minval; |
| 3432 | *minval = *maxval; |
| 3433 | *maxval = tmp; |
| 3434 | } |
| 3435 | skipchr(); /* let's be friends with the lexer again */ |
| 3436 | return OK; |
| 3437 | } |
| 3438 | |
| 3439 | /* |
| 3440 | * vim_regexec and friends |
| 3441 | */ |
| 3442 | |
| 3443 | /* |
| 3444 | * Global work variables for vim_regexec(). |
| 3445 | */ |
| 3446 | |
| 3447 | /* The current match-position is remembered with these variables: */ |
| 3448 | static linenr_T reglnum; /* line number, relative to first line */ |
| 3449 | static char_u *regline; /* start of current line */ |
| 3450 | static char_u *reginput; /* current input, points into "regline" */ |
| 3451 | |
| 3452 | static int need_clear_subexpr; /* subexpressions still need to be |
| 3453 | * cleared */ |
| 3454 | #ifdef FEAT_SYN_HL |
| 3455 | static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions |
| 3456 | * still need to be cleared */ |
| 3457 | #endif |
| 3458 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3459 | /* |
| 3460 | * Structure used to save the current input state, when it needs to be |
| 3461 | * restored after trying a match. Used by reg_save() and reg_restore(). |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 3462 | * Also stores the length of "backpos". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3463 | */ |
| 3464 | typedef struct |
| 3465 | { |
| 3466 | union |
| 3467 | { |
| 3468 | char_u *ptr; /* reginput pointer, for single-line regexp */ |
| 3469 | lpos_T pos; /* reginput pos, for multi-line regexp */ |
| 3470 | } rs_u; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 3471 | int rs_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3472 | } regsave_T; |
| 3473 | |
| 3474 | /* struct to save start/end pointer/position in for \(\) */ |
| 3475 | typedef struct |
| 3476 | { |
| 3477 | union |
| 3478 | { |
| 3479 | char_u *ptr; |
| 3480 | lpos_T pos; |
| 3481 | } se_u; |
| 3482 | } save_se_T; |
| 3483 | |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 3484 | /* used for BEHIND and NOBEHIND matching */ |
| 3485 | typedef struct regbehind_S |
| 3486 | { |
| 3487 | regsave_T save_after; |
| 3488 | regsave_T save_behind; |
Bram Moolenaar | fde483c | 2008-06-15 12:21:50 +0000 | [diff] [blame] | 3489 | int save_need_clear_subexpr; |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 3490 | save_se_T save_start[NSUBEXP]; |
| 3491 | save_se_T save_end[NSUBEXP]; |
| 3492 | } regbehind_T; |
| 3493 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3494 | static char_u *reg_getline __ARGS((linenr_T lnum)); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3495 | static long bt_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm)); |
| 3496 | static long regtry __ARGS((bt_regprog_T *prog, colnr_T col)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3497 | static void cleanup_subexpr __ARGS((void)); |
| 3498 | #ifdef FEAT_SYN_HL |
| 3499 | static void cleanup_zsubexpr __ARGS((void)); |
| 3500 | #endif |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 3501 | static void save_subexpr __ARGS((regbehind_T *bp)); |
| 3502 | static void restore_subexpr __ARGS((regbehind_T *bp)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3503 | static void reg_nextline __ARGS((void)); |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 3504 | static void reg_save __ARGS((regsave_T *save, garray_T *gap)); |
| 3505 | static void reg_restore __ARGS((regsave_T *save, garray_T *gap)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3506 | static int reg_save_equal __ARGS((regsave_T *save)); |
| 3507 | static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp)); |
| 3508 | static void save_se_one __ARGS((save_se_T *savep, char_u **pp)); |
| 3509 | |
| 3510 | /* Save the sub-expressions before attempting a match. */ |
| 3511 | #define save_se(savep, posp, pp) \ |
| 3512 | REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp)) |
| 3513 | |
| 3514 | /* After a failed match restore the sub-expressions. */ |
| 3515 | #define restore_se(savep, posp, pp) { \ |
| 3516 | if (REG_MULTI) \ |
| 3517 | *(posp) = (savep)->se_u.pos; \ |
| 3518 | else \ |
| 3519 | *(pp) = (savep)->se_u.ptr; } |
| 3520 | |
| 3521 | static int re_num_cmp __ARGS((long_u val, char_u *scan)); |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 3522 | static int match_with_backref __ARGS((linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum, colnr_T end_col, int *bytelen)); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3523 | static int regmatch __ARGS((char_u *prog)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3524 | static int regrepeat __ARGS((char_u *p, long maxcount)); |
| 3525 | |
| 3526 | #ifdef DEBUG |
| 3527 | int regnarrate = 0; |
| 3528 | #endif |
| 3529 | |
| 3530 | /* |
| 3531 | * Internal copy of 'ignorecase'. It is set at each call to vim_regexec(). |
| 3532 | * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern |
| 3533 | * contains '\c' or '\C' the value is overruled. |
| 3534 | */ |
| 3535 | static int ireg_ic; |
| 3536 | |
| 3537 | #ifdef FEAT_MBYTE |
| 3538 | /* |
| 3539 | * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag |
| 3540 | * in the regexp. Defaults to false, always. |
| 3541 | */ |
| 3542 | static int ireg_icombine; |
| 3543 | #endif |
| 3544 | |
| 3545 | /* |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 3546 | * Copy of "rmm_maxcol": maximum column to search for a match. Zero when |
| 3547 | * there is no maximum. |
| 3548 | */ |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 3549 | static colnr_T ireg_maxcol; |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 3550 | |
| 3551 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3552 | * Sometimes need to save a copy of a line. Since alloc()/free() is very |
| 3553 | * slow, we keep one allocated piece of memory and only re-allocate it when |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3554 | * it's too small. It's freed in bt_regexec_both() when finished. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3555 | */ |
Bram Moolenaar | d421077 | 2008-01-02 14:35:30 +0000 | [diff] [blame] | 3556 | static char_u *reg_tofree = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3557 | static unsigned reg_tofreelen; |
| 3558 | |
| 3559 | /* |
| 3560 | * These variables are set when executing a regexp to speed up the execution. |
Bram Moolenaar | 7aa9f6a | 2007-05-10 18:00:30 +0000 | [diff] [blame] | 3561 | * Which ones are set depends on whether a single-line or multi-line match is |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3562 | * done: |
| 3563 | * single-line multi-line |
| 3564 | * reg_match ®match_T NULL |
| 3565 | * reg_mmatch NULL ®mmatch_T |
| 3566 | * reg_startp reg_match->startp <invalid> |
| 3567 | * reg_endp reg_match->endp <invalid> |
| 3568 | * reg_startpos <invalid> reg_mmatch->startpos |
| 3569 | * reg_endpos <invalid> reg_mmatch->endpos |
| 3570 | * reg_win NULL window in which to search |
Bram Moolenaar | 2f315ab | 2013-01-25 20:11:01 +0100 | [diff] [blame] | 3571 | * reg_buf curbuf buffer in which to search |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3572 | * reg_firstlnum <invalid> first line in which to search |
| 3573 | * reg_maxline 0 last line nr |
| 3574 | * reg_line_lbr FALSE or TRUE FALSE |
| 3575 | */ |
| 3576 | static regmatch_T *reg_match; |
| 3577 | static regmmatch_T *reg_mmatch; |
| 3578 | static char_u **reg_startp = NULL; |
| 3579 | static char_u **reg_endp = NULL; |
| 3580 | static lpos_T *reg_startpos = NULL; |
| 3581 | static lpos_T *reg_endpos = NULL; |
| 3582 | static win_T *reg_win; |
| 3583 | static buf_T *reg_buf; |
| 3584 | static linenr_T reg_firstlnum; |
| 3585 | static linenr_T reg_maxline; |
| 3586 | static int reg_line_lbr; /* "\n" in string is line break */ |
| 3587 | |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 3588 | /* Values for rs_state in regitem_T. */ |
| 3589 | typedef enum regstate_E |
| 3590 | { |
| 3591 | RS_NOPEN = 0 /* NOPEN and NCLOSE */ |
| 3592 | , RS_MOPEN /* MOPEN + [0-9] */ |
| 3593 | , RS_MCLOSE /* MCLOSE + [0-9] */ |
| 3594 | #ifdef FEAT_SYN_HL |
| 3595 | , RS_ZOPEN /* ZOPEN + [0-9] */ |
| 3596 | , RS_ZCLOSE /* ZCLOSE + [0-9] */ |
| 3597 | #endif |
| 3598 | , RS_BRANCH /* BRANCH */ |
| 3599 | , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */ |
| 3600 | , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */ |
| 3601 | , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */ |
| 3602 | , RS_NOMATCH /* NOMATCH */ |
| 3603 | , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */ |
| 3604 | , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */ |
| 3605 | , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */ |
| 3606 | , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */ |
| 3607 | } regstate_T; |
| 3608 | |
| 3609 | /* |
| 3610 | * When there are alternatives a regstate_T is put on the regstack to remember |
| 3611 | * what we are doing. |
| 3612 | * Before it may be another type of item, depending on rs_state, to remember |
| 3613 | * more things. |
| 3614 | */ |
| 3615 | typedef struct regitem_S |
| 3616 | { |
| 3617 | regstate_T rs_state; /* what we are doing, one of RS_ above */ |
| 3618 | char_u *rs_scan; /* current node in program */ |
| 3619 | union |
| 3620 | { |
| 3621 | save_se_T sesave; |
| 3622 | regsave_T regsave; |
| 3623 | } rs_un; /* room for saving reginput */ |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 3624 | short rs_no; /* submatch nr or BEHIND/NOBEHIND */ |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 3625 | } regitem_T; |
| 3626 | |
| 3627 | static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan)); |
| 3628 | static void regstack_pop __ARGS((char_u **scan)); |
| 3629 | |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 3630 | /* used for STAR, PLUS and BRACE_SIMPLE matching */ |
| 3631 | typedef struct regstar_S |
| 3632 | { |
| 3633 | int nextb; /* next byte */ |
| 3634 | int nextb_ic; /* next byte reverse case */ |
| 3635 | long count; |
| 3636 | long minval; |
| 3637 | long maxval; |
| 3638 | } regstar_T; |
| 3639 | |
| 3640 | /* used to store input position when a BACK was encountered, so that we now if |
| 3641 | * we made any progress since the last time. */ |
| 3642 | typedef struct backpos_S |
| 3643 | { |
| 3644 | char_u *bp_scan; /* "scan" where BACK was encountered */ |
| 3645 | regsave_T bp_pos; /* last input position */ |
| 3646 | } backpos_T; |
| 3647 | |
| 3648 | /* |
Bram Moolenaar | 4bad6c8 | 2008-01-18 19:37:23 +0000 | [diff] [blame] | 3649 | * "regstack" and "backpos" are used by regmatch(). They are kept over calls |
| 3650 | * to avoid invoking malloc() and free() often. |
| 3651 | * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T |
| 3652 | * or regbehind_T. |
| 3653 | * "backpos_T" is a table with backpos_T for BACK |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 3654 | */ |
Bram Moolenaar | 4bad6c8 | 2008-01-18 19:37:23 +0000 | [diff] [blame] | 3655 | static garray_T regstack = {0, 0, 0, 0, NULL}; |
| 3656 | static garray_T backpos = {0, 0, 0, 0, NULL}; |
| 3657 | |
| 3658 | /* |
| 3659 | * Both for regstack and backpos tables we use the following strategy of |
| 3660 | * allocation (to reduce malloc/free calls): |
| 3661 | * - Initial size is fairly small. |
| 3662 | * - When needed, the tables are grown bigger (8 times at first, double after |
| 3663 | * that). |
| 3664 | * - After executing the match we free the memory only if the array has grown. |
| 3665 | * Thus the memory is kept allocated when it's at the initial size. |
| 3666 | * This makes it fast while not keeping a lot of memory allocated. |
| 3667 | * A three times speed increase was observed when using many simple patterns. |
| 3668 | */ |
| 3669 | #define REGSTACK_INITIAL 2048 |
| 3670 | #define BACKPOS_INITIAL 64 |
| 3671 | |
| 3672 | #if defined(EXITFREE) || defined(PROTO) |
| 3673 | void |
| 3674 | free_regexp_stuff() |
| 3675 | { |
| 3676 | ga_clear(®stack); |
| 3677 | ga_clear(&backpos); |
| 3678 | vim_free(reg_tofree); |
| 3679 | vim_free(reg_prev_sub); |
| 3680 | } |
| 3681 | #endif |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 3682 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3683 | /* |
| 3684 | * Get pointer to the line "lnum", which is relative to "reg_firstlnum". |
| 3685 | */ |
| 3686 | static char_u * |
| 3687 | reg_getline(lnum) |
| 3688 | linenr_T lnum; |
| 3689 | { |
| 3690 | /* when looking behind for a match/no-match lnum is negative. But we |
| 3691 | * can't go before line 1 */ |
| 3692 | if (reg_firstlnum + lnum < 1) |
| 3693 | return NULL; |
Bram Moolenaar | 5b8d8fd | 2005-08-16 23:01:50 +0000 | [diff] [blame] | 3694 | if (lnum > reg_maxline) |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 3695 | /* Must have matched the "\n" in the last line. */ |
| 3696 | return (char_u *)""; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3697 | return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE); |
| 3698 | } |
| 3699 | |
| 3700 | static regsave_T behind_pos; |
| 3701 | |
| 3702 | #ifdef FEAT_SYN_HL |
| 3703 | static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */ |
| 3704 | static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */ |
| 3705 | static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */ |
| 3706 | static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */ |
| 3707 | #endif |
| 3708 | |
| 3709 | /* TRUE if using multi-line regexp. */ |
| 3710 | #define REG_MULTI (reg_match == NULL) |
| 3711 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3712 | static int bt_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 3713 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3714 | /* |
| 3715 | * Match a regexp against a string. |
| 3716 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 3717 | * Uses curbuf for line count and 'iskeyword'. |
| 3718 | * |
| 3719 | * Return TRUE if there is a match, FALSE if not. |
| 3720 | */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3721 | static int |
| 3722 | bt_regexec(rmp, line, col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3723 | regmatch_T *rmp; |
| 3724 | char_u *line; /* string to match against */ |
| 3725 | colnr_T col; /* column to start looking for match */ |
| 3726 | { |
| 3727 | reg_match = rmp; |
| 3728 | reg_mmatch = NULL; |
| 3729 | reg_maxline = 0; |
| 3730 | reg_line_lbr = FALSE; |
Bram Moolenaar | 2f315ab | 2013-01-25 20:11:01 +0100 | [diff] [blame] | 3731 | reg_buf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3732 | reg_win = NULL; |
| 3733 | ireg_ic = rmp->rm_ic; |
| 3734 | #ifdef FEAT_MBYTE |
| 3735 | ireg_icombine = FALSE; |
| 3736 | #endif |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 3737 | ireg_maxcol = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3738 | return (bt_regexec_both(line, col, NULL) != 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3739 | } |
| 3740 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 3741 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 3742 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3743 | |
| 3744 | static int bt_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); |
| 3745 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3746 | /* |
| 3747 | * Like vim_regexec(), but consider a "\n" in "line" to be a line break. |
| 3748 | */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3749 | static int |
| 3750 | bt_regexec_nl(rmp, line, col) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3751 | regmatch_T *rmp; |
| 3752 | char_u *line; /* string to match against */ |
| 3753 | colnr_T col; /* column to start looking for match */ |
| 3754 | { |
| 3755 | reg_match = rmp; |
| 3756 | reg_mmatch = NULL; |
| 3757 | reg_maxline = 0; |
| 3758 | reg_line_lbr = TRUE; |
Bram Moolenaar | 2f315ab | 2013-01-25 20:11:01 +0100 | [diff] [blame] | 3759 | reg_buf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3760 | reg_win = NULL; |
| 3761 | ireg_ic = rmp->rm_ic; |
| 3762 | #ifdef FEAT_MBYTE |
| 3763 | ireg_icombine = FALSE; |
| 3764 | #endif |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 3765 | ireg_maxcol = 0; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3766 | return (bt_regexec_both(line, col, NULL) != 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | } |
| 3768 | #endif |
| 3769 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3770 | static long bt_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); |
| 3771 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3772 | /* |
| 3773 | * Match a regexp against multiple lines. |
| 3774 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 3775 | * Uses curbuf for line count and 'iskeyword'. |
| 3776 | * |
| 3777 | * Return zero if there is no match. Return number of lines contained in the |
| 3778 | * match otherwise. |
| 3779 | */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3780 | static long |
| 3781 | bt_regexec_multi(rmp, win, buf, lnum, col, tm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3782 | regmmatch_T *rmp; |
| 3783 | win_T *win; /* window in which to search or NULL */ |
| 3784 | buf_T *buf; /* buffer in which to search */ |
| 3785 | linenr_T lnum; /* nr of line to start looking for match */ |
| 3786 | colnr_T col; /* column to start looking for match */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 3787 | proftime_T *tm; /* timeout limit or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3788 | { |
| 3789 | long r; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3790 | |
| 3791 | reg_match = NULL; |
| 3792 | reg_mmatch = rmp; |
| 3793 | reg_buf = buf; |
| 3794 | reg_win = win; |
| 3795 | reg_firstlnum = lnum; |
| 3796 | reg_maxline = reg_buf->b_ml.ml_line_count - lnum; |
| 3797 | reg_line_lbr = FALSE; |
| 3798 | ireg_ic = rmp->rmm_ic; |
| 3799 | #ifdef FEAT_MBYTE |
| 3800 | ireg_icombine = FALSE; |
| 3801 | #endif |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 3802 | ireg_maxcol = rmp->rmm_maxcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3803 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3804 | r = bt_regexec_both(NULL, col, tm); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3805 | |
| 3806 | return r; |
| 3807 | } |
| 3808 | |
| 3809 | /* |
| 3810 | * Match a regexp against a string ("line" points to the string) or multiple |
| 3811 | * lines ("line" is NULL, use reg_getline()). |
| 3812 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | static long |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3814 | bt_regexec_both(line, col, tm) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3815 | char_u *line; |
| 3816 | colnr_T col; /* column to start looking for match */ |
Bram Moolenaar | 78a1531 | 2009-05-15 19:33:18 +0000 | [diff] [blame] | 3817 | proftime_T *tm UNUSED; /* timeout limit or NULL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3818 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3819 | bt_regprog_T *prog; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3820 | char_u *s; |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 3821 | long retval = 0L; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3822 | |
Bram Moolenaar | 4bad6c8 | 2008-01-18 19:37:23 +0000 | [diff] [blame] | 3823 | /* Create "regstack" and "backpos" if they are not allocated yet. |
| 3824 | * We allocate *_INITIAL amount of bytes first and then set the grow size |
| 3825 | * to much bigger value to avoid many malloc calls in case of deep regular |
| 3826 | * expressions. */ |
| 3827 | if (regstack.ga_data == NULL) |
| 3828 | { |
| 3829 | /* Use an item size of 1 byte, since we push different things |
| 3830 | * onto the regstack. */ |
| 3831 | ga_init2(®stack, 1, REGSTACK_INITIAL); |
| 3832 | ga_grow(®stack, REGSTACK_INITIAL); |
| 3833 | regstack.ga_growsize = REGSTACK_INITIAL * 8; |
| 3834 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3835 | |
Bram Moolenaar | 4bad6c8 | 2008-01-18 19:37:23 +0000 | [diff] [blame] | 3836 | if (backpos.ga_data == NULL) |
| 3837 | { |
| 3838 | ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL); |
| 3839 | ga_grow(&backpos, BACKPOS_INITIAL); |
| 3840 | backpos.ga_growsize = BACKPOS_INITIAL * 8; |
| 3841 | } |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 3842 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3843 | if (REG_MULTI) |
| 3844 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3845 | prog = (bt_regprog_T *)reg_mmatch->regprog; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3846 | line = reg_getline((linenr_T)0); |
| 3847 | reg_startpos = reg_mmatch->startpos; |
| 3848 | reg_endpos = reg_mmatch->endpos; |
| 3849 | } |
| 3850 | else |
| 3851 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 3852 | prog = (bt_regprog_T *)reg_match->regprog; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3853 | reg_startp = reg_match->startp; |
| 3854 | reg_endp = reg_match->endp; |
| 3855 | } |
| 3856 | |
| 3857 | /* Be paranoid... */ |
| 3858 | if (prog == NULL || line == NULL) |
| 3859 | { |
| 3860 | EMSG(_(e_null)); |
| 3861 | goto theend; |
| 3862 | } |
| 3863 | |
| 3864 | /* Check validity of program. */ |
| 3865 | if (prog_magic_wrong()) |
| 3866 | goto theend; |
| 3867 | |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 3868 | /* If the start column is past the maximum column: no need to try. */ |
| 3869 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 3870 | goto theend; |
| 3871 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3872 | /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ |
| 3873 | if (prog->regflags & RF_ICASE) |
| 3874 | ireg_ic = TRUE; |
| 3875 | else if (prog->regflags & RF_NOICASE) |
| 3876 | ireg_ic = FALSE; |
| 3877 | |
| 3878 | #ifdef FEAT_MBYTE |
| 3879 | /* If pattern contains "\Z" overrule value of ireg_icombine */ |
| 3880 | if (prog->regflags & RF_ICOMBINE) |
| 3881 | ireg_icombine = TRUE; |
| 3882 | #endif |
| 3883 | |
| 3884 | /* If there is a "must appear" string, look for it. */ |
| 3885 | if (prog->regmust != NULL) |
| 3886 | { |
| 3887 | int c; |
| 3888 | |
| 3889 | #ifdef FEAT_MBYTE |
| 3890 | if (has_mbyte) |
| 3891 | c = (*mb_ptr2char)(prog->regmust); |
| 3892 | else |
| 3893 | #endif |
| 3894 | c = *prog->regmust; |
| 3895 | s = line + col; |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3896 | |
| 3897 | /* |
| 3898 | * This is used very often, esp. for ":global". Use three versions of |
| 3899 | * the loop to avoid overhead of conditions. |
| 3900 | */ |
| 3901 | if (!ireg_ic |
| 3902 | #ifdef FEAT_MBYTE |
| 3903 | && !has_mbyte |
| 3904 | #endif |
| 3905 | ) |
| 3906 | while ((s = vim_strbyte(s, c)) != NULL) |
| 3907 | { |
| 3908 | if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) |
| 3909 | break; /* Found it. */ |
| 3910 | ++s; |
| 3911 | } |
| 3912 | #ifdef FEAT_MBYTE |
| 3913 | else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1)) |
| 3914 | while ((s = vim_strchr(s, c)) != NULL) |
| 3915 | { |
| 3916 | if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) |
| 3917 | break; /* Found it. */ |
| 3918 | mb_ptr_adv(s); |
| 3919 | } |
| 3920 | #endif |
| 3921 | else |
| 3922 | while ((s = cstrchr(s, c)) != NULL) |
| 3923 | { |
| 3924 | if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0) |
| 3925 | break; /* Found it. */ |
| 3926 | mb_ptr_adv(s); |
| 3927 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3928 | if (s == NULL) /* Not present. */ |
| 3929 | goto theend; |
| 3930 | } |
| 3931 | |
| 3932 | regline = line; |
| 3933 | reglnum = 0; |
Bram Moolenaar | 73a92fe | 2010-09-14 10:55:47 +0200 | [diff] [blame] | 3934 | reg_toolong = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3935 | |
| 3936 | /* Simplest case: Anchored match need be tried only once. */ |
| 3937 | if (prog->reganch) |
| 3938 | { |
| 3939 | int c; |
| 3940 | |
| 3941 | #ifdef FEAT_MBYTE |
| 3942 | if (has_mbyte) |
| 3943 | c = (*mb_ptr2char)(regline + col); |
| 3944 | else |
| 3945 | #endif |
| 3946 | c = regline[col]; |
| 3947 | if (prog->regstart == NUL |
| 3948 | || prog->regstart == c |
| 3949 | || (ireg_ic && (( |
| 3950 | #ifdef FEAT_MBYTE |
| 3951 | (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c))) |
| 3952 | || (c < 255 && prog->regstart < 255 && |
| 3953 | #endif |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 3954 | MB_TOLOWER(prog->regstart) == MB_TOLOWER(c))))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3955 | retval = regtry(prog, col); |
| 3956 | else |
| 3957 | retval = 0; |
| 3958 | } |
| 3959 | else |
| 3960 | { |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 3961 | #ifdef FEAT_RELTIME |
| 3962 | int tm_count = 0; |
| 3963 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3964 | /* Messy cases: unanchored match. */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 3965 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3966 | { |
| 3967 | if (prog->regstart != NUL) |
| 3968 | { |
Bram Moolenaar | 05159a0 | 2005-02-26 23:04:13 +0000 | [diff] [blame] | 3969 | /* Skip until the char we know it must start with. |
| 3970 | * Used often, do some work to avoid call overhead. */ |
| 3971 | if (!ireg_ic |
| 3972 | #ifdef FEAT_MBYTE |
| 3973 | && !has_mbyte |
| 3974 | #endif |
| 3975 | ) |
| 3976 | s = vim_strbyte(regline + col, prog->regstart); |
| 3977 | else |
| 3978 | s = cstrchr(regline + col, prog->regstart); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3979 | if (s == NULL) |
| 3980 | { |
| 3981 | retval = 0; |
| 3982 | break; |
| 3983 | } |
| 3984 | col = (int)(s - regline); |
| 3985 | } |
| 3986 | |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 3987 | /* Check for maximum column to try. */ |
| 3988 | if (ireg_maxcol > 0 && col >= ireg_maxcol) |
| 3989 | { |
| 3990 | retval = 0; |
| 3991 | break; |
| 3992 | } |
| 3993 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3994 | retval = regtry(prog, col); |
| 3995 | if (retval > 0) |
| 3996 | break; |
| 3997 | |
| 3998 | /* if not currently on the first line, get it again */ |
| 3999 | if (reglnum != 0) |
| 4000 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4001 | reglnum = 0; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 4002 | regline = reg_getline((linenr_T)0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4003 | } |
| 4004 | if (regline[col] == NUL) |
| 4005 | break; |
| 4006 | #ifdef FEAT_MBYTE |
| 4007 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4008 | col += (*mb_ptr2len)(regline + col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4009 | else |
| 4010 | #endif |
| 4011 | ++col; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 4012 | #ifdef FEAT_RELTIME |
| 4013 | /* Check for timeout once in a twenty times to avoid overhead. */ |
| 4014 | if (tm != NULL && ++tm_count == 20) |
| 4015 | { |
| 4016 | tm_count = 0; |
| 4017 | if (profile_passed_limit(tm)) |
| 4018 | break; |
| 4019 | } |
| 4020 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4021 | } |
| 4022 | } |
| 4023 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4024 | theend: |
Bram Moolenaar | 4bad6c8 | 2008-01-18 19:37:23 +0000 | [diff] [blame] | 4025 | /* Free "reg_tofree" when it's a bit big. |
| 4026 | * Free regstack and backpos if they are bigger than their initial size. */ |
| 4027 | if (reg_tofreelen > 400) |
| 4028 | { |
| 4029 | vim_free(reg_tofree); |
| 4030 | reg_tofree = NULL; |
| 4031 | } |
| 4032 | if (regstack.ga_maxlen > REGSTACK_INITIAL) |
| 4033 | ga_clear(®stack); |
| 4034 | if (backpos.ga_maxlen > BACKPOS_INITIAL) |
| 4035 | ga_clear(&backpos); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 4036 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4037 | return retval; |
| 4038 | } |
| 4039 | |
| 4040 | #ifdef FEAT_SYN_HL |
| 4041 | static reg_extmatch_T *make_extmatch __ARGS((void)); |
| 4042 | |
| 4043 | /* |
| 4044 | * Create a new extmatch and mark it as referenced once. |
| 4045 | */ |
| 4046 | static reg_extmatch_T * |
| 4047 | make_extmatch() |
| 4048 | { |
| 4049 | reg_extmatch_T *em; |
| 4050 | |
| 4051 | em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T)); |
| 4052 | if (em != NULL) |
| 4053 | em->refcnt = 1; |
| 4054 | return em; |
| 4055 | } |
| 4056 | |
| 4057 | /* |
| 4058 | * Add a reference to an extmatch. |
| 4059 | */ |
| 4060 | reg_extmatch_T * |
| 4061 | ref_extmatch(em) |
| 4062 | reg_extmatch_T *em; |
| 4063 | { |
| 4064 | if (em != NULL) |
| 4065 | em->refcnt++; |
| 4066 | return em; |
| 4067 | } |
| 4068 | |
| 4069 | /* |
| 4070 | * Remove a reference to an extmatch. If there are no references left, free |
| 4071 | * the info. |
| 4072 | */ |
| 4073 | void |
| 4074 | unref_extmatch(em) |
| 4075 | reg_extmatch_T *em; |
| 4076 | { |
| 4077 | int i; |
| 4078 | |
| 4079 | if (em != NULL && --em->refcnt <= 0) |
| 4080 | { |
| 4081 | for (i = 0; i < NSUBEXP; ++i) |
| 4082 | vim_free(em->matches[i]); |
| 4083 | vim_free(em); |
| 4084 | } |
| 4085 | } |
| 4086 | #endif |
| 4087 | |
| 4088 | /* |
| 4089 | * regtry - try match of "prog" with at regline["col"]. |
| 4090 | * Returns 0 for failure, number of lines contained in the match otherwise. |
| 4091 | */ |
| 4092 | static long |
| 4093 | regtry(prog, col) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4094 | bt_regprog_T *prog; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4095 | colnr_T col; |
| 4096 | { |
| 4097 | reginput = regline + col; |
| 4098 | need_clear_subexpr = TRUE; |
| 4099 | #ifdef FEAT_SYN_HL |
| 4100 | /* Clear the external match subpointers if necessary. */ |
| 4101 | if (prog->reghasz == REX_SET) |
| 4102 | need_clear_zsubexpr = TRUE; |
| 4103 | #endif |
| 4104 | |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4105 | if (regmatch(prog->program + 1) == 0) |
| 4106 | return 0; |
| 4107 | |
| 4108 | cleanup_subexpr(); |
| 4109 | if (REG_MULTI) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4110 | { |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4111 | if (reg_startpos[0].lnum < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4112 | { |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4113 | reg_startpos[0].lnum = 0; |
| 4114 | reg_startpos[0].col = col; |
| 4115 | } |
| 4116 | if (reg_endpos[0].lnum < 0) |
| 4117 | { |
| 4118 | reg_endpos[0].lnum = reglnum; |
| 4119 | reg_endpos[0].col = (int)(reginput - regline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4120 | } |
| 4121 | else |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4122 | /* Use line number of "\ze". */ |
| 4123 | reglnum = reg_endpos[0].lnum; |
| 4124 | } |
| 4125 | else |
| 4126 | { |
| 4127 | if (reg_startp[0] == NULL) |
| 4128 | reg_startp[0] = regline + col; |
| 4129 | if (reg_endp[0] == NULL) |
| 4130 | reg_endp[0] = reginput; |
| 4131 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4132 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4133 | /* Package any found \z(...\) matches for export. Default is none. */ |
| 4134 | unref_extmatch(re_extmatch_out); |
| 4135 | re_extmatch_out = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4136 | |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4137 | if (prog->reghasz == REX_SET) |
| 4138 | { |
| 4139 | int i; |
| 4140 | |
| 4141 | cleanup_zsubexpr(); |
| 4142 | re_extmatch_out = make_extmatch(); |
| 4143 | for (i = 0; i < NSUBEXP; i++) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4144 | { |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4145 | if (REG_MULTI) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4146 | { |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4147 | /* Only accept single line matches. */ |
| 4148 | if (reg_startzpos[i].lnum >= 0 |
| 4149 | && reg_endzpos[i].lnum == reg_startzpos[i].lnum) |
| 4150 | re_extmatch_out->matches[i] = |
| 4151 | vim_strnsave(reg_getline(reg_startzpos[i].lnum) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4152 | + reg_startzpos[i].col, |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4153 | reg_endzpos[i].col - reg_startzpos[i].col); |
| 4154 | } |
| 4155 | else |
| 4156 | { |
| 4157 | if (reg_startzp[i] != NULL && reg_endzp[i] != NULL) |
| 4158 | re_extmatch_out->matches[i] = |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4159 | vim_strnsave(reg_startzp[i], |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4160 | (int)(reg_endzp[i] - reg_startzp[i])); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4161 | } |
| 4162 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4163 | } |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 4164 | #endif |
| 4165 | return 1 + reglnum; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4166 | } |
| 4167 | |
| 4168 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4169 | static int reg_prev_class __ARGS((void)); |
| 4170 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4171 | /* |
| 4172 | * Get class of previous character. |
| 4173 | */ |
| 4174 | static int |
| 4175 | reg_prev_class() |
| 4176 | { |
| 4177 | if (reginput > regline) |
Bram Moolenaar | f813a18 | 2013-01-30 13:59:37 +0100 | [diff] [blame] | 4178 | return mb_get_class_buf(reginput - 1 |
| 4179 | - (*mb_head_off)(regline, reginput - 1), reg_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4180 | return -1; |
| 4181 | } |
| 4182 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4183 | #endif |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 4184 | #ifdef FEAT_VISUAL |
| 4185 | static int reg_match_visual __ARGS((void)); |
| 4186 | |
| 4187 | /* |
| 4188 | * Return TRUE if the current reginput position matches the Visual area. |
| 4189 | */ |
| 4190 | static int |
| 4191 | reg_match_visual() |
| 4192 | { |
| 4193 | pos_T top, bot; |
| 4194 | linenr_T lnum; |
| 4195 | colnr_T col; |
| 4196 | win_T *wp = reg_win == NULL ? curwin : reg_win; |
| 4197 | int mode; |
| 4198 | colnr_T start, end; |
| 4199 | colnr_T start2, end2; |
| 4200 | colnr_T cols; |
| 4201 | |
| 4202 | /* Check if the buffer is the current buffer. */ |
| 4203 | if (reg_buf != curbuf || VIsual.lnum == 0) |
| 4204 | return FALSE; |
| 4205 | |
| 4206 | if (VIsual_active) |
| 4207 | { |
| 4208 | if (lt(VIsual, wp->w_cursor)) |
| 4209 | { |
| 4210 | top = VIsual; |
| 4211 | bot = wp->w_cursor; |
| 4212 | } |
| 4213 | else |
| 4214 | { |
| 4215 | top = wp->w_cursor; |
| 4216 | bot = VIsual; |
| 4217 | } |
| 4218 | mode = VIsual_mode; |
| 4219 | } |
| 4220 | else |
| 4221 | { |
| 4222 | if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end)) |
| 4223 | { |
| 4224 | top = curbuf->b_visual.vi_start; |
| 4225 | bot = curbuf->b_visual.vi_end; |
| 4226 | } |
| 4227 | else |
| 4228 | { |
| 4229 | top = curbuf->b_visual.vi_end; |
| 4230 | bot = curbuf->b_visual.vi_start; |
| 4231 | } |
| 4232 | mode = curbuf->b_visual.vi_mode; |
| 4233 | } |
| 4234 | lnum = reglnum + reg_firstlnum; |
| 4235 | if (lnum < top.lnum || lnum > bot.lnum) |
| 4236 | return FALSE; |
| 4237 | |
| 4238 | if (mode == 'v') |
| 4239 | { |
| 4240 | col = (colnr_T)(reginput - regline); |
| 4241 | if ((lnum == top.lnum && col < top.col) |
| 4242 | || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e'))) |
| 4243 | return FALSE; |
| 4244 | } |
| 4245 | else if (mode == Ctrl_V) |
| 4246 | { |
| 4247 | getvvcol(wp, &top, &start, NULL, &end); |
| 4248 | getvvcol(wp, &bot, &start2, NULL, &end2); |
| 4249 | if (start2 < start) |
| 4250 | start = start2; |
| 4251 | if (end2 > end) |
| 4252 | end = end2; |
| 4253 | if (top.col == MAXCOL || bot.col == MAXCOL) |
| 4254 | end = MAXCOL; |
| 4255 | cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline)); |
| 4256 | if (cols < start || cols > end - (*p_sel == 'e')) |
| 4257 | return FALSE; |
| 4258 | } |
| 4259 | return TRUE; |
| 4260 | } |
| 4261 | #endif |
| 4262 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4263 | #define ADVANCE_REGINPUT() mb_ptr_adv(reginput) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4264 | |
| 4265 | /* |
| 4266 | * The arguments from BRACE_LIMITS are stored here. They are actually local |
| 4267 | * to regmatch(), but they are here to reduce the amount of stack space used |
| 4268 | * (it can be called recursively many times). |
| 4269 | */ |
| 4270 | static long bl_minval; |
| 4271 | static long bl_maxval; |
| 4272 | |
| 4273 | /* |
| 4274 | * regmatch - main matching routine |
| 4275 | * |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4276 | * Conceptually the strategy is simple: Check to see whether the current node |
| 4277 | * matches, push an item onto the regstack and loop to see whether the rest |
| 4278 | * matches, and then act accordingly. In practice we make some effort to |
| 4279 | * avoid using the regstack, in particular by going through "ordinary" nodes |
| 4280 | * (that don't need to know whether the rest of the match failed) by a nested |
| 4281 | * loop. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4282 | * |
| 4283 | * Returns TRUE when there is a match. Leaves reginput and reglnum just after |
| 4284 | * the last matched character. |
| 4285 | * Returns FALSE when there is no match. Leaves reginput and reglnum in an |
| 4286 | * undefined state! |
| 4287 | */ |
| 4288 | static int |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4289 | regmatch(scan) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4290 | char_u *scan; /* Current node. */ |
| 4291 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4292 | char_u *next; /* Next node. */ |
| 4293 | int op; |
| 4294 | int c; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4295 | regitem_T *rp; |
| 4296 | int no; |
| 4297 | int status; /* one of the RA_ values: */ |
| 4298 | #define RA_FAIL 1 /* something failed, abort */ |
| 4299 | #define RA_CONT 2 /* continue in inner loop */ |
| 4300 | #define RA_BREAK 3 /* break inner loop */ |
| 4301 | #define RA_MATCH 4 /* successful match */ |
| 4302 | #define RA_NOMATCH 5 /* didn't match */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4303 | |
Bram Moolenaar | 4bad6c8 | 2008-01-18 19:37:23 +0000 | [diff] [blame] | 4304 | /* Make "regstack" and "backpos" empty. They are allocated and freed in |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4305 | * bt_regexec_both() to reduce malloc()/free() calls. */ |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 4306 | regstack.ga_len = 0; |
| 4307 | backpos.ga_len = 0; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 4308 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4309 | /* |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 4310 | * Repeat until "regstack" is empty. |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4311 | */ |
| 4312 | for (;;) |
| 4313 | { |
Bram Moolenaar | 41f1205 | 2013-08-25 17:01:42 +0200 | [diff] [blame] | 4314 | /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q". |
| 4315 | * Allow interrupting them with CTRL-C. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4316 | fast_breakcheck(); |
| 4317 | |
| 4318 | #ifdef DEBUG |
| 4319 | if (scan != NULL && regnarrate) |
| 4320 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4321 | mch_errmsg((char *)regprop(scan)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4322 | mch_errmsg("(\n"); |
| 4323 | } |
| 4324 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4325 | |
| 4326 | /* |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 4327 | * Repeat for items that can be matched sequentially, without using the |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4328 | * regstack. |
| 4329 | */ |
| 4330 | for (;;) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4331 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4332 | if (got_int || scan == NULL) |
| 4333 | { |
| 4334 | status = RA_FAIL; |
| 4335 | break; |
| 4336 | } |
| 4337 | status = RA_CONT; |
| 4338 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4339 | #ifdef DEBUG |
| 4340 | if (regnarrate) |
| 4341 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4342 | mch_errmsg((char *)regprop(scan)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4343 | mch_errmsg("...\n"); |
| 4344 | # ifdef FEAT_SYN_HL |
| 4345 | if (re_extmatch_in != NULL) |
| 4346 | { |
| 4347 | int i; |
| 4348 | |
| 4349 | mch_errmsg(_("External submatches:\n")); |
| 4350 | for (i = 0; i < NSUBEXP; i++) |
| 4351 | { |
| 4352 | mch_errmsg(" \""); |
| 4353 | if (re_extmatch_in->matches[i] != NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 4354 | mch_errmsg((char *)re_extmatch_in->matches[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4355 | mch_errmsg("\"\n"); |
| 4356 | } |
| 4357 | } |
| 4358 | # endif |
| 4359 | } |
| 4360 | #endif |
| 4361 | next = regnext(scan); |
| 4362 | |
| 4363 | op = OP(scan); |
| 4364 | /* Check for character class with NL added. */ |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 4365 | if (!reg_line_lbr && WITH_NL(op) && REG_MULTI |
| 4366 | && *reginput == NUL && reglnum <= reg_maxline) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4367 | { |
| 4368 | reg_nextline(); |
| 4369 | } |
| 4370 | else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n') |
| 4371 | { |
| 4372 | ADVANCE_REGINPUT(); |
| 4373 | } |
| 4374 | else |
| 4375 | { |
| 4376 | if (WITH_NL(op)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4377 | op -= ADD_NL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4378 | #ifdef FEAT_MBYTE |
| 4379 | if (has_mbyte) |
| 4380 | c = (*mb_ptr2char)(reginput); |
| 4381 | else |
| 4382 | #endif |
| 4383 | c = *reginput; |
| 4384 | switch (op) |
| 4385 | { |
| 4386 | case BOL: |
| 4387 | if (reginput != regline) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4388 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4389 | break; |
| 4390 | |
| 4391 | case EOL: |
| 4392 | if (c != NUL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4393 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4394 | break; |
| 4395 | |
| 4396 | case RE_BOF: |
Bram Moolenaar | a713933 | 2007-12-09 18:26:22 +0000 | [diff] [blame] | 4397 | /* We're not at the beginning of the file when below the first |
| 4398 | * line where we started, not at the start of the line or we |
| 4399 | * didn't start at the first line of the buffer. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4400 | if (reglnum != 0 || reginput != regline |
Bram Moolenaar | a713933 | 2007-12-09 18:26:22 +0000 | [diff] [blame] | 4401 | || (REG_MULTI && reg_firstlnum > 1)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4402 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4403 | break; |
| 4404 | |
| 4405 | case RE_EOF: |
| 4406 | if (reglnum != reg_maxline || c != NUL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4407 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4408 | break; |
| 4409 | |
| 4410 | case CURSOR: |
| 4411 | /* Check if the buffer is in a window and compare the |
| 4412 | * reg_win->w_cursor position to the match position. */ |
| 4413 | if (reg_win == NULL |
| 4414 | || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum) |
| 4415 | || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4416 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4417 | break; |
| 4418 | |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 4419 | case RE_MARK: |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 4420 | /* Compare the mark position to the match position. */ |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 4421 | { |
| 4422 | int mark = OPERAND(scan)[0]; |
| 4423 | int cmp = OPERAND(scan)[1]; |
| 4424 | pos_T *pos; |
| 4425 | |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 4426 | pos = getmark_buf(reg_buf, mark, FALSE); |
Bram Moolenaar | e9400a4 | 2007-05-06 13:04:32 +0000 | [diff] [blame] | 4427 | if (pos == NULL /* mark doesn't exist */ |
Bram Moolenaar | 044aa29 | 2013-06-04 21:27:38 +0200 | [diff] [blame] | 4428 | || pos->lnum <= 0 /* mark isn't set in reg_buf */ |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 4429 | || (pos->lnum == reglnum + reg_firstlnum |
| 4430 | ? (pos->col == (colnr_T)(reginput - regline) |
| 4431 | ? (cmp == '<' || cmp == '>') |
| 4432 | : (pos->col < (colnr_T)(reginput - regline) |
| 4433 | ? cmp != '>' |
| 4434 | : cmp != '<')) |
| 4435 | : (pos->lnum < reglnum + reg_firstlnum |
| 4436 | ? cmp != '>' |
| 4437 | : cmp != '<'))) |
| 4438 | status = RA_NOMATCH; |
| 4439 | } |
| 4440 | break; |
| 4441 | |
| 4442 | case RE_VISUAL: |
| 4443 | #ifdef FEAT_VISUAL |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 4444 | if (!reg_match_visual()) |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 4445 | #endif |
Bram Moolenaar | dacd7de | 2013-06-04 18:28:48 +0200 | [diff] [blame] | 4446 | status = RA_NOMATCH; |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 4447 | break; |
| 4448 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4449 | case RE_LNUM: |
| 4450 | if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum), |
| 4451 | scan)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4452 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4453 | break; |
| 4454 | |
| 4455 | case RE_COL: |
| 4456 | if (!re_num_cmp((long_u)(reginput - regline) + 1, scan)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4457 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4458 | break; |
| 4459 | |
| 4460 | case RE_VCOL: |
| 4461 | if (!re_num_cmp((long_u)win_linetabsize( |
| 4462 | reg_win == NULL ? curwin : reg_win, |
| 4463 | regline, (colnr_T)(reginput - regline)) + 1, scan)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4464 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4465 | break; |
| 4466 | |
| 4467 | case BOW: /* \<word; reginput points to w */ |
| 4468 | if (c == NUL) /* Can't match at end of line */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4469 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4470 | #ifdef FEAT_MBYTE |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4471 | else if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4472 | { |
| 4473 | int this_class; |
| 4474 | |
| 4475 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f813a18 | 2013-01-30 13:59:37 +0100 | [diff] [blame] | 4476 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4477 | if (this_class <= 1) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4478 | status = RA_NOMATCH; /* not on a word at all */ |
| 4479 | else if (reg_prev_class() == this_class) |
| 4480 | status = RA_NOMATCH; /* previous char is in same word */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4481 | } |
| 4482 | #endif |
| 4483 | else |
| 4484 | { |
Bram Moolenaar | 2f315ab | 2013-01-25 20:11:01 +0100 | [diff] [blame] | 4485 | if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline |
| 4486 | && vim_iswordc_buf(reginput[-1], reg_buf))) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4487 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4488 | } |
| 4489 | break; |
| 4490 | |
| 4491 | case EOW: /* word\>; reginput points after d */ |
| 4492 | if (reginput == regline) /* Can't match at start of line */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4493 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4494 | #ifdef FEAT_MBYTE |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4495 | else if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4496 | { |
| 4497 | int this_class, prev_class; |
| 4498 | |
| 4499 | /* Get class of current and previous char (if it exists). */ |
Bram Moolenaar | f813a18 | 2013-01-30 13:59:37 +0100 | [diff] [blame] | 4500 | this_class = mb_get_class_buf(reginput, reg_buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4501 | prev_class = reg_prev_class(); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4502 | if (this_class == prev_class |
| 4503 | || prev_class == 0 || prev_class == 1) |
| 4504 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4505 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4506 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4507 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4508 | { |
Bram Moolenaar | 9d182dd | 2013-01-23 15:53:15 +0100 | [diff] [blame] | 4509 | if (!vim_iswordc_buf(reginput[-1], reg_buf) |
| 4510 | || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf))) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4511 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4512 | } |
| 4513 | break; /* Matched with EOW */ |
| 4514 | |
| 4515 | case ANY: |
Bram Moolenaar | e337e5f | 2013-01-30 18:21:51 +0100 | [diff] [blame] | 4516 | /* ANY does not match new lines. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4517 | if (c == NUL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4518 | status = RA_NOMATCH; |
| 4519 | else |
| 4520 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4521 | break; |
| 4522 | |
| 4523 | case IDENT: |
| 4524 | if (!vim_isIDc(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4525 | status = RA_NOMATCH; |
| 4526 | else |
| 4527 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4528 | break; |
| 4529 | |
| 4530 | case SIDENT: |
| 4531 | if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4532 | status = RA_NOMATCH; |
| 4533 | else |
| 4534 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4535 | break; |
| 4536 | |
| 4537 | case KWORD: |
Bram Moolenaar | f813a18 | 2013-01-30 13:59:37 +0100 | [diff] [blame] | 4538 | if (!vim_iswordp_buf(reginput, reg_buf)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4539 | status = RA_NOMATCH; |
| 4540 | else |
| 4541 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4542 | break; |
| 4543 | |
| 4544 | case SKWORD: |
Bram Moolenaar | f813a18 | 2013-01-30 13:59:37 +0100 | [diff] [blame] | 4545 | if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4546 | status = RA_NOMATCH; |
| 4547 | else |
| 4548 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4549 | break; |
| 4550 | |
| 4551 | case FNAME: |
| 4552 | if (!vim_isfilec(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4553 | status = RA_NOMATCH; |
| 4554 | else |
| 4555 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4556 | break; |
| 4557 | |
| 4558 | case SFNAME: |
| 4559 | if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4560 | status = RA_NOMATCH; |
| 4561 | else |
| 4562 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4563 | break; |
| 4564 | |
| 4565 | case PRINT: |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 4566 | if (!vim_isprintc(PTR2CHAR(reginput))) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4567 | status = RA_NOMATCH; |
| 4568 | else |
| 4569 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4570 | break; |
| 4571 | |
| 4572 | case SPRINT: |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 4573 | if (VIM_ISDIGIT(*reginput) || !vim_isprintc(PTR2CHAR(reginput))) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4574 | status = RA_NOMATCH; |
| 4575 | else |
| 4576 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4577 | break; |
| 4578 | |
| 4579 | case WHITE: |
| 4580 | if (!vim_iswhite(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4581 | status = RA_NOMATCH; |
| 4582 | else |
| 4583 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4584 | break; |
| 4585 | |
| 4586 | case NWHITE: |
| 4587 | if (c == NUL || vim_iswhite(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4588 | status = RA_NOMATCH; |
| 4589 | else |
| 4590 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4591 | break; |
| 4592 | |
| 4593 | case DIGIT: |
| 4594 | if (!ri_digit(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4595 | status = RA_NOMATCH; |
| 4596 | else |
| 4597 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4598 | break; |
| 4599 | |
| 4600 | case NDIGIT: |
| 4601 | if (c == NUL || ri_digit(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4602 | status = RA_NOMATCH; |
| 4603 | else |
| 4604 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4605 | break; |
| 4606 | |
| 4607 | case HEX: |
| 4608 | if (!ri_hex(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4609 | status = RA_NOMATCH; |
| 4610 | else |
| 4611 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4612 | break; |
| 4613 | |
| 4614 | case NHEX: |
| 4615 | if (c == NUL || ri_hex(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4616 | status = RA_NOMATCH; |
| 4617 | else |
| 4618 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4619 | break; |
| 4620 | |
| 4621 | case OCTAL: |
| 4622 | if (!ri_octal(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4623 | status = RA_NOMATCH; |
| 4624 | else |
| 4625 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4626 | break; |
| 4627 | |
| 4628 | case NOCTAL: |
| 4629 | if (c == NUL || ri_octal(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4630 | status = RA_NOMATCH; |
| 4631 | else |
| 4632 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4633 | break; |
| 4634 | |
| 4635 | case WORD: |
| 4636 | if (!ri_word(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4637 | status = RA_NOMATCH; |
| 4638 | else |
| 4639 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4640 | break; |
| 4641 | |
| 4642 | case NWORD: |
| 4643 | if (c == NUL || ri_word(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4644 | status = RA_NOMATCH; |
| 4645 | else |
| 4646 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4647 | break; |
| 4648 | |
| 4649 | case HEAD: |
| 4650 | if (!ri_head(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4651 | status = RA_NOMATCH; |
| 4652 | else |
| 4653 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4654 | break; |
| 4655 | |
| 4656 | case NHEAD: |
| 4657 | if (c == NUL || ri_head(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4658 | status = RA_NOMATCH; |
| 4659 | else |
| 4660 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4661 | break; |
| 4662 | |
| 4663 | case ALPHA: |
| 4664 | if (!ri_alpha(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4665 | status = RA_NOMATCH; |
| 4666 | else |
| 4667 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4668 | break; |
| 4669 | |
| 4670 | case NALPHA: |
| 4671 | if (c == NUL || ri_alpha(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4672 | status = RA_NOMATCH; |
| 4673 | else |
| 4674 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4675 | break; |
| 4676 | |
| 4677 | case LOWER: |
| 4678 | if (!ri_lower(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4679 | status = RA_NOMATCH; |
| 4680 | else |
| 4681 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4682 | break; |
| 4683 | |
| 4684 | case NLOWER: |
| 4685 | if (c == NUL || ri_lower(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4686 | status = RA_NOMATCH; |
| 4687 | else |
| 4688 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4689 | break; |
| 4690 | |
| 4691 | case UPPER: |
| 4692 | if (!ri_upper(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4693 | status = RA_NOMATCH; |
| 4694 | else |
| 4695 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4696 | break; |
| 4697 | |
| 4698 | case NUPPER: |
| 4699 | if (c == NUL || ri_upper(c)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4700 | status = RA_NOMATCH; |
| 4701 | else |
| 4702 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4703 | break; |
| 4704 | |
| 4705 | case EXACTLY: |
| 4706 | { |
| 4707 | int len; |
| 4708 | char_u *opnd; |
| 4709 | |
| 4710 | opnd = OPERAND(scan); |
| 4711 | /* Inline the first byte, for speed. */ |
| 4712 | if (*opnd != *reginput |
| 4713 | && (!ireg_ic || ( |
| 4714 | #ifdef FEAT_MBYTE |
| 4715 | !enc_utf8 && |
| 4716 | #endif |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 4717 | MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput)))) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4718 | status = RA_NOMATCH; |
| 4719 | else if (*opnd == NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4720 | { |
| 4721 | /* match empty string always works; happens when "~" is |
| 4722 | * empty. */ |
| 4723 | } |
| 4724 | else if (opnd[1] == NUL |
| 4725 | #ifdef FEAT_MBYTE |
| 4726 | && !(enc_utf8 && ireg_ic) |
| 4727 | #endif |
| 4728 | ) |
| 4729 | ++reginput; /* matched a single char */ |
| 4730 | else |
| 4731 | { |
| 4732 | len = (int)STRLEN(opnd); |
| 4733 | /* Need to match first byte again for multi-byte. */ |
| 4734 | if (cstrncmp(opnd, reginput, &len) != 0) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4735 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4736 | #ifdef FEAT_MBYTE |
| 4737 | /* Check for following composing character. */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4738 | else if (enc_utf8 |
| 4739 | && UTF_COMPOSINGLIKE(reginput, reginput + len)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4740 | { |
| 4741 | /* raaron: This code makes a composing character get |
| 4742 | * ignored, which is the correct behavior (sometimes) |
| 4743 | * for voweled Hebrew texts. */ |
| 4744 | if (!ireg_icombine) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4745 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4746 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4747 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4748 | else |
| 4749 | reginput += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4750 | } |
| 4751 | } |
| 4752 | break; |
| 4753 | |
| 4754 | case ANYOF: |
| 4755 | case ANYBUT: |
| 4756 | if (c == NUL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4757 | status = RA_NOMATCH; |
| 4758 | else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF)) |
| 4759 | status = RA_NOMATCH; |
| 4760 | else |
| 4761 | ADVANCE_REGINPUT(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4762 | break; |
| 4763 | |
| 4764 | #ifdef FEAT_MBYTE |
| 4765 | case MULTIBYTECODE: |
| 4766 | if (has_mbyte) |
| 4767 | { |
| 4768 | int i, len; |
| 4769 | char_u *opnd; |
Bram Moolenaar | 89d4032 | 2006-08-29 15:30:07 +0000 | [diff] [blame] | 4770 | int opndc = 0, inpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4771 | |
| 4772 | opnd = OPERAND(scan); |
| 4773 | /* Safety check (just in case 'encoding' was changed since |
| 4774 | * compiling the program). */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 4775 | if ((len = (*mb_ptr2len)(opnd)) < 2) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4776 | { |
| 4777 | status = RA_NOMATCH; |
| 4778 | break; |
| 4779 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4780 | if (enc_utf8) |
| 4781 | opndc = mb_ptr2char(opnd); |
| 4782 | if (enc_utf8 && utf_iscomposing(opndc)) |
| 4783 | { |
| 4784 | /* When only a composing char is given match at any |
| 4785 | * position where that composing char appears. */ |
| 4786 | status = RA_NOMATCH; |
| 4787 | for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4788 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4789 | inpc = mb_ptr2char(reginput + i); |
| 4790 | if (!utf_iscomposing(inpc)) |
| 4791 | { |
| 4792 | if (i > 0) |
| 4793 | break; |
| 4794 | } |
| 4795 | else if (opndc == inpc) |
| 4796 | { |
| 4797 | /* Include all following composing chars. */ |
| 4798 | len = i + mb_ptr2len(reginput + i); |
| 4799 | status = RA_MATCH; |
| 4800 | break; |
| 4801 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4802 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4803 | } |
| 4804 | else |
| 4805 | for (i = 0; i < len; ++i) |
| 4806 | if (opnd[i] != reginput[i]) |
| 4807 | { |
| 4808 | status = RA_NOMATCH; |
| 4809 | break; |
| 4810 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4811 | reginput += len; |
| 4812 | } |
| 4813 | else |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4814 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4815 | break; |
| 4816 | #endif |
| 4817 | |
| 4818 | case NOTHING: |
| 4819 | break; |
| 4820 | |
| 4821 | case BACK: |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 4822 | { |
| 4823 | int i; |
| 4824 | backpos_T *bp; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4825 | |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 4826 | /* |
| 4827 | * When we run into BACK we need to check if we don't keep |
| 4828 | * looping without matching any input. The second and later |
| 4829 | * times a BACK is encountered it fails if the input is still |
| 4830 | * at the same position as the previous time. |
| 4831 | * The positions are stored in "backpos" and found by the |
| 4832 | * current value of "scan", the position in the RE program. |
| 4833 | */ |
| 4834 | bp = (backpos_T *)backpos.ga_data; |
| 4835 | for (i = 0; i < backpos.ga_len; ++i) |
| 4836 | if (bp[i].bp_scan == scan) |
| 4837 | break; |
| 4838 | if (i == backpos.ga_len) |
| 4839 | { |
| 4840 | /* First time at this BACK, make room to store the pos. */ |
| 4841 | if (ga_grow(&backpos, 1) == FAIL) |
| 4842 | status = RA_FAIL; |
| 4843 | else |
| 4844 | { |
| 4845 | /* get "ga_data" again, it may have changed */ |
| 4846 | bp = (backpos_T *)backpos.ga_data; |
| 4847 | bp[i].bp_scan = scan; |
| 4848 | ++backpos.ga_len; |
| 4849 | } |
| 4850 | } |
| 4851 | else if (reg_save_equal(&bp[i].bp_pos)) |
| 4852 | /* Still at same position as last time, fail. */ |
| 4853 | status = RA_NOMATCH; |
| 4854 | |
| 4855 | if (status != RA_FAIL && status != RA_NOMATCH) |
| 4856 | reg_save(&bp[i].bp_pos, &backpos); |
| 4857 | } |
Bram Moolenaar | 19a09a1 | 2005-03-04 23:39:37 +0000 | [diff] [blame] | 4858 | break; |
| 4859 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4860 | case MOPEN + 0: /* Match start: \zs */ |
| 4861 | case MOPEN + 1: /* \( */ |
| 4862 | case MOPEN + 2: |
| 4863 | case MOPEN + 3: |
| 4864 | case MOPEN + 4: |
| 4865 | case MOPEN + 5: |
| 4866 | case MOPEN + 6: |
| 4867 | case MOPEN + 7: |
| 4868 | case MOPEN + 8: |
| 4869 | case MOPEN + 9: |
| 4870 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4871 | no = op - MOPEN; |
| 4872 | cleanup_subexpr(); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 4873 | rp = regstack_push(RS_MOPEN, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4874 | if (rp == NULL) |
| 4875 | status = RA_FAIL; |
| 4876 | else |
| 4877 | { |
| 4878 | rp->rs_no = no; |
| 4879 | save_se(&rp->rs_un.sesave, ®_startpos[no], |
| 4880 | ®_startp[no]); |
| 4881 | /* We simply continue and handle the result when done. */ |
| 4882 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4883 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4884 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4885 | |
| 4886 | case NOPEN: /* \%( */ |
| 4887 | case NCLOSE: /* \) after \%( */ |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 4888 | if (regstack_push(RS_NOPEN, scan) == NULL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4889 | status = RA_FAIL; |
| 4890 | /* We simply continue and handle the result when done. */ |
| 4891 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4892 | |
| 4893 | #ifdef FEAT_SYN_HL |
| 4894 | case ZOPEN + 1: |
| 4895 | case ZOPEN + 2: |
| 4896 | case ZOPEN + 3: |
| 4897 | case ZOPEN + 4: |
| 4898 | case ZOPEN + 5: |
| 4899 | case ZOPEN + 6: |
| 4900 | case ZOPEN + 7: |
| 4901 | case ZOPEN + 8: |
| 4902 | case ZOPEN + 9: |
| 4903 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4904 | no = op - ZOPEN; |
| 4905 | cleanup_zsubexpr(); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 4906 | rp = regstack_push(RS_ZOPEN, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4907 | if (rp == NULL) |
| 4908 | status = RA_FAIL; |
| 4909 | else |
| 4910 | { |
| 4911 | rp->rs_no = no; |
| 4912 | save_se(&rp->rs_un.sesave, ®_startzpos[no], |
| 4913 | ®_startzp[no]); |
| 4914 | /* We simply continue and handle the result when done. */ |
| 4915 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4916 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4917 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4918 | #endif |
| 4919 | |
| 4920 | case MCLOSE + 0: /* Match end: \ze */ |
| 4921 | case MCLOSE + 1: /* \) */ |
| 4922 | case MCLOSE + 2: |
| 4923 | case MCLOSE + 3: |
| 4924 | case MCLOSE + 4: |
| 4925 | case MCLOSE + 5: |
| 4926 | case MCLOSE + 6: |
| 4927 | case MCLOSE + 7: |
| 4928 | case MCLOSE + 8: |
| 4929 | case MCLOSE + 9: |
| 4930 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4931 | no = op - MCLOSE; |
| 4932 | cleanup_subexpr(); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 4933 | rp = regstack_push(RS_MCLOSE, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4934 | if (rp == NULL) |
| 4935 | status = RA_FAIL; |
| 4936 | else |
| 4937 | { |
| 4938 | rp->rs_no = no; |
| 4939 | save_se(&rp->rs_un.sesave, ®_endpos[no], ®_endp[no]); |
| 4940 | /* We simply continue and handle the result when done. */ |
| 4941 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4942 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4943 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4944 | |
| 4945 | #ifdef FEAT_SYN_HL |
| 4946 | case ZCLOSE + 1: /* \) after \z( */ |
| 4947 | case ZCLOSE + 2: |
| 4948 | case ZCLOSE + 3: |
| 4949 | case ZCLOSE + 4: |
| 4950 | case ZCLOSE + 5: |
| 4951 | case ZCLOSE + 6: |
| 4952 | case ZCLOSE + 7: |
| 4953 | case ZCLOSE + 8: |
| 4954 | case ZCLOSE + 9: |
| 4955 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4956 | no = op - ZCLOSE; |
| 4957 | cleanup_zsubexpr(); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 4958 | rp = regstack_push(RS_ZCLOSE, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4959 | if (rp == NULL) |
| 4960 | status = RA_FAIL; |
| 4961 | else |
| 4962 | { |
| 4963 | rp->rs_no = no; |
| 4964 | save_se(&rp->rs_un.sesave, ®_endzpos[no], |
| 4965 | ®_endzp[no]); |
| 4966 | /* We simply continue and handle the result when done. */ |
| 4967 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4968 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4969 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4970 | #endif |
| 4971 | |
| 4972 | case BACKREF + 1: |
| 4973 | case BACKREF + 2: |
| 4974 | case BACKREF + 3: |
| 4975 | case BACKREF + 4: |
| 4976 | case BACKREF + 5: |
| 4977 | case BACKREF + 6: |
| 4978 | case BACKREF + 7: |
| 4979 | case BACKREF + 8: |
| 4980 | case BACKREF + 9: |
| 4981 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4982 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4983 | |
| 4984 | no = op - BACKREF; |
| 4985 | cleanup_subexpr(); |
| 4986 | if (!REG_MULTI) /* Single-line regexp */ |
| 4987 | { |
Bram Moolenaar | 7670fa0 | 2009-02-21 21:04:20 +0000 | [diff] [blame] | 4988 | if (reg_startp[no] == NULL || reg_endp[no] == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4989 | { |
| 4990 | /* Backref was not set: Match an empty string. */ |
| 4991 | len = 0; |
| 4992 | } |
| 4993 | else |
| 4994 | { |
| 4995 | /* Compare current input with back-ref in the same |
| 4996 | * line. */ |
| 4997 | len = (int)(reg_endp[no] - reg_startp[no]); |
| 4998 | if (cstrncmp(reg_startp[no], reginput, &len) != 0) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 4999 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5000 | } |
| 5001 | } |
| 5002 | else /* Multi-line regexp */ |
| 5003 | { |
Bram Moolenaar | 7670fa0 | 2009-02-21 21:04:20 +0000 | [diff] [blame] | 5004 | if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5005 | { |
| 5006 | /* Backref was not set: Match an empty string. */ |
| 5007 | len = 0; |
| 5008 | } |
| 5009 | else |
| 5010 | { |
| 5011 | if (reg_startpos[no].lnum == reglnum |
| 5012 | && reg_endpos[no].lnum == reglnum) |
| 5013 | { |
| 5014 | /* Compare back-ref within the current line. */ |
| 5015 | len = reg_endpos[no].col - reg_startpos[no].col; |
| 5016 | if (cstrncmp(regline + reg_startpos[no].col, |
| 5017 | reginput, &len) != 0) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5018 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | } |
| 5020 | else |
| 5021 | { |
| 5022 | /* Messy situation: Need to compare between two |
| 5023 | * lines. */ |
Bram Moolenaar | 141f6bb | 2013-06-15 15:09:50 +0200 | [diff] [blame] | 5024 | int r = match_with_backref( |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 5025 | reg_startpos[no].lnum, |
| 5026 | reg_startpos[no].col, |
| 5027 | reg_endpos[no].lnum, |
| 5028 | reg_endpos[no].col, |
Bram Moolenaar | 4cff8fa | 2013-06-14 22:48:54 +0200 | [diff] [blame] | 5029 | &len); |
Bram Moolenaar | 141f6bb | 2013-06-15 15:09:50 +0200 | [diff] [blame] | 5030 | |
| 5031 | if (r != RA_MATCH) |
| 5032 | status = r; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5033 | } |
| 5034 | } |
| 5035 | } |
| 5036 | |
| 5037 | /* Matched the backref, skip over it. */ |
| 5038 | reginput += len; |
| 5039 | } |
| 5040 | break; |
| 5041 | |
| 5042 | #ifdef FEAT_SYN_HL |
| 5043 | case ZREF + 1: |
| 5044 | case ZREF + 2: |
| 5045 | case ZREF + 3: |
| 5046 | case ZREF + 4: |
| 5047 | case ZREF + 5: |
| 5048 | case ZREF + 6: |
| 5049 | case ZREF + 7: |
| 5050 | case ZREF + 8: |
| 5051 | case ZREF + 9: |
| 5052 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5053 | int len; |
| 5054 | |
| 5055 | cleanup_zsubexpr(); |
| 5056 | no = op - ZREF; |
| 5057 | if (re_extmatch_in != NULL |
| 5058 | && re_extmatch_in->matches[no] != NULL) |
| 5059 | { |
| 5060 | len = (int)STRLEN(re_extmatch_in->matches[no]); |
| 5061 | if (cstrncmp(re_extmatch_in->matches[no], |
| 5062 | reginput, &len) != 0) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5063 | status = RA_NOMATCH; |
| 5064 | else |
| 5065 | reginput += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5066 | } |
| 5067 | else |
| 5068 | { |
| 5069 | /* Backref was not set: Match an empty string. */ |
| 5070 | } |
| 5071 | } |
| 5072 | break; |
| 5073 | #endif |
| 5074 | |
| 5075 | case BRANCH: |
| 5076 | { |
| 5077 | if (OP(next) != BRANCH) /* No choice. */ |
| 5078 | next = OPERAND(scan); /* Avoid recursion. */ |
| 5079 | else |
| 5080 | { |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5081 | rp = regstack_push(RS_BRANCH, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5082 | if (rp == NULL) |
| 5083 | status = RA_FAIL; |
| 5084 | else |
| 5085 | status = RA_BREAK; /* rest is below */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5086 | } |
| 5087 | } |
| 5088 | break; |
| 5089 | |
| 5090 | case BRACE_LIMITS: |
| 5091 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5092 | if (OP(next) == BRACE_SIMPLE) |
| 5093 | { |
| 5094 | bl_minval = OPERAND_MIN(scan); |
| 5095 | bl_maxval = OPERAND_MAX(scan); |
| 5096 | } |
| 5097 | else if (OP(next) >= BRACE_COMPLEX |
| 5098 | && OP(next) < BRACE_COMPLEX + 10) |
| 5099 | { |
| 5100 | no = OP(next) - BRACE_COMPLEX; |
| 5101 | brace_min[no] = OPERAND_MIN(scan); |
| 5102 | brace_max[no] = OPERAND_MAX(scan); |
| 5103 | brace_count[no] = 0; |
| 5104 | } |
| 5105 | else |
| 5106 | { |
| 5107 | EMSG(_(e_internal)); /* Shouldn't happen */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5108 | status = RA_FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | } |
| 5110 | } |
| 5111 | break; |
| 5112 | |
| 5113 | case BRACE_COMPLEX + 0: |
| 5114 | case BRACE_COMPLEX + 1: |
| 5115 | case BRACE_COMPLEX + 2: |
| 5116 | case BRACE_COMPLEX + 3: |
| 5117 | case BRACE_COMPLEX + 4: |
| 5118 | case BRACE_COMPLEX + 5: |
| 5119 | case BRACE_COMPLEX + 6: |
| 5120 | case BRACE_COMPLEX + 7: |
| 5121 | case BRACE_COMPLEX + 8: |
| 5122 | case BRACE_COMPLEX + 9: |
| 5123 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5124 | no = op - BRACE_COMPLEX; |
| 5125 | ++brace_count[no]; |
| 5126 | |
| 5127 | /* If not matched enough times yet, try one more */ |
| 5128 | if (brace_count[no] <= (brace_min[no] <= brace_max[no] |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5129 | ? brace_min[no] : brace_max[no])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5130 | { |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5131 | rp = regstack_push(RS_BRCPLX_MORE, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5132 | if (rp == NULL) |
| 5133 | status = RA_FAIL; |
| 5134 | else |
| 5135 | { |
| 5136 | rp->rs_no = no; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5137 | reg_save(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5138 | next = OPERAND(scan); |
| 5139 | /* We continue and handle the result when done. */ |
| 5140 | } |
| 5141 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | /* If matched enough times, may try matching some more */ |
| 5145 | if (brace_min[no] <= brace_max[no]) |
| 5146 | { |
| 5147 | /* Range is the normal way around, use longest match */ |
| 5148 | if (brace_count[no] <= brace_max[no]) |
| 5149 | { |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5150 | rp = regstack_push(RS_BRCPLX_LONG, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5151 | if (rp == NULL) |
| 5152 | status = RA_FAIL; |
| 5153 | else |
| 5154 | { |
| 5155 | rp->rs_no = no; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5156 | reg_save(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5157 | next = OPERAND(scan); |
| 5158 | /* We continue and handle the result when done. */ |
| 5159 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5160 | } |
| 5161 | } |
| 5162 | else |
| 5163 | { |
| 5164 | /* Range is backwards, use shortest match first */ |
| 5165 | if (brace_count[no] <= brace_min[no]) |
| 5166 | { |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5167 | rp = regstack_push(RS_BRCPLX_SHORT, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5168 | if (rp == NULL) |
| 5169 | status = RA_FAIL; |
| 5170 | else |
| 5171 | { |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5172 | reg_save(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5173 | /* We continue and handle the result when done. */ |
| 5174 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5175 | } |
| 5176 | } |
| 5177 | } |
| 5178 | break; |
| 5179 | |
| 5180 | case BRACE_SIMPLE: |
| 5181 | case STAR: |
| 5182 | case PLUS: |
| 5183 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5184 | regstar_T rst; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5185 | |
| 5186 | /* |
| 5187 | * Lookahead to avoid useless match attempts when we know |
| 5188 | * what character comes next. |
| 5189 | */ |
| 5190 | if (OP(next) == EXACTLY) |
| 5191 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5192 | rst.nextb = *OPERAND(next); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5193 | if (ireg_ic) |
| 5194 | { |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 5195 | if (MB_ISUPPER(rst.nextb)) |
| 5196 | rst.nextb_ic = MB_TOLOWER(rst.nextb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5197 | else |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 5198 | rst.nextb_ic = MB_TOUPPER(rst.nextb); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5199 | } |
| 5200 | else |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5201 | rst.nextb_ic = rst.nextb; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5202 | } |
| 5203 | else |
| 5204 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5205 | rst.nextb = NUL; |
| 5206 | rst.nextb_ic = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5207 | } |
| 5208 | if (op != BRACE_SIMPLE) |
| 5209 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5210 | rst.minval = (op == STAR) ? 0 : 1; |
| 5211 | rst.maxval = MAX_LIMIT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5212 | } |
| 5213 | else |
| 5214 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5215 | rst.minval = bl_minval; |
| 5216 | rst.maxval = bl_maxval; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | /* |
| 5220 | * When maxval > minval, try matching as much as possible, up |
| 5221 | * to maxval. When maxval < minval, try matching at least the |
| 5222 | * minimal number (since the range is backwards, that's also |
| 5223 | * maxval!). |
| 5224 | */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5225 | rst.count = regrepeat(OPERAND(scan), rst.maxval); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5226 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5227 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5228 | status = RA_FAIL; |
| 5229 | break; |
| 5230 | } |
| 5231 | if (rst.minval <= rst.maxval |
| 5232 | ? rst.count >= rst.minval : rst.count >= rst.maxval) |
| 5233 | { |
| 5234 | /* It could match. Prepare for trying to match what |
| 5235 | * follows. The code is below. Parameters are stored in |
| 5236 | * a regstar_T on the regstack. */ |
Bram Moolenaar | 916b7af | 2005-03-16 09:52:38 +0000 | [diff] [blame] | 5237 | if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 5238 | { |
| 5239 | EMSG(_(e_maxmempat)); |
| 5240 | status = RA_FAIL; |
| 5241 | } |
| 5242 | else if (ga_grow(®stack, sizeof(regstar_T)) == FAIL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5243 | status = RA_FAIL; |
| 5244 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5245 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5246 | regstack.ga_len += sizeof(regstar_T); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5247 | rp = regstack_push(rst.minval <= rst.maxval |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5248 | ? RS_STAR_LONG : RS_STAR_SHORT, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5249 | if (rp == NULL) |
| 5250 | status = RA_FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5251 | else |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5252 | { |
| 5253 | *(((regstar_T *)rp) - 1) = rst; |
| 5254 | status = RA_BREAK; /* skip the restore bits */ |
| 5255 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5256 | } |
| 5257 | } |
| 5258 | else |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5259 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5260 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5261 | } |
| 5262 | break; |
| 5263 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5264 | case NOMATCH: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5265 | case MATCH: |
| 5266 | case SUBPAT: |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5267 | rp = regstack_push(RS_NOMATCH, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5268 | if (rp == NULL) |
| 5269 | status = RA_FAIL; |
| 5270 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5271 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5272 | rp->rs_no = op; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5273 | reg_save(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5274 | next = OPERAND(scan); |
| 5275 | /* We continue and handle the result when done. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5276 | } |
| 5277 | break; |
| 5278 | |
| 5279 | case BEHIND: |
| 5280 | case NOBEHIND: |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5281 | /* Need a bit of room to store extra positions. */ |
Bram Moolenaar | 916b7af | 2005-03-16 09:52:38 +0000 | [diff] [blame] | 5282 | if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 5283 | { |
| 5284 | EMSG(_(e_maxmempat)); |
| 5285 | status = RA_FAIL; |
| 5286 | } |
| 5287 | else if (ga_grow(®stack, sizeof(regbehind_T)) == FAIL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5288 | status = RA_FAIL; |
| 5289 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5290 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5291 | regstack.ga_len += sizeof(regbehind_T); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5292 | rp = regstack_push(RS_BEHIND1, scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5293 | if (rp == NULL) |
| 5294 | status = RA_FAIL; |
| 5295 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5296 | { |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 5297 | /* Need to save the subexpr to be able to restore them |
| 5298 | * when there is a match but we don't use it. */ |
| 5299 | save_subexpr(((regbehind_T *)rp) - 1); |
| 5300 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5301 | rp->rs_no = op; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5302 | reg_save(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5303 | /* First try if what follows matches. If it does then we |
| 5304 | * check the behind match by looping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5305 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5306 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5307 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5308 | |
| 5309 | case BHPOS: |
| 5310 | if (REG_MULTI) |
| 5311 | { |
| 5312 | if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline) |
| 5313 | || behind_pos.rs_u.pos.lnum != reglnum) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5314 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5315 | } |
| 5316 | else if (behind_pos.rs_u.ptr != reginput) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5317 | status = RA_NOMATCH; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5318 | break; |
| 5319 | |
| 5320 | case NEWL: |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 5321 | if ((c != NUL || !REG_MULTI || reglnum > reg_maxline |
| 5322 | || reg_line_lbr) && (c != '\n' || !reg_line_lbr)) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5323 | status = RA_NOMATCH; |
| 5324 | else if (reg_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5325 | ADVANCE_REGINPUT(); |
| 5326 | else |
| 5327 | reg_nextline(); |
| 5328 | break; |
| 5329 | |
| 5330 | case END: |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5331 | status = RA_MATCH; /* Success! */ |
| 5332 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5333 | |
| 5334 | default: |
| 5335 | EMSG(_(e_re_corr)); |
| 5336 | #ifdef DEBUG |
| 5337 | printf("Illegal op code %d\n", op); |
| 5338 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5339 | status = RA_FAIL; |
| 5340 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5341 | } |
| 5342 | } |
| 5343 | |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5344 | /* If we can't continue sequentially, break the inner loop. */ |
| 5345 | if (status != RA_CONT) |
| 5346 | break; |
| 5347 | |
| 5348 | /* Continue in inner loop, advance to next item. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5349 | scan = next; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5350 | |
| 5351 | } /* end of inner loop */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5352 | |
| 5353 | /* |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5354 | * If there is something on the regstack execute the code for the state. |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5355 | * If the state is popped then loop and use the older state. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5356 | */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5357 | while (regstack.ga_len > 0 && status != RA_FAIL) |
| 5358 | { |
| 5359 | rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; |
| 5360 | switch (rp->rs_state) |
| 5361 | { |
| 5362 | case RS_NOPEN: |
| 5363 | /* Result is passed on as-is, simply pop the state. */ |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5364 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5365 | break; |
| 5366 | |
| 5367 | case RS_MOPEN: |
| 5368 | /* Pop the state. Restore pointers when there is no match. */ |
| 5369 | if (status == RA_NOMATCH) |
| 5370 | restore_se(&rp->rs_un.sesave, ®_startpos[rp->rs_no], |
| 5371 | ®_startp[rp->rs_no]); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5372 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5373 | break; |
| 5374 | |
| 5375 | #ifdef FEAT_SYN_HL |
| 5376 | case RS_ZOPEN: |
| 5377 | /* Pop the state. Restore pointers when there is no match. */ |
| 5378 | if (status == RA_NOMATCH) |
| 5379 | restore_se(&rp->rs_un.sesave, ®_startzpos[rp->rs_no], |
| 5380 | ®_startzp[rp->rs_no]); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5381 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5382 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5383 | #endif |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5384 | |
| 5385 | case RS_MCLOSE: |
| 5386 | /* Pop the state. Restore pointers when there is no match. */ |
| 5387 | if (status == RA_NOMATCH) |
| 5388 | restore_se(&rp->rs_un.sesave, ®_endpos[rp->rs_no], |
| 5389 | ®_endp[rp->rs_no]); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5390 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5391 | break; |
| 5392 | |
| 5393 | #ifdef FEAT_SYN_HL |
| 5394 | case RS_ZCLOSE: |
| 5395 | /* Pop the state. Restore pointers when there is no match. */ |
| 5396 | if (status == RA_NOMATCH) |
| 5397 | restore_se(&rp->rs_un.sesave, ®_endzpos[rp->rs_no], |
| 5398 | ®_endzp[rp->rs_no]); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5399 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5400 | break; |
| 5401 | #endif |
| 5402 | |
| 5403 | case RS_BRANCH: |
| 5404 | if (status == RA_MATCH) |
| 5405 | /* this branch matched, use it */ |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5406 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5407 | else |
| 5408 | { |
| 5409 | if (status != RA_BREAK) |
| 5410 | { |
| 5411 | /* After a non-matching branch: try next one. */ |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5412 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5413 | scan = rp->rs_scan; |
| 5414 | } |
| 5415 | if (scan == NULL || OP(scan) != BRANCH) |
| 5416 | { |
| 5417 | /* no more branches, didn't find a match */ |
| 5418 | status = RA_NOMATCH; |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5419 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5420 | } |
| 5421 | else |
| 5422 | { |
| 5423 | /* Prepare to try a branch. */ |
| 5424 | rp->rs_scan = regnext(scan); |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5425 | reg_save(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5426 | scan = OPERAND(scan); |
| 5427 | } |
| 5428 | } |
| 5429 | break; |
| 5430 | |
| 5431 | case RS_BRCPLX_MORE: |
| 5432 | /* Pop the state. Restore pointers when there is no match. */ |
| 5433 | if (status == RA_NOMATCH) |
| 5434 | { |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5435 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5436 | --brace_count[rp->rs_no]; /* decrement match count */ |
| 5437 | } |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5438 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5439 | break; |
| 5440 | |
| 5441 | case RS_BRCPLX_LONG: |
| 5442 | /* Pop the state. Restore pointers when there is no match. */ |
| 5443 | if (status == RA_NOMATCH) |
| 5444 | { |
| 5445 | /* There was no match, but we did find enough matches. */ |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5446 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5447 | --brace_count[rp->rs_no]; |
| 5448 | /* continue with the items after "\{}" */ |
| 5449 | status = RA_CONT; |
| 5450 | } |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5451 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5452 | if (status == RA_CONT) |
| 5453 | scan = regnext(scan); |
| 5454 | break; |
| 5455 | |
| 5456 | case RS_BRCPLX_SHORT: |
| 5457 | /* Pop the state. Restore pointers when there is no match. */ |
| 5458 | if (status == RA_NOMATCH) |
| 5459 | /* There was no match, try to match one more item. */ |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5460 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5461 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5462 | if (status == RA_NOMATCH) |
| 5463 | { |
| 5464 | scan = OPERAND(scan); |
| 5465 | status = RA_CONT; |
| 5466 | } |
| 5467 | break; |
| 5468 | |
| 5469 | case RS_NOMATCH: |
| 5470 | /* Pop the state. If the operand matches for NOMATCH or |
| 5471 | * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup, |
| 5472 | * except for SUBPAT, and continue with the next item. */ |
| 5473 | if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH)) |
| 5474 | status = RA_NOMATCH; |
| 5475 | else |
| 5476 | { |
| 5477 | status = RA_CONT; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5478 | if (rp->rs_no != SUBPAT) /* zero-width */ |
| 5479 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5480 | } |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5481 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5482 | if (status == RA_CONT) |
| 5483 | scan = regnext(scan); |
| 5484 | break; |
| 5485 | |
| 5486 | case RS_BEHIND1: |
| 5487 | if (status == RA_NOMATCH) |
| 5488 | { |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5489 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5490 | regstack.ga_len -= sizeof(regbehind_T); |
| 5491 | } |
| 5492 | else |
| 5493 | { |
| 5494 | /* The stuff after BEHIND/NOBEHIND matches. Now try if |
| 5495 | * the behind part does (not) match before the current |
| 5496 | * position in the input. This must be done at every |
| 5497 | * position in the input and checking if the match ends at |
| 5498 | * the current position. */ |
| 5499 | |
| 5500 | /* save the position after the found match for next */ |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5501 | reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5502 | |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5503 | /* Start looking for a match with operand at the current |
Bram Moolenaar | 7aa9f6a | 2007-05-10 18:00:30 +0000 | [diff] [blame] | 5504 | * position. Go back one character until we find the |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5505 | * result, hitting the start of the line or the previous |
| 5506 | * line (for multi-line matching). |
| 5507 | * Set behind_pos to where the match should end, BHPOS |
| 5508 | * will match it. Save the current value. */ |
| 5509 | (((regbehind_T *)rp) - 1)->save_behind = behind_pos; |
| 5510 | behind_pos = rp->rs_un.regsave; |
| 5511 | |
| 5512 | rp->rs_state = RS_BEHIND2; |
| 5513 | |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5514 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5515 | scan = OPERAND(rp->rs_scan) + 4; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5516 | } |
| 5517 | break; |
| 5518 | |
| 5519 | case RS_BEHIND2: |
| 5520 | /* |
| 5521 | * Looping for BEHIND / NOBEHIND match. |
| 5522 | */ |
| 5523 | if (status == RA_MATCH && reg_save_equal(&behind_pos)) |
| 5524 | { |
| 5525 | /* found a match that ends where "next" started */ |
| 5526 | behind_pos = (((regbehind_T *)rp) - 1)->save_behind; |
| 5527 | if (rp->rs_no == BEHIND) |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5528 | reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
| 5529 | &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5530 | else |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 5531 | { |
| 5532 | /* But we didn't want a match. Need to restore the |
| 5533 | * subexpr, because what follows matched, so they have |
| 5534 | * been set. */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5535 | status = RA_NOMATCH; |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 5536 | restore_subexpr(((regbehind_T *)rp) - 1); |
| 5537 | } |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5538 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5539 | regstack.ga_len -= sizeof(regbehind_T); |
| 5540 | } |
| 5541 | else |
| 5542 | { |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5543 | long limit; |
| 5544 | |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 5545 | /* No match or a match that doesn't end where we want it: Go |
| 5546 | * back one character. May go to previous line once. */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5547 | no = OK; |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5548 | limit = OPERAND_MIN(rp->rs_scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5549 | if (REG_MULTI) |
| 5550 | { |
Bram Moolenaar | 61602c5 | 2013-06-01 19:54:43 +0200 | [diff] [blame] | 5551 | if (limit > 0 |
| 5552 | && ((rp->rs_un.regsave.rs_u.pos.lnum |
| 5553 | < behind_pos.rs_u.pos.lnum |
| 5554 | ? (colnr_T)STRLEN(regline) |
| 5555 | : behind_pos.rs_u.pos.col) |
| 5556 | - rp->rs_un.regsave.rs_u.pos.col >= limit)) |
| 5557 | no = FAIL; |
| 5558 | else if (rp->rs_un.regsave.rs_u.pos.col == 0) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5559 | { |
| 5560 | if (rp->rs_un.regsave.rs_u.pos.lnum |
| 5561 | < behind_pos.rs_u.pos.lnum |
| 5562 | || reg_getline( |
| 5563 | --rp->rs_un.regsave.rs_u.pos.lnum) |
| 5564 | == NULL) |
| 5565 | no = FAIL; |
| 5566 | else |
| 5567 | { |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5568 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5569 | rp->rs_un.regsave.rs_u.pos.col = |
| 5570 | (colnr_T)STRLEN(regline); |
| 5571 | } |
| 5572 | } |
| 5573 | else |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5574 | { |
Bram Moolenaar | f5e44a7 | 2013-02-26 18:46:01 +0100 | [diff] [blame] | 5575 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5576 | if (has_mbyte) |
| 5577 | rp->rs_un.regsave.rs_u.pos.col -= |
| 5578 | (*mb_head_off)(regline, regline |
Bram Moolenaar | f5e44a7 | 2013-02-26 18:46:01 +0100 | [diff] [blame] | 5579 | + rp->rs_un.regsave.rs_u.pos.col - 1) + 1; |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5580 | else |
Bram Moolenaar | f5e44a7 | 2013-02-26 18:46:01 +0100 | [diff] [blame] | 5581 | #endif |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5582 | --rp->rs_un.regsave.rs_u.pos.col; |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5583 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5584 | } |
| 5585 | else |
| 5586 | { |
| 5587 | if (rp->rs_un.regsave.rs_u.ptr == regline) |
| 5588 | no = FAIL; |
| 5589 | else |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5590 | { |
| 5591 | mb_ptr_back(regline, rp->rs_un.regsave.rs_u.ptr); |
| 5592 | if (limit > 0 && (long)(behind_pos.rs_u.ptr |
| 5593 | - rp->rs_un.regsave.rs_u.ptr) > limit) |
| 5594 | no = FAIL; |
| 5595 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5596 | } |
| 5597 | if (no == OK) |
| 5598 | { |
| 5599 | /* Advanced, prepare for finding match again. */ |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5600 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 5601 | scan = OPERAND(rp->rs_scan) + 4; |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 5602 | if (status == RA_MATCH) |
| 5603 | { |
| 5604 | /* We did match, so subexpr may have been changed, |
| 5605 | * need to restore them for the next try. */ |
| 5606 | status = RA_NOMATCH; |
| 5607 | restore_subexpr(((regbehind_T *)rp) - 1); |
| 5608 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5609 | } |
| 5610 | else |
| 5611 | { |
| 5612 | /* Can't advance. For NOBEHIND that's a match. */ |
| 5613 | behind_pos = (((regbehind_T *)rp) - 1)->save_behind; |
| 5614 | if (rp->rs_no == NOBEHIND) |
| 5615 | { |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5616 | reg_restore(&(((regbehind_T *)rp) - 1)->save_after, |
| 5617 | &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5618 | status = RA_MATCH; |
| 5619 | } |
| 5620 | else |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 5621 | { |
| 5622 | /* We do want a proper match. Need to restore the |
| 5623 | * subexpr if we had a match, because they may have |
| 5624 | * been set. */ |
| 5625 | if (status == RA_MATCH) |
| 5626 | { |
| 5627 | status = RA_NOMATCH; |
| 5628 | restore_subexpr(((regbehind_T *)rp) - 1); |
| 5629 | } |
| 5630 | } |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5631 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5632 | regstack.ga_len -= sizeof(regbehind_T); |
| 5633 | } |
| 5634 | } |
| 5635 | break; |
| 5636 | |
| 5637 | case RS_STAR_LONG: |
| 5638 | case RS_STAR_SHORT: |
| 5639 | { |
| 5640 | regstar_T *rst = ((regstar_T *)rp) - 1; |
| 5641 | |
| 5642 | if (status == RA_MATCH) |
| 5643 | { |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5644 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5645 | regstack.ga_len -= sizeof(regstar_T); |
| 5646 | break; |
| 5647 | } |
| 5648 | |
| 5649 | /* Tried once already, restore input pointers. */ |
| 5650 | if (status != RA_BREAK) |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5651 | reg_restore(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5652 | |
| 5653 | /* Repeat until we found a position where it could match. */ |
| 5654 | for (;;) |
| 5655 | { |
| 5656 | if (status != RA_BREAK) |
| 5657 | { |
| 5658 | /* Tried first position already, advance. */ |
| 5659 | if (rp->rs_state == RS_STAR_LONG) |
| 5660 | { |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 5661 | /* Trying for longest match, but couldn't or |
| 5662 | * didn't match -- back up one char. */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5663 | if (--rst->count < rst->minval) |
| 5664 | break; |
| 5665 | if (reginput == regline) |
| 5666 | { |
| 5667 | /* backup to last char of previous line */ |
| 5668 | --reglnum; |
| 5669 | regline = reg_getline(reglnum); |
| 5670 | /* Just in case regrepeat() didn't count |
| 5671 | * right. */ |
| 5672 | if (regline == NULL) |
| 5673 | break; |
| 5674 | reginput = regline + STRLEN(regline); |
| 5675 | fast_breakcheck(); |
| 5676 | } |
| 5677 | else |
| 5678 | mb_ptr_back(regline, reginput); |
| 5679 | } |
| 5680 | else |
| 5681 | { |
| 5682 | /* Range is backwards, use shortest match first. |
| 5683 | * Careful: maxval and minval are exchanged! |
| 5684 | * Couldn't or didn't match: try advancing one |
| 5685 | * char. */ |
| 5686 | if (rst->count == rst->minval |
| 5687 | || regrepeat(OPERAND(rp->rs_scan), 1L) == 0) |
| 5688 | break; |
| 5689 | ++rst->count; |
| 5690 | } |
| 5691 | if (got_int) |
| 5692 | break; |
| 5693 | } |
| 5694 | else |
| 5695 | status = RA_NOMATCH; |
| 5696 | |
| 5697 | /* If it could match, try it. */ |
| 5698 | if (rst->nextb == NUL || *reginput == rst->nextb |
| 5699 | || *reginput == rst->nextb_ic) |
| 5700 | { |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 5701 | reg_save(&rp->rs_un.regsave, &backpos); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5702 | scan = regnext(rp->rs_scan); |
| 5703 | status = RA_CONT; |
| 5704 | break; |
| 5705 | } |
| 5706 | } |
| 5707 | if (status != RA_CONT) |
| 5708 | { |
| 5709 | /* Failed. */ |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5710 | regstack_pop(&scan); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5711 | regstack.ga_len -= sizeof(regstar_T); |
| 5712 | status = RA_NOMATCH; |
| 5713 | } |
| 5714 | } |
| 5715 | break; |
| 5716 | } |
| 5717 | |
Bram Moolenaar | 32466aa | 2006-02-24 23:53:04 +0000 | [diff] [blame] | 5718 | /* If we want to continue the inner loop or didn't pop a state |
| 5719 | * continue matching loop */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5720 | if (status == RA_CONT || rp == (regitem_T *) |
| 5721 | ((char *)regstack.ga_data + regstack.ga_len) - 1) |
| 5722 | break; |
| 5723 | } |
| 5724 | |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 5725 | /* May need to continue with the inner loop, starting at "scan". */ |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5726 | if (status == RA_CONT) |
| 5727 | continue; |
| 5728 | |
| 5729 | /* |
| 5730 | * If the regstack is empty or something failed we are done. |
| 5731 | */ |
| 5732 | if (regstack.ga_len == 0 || status == RA_FAIL) |
| 5733 | { |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5734 | if (scan == NULL) |
| 5735 | { |
| 5736 | /* |
| 5737 | * We get here only if there's trouble -- normally "case END" is |
| 5738 | * the terminating point. |
| 5739 | */ |
| 5740 | EMSG(_(e_re_corr)); |
| 5741 | #ifdef DEBUG |
| 5742 | printf("Premature EOL\n"); |
| 5743 | #endif |
| 5744 | } |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 5745 | if (status == RA_FAIL) |
| 5746 | got_int = TRUE; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5747 | return (status == RA_MATCH); |
| 5748 | } |
| 5749 | |
| 5750 | } /* End of loop until the regstack is empty. */ |
| 5751 | |
| 5752 | /* NOTREACHED */ |
| 5753 | } |
| 5754 | |
| 5755 | /* |
| 5756 | * Push an item onto the regstack. |
| 5757 | * Returns pointer to new item. Returns NULL when out of memory. |
| 5758 | */ |
| 5759 | static regitem_T * |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5760 | regstack_push(state, scan) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5761 | regstate_T state; |
| 5762 | char_u *scan; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5763 | { |
| 5764 | regitem_T *rp; |
| 5765 | |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5766 | if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp) |
Bram Moolenaar | e4efc3b | 2005-03-07 23:16:51 +0000 | [diff] [blame] | 5767 | { |
| 5768 | EMSG(_(e_maxmempat)); |
| 5769 | return NULL; |
| 5770 | } |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5771 | if (ga_grow(®stack, sizeof(regitem_T)) == FAIL) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5772 | return NULL; |
| 5773 | |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5774 | rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5775 | rp->rs_state = state; |
| 5776 | rp->rs_scan = scan; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5777 | |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5778 | regstack.ga_len += sizeof(regitem_T); |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5779 | return rp; |
| 5780 | } |
| 5781 | |
| 5782 | /* |
| 5783 | * Pop an item from the regstack. |
| 5784 | */ |
| 5785 | static void |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5786 | regstack_pop(scan) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5787 | char_u **scan; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5788 | { |
| 5789 | regitem_T *rp; |
| 5790 | |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5791 | rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5792 | *scan = rp->rs_scan; |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 5793 | |
Bram Moolenaar | a7fc010 | 2005-05-18 22:17:12 +0000 | [diff] [blame] | 5794 | regstack.ga_len -= sizeof(regitem_T); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5795 | } |
| 5796 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5797 | /* |
| 5798 | * regrepeat - repeatedly match something simple, return how many. |
| 5799 | * Advances reginput (and reglnum) to just after the matched chars. |
| 5800 | */ |
| 5801 | static int |
| 5802 | regrepeat(p, maxcount) |
| 5803 | char_u *p; |
| 5804 | long maxcount; /* maximum number of matches allowed */ |
| 5805 | { |
| 5806 | long count = 0; |
| 5807 | char_u *scan; |
| 5808 | char_u *opnd; |
| 5809 | int mask; |
| 5810 | int testval = 0; |
| 5811 | |
| 5812 | scan = reginput; /* Make local copy of reginput for speed. */ |
| 5813 | opnd = OPERAND(p); |
| 5814 | switch (OP(p)) |
| 5815 | { |
| 5816 | case ANY: |
| 5817 | case ANY + ADD_NL: |
| 5818 | while (count < maxcount) |
| 5819 | { |
| 5820 | /* Matching anything means we continue until end-of-line (or |
| 5821 | * end-of-file for ANY + ADD_NL), only limited by maxcount. */ |
| 5822 | while (*scan != NUL && count < maxcount) |
| 5823 | { |
| 5824 | ++count; |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5825 | mb_ptr_adv(scan); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5826 | } |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 5827 | if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
| 5828 | || reg_line_lbr || count == maxcount) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5829 | break; |
| 5830 | ++count; /* count the line-break */ |
| 5831 | reg_nextline(); |
| 5832 | scan = reginput; |
| 5833 | if (got_int) |
| 5834 | break; |
| 5835 | } |
| 5836 | break; |
| 5837 | |
| 5838 | case IDENT: |
| 5839 | case IDENT + ADD_NL: |
| 5840 | testval = TRUE; |
| 5841 | /*FALLTHROUGH*/ |
| 5842 | case SIDENT: |
| 5843 | case SIDENT + ADD_NL: |
| 5844 | while (count < maxcount) |
| 5845 | { |
Bram Moolenaar | 09ea9fc | 2013-05-21 00:03:02 +0200 | [diff] [blame] | 5846 | if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5847 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5848 | mb_ptr_adv(scan); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5849 | } |
| 5850 | else if (*scan == NUL) |
| 5851 | { |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 5852 | if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
| 5853 | || reg_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5854 | break; |
| 5855 | reg_nextline(); |
| 5856 | scan = reginput; |
| 5857 | if (got_int) |
| 5858 | break; |
| 5859 | } |
| 5860 | else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
| 5861 | ++scan; |
| 5862 | else |
| 5863 | break; |
| 5864 | ++count; |
| 5865 | } |
| 5866 | break; |
| 5867 | |
| 5868 | case KWORD: |
| 5869 | case KWORD + ADD_NL: |
| 5870 | testval = TRUE; |
| 5871 | /*FALLTHROUGH*/ |
| 5872 | case SKWORD: |
| 5873 | case SKWORD + ADD_NL: |
| 5874 | while (count < maxcount) |
| 5875 | { |
Bram Moolenaar | f813a18 | 2013-01-30 13:59:37 +0100 | [diff] [blame] | 5876 | if (vim_iswordp_buf(scan, reg_buf) |
| 5877 | && (testval || !VIM_ISDIGIT(*scan))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5878 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5879 | mb_ptr_adv(scan); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5880 | } |
| 5881 | else if (*scan == NUL) |
| 5882 | { |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 5883 | if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
| 5884 | || reg_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5885 | break; |
| 5886 | reg_nextline(); |
| 5887 | scan = reginput; |
| 5888 | if (got_int) |
| 5889 | break; |
| 5890 | } |
| 5891 | else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
| 5892 | ++scan; |
| 5893 | else |
| 5894 | break; |
| 5895 | ++count; |
| 5896 | } |
| 5897 | break; |
| 5898 | |
| 5899 | case FNAME: |
| 5900 | case FNAME + ADD_NL: |
| 5901 | testval = TRUE; |
| 5902 | /*FALLTHROUGH*/ |
| 5903 | case SFNAME: |
| 5904 | case SFNAME + ADD_NL: |
| 5905 | while (count < maxcount) |
| 5906 | { |
Bram Moolenaar | 09ea9fc | 2013-05-21 00:03:02 +0200 | [diff] [blame] | 5907 | if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5908 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5909 | mb_ptr_adv(scan); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5910 | } |
| 5911 | else if (*scan == NUL) |
| 5912 | { |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 5913 | if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
| 5914 | || reg_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5915 | break; |
| 5916 | reg_nextline(); |
| 5917 | scan = reginput; |
| 5918 | if (got_int) |
| 5919 | break; |
| 5920 | } |
| 5921 | else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
| 5922 | ++scan; |
| 5923 | else |
| 5924 | break; |
| 5925 | ++count; |
| 5926 | } |
| 5927 | break; |
| 5928 | |
| 5929 | case PRINT: |
| 5930 | case PRINT + ADD_NL: |
| 5931 | testval = TRUE; |
| 5932 | /*FALLTHROUGH*/ |
| 5933 | case SPRINT: |
| 5934 | case SPRINT + ADD_NL: |
| 5935 | while (count < maxcount) |
| 5936 | { |
| 5937 | if (*scan == NUL) |
| 5938 | { |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 5939 | if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
| 5940 | || reg_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5941 | break; |
| 5942 | reg_nextline(); |
| 5943 | scan = reginput; |
| 5944 | if (got_int) |
| 5945 | break; |
| 5946 | } |
Bram Moolenaar | ac7c33e | 2013-07-21 17:06:00 +0200 | [diff] [blame] | 5947 | else if (vim_isprintc(PTR2CHAR(scan)) == 1 |
| 5948 | && (testval || !VIM_ISDIGIT(*scan))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5949 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 5950 | mb_ptr_adv(scan); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5951 | } |
| 5952 | else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
| 5953 | ++scan; |
| 5954 | else |
| 5955 | break; |
| 5956 | ++count; |
| 5957 | } |
| 5958 | break; |
| 5959 | |
| 5960 | case WHITE: |
| 5961 | case WHITE + ADD_NL: |
| 5962 | testval = mask = RI_WHITE; |
| 5963 | do_class: |
| 5964 | while (count < maxcount) |
| 5965 | { |
| 5966 | #ifdef FEAT_MBYTE |
| 5967 | int l; |
| 5968 | #endif |
| 5969 | if (*scan == NUL) |
| 5970 | { |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 5971 | if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
| 5972 | || reg_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5973 | break; |
| 5974 | reg_nextline(); |
| 5975 | scan = reginput; |
| 5976 | if (got_int) |
| 5977 | break; |
| 5978 | } |
| 5979 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 5980 | else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5981 | { |
| 5982 | if (testval != 0) |
| 5983 | break; |
| 5984 | scan += l; |
| 5985 | } |
| 5986 | #endif |
| 5987 | else if ((class_tab[*scan] & mask) == testval) |
| 5988 | ++scan; |
| 5989 | else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
| 5990 | ++scan; |
| 5991 | else |
| 5992 | break; |
| 5993 | ++count; |
| 5994 | } |
| 5995 | break; |
| 5996 | |
| 5997 | case NWHITE: |
| 5998 | case NWHITE + ADD_NL: |
| 5999 | mask = RI_WHITE; |
| 6000 | goto do_class; |
| 6001 | case DIGIT: |
| 6002 | case DIGIT + ADD_NL: |
| 6003 | testval = mask = RI_DIGIT; |
| 6004 | goto do_class; |
| 6005 | case NDIGIT: |
| 6006 | case NDIGIT + ADD_NL: |
| 6007 | mask = RI_DIGIT; |
| 6008 | goto do_class; |
| 6009 | case HEX: |
| 6010 | case HEX + ADD_NL: |
| 6011 | testval = mask = RI_HEX; |
| 6012 | goto do_class; |
| 6013 | case NHEX: |
| 6014 | case NHEX + ADD_NL: |
| 6015 | mask = RI_HEX; |
| 6016 | goto do_class; |
| 6017 | case OCTAL: |
| 6018 | case OCTAL + ADD_NL: |
| 6019 | testval = mask = RI_OCTAL; |
| 6020 | goto do_class; |
| 6021 | case NOCTAL: |
| 6022 | case NOCTAL + ADD_NL: |
| 6023 | mask = RI_OCTAL; |
| 6024 | goto do_class; |
| 6025 | case WORD: |
| 6026 | case WORD + ADD_NL: |
| 6027 | testval = mask = RI_WORD; |
| 6028 | goto do_class; |
| 6029 | case NWORD: |
| 6030 | case NWORD + ADD_NL: |
| 6031 | mask = RI_WORD; |
| 6032 | goto do_class; |
| 6033 | case HEAD: |
| 6034 | case HEAD + ADD_NL: |
| 6035 | testval = mask = RI_HEAD; |
| 6036 | goto do_class; |
| 6037 | case NHEAD: |
| 6038 | case NHEAD + ADD_NL: |
| 6039 | mask = RI_HEAD; |
| 6040 | goto do_class; |
| 6041 | case ALPHA: |
| 6042 | case ALPHA + ADD_NL: |
| 6043 | testval = mask = RI_ALPHA; |
| 6044 | goto do_class; |
| 6045 | case NALPHA: |
| 6046 | case NALPHA + ADD_NL: |
| 6047 | mask = RI_ALPHA; |
| 6048 | goto do_class; |
| 6049 | case LOWER: |
| 6050 | case LOWER + ADD_NL: |
| 6051 | testval = mask = RI_LOWER; |
| 6052 | goto do_class; |
| 6053 | case NLOWER: |
| 6054 | case NLOWER + ADD_NL: |
| 6055 | mask = RI_LOWER; |
| 6056 | goto do_class; |
| 6057 | case UPPER: |
| 6058 | case UPPER + ADD_NL: |
| 6059 | testval = mask = RI_UPPER; |
| 6060 | goto do_class; |
| 6061 | case NUPPER: |
| 6062 | case NUPPER + ADD_NL: |
| 6063 | mask = RI_UPPER; |
| 6064 | goto do_class; |
| 6065 | |
| 6066 | case EXACTLY: |
| 6067 | { |
| 6068 | int cu, cl; |
| 6069 | |
| 6070 | /* This doesn't do a multi-byte character, because a MULTIBYTECODE |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 6071 | * would have been used for it. It does handle single-byte |
| 6072 | * characters, such as latin1. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6073 | if (ireg_ic) |
| 6074 | { |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 6075 | cu = MB_TOUPPER(*opnd); |
| 6076 | cl = MB_TOLOWER(*opnd); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6077 | while (count < maxcount && (*scan == cu || *scan == cl)) |
| 6078 | { |
| 6079 | count++; |
| 6080 | scan++; |
| 6081 | } |
| 6082 | } |
| 6083 | else |
| 6084 | { |
| 6085 | cu = *opnd; |
| 6086 | while (count < maxcount && *scan == cu) |
| 6087 | { |
| 6088 | count++; |
| 6089 | scan++; |
| 6090 | } |
| 6091 | } |
| 6092 | break; |
| 6093 | } |
| 6094 | |
| 6095 | #ifdef FEAT_MBYTE |
| 6096 | case MULTIBYTECODE: |
| 6097 | { |
| 6098 | int i, len, cf = 0; |
| 6099 | |
| 6100 | /* Safety check (just in case 'encoding' was changed since |
| 6101 | * compiling the program). */ |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6102 | if ((len = (*mb_ptr2len)(opnd)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6103 | { |
| 6104 | if (ireg_ic && enc_utf8) |
| 6105 | cf = utf_fold(utf_ptr2char(opnd)); |
| 6106 | while (count < maxcount) |
| 6107 | { |
| 6108 | for (i = 0; i < len; ++i) |
| 6109 | if (opnd[i] != scan[i]) |
| 6110 | break; |
| 6111 | if (i < len && (!ireg_ic || !enc_utf8 |
| 6112 | || utf_fold(utf_ptr2char(scan)) != cf)) |
| 6113 | break; |
| 6114 | scan += len; |
| 6115 | ++count; |
| 6116 | } |
| 6117 | } |
| 6118 | } |
| 6119 | break; |
| 6120 | #endif |
| 6121 | |
| 6122 | case ANYOF: |
| 6123 | case ANYOF + ADD_NL: |
| 6124 | testval = TRUE; |
| 6125 | /*FALLTHROUGH*/ |
| 6126 | |
| 6127 | case ANYBUT: |
| 6128 | case ANYBUT + ADD_NL: |
| 6129 | while (count < maxcount) |
| 6130 | { |
| 6131 | #ifdef FEAT_MBYTE |
| 6132 | int len; |
| 6133 | #endif |
| 6134 | if (*scan == NUL) |
| 6135 | { |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 6136 | if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline |
| 6137 | || reg_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6138 | break; |
| 6139 | reg_nextline(); |
| 6140 | scan = reginput; |
| 6141 | if (got_int) |
| 6142 | break; |
| 6143 | } |
| 6144 | else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p))) |
| 6145 | ++scan; |
| 6146 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 6147 | else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6148 | { |
| 6149 | if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval) |
| 6150 | break; |
| 6151 | scan += len; |
| 6152 | } |
| 6153 | #endif |
| 6154 | else |
| 6155 | { |
| 6156 | if ((cstrchr(opnd, *scan) == NULL) == testval) |
| 6157 | break; |
| 6158 | ++scan; |
| 6159 | } |
| 6160 | ++count; |
| 6161 | } |
| 6162 | break; |
| 6163 | |
| 6164 | case NEWL: |
| 6165 | while (count < maxcount |
Bram Moolenaar | 640009d | 2006-10-17 16:48:26 +0000 | [diff] [blame] | 6166 | && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr |
| 6167 | && REG_MULTI) || (*scan == '\n' && reg_line_lbr))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6168 | { |
| 6169 | count++; |
| 6170 | if (reg_line_lbr) |
| 6171 | ADVANCE_REGINPUT(); |
| 6172 | else |
| 6173 | reg_nextline(); |
| 6174 | scan = reginput; |
| 6175 | if (got_int) |
| 6176 | break; |
| 6177 | } |
| 6178 | break; |
| 6179 | |
| 6180 | default: /* Oh dear. Called inappropriately. */ |
| 6181 | EMSG(_(e_re_corr)); |
| 6182 | #ifdef DEBUG |
| 6183 | printf("Called regrepeat with op code %d\n", OP(p)); |
| 6184 | #endif |
| 6185 | break; |
| 6186 | } |
| 6187 | |
| 6188 | reginput = scan; |
| 6189 | |
| 6190 | return (int)count; |
| 6191 | } |
| 6192 | |
| 6193 | /* |
| 6194 | * regnext - dig the "next" pointer out of a node |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 6195 | * Returns NULL when calculating size, when there is no next item and when |
| 6196 | * there is an error. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6197 | */ |
| 6198 | static char_u * |
| 6199 | regnext(p) |
| 6200 | char_u *p; |
| 6201 | { |
| 6202 | int offset; |
| 6203 | |
Bram Moolenaar | d300580 | 2009-11-25 17:21:32 +0000 | [diff] [blame] | 6204 | if (p == JUST_CALC_SIZE || reg_toolong) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6205 | return NULL; |
| 6206 | |
| 6207 | offset = NEXT(p); |
| 6208 | if (offset == 0) |
| 6209 | return NULL; |
| 6210 | |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6211 | if (OP(p) == BACK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6212 | return p - offset; |
| 6213 | else |
| 6214 | return p + offset; |
| 6215 | } |
| 6216 | |
| 6217 | /* |
| 6218 | * Check the regexp program for its magic number. |
| 6219 | * Return TRUE if it's wrong. |
| 6220 | */ |
| 6221 | static int |
| 6222 | prog_magic_wrong() |
| 6223 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6224 | regprog_T *prog; |
| 6225 | |
| 6226 | prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog; |
| 6227 | if (prog->engine == &nfa_regengine) |
| 6228 | /* For NFA matcher we don't check the magic */ |
| 6229 | return FALSE; |
| 6230 | |
| 6231 | if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6232 | { |
| 6233 | EMSG(_(e_re_corr)); |
| 6234 | return TRUE; |
| 6235 | } |
| 6236 | return FALSE; |
| 6237 | } |
| 6238 | |
| 6239 | /* |
| 6240 | * Cleanup the subexpressions, if this wasn't done yet. |
| 6241 | * This construction is used to clear the subexpressions only when they are |
| 6242 | * used (to increase speed). |
| 6243 | */ |
| 6244 | static void |
| 6245 | cleanup_subexpr() |
| 6246 | { |
| 6247 | if (need_clear_subexpr) |
| 6248 | { |
| 6249 | if (REG_MULTI) |
| 6250 | { |
| 6251 | /* Use 0xff to set lnum to -1 */ |
| 6252 | vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP); |
| 6253 | vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP); |
| 6254 | } |
| 6255 | else |
| 6256 | { |
| 6257 | vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP); |
| 6258 | vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP); |
| 6259 | } |
| 6260 | need_clear_subexpr = FALSE; |
| 6261 | } |
| 6262 | } |
| 6263 | |
| 6264 | #ifdef FEAT_SYN_HL |
| 6265 | static void |
| 6266 | cleanup_zsubexpr() |
| 6267 | { |
| 6268 | if (need_clear_zsubexpr) |
| 6269 | { |
| 6270 | if (REG_MULTI) |
| 6271 | { |
| 6272 | /* Use 0xff to set lnum to -1 */ |
| 6273 | vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP); |
| 6274 | vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP); |
| 6275 | } |
| 6276 | else |
| 6277 | { |
| 6278 | vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP); |
| 6279 | vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP); |
| 6280 | } |
| 6281 | need_clear_zsubexpr = FALSE; |
| 6282 | } |
| 6283 | } |
| 6284 | #endif |
| 6285 | |
| 6286 | /* |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 6287 | * Save the current subexpr to "bp", so that they can be restored |
| 6288 | * later by restore_subexpr(). |
| 6289 | */ |
| 6290 | static void |
| 6291 | save_subexpr(bp) |
| 6292 | regbehind_T *bp; |
| 6293 | { |
| 6294 | int i; |
| 6295 | |
Bram Moolenaar | fde483c | 2008-06-15 12:21:50 +0000 | [diff] [blame] | 6296 | /* When "need_clear_subexpr" is set we don't need to save the values, only |
| 6297 | * remember that this flag needs to be set again when restoring. */ |
| 6298 | bp->save_need_clear_subexpr = need_clear_subexpr; |
| 6299 | if (!need_clear_subexpr) |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 6300 | { |
Bram Moolenaar | fde483c | 2008-06-15 12:21:50 +0000 | [diff] [blame] | 6301 | for (i = 0; i < NSUBEXP; ++i) |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 6302 | { |
Bram Moolenaar | fde483c | 2008-06-15 12:21:50 +0000 | [diff] [blame] | 6303 | if (REG_MULTI) |
| 6304 | { |
| 6305 | bp->save_start[i].se_u.pos = reg_startpos[i]; |
| 6306 | bp->save_end[i].se_u.pos = reg_endpos[i]; |
| 6307 | } |
| 6308 | else |
| 6309 | { |
| 6310 | bp->save_start[i].se_u.ptr = reg_startp[i]; |
| 6311 | bp->save_end[i].se_u.ptr = reg_endp[i]; |
| 6312 | } |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 6313 | } |
| 6314 | } |
| 6315 | } |
| 6316 | |
| 6317 | /* |
| 6318 | * Restore the subexpr from "bp". |
| 6319 | */ |
| 6320 | static void |
| 6321 | restore_subexpr(bp) |
| 6322 | regbehind_T *bp; |
| 6323 | { |
| 6324 | int i; |
| 6325 | |
Bram Moolenaar | fde483c | 2008-06-15 12:21:50 +0000 | [diff] [blame] | 6326 | /* Only need to restore saved values when they are not to be cleared. */ |
| 6327 | need_clear_subexpr = bp->save_need_clear_subexpr; |
| 6328 | if (!need_clear_subexpr) |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 6329 | { |
Bram Moolenaar | fde483c | 2008-06-15 12:21:50 +0000 | [diff] [blame] | 6330 | for (i = 0; i < NSUBEXP; ++i) |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 6331 | { |
Bram Moolenaar | fde483c | 2008-06-15 12:21:50 +0000 | [diff] [blame] | 6332 | if (REG_MULTI) |
| 6333 | { |
| 6334 | reg_startpos[i] = bp->save_start[i].se_u.pos; |
| 6335 | reg_endpos[i] = bp->save_end[i].se_u.pos; |
| 6336 | } |
| 6337 | else |
| 6338 | { |
| 6339 | reg_startp[i] = bp->save_start[i].se_u.ptr; |
| 6340 | reg_endp[i] = bp->save_end[i].se_u.ptr; |
| 6341 | } |
Bram Moolenaar | 34cbfdf | 2008-04-09 10:16:02 +0000 | [diff] [blame] | 6342 | } |
| 6343 | } |
| 6344 | } |
| 6345 | |
| 6346 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | * Advance reglnum, regline and reginput to the next line. |
| 6348 | */ |
| 6349 | static void |
| 6350 | reg_nextline() |
| 6351 | { |
| 6352 | regline = reg_getline(++reglnum); |
| 6353 | reginput = regline; |
| 6354 | fast_breakcheck(); |
| 6355 | } |
| 6356 | |
| 6357 | /* |
| 6358 | * Save the input line and position in a regsave_T. |
| 6359 | */ |
| 6360 | static void |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6361 | reg_save(save, gap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6362 | regsave_T *save; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6363 | garray_T *gap; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6364 | { |
| 6365 | if (REG_MULTI) |
| 6366 | { |
| 6367 | save->rs_u.pos.col = (colnr_T)(reginput - regline); |
| 6368 | save->rs_u.pos.lnum = reglnum; |
| 6369 | } |
| 6370 | else |
| 6371 | save->rs_u.ptr = reginput; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6372 | save->rs_len = gap->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6373 | } |
| 6374 | |
| 6375 | /* |
| 6376 | * Restore the input line and position from a regsave_T. |
| 6377 | */ |
| 6378 | static void |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6379 | reg_restore(save, gap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6380 | regsave_T *save; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6381 | garray_T *gap; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6382 | { |
| 6383 | if (REG_MULTI) |
| 6384 | { |
| 6385 | if (reglnum != save->rs_u.pos.lnum) |
| 6386 | { |
| 6387 | /* only call reg_getline() when the line number changed to save |
| 6388 | * a bit of time */ |
| 6389 | reglnum = save->rs_u.pos.lnum; |
| 6390 | regline = reg_getline(reglnum); |
| 6391 | } |
| 6392 | reginput = regline + save->rs_u.pos.col; |
| 6393 | } |
| 6394 | else |
| 6395 | reginput = save->rs_u.ptr; |
Bram Moolenaar | 582fd85 | 2005-03-28 20:58:01 +0000 | [diff] [blame] | 6396 | gap->ga_len = save->rs_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6397 | } |
| 6398 | |
| 6399 | /* |
| 6400 | * Return TRUE if current position is equal to saved position. |
| 6401 | */ |
| 6402 | static int |
| 6403 | reg_save_equal(save) |
| 6404 | regsave_T *save; |
| 6405 | { |
| 6406 | if (REG_MULTI) |
| 6407 | return reglnum == save->rs_u.pos.lnum |
| 6408 | && reginput == regline + save->rs_u.pos.col; |
| 6409 | return reginput == save->rs_u.ptr; |
| 6410 | } |
| 6411 | |
| 6412 | /* |
| 6413 | * Tentatively set the sub-expression start to the current position (after |
| 6414 | * calling regmatch() they will have changed). Need to save the existing |
| 6415 | * values for when there is no match. |
| 6416 | * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()), |
| 6417 | * depending on REG_MULTI. |
| 6418 | */ |
| 6419 | static void |
| 6420 | save_se_multi(savep, posp) |
| 6421 | save_se_T *savep; |
| 6422 | lpos_T *posp; |
| 6423 | { |
| 6424 | savep->se_u.pos = *posp; |
| 6425 | posp->lnum = reglnum; |
| 6426 | posp->col = (colnr_T)(reginput - regline); |
| 6427 | } |
| 6428 | |
| 6429 | static void |
| 6430 | save_se_one(savep, pp) |
| 6431 | save_se_T *savep; |
| 6432 | char_u **pp; |
| 6433 | { |
| 6434 | savep->se_u.ptr = *pp; |
| 6435 | *pp = reginput; |
| 6436 | } |
| 6437 | |
| 6438 | /* |
| 6439 | * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL. |
| 6440 | */ |
| 6441 | static int |
| 6442 | re_num_cmp(val, scan) |
| 6443 | long_u val; |
| 6444 | char_u *scan; |
| 6445 | { |
| 6446 | long_u n = OPERAND_MIN(scan); |
| 6447 | |
| 6448 | if (OPERAND_CMP(scan) == '>') |
| 6449 | return val > n; |
| 6450 | if (OPERAND_CMP(scan) == '<') |
| 6451 | return val < n; |
| 6452 | return val == n; |
| 6453 | } |
| 6454 | |
Bram Moolenaar | 580abea | 2013-06-14 20:31:28 +0200 | [diff] [blame] | 6455 | /* |
| 6456 | * Check whether a backreference matches. |
| 6457 | * Returns RA_FAIL, RA_NOMATCH or RA_MATCH. |
| 6458 | * If "bytelen" is not NULL, it is set to the bytelength of the whole match. |
| 6459 | */ |
| 6460 | static int |
| 6461 | match_with_backref(start_lnum, start_col, end_lnum, end_col, bytelen) |
| 6462 | linenr_T start_lnum; |
| 6463 | colnr_T start_col; |
| 6464 | linenr_T end_lnum; |
| 6465 | colnr_T end_col; |
| 6466 | int *bytelen; |
| 6467 | { |
| 6468 | linenr_T clnum = start_lnum; |
| 6469 | colnr_T ccol = start_col; |
| 6470 | int len; |
| 6471 | char_u *p; |
| 6472 | |
| 6473 | if (bytelen != NULL) |
| 6474 | *bytelen = 0; |
| 6475 | for (;;) |
| 6476 | { |
| 6477 | /* Since getting one line may invalidate the other, need to make copy. |
| 6478 | * Slow! */ |
| 6479 | if (regline != reg_tofree) |
| 6480 | { |
| 6481 | len = (int)STRLEN(regline); |
| 6482 | if (reg_tofree == NULL || len >= (int)reg_tofreelen) |
| 6483 | { |
| 6484 | len += 50; /* get some extra */ |
| 6485 | vim_free(reg_tofree); |
| 6486 | reg_tofree = alloc(len); |
| 6487 | if (reg_tofree == NULL) |
| 6488 | return RA_FAIL; /* out of memory!*/ |
| 6489 | reg_tofreelen = len; |
| 6490 | } |
| 6491 | STRCPY(reg_tofree, regline); |
| 6492 | reginput = reg_tofree + (reginput - regline); |
| 6493 | regline = reg_tofree; |
| 6494 | } |
| 6495 | |
| 6496 | /* Get the line to compare with. */ |
| 6497 | p = reg_getline(clnum); |
| 6498 | if (clnum == end_lnum) |
| 6499 | len = end_col - ccol; |
| 6500 | else |
| 6501 | len = (int)STRLEN(p + ccol); |
| 6502 | |
| 6503 | if (cstrncmp(p + ccol, reginput, &len) != 0) |
| 6504 | return RA_NOMATCH; /* doesn't match */ |
| 6505 | if (bytelen != NULL) |
| 6506 | *bytelen += len; |
| 6507 | if (clnum == end_lnum) |
| 6508 | break; /* match and at end! */ |
| 6509 | if (reglnum >= reg_maxline) |
| 6510 | return RA_NOMATCH; /* text too short */ |
| 6511 | |
| 6512 | /* Advance to next line. */ |
| 6513 | reg_nextline(); |
| 6514 | ++clnum; |
| 6515 | ccol = 0; |
| 6516 | if (got_int) |
| 6517 | return RA_FAIL; |
| 6518 | } |
| 6519 | |
| 6520 | /* found a match! Note that regline may now point to a copy of the line, |
| 6521 | * that should not matter. */ |
| 6522 | return RA_MATCH; |
| 6523 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6524 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6525 | #ifdef BT_REGEXP_DUMP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6526 | |
| 6527 | /* |
| 6528 | * regdump - dump a regexp onto stdout in vaguely comprehensible form |
| 6529 | */ |
| 6530 | static void |
| 6531 | regdump(pattern, r) |
| 6532 | char_u *pattern; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6533 | bt_regprog_T *r; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6534 | { |
| 6535 | char_u *s; |
| 6536 | int op = EXACTLY; /* Arbitrary non-END op. */ |
| 6537 | char_u *next; |
| 6538 | char_u *end = NULL; |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6539 | FILE *f; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6540 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6541 | #ifdef BT_REGEXP_LOG |
| 6542 | f = fopen("bt_regexp_log.log", "a"); |
| 6543 | #else |
| 6544 | f = stdout; |
| 6545 | #endif |
| 6546 | if (f == NULL) |
| 6547 | return; |
| 6548 | fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6549 | |
| 6550 | s = r->program + 1; |
| 6551 | /* |
| 6552 | * Loop until we find the END that isn't before a referred next (an END |
| 6553 | * can also appear in a NOMATCH operand). |
| 6554 | */ |
| 6555 | while (op != END || s <= end) |
| 6556 | { |
| 6557 | op = OP(s); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6558 | fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6559 | next = regnext(s); |
| 6560 | if (next == NULL) /* Next ptr. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6561 | fprintf(f, "(0)"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6562 | else |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6563 | fprintf(f, "(%d)", (int)((s - r->program) + (next - s))); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6564 | if (end < next) |
| 6565 | end = next; |
| 6566 | if (op == BRACE_LIMITS) |
| 6567 | { |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 6568 | /* Two ints */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6569 | fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6570 | s += 8; |
| 6571 | } |
Bram Moolenaar | 5b84ddc | 2013-06-05 16:33:10 +0200 | [diff] [blame] | 6572 | else if (op == BEHIND || op == NOBEHIND) |
| 6573 | { |
| 6574 | /* one int */ |
| 6575 | fprintf(f, " count %ld", OPERAND_MIN(s)); |
| 6576 | s += 4; |
| 6577 | } |
Bram Moolenaar | 6d3a5d7 | 2013-06-06 18:04:51 +0200 | [diff] [blame] | 6578 | else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL) |
| 6579 | { |
| 6580 | /* one int plus comperator */ |
| 6581 | fprintf(f, " count %ld", OPERAND_MIN(s)); |
| 6582 | s += 5; |
| 6583 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6584 | s += 3; |
| 6585 | if (op == ANYOF || op == ANYOF + ADD_NL |
| 6586 | || op == ANYBUT || op == ANYBUT + ADD_NL |
| 6587 | || op == EXACTLY) |
| 6588 | { |
| 6589 | /* Literal string, where present. */ |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6590 | fprintf(f, "\nxxxxxxxxx\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6591 | while (*s != NUL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6592 | fprintf(f, "%c", *s++); |
| 6593 | fprintf(f, "\nxxxxxxxxx\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6594 | s++; |
| 6595 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6596 | fprintf(f, "\r\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6597 | } |
| 6598 | |
| 6599 | /* Header fields of interest. */ |
| 6600 | if (r->regstart != NUL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6601 | fprintf(f, "start `%s' 0x%x; ", r->regstart < 256 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6602 | ? (char *)transchar(r->regstart) |
| 6603 | : "multibyte", r->regstart); |
| 6604 | if (r->reganch) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6605 | fprintf(f, "anchored; "); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6606 | if (r->regmust != NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6607 | fprintf(f, "must have \"%s\"", r->regmust); |
| 6608 | fprintf(f, "\r\n"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6609 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6610 | #ifdef BT_REGEXP_LOG |
| 6611 | fclose(f); |
| 6612 | #endif |
| 6613 | } |
| 6614 | #endif /* BT_REGEXP_DUMP */ |
| 6615 | |
| 6616 | #ifdef DEBUG |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6617 | /* |
| 6618 | * regprop - printable representation of opcode |
| 6619 | */ |
| 6620 | static char_u * |
| 6621 | regprop(op) |
| 6622 | char_u *op; |
| 6623 | { |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6624 | char *p; |
| 6625 | static char buf[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6626 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6627 | STRCPY(buf, ":"); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6628 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6629 | switch ((int) OP(op)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6630 | { |
| 6631 | case BOL: |
| 6632 | p = "BOL"; |
| 6633 | break; |
| 6634 | case EOL: |
| 6635 | p = "EOL"; |
| 6636 | break; |
| 6637 | case RE_BOF: |
| 6638 | p = "BOF"; |
| 6639 | break; |
| 6640 | case RE_EOF: |
| 6641 | p = "EOF"; |
| 6642 | break; |
| 6643 | case CURSOR: |
| 6644 | p = "CURSOR"; |
| 6645 | break; |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 6646 | case RE_VISUAL: |
| 6647 | p = "RE_VISUAL"; |
| 6648 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6649 | case RE_LNUM: |
| 6650 | p = "RE_LNUM"; |
| 6651 | break; |
Bram Moolenaar | 71fe80d | 2006-01-22 23:25:56 +0000 | [diff] [blame] | 6652 | case RE_MARK: |
| 6653 | p = "RE_MARK"; |
| 6654 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6655 | case RE_COL: |
| 6656 | p = "RE_COL"; |
| 6657 | break; |
| 6658 | case RE_VCOL: |
| 6659 | p = "RE_VCOL"; |
| 6660 | break; |
| 6661 | case BOW: |
| 6662 | p = "BOW"; |
| 6663 | break; |
| 6664 | case EOW: |
| 6665 | p = "EOW"; |
| 6666 | break; |
| 6667 | case ANY: |
| 6668 | p = "ANY"; |
| 6669 | break; |
| 6670 | case ANY + ADD_NL: |
| 6671 | p = "ANY+NL"; |
| 6672 | break; |
| 6673 | case ANYOF: |
| 6674 | p = "ANYOF"; |
| 6675 | break; |
| 6676 | case ANYOF + ADD_NL: |
| 6677 | p = "ANYOF+NL"; |
| 6678 | break; |
| 6679 | case ANYBUT: |
| 6680 | p = "ANYBUT"; |
| 6681 | break; |
| 6682 | case ANYBUT + ADD_NL: |
| 6683 | p = "ANYBUT+NL"; |
| 6684 | break; |
| 6685 | case IDENT: |
| 6686 | p = "IDENT"; |
| 6687 | break; |
| 6688 | case IDENT + ADD_NL: |
| 6689 | p = "IDENT+NL"; |
| 6690 | break; |
| 6691 | case SIDENT: |
| 6692 | p = "SIDENT"; |
| 6693 | break; |
| 6694 | case SIDENT + ADD_NL: |
| 6695 | p = "SIDENT+NL"; |
| 6696 | break; |
| 6697 | case KWORD: |
| 6698 | p = "KWORD"; |
| 6699 | break; |
| 6700 | case KWORD + ADD_NL: |
| 6701 | p = "KWORD+NL"; |
| 6702 | break; |
| 6703 | case SKWORD: |
| 6704 | p = "SKWORD"; |
| 6705 | break; |
| 6706 | case SKWORD + ADD_NL: |
| 6707 | p = "SKWORD+NL"; |
| 6708 | break; |
| 6709 | case FNAME: |
| 6710 | p = "FNAME"; |
| 6711 | break; |
| 6712 | case FNAME + ADD_NL: |
| 6713 | p = "FNAME+NL"; |
| 6714 | break; |
| 6715 | case SFNAME: |
| 6716 | p = "SFNAME"; |
| 6717 | break; |
| 6718 | case SFNAME + ADD_NL: |
| 6719 | p = "SFNAME+NL"; |
| 6720 | break; |
| 6721 | case PRINT: |
| 6722 | p = "PRINT"; |
| 6723 | break; |
| 6724 | case PRINT + ADD_NL: |
| 6725 | p = "PRINT+NL"; |
| 6726 | break; |
| 6727 | case SPRINT: |
| 6728 | p = "SPRINT"; |
| 6729 | break; |
| 6730 | case SPRINT + ADD_NL: |
| 6731 | p = "SPRINT+NL"; |
| 6732 | break; |
| 6733 | case WHITE: |
| 6734 | p = "WHITE"; |
| 6735 | break; |
| 6736 | case WHITE + ADD_NL: |
| 6737 | p = "WHITE+NL"; |
| 6738 | break; |
| 6739 | case NWHITE: |
| 6740 | p = "NWHITE"; |
| 6741 | break; |
| 6742 | case NWHITE + ADD_NL: |
| 6743 | p = "NWHITE+NL"; |
| 6744 | break; |
| 6745 | case DIGIT: |
| 6746 | p = "DIGIT"; |
| 6747 | break; |
| 6748 | case DIGIT + ADD_NL: |
| 6749 | p = "DIGIT+NL"; |
| 6750 | break; |
| 6751 | case NDIGIT: |
| 6752 | p = "NDIGIT"; |
| 6753 | break; |
| 6754 | case NDIGIT + ADD_NL: |
| 6755 | p = "NDIGIT+NL"; |
| 6756 | break; |
| 6757 | case HEX: |
| 6758 | p = "HEX"; |
| 6759 | break; |
| 6760 | case HEX + ADD_NL: |
| 6761 | p = "HEX+NL"; |
| 6762 | break; |
| 6763 | case NHEX: |
| 6764 | p = "NHEX"; |
| 6765 | break; |
| 6766 | case NHEX + ADD_NL: |
| 6767 | p = "NHEX+NL"; |
| 6768 | break; |
| 6769 | case OCTAL: |
| 6770 | p = "OCTAL"; |
| 6771 | break; |
| 6772 | case OCTAL + ADD_NL: |
| 6773 | p = "OCTAL+NL"; |
| 6774 | break; |
| 6775 | case NOCTAL: |
| 6776 | p = "NOCTAL"; |
| 6777 | break; |
| 6778 | case NOCTAL + ADD_NL: |
| 6779 | p = "NOCTAL+NL"; |
| 6780 | break; |
| 6781 | case WORD: |
| 6782 | p = "WORD"; |
| 6783 | break; |
| 6784 | case WORD + ADD_NL: |
| 6785 | p = "WORD+NL"; |
| 6786 | break; |
| 6787 | case NWORD: |
| 6788 | p = "NWORD"; |
| 6789 | break; |
| 6790 | case NWORD + ADD_NL: |
| 6791 | p = "NWORD+NL"; |
| 6792 | break; |
| 6793 | case HEAD: |
| 6794 | p = "HEAD"; |
| 6795 | break; |
| 6796 | case HEAD + ADD_NL: |
| 6797 | p = "HEAD+NL"; |
| 6798 | break; |
| 6799 | case NHEAD: |
| 6800 | p = "NHEAD"; |
| 6801 | break; |
| 6802 | case NHEAD + ADD_NL: |
| 6803 | p = "NHEAD+NL"; |
| 6804 | break; |
| 6805 | case ALPHA: |
| 6806 | p = "ALPHA"; |
| 6807 | break; |
| 6808 | case ALPHA + ADD_NL: |
| 6809 | p = "ALPHA+NL"; |
| 6810 | break; |
| 6811 | case NALPHA: |
| 6812 | p = "NALPHA"; |
| 6813 | break; |
| 6814 | case NALPHA + ADD_NL: |
| 6815 | p = "NALPHA+NL"; |
| 6816 | break; |
| 6817 | case LOWER: |
| 6818 | p = "LOWER"; |
| 6819 | break; |
| 6820 | case LOWER + ADD_NL: |
| 6821 | p = "LOWER+NL"; |
| 6822 | break; |
| 6823 | case NLOWER: |
| 6824 | p = "NLOWER"; |
| 6825 | break; |
| 6826 | case NLOWER + ADD_NL: |
| 6827 | p = "NLOWER+NL"; |
| 6828 | break; |
| 6829 | case UPPER: |
| 6830 | p = "UPPER"; |
| 6831 | break; |
| 6832 | case UPPER + ADD_NL: |
| 6833 | p = "UPPER+NL"; |
| 6834 | break; |
| 6835 | case NUPPER: |
| 6836 | p = "NUPPER"; |
| 6837 | break; |
| 6838 | case NUPPER + ADD_NL: |
| 6839 | p = "NUPPER+NL"; |
| 6840 | break; |
| 6841 | case BRANCH: |
| 6842 | p = "BRANCH"; |
| 6843 | break; |
| 6844 | case EXACTLY: |
| 6845 | p = "EXACTLY"; |
| 6846 | break; |
| 6847 | case NOTHING: |
| 6848 | p = "NOTHING"; |
| 6849 | break; |
| 6850 | case BACK: |
| 6851 | p = "BACK"; |
| 6852 | break; |
| 6853 | case END: |
| 6854 | p = "END"; |
| 6855 | break; |
| 6856 | case MOPEN + 0: |
| 6857 | p = "MATCH START"; |
| 6858 | break; |
| 6859 | case MOPEN + 1: |
| 6860 | case MOPEN + 2: |
| 6861 | case MOPEN + 3: |
| 6862 | case MOPEN + 4: |
| 6863 | case MOPEN + 5: |
| 6864 | case MOPEN + 6: |
| 6865 | case MOPEN + 7: |
| 6866 | case MOPEN + 8: |
| 6867 | case MOPEN + 9: |
| 6868 | sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN); |
| 6869 | p = NULL; |
| 6870 | break; |
| 6871 | case MCLOSE + 0: |
| 6872 | p = "MATCH END"; |
| 6873 | break; |
| 6874 | case MCLOSE + 1: |
| 6875 | case MCLOSE + 2: |
| 6876 | case MCLOSE + 3: |
| 6877 | case MCLOSE + 4: |
| 6878 | case MCLOSE + 5: |
| 6879 | case MCLOSE + 6: |
| 6880 | case MCLOSE + 7: |
| 6881 | case MCLOSE + 8: |
| 6882 | case MCLOSE + 9: |
| 6883 | sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE); |
| 6884 | p = NULL; |
| 6885 | break; |
| 6886 | case BACKREF + 1: |
| 6887 | case BACKREF + 2: |
| 6888 | case BACKREF + 3: |
| 6889 | case BACKREF + 4: |
| 6890 | case BACKREF + 5: |
| 6891 | case BACKREF + 6: |
| 6892 | case BACKREF + 7: |
| 6893 | case BACKREF + 8: |
| 6894 | case BACKREF + 9: |
| 6895 | sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF); |
| 6896 | p = NULL; |
| 6897 | break; |
| 6898 | case NOPEN: |
| 6899 | p = "NOPEN"; |
| 6900 | break; |
| 6901 | case NCLOSE: |
| 6902 | p = "NCLOSE"; |
| 6903 | break; |
| 6904 | #ifdef FEAT_SYN_HL |
| 6905 | case ZOPEN + 1: |
| 6906 | case ZOPEN + 2: |
| 6907 | case ZOPEN + 3: |
| 6908 | case ZOPEN + 4: |
| 6909 | case ZOPEN + 5: |
| 6910 | case ZOPEN + 6: |
| 6911 | case ZOPEN + 7: |
| 6912 | case ZOPEN + 8: |
| 6913 | case ZOPEN + 9: |
| 6914 | sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN); |
| 6915 | p = NULL; |
| 6916 | break; |
| 6917 | case ZCLOSE + 1: |
| 6918 | case ZCLOSE + 2: |
| 6919 | case ZCLOSE + 3: |
| 6920 | case ZCLOSE + 4: |
| 6921 | case ZCLOSE + 5: |
| 6922 | case ZCLOSE + 6: |
| 6923 | case ZCLOSE + 7: |
| 6924 | case ZCLOSE + 8: |
| 6925 | case ZCLOSE + 9: |
| 6926 | sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE); |
| 6927 | p = NULL; |
| 6928 | break; |
| 6929 | case ZREF + 1: |
| 6930 | case ZREF + 2: |
| 6931 | case ZREF + 3: |
| 6932 | case ZREF + 4: |
| 6933 | case ZREF + 5: |
| 6934 | case ZREF + 6: |
| 6935 | case ZREF + 7: |
| 6936 | case ZREF + 8: |
| 6937 | case ZREF + 9: |
| 6938 | sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF); |
| 6939 | p = NULL; |
| 6940 | break; |
| 6941 | #endif |
| 6942 | case STAR: |
| 6943 | p = "STAR"; |
| 6944 | break; |
| 6945 | case PLUS: |
| 6946 | p = "PLUS"; |
| 6947 | break; |
| 6948 | case NOMATCH: |
| 6949 | p = "NOMATCH"; |
| 6950 | break; |
| 6951 | case MATCH: |
| 6952 | p = "MATCH"; |
| 6953 | break; |
| 6954 | case BEHIND: |
| 6955 | p = "BEHIND"; |
| 6956 | break; |
| 6957 | case NOBEHIND: |
| 6958 | p = "NOBEHIND"; |
| 6959 | break; |
| 6960 | case SUBPAT: |
| 6961 | p = "SUBPAT"; |
| 6962 | break; |
| 6963 | case BRACE_LIMITS: |
| 6964 | p = "BRACE_LIMITS"; |
| 6965 | break; |
| 6966 | case BRACE_SIMPLE: |
| 6967 | p = "BRACE_SIMPLE"; |
| 6968 | break; |
| 6969 | case BRACE_COMPLEX + 0: |
| 6970 | case BRACE_COMPLEX + 1: |
| 6971 | case BRACE_COMPLEX + 2: |
| 6972 | case BRACE_COMPLEX + 3: |
| 6973 | case BRACE_COMPLEX + 4: |
| 6974 | case BRACE_COMPLEX + 5: |
| 6975 | case BRACE_COMPLEX + 6: |
| 6976 | case BRACE_COMPLEX + 7: |
| 6977 | case BRACE_COMPLEX + 8: |
| 6978 | case BRACE_COMPLEX + 9: |
| 6979 | sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX); |
| 6980 | p = NULL; |
| 6981 | break; |
| 6982 | #ifdef FEAT_MBYTE |
| 6983 | case MULTIBYTECODE: |
| 6984 | p = "MULTIBYTECODE"; |
| 6985 | break; |
| 6986 | #endif |
| 6987 | case NEWL: |
| 6988 | p = "NEWL"; |
| 6989 | break; |
| 6990 | default: |
| 6991 | sprintf(buf + STRLEN(buf), "corrupt %d", OP(op)); |
| 6992 | p = NULL; |
| 6993 | break; |
| 6994 | } |
| 6995 | if (p != NULL) |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6996 | STRCAT(buf, p); |
| 6997 | return (char_u *)buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6998 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 6999 | #endif /* DEBUG */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7000 | |
| 7001 | #ifdef FEAT_MBYTE |
| 7002 | static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3)); |
| 7003 | |
| 7004 | typedef struct |
| 7005 | { |
| 7006 | int a, b, c; |
| 7007 | } decomp_T; |
| 7008 | |
| 7009 | |
| 7010 | /* 0xfb20 - 0xfb4f */ |
Bram Moolenaar | d6f676d | 2005-06-01 21:51:55 +0000 | [diff] [blame] | 7011 | static decomp_T decomp_table[0xfb4f-0xfb20+1] = |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7012 | { |
| 7013 | {0x5e2,0,0}, /* 0xfb20 alt ayin */ |
| 7014 | {0x5d0,0,0}, /* 0xfb21 alt alef */ |
| 7015 | {0x5d3,0,0}, /* 0xfb22 alt dalet */ |
| 7016 | {0x5d4,0,0}, /* 0xfb23 alt he */ |
| 7017 | {0x5db,0,0}, /* 0xfb24 alt kaf */ |
| 7018 | {0x5dc,0,0}, /* 0xfb25 alt lamed */ |
| 7019 | {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */ |
| 7020 | {0x5e8,0,0}, /* 0xfb27 alt resh */ |
| 7021 | {0x5ea,0,0}, /* 0xfb28 alt tav */ |
| 7022 | {'+', 0, 0}, /* 0xfb29 alt plus */ |
| 7023 | {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */ |
| 7024 | {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */ |
| 7025 | {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */ |
| 7026 | {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */ |
| 7027 | {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */ |
| 7028 | {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */ |
| 7029 | {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */ |
| 7030 | {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */ |
| 7031 | {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */ |
| 7032 | {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */ |
| 7033 | {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */ |
| 7034 | {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */ |
| 7035 | {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */ |
| 7036 | {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */ |
| 7037 | {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */ |
| 7038 | {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */ |
| 7039 | {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */ |
| 7040 | {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */ |
| 7041 | {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */ |
| 7042 | {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */ |
| 7043 | {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */ |
| 7044 | {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */ |
| 7045 | {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */ |
| 7046 | {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */ |
| 7047 | {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */ |
| 7048 | {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */ |
| 7049 | {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */ |
| 7050 | {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */ |
| 7051 | {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */ |
| 7052 | {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */ |
| 7053 | {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */ |
| 7054 | {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */ |
| 7055 | {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */ |
| 7056 | {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */ |
| 7057 | {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */ |
| 7058 | {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */ |
| 7059 | {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */ |
| 7060 | {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */ |
| 7061 | }; |
| 7062 | |
| 7063 | static void |
| 7064 | mb_decompose(c, c1, c2, c3) |
| 7065 | int c, *c1, *c2, *c3; |
| 7066 | { |
| 7067 | decomp_T d; |
| 7068 | |
Bram Moolenaar | 2eec59e | 2013-05-21 21:37:20 +0200 | [diff] [blame] | 7069 | if (c >= 0xfb20 && c <= 0xfb4f) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7070 | { |
| 7071 | d = decomp_table[c - 0xfb20]; |
| 7072 | *c1 = d.a; |
| 7073 | *c2 = d.b; |
| 7074 | *c3 = d.c; |
| 7075 | } |
| 7076 | else |
| 7077 | { |
| 7078 | *c1 = c; |
| 7079 | *c2 = *c3 = 0; |
| 7080 | } |
| 7081 | } |
| 7082 | #endif |
| 7083 | |
| 7084 | /* |
| 7085 | * Compare two strings, ignore case if ireg_ic set. |
| 7086 | * Return 0 if strings match, non-zero otherwise. |
| 7087 | * Correct the length "*n" when composing characters are ignored. |
| 7088 | */ |
| 7089 | static int |
| 7090 | cstrncmp(s1, s2, n) |
| 7091 | char_u *s1, *s2; |
| 7092 | int *n; |
| 7093 | { |
| 7094 | int result; |
| 7095 | |
| 7096 | if (!ireg_ic) |
| 7097 | result = STRNCMP(s1, s2, *n); |
| 7098 | else |
| 7099 | result = MB_STRNICMP(s1, s2, *n); |
| 7100 | |
| 7101 | #ifdef FEAT_MBYTE |
| 7102 | /* if it failed and it's utf8 and we want to combineignore: */ |
| 7103 | if (result != 0 && enc_utf8 && ireg_icombine) |
| 7104 | { |
| 7105 | char_u *str1, *str2; |
| 7106 | int c1, c2, c11, c12; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7107 | int junk; |
| 7108 | |
| 7109 | /* we have to handle the strcmp ourselves, since it is necessary to |
| 7110 | * deal with the composing characters by ignoring them: */ |
| 7111 | str1 = s1; |
| 7112 | str2 = s2; |
| 7113 | c1 = c2 = 0; |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 7114 | while ((int)(str1 - s1) < *n) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7115 | { |
| 7116 | c1 = mb_ptr2char_adv(&str1); |
| 7117 | c2 = mb_ptr2char_adv(&str2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7118 | |
| 7119 | /* decompose the character if necessary, into 'base' characters |
| 7120 | * because I don't care about Arabic, I will hard-code the Hebrew |
| 7121 | * which I *do* care about! So sue me... */ |
| 7122 | if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2))) |
| 7123 | { |
| 7124 | /* decomposition necessary? */ |
| 7125 | mb_decompose(c1, &c11, &junk, &junk); |
| 7126 | mb_decompose(c2, &c12, &junk, &junk); |
| 7127 | c1 = c11; |
| 7128 | c2 = c12; |
| 7129 | if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12))) |
| 7130 | break; |
| 7131 | } |
| 7132 | } |
| 7133 | result = c2 - c1; |
| 7134 | if (result == 0) |
| 7135 | *n = (int)(str2 - s2); |
| 7136 | } |
| 7137 | #endif |
| 7138 | |
| 7139 | return result; |
| 7140 | } |
| 7141 | |
| 7142 | /* |
| 7143 | * cstrchr: This function is used a lot for simple searches, keep it fast! |
| 7144 | */ |
| 7145 | static char_u * |
| 7146 | cstrchr(s, c) |
| 7147 | char_u *s; |
| 7148 | int c; |
| 7149 | { |
| 7150 | char_u *p; |
| 7151 | int cc; |
| 7152 | |
| 7153 | if (!ireg_ic |
| 7154 | #ifdef FEAT_MBYTE |
| 7155 | || (!enc_utf8 && mb_char2len(c) > 1) |
| 7156 | #endif |
| 7157 | ) |
| 7158 | return vim_strchr(s, c); |
| 7159 | |
| 7160 | /* tolower() and toupper() can be slow, comparing twice should be a lot |
| 7161 | * faster (esp. when using MS Visual C++!). |
| 7162 | * For UTF-8 need to use folded case. */ |
| 7163 | #ifdef FEAT_MBYTE |
| 7164 | if (enc_utf8 && c > 0x80) |
| 7165 | cc = utf_fold(c); |
| 7166 | else |
| 7167 | #endif |
Bram Moolenaar | a245a5b | 2007-08-11 11:58:23 +0000 | [diff] [blame] | 7168 | if (MB_ISUPPER(c)) |
| 7169 | cc = MB_TOLOWER(c); |
| 7170 | else if (MB_ISLOWER(c)) |
| 7171 | cc = MB_TOUPPER(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7172 | else |
| 7173 | return vim_strchr(s, c); |
| 7174 | |
| 7175 | #ifdef FEAT_MBYTE |
| 7176 | if (has_mbyte) |
| 7177 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7178 | for (p = s; *p != NUL; p += (*mb_ptr2len)(p)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7179 | { |
| 7180 | if (enc_utf8 && c > 0x80) |
| 7181 | { |
| 7182 | if (utf_fold(utf_ptr2char(p)) == cc) |
| 7183 | return p; |
| 7184 | } |
| 7185 | else if (*p == c || *p == cc) |
| 7186 | return p; |
| 7187 | } |
| 7188 | } |
| 7189 | else |
| 7190 | #endif |
| 7191 | /* Faster version for when there are no multi-byte characters. */ |
| 7192 | for (p = s; *p != NUL; ++p) |
| 7193 | if (*p == c || *p == cc) |
| 7194 | return p; |
| 7195 | |
| 7196 | return NULL; |
| 7197 | } |
| 7198 | |
| 7199 | /*************************************************************** |
| 7200 | * regsub stuff * |
| 7201 | ***************************************************************/ |
| 7202 | |
| 7203 | /* This stuff below really confuses cc on an SGI -- webb */ |
| 7204 | #ifdef __sgi |
| 7205 | # undef __ARGS |
| 7206 | # define __ARGS(x) () |
| 7207 | #endif |
| 7208 | |
| 7209 | /* |
| 7210 | * We should define ftpr as a pointer to a function returning a pointer to |
| 7211 | * a function returning a pointer to a function ... |
| 7212 | * This is impossible, so we declare a pointer to a function returning a |
| 7213 | * pointer to a function returning void. This should work for all compilers. |
| 7214 | */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7215 | typedef void (*(*fptr_T) __ARGS((int *, int)))(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7216 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7217 | static fptr_T do_upper __ARGS((int *, int)); |
| 7218 | static fptr_T do_Upper __ARGS((int *, int)); |
| 7219 | static fptr_T do_lower __ARGS((int *, int)); |
| 7220 | static fptr_T do_Lower __ARGS((int *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7221 | |
| 7222 | static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash)); |
| 7223 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7224 | static fptr_T |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7225 | do_upper(d, c) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7226 | int *d; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7227 | int c; |
| 7228 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7229 | *d = MB_TOUPPER(c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7230 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7231 | return (fptr_T)NULL; |
| 7232 | } |
| 7233 | |
| 7234 | static fptr_T |
| 7235 | do_Upper(d, c) |
| 7236 | int *d; |
| 7237 | int c; |
| 7238 | { |
| 7239 | *d = MB_TOUPPER(c); |
| 7240 | |
| 7241 | return (fptr_T)do_Upper; |
| 7242 | } |
| 7243 | |
| 7244 | static fptr_T |
| 7245 | do_lower(d, c) |
| 7246 | int *d; |
| 7247 | int c; |
| 7248 | { |
| 7249 | *d = MB_TOLOWER(c); |
| 7250 | |
| 7251 | return (fptr_T)NULL; |
| 7252 | } |
| 7253 | |
| 7254 | static fptr_T |
| 7255 | do_Lower(d, c) |
| 7256 | int *d; |
| 7257 | int c; |
| 7258 | { |
| 7259 | *d = MB_TOLOWER(c); |
| 7260 | |
| 7261 | return (fptr_T)do_Lower; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7262 | } |
| 7263 | |
| 7264 | /* |
| 7265 | * regtilde(): Replace tildes in the pattern by the old pattern. |
| 7266 | * |
| 7267 | * Short explanation of the tilde: It stands for the previous replacement |
| 7268 | * pattern. If that previous pattern also contains a ~ we should go back a |
| 7269 | * step further... But we insert the previous pattern into the current one |
| 7270 | * and remember that. |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7271 | * This still does not handle the case where "magic" changes. So require the |
| 7272 | * user to keep his hands off of "magic". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7273 | * |
| 7274 | * The tildes are parsed once before the first call to vim_regsub(). |
| 7275 | */ |
| 7276 | char_u * |
| 7277 | regtilde(source, magic) |
| 7278 | char_u *source; |
| 7279 | int magic; |
| 7280 | { |
| 7281 | char_u *newsub = source; |
| 7282 | char_u *tmpsub; |
| 7283 | char_u *p; |
| 7284 | int len; |
| 7285 | int prevlen; |
| 7286 | |
| 7287 | for (p = newsub; *p; ++p) |
| 7288 | { |
| 7289 | if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic)) |
| 7290 | { |
| 7291 | if (reg_prev_sub != NULL) |
| 7292 | { |
| 7293 | /* length = len(newsub) - 1 + len(prev_sub) + 1 */ |
| 7294 | prevlen = (int)STRLEN(reg_prev_sub); |
| 7295 | tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen)); |
| 7296 | if (tmpsub != NULL) |
| 7297 | { |
| 7298 | /* copy prefix */ |
| 7299 | len = (int)(p - newsub); /* not including ~ */ |
| 7300 | mch_memmove(tmpsub, newsub, (size_t)len); |
Bram Moolenaar | 7aa9f6a | 2007-05-10 18:00:30 +0000 | [diff] [blame] | 7301 | /* interpret tilde */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7302 | mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen); |
| 7303 | /* copy postfix */ |
| 7304 | if (!magic) |
| 7305 | ++p; /* back off \ */ |
| 7306 | STRCPY(tmpsub + len + prevlen, p + 1); |
| 7307 | |
| 7308 | if (newsub != source) /* already allocated newsub */ |
| 7309 | vim_free(newsub); |
| 7310 | newsub = tmpsub; |
| 7311 | p = newsub + len + prevlen; |
| 7312 | } |
| 7313 | } |
| 7314 | else if (magic) |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 7315 | STRMOVE(p, p + 1); /* remove '~' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7316 | else |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 7317 | STRMOVE(p, p + 2); /* remove '\~' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7318 | --p; |
| 7319 | } |
| 7320 | else |
| 7321 | { |
| 7322 | if (*p == '\\' && p[1]) /* skip escaped characters */ |
| 7323 | ++p; |
| 7324 | #ifdef FEAT_MBYTE |
| 7325 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 7326 | p += (*mb_ptr2len)(p) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7327 | #endif |
| 7328 | } |
| 7329 | } |
| 7330 | |
| 7331 | vim_free(reg_prev_sub); |
| 7332 | if (newsub != source) /* newsub was allocated, just keep it */ |
| 7333 | reg_prev_sub = newsub; |
| 7334 | else /* no ~ found, need to save newsub */ |
| 7335 | reg_prev_sub = vim_strsave(newsub); |
| 7336 | return newsub; |
| 7337 | } |
| 7338 | |
| 7339 | #ifdef FEAT_EVAL |
| 7340 | static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */ |
| 7341 | |
| 7342 | /* These pointers are used instead of reg_match and reg_mmatch for |
| 7343 | * reg_submatch(). Needed for when the substitution string is an expression |
| 7344 | * that contains a call to substitute() and submatch(). */ |
| 7345 | static regmatch_T *submatch_match; |
| 7346 | static regmmatch_T *submatch_mmatch; |
Bram Moolenaar | 5ea08a8 | 2009-11-25 18:51:24 +0000 | [diff] [blame] | 7347 | static linenr_T submatch_firstlnum; |
| 7348 | static linenr_T submatch_maxline; |
Bram Moolenaar | 978287b | 2011-06-19 04:32:15 +0200 | [diff] [blame] | 7349 | static int submatch_line_lbr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7350 | #endif |
| 7351 | |
| 7352 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO) |
| 7353 | /* |
| 7354 | * vim_regsub() - perform substitutions after a vim_regexec() or |
| 7355 | * vim_regexec_multi() match. |
| 7356 | * |
| 7357 | * If "copy" is TRUE really copy into "dest". |
| 7358 | * If "copy" is FALSE nothing is copied, this is just to find out the length |
| 7359 | * of the result. |
| 7360 | * |
| 7361 | * If "backslash" is TRUE, a backslash will be removed later, need to double |
| 7362 | * them to keep them, and insert a backslash before a CR to avoid it being |
| 7363 | * replaced with a line break later. |
| 7364 | * |
| 7365 | * Note: The matched text must not change between the call of |
| 7366 | * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back |
| 7367 | * references invalid! |
| 7368 | * |
| 7369 | * Returns the size of the replacement, including terminating NUL. |
| 7370 | */ |
| 7371 | int |
| 7372 | vim_regsub(rmp, source, dest, copy, magic, backslash) |
| 7373 | regmatch_T *rmp; |
| 7374 | char_u *source; |
| 7375 | char_u *dest; |
| 7376 | int copy; |
| 7377 | int magic; |
| 7378 | int backslash; |
| 7379 | { |
| 7380 | reg_match = rmp; |
| 7381 | reg_mmatch = NULL; |
| 7382 | reg_maxline = 0; |
Bram Moolenaar | 2f315ab | 2013-01-25 20:11:01 +0100 | [diff] [blame] | 7383 | reg_buf = curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7384 | return vim_regsub_both(source, dest, copy, magic, backslash); |
| 7385 | } |
| 7386 | #endif |
| 7387 | |
| 7388 | int |
| 7389 | vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash) |
| 7390 | regmmatch_T *rmp; |
| 7391 | linenr_T lnum; |
| 7392 | char_u *source; |
| 7393 | char_u *dest; |
| 7394 | int copy; |
| 7395 | int magic; |
| 7396 | int backslash; |
| 7397 | { |
| 7398 | reg_match = NULL; |
| 7399 | reg_mmatch = rmp; |
| 7400 | reg_buf = curbuf; /* always works on the current buffer! */ |
| 7401 | reg_firstlnum = lnum; |
| 7402 | reg_maxline = curbuf->b_ml.ml_line_count - lnum; |
| 7403 | return vim_regsub_both(source, dest, copy, magic, backslash); |
| 7404 | } |
| 7405 | |
| 7406 | static int |
| 7407 | vim_regsub_both(source, dest, copy, magic, backslash) |
| 7408 | char_u *source; |
| 7409 | char_u *dest; |
| 7410 | int copy; |
| 7411 | int magic; |
| 7412 | int backslash; |
| 7413 | { |
| 7414 | char_u *src; |
| 7415 | char_u *dst; |
| 7416 | char_u *s; |
| 7417 | int c; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7418 | int cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7419 | int no = -1; |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7420 | fptr_T func_all = (fptr_T)NULL; |
| 7421 | fptr_T func_one = (fptr_T)NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7422 | linenr_T clnum = 0; /* init for GCC */ |
| 7423 | int len = 0; /* init for GCC */ |
| 7424 | #ifdef FEAT_EVAL |
| 7425 | static char_u *eval_result = NULL; |
| 7426 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7427 | |
| 7428 | /* Be paranoid... */ |
| 7429 | if (source == NULL || dest == NULL) |
| 7430 | { |
| 7431 | EMSG(_(e_null)); |
| 7432 | return 0; |
| 7433 | } |
| 7434 | if (prog_magic_wrong()) |
| 7435 | return 0; |
| 7436 | src = source; |
| 7437 | dst = dest; |
| 7438 | |
| 7439 | /* |
| 7440 | * When the substitute part starts with "\=" evaluate it as an expression. |
| 7441 | */ |
| 7442 | if (source[0] == '\\' && source[1] == '=' |
| 7443 | #ifdef FEAT_EVAL |
| 7444 | && !can_f_submatch /* can't do this recursively */ |
| 7445 | #endif |
| 7446 | ) |
| 7447 | { |
| 7448 | #ifdef FEAT_EVAL |
| 7449 | /* To make sure that the length doesn't change between checking the |
| 7450 | * length and copying the string, and to speed up things, the |
| 7451 | * resulting string is saved from the call with "copy" == FALSE to the |
| 7452 | * call with "copy" == TRUE. */ |
| 7453 | if (copy) |
| 7454 | { |
| 7455 | if (eval_result != NULL) |
| 7456 | { |
| 7457 | STRCPY(dest, eval_result); |
| 7458 | dst += STRLEN(eval_result); |
| 7459 | vim_free(eval_result); |
| 7460 | eval_result = NULL; |
| 7461 | } |
| 7462 | } |
| 7463 | else |
| 7464 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7465 | win_T *save_reg_win; |
| 7466 | int save_ireg_ic; |
| 7467 | |
| 7468 | vim_free(eval_result); |
| 7469 | |
| 7470 | /* The expression may contain substitute(), which calls us |
| 7471 | * recursively. Make sure submatch() gets the text from the first |
| 7472 | * level. Don't need to save "reg_buf", because |
| 7473 | * vim_regexec_multi() can't be called recursively. */ |
| 7474 | submatch_match = reg_match; |
| 7475 | submatch_mmatch = reg_mmatch; |
Bram Moolenaar | 5ea08a8 | 2009-11-25 18:51:24 +0000 | [diff] [blame] | 7476 | submatch_firstlnum = reg_firstlnum; |
| 7477 | submatch_maxline = reg_maxline; |
Bram Moolenaar | 978287b | 2011-06-19 04:32:15 +0200 | [diff] [blame] | 7478 | submatch_line_lbr = reg_line_lbr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7479 | save_reg_win = reg_win; |
| 7480 | save_ireg_ic = ireg_ic; |
| 7481 | can_f_submatch = TRUE; |
| 7482 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 7483 | eval_result = eval_to_string(source + 2, NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7484 | if (eval_result != NULL) |
| 7485 | { |
Bram Moolenaar | 06975a4 | 2010-03-23 16:27:22 +0100 | [diff] [blame] | 7486 | int had_backslash = FALSE; |
| 7487 | |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 7488 | for (s = eval_result; *s != NUL; mb_ptr_adv(s)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7489 | { |
Bram Moolenaar | 978287b | 2011-06-19 04:32:15 +0200 | [diff] [blame] | 7490 | /* Change NL to CR, so that it becomes a line break, |
| 7491 | * unless called from vim_regexec_nl(). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7492 | * Skip over a backslashed character. */ |
Bram Moolenaar | 978287b | 2011-06-19 04:32:15 +0200 | [diff] [blame] | 7493 | if (*s == NL && !submatch_line_lbr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7494 | *s = CAR; |
| 7495 | else if (*s == '\\' && s[1] != NUL) |
Bram Moolenaar | 06975a4 | 2010-03-23 16:27:22 +0100 | [diff] [blame] | 7496 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7497 | ++s; |
Bram Moolenaar | 6019078 | 2010-05-21 13:08:58 +0200 | [diff] [blame] | 7498 | /* Change NL to CR here too, so that this works: |
| 7499 | * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text: |
| 7500 | * abc\ |
| 7501 | * def |
Bram Moolenaar | 978287b | 2011-06-19 04:32:15 +0200 | [diff] [blame] | 7502 | * Not when called from vim_regexec_nl(). |
Bram Moolenaar | 6019078 | 2010-05-21 13:08:58 +0200 | [diff] [blame] | 7503 | */ |
Bram Moolenaar | 978287b | 2011-06-19 04:32:15 +0200 | [diff] [blame] | 7504 | if (*s == NL && !submatch_line_lbr) |
Bram Moolenaar | 6019078 | 2010-05-21 13:08:58 +0200 | [diff] [blame] | 7505 | *s = CAR; |
Bram Moolenaar | 06975a4 | 2010-03-23 16:27:22 +0100 | [diff] [blame] | 7506 | had_backslash = TRUE; |
| 7507 | } |
| 7508 | } |
| 7509 | if (had_backslash && backslash) |
| 7510 | { |
| 7511 | /* Backslashes will be consumed, need to double them. */ |
| 7512 | s = vim_strsave_escaped(eval_result, (char_u *)"\\"); |
| 7513 | if (s != NULL) |
| 7514 | { |
| 7515 | vim_free(eval_result); |
| 7516 | eval_result = s; |
| 7517 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7518 | } |
| 7519 | |
| 7520 | dst += STRLEN(eval_result); |
| 7521 | } |
| 7522 | |
| 7523 | reg_match = submatch_match; |
| 7524 | reg_mmatch = submatch_mmatch; |
Bram Moolenaar | 5ea08a8 | 2009-11-25 18:51:24 +0000 | [diff] [blame] | 7525 | reg_firstlnum = submatch_firstlnum; |
| 7526 | reg_maxline = submatch_maxline; |
Bram Moolenaar | 978287b | 2011-06-19 04:32:15 +0200 | [diff] [blame] | 7527 | reg_line_lbr = submatch_line_lbr; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7528 | reg_win = save_reg_win; |
| 7529 | ireg_ic = save_ireg_ic; |
| 7530 | can_f_submatch = FALSE; |
| 7531 | } |
| 7532 | #endif |
| 7533 | } |
| 7534 | else |
| 7535 | while ((c = *src++) != NUL) |
| 7536 | { |
| 7537 | if (c == '&' && magic) |
| 7538 | no = 0; |
| 7539 | else if (c == '\\' && *src != NUL) |
| 7540 | { |
| 7541 | if (*src == '&' && !magic) |
| 7542 | { |
| 7543 | ++src; |
| 7544 | no = 0; |
| 7545 | } |
| 7546 | else if ('0' <= *src && *src <= '9') |
| 7547 | { |
| 7548 | no = *src++ - '0'; |
| 7549 | } |
| 7550 | else if (vim_strchr((char_u *)"uUlLeE", *src)) |
| 7551 | { |
| 7552 | switch (*src++) |
| 7553 | { |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7554 | case 'u': func_one = (fptr_T)do_upper; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7555 | continue; |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7556 | case 'U': func_all = (fptr_T)do_Upper; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7557 | continue; |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7558 | case 'l': func_one = (fptr_T)do_lower; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7559 | continue; |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7560 | case 'L': func_all = (fptr_T)do_Lower; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7561 | continue; |
| 7562 | case 'e': |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7563 | case 'E': func_one = func_all = (fptr_T)NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7564 | continue; |
| 7565 | } |
| 7566 | } |
| 7567 | } |
| 7568 | if (no < 0) /* Ordinary character. */ |
| 7569 | { |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 7570 | if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL) |
| 7571 | { |
Bram Moolenaar | 7aa9f6a | 2007-05-10 18:00:30 +0000 | [diff] [blame] | 7572 | /* Copy a special key as-is. */ |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 7573 | if (copy) |
| 7574 | { |
| 7575 | *dst++ = c; |
| 7576 | *dst++ = *src++; |
| 7577 | *dst++ = *src++; |
| 7578 | } |
| 7579 | else |
| 7580 | { |
| 7581 | dst += 3; |
| 7582 | src += 2; |
| 7583 | } |
| 7584 | continue; |
| 7585 | } |
| 7586 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7587 | if (c == '\\' && *src != NUL) |
| 7588 | { |
| 7589 | /* Check for abbreviations -- webb */ |
| 7590 | switch (*src) |
| 7591 | { |
| 7592 | case 'r': c = CAR; ++src; break; |
| 7593 | case 'n': c = NL; ++src; break; |
| 7594 | case 't': c = TAB; ++src; break; |
| 7595 | /* Oh no! \e already has meaning in subst pat :-( */ |
| 7596 | /* case 'e': c = ESC; ++src; break; */ |
| 7597 | case 'b': c = Ctrl_H; ++src; break; |
| 7598 | |
| 7599 | /* If "backslash" is TRUE the backslash will be removed |
| 7600 | * later. Used to insert a literal CR. */ |
| 7601 | default: if (backslash) |
| 7602 | { |
| 7603 | if (copy) |
| 7604 | *dst = '\\'; |
| 7605 | ++dst; |
| 7606 | } |
| 7607 | c = *src++; |
| 7608 | } |
| 7609 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7610 | #ifdef FEAT_MBYTE |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 7611 | else if (has_mbyte) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7612 | c = mb_ptr2char(src - 1); |
| 7613 | #endif |
| 7614 | |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 7615 | /* Write to buffer, if copy is set. */ |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7616 | if (func_one != (fptr_T)NULL) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7617 | /* Turbo C complains without the typecast */ |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7618 | func_one = (fptr_T)(func_one(&cc, c)); |
| 7619 | else if (func_all != (fptr_T)NULL) |
| 7620 | /* Turbo C complains without the typecast */ |
| 7621 | func_all = (fptr_T)(func_all(&cc, c)); |
| 7622 | else /* just copy */ |
| 7623 | cc = c; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7624 | |
| 7625 | #ifdef FEAT_MBYTE |
| 7626 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7627 | { |
Bram Moolenaar | 0c56c60 | 2010-07-12 22:42:33 +0200 | [diff] [blame] | 7628 | int totlen = mb_ptr2len(src - 1); |
| 7629 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7630 | if (copy) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7631 | mb_char2bytes(cc, dst); |
| 7632 | dst += mb_char2len(cc) - 1; |
Bram Moolenaar | 0c56c60 | 2010-07-12 22:42:33 +0200 | [diff] [blame] | 7633 | if (enc_utf8) |
| 7634 | { |
| 7635 | int clen = utf_ptr2len(src - 1); |
| 7636 | |
| 7637 | /* If the character length is shorter than "totlen", there |
| 7638 | * are composing characters; copy them as-is. */ |
| 7639 | if (clen < totlen) |
| 7640 | { |
| 7641 | if (copy) |
| 7642 | mch_memmove(dst + 1, src - 1 + clen, |
| 7643 | (size_t)(totlen - clen)); |
| 7644 | dst += totlen - clen; |
| 7645 | } |
| 7646 | } |
| 7647 | src += totlen - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7648 | } |
| 7649 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7650 | #endif |
| 7651 | if (copy) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7652 | *dst = cc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7653 | dst++; |
| 7654 | } |
| 7655 | else |
| 7656 | { |
| 7657 | if (REG_MULTI) |
| 7658 | { |
| 7659 | clnum = reg_mmatch->startpos[no].lnum; |
| 7660 | if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0) |
| 7661 | s = NULL; |
| 7662 | else |
| 7663 | { |
| 7664 | s = reg_getline(clnum) + reg_mmatch->startpos[no].col; |
| 7665 | if (reg_mmatch->endpos[no].lnum == clnum) |
| 7666 | len = reg_mmatch->endpos[no].col |
| 7667 | - reg_mmatch->startpos[no].col; |
| 7668 | else |
| 7669 | len = (int)STRLEN(s); |
| 7670 | } |
| 7671 | } |
| 7672 | else |
| 7673 | { |
| 7674 | s = reg_match->startp[no]; |
| 7675 | if (reg_match->endp[no] == NULL) |
| 7676 | s = NULL; |
| 7677 | else |
| 7678 | len = (int)(reg_match->endp[no] - s); |
| 7679 | } |
| 7680 | if (s != NULL) |
| 7681 | { |
| 7682 | for (;;) |
| 7683 | { |
| 7684 | if (len == 0) |
| 7685 | { |
| 7686 | if (REG_MULTI) |
| 7687 | { |
| 7688 | if (reg_mmatch->endpos[no].lnum == clnum) |
| 7689 | break; |
| 7690 | if (copy) |
| 7691 | *dst = CAR; |
| 7692 | ++dst; |
| 7693 | s = reg_getline(++clnum); |
| 7694 | if (reg_mmatch->endpos[no].lnum == clnum) |
| 7695 | len = reg_mmatch->endpos[no].col; |
| 7696 | else |
| 7697 | len = (int)STRLEN(s); |
| 7698 | } |
| 7699 | else |
| 7700 | break; |
| 7701 | } |
| 7702 | else if (*s == NUL) /* we hit NUL. */ |
| 7703 | { |
| 7704 | if (copy) |
| 7705 | EMSG(_(e_re_damg)); |
| 7706 | goto exit; |
| 7707 | } |
| 7708 | else |
| 7709 | { |
| 7710 | if (backslash && (*s == CAR || *s == '\\')) |
| 7711 | { |
| 7712 | /* |
| 7713 | * Insert a backslash in front of a CR, otherwise |
| 7714 | * it will be replaced by a line break. |
| 7715 | * Number of backslashes will be halved later, |
| 7716 | * double them here. |
| 7717 | */ |
| 7718 | if (copy) |
| 7719 | { |
| 7720 | dst[0] = '\\'; |
| 7721 | dst[1] = *s; |
| 7722 | } |
| 7723 | dst += 2; |
| 7724 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7725 | else |
| 7726 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7727 | #ifdef FEAT_MBYTE |
| 7728 | if (has_mbyte) |
| 7729 | c = mb_ptr2char(s); |
| 7730 | else |
| 7731 | #endif |
| 7732 | c = *s; |
| 7733 | |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7734 | if (func_one != (fptr_T)NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7735 | /* Turbo C complains without the typecast */ |
Bram Moolenaar | c2c355d | 2013-03-19 17:42:15 +0100 | [diff] [blame] | 7736 | func_one = (fptr_T)(func_one(&cc, c)); |
| 7737 | else if (func_all != (fptr_T)NULL) |
| 7738 | /* Turbo C complains without the typecast */ |
| 7739 | func_all = (fptr_T)(func_all(&cc, c)); |
| 7740 | else /* just copy */ |
| 7741 | cc = c; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7742 | |
| 7743 | #ifdef FEAT_MBYTE |
| 7744 | if (has_mbyte) |
| 7745 | { |
Bram Moolenaar | 9225efb | 2007-07-30 20:32:53 +0000 | [diff] [blame] | 7746 | int l; |
| 7747 | |
| 7748 | /* Copy composing characters separately, one |
| 7749 | * at a time. */ |
| 7750 | if (enc_utf8) |
| 7751 | l = utf_ptr2len(s) - 1; |
| 7752 | else |
| 7753 | l = mb_ptr2len(s) - 1; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7754 | |
| 7755 | s += l; |
| 7756 | len -= l; |
| 7757 | if (copy) |
| 7758 | mb_char2bytes(cc, dst); |
| 7759 | dst += mb_char2len(cc) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7760 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7761 | else |
| 7762 | #endif |
| 7763 | if (copy) |
| 7764 | *dst = cc; |
| 7765 | dst++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7766 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 7767 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7768 | ++s; |
| 7769 | --len; |
| 7770 | } |
| 7771 | } |
| 7772 | } |
| 7773 | no = -1; |
| 7774 | } |
| 7775 | } |
| 7776 | if (copy) |
| 7777 | *dst = NUL; |
| 7778 | |
| 7779 | exit: |
| 7780 | return (int)((dst - dest) + 1); |
| 7781 | } |
| 7782 | |
| 7783 | #ifdef FEAT_EVAL |
Bram Moolenaar | d32a319 | 2009-11-26 19:40:49 +0000 | [diff] [blame] | 7784 | static char_u *reg_getline_submatch __ARGS((linenr_T lnum)); |
| 7785 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7786 | /* |
Bram Moolenaar | 5ea08a8 | 2009-11-25 18:51:24 +0000 | [diff] [blame] | 7787 | * Call reg_getline() with the line numbers from the submatch. If a |
| 7788 | * substitute() was used the reg_maxline and other values have been |
| 7789 | * overwritten. |
| 7790 | */ |
| 7791 | static char_u * |
| 7792 | reg_getline_submatch(lnum) |
| 7793 | linenr_T lnum; |
| 7794 | { |
| 7795 | char_u *s; |
| 7796 | linenr_T save_first = reg_firstlnum; |
| 7797 | linenr_T save_max = reg_maxline; |
| 7798 | |
| 7799 | reg_firstlnum = submatch_firstlnum; |
| 7800 | reg_maxline = submatch_maxline; |
| 7801 | |
| 7802 | s = reg_getline(lnum); |
| 7803 | |
| 7804 | reg_firstlnum = save_first; |
| 7805 | reg_maxline = save_max; |
| 7806 | return s; |
| 7807 | } |
| 7808 | |
| 7809 | /* |
Bram Moolenaar | 7aa9f6a | 2007-05-10 18:00:30 +0000 | [diff] [blame] | 7810 | * Used for the submatch() function: get the string from the n'th submatch in |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7811 | * allocated memory. |
| 7812 | * Returns NULL when not in a ":s" command and for a non-existing submatch. |
| 7813 | */ |
| 7814 | char_u * |
| 7815 | reg_submatch(no) |
| 7816 | int no; |
| 7817 | { |
| 7818 | char_u *retval = NULL; |
| 7819 | char_u *s; |
| 7820 | int len; |
| 7821 | int round; |
| 7822 | linenr_T lnum; |
| 7823 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 7824 | if (!can_f_submatch || no < 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7825 | return NULL; |
| 7826 | |
| 7827 | if (submatch_match == NULL) |
| 7828 | { |
| 7829 | /* |
| 7830 | * First round: compute the length and allocate memory. |
| 7831 | * Second round: copy the text. |
| 7832 | */ |
| 7833 | for (round = 1; round <= 2; ++round) |
| 7834 | { |
| 7835 | lnum = submatch_mmatch->startpos[no].lnum; |
| 7836 | if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0) |
| 7837 | return NULL; |
| 7838 | |
Bram Moolenaar | 5ea08a8 | 2009-11-25 18:51:24 +0000 | [diff] [blame] | 7839 | s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7840 | if (s == NULL) /* anti-crash check, cannot happen? */ |
| 7841 | break; |
| 7842 | if (submatch_mmatch->endpos[no].lnum == lnum) |
| 7843 | { |
| 7844 | /* Within one line: take form start to end col. */ |
| 7845 | len = submatch_mmatch->endpos[no].col |
| 7846 | - submatch_mmatch->startpos[no].col; |
| 7847 | if (round == 2) |
Bram Moolenaar | bbebc85 | 2005-07-18 21:47:53 +0000 | [diff] [blame] | 7848 | vim_strncpy(retval, s, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7849 | ++len; |
| 7850 | } |
| 7851 | else |
| 7852 | { |
| 7853 | /* Multiple lines: take start line from start col, middle |
| 7854 | * lines completely and end line up to end col. */ |
| 7855 | len = (int)STRLEN(s); |
| 7856 | if (round == 2) |
| 7857 | { |
| 7858 | STRCPY(retval, s); |
| 7859 | retval[len] = '\n'; |
| 7860 | } |
| 7861 | ++len; |
| 7862 | ++lnum; |
| 7863 | while (lnum < submatch_mmatch->endpos[no].lnum) |
| 7864 | { |
Bram Moolenaar | 5ea08a8 | 2009-11-25 18:51:24 +0000 | [diff] [blame] | 7865 | s = reg_getline_submatch(lnum++); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7866 | if (round == 2) |
| 7867 | STRCPY(retval + len, s); |
| 7868 | len += (int)STRLEN(s); |
| 7869 | if (round == 2) |
| 7870 | retval[len] = '\n'; |
| 7871 | ++len; |
| 7872 | } |
| 7873 | if (round == 2) |
Bram Moolenaar | 5ea08a8 | 2009-11-25 18:51:24 +0000 | [diff] [blame] | 7874 | STRNCPY(retval + len, reg_getline_submatch(lnum), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7875 | submatch_mmatch->endpos[no].col); |
| 7876 | len += submatch_mmatch->endpos[no].col; |
| 7877 | if (round == 2) |
| 7878 | retval[len] = NUL; |
| 7879 | ++len; |
| 7880 | } |
| 7881 | |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 7882 | if (retval == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7883 | { |
| 7884 | retval = lalloc((long_u)len, TRUE); |
Bram Moolenaar | eb3593b | 2006-04-22 22:33:57 +0000 | [diff] [blame] | 7885 | if (retval == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7886 | return NULL; |
| 7887 | } |
| 7888 | } |
| 7889 | } |
| 7890 | else |
| 7891 | { |
Bram Moolenaar | 7670fa0 | 2009-02-21 21:04:20 +0000 | [diff] [blame] | 7892 | s = submatch_match->startp[no]; |
| 7893 | if (s == NULL || submatch_match->endp[no] == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7894 | retval = NULL; |
| 7895 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7896 | retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7897 | } |
| 7898 | |
| 7899 | return retval; |
| 7900 | } |
| 7901 | #endif |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7902 | |
| 7903 | static regengine_T bt_regengine = |
| 7904 | { |
| 7905 | bt_regcomp, |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7906 | bt_regfree, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7907 | bt_regexec, |
| 7908 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 7909 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 7910 | bt_regexec_nl, |
| 7911 | #endif |
| 7912 | bt_regexec_multi |
| 7913 | #ifdef DEBUG |
| 7914 | ,(char_u *)"" |
| 7915 | #endif |
| 7916 | }; |
| 7917 | |
| 7918 | |
| 7919 | #include "regexp_nfa.c" |
| 7920 | |
| 7921 | static regengine_T nfa_regengine = |
| 7922 | { |
| 7923 | nfa_regcomp, |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7924 | nfa_regfree, |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7925 | nfa_regexec, |
| 7926 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 7927 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 7928 | nfa_regexec_nl, |
| 7929 | #endif |
| 7930 | nfa_regexec_multi |
| 7931 | #ifdef DEBUG |
| 7932 | ,(char_u *)"" |
| 7933 | #endif |
| 7934 | }; |
| 7935 | |
| 7936 | /* Which regexp engine to use? Needed for vim_regcomp(). |
| 7937 | * Must match with 'regexpengine'. */ |
| 7938 | static int regexp_engine = 0; |
| 7939 | #define AUTOMATIC_ENGINE 0 |
| 7940 | #define BACKTRACKING_ENGINE 1 |
| 7941 | #define NFA_ENGINE 2 |
| 7942 | #ifdef DEBUG |
| 7943 | static char_u regname[][30] = { |
| 7944 | "AUTOMATIC Regexp Engine", |
Bram Moolenaar | 75eb161 | 2013-05-29 18:45:11 +0200 | [diff] [blame] | 7945 | "BACKTRACKING Regexp Engine", |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7946 | "NFA Regexp Engine" |
| 7947 | }; |
| 7948 | #endif |
| 7949 | |
| 7950 | /* |
| 7951 | * Compile a regular expression into internal code. |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 7952 | * Returns the program in allocated memory. |
| 7953 | * Use vim_regfree() to free the memory. |
| 7954 | * Returns NULL for an error. |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7955 | */ |
| 7956 | regprog_T * |
| 7957 | vim_regcomp(expr_arg, re_flags) |
| 7958 | char_u *expr_arg; |
| 7959 | int re_flags; |
| 7960 | { |
| 7961 | regprog_T *prog = NULL; |
| 7962 | char_u *expr = expr_arg; |
| 7963 | |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 7964 | regexp_engine = p_re; |
| 7965 | |
| 7966 | /* Check for prefix "\%#=", that sets the regexp engine */ |
| 7967 | if (STRNCMP(expr, "\\%#=", 4) == 0) |
| 7968 | { |
| 7969 | int newengine = expr[4] - '0'; |
| 7970 | |
| 7971 | if (newengine == AUTOMATIC_ENGINE |
| 7972 | || newengine == BACKTRACKING_ENGINE |
| 7973 | || newengine == NFA_ENGINE) |
| 7974 | { |
| 7975 | regexp_engine = expr[4] - '0'; |
| 7976 | expr += 5; |
| 7977 | #ifdef DEBUG |
| 7978 | EMSG3("New regexp mode selected (%d): %s", regexp_engine, |
| 7979 | regname[newengine]); |
| 7980 | #endif |
| 7981 | } |
| 7982 | else |
| 7983 | { |
| 7984 | EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used ")); |
| 7985 | regexp_engine = AUTOMATIC_ENGINE; |
| 7986 | } |
| 7987 | } |
| 7988 | #ifdef DEBUG |
| 7989 | bt_regengine.expr = expr; |
| 7990 | nfa_regengine.expr = expr; |
| 7991 | #endif |
| 7992 | |
| 7993 | /* |
| 7994 | * First try the NFA engine, unless backtracking was requested. |
| 7995 | */ |
| 7996 | if (regexp_engine != BACKTRACKING_ENGINE) |
| 7997 | prog = nfa_regengine.regcomp(expr, re_flags); |
| 7998 | else |
| 7999 | prog = bt_regengine.regcomp(expr, re_flags); |
| 8000 | |
| 8001 | if (prog == NULL) /* error compiling regexp with initial engine */ |
| 8002 | { |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 8003 | #ifdef BT_REGEXP_DEBUG_LOG |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 8004 | if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */ |
| 8005 | { |
| 8006 | FILE *f; |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 8007 | f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a"); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 8008 | if (f) |
| 8009 | { |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 8010 | fprintf(f, "Syntax error in \"%s\"\n", expr); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 8011 | fclose(f); |
| 8012 | } |
| 8013 | else |
Bram Moolenaar | 7fcff1f | 2013-05-20 21:49:13 +0200 | [diff] [blame] | 8014 | EMSG2("(NFA) Could not open \"%s\" to write !!!", |
| 8015 | BT_REGEXP_DEBUG_LOG_NAME); |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 8016 | } |
| 8017 | #endif |
| 8018 | /* |
Bram Moolenaar | 917789f | 2013-09-19 17:04:01 +0200 | [diff] [blame] | 8019 | * If the NFA engine failed, the backtracking engine won't work either. |
| 8020 | * |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 8021 | if (regexp_engine == AUTOMATIC_ENGINE) |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 8022 | prog = bt_regengine.regcomp(expr, re_flags); |
Bram Moolenaar | 917789f | 2013-09-19 17:04:01 +0200 | [diff] [blame] | 8023 | */ |
Bram Moolenaar | cd2d8bb | 2013-06-05 21:42:53 +0200 | [diff] [blame] | 8024 | } |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 8025 | |
| 8026 | return prog; |
| 8027 | } |
| 8028 | |
| 8029 | /* |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 8030 | * Free a compiled regexp program, returned by vim_regcomp(). |
| 8031 | */ |
| 8032 | void |
| 8033 | vim_regfree(prog) |
| 8034 | regprog_T *prog; |
| 8035 | { |
| 8036 | if (prog != NULL) |
| 8037 | prog->engine->regfree(prog); |
| 8038 | } |
| 8039 | |
| 8040 | /* |
Bram Moolenaar | fbc0d2e | 2013-05-19 19:40:29 +0200 | [diff] [blame] | 8041 | * Match a regexp against a string. |
| 8042 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 8043 | * Uses curbuf for line count and 'iskeyword'. |
| 8044 | * |
| 8045 | * Return TRUE if there is a match, FALSE if not. |
| 8046 | */ |
| 8047 | int |
| 8048 | vim_regexec(rmp, line, col) |
| 8049 | regmatch_T *rmp; |
| 8050 | char_u *line; /* string to match against */ |
| 8051 | colnr_T col; /* column to start looking for match */ |
| 8052 | { |
| 8053 | return rmp->regprog->engine->regexec(rmp, line, col); |
| 8054 | } |
| 8055 | |
| 8056 | #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ |
| 8057 | || defined(FIND_REPLACE_DIALOG) || defined(PROTO) |
| 8058 | /* |
| 8059 | * Like vim_regexec(), but consider a "\n" in "line" to be a line break. |
| 8060 | */ |
| 8061 | int |
| 8062 | vim_regexec_nl(rmp, line, col) |
| 8063 | regmatch_T *rmp; |
| 8064 | char_u *line; |
| 8065 | colnr_T col; |
| 8066 | { |
| 8067 | return rmp->regprog->engine->regexec_nl(rmp, line, col); |
| 8068 | } |
| 8069 | #endif |
| 8070 | |
| 8071 | /* |
| 8072 | * Match a regexp against multiple lines. |
| 8073 | * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). |
| 8074 | * Uses curbuf for line count and 'iskeyword'. |
| 8075 | * |
| 8076 | * Return zero if there is no match. Return number of lines contained in the |
| 8077 | * match otherwise. |
| 8078 | */ |
| 8079 | long |
| 8080 | vim_regexec_multi(rmp, win, buf, lnum, col, tm) |
| 8081 | regmmatch_T *rmp; |
| 8082 | win_T *win; /* window in which to search or NULL */ |
| 8083 | buf_T *buf; /* buffer in which to search */ |
| 8084 | linenr_T lnum; /* nr of line to start looking for match */ |
| 8085 | colnr_T col; /* column to start looking for match */ |
| 8086 | proftime_T *tm; /* timeout limit or NULL */ |
| 8087 | { |
| 8088 | return rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm); |
| 8089 | } |