blob: a27c594c2a7a7152571f6716908d85bcc3d1944b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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 Moolenaarc0197e22004-09-13 20:26:32 +000036 * Changes have been made by Tony Andrews, Olaf 'Rhialto' Seibert, Robert
37 * Webb, Ciaran McCreesh and Bram Moolenaar.
Bram Moolenaar071d4272004-06-13 20:20:40 +000038 * Named character class support added by Walter Briscoe (1998 Jul 01)
39 */
40
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020041/* 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 Moolenaar071d4272004-06-13 20:20:40 +000047#include "vim.h"
48
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020049#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 Moolenaar7fcff1f2013-05-20 21:49:13 +020054# define BT_REGEXP_DEBUG_LOG
55# define BT_REGEXP_DEBUG_LOG_NAME "bt_regexp_debug.log"
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +020056#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000057
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 Moolenaardf177f62005-02-22 08:39:57 +000090 * 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 Moolenaar071d4272004-06-13 20:20:40 +000096 *
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 Moolenaardf177f62005-02-22 08:39:57 +0000114 * +----------------------+
115 * V |
Bram Moolenaar582fd852005-03-28 20:58:01 +0000116 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000117 * | | ^ ^
118 * | +-----------+ |
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000119 * +--------------------------------------------------+
Bram Moolenaardf177f62005-02-22 08:39:57 +0000120 *
121 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000122 * +-------------------------+
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 Moolenaar7aa9f6a2007-05-10 18:00:30 +0000144 * They all start with a BRANCH for "\|" alternatives, even when there is only
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145 * 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 Moolenaar71fe80d2006-01-22 23:25:56 +0000245#define RE_MARK 207 /* mark cmp Match mark position */
246#define RE_VISUAL 208 /* Match Visual area */
Bram Moolenaar8df5acf2014-05-13 19:37:29 +0200247#define RE_COMPOSING 209 /* any composing characters */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +0000248
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249/*
250 * Magic characters have a special meaning, they don't match literally.
251 * Magic characters are negative. This separates them from literal characters
252 * (possibly multi-byte). Only ASCII characters can be Magic.
253 */
254#define Magic(x) ((int)(x) - 256)
255#define un_Magic(x) ((x) + 256)
256#define is_Magic(x) ((x) < 0)
257
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100259no_Magic(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000260{
261 if (is_Magic(x))
262 return un_Magic(x);
263 return x;
264}
265
266 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100267toggle_Magic(int x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268{
269 if (is_Magic(x))
270 return un_Magic(x);
271 return Magic(x);
272}
273
274/*
275 * The first byte of the regexp internal "program" is actually this magic
276 * number; the start node begins in the second byte. It's used to catch the
277 * most severe mutilation of the program by the caller.
278 */
279
280#define REGMAGIC 0234
281
282/*
283 * Opcode notes:
284 *
285 * BRANCH The set of branches constituting a single choice are hooked
286 * together with their "next" pointers, since precedence prevents
287 * anything being concatenated to any individual branch. The
288 * "next" pointer of the last BRANCH in a choice points to the
289 * thing following the whole choice. This is also where the
290 * final "next" pointer of each individual branch points; each
291 * branch starts with the operand node of a BRANCH node.
292 *
293 * BACK Normal "next" pointers all implicitly point forward; BACK
294 * exists to make loop structures possible.
295 *
296 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
297 * BRANCH structures using BACK. Simple cases (one character
298 * per match) are implemented with STAR and PLUS for speed
299 * and to minimize recursive plunges.
300 *
301 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
302 * node, and defines the min and max limits to be used for that
303 * node.
304 *
305 * MOPEN,MCLOSE ...are numbered at compile time.
306 * ZOPEN,ZCLOSE ...ditto
307 */
308
309/*
310 * A node is one char of opcode followed by two chars of "next" pointer.
311 * "Next" pointers are stored as two 8-bit bytes, high order first. The
312 * value is a positive offset from the opcode of the node containing it.
313 * An operand, if any, simply follows the node. (Note that much of the
314 * code generation knows about this implicit relationship.)
315 *
316 * Using two bytes for the "next" pointer is vast overkill for most things,
317 * but allows patterns to get big without disasters.
318 */
319#define OP(p) ((int)*(p))
320#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
321#define OPERAND(p) ((p) + 3)
322/* Obtain an operand that was stored as four bytes, MSB first. */
323#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
324 + ((long)(p)[5] << 8) + (long)(p)[6])
325/* Obtain a second operand stored as four bytes. */
326#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
327/* Obtain a second single-byte operand stored after a four bytes operand. */
328#define OPERAND_CMP(p) (p)[7]
329
330/*
331 * Utility definitions.
332 */
333#define UCHARAT(p) ((int)*(char_u *)(p))
334
335/* Used for an error (down from) vim_regcomp(): give the error message, set
336 * rc_did_emsg and return NULL */
Bram Moolenaar98692072006-02-04 00:57:42 +0000337#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar95f09602016-11-10 20:01:45 +0100338#define IEMSG_RET_NULL(m) return (IEMSG(m), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar45eeb132005-06-06 21:59:07 +0000339#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200340#define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
341#define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL)
342#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000343
Bram Moolenaar95f09602016-11-10 20:01:45 +0100344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#define MAX_LIMIT (32767L << 16L)
346
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100347static int re_multi_type(int);
348static int cstrncmp(char_u *s1, char_u *s2, int *n);
349static char_u *cstrchr(char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200351#ifdef BT_REGEXP_DUMP
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100352static void regdump(char_u *, bt_regprog_T *);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200353#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100355static char_u *regprop(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000356#endif
357
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100358static int re_mult_next(char *what);
Bram Moolenaarfb031402014-09-09 17:18:49 +0200359
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200360static char_u e_missingbracket[] = N_("E769: Missing ] after %s[");
Bram Moolenaar966e58e2017-06-05 16:54:08 +0200361static char_u e_reverse_range[] = N_("E944: Reverse range in character class");
Bram Moolenaar6c95fbc2017-06-05 17:53:37 +0200362#ifdef FEAT_MBYTE
Bram Moolenaar966e58e2017-06-05 16:54:08 +0200363static char_u e_large_class[] = N_("E945: Range too large in character class");
Bram Moolenaar6c95fbc2017-06-05 17:53:37 +0200364#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200365static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%(");
366static char_u e_unmatchedp[] = N_("E54: Unmatched %s(");
367static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200368#ifdef FEAT_SYN_HL
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200369static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here");
370static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200371#endif
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +0200372static char_u e_missing_sb[] = N_("E69: Missing ] after %s%%[");
Bram Moolenaar2976c022013-06-05 21:30:37 +0200373static char_u e_empty_sb[] = N_("E70: Empty %s%%[]");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374#define NOT_MULTI 0
375#define MULTI_ONE 1
376#define MULTI_MULT 2
377/*
378 * Return NOT_MULTI if c is not a "multi" operator.
379 * Return MULTI_ONE if c is a single "multi" operator.
380 * Return MULTI_MULT if c is a multi "multi" operator.
381 */
382 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100383re_multi_type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000384{
385 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
386 return MULTI_ONE;
387 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
388 return MULTI_MULT;
389 return NOT_MULTI;
390}
391
392/*
393 * Flags to be passed up and down.
394 */
395#define HASWIDTH 0x1 /* Known never to match null string. */
396#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
397#define SPSTART 0x4 /* Starts with * or +. */
398#define HASNL 0x8 /* Contains some \n. */
399#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
400#define WORST 0 /* Worst case. */
401
402/*
403 * When regcode is set to this value, code is not emitted and size is computed
404 * instead.
405 */
406#define JUST_CALC_SIZE ((char_u *) -1)
407
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000408static char_u *reg_prev_sub = NULL;
409
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410/*
411 * REGEXP_INRANGE contains all characters which are always special in a []
412 * range after '\'.
413 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
414 * These are:
415 * \n - New line (NL).
416 * \r - Carriage Return (CR).
417 * \t - Tab (TAB).
418 * \e - Escape (ESC).
419 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000420 * \d - Character code in decimal, eg \d123
421 * \o - Character code in octal, eg \o80
422 * \x - Character code in hex, eg \x4a
423 * \u - Multibyte character code, eg \u20ac
424 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000425 */
426static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000427static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000428
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100429static int backslash_trans(int c);
430static int get_char_class(char_u **pp);
431static int get_equi_class(char_u **pp);
432static void reg_equi_class(int c);
433static int get_coll_element(char_u **pp);
434static char_u *skip_anyof(char_u *p);
435static void init_class_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436
437/*
438 * Translate '\x' to its control character, except "\n", which is Magic.
439 */
440 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100441backslash_trans(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442{
443 switch (c)
444 {
445 case 'r': return CAR;
446 case 't': return TAB;
447 case 'e': return ESC;
448 case 'b': return BS;
449 }
450 return c;
451}
452
453/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000454 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000455 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
456 * recognized. Otherwise "pp" is advanced to after the item.
457 */
458 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100459get_char_class(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460{
461 static const char *(class_names[]) =
462 {
463 "alnum:]",
464#define CLASS_ALNUM 0
465 "alpha:]",
466#define CLASS_ALPHA 1
467 "blank:]",
468#define CLASS_BLANK 2
469 "cntrl:]",
470#define CLASS_CNTRL 3
471 "digit:]",
472#define CLASS_DIGIT 4
473 "graph:]",
474#define CLASS_GRAPH 5
475 "lower:]",
476#define CLASS_LOWER 6
477 "print:]",
478#define CLASS_PRINT 7
479 "punct:]",
480#define CLASS_PUNCT 8
481 "space:]",
482#define CLASS_SPACE 9
483 "upper:]",
484#define CLASS_UPPER 10
485 "xdigit:]",
486#define CLASS_XDIGIT 11
487 "tab:]",
488#define CLASS_TAB 12
489 "return:]",
490#define CLASS_RETURN 13
491 "backspace:]",
492#define CLASS_BACKSPACE 14
493 "escape:]",
494#define CLASS_ESCAPE 15
495 };
496#define CLASS_NONE 99
497 int i;
498
499 if ((*pp)[1] == ':')
500 {
Bram Moolenaar78a15312009-05-15 19:33:18 +0000501 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
503 {
504 *pp += STRLEN(class_names[i]) + 2;
505 return i;
506 }
507 }
508 return CLASS_NONE;
509}
510
511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * Specific version of character class functions.
513 * Using a table to keep this fast.
514 */
515static short class_tab[256];
516
517#define RI_DIGIT 0x01
518#define RI_HEX 0x02
519#define RI_OCTAL 0x04
520#define RI_WORD 0x08
521#define RI_HEAD 0x10
522#define RI_ALPHA 0x20
523#define RI_LOWER 0x40
524#define RI_UPPER 0x80
525#define RI_WHITE 0x100
526
527 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100528init_class_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000529{
530 int i;
531 static int done = FALSE;
532
533 if (done)
534 return;
535
536 for (i = 0; i < 256; ++i)
537 {
538 if (i >= '0' && i <= '7')
539 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
540 else if (i >= '8' && i <= '9')
541 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
542 else if (i >= 'a' && i <= 'f')
543 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
544#ifdef EBCDIC
545 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
546 || (i >= 's' && i <= 'z'))
547#else
548 else if (i >= 'g' && i <= 'z')
549#endif
550 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
551 else if (i >= 'A' && i <= 'F')
552 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
553#ifdef EBCDIC
554 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
555 || (i >= 'S' && i <= 'Z'))
556#else
557 else if (i >= 'G' && i <= 'Z')
558#endif
559 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
560 else if (i == '_')
561 class_tab[i] = RI_WORD + RI_HEAD;
562 else
563 class_tab[i] = 0;
564 }
565 class_tab[' '] |= RI_WHITE;
566 class_tab['\t'] |= RI_WHITE;
567 done = TRUE;
568}
569
570#ifdef FEAT_MBYTE
571# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
572# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
573# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
574# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
575# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
576# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
577# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
578# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
579# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
580#else
581# define ri_digit(c) (class_tab[c] & RI_DIGIT)
582# define ri_hex(c) (class_tab[c] & RI_HEX)
583# define ri_octal(c) (class_tab[c] & RI_OCTAL)
584# define ri_word(c) (class_tab[c] & RI_WORD)
585# define ri_head(c) (class_tab[c] & RI_HEAD)
586# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
587# define ri_lower(c) (class_tab[c] & RI_LOWER)
588# define ri_upper(c) (class_tab[c] & RI_UPPER)
589# define ri_white(c) (class_tab[c] & RI_WHITE)
590#endif
591
592/* flags for regflags */
593#define RF_ICASE 1 /* ignore case */
594#define RF_NOICASE 2 /* don't ignore case */
595#define RF_HASNL 4 /* can match a NL */
596#define RF_ICOMBINE 8 /* ignore combining characters */
597#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
598
599/*
600 * Global work variables for vim_regcomp().
601 */
602
603static char_u *regparse; /* Input-scan pointer. */
604static int prevchr_len; /* byte length of previous char */
605static int num_complex_braces; /* Complex \{...} count */
606static int regnpar; /* () count. */
607#ifdef FEAT_SYN_HL
608static int regnzpar; /* \z() count. */
609static int re_has_z; /* \z item detected */
610#endif
611static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
612static long regsize; /* Code size. */
Bram Moolenaard3005802009-11-25 17:21:32 +0000613static int reg_toolong; /* TRUE when offset out of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
615static unsigned regflags; /* RF_ flags for prog */
616static long brace_min[10]; /* Minimums for complex brace repeats */
617static long brace_max[10]; /* Maximums for complex brace repeats */
618static int brace_count[10]; /* Current counts for complex brace repeats */
619#if defined(FEAT_SYN_HL) || defined(PROTO)
620static int had_eol; /* TRUE when EOL found by vim_regcomp() */
621#endif
622static int one_exactly = FALSE; /* only do one char for EXACTLY */
623
624static int reg_magic; /* magicness of the pattern: */
625#define MAGIC_NONE 1 /* "\V" very unmagic */
626#define MAGIC_OFF 2 /* "\M" or 'magic' off */
627#define MAGIC_ON 3 /* "\m" or 'magic' */
628#define MAGIC_ALL 4 /* "\v" very magic */
629
630static int reg_string; /* matching with a string instead of a buffer
631 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000632static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634/*
635 * META contains all characters that may be magic, except '^' and '$'.
636 */
637
638#ifdef EBCDIC
639static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
640#else
641/* META[] is used often enough to justify turning it into a table. */
642static char_u META_flags[] = {
643 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
644 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
645/* % & ( ) * + . */
646 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
647/* 1 2 3 4 5 6 7 8 9 < = > ? */
648 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
649/* @ A C D F H I K L M O */
650 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
651/* P S U V W X Z [ _ */
652 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
653/* a c d f h i k l m n o */
654 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
655/* p s u v w x z { | ~ */
656 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
657};
658#endif
659
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660static int curchr; /* currently parsed character */
661/* Previous character. Note: prevchr is sometimes -1 when we are not at the
662 * start, eg in /[ ^I]^ the pattern was never found even if it existed,
663 * because ^ was taken to be magic -- webb */
664static int prevchr;
665static int prevprevchr; /* previous-previous character */
666static int nextchr; /* used for ungetchr() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668/* arguments for reg() */
669#define REG_NOPAREN 0 /* toplevel reg() */
670#define REG_PAREN 1 /* \(\) */
671#define REG_ZPAREN 2 /* \z(\) */
672#define REG_NPAREN 3 /* \%(\) */
673
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200674typedef struct
675{
676 char_u *regparse;
677 int prevchr_len;
678 int curchr;
679 int prevchr;
680 int prevprevchr;
681 int nextchr;
682 int at_start;
683 int prev_at_start;
684 int regnpar;
685} parse_state_T;
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687/*
688 * Forward declarations for vim_regcomp()'s friends.
689 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100690static void initchr(char_u *);
691static void save_parse_state(parse_state_T *ps);
692static void restore_parse_state(parse_state_T *ps);
693static int getchr(void);
694static void skipchr_keepstart(void);
695static int peekchr(void);
696static void skipchr(void);
697static void ungetchr(void);
698static int gethexchrs(int maxinputlen);
699static int getoctchrs(void);
700static int getdecchrs(void);
701static int coll_get_char(void);
702static void regcomp_start(char_u *expr, int flags);
703static char_u *reg(int, int *);
704static char_u *regbranch(int *flagp);
705static char_u *regconcat(int *flagp);
706static char_u *regpiece(int *);
707static char_u *regatom(int *);
708static char_u *regnode(int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000709#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100710static int use_multibytecode(int c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000711#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100712static int prog_magic_wrong(void);
713static char_u *regnext(char_u *);
714static void regc(int b);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100716static void regmbc(int c);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200717# define REGMBC(x) regmbc(x);
718# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000719#else
720# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200721# define REGMBC(x)
722# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100724static void reginsert(int, char_u *);
725static void reginsert_nr(int op, long val, char_u *opnd);
726static void reginsert_limits(int, long, long, char_u *);
727static char_u *re_put_long(char_u *pr, long_u val);
728static int read_limits(long *, long *);
729static void regtail(char_u *, char_u *);
730static void regoptail(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732static regengine_T bt_regengine;
733static regengine_T nfa_regengine;
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735/*
736 * Return TRUE if compiled regular expression "prog" can match a line break.
737 */
738 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100739re_multiline(regprog_T *prog)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000740{
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
Bram Moolenaar05540972016-01-30 20:31:25 +0100749re_lookbehind(regprog_T *prog)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750{
751 return (prog->regflags & RF_LOOKBH);
752}
753
754/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000755 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
756 * Returns a character representing the class. Zero means that no item was
757 * recognized. Otherwise "pp" is advanced to after the item.
758 */
759 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100760get_equi_class(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000761{
762 int c;
763 int l = 1;
764 char_u *p = *pp;
765
766 if (p[1] == '=')
767 {
768#ifdef FEAT_MBYTE
769 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000770 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000771#endif
772 if (p[l + 2] == '=' && p[l + 3] == ']')
773 {
774#ifdef FEAT_MBYTE
775 if (has_mbyte)
776 c = mb_ptr2char(p + 2);
777 else
778#endif
779 c = p[2];
780 *pp += l + 4;
781 return c;
782 }
783 }
784 return 0;
785}
786
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200787#ifdef EBCDIC
788/*
789 * Table for equivalence class "c". (IBM-1047)
790 */
791char *EQUIVAL_CLASS_C[16] = {
792 "A\x62\x63\x64\x65\x66\x67",
793 "C\x68",
794 "E\x71\x72\x73\x74",
795 "I\x75\x76\x77\x78",
796 "N\x69",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200797 "O\xEB\xEC\xED\xEE\xEF\x80",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200798 "U\xFB\xFC\xFD\xFE",
799 "Y\xBA",
800 "a\x42\x43\x44\x45\x46\x47",
801 "c\x48",
802 "e\x51\x52\x53\x54",
803 "i\x55\x56\x57\x58",
804 "n\x49",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200805 "o\xCB\xCC\xCD\xCE\xCF\x70",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200806 "u\xDB\xDC\xDD\xDE",
807 "y\x8D\xDF",
808};
809#endif
810
Bram Moolenaardf177f62005-02-22 08:39:57 +0000811/*
812 * Produce the bytes for equivalence class "c".
813 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200814 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000815 */
816 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100817reg_equi_class(int c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000818{
819#ifdef FEAT_MBYTE
820 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000821 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000822#endif
823 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200824#ifdef EBCDIC
825 int i;
826
827 /* This might be slower than switch/case below. */
828 for (i = 0; i < 16; i++)
829 {
830 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
831 {
832 char *p = EQUIVAL_CLASS_C[i];
833
834 while (*p != 0)
835 regmbc(*p++);
836 return;
837 }
838 }
839#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000840 switch (c)
841 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200842 /* Do not use '\300' style, it results in a negative number. */
843 case 'A': case 0xc0: case 0xc1: case 0xc2:
844 case 0xc3: case 0xc4: case 0xc5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200845 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
846 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200847 regmbc('A'); regmbc(0xc0); regmbc(0xc1);
848 regmbc(0xc2); regmbc(0xc3); regmbc(0xc4);
849 regmbc(0xc5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200850 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
851 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
852 REGMBC(0x1ea2)
853 return;
854 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
855 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000856 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200857 case 'C': case 0xc7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200858 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200859 regmbc('C'); regmbc(0xc7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200860 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
861 REGMBC(0x10c)
862 return;
863 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
864 CASEMBC(0x1e0e) CASEMBC(0x1e10)
865 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
866 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000867 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200868 case 'E': case 0xc8: case 0xc9: case 0xca: case 0xcb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200869 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
870 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200871 regmbc('E'); regmbc(0xc8); regmbc(0xc9);
872 regmbc(0xca); regmbc(0xcb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200873 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
874 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
875 REGMBC(0x1ebc)
876 return;
877 case 'F': CASEMBC(0x1e1e)
878 regmbc('F'); REGMBC(0x1e1e)
879 return;
880 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
881 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
882 CASEMBC(0x1e20)
883 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
884 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
885 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
886 return;
887 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
888 CASEMBC(0x1e26) CASEMBC(0x1e28)
889 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
890 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000891 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200892 case 'I': case 0xcc: case 0xcd: case 0xce: case 0xcf:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200893 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
894 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200895 regmbc('I'); regmbc(0xcc); regmbc(0xcd);
896 regmbc(0xce); regmbc(0xcf);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200897 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
898 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
899 REGMBC(0x1ec8)
900 return;
901 case 'J': CASEMBC(0x134)
902 regmbc('J'); REGMBC(0x134)
903 return;
904 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
905 CASEMBC(0x1e34)
906 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
907 REGMBC(0x1e30) REGMBC(0x1e34)
908 return;
909 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
910 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
911 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
912 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
913 REGMBC(0x1e3a)
914 return;
915 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
916 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000917 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200918 case 'N': case 0xd1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200919 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
920 CASEMBC(0x1e48)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200921 regmbc('N'); regmbc(0xd1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200922 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
923 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000924 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200925 case 'O': case 0xd2: case 0xd3: case 0xd4: case 0xd5:
926 case 0xd6: case 0xd8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200927 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
928 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200929 regmbc('O'); regmbc(0xd2); regmbc(0xd3);
930 regmbc(0xd4); regmbc(0xd5); regmbc(0xd6);
931 regmbc(0xd8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200932 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
933 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
934 REGMBC(0x1ec) REGMBC(0x1ece)
935 return;
936 case 'P': case 0x1e54: case 0x1e56:
937 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
938 return;
939 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
940 CASEMBC(0x1e58) CASEMBC(0x1e5e)
941 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
942 REGMBC(0x1e58) REGMBC(0x1e5e)
943 return;
944 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
945 CASEMBC(0x160) CASEMBC(0x1e60)
946 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
947 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
948 return;
949 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
950 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
951 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
952 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000953 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200954 case 'U': case 0xd9: case 0xda: case 0xdb: case 0xdc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200955 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
956 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
957 CASEMBC(0x1ee6)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200958 regmbc('U'); regmbc(0xd9); regmbc(0xda);
959 regmbc(0xdb); regmbc(0xdc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200960 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
961 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
962 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
963 return;
964 case 'V': CASEMBC(0x1e7c)
965 regmbc('V'); REGMBC(0x1e7c)
966 return;
967 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
968 CASEMBC(0x1e84) CASEMBC(0x1e86)
969 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
970 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
971 return;
972 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
973 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000974 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200975 case 'Y': case 0xdd:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200976 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
977 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200978 regmbc('Y'); regmbc(0xdd);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200979 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
980 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
981 return;
982 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
983 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
984 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
985 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
986 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000987 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200988 case 'a': case 0xe0: case 0xe1: case 0xe2:
989 case 0xe3: case 0xe4: case 0xe5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200990 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
991 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200992 regmbc('a'); regmbc(0xe0); regmbc(0xe1);
993 regmbc(0xe2); regmbc(0xe3); regmbc(0xe4);
994 regmbc(0xe5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200995 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
996 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
997 REGMBC(0x1ea3)
998 return;
999 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1000 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001001 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001002 case 'c': case 0xe7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001003 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001004 regmbc('c'); regmbc(0xe7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001005 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
1006 REGMBC(0x10d)
1007 return;
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001008 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
1009 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001010 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001011 REGMBC(0x1e0b) REGMBC(0x1e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001012 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001013 case 'e': case 0xe8: case 0xe9: case 0xea: case 0xeb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001014 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
1015 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001016 regmbc('e'); regmbc(0xe8); regmbc(0xe9);
1017 regmbc(0xea); regmbc(0xeb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001018 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1019 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1020 REGMBC(0x1ebd)
1021 return;
1022 case 'f': CASEMBC(0x1e1f)
1023 regmbc('f'); REGMBC(0x1e1f)
1024 return;
1025 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1026 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1027 CASEMBC(0x1e21)
1028 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1029 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1030 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1031 return;
1032 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1033 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1034 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1035 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1036 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001037 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001038 case 'i': case 0xec: case 0xed: case 0xee: case 0xef:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001039 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1040 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001041 regmbc('i'); regmbc(0xec); regmbc(0xed);
1042 regmbc(0xee); regmbc(0xef);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001043 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1044 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1045 return;
1046 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1047 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1048 return;
1049 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1050 CASEMBC(0x1e35)
1051 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1052 REGMBC(0x1e31) REGMBC(0x1e35)
1053 return;
1054 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1055 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1056 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1057 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1058 REGMBC(0x1e3b)
1059 return;
1060 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1061 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001062 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001063 case 'n': case 0xf1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001064 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1065 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001066 regmbc('n'); regmbc(0xf1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001067 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1068 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001069 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001070 case 'o': case 0xf2: case 0xf3: case 0xf4: case 0xf5:
1071 case 0xf6: case 0xf8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001072 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1073 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001074 regmbc('o'); regmbc(0xf2); regmbc(0xf3);
1075 regmbc(0xf4); regmbc(0xf5); regmbc(0xf6);
1076 regmbc(0xf8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001077 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1078 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1079 REGMBC(0x1ed) REGMBC(0x1ecf)
1080 return;
1081 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1082 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1083 return;
1084 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1085 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1086 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1087 REGMBC(0x1e59) REGMBC(0x1e5f)
1088 return;
1089 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1090 CASEMBC(0x161) CASEMBC(0x1e61)
1091 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1092 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1093 return;
1094 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1095 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1096 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1097 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001098 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001099 case 'u': case 0xf9: case 0xfa: case 0xfb: case 0xfc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001100 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1101 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1102 CASEMBC(0x1ee7)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001103 regmbc('u'); regmbc(0xf9); regmbc(0xfa);
1104 regmbc(0xfb); regmbc(0xfc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001105 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1106 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1107 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1108 return;
1109 case 'v': CASEMBC(0x1e7d)
1110 regmbc('v'); REGMBC(0x1e7d)
1111 return;
1112 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1113 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1114 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1115 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1116 REGMBC(0x1e98)
1117 return;
1118 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1119 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001120 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001121 case 'y': case 0xfd: case 0xff:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001122 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1123 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001124 regmbc('y'); regmbc(0xfd); regmbc(0xff);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001125 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1126 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1127 return;
1128 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1129 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1130 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1131 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1132 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001133 return;
1134 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001135#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001136 }
1137 regmbc(c);
1138}
1139
1140/*
1141 * Check for a collating element "[.a.]". "pp" points to the '['.
1142 * Returns a character. Zero means that no item was recognized. Otherwise
1143 * "pp" is advanced to after the item.
1144 * Currently only single characters are recognized!
1145 */
1146 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001147get_coll_element(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001148{
1149 int c;
1150 int l = 1;
1151 char_u *p = *pp;
1152
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001153 if (p[0] != NUL && p[1] == '.')
Bram Moolenaardf177f62005-02-22 08:39:57 +00001154 {
1155#ifdef FEAT_MBYTE
1156 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001157 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001158#endif
1159 if (p[l + 2] == '.' && p[l + 3] == ']')
1160 {
1161#ifdef FEAT_MBYTE
1162 if (has_mbyte)
1163 c = mb_ptr2char(p + 2);
1164 else
1165#endif
1166 c = p[2];
1167 *pp += l + 4;
1168 return c;
1169 }
1170 }
1171 return 0;
1172}
1173
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001174static void get_cpo_flags(void);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001175static int reg_cpo_lit; /* 'cpoptions' contains 'l' flag */
1176static int reg_cpo_bsl; /* 'cpoptions' contains '\' flag */
1177
1178 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001179get_cpo_flags(void)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001180{
1181 reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1182 reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
1183}
Bram Moolenaardf177f62005-02-22 08:39:57 +00001184
1185/*
1186 * Skip over a "[]" range.
1187 * "p" must point to the character after the '['.
1188 * The returned pointer is on the matching ']', or the terminating NUL.
1189 */
1190 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001191skip_anyof(char_u *p)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001192{
Bram Moolenaardf177f62005-02-22 08:39:57 +00001193#ifdef FEAT_MBYTE
1194 int l;
1195#endif
1196
Bram Moolenaardf177f62005-02-22 08:39:57 +00001197 if (*p == '^') /* Complement of range. */
1198 ++p;
1199 if (*p == ']' || *p == '-')
1200 ++p;
1201 while (*p != NUL && *p != ']')
1202 {
1203#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001204 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001205 p += l;
1206 else
1207#endif
1208 if (*p == '-')
1209 {
1210 ++p;
1211 if (*p != ']' && *p != NUL)
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001212 MB_PTR_ADV(p);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001213 }
1214 else if (*p == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001215 && !reg_cpo_bsl
Bram Moolenaardf177f62005-02-22 08:39:57 +00001216 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001217 || (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001218 p += 2;
1219 else if (*p == '[')
1220 {
1221 if (get_char_class(&p) == CLASS_NONE
1222 && get_equi_class(&p) == 0
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001223 && get_coll_element(&p) == 0
1224 && *p != NUL)
1225 ++p; /* it is not a class name and not NUL */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001226 }
1227 else
1228 ++p;
1229 }
1230
1231 return p;
1232}
1233
1234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001236 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 * Take care of characters with a backslash in front of it.
1238 * Skip strings inside [ and ].
1239 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1240 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1241 * is changed in-place.
1242 */
1243 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001244skip_regexp(
1245 char_u *startp,
1246 int dirc,
1247 int magic,
1248 char_u **newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249{
1250 int mymagic;
1251 char_u *p = startp;
1252
1253 if (magic)
1254 mymagic = MAGIC_ON;
1255 else
1256 mymagic = MAGIC_OFF;
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001257 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258
Bram Moolenaar91acfff2017-03-12 19:22:36 +01001259 for (; p[0] != NUL; MB_PTR_ADV(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 {
1261 if (p[0] == dirc) /* found end of regexp */
1262 break;
1263 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1264 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1265 {
1266 p = skip_anyof(p + 1);
1267 if (p[0] == NUL)
1268 break;
1269 }
1270 else if (p[0] == '\\' && p[1] != NUL)
1271 {
1272 if (dirc == '?' && newp != NULL && p[1] == '?')
1273 {
1274 /* change "\?" to "?", make a copy first. */
1275 if (*newp == NULL)
1276 {
1277 *newp = vim_strsave(startp);
1278 if (*newp != NULL)
1279 p = *newp + (p - startp);
1280 }
1281 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001282 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 else
1284 ++p;
1285 }
1286 else
1287 ++p; /* skip next character */
1288 if (*p == 'v')
1289 mymagic = MAGIC_ALL;
1290 else if (*p == 'V')
1291 mymagic = MAGIC_NONE;
1292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 }
1294 return p;
1295}
1296
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001297static regprog_T *bt_regcomp(char_u *expr, int re_flags);
1298static void bt_regfree(regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001299
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001301 * bt_regcomp() - compile a regular expression into internal code for the
1302 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001303 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 *
1305 * We can't allocate space until we know how big the compiled form will be,
1306 * but we can't compile it (and thus know how big it is) until we've got a
1307 * place to put the code. So we cheat: we compile it twice, once with code
1308 * generation turned off and size counting turned on, and once "for real".
1309 * This also means that we don't allocate space until we are sure that the
1310 * thing really will compile successfully, and we never have to move the
1311 * code and thus invalidate pointers into it. (Note that it has to be in
1312 * one piece because vim_free() must be able to free it all.)
1313 *
1314 * Whether upper/lower case is to be ignored is decided when executing the
1315 * program, it does not matter here.
1316 *
1317 * Beware that the optimization-preparation code in here knows about some
1318 * of the structure of the compiled regexp.
1319 * "re_flags": RE_MAGIC and/or RE_STRING.
1320 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001321 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001322bt_regcomp(char_u *expr, int re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001324 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 char_u *scan;
1326 char_u *longest;
1327 int len;
1328 int flags;
1329
1330 if (expr == NULL)
1331 EMSG_RET_NULL(_(e_null));
1332
1333 init_class_tab();
1334
1335 /*
1336 * First pass: determine size, legality.
1337 */
1338 regcomp_start(expr, re_flags);
1339 regcode = JUST_CALC_SIZE;
1340 regc(REGMAGIC);
1341 if (reg(REG_NOPAREN, &flags) == NULL)
1342 return NULL;
1343
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001345 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 if (r == NULL)
1347 return NULL;
1348
1349 /*
1350 * Second pass: emit code.
1351 */
1352 regcomp_start(expr, re_flags);
1353 regcode = r->program;
1354 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001355 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 {
1357 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001358 if (reg_toolong)
1359 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 return NULL;
1361 }
1362
1363 /* Dig out information for optimizations. */
1364 r->regstart = NUL; /* Worst-case defaults. */
1365 r->reganch = 0;
1366 r->regmust = NULL;
1367 r->regmlen = 0;
1368 r->regflags = regflags;
1369 if (flags & HASNL)
1370 r->regflags |= RF_HASNL;
1371 if (flags & HASLOOKBH)
1372 r->regflags |= RF_LOOKBH;
1373#ifdef FEAT_SYN_HL
1374 /* Remember whether this pattern has any \z specials in it. */
1375 r->reghasz = re_has_z;
1376#endif
1377 scan = r->program + 1; /* First BRANCH. */
1378 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1379 {
1380 scan = OPERAND(scan);
1381
1382 /* Starting-point info. */
1383 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1384 {
1385 r->reganch++;
1386 scan = regnext(scan);
1387 }
1388
1389 if (OP(scan) == EXACTLY)
1390 {
1391#ifdef FEAT_MBYTE
1392 if (has_mbyte)
1393 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1394 else
1395#endif
1396 r->regstart = *OPERAND(scan);
1397 }
1398 else if ((OP(scan) == BOW
1399 || OP(scan) == EOW
1400 || OP(scan) == NOTHING
1401 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1402 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1403 && OP(regnext(scan)) == EXACTLY)
1404 {
1405#ifdef FEAT_MBYTE
1406 if (has_mbyte)
1407 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1408 else
1409#endif
1410 r->regstart = *OPERAND(regnext(scan));
1411 }
1412
1413 /*
1414 * If there's something expensive in the r.e., find the longest
1415 * literal string that must appear and make it the regmust. Resolve
1416 * ties in favor of later strings, since the regstart check works
1417 * with the beginning of the r.e. and avoiding duplication
1418 * strengthens checking. Not a strong reason, but sufficient in the
1419 * absence of others.
1420 */
1421 /*
1422 * When the r.e. starts with BOW, it is faster to look for a regmust
1423 * first. Used a lot for "#" and "*" commands. (Added by mool).
1424 */
1425 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1426 && !(flags & HASNL))
1427 {
1428 longest = NULL;
1429 len = 0;
1430 for (; scan != NULL; scan = regnext(scan))
1431 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1432 {
1433 longest = OPERAND(scan);
1434 len = (int)STRLEN(OPERAND(scan));
1435 }
1436 r->regmust = longest;
1437 r->regmlen = len;
1438 }
1439 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001440#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 regdump(expr, r);
1442#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001443 r->engine = &bt_regengine;
1444 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001445}
1446
1447/*
Bram Moolenaar473de612013-06-08 18:19:48 +02001448 * Free a compiled regexp program, returned by bt_regcomp().
1449 */
1450 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001451bt_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02001452{
1453 vim_free(prog);
1454}
1455
1456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 * Setup to parse the regexp. Used once to get the length and once to do it.
1458 */
1459 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001460regcomp_start(
1461 char_u *expr,
1462 int re_flags) /* see vim_regcomp() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463{
1464 initchr(expr);
1465 if (re_flags & RE_MAGIC)
1466 reg_magic = MAGIC_ON;
1467 else
1468 reg_magic = MAGIC_OFF;
1469 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001470 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001471 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472
1473 num_complex_braces = 0;
1474 regnpar = 1;
1475 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1476#ifdef FEAT_SYN_HL
1477 regnzpar = 1;
1478 re_has_z = 0;
1479#endif
1480 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001481 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 regflags = 0;
1483#if defined(FEAT_SYN_HL) || defined(PROTO)
1484 had_eol = FALSE;
1485#endif
1486}
1487
1488#if defined(FEAT_SYN_HL) || defined(PROTO)
1489/*
1490 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1491 * found. This is messy, but it works fine.
1492 */
1493 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001494vim_regcomp_had_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495{
1496 return had_eol;
1497}
1498#endif
1499
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001500/* variables for parsing reginput */
1501static int at_start; /* True when on the first character */
1502static int prev_at_start; /* True when on the second character */
1503
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001505 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 *
1507 * Caller must absorb opening parenthesis.
1508 *
1509 * Combining parenthesis handling with the base level of regular expression
1510 * is a trifle forced, but the need to tie the tails of the branches to what
1511 * follows makes it hard to avoid.
1512 */
1513 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001514reg(
1515 int paren, /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1516 int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517{
1518 char_u *ret;
1519 char_u *br;
1520 char_u *ender;
1521 int parno = 0;
1522 int flags;
1523
1524 *flagp = HASWIDTH; /* Tentatively. */
1525
1526#ifdef FEAT_SYN_HL
1527 if (paren == REG_ZPAREN)
1528 {
1529 /* Make a ZOPEN node. */
1530 if (regnzpar >= NSUBEXP)
1531 EMSG_RET_NULL(_("E50: Too many \\z("));
1532 parno = regnzpar;
1533 regnzpar++;
1534 ret = regnode(ZOPEN + parno);
1535 }
1536 else
1537#endif
1538 if (paren == REG_PAREN)
1539 {
1540 /* Make a MOPEN node. */
1541 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001542 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 parno = regnpar;
1544 ++regnpar;
1545 ret = regnode(MOPEN + parno);
1546 }
1547 else if (paren == REG_NPAREN)
1548 {
1549 /* Make a NOPEN node. */
1550 ret = regnode(NOPEN);
1551 }
1552 else
1553 ret = NULL;
1554
1555 /* Pick up the branches, linking them together. */
1556 br = regbranch(&flags);
1557 if (br == NULL)
1558 return NULL;
1559 if (ret != NULL)
1560 regtail(ret, br); /* [MZ]OPEN -> first. */
1561 else
1562 ret = br;
1563 /* If one of the branches can be zero-width, the whole thing can.
1564 * If one of the branches has * at start or matches a line-break, the
1565 * whole thing can. */
1566 if (!(flags & HASWIDTH))
1567 *flagp &= ~HASWIDTH;
1568 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1569 while (peekchr() == Magic('|'))
1570 {
1571 skipchr();
1572 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001573 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574 return NULL;
1575 regtail(ret, br); /* BRANCH -> BRANCH. */
1576 if (!(flags & HASWIDTH))
1577 *flagp &= ~HASWIDTH;
1578 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1579 }
1580
1581 /* Make a closing node, and hook it on the end. */
1582 ender = regnode(
1583#ifdef FEAT_SYN_HL
1584 paren == REG_ZPAREN ? ZCLOSE + parno :
1585#endif
1586 paren == REG_PAREN ? MCLOSE + parno :
1587 paren == REG_NPAREN ? NCLOSE : END);
1588 regtail(ret, ender);
1589
1590 /* Hook the tails of the branches to the closing node. */
1591 for (br = ret; br != NULL; br = regnext(br))
1592 regoptail(br, ender);
1593
1594 /* Check for proper termination. */
1595 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1596 {
1597#ifdef FEAT_SYN_HL
1598 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001599 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 else
1601#endif
1602 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001603 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001605 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 }
1607 else if (paren == REG_NOPAREN && peekchr() != NUL)
1608 {
1609 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001610 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001612 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 /* NOTREACHED */
1614 }
1615 /*
1616 * Here we set the flag allowing back references to this set of
1617 * parentheses.
1618 */
1619 if (paren == REG_PAREN)
1620 had_endbrace[parno] = TRUE; /* have seen the close paren */
1621 return ret;
1622}
1623
1624/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001625 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 * Implements the & operator.
1627 */
1628 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001629regbranch(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
1631 char_u *ret;
1632 char_u *chain = NULL;
1633 char_u *latest;
1634 int flags;
1635
1636 *flagp = WORST | HASNL; /* Tentatively. */
1637
1638 ret = regnode(BRANCH);
1639 for (;;)
1640 {
1641 latest = regconcat(&flags);
1642 if (latest == NULL)
1643 return NULL;
1644 /* If one of the branches has width, the whole thing has. If one of
1645 * the branches anchors at start-of-line, the whole thing does.
1646 * If one of the branches uses look-behind, the whole thing does. */
1647 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1648 /* If one of the branches doesn't match a line-break, the whole thing
1649 * doesn't. */
1650 *flagp &= ~HASNL | (flags & HASNL);
1651 if (chain != NULL)
1652 regtail(chain, latest);
1653 if (peekchr() != Magic('&'))
1654 break;
1655 skipchr();
1656 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001657 if (reg_toolong)
1658 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 reginsert(MATCH, latest);
1660 chain = latest;
1661 }
1662
1663 return ret;
1664}
1665
1666/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001667 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 * Implements the concatenation operator.
1669 */
1670 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001671regconcat(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672{
1673 char_u *first = NULL;
1674 char_u *chain = NULL;
1675 char_u *latest;
1676 int flags;
1677 int cont = TRUE;
1678
1679 *flagp = WORST; /* Tentatively. */
1680
1681 while (cont)
1682 {
1683 switch (peekchr())
1684 {
1685 case NUL:
1686 case Magic('|'):
1687 case Magic('&'):
1688 case Magic(')'):
1689 cont = FALSE;
1690 break;
1691 case Magic('Z'):
1692#ifdef FEAT_MBYTE
1693 regflags |= RF_ICOMBINE;
1694#endif
1695 skipchr_keepstart();
1696 break;
1697 case Magic('c'):
1698 regflags |= RF_ICASE;
1699 skipchr_keepstart();
1700 break;
1701 case Magic('C'):
1702 regflags |= RF_NOICASE;
1703 skipchr_keepstart();
1704 break;
1705 case Magic('v'):
1706 reg_magic = MAGIC_ALL;
1707 skipchr_keepstart();
1708 curchr = -1;
1709 break;
1710 case Magic('m'):
1711 reg_magic = MAGIC_ON;
1712 skipchr_keepstart();
1713 curchr = -1;
1714 break;
1715 case Magic('M'):
1716 reg_magic = MAGIC_OFF;
1717 skipchr_keepstart();
1718 curchr = -1;
1719 break;
1720 case Magic('V'):
1721 reg_magic = MAGIC_NONE;
1722 skipchr_keepstart();
1723 curchr = -1;
1724 break;
1725 default:
1726 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001727 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728 return NULL;
1729 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1730 if (chain == NULL) /* First piece. */
1731 *flagp |= flags & SPSTART;
1732 else
1733 regtail(chain, latest);
1734 chain = latest;
1735 if (first == NULL)
1736 first = latest;
1737 break;
1738 }
1739 }
1740 if (first == NULL) /* Loop ran zero times. */
1741 first = regnode(NOTHING);
1742 return first;
1743}
1744
1745/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001746 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 *
1748 * Note that the branching code sequences used for = and the general cases
1749 * of * and + are somewhat optimized: they use the same NOTHING node as
1750 * both the endmarker for their branch list and the body of the last branch.
1751 * It might seem that this node could be dispensed with entirely, but the
1752 * endmarker role is not redundant.
1753 */
1754 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001755regpiece(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 char_u *ret;
1758 int op;
1759 char_u *next;
1760 int flags;
1761 long minval;
1762 long maxval;
1763
1764 ret = regatom(&flags);
1765 if (ret == NULL)
1766 return NULL;
1767
1768 op = peekchr();
1769 if (re_multi_type(op) == NOT_MULTI)
1770 {
1771 *flagp = flags;
1772 return ret;
1773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 /* default flags */
1775 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1776
1777 skipchr();
1778 switch (op)
1779 {
1780 case Magic('*'):
1781 if (flags & SIMPLE)
1782 reginsert(STAR, ret);
1783 else
1784 {
1785 /* Emit x* as (x&|), where & means "self". */
1786 reginsert(BRANCH, ret); /* Either x */
1787 regoptail(ret, regnode(BACK)); /* and loop */
1788 regoptail(ret, ret); /* back */
1789 regtail(ret, regnode(BRANCH)); /* or */
1790 regtail(ret, regnode(NOTHING)); /* null. */
1791 }
1792 break;
1793
1794 case Magic('+'):
1795 if (flags & SIMPLE)
1796 reginsert(PLUS, ret);
1797 else
1798 {
1799 /* Emit x+ as x(&|), where & means "self". */
1800 next = regnode(BRANCH); /* Either */
1801 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001802 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803 regtail(next, regnode(BRANCH)); /* or */
1804 regtail(ret, regnode(NOTHING)); /* null. */
1805 }
1806 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1807 break;
1808
1809 case Magic('@'):
1810 {
1811 int lop = END;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001812 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001814 nr = getdecchrs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 switch (no_Magic(getchr()))
1816 {
1817 case '=': lop = MATCH; break; /* \@= */
1818 case '!': lop = NOMATCH; break; /* \@! */
1819 case '>': lop = SUBPAT; break; /* \@> */
1820 case '<': switch (no_Magic(getchr()))
1821 {
1822 case '=': lop = BEHIND; break; /* \@<= */
1823 case '!': lop = NOBEHIND; break; /* \@<! */
1824 }
1825 }
1826 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001827 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 reg_magic == MAGIC_ALL);
1829 /* Look behind must match with behind_pos. */
1830 if (lop == BEHIND || lop == NOBEHIND)
1831 {
1832 regtail(ret, regnode(BHPOS));
1833 *flagp |= HASLOOKBH;
1834 }
1835 regtail(ret, regnode(END)); /* operand ends */
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001836 if (lop == BEHIND || lop == NOBEHIND)
1837 {
1838 if (nr < 0)
1839 nr = 0; /* no limit is same as zero limit */
1840 reginsert_nr(lop, nr, ret);
1841 }
1842 else
1843 reginsert(lop, ret);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844 break;
1845 }
1846
1847 case Magic('?'):
1848 case Magic('='):
1849 /* Emit x= as (x|) */
1850 reginsert(BRANCH, ret); /* Either x */
1851 regtail(ret, regnode(BRANCH)); /* or */
1852 next = regnode(NOTHING); /* null. */
1853 regtail(ret, next);
1854 regoptail(ret, next);
1855 break;
1856
1857 case Magic('{'):
1858 if (!read_limits(&minval, &maxval))
1859 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 if (flags & SIMPLE)
1861 {
1862 reginsert(BRACE_SIMPLE, ret);
1863 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1864 }
1865 else
1866 {
1867 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001868 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 reg_magic == MAGIC_ALL);
1870 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1871 regoptail(ret, regnode(BACK));
1872 regoptail(ret, ret);
1873 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1874 ++num_complex_braces;
1875 }
1876 if (minval > 0 && maxval > 0)
1877 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1878 break;
1879 }
1880 if (re_multi_type(peekchr()) != NOT_MULTI)
1881 {
1882 /* Can't have a multi follow a multi. */
1883 if (peekchr() == Magic('*'))
1884 sprintf((char *)IObuff, _("E61: Nested %s*"),
1885 reg_magic >= MAGIC_ON ? "" : "\\");
1886 else
1887 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1888 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1889 EMSG_RET_NULL(IObuff);
1890 }
1891
1892 return ret;
1893}
1894
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001895/* When making changes to classchars also change nfa_classcodes. */
1896static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1897static int classcodes[] = {
1898 ANY, IDENT, SIDENT, KWORD, SKWORD,
1899 FNAME, SFNAME, PRINT, SPRINT,
1900 WHITE, NWHITE, DIGIT, NDIGIT,
1901 HEX, NHEX, OCTAL, NOCTAL,
1902 WORD, NWORD, HEAD, NHEAD,
1903 ALPHA, NALPHA, LOWER, NLOWER,
1904 UPPER, NUPPER
1905};
1906
Bram Moolenaar071d4272004-06-13 20:20:40 +00001907/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001908 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 *
1910 * Optimization: gobbles an entire sequence of ordinary characters so that
1911 * it can turn them into a single node, which is smaller to store and
1912 * faster to run. Don't do this when one_exactly is set.
1913 */
1914 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001915regatom(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916{
1917 char_u *ret;
1918 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 char_u *p;
1921 int extra = 0;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001922 int save_prev_at_start = prev_at_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923
1924 *flagp = WORST; /* Tentatively. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925
1926 c = getchr();
1927 switch (c)
1928 {
1929 case Magic('^'):
1930 ret = regnode(BOL);
1931 break;
1932
1933 case Magic('$'):
1934 ret = regnode(EOL);
1935#if defined(FEAT_SYN_HL) || defined(PROTO)
1936 had_eol = TRUE;
1937#endif
1938 break;
1939
1940 case Magic('<'):
1941 ret = regnode(BOW);
1942 break;
1943
1944 case Magic('>'):
1945 ret = regnode(EOW);
1946 break;
1947
1948 case Magic('_'):
1949 c = no_Magic(getchr());
1950 if (c == '^') /* "\_^" is start-of-line */
1951 {
1952 ret = regnode(BOL);
1953 break;
1954 }
1955 if (c == '$') /* "\_$" is end-of-line */
1956 {
1957 ret = regnode(EOL);
1958#if defined(FEAT_SYN_HL) || defined(PROTO)
1959 had_eol = TRUE;
1960#endif
1961 break;
1962 }
1963
1964 extra = ADD_NL;
1965 *flagp |= HASNL;
1966
1967 /* "\_[" is character range plus newline */
1968 if (c == '[')
1969 goto collection;
1970
1971 /* "\_x" is character class plus newline */
1972 /*FALLTHROUGH*/
1973
1974 /*
1975 * Character classes.
1976 */
1977 case Magic('.'):
1978 case Magic('i'):
1979 case Magic('I'):
1980 case Magic('k'):
1981 case Magic('K'):
1982 case Magic('f'):
1983 case Magic('F'):
1984 case Magic('p'):
1985 case Magic('P'):
1986 case Magic('s'):
1987 case Magic('S'):
1988 case Magic('d'):
1989 case Magic('D'):
1990 case Magic('x'):
1991 case Magic('X'):
1992 case Magic('o'):
1993 case Magic('O'):
1994 case Magic('w'):
1995 case Magic('W'):
1996 case Magic('h'):
1997 case Magic('H'):
1998 case Magic('a'):
1999 case Magic('A'):
2000 case Magic('l'):
2001 case Magic('L'):
2002 case Magic('u'):
2003 case Magic('U'):
2004 p = vim_strchr(classchars, no_Magic(c));
2005 if (p == NULL)
2006 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002007#ifdef FEAT_MBYTE
2008 /* When '.' is followed by a composing char ignore the dot, so that
2009 * the composing char is matched here. */
2010 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
2011 {
2012 c = getchr();
2013 goto do_multibyte;
2014 }
2015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 ret = regnode(classcodes[p - classchars] + extra);
2017 *flagp |= HASWIDTH | SIMPLE;
2018 break;
2019
2020 case Magic('n'):
2021 if (reg_string)
2022 {
2023 /* In a string "\n" matches a newline character. */
2024 ret = regnode(EXACTLY);
2025 regc(NL);
2026 regc(NUL);
2027 *flagp |= HASWIDTH | SIMPLE;
2028 }
2029 else
2030 {
2031 /* In buffer text "\n" matches the end of a line. */
2032 ret = regnode(NEWL);
2033 *flagp |= HASWIDTH | HASNL;
2034 }
2035 break;
2036
2037 case Magic('('):
2038 if (one_exactly)
2039 EMSG_ONE_RET_NULL;
2040 ret = reg(REG_PAREN, &flags);
2041 if (ret == NULL)
2042 return NULL;
2043 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2044 break;
2045
2046 case NUL:
2047 case Magic('|'):
2048 case Magic('&'):
2049 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002050 if (one_exactly)
2051 EMSG_ONE_RET_NULL;
Bram Moolenaar95f09602016-11-10 20:01:45 +01002052 IEMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 /* NOTREACHED */
2054
2055 case Magic('='):
2056 case Magic('?'):
2057 case Magic('+'):
2058 case Magic('@'):
2059 case Magic('{'):
2060 case Magic('*'):
2061 c = no_Magic(c);
2062 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
2063 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
2064 ? "" : "\\", c);
2065 EMSG_RET_NULL(IObuff);
2066 /* NOTREACHED */
2067
2068 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002069 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
2071 char_u *lp;
2072
2073 ret = regnode(EXACTLY);
2074 lp = reg_prev_sub;
2075 while (*lp != NUL)
2076 regc(*lp++);
2077 regc(NUL);
2078 if (*reg_prev_sub != NUL)
2079 {
2080 *flagp |= HASWIDTH;
2081 if ((lp - reg_prev_sub) == 1)
2082 *flagp |= SIMPLE;
2083 }
2084 }
2085 else
2086 EMSG_RET_NULL(_(e_nopresub));
2087 break;
2088
2089 case Magic('1'):
2090 case Magic('2'):
2091 case Magic('3'):
2092 case Magic('4'):
2093 case Magic('5'):
2094 case Magic('6'):
2095 case Magic('7'):
2096 case Magic('8'):
2097 case Magic('9'):
2098 {
2099 int refnum;
2100
2101 refnum = c - Magic('0');
2102 /*
2103 * Check if the back reference is legal. We must have seen the
2104 * close brace.
2105 * TODO: Should also check that we don't refer to something
2106 * that is repeated (+*=): what instance of the repetition
2107 * should we match?
2108 */
2109 if (!had_endbrace[refnum])
2110 {
2111 /* Trick: check if "@<=" or "@<!" follows, in which case
2112 * the \1 can appear before the referenced match. */
2113 for (p = regparse; *p != NUL; ++p)
2114 if (p[0] == '@' && p[1] == '<'
2115 && (p[2] == '!' || p[2] == '='))
2116 break;
2117 if (*p == NUL)
2118 EMSG_RET_NULL(_("E65: Illegal back reference"));
2119 }
2120 ret = regnode(BACKREF + refnum);
2121 }
2122 break;
2123
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 case Magic('z'):
2125 {
2126 c = no_Magic(getchr());
2127 switch (c)
2128 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002129#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 case '(': if (reg_do_extmatch != REX_SET)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002131 EMSG_RET_NULL(_(e_z_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132 if (one_exactly)
2133 EMSG_ONE_RET_NULL;
2134 ret = reg(REG_ZPAREN, &flags);
2135 if (ret == NULL)
2136 return NULL;
2137 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2138 re_has_z = REX_SET;
2139 break;
2140
2141 case '1':
2142 case '2':
2143 case '3':
2144 case '4':
2145 case '5':
2146 case '6':
2147 case '7':
2148 case '8':
2149 case '9': if (reg_do_extmatch != REX_USE)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002150 EMSG_RET_NULL(_(e_z1_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 ret = regnode(ZREF + c - '0');
2152 re_has_z = REX_USE;
2153 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155
2156 case 's': ret = regnode(MOPEN + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002157 if (re_mult_next("\\zs") == FAIL)
2158 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 break;
2160
2161 case 'e': ret = regnode(MCLOSE + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002162 if (re_mult_next("\\ze") == FAIL)
2163 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 break;
2165
2166 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2167 }
2168 }
2169 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
2171 case Magic('%'):
2172 {
2173 c = no_Magic(getchr());
2174 switch (c)
2175 {
2176 /* () without a back reference */
2177 case '(':
2178 if (one_exactly)
2179 EMSG_ONE_RET_NULL;
2180 ret = reg(REG_NPAREN, &flags);
2181 if (ret == NULL)
2182 return NULL;
2183 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2184 break;
2185
2186 /* Catch \%^ and \%$ regardless of where they appear in the
2187 * pattern -- regardless of whether or not it makes sense. */
2188 case '^':
2189 ret = regnode(RE_BOF);
2190 break;
2191
2192 case '$':
2193 ret = regnode(RE_EOF);
2194 break;
2195
2196 case '#':
2197 ret = regnode(CURSOR);
2198 break;
2199
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002200 case 'V':
2201 ret = regnode(RE_VISUAL);
2202 break;
2203
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002204 case 'C':
2205 ret = regnode(RE_COMPOSING);
2206 break;
2207
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 /* \%[abc]: Emit as a list of branches, all ending at the last
2209 * branch which matches nothing. */
2210 case '[':
2211 if (one_exactly) /* doesn't nest */
2212 EMSG_ONE_RET_NULL;
2213 {
2214 char_u *lastbranch;
2215 char_u *lastnode = NULL;
2216 char_u *br;
2217
2218 ret = NULL;
2219 while ((c = getchr()) != ']')
2220 {
2221 if (c == NUL)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002222 EMSG2_RET_NULL(_(e_missing_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 reg_magic == MAGIC_ALL);
2224 br = regnode(BRANCH);
2225 if (ret == NULL)
2226 ret = br;
2227 else
2228 regtail(lastnode, br);
2229
2230 ungetchr();
2231 one_exactly = TRUE;
2232 lastnode = regatom(flagp);
2233 one_exactly = FALSE;
2234 if (lastnode == NULL)
2235 return NULL;
2236 }
2237 if (ret == NULL)
Bram Moolenaar2976c022013-06-05 21:30:37 +02002238 EMSG2_RET_NULL(_(e_empty_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 reg_magic == MAGIC_ALL);
2240 lastbranch = regnode(BRANCH);
2241 br = regnode(NOTHING);
2242 if (ret != JUST_CALC_SIZE)
2243 {
2244 regtail(lastnode, br);
2245 regtail(lastbranch, br);
2246 /* connect all branches to the NOTHING
2247 * branch at the end */
2248 for (br = ret; br != lastnode; )
2249 {
2250 if (OP(br) == BRANCH)
2251 {
2252 regtail(br, lastbranch);
2253 br = OPERAND(br);
2254 }
2255 else
2256 br = regnext(br);
2257 }
2258 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002259 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 break;
2261 }
2262
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002263 case 'd': /* %d123 decimal */
2264 case 'o': /* %o123 octal */
2265 case 'x': /* %xab hex 2 */
2266 case 'u': /* %uabcd hex 4 */
2267 case 'U': /* %U1234abcd hex 8 */
2268 {
2269 int i;
2270
2271 switch (c)
2272 {
2273 case 'd': i = getdecchrs(); break;
2274 case 'o': i = getoctchrs(); break;
2275 case 'x': i = gethexchrs(2); break;
2276 case 'u': i = gethexchrs(4); break;
2277 case 'U': i = gethexchrs(8); break;
2278 default: i = -1; break;
2279 }
2280
2281 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002282 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002283 _("E678: Invalid character after %s%%[dxouU]"),
2284 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002285#ifdef FEAT_MBYTE
2286 if (use_multibytecode(i))
2287 ret = regnode(MULTIBYTECODE);
2288 else
2289#endif
2290 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002291 if (i == 0)
2292 regc(0x0a);
2293 else
2294#ifdef FEAT_MBYTE
2295 regmbc(i);
2296#else
2297 regc(i);
2298#endif
2299 regc(NUL);
2300 *flagp |= HASWIDTH;
2301 break;
2302 }
2303
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002305 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2306 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 {
2308 long_u n = 0;
2309 int cmp;
2310
2311 cmp = c;
2312 if (cmp == '<' || cmp == '>')
2313 c = getchr();
2314 while (VIM_ISDIGIT(c))
2315 {
2316 n = n * 10 + (c - '0');
2317 c = getchr();
2318 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002319 if (c == '\'' && n == 0)
2320 {
2321 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2322 c = getchr();
2323 ret = regnode(RE_MARK);
2324 if (ret == JUST_CALC_SIZE)
2325 regsize += 2;
2326 else
2327 {
2328 *regcode++ = c;
2329 *regcode++ = cmp;
2330 }
2331 break;
2332 }
2333 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
2335 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002336 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 ret = regnode(RE_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002338 if (save_prev_at_start)
2339 at_start = TRUE;
2340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 else if (c == 'c')
2342 ret = regnode(RE_COL);
2343 else
2344 ret = regnode(RE_VCOL);
2345 if (ret == JUST_CALC_SIZE)
2346 regsize += 5;
2347 else
2348 {
2349 /* put the number and the optional
2350 * comparator after the opcode */
2351 regcode = re_put_long(regcode, n);
2352 *regcode++ = cmp;
2353 }
2354 break;
2355 }
2356 }
2357
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002358 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 reg_magic == MAGIC_ALL);
2360 }
2361 }
2362 break;
2363
2364 case Magic('['):
2365collection:
2366 {
2367 char_u *lp;
2368
2369 /*
2370 * If there is no matching ']', we assume the '[' is a normal
2371 * character. This makes 'incsearch' and ":help [" work.
2372 */
2373 lp = skip_anyof(regparse);
2374 if (*lp == ']') /* there is a matching ']' */
2375 {
2376 int startc = -1; /* > 0 when next '-' is a range */
2377 int endc;
2378
2379 /*
2380 * In a character class, different parsing rules apply.
2381 * Not even \ is special anymore, nothing is.
2382 */
2383 if (*regparse == '^') /* Complement of range. */
2384 {
2385 ret = regnode(ANYBUT + extra);
2386 regparse++;
2387 }
2388 else
2389 ret = regnode(ANYOF + extra);
2390
2391 /* At the start ']' and '-' mean the literal character. */
2392 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002393 {
2394 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 while (*regparse != NUL && *regparse != ']')
2399 {
2400 if (*regparse == '-')
2401 {
2402 ++regparse;
2403 /* The '-' is not used for a range at the end and
2404 * after or before a '\n'. */
2405 if (*regparse == ']' || *regparse == NUL
2406 || startc == -1
2407 || (regparse[0] == '\\' && regparse[1] == 'n'))
2408 {
2409 regc('-');
2410 startc = '-'; /* [--x] is a range */
2411 }
2412 else
2413 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002414 /* Also accept "a-[.z.]" */
2415 endc = 0;
2416 if (*regparse == '[')
2417 endc = get_coll_element(&regparse);
2418 if (endc == 0)
2419 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002421 if (has_mbyte)
2422 endc = mb_ptr2char_adv(&regparse);
2423 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002425 endc = *regparse++;
2426 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002427
2428 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002429 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002430 endc = coll_get_char();
2431
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 if (startc > endc)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02002433 EMSG_RET_NULL(_(e_reverse_range));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434#ifdef FEAT_MBYTE
2435 if (has_mbyte && ((*mb_char2len)(startc) > 1
2436 || (*mb_char2len)(endc) > 1))
2437 {
Bram Moolenaar966e58e2017-06-05 16:54:08 +02002438 /* Limit to a range of 256 chars. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 if (endc > startc + 256)
Bram Moolenaar966e58e2017-06-05 16:54:08 +02002440 EMSG_RET_NULL(_(e_large_class));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 while (++startc <= endc)
2442 regmbc(startc);
2443 }
2444 else
2445#endif
2446 {
2447#ifdef EBCDIC
2448 int alpha_only = FALSE;
2449
2450 /* for alphabetical range skip the gaps
2451 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2452 if (isalpha(startc) && isalpha(endc))
2453 alpha_only = TRUE;
2454#endif
2455 while (++startc <= endc)
2456#ifdef EBCDIC
2457 if (!alpha_only || isalpha(startc))
2458#endif
2459 regc(startc);
2460 }
2461 startc = -1;
2462 }
2463 }
2464 /*
2465 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2466 * accepts "\t", "\e", etc., but only when the 'l' flag in
2467 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002468 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 */
2470 else if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002471 && !reg_cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002473 || (!reg_cpo_lit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 && vim_strchr(REGEXP_ABBR,
2475 regparse[1]) != NULL)))
2476 {
2477 regparse++;
2478 if (*regparse == 'n')
2479 {
2480 /* '\n' in range: also match NL */
2481 if (ret != JUST_CALC_SIZE)
2482 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002483 /* Using \n inside [^] does not change what
2484 * matches. "[^\n]" is the same as ".". */
2485 if (*ret == ANYOF)
2486 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002488 *flagp |= HASNL;
2489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 /* else: must have had a \n already */
2491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 regparse++;
2493 startc = -1;
2494 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002495 else if (*regparse == 'd'
2496 || *regparse == 'o'
2497 || *regparse == 'x'
2498 || *regparse == 'u'
2499 || *regparse == 'U')
2500 {
2501 startc = coll_get_char();
2502 if (startc == 0)
2503 regc(0x0a);
2504 else
2505#ifdef FEAT_MBYTE
2506 regmbc(startc);
2507#else
2508 regc(startc);
2509#endif
2510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 else
2512 {
2513 startc = backslash_trans(*regparse++);
2514 regc(startc);
2515 }
2516 }
2517 else if (*regparse == '[')
2518 {
2519 int c_class;
2520 int cu;
2521
Bram Moolenaardf177f62005-02-22 08:39:57 +00002522 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 startc = -1;
2524 /* Characters assumed to be 8 bits! */
2525 switch (c_class)
2526 {
2527 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002528 c_class = get_equi_class(&regparse);
2529 if (c_class != 0)
2530 {
2531 /* produce equivalence class */
2532 reg_equi_class(c_class);
2533 }
2534 else if ((c_class =
2535 get_coll_element(&regparse)) != 0)
2536 {
2537 /* produce a collating element */
2538 regmbc(c_class);
2539 }
2540 else
2541 {
2542 /* literal '[', allow [[-x] as a range */
2543 startc = *regparse++;
2544 regc(startc);
2545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 break;
2547 case CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002548 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (isalnum(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002550 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 break;
2552 case CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002553 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 if (isalpha(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002555 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 break;
2557 case CLASS_BLANK:
2558 regc(' ');
2559 regc('\t');
2560 break;
2561 case CLASS_CNTRL:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02002562 for (cu = 1; cu <= 127; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 if (iscntrl(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002564 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 break;
2566 case CLASS_DIGIT:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02002567 for (cu = 1; cu <= 127; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 if (VIM_ISDIGIT(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002569 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 break;
2571 case CLASS_GRAPH:
Bram Moolenaar0c078fc2017-03-29 15:31:20 +02002572 for (cu = 1; cu <= 127; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 if (isgraph(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002574 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 break;
2576 case CLASS_LOWER:
2577 for (cu = 1; cu <= 255; cu++)
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002578 if (MB_ISLOWER(cu) && cu != 170
2579 && cu != 186)
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002580 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 break;
2582 case CLASS_PRINT:
2583 for (cu = 1; cu <= 255; cu++)
2584 if (vim_isprintc(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002585 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 break;
2587 case CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002588 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589 if (ispunct(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002590 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 break;
2592 case CLASS_SPACE:
2593 for (cu = 9; cu <= 13; cu++)
2594 regc(cu);
2595 regc(' ');
2596 break;
2597 case CLASS_UPPER:
2598 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002599 if (MB_ISUPPER(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002600 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 break;
2602 case CLASS_XDIGIT:
2603 for (cu = 1; cu <= 255; cu++)
2604 if (vim_isxdigit(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002605 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 break;
2607 case CLASS_TAB:
2608 regc('\t');
2609 break;
2610 case CLASS_RETURN:
2611 regc('\r');
2612 break;
2613 case CLASS_BACKSPACE:
2614 regc('\b');
2615 break;
2616 case CLASS_ESCAPE:
2617 regc('\033');
2618 break;
2619 }
2620 }
2621 else
2622 {
2623#ifdef FEAT_MBYTE
2624 if (has_mbyte)
2625 {
2626 int len;
2627
2628 /* produce a multibyte character, including any
2629 * following composing characters */
2630 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002631 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002632 if (enc_utf8 && utf_char2len(startc) != len)
2633 startc = -1; /* composing chars */
2634 while (--len >= 0)
2635 regc(*regparse++);
2636 }
2637 else
2638#endif
2639 {
2640 startc = *regparse++;
2641 regc(startc);
2642 }
2643 }
2644 }
2645 regc(NUL);
2646 prevchr_len = 1; /* last char was the ']' */
2647 if (*regparse != ']')
2648 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2649 skipchr(); /* let's be friends with the lexer again */
2650 *flagp |= HASWIDTH | SIMPLE;
2651 break;
2652 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002653 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002654 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002655 }
2656 /* FALLTHROUGH */
2657
2658 default:
2659 {
2660 int len;
2661
2662#ifdef FEAT_MBYTE
2663 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002664 * before a multi and when it's a composing char. */
2665 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002667do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668 ret = regnode(MULTIBYTECODE);
2669 regmbc(c);
2670 *flagp |= HASWIDTH | SIMPLE;
2671 break;
2672 }
2673#endif
2674
2675 ret = regnode(EXACTLY);
2676
2677 /*
2678 * Append characters as long as:
2679 * - there is no following multi, we then need the character in
2680 * front of it as a single character operand
2681 * - not running into a Magic character
2682 * - "one_exactly" is not set
2683 * But always emit at least one character. Might be a Multi,
2684 * e.g., a "[" without matching "]".
2685 */
2686 for (len = 0; c != NUL && (len == 0
2687 || (re_multi_type(peekchr()) == NOT_MULTI
2688 && !one_exactly
2689 && !is_Magic(c))); ++len)
2690 {
2691 c = no_Magic(c);
2692#ifdef FEAT_MBYTE
2693 if (has_mbyte)
2694 {
2695 regmbc(c);
2696 if (enc_utf8)
2697 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 int l;
2699
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 for (;;)
2702 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002703 l = utf_ptr2len(regparse);
2704 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 regmbc(utf_ptr2char(regparse));
2707 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 }
2710 }
2711 else
2712#endif
2713 regc(c);
2714 c = getchr();
2715 }
2716 ungetchr();
2717
2718 regc(NUL);
2719 *flagp |= HASWIDTH;
2720 if (len == 1)
2721 *flagp |= SIMPLE;
2722 }
2723 break;
2724 }
2725
2726 return ret;
2727}
2728
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002729#ifdef FEAT_MBYTE
2730/*
2731 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2732 * character "c".
2733 */
2734 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002735use_multibytecode(int c)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002736{
2737 return has_mbyte && (*mb_char2len)(c) > 1
2738 && (re_multi_type(peekchr()) != NOT_MULTI
2739 || (enc_utf8 && utf_iscomposing(c)));
2740}
2741#endif
2742
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002744 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 * Return pointer to generated code.
2746 */
2747 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002748regnode(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749{
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
Bram Moolenaar05540972016-01-30 20:31:25 +01002768regc(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
2770 if (regcode == JUST_CALC_SIZE)
2771 regsize++;
2772 else
2773 *regcode++ = b;
2774}
2775
2776#ifdef FEAT_MBYTE
2777/*
2778 * Emit (if appropriate) a multi-byte character of code
2779 */
2780 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002781regmbc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002782{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002783 if (!has_mbyte && c > 0xff)
2784 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002785 if (regcode == JUST_CALC_SIZE)
2786 regsize += (*mb_char2len)(c);
2787 else
2788 regcode += (*mb_char2bytes)(c, regcode);
2789}
2790#endif
2791
2792/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002793 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002794 *
2795 * Means relocating the operand.
2796 */
2797 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002798reginsert(int op, char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002799{
2800 char_u *src;
2801 char_u *dst;
2802 char_u *place;
2803
2804 if (regcode == JUST_CALC_SIZE)
2805 {
2806 regsize += 3;
2807 return;
2808 }
2809 src = regcode;
2810 regcode += 3;
2811 dst = regcode;
2812 while (src > opnd)
2813 *--dst = *--src;
2814
2815 place = opnd; /* Op node, where operand used to be. */
2816 *place++ = op;
2817 *place++ = NUL;
2818 *place = NUL;
2819}
2820
2821/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002822 * Insert an operator in front of already-emitted operand.
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002823 * Add a number to the operator.
2824 */
2825 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002826reginsert_nr(int op, long val, char_u *opnd)
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002827{
2828 char_u *src;
2829 char_u *dst;
2830 char_u *place;
2831
2832 if (regcode == JUST_CALC_SIZE)
2833 {
2834 regsize += 7;
2835 return;
2836 }
2837 src = regcode;
2838 regcode += 7;
2839 dst = regcode;
2840 while (src > opnd)
2841 *--dst = *--src;
2842
2843 place = opnd; /* Op node, where operand used to be. */
2844 *place++ = op;
2845 *place++ = NUL;
2846 *place++ = NUL;
2847 place = re_put_long(place, (long_u)val);
2848}
2849
2850/*
2851 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 * The operator has the given limit values as operands. Also set next pointer.
2853 *
2854 * Means relocating the operand.
2855 */
2856 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002857reginsert_limits(
2858 int op,
2859 long minval,
2860 long maxval,
2861 char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002862{
2863 char_u *src;
2864 char_u *dst;
2865 char_u *place;
2866
2867 if (regcode == JUST_CALC_SIZE)
2868 {
2869 regsize += 11;
2870 return;
2871 }
2872 src = regcode;
2873 regcode += 11;
2874 dst = regcode;
2875 while (src > opnd)
2876 *--dst = *--src;
2877
2878 place = opnd; /* Op node, where operand used to be. */
2879 *place++ = op;
2880 *place++ = NUL;
2881 *place++ = NUL;
2882 place = re_put_long(place, (long_u)minval);
2883 place = re_put_long(place, (long_u)maxval);
2884 regtail(opnd, place);
2885}
2886
2887/*
2888 * Write a long as four bytes at "p" and return pointer to the next char.
2889 */
2890 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002891re_put_long(char_u *p, long_u val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892{
2893 *p++ = (char_u) ((val >> 24) & 0377);
2894 *p++ = (char_u) ((val >> 16) & 0377);
2895 *p++ = (char_u) ((val >> 8) & 0377);
2896 *p++ = (char_u) (val & 0377);
2897 return p;
2898}
2899
2900/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002901 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902 */
2903 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002904regtail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905{
2906 char_u *scan;
2907 char_u *temp;
2908 int offset;
2909
2910 if (p == JUST_CALC_SIZE)
2911 return;
2912
2913 /* Find last node. */
2914 scan = p;
2915 for (;;)
2916 {
2917 temp = regnext(scan);
2918 if (temp == NULL)
2919 break;
2920 scan = temp;
2921 }
2922
Bram Moolenaar582fd852005-03-28 20:58:01 +00002923 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 offset = (int)(scan - val);
2925 else
2926 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002927 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002928 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002929 * values in too many places. */
2930 if (offset > 0xffff)
2931 reg_toolong = TRUE;
2932 else
2933 {
2934 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2935 *(scan + 2) = (char_u) (offset & 0377);
2936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937}
2938
2939/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002940 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002941 */
2942 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002943regoptail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002944{
2945 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2946 if (p == NULL || p == JUST_CALC_SIZE
2947 || (OP(p) != BRANCH
2948 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2949 return;
2950 regtail(OPERAND(p), val);
2951}
2952
2953/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002954 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956/*
2957 * Start parsing at "str".
2958 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002959 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002960initchr(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002961{
2962 regparse = str;
2963 prevchr_len = 0;
2964 curchr = prevprevchr = prevchr = nextchr = -1;
2965 at_start = TRUE;
2966 prev_at_start = FALSE;
2967}
2968
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002969/*
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002970 * Save the current parse state, so that it can be restored and parsing
2971 * starts in the same state again.
2972 */
2973 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002974save_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002975{
2976 ps->regparse = regparse;
2977 ps->prevchr_len = prevchr_len;
2978 ps->curchr = curchr;
2979 ps->prevchr = prevchr;
2980 ps->prevprevchr = prevprevchr;
2981 ps->nextchr = nextchr;
2982 ps->at_start = at_start;
2983 ps->prev_at_start = prev_at_start;
2984 ps->regnpar = regnpar;
2985}
2986
2987/*
2988 * Restore a previously saved parse state.
2989 */
2990 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002991restore_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002992{
2993 regparse = ps->regparse;
2994 prevchr_len = ps->prevchr_len;
2995 curchr = ps->curchr;
2996 prevchr = ps->prevchr;
2997 prevprevchr = ps->prevprevchr;
2998 nextchr = ps->nextchr;
2999 at_start = ps->at_start;
3000 prev_at_start = ps->prev_at_start;
3001 regnpar = ps->regnpar;
3002}
3003
3004
3005/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003006 * Get the next character without advancing.
3007 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003009peekchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003011 static int after_slash = FALSE;
3012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013 if (curchr == -1)
3014 {
3015 switch (curchr = regparse[0])
3016 {
3017 case '.':
3018 case '[':
3019 case '~':
3020 /* magic when 'magic' is on */
3021 if (reg_magic >= MAGIC_ON)
3022 curchr = Magic(curchr);
3023 break;
3024 case '(':
3025 case ')':
3026 case '{':
3027 case '%':
3028 case '+':
3029 case '=':
3030 case '?':
3031 case '@':
3032 case '!':
3033 case '&':
3034 case '|':
3035 case '<':
3036 case '>':
3037 case '#': /* future ext. */
3038 case '"': /* future ext. */
3039 case '\'': /* future ext. */
3040 case ',': /* future ext. */
3041 case '-': /* future ext. */
3042 case ':': /* future ext. */
3043 case ';': /* future ext. */
3044 case '`': /* future ext. */
3045 case '/': /* Can't be used in / command */
3046 /* magic only after "\v" */
3047 if (reg_magic == MAGIC_ALL)
3048 curchr = Magic(curchr);
3049 break;
3050 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00003051 /* * is not magic as the very first character, eg "?*ptr", when
3052 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
3053 * "\(\*" is not magic, thus must be magic if "after_slash" */
3054 if (reg_magic >= MAGIC_ON
3055 && !at_start
3056 && !(prev_at_start && prevchr == Magic('^'))
3057 && (after_slash
3058 || (prevchr != Magic('(')
3059 && prevchr != Magic('&')
3060 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 curchr = Magic('*');
3062 break;
3063 case '^':
3064 /* '^' is only magic as the very first character and if it's after
3065 * "\(", "\|", "\&' or "\n" */
3066 if (reg_magic >= MAGIC_OFF
3067 && (at_start
3068 || reg_magic == MAGIC_ALL
3069 || prevchr == Magic('(')
3070 || prevchr == Magic('|')
3071 || prevchr == Magic('&')
3072 || prevchr == Magic('n')
3073 || (no_Magic(prevchr) == '('
3074 && prevprevchr == Magic('%'))))
3075 {
3076 curchr = Magic('^');
3077 at_start = TRUE;
3078 prev_at_start = FALSE;
3079 }
3080 break;
3081 case '$':
3082 /* '$' is only magic as the very last char and if it's in front of
3083 * either "\|", "\)", "\&", or "\n" */
3084 if (reg_magic >= MAGIC_OFF)
3085 {
3086 char_u *p = regparse + 1;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003087 int is_magic_all = (reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003089 /* ignore \c \C \m \M \v \V and \Z after '$' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003091 || p[1] == 'm' || p[1] == 'M'
3092 || p[1] == 'v' || p[1] == 'V' || p[1] == 'Z'))
3093 {
3094 if (p[1] == 'v')
3095 is_magic_all = TRUE;
3096 else if (p[1] == 'm' || p[1] == 'M' || p[1] == 'V')
3097 is_magic_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 p += 2;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 if (p[0] == NUL
3101 || (p[0] == '\\'
3102 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3103 || p[1] == 'n'))
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003104 || (is_magic_all
3105 && (p[0] == '|' || p[0] == '&' || p[0] == ')'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 || reg_magic == MAGIC_ALL)
3107 curchr = Magic('$');
3108 }
3109 break;
3110 case '\\':
3111 {
3112 int c = regparse[1];
3113
3114 if (c == NUL)
3115 curchr = '\\'; /* trailing '\' */
3116 else if (
3117#ifdef EBCDIC
3118 vim_strchr(META, c)
3119#else
3120 c <= '~' && META_flags[c]
3121#endif
3122 )
3123 {
3124 /*
3125 * META contains everything that may be magic sometimes,
3126 * except ^ and $ ("\^" and "\$" are only magic after
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02003127 * "\V"). We now fetch the next character and toggle its
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 * magicness. Therefore, \ is so meta-magic that it is
3129 * not in META.
3130 */
3131 curchr = -1;
3132 prev_at_start = at_start;
3133 at_start = FALSE; /* be able to say "/\*ptr" */
3134 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003135 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 peekchr();
3137 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003138 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 curchr = toggle_Magic(curchr);
3140 }
3141 else if (vim_strchr(REGEXP_ABBR, c))
3142 {
3143 /*
3144 * Handle abbreviations, like "\t" for TAB -- webb
3145 */
3146 curchr = backslash_trans(c);
3147 }
3148 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3149 curchr = toggle_Magic(c);
3150 else
3151 {
3152 /*
3153 * Next character can never be (made) magic?
3154 * Then backslashing it won't do anything.
3155 */
3156#ifdef FEAT_MBYTE
3157 if (has_mbyte)
3158 curchr = (*mb_ptr2char)(regparse + 1);
3159 else
3160#endif
3161 curchr = c;
3162 }
3163 break;
3164 }
3165
3166#ifdef FEAT_MBYTE
3167 default:
3168 if (has_mbyte)
3169 curchr = (*mb_ptr2char)(regparse);
3170#endif
3171 }
3172 }
3173
3174 return curchr;
3175}
3176
3177/*
3178 * Eat one lexed character. Do this in a way that we can undo it.
3179 */
3180 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003181skipchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003182{
3183 /* peekchr() eats a backslash, do the same here */
3184 if (*regparse == '\\')
3185 prevchr_len = 1;
3186 else
3187 prevchr_len = 0;
3188 if (regparse[prevchr_len] != NUL)
3189 {
3190#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003191 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003192 /* exclude composing chars that mb_ptr2len does include */
3193 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003194 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003195 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 else
3197#endif
3198 ++prevchr_len;
3199 }
3200 regparse += prevchr_len;
3201 prev_at_start = at_start;
3202 at_start = FALSE;
3203 prevprevchr = prevchr;
3204 prevchr = curchr;
3205 curchr = nextchr; /* use previously unget char, or -1 */
3206 nextchr = -1;
3207}
3208
3209/*
3210 * Skip a character while keeping the value of prev_at_start for at_start.
3211 * prevchr and prevprevchr are also kept.
3212 */
3213 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003214skipchr_keepstart(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215{
3216 int as = prev_at_start;
3217 int pr = prevchr;
3218 int prpr = prevprevchr;
3219
3220 skipchr();
3221 at_start = as;
3222 prevchr = pr;
3223 prevprevchr = prpr;
3224}
3225
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003226/*
3227 * Get the next character from the pattern. We know about magic and such, so
3228 * therefore we need a lexical analyzer.
3229 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003231getchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003232{
3233 int chr = peekchr();
3234
3235 skipchr();
3236 return chr;
3237}
3238
3239/*
3240 * put character back. Works only once!
3241 */
3242 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003243ungetchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244{
3245 nextchr = curchr;
3246 curchr = prevchr;
3247 prevchr = prevprevchr;
3248 at_start = prev_at_start;
3249 prev_at_start = FALSE;
3250
3251 /* Backup regparse, so that it's at the same position as before the
3252 * getchr(). */
3253 regparse -= prevchr_len;
3254}
3255
3256/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003257 * Get and return the value of the hex string at the current position.
3258 * Return -1 if there is no valid hex number.
3259 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003260 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003261 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003262 * The parameter controls the maximum number of input characters. This will be
3263 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3264 */
3265 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003266gethexchrs(int maxinputlen)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003267{
3268 int nr = 0;
3269 int c;
3270 int i;
3271
3272 for (i = 0; i < maxinputlen; ++i)
3273 {
3274 c = regparse[0];
3275 if (!vim_isxdigit(c))
3276 break;
3277 nr <<= 4;
3278 nr |= hex2nr(c);
3279 ++regparse;
3280 }
3281
3282 if (i == 0)
3283 return -1;
3284 return nr;
3285}
3286
3287/*
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003288 * Get and return the value of the decimal string immediately after the
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003289 * current position. Return -1 for invalid. Consumes all digits.
3290 */
3291 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003292getdecchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003293{
3294 int nr = 0;
3295 int c;
3296 int i;
3297
3298 for (i = 0; ; ++i)
3299 {
3300 c = regparse[0];
3301 if (c < '0' || c > '9')
3302 break;
3303 nr *= 10;
3304 nr += c - '0';
3305 ++regparse;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003306 curchr = -1; /* no longer valid */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003307 }
3308
3309 if (i == 0)
3310 return -1;
3311 return nr;
3312}
3313
3314/*
3315 * get and return the value of the octal string immediately after the current
3316 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3317 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3318 * treat 8 or 9 as recognised characters. Position is updated:
3319 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003320 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003321 */
3322 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003323getoctchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003324{
3325 int nr = 0;
3326 int c;
3327 int i;
3328
3329 for (i = 0; i < 3 && nr < 040; ++i)
3330 {
3331 c = regparse[0];
3332 if (c < '0' || c > '7')
3333 break;
3334 nr <<= 3;
3335 nr |= hex2nr(c);
3336 ++regparse;
3337 }
3338
3339 if (i == 0)
3340 return -1;
3341 return nr;
3342}
3343
3344/*
3345 * Get a number after a backslash that is inside [].
3346 * When nothing is recognized return a backslash.
3347 */
3348 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003349coll_get_char(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003350{
3351 int nr = -1;
3352
3353 switch (*regparse++)
3354 {
3355 case 'd': nr = getdecchrs(); break;
3356 case 'o': nr = getoctchrs(); break;
3357 case 'x': nr = gethexchrs(2); break;
3358 case 'u': nr = gethexchrs(4); break;
3359 case 'U': nr = gethexchrs(8); break;
3360 }
3361 if (nr < 0)
3362 {
3363 /* If getting the number fails be backwards compatible: the character
3364 * is a backslash. */
3365 --regparse;
3366 nr = '\\';
3367 }
3368 return nr;
3369}
3370
3371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 * read_limits - Read two integers to be taken as a minimum and maximum.
3373 * If the first character is '-', then the range is reversed.
3374 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3375 * missing, a very big number is the default.
3376 */
3377 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003378read_limits(long *minval, long *maxval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
3380 int reverse = FALSE;
3381 char_u *first_char;
3382 long tmp;
3383
3384 if (*regparse == '-')
3385 {
3386 /* Starts with '-', so reverse the range later */
3387 regparse++;
3388 reverse = TRUE;
3389 }
3390 first_char = regparse;
3391 *minval = getdigits(&regparse);
3392 if (*regparse == ',') /* There is a comma */
3393 {
3394 if (vim_isdigit(*++regparse))
3395 *maxval = getdigits(&regparse);
3396 else
3397 *maxval = MAX_LIMIT;
3398 }
3399 else if (VIM_ISDIGIT(*first_char))
3400 *maxval = *minval; /* It was \{n} or \{-n} */
3401 else
3402 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3403 if (*regparse == '\\')
3404 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003405 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 {
3407 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
3408 reg_magic == MAGIC_ALL ? "" : "\\");
3409 EMSG_RET_FAIL(IObuff);
3410 }
3411
3412 /*
3413 * Reverse the range if there was a '-', or make sure it is in the right
3414 * order otherwise.
3415 */
3416 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3417 {
3418 tmp = *minval;
3419 *minval = *maxval;
3420 *maxval = tmp;
3421 }
3422 skipchr(); /* let's be friends with the lexer again */
3423 return OK;
3424}
3425
3426/*
3427 * vim_regexec and friends
3428 */
3429
3430/*
3431 * Global work variables for vim_regexec().
3432 */
3433
3434/* The current match-position is remembered with these variables: */
3435static linenr_T reglnum; /* line number, relative to first line */
3436static char_u *regline; /* start of current line */
3437static char_u *reginput; /* current input, points into "regline" */
3438
3439static int need_clear_subexpr; /* subexpressions still need to be
3440 * cleared */
3441#ifdef FEAT_SYN_HL
3442static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3443 * still need to be cleared */
3444#endif
3445
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446/*
3447 * Structure used to save the current input state, when it needs to be
3448 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003449 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003450 */
3451typedef struct
3452{
3453 union
3454 {
3455 char_u *ptr; /* reginput pointer, for single-line regexp */
3456 lpos_T pos; /* reginput pos, for multi-line regexp */
3457 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003458 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459} regsave_T;
3460
3461/* struct to save start/end pointer/position in for \(\) */
3462typedef struct
3463{
3464 union
3465 {
3466 char_u *ptr;
3467 lpos_T pos;
3468 } se_u;
3469} save_se_T;
3470
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003471/* used for BEHIND and NOBEHIND matching */
3472typedef struct regbehind_S
3473{
3474 regsave_T save_after;
3475 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003476 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003477 save_se_T save_start[NSUBEXP];
3478 save_se_T save_end[NSUBEXP];
3479} regbehind_T;
3480
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003481static char_u *reg_getline(linenr_T lnum);
3482static long bt_regexec_both(char_u *line, colnr_T col, proftime_T *tm);
3483static long regtry(bt_regprog_T *prog, colnr_T col);
3484static void cleanup_subexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003485#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003486static void cleanup_zsubexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003488static void save_subexpr(regbehind_T *bp);
3489static void restore_subexpr(regbehind_T *bp);
3490static void reg_nextline(void);
3491static void reg_save(regsave_T *save, garray_T *gap);
3492static void reg_restore(regsave_T *save, garray_T *gap);
3493static int reg_save_equal(regsave_T *save);
3494static void save_se_multi(save_se_T *savep, lpos_T *posp);
3495static void save_se_one(save_se_T *savep, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497/* Save the sub-expressions before attempting a match. */
3498#define save_se(savep, posp, pp) \
3499 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3500
3501/* After a failed match restore the sub-expressions. */
3502#define restore_se(savep, posp, pp) { \
3503 if (REG_MULTI) \
3504 *(posp) = (savep)->se_u.pos; \
3505 else \
3506 *(pp) = (savep)->se_u.ptr; }
3507
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003508static int re_num_cmp(long_u val, char_u *scan);
3509static int match_with_backref(linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum, colnr_T end_col, int *bytelen);
3510static int regmatch(char_u *prog);
3511static int regrepeat(char_u *p, long maxcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512
3513#ifdef DEBUG
3514int regnarrate = 0;
3515#endif
3516
3517/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3519 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003520 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003521 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003522static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003523static unsigned reg_tofreelen;
3524
3525/*
Bram Moolenaar6100d022016-10-02 16:51:57 +02003526 * Structure used to store the execution state of the regex engine.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003527 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 * done:
3529 * single-line multi-line
3530 * reg_match &regmatch_T NULL
3531 * reg_mmatch NULL &regmmatch_T
3532 * reg_startp reg_match->startp <invalid>
3533 * reg_endp reg_match->endp <invalid>
3534 * reg_startpos <invalid> reg_mmatch->startpos
3535 * reg_endpos <invalid> reg_mmatch->endpos
3536 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003537 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 * reg_firstlnum <invalid> first line in which to search
3539 * reg_maxline 0 last line nr
3540 * reg_line_lbr FALSE or TRUE FALSE
3541 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003542typedef struct {
3543 regmatch_T *reg_match;
3544 regmmatch_T *reg_mmatch;
3545 char_u **reg_startp;
3546 char_u **reg_endp;
3547 lpos_T *reg_startpos;
3548 lpos_T *reg_endpos;
3549 win_T *reg_win;
3550 buf_T *reg_buf;
3551 linenr_T reg_firstlnum;
3552 linenr_T reg_maxline;
3553 int reg_line_lbr; /* "\n" in string is line break */
3554
3555 /* Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3556 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3557 * contains '\c' or '\C' the value is overruled. */
3558 int reg_ic;
3559
3560#ifdef FEAT_MBYTE
3561 /* Similar to rex.reg_ic, but only for 'combining' characters. Set with \Z
3562 * flag in the regexp. Defaults to false, always. */
3563 int reg_icombine;
3564#endif
3565
3566 /* Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3567 * there is no maximum. */
3568 colnr_T reg_maxcol;
3569} regexec_T;
3570
3571static regexec_T rex;
3572static int rex_in_use = FALSE;
3573
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003575/* Values for rs_state in regitem_T. */
3576typedef enum regstate_E
3577{
3578 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3579 , RS_MOPEN /* MOPEN + [0-9] */
3580 , RS_MCLOSE /* MCLOSE + [0-9] */
3581#ifdef FEAT_SYN_HL
3582 , RS_ZOPEN /* ZOPEN + [0-9] */
3583 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3584#endif
3585 , RS_BRANCH /* BRANCH */
3586 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3587 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3588 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3589 , RS_NOMATCH /* NOMATCH */
3590 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3591 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3592 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3593 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3594} regstate_T;
3595
3596/*
3597 * When there are alternatives a regstate_T is put on the regstack to remember
3598 * what we are doing.
3599 * Before it may be another type of item, depending on rs_state, to remember
3600 * more things.
3601 */
3602typedef struct regitem_S
3603{
3604 regstate_T rs_state; /* what we are doing, one of RS_ above */
3605 char_u *rs_scan; /* current node in program */
3606 union
3607 {
3608 save_se_T sesave;
3609 regsave_T regsave;
3610 } rs_un; /* room for saving reginput */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003611 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003612} regitem_T;
3613
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003614static regitem_T *regstack_push(regstate_T state, char_u *scan);
3615static void regstack_pop(char_u **scan);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003616
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003617/* used for STAR, PLUS and BRACE_SIMPLE matching */
3618typedef struct regstar_S
3619{
3620 int nextb; /* next byte */
3621 int nextb_ic; /* next byte reverse case */
3622 long count;
3623 long minval;
3624 long maxval;
3625} regstar_T;
3626
3627/* used to store input position when a BACK was encountered, so that we now if
3628 * we made any progress since the last time. */
3629typedef struct backpos_S
3630{
3631 char_u *bp_scan; /* "scan" where BACK was encountered */
3632 regsave_T bp_pos; /* last input position */
3633} backpos_T;
3634
3635/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003636 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3637 * to avoid invoking malloc() and free() often.
3638 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3639 * or regbehind_T.
3640 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003641 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003642static garray_T regstack = {0, 0, 0, 0, NULL};
3643static garray_T backpos = {0, 0, 0, 0, NULL};
3644
3645/*
3646 * Both for regstack and backpos tables we use the following strategy of
3647 * allocation (to reduce malloc/free calls):
3648 * - Initial size is fairly small.
3649 * - When needed, the tables are grown bigger (8 times at first, double after
3650 * that).
3651 * - After executing the match we free the memory only if the array has grown.
3652 * Thus the memory is kept allocated when it's at the initial size.
3653 * This makes it fast while not keeping a lot of memory allocated.
3654 * A three times speed increase was observed when using many simple patterns.
3655 */
3656#define REGSTACK_INITIAL 2048
3657#define BACKPOS_INITIAL 64
3658
3659#if defined(EXITFREE) || defined(PROTO)
3660 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003661free_regexp_stuff(void)
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003662{
3663 ga_clear(&regstack);
3664 ga_clear(&backpos);
3665 vim_free(reg_tofree);
3666 vim_free(reg_prev_sub);
3667}
3668#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003669
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670/*
3671 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3672 */
3673 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003674reg_getline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675{
3676 /* when looking behind for a match/no-match lnum is negative. But we
3677 * can't go before line 1 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003678 if (rex.reg_firstlnum + lnum < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 return NULL;
Bram Moolenaar6100d022016-10-02 16:51:57 +02003680 if (lnum > rex.reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003681 /* Must have matched the "\n" in the last line. */
3682 return (char_u *)"";
Bram Moolenaar6100d022016-10-02 16:51:57 +02003683 return ml_get_buf(rex.reg_buf, rex.reg_firstlnum + lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684}
3685
3686static regsave_T behind_pos;
3687
3688#ifdef FEAT_SYN_HL
3689static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3690static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3691static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3692static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3693#endif
3694
3695/* TRUE if using multi-line regexp. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003696#define REG_MULTI (rex.reg_match == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698/*
3699 * Match a regexp against a string.
3700 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3701 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003702 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 *
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003704 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003706 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003707bt_regexec_nl(
3708 regmatch_T *rmp,
3709 char_u *line, /* string to match against */
3710 colnr_T col, /* column to start looking for match */
3711 int line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712{
Bram Moolenaar6100d022016-10-02 16:51:57 +02003713 rex.reg_match = rmp;
3714 rex.reg_mmatch = NULL;
3715 rex.reg_maxline = 0;
3716 rex.reg_line_lbr = line_lbr;
3717 rex.reg_buf = curbuf;
3718 rex.reg_win = NULL;
3719 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003721 rex.reg_icombine = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003723 rex.reg_maxcol = 0;
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003724
3725 return bt_regexec_both(line, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726}
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728/*
3729 * Match a regexp against multiple lines.
3730 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3731 * Uses curbuf for line count and 'iskeyword'.
3732 *
3733 * Return zero if there is no match. Return number of lines contained in the
3734 * match otherwise.
3735 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003736 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003737bt_regexec_multi(
3738 regmmatch_T *rmp,
3739 win_T *win, /* window in which to search or NULL */
3740 buf_T *buf, /* buffer in which to search */
3741 linenr_T lnum, /* nr of line to start looking for match */
3742 colnr_T col, /* column to start looking for match */
3743 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744{
Bram Moolenaar6100d022016-10-02 16:51:57 +02003745 rex.reg_match = NULL;
3746 rex.reg_mmatch = rmp;
3747 rex.reg_buf = buf;
3748 rex.reg_win = win;
3749 rex.reg_firstlnum = lnum;
3750 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
3751 rex.reg_line_lbr = FALSE;
3752 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003754 rex.reg_icombine = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003756 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003758 return bt_regexec_both(NULL, col, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759}
3760
3761/*
3762 * Match a regexp against a string ("line" points to the string) or multiple
3763 * lines ("line" is NULL, use reg_getline()).
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003764 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003767bt_regexec_both(
3768 char_u *line,
3769 colnr_T col, /* column to start looking for match */
3770 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771{
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003772 bt_regprog_T *prog;
3773 char_u *s;
3774 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003776 /* Create "regstack" and "backpos" if they are not allocated yet.
3777 * We allocate *_INITIAL amount of bytes first and then set the grow size
3778 * to much bigger value to avoid many malloc calls in case of deep regular
3779 * expressions. */
3780 if (regstack.ga_data == NULL)
3781 {
3782 /* Use an item size of 1 byte, since we push different things
3783 * onto the regstack. */
3784 ga_init2(&regstack, 1, REGSTACK_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003785 (void)ga_grow(&regstack, REGSTACK_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003786 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003789 if (backpos.ga_data == NULL)
3790 {
3791 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003792 (void)ga_grow(&backpos, BACKPOS_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003793 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3794 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003795
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 if (REG_MULTI)
3797 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003798 prog = (bt_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 line = reg_getline((linenr_T)0);
Bram Moolenaar6100d022016-10-02 16:51:57 +02003800 rex.reg_startpos = rex.reg_mmatch->startpos;
3801 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803 else
3804 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003805 prog = (bt_regprog_T *)rex.reg_match->regprog;
3806 rex.reg_startp = rex.reg_match->startp;
3807 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 }
3809
3810 /* Be paranoid... */
3811 if (prog == NULL || line == NULL)
3812 {
3813 EMSG(_(e_null));
3814 goto theend;
3815 }
3816
3817 /* Check validity of program. */
3818 if (prog_magic_wrong())
3819 goto theend;
3820
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003821 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003822 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003823 goto theend;
3824
Bram Moolenaar6100d022016-10-02 16:51:57 +02003825 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003827 rex.reg_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003829 rex.reg_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830
3831#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003832 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003834 rex.reg_icombine = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835#endif
3836
3837 /* If there is a "must appear" string, look for it. */
3838 if (prog->regmust != NULL)
3839 {
3840 int c;
3841
3842#ifdef FEAT_MBYTE
3843 if (has_mbyte)
3844 c = (*mb_ptr2char)(prog->regmust);
3845 else
3846#endif
3847 c = *prog->regmust;
3848 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003849
3850 /*
3851 * This is used very often, esp. for ":global". Use three versions of
3852 * the loop to avoid overhead of conditions.
3853 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003854 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003855#ifdef FEAT_MBYTE
3856 && !has_mbyte
3857#endif
3858 )
3859 while ((s = vim_strbyte(s, c)) != NULL)
3860 {
3861 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3862 break; /* Found it. */
3863 ++s;
3864 }
3865#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003866 else if (!rex.reg_ic || (!enc_utf8 && mb_char2len(c) > 1))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003867 while ((s = vim_strchr(s, c)) != NULL)
3868 {
3869 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3870 break; /* Found it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003871 MB_PTR_ADV(s);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003872 }
3873#endif
3874 else
3875 while ((s = cstrchr(s, c)) != NULL)
3876 {
3877 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3878 break; /* Found it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003879 MB_PTR_ADV(s);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003881 if (s == NULL) /* Not present. */
3882 goto theend;
3883 }
3884
3885 regline = line;
3886 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003887 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888
3889 /* Simplest case: Anchored match need be tried only once. */
3890 if (prog->reganch)
3891 {
3892 int c;
3893
3894#ifdef FEAT_MBYTE
3895 if (has_mbyte)
3896 c = (*mb_ptr2char)(regline + col);
3897 else
3898#endif
3899 c = regline[col];
3900 if (prog->regstart == NUL
3901 || prog->regstart == c
Bram Moolenaar6100d022016-10-02 16:51:57 +02003902 || (rex.reg_ic && ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003903#ifdef FEAT_MBYTE
3904 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3905 || (c < 255 && prog->regstart < 255 &&
3906#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003907 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 retval = regtry(prog, col);
3909 else
3910 retval = 0;
3911 }
3912 else
3913 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003914#ifdef FEAT_RELTIME
3915 int tm_count = 0;
3916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003918 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 {
3920 if (prog->regstart != NUL)
3921 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003922 /* Skip until the char we know it must start with.
3923 * Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003924 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003925#ifdef FEAT_MBYTE
3926 && !has_mbyte
3927#endif
3928 )
3929 s = vim_strbyte(regline + col, prog->regstart);
3930 else
3931 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (s == NULL)
3933 {
3934 retval = 0;
3935 break;
3936 }
3937 col = (int)(s - regline);
3938 }
3939
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003940 /* Check for maximum column to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003941 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003942 {
3943 retval = 0;
3944 break;
3945 }
3946
Bram Moolenaar071d4272004-06-13 20:20:40 +00003947 retval = regtry(prog, col);
3948 if (retval > 0)
3949 break;
3950
3951 /* if not currently on the first line, get it again */
3952 if (reglnum != 0)
3953 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003955 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957 if (regline[col] == NUL)
3958 break;
3959#ifdef FEAT_MBYTE
3960 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003961 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 else
3963#endif
3964 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003965#ifdef FEAT_RELTIME
3966 /* Check for timeout once in a twenty times to avoid overhead. */
3967 if (tm != NULL && ++tm_count == 20)
3968 {
3969 tm_count = 0;
3970 if (profile_passed_limit(tm))
3971 break;
3972 }
3973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 }
3975 }
3976
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003978 /* Free "reg_tofree" when it's a bit big.
3979 * Free regstack and backpos if they are bigger than their initial size. */
3980 if (reg_tofreelen > 400)
3981 {
3982 vim_free(reg_tofree);
3983 reg_tofree = NULL;
3984 }
3985 if (regstack.ga_maxlen > REGSTACK_INITIAL)
3986 ga_clear(&regstack);
3987 if (backpos.ga_maxlen > BACKPOS_INITIAL)
3988 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003989
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 return retval;
3991}
3992
3993#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003994static reg_extmatch_T *make_extmatch(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995
3996/*
3997 * Create a new extmatch and mark it as referenced once.
3998 */
3999 static reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004000make_extmatch(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001{
4002 reg_extmatch_T *em;
4003
4004 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
4005 if (em != NULL)
4006 em->refcnt = 1;
4007 return em;
4008}
4009
4010/*
4011 * Add a reference to an extmatch.
4012 */
4013 reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004014ref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015{
4016 if (em != NULL)
4017 em->refcnt++;
4018 return em;
4019}
4020
4021/*
4022 * Remove a reference to an extmatch. If there are no references left, free
4023 * the info.
4024 */
4025 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004026unref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027{
4028 int i;
4029
4030 if (em != NULL && --em->refcnt <= 0)
4031 {
4032 for (i = 0; i < NSUBEXP; ++i)
4033 vim_free(em->matches[i]);
4034 vim_free(em);
4035 }
4036}
4037#endif
4038
4039/*
4040 * regtry - try match of "prog" with at regline["col"].
4041 * Returns 0 for failure, number of lines contained in the match otherwise.
4042 */
4043 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01004044regtry(bt_regprog_T *prog, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045{
4046 reginput = regline + col;
4047 need_clear_subexpr = TRUE;
4048#ifdef FEAT_SYN_HL
4049 /* Clear the external match subpointers if necessary. */
4050 if (prog->reghasz == REX_SET)
4051 need_clear_zsubexpr = TRUE;
4052#endif
4053
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004054 if (regmatch(prog->program + 1) == 0)
4055 return 0;
4056
4057 cleanup_subexpr();
4058 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004060 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004062 rex.reg_startpos[0].lnum = 0;
4063 rex.reg_startpos[0].col = col;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004064 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004065 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004066 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004067 rex.reg_endpos[0].lnum = reglnum;
4068 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004071 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004072 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004073 }
4074 else
4075 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004076 if (rex.reg_startp[0] == NULL)
4077 rex.reg_startp[0] = regline + col;
4078 if (rex.reg_endp[0] == NULL)
4079 rex.reg_endp[0] = reginput;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004080 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004082 /* Package any found \z(...\) matches for export. Default is none. */
4083 unref_extmatch(re_extmatch_out);
4084 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004085
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004086 if (prog->reghasz == REX_SET)
4087 {
4088 int i;
4089
4090 cleanup_zsubexpr();
4091 re_extmatch_out = make_extmatch();
4092 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004094 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004096 /* Only accept single line matches. */
4097 if (reg_startzpos[i].lnum >= 0
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02004098 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4099 && reg_endzpos[i].col >= reg_startzpos[i].col)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004100 re_extmatch_out->matches[i] =
4101 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004103 reg_endzpos[i].col - reg_startzpos[i].col);
4104 }
4105 else
4106 {
4107 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4108 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004110 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 }
4112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004114#endif
4115 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004116}
4117
4118#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004119static int reg_prev_class(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121/*
4122 * Get class of previous character.
4123 */
4124 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004125reg_prev_class(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126{
4127 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004128 return mb_get_class_buf(reginput - 1
Bram Moolenaar6100d022016-10-02 16:51:57 +02004129 - (*mb_head_off)(regline, reginput - 1), rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 return -1;
4131}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004133
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004134static int reg_match_visual(void);
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004135
4136/*
4137 * Return TRUE if the current reginput position matches the Visual area.
4138 */
4139 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004140reg_match_visual(void)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004141{
4142 pos_T top, bot;
4143 linenr_T lnum;
4144 colnr_T col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004145 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004146 int mode;
4147 colnr_T start, end;
4148 colnr_T start2, end2;
4149 colnr_T cols;
4150
4151 /* Check if the buffer is the current buffer. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004152 if (rex.reg_buf != curbuf || VIsual.lnum == 0)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004153 return FALSE;
4154
4155 if (VIsual_active)
4156 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004157 if (LT_POS(VIsual, wp->w_cursor))
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004158 {
4159 top = VIsual;
4160 bot = wp->w_cursor;
4161 }
4162 else
4163 {
4164 top = wp->w_cursor;
4165 bot = VIsual;
4166 }
4167 mode = VIsual_mode;
4168 }
4169 else
4170 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004171 if (LT_POS(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004172 {
4173 top = curbuf->b_visual.vi_start;
4174 bot = curbuf->b_visual.vi_end;
4175 }
4176 else
4177 {
4178 top = curbuf->b_visual.vi_end;
4179 bot = curbuf->b_visual.vi_start;
4180 }
4181 mode = curbuf->b_visual.vi_mode;
4182 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004183 lnum = reglnum + rex.reg_firstlnum;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004184 if (lnum < top.lnum || lnum > bot.lnum)
4185 return FALSE;
4186
4187 if (mode == 'v')
4188 {
4189 col = (colnr_T)(reginput - regline);
4190 if ((lnum == top.lnum && col < top.col)
4191 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e')))
4192 return FALSE;
4193 }
4194 else if (mode == Ctrl_V)
4195 {
4196 getvvcol(wp, &top, &start, NULL, &end);
4197 getvvcol(wp, &bot, &start2, NULL, &end2);
4198 if (start2 < start)
4199 start = start2;
4200 if (end2 > end)
4201 end = end2;
4202 if (top.col == MAXCOL || bot.col == MAXCOL)
4203 end = MAXCOL;
4204 cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline));
4205 if (cols < start || cols > end - (*p_sel == 'e'))
4206 return FALSE;
4207 }
4208 return TRUE;
4209}
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004210
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004211#define ADVANCE_REGINPUT() MB_PTR_ADV(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212
4213/*
4214 * The arguments from BRACE_LIMITS are stored here. They are actually local
4215 * to regmatch(), but they are here to reduce the amount of stack space used
4216 * (it can be called recursively many times).
4217 */
4218static long bl_minval;
4219static long bl_maxval;
4220
4221/*
4222 * regmatch - main matching routine
4223 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004224 * Conceptually the strategy is simple: Check to see whether the current node
4225 * matches, push an item onto the regstack and loop to see whether the rest
4226 * matches, and then act accordingly. In practice we make some effort to
4227 * avoid using the regstack, in particular by going through "ordinary" nodes
4228 * (that don't need to know whether the rest of the match failed) by a nested
4229 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 *
4231 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4232 * the last matched character.
4233 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4234 * undefined state!
4235 */
4236 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004237regmatch(
4238 char_u *scan) /* Current node. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004240 char_u *next; /* Next node. */
4241 int op;
4242 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004243 regitem_T *rp;
4244 int no;
4245 int status; /* one of the RA_ values: */
4246#define RA_FAIL 1 /* something failed, abort */
4247#define RA_CONT 2 /* continue in inner loop */
4248#define RA_BREAK 3 /* break inner loop */
4249#define RA_MATCH 4 /* successful match */
4250#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004252 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004253 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004254 regstack.ga_len = 0;
4255 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004256
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004257 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004258 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004259 */
4260 for (;;)
4261 {
Bram Moolenaar41f12052013-08-25 17:01:42 +02004262 /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
4263 * Allow interrupting them with CTRL-C. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 fast_breakcheck();
4265
4266#ifdef DEBUG
4267 if (scan != NULL && regnarrate)
4268 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004269 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 mch_errmsg("(\n");
4271 }
4272#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004273
4274 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004275 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004276 * regstack.
4277 */
4278 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004280 if (got_int || scan == NULL)
4281 {
4282 status = RA_FAIL;
4283 break;
4284 }
4285 status = RA_CONT;
4286
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287#ifdef DEBUG
4288 if (regnarrate)
4289 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004290 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 mch_errmsg("...\n");
4292# ifdef FEAT_SYN_HL
4293 if (re_extmatch_in != NULL)
4294 {
4295 int i;
4296
4297 mch_errmsg(_("External submatches:\n"));
4298 for (i = 0; i < NSUBEXP; i++)
4299 {
4300 mch_errmsg(" \"");
4301 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004302 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 mch_errmsg("\"\n");
4304 }
4305 }
4306# endif
4307 }
4308#endif
4309 next = regnext(scan);
4310
4311 op = OP(scan);
4312 /* Check for character class with NL added. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004313 if (!rex.reg_line_lbr && WITH_NL(op) && REG_MULTI
4314 && *reginput == NUL && reglnum <= rex.reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 {
4316 reg_nextline();
4317 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004318 else if (rex.reg_line_lbr && WITH_NL(op) && *reginput == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 {
4320 ADVANCE_REGINPUT();
4321 }
4322 else
4323 {
4324 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004325 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326#ifdef FEAT_MBYTE
4327 if (has_mbyte)
4328 c = (*mb_ptr2char)(reginput);
4329 else
4330#endif
4331 c = *reginput;
4332 switch (op)
4333 {
4334 case BOL:
4335 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004336 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 break;
4338
4339 case EOL:
4340 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004341 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 break;
4343
4344 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004345 /* We're not at the beginning of the file when below the first
4346 * line where we started, not at the start of the line or we
4347 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 if (reglnum != 0 || reginput != regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02004349 || (REG_MULTI && rex.reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004350 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 break;
4352
4353 case RE_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004354 if (reglnum != rex.reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 break;
4357
4358 case CURSOR:
4359 /* Check if the buffer is in a window and compare the
Bram Moolenaar6100d022016-10-02 16:51:57 +02004360 * rex.reg_win->w_cursor position to the match position. */
4361 if (rex.reg_win == NULL
4362 || (reglnum + rex.reg_firstlnum
4363 != rex.reg_win->w_cursor.lnum)
4364 || ((colnr_T)(reginput - regline)
4365 != rex.reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 break;
4368
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004369 case RE_MARK:
Bram Moolenaar044aa292013-06-04 21:27:38 +02004370 /* Compare the mark position to the match position. */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004371 {
4372 int mark = OPERAND(scan)[0];
4373 int cmp = OPERAND(scan)[1];
4374 pos_T *pos;
4375
Bram Moolenaar6100d022016-10-02 16:51:57 +02004376 pos = getmark_buf(rex.reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004377 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar044aa292013-06-04 21:27:38 +02004378 || pos->lnum <= 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004379 || (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004380 ? (pos->col == (colnr_T)(reginput - regline)
4381 ? (cmp == '<' || cmp == '>')
4382 : (pos->col < (colnr_T)(reginput - regline)
4383 ? cmp != '>'
4384 : cmp != '<'))
Bram Moolenaar6100d022016-10-02 16:51:57 +02004385 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004386 ? cmp != '>'
4387 : cmp != '<')))
4388 status = RA_NOMATCH;
4389 }
4390 break;
4391
4392 case RE_VISUAL:
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004393 if (!reg_match_visual())
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004394 status = RA_NOMATCH;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004395 break;
4396
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 case RE_LNUM:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004398 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + rex.reg_firstlnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004400 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 break;
4402
4403 case RE_COL:
4404 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004405 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 break;
4407
4408 case RE_VCOL:
4409 if (!re_num_cmp((long_u)win_linetabsize(
Bram Moolenaar6100d022016-10-02 16:51:57 +02004410 rex.reg_win == NULL ? curwin : rex.reg_win,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004412 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 break;
4414
4415 case BOW: /* \<word; reginput points to w */
4416 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004417 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004419 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 {
4421 int this_class;
4422
4423 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004424 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004426 status = RA_NOMATCH; /* not on a word at all */
4427 else if (reg_prev_class() == this_class)
4428 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 }
4430#endif
4431 else
4432 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004433 if (!vim_iswordc_buf(c, rex.reg_buf) || (reginput > regline
4434 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004435 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 }
4437 break;
4438
4439 case EOW: /* word\>; reginput points after d */
4440 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004441 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004443 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
4445 int this_class, prev_class;
4446
4447 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004448 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004450 if (this_class == prev_class
4451 || prev_class == 0 || prev_class == 1)
4452 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004455 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004457 if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
4458 || (reginput[0] != NUL
4459 && vim_iswordc_buf(c, rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004460 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 break; /* Matched with EOW */
4463
4464 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004465 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004467 status = RA_NOMATCH;
4468 else
4469 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 break;
4471
4472 case IDENT:
4473 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004474 status = RA_NOMATCH;
4475 else
4476 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 break;
4478
4479 case SIDENT:
4480 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004481 status = RA_NOMATCH;
4482 else
4483 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 break;
4485
4486 case KWORD:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004487 if (!vim_iswordp_buf(reginput, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004488 status = RA_NOMATCH;
4489 else
4490 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 break;
4492
4493 case SKWORD:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004494 if (VIM_ISDIGIT(*reginput)
4495 || !vim_iswordp_buf(reginput, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004496 status = RA_NOMATCH;
4497 else
4498 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 break;
4500
4501 case FNAME:
4502 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004503 status = RA_NOMATCH;
4504 else
4505 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 break;
4507
4508 case SFNAME:
4509 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004510 status = RA_NOMATCH;
4511 else
4512 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 break;
4514
4515 case PRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004516 if (!vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004517 status = RA_NOMATCH;
4518 else
4519 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 break;
4521
4522 case SPRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004523 if (VIM_ISDIGIT(*reginput) || !vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004524 status = RA_NOMATCH;
4525 else
4526 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 break;
4528
4529 case WHITE:
Bram Moolenaar1c465442017-03-12 20:10:05 +01004530 if (!VIM_ISWHITE(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004531 status = RA_NOMATCH;
4532 else
4533 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 break;
4535
4536 case NWHITE:
Bram Moolenaar1c465442017-03-12 20:10:05 +01004537 if (c == NUL || VIM_ISWHITE(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004538 status = RA_NOMATCH;
4539 else
4540 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 break;
4542
4543 case DIGIT:
4544 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004545 status = RA_NOMATCH;
4546 else
4547 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 break;
4549
4550 case NDIGIT:
4551 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004552 status = RA_NOMATCH;
4553 else
4554 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 break;
4556
4557 case HEX:
4558 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004559 status = RA_NOMATCH;
4560 else
4561 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 break;
4563
4564 case NHEX:
4565 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004566 status = RA_NOMATCH;
4567 else
4568 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 break;
4570
4571 case OCTAL:
4572 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004573 status = RA_NOMATCH;
4574 else
4575 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 break;
4577
4578 case NOCTAL:
4579 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004580 status = RA_NOMATCH;
4581 else
4582 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 break;
4584
4585 case WORD:
4586 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004587 status = RA_NOMATCH;
4588 else
4589 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 break;
4591
4592 case NWORD:
4593 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004594 status = RA_NOMATCH;
4595 else
4596 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 break;
4598
4599 case HEAD:
4600 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004601 status = RA_NOMATCH;
4602 else
4603 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 break;
4605
4606 case NHEAD:
4607 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004608 status = RA_NOMATCH;
4609 else
4610 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 break;
4612
4613 case ALPHA:
4614 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004615 status = RA_NOMATCH;
4616 else
4617 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 break;
4619
4620 case NALPHA:
4621 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004622 status = RA_NOMATCH;
4623 else
4624 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 break;
4626
4627 case LOWER:
4628 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004629 status = RA_NOMATCH;
4630 else
4631 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 break;
4633
4634 case NLOWER:
4635 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004636 status = RA_NOMATCH;
4637 else
4638 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 break;
4640
4641 case UPPER:
4642 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004643 status = RA_NOMATCH;
4644 else
4645 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 break;
4647
4648 case NUPPER:
4649 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004650 status = RA_NOMATCH;
4651 else
4652 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653 break;
4654
4655 case EXACTLY:
4656 {
4657 int len;
4658 char_u *opnd;
4659
4660 opnd = OPERAND(scan);
4661 /* Inline the first byte, for speed. */
4662 if (*opnd != *reginput
Bram Moolenaar6100d022016-10-02 16:51:57 +02004663 && (!rex.reg_ic || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664#ifdef FEAT_MBYTE
4665 !enc_utf8 &&
4666#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004667 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004668 status = RA_NOMATCH;
4669 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 {
4671 /* match empty string always works; happens when "~" is
4672 * empty. */
4673 }
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004674 else
4675 {
4676 if (opnd[1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02004678 && !(enc_utf8 && rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679#endif
4680 )
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004681 {
4682 len = 1; /* matched a single byte above */
4683 }
4684 else
4685 {
4686 /* Need to match first byte again for multi-byte. */
4687 len = (int)STRLEN(opnd);
4688 if (cstrncmp(opnd, reginput, &len) != 0)
4689 status = RA_NOMATCH;
4690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004692 /* Check for following composing character, unless %C
4693 * follows (skips over all composing chars). */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004694 if (status != RA_NOMATCH
4695 && enc_utf8
4696 && UTF_COMPOSINGLIKE(reginput, reginput + len)
Bram Moolenaar6100d022016-10-02 16:51:57 +02004697 && !rex.reg_icombine
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004698 && OP(next) != RE_COMPOSING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 {
4700 /* raaron: This code makes a composing character get
4701 * ignored, which is the correct behavior (sometimes)
4702 * for voweled Hebrew texts. */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004703 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705#endif
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004706 if (status != RA_NOMATCH)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004707 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709 }
4710 break;
4711
4712 case ANYOF:
4713 case ANYBUT:
4714 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004715 status = RA_NOMATCH;
4716 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4717 status = RA_NOMATCH;
4718 else
4719 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720 break;
4721
4722#ifdef FEAT_MBYTE
4723 case MULTIBYTECODE:
4724 if (has_mbyte)
4725 {
4726 int i, len;
4727 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004728 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729
4730 opnd = OPERAND(scan);
4731 /* Safety check (just in case 'encoding' was changed since
4732 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004733 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004734 {
4735 status = RA_NOMATCH;
4736 break;
4737 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004738 if (enc_utf8)
Bram Moolenaarace95982017-03-29 17:30:27 +02004739 opndc = utf_ptr2char(opnd);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004740 if (enc_utf8 && utf_iscomposing(opndc))
4741 {
4742 /* When only a composing char is given match at any
4743 * position where that composing char appears. */
4744 status = RA_NOMATCH;
Bram Moolenaar0e462412015-03-31 14:17:31 +02004745 for (i = 0; reginput[i] != NUL;
4746 i += utf_ptr2len(reginput + i))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004747 {
Bram Moolenaarace95982017-03-29 17:30:27 +02004748 inpc = utf_ptr2char(reginput + i);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004749 if (!utf_iscomposing(inpc))
4750 {
4751 if (i > 0)
4752 break;
4753 }
4754 else if (opndc == inpc)
4755 {
4756 /* Include all following composing chars. */
Bram Moolenaarace95982017-03-29 17:30:27 +02004757 len = i + utfc_ptr2len(reginput + i);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004758 status = RA_MATCH;
4759 break;
4760 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004761 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004762 }
4763 else
4764 for (i = 0; i < len; ++i)
4765 if (opnd[i] != reginput[i])
4766 {
4767 status = RA_NOMATCH;
4768 break;
4769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 reginput += len;
4771 }
4772 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004773 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 break;
4775#endif
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004776 case RE_COMPOSING:
4777#ifdef FEAT_MBYTE
4778 if (enc_utf8)
4779 {
4780 /* Skip composing characters. */
4781 while (utf_iscomposing(utf_ptr2char(reginput)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004782 MB_CPTR_ADV(reginput);
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004783 }
4784#endif
4785 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786
4787 case NOTHING:
4788 break;
4789
4790 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004791 {
4792 int i;
4793 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794
Bram Moolenaar582fd852005-03-28 20:58:01 +00004795 /*
4796 * When we run into BACK we need to check if we don't keep
4797 * looping without matching any input. The second and later
4798 * times a BACK is encountered it fails if the input is still
4799 * at the same position as the previous time.
4800 * The positions are stored in "backpos" and found by the
4801 * current value of "scan", the position in the RE program.
4802 */
4803 bp = (backpos_T *)backpos.ga_data;
4804 for (i = 0; i < backpos.ga_len; ++i)
4805 if (bp[i].bp_scan == scan)
4806 break;
4807 if (i == backpos.ga_len)
4808 {
4809 /* First time at this BACK, make room to store the pos. */
4810 if (ga_grow(&backpos, 1) == FAIL)
4811 status = RA_FAIL;
4812 else
4813 {
4814 /* get "ga_data" again, it may have changed */
4815 bp = (backpos_T *)backpos.ga_data;
4816 bp[i].bp_scan = scan;
4817 ++backpos.ga_len;
4818 }
4819 }
4820 else if (reg_save_equal(&bp[i].bp_pos))
4821 /* Still at same position as last time, fail. */
4822 status = RA_NOMATCH;
4823
4824 if (status != RA_FAIL && status != RA_NOMATCH)
4825 reg_save(&bp[i].bp_pos, &backpos);
4826 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004827 break;
4828
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 case MOPEN + 0: /* Match start: \zs */
4830 case MOPEN + 1: /* \( */
4831 case MOPEN + 2:
4832 case MOPEN + 3:
4833 case MOPEN + 4:
4834 case MOPEN + 5:
4835 case MOPEN + 6:
4836 case MOPEN + 7:
4837 case MOPEN + 8:
4838 case MOPEN + 9:
4839 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 no = op - MOPEN;
4841 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004842 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004843 if (rp == NULL)
4844 status = RA_FAIL;
4845 else
4846 {
4847 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004848 save_se(&rp->rs_un.sesave, &rex.reg_startpos[no],
4849 &rex.reg_startp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004850 /* We simply continue and handle the result when done. */
4851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004853 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854
4855 case NOPEN: /* \%( */
4856 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004857 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004858 status = RA_FAIL;
4859 /* We simply continue and handle the result when done. */
4860 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861
4862#ifdef FEAT_SYN_HL
4863 case ZOPEN + 1:
4864 case ZOPEN + 2:
4865 case ZOPEN + 3:
4866 case ZOPEN + 4:
4867 case ZOPEN + 5:
4868 case ZOPEN + 6:
4869 case ZOPEN + 7:
4870 case ZOPEN + 8:
4871 case ZOPEN + 9:
4872 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 no = op - ZOPEN;
4874 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004875 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004876 if (rp == NULL)
4877 status = RA_FAIL;
4878 else
4879 {
4880 rp->rs_no = no;
4881 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4882 &reg_startzp[no]);
4883 /* We simply continue and handle the result when done. */
4884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004886 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#endif
4888
4889 case MCLOSE + 0: /* Match end: \ze */
4890 case MCLOSE + 1: /* \) */
4891 case MCLOSE + 2:
4892 case MCLOSE + 3:
4893 case MCLOSE + 4:
4894 case MCLOSE + 5:
4895 case MCLOSE + 6:
4896 case MCLOSE + 7:
4897 case MCLOSE + 8:
4898 case MCLOSE + 9:
4899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 no = op - MCLOSE;
4901 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004902 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004903 if (rp == NULL)
4904 status = RA_FAIL;
4905 else
4906 {
4907 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004908 save_se(&rp->rs_un.sesave, &rex.reg_endpos[no],
4909 &rex.reg_endp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004910 /* We simply continue and handle the result when done. */
4911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004913 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914
4915#ifdef FEAT_SYN_HL
4916 case ZCLOSE + 1: /* \) after \z( */
4917 case ZCLOSE + 2:
4918 case ZCLOSE + 3:
4919 case ZCLOSE + 4:
4920 case ZCLOSE + 5:
4921 case ZCLOSE + 6:
4922 case ZCLOSE + 7:
4923 case ZCLOSE + 8:
4924 case ZCLOSE + 9:
4925 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 no = op - ZCLOSE;
4927 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004928 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004929 if (rp == NULL)
4930 status = RA_FAIL;
4931 else
4932 {
4933 rp->rs_no = no;
4934 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4935 &reg_endzp[no]);
4936 /* We simply continue and handle the result when done. */
4937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004939 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940#endif
4941
4942 case BACKREF + 1:
4943 case BACKREF + 2:
4944 case BACKREF + 3:
4945 case BACKREF + 4:
4946 case BACKREF + 5:
4947 case BACKREF + 6:
4948 case BACKREF + 7:
4949 case BACKREF + 8:
4950 case BACKREF + 9:
4951 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 no = op - BACKREF;
4955 cleanup_subexpr();
4956 if (!REG_MULTI) /* Single-line regexp */
4957 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004958 if (rex.reg_startp[no] == NULL || rex.reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 {
4960 /* Backref was not set: Match an empty string. */
4961 len = 0;
4962 }
4963 else
4964 {
4965 /* Compare current input with back-ref in the same
4966 * line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004967 len = (int)(rex.reg_endp[no] - rex.reg_startp[no]);
4968 if (cstrncmp(rex.reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004969 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
4971 }
4972 else /* Multi-line regexp */
4973 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004974 if (rex.reg_startpos[no].lnum < 0
4975 || rex.reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 /* Backref was not set: Match an empty string. */
4978 len = 0;
4979 }
4980 else
4981 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004982 if (rex.reg_startpos[no].lnum == reglnum
4983 && rex.reg_endpos[no].lnum == reglnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
4985 /* Compare back-ref within the current line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004986 len = rex.reg_endpos[no].col
4987 - rex.reg_startpos[no].col;
4988 if (cstrncmp(regline + rex.reg_startpos[no].col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004990 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
4993 {
4994 /* Messy situation: Need to compare between two
4995 * lines. */
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02004996 int r = match_with_backref(
Bram Moolenaar6100d022016-10-02 16:51:57 +02004997 rex.reg_startpos[no].lnum,
4998 rex.reg_startpos[no].col,
4999 rex.reg_endpos[no].lnum,
5000 rex.reg_endpos[no].col,
Bram Moolenaar4cff8fa2013-06-14 22:48:54 +02005001 &len);
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02005002
5003 if (r != RA_MATCH)
5004 status = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
5006 }
5007 }
5008
5009 /* Matched the backref, skip over it. */
5010 reginput += len;
5011 }
5012 break;
5013
5014#ifdef FEAT_SYN_HL
5015 case ZREF + 1:
5016 case ZREF + 2:
5017 case ZREF + 3:
5018 case ZREF + 4:
5019 case ZREF + 5:
5020 case ZREF + 6:
5021 case ZREF + 7:
5022 case ZREF + 8:
5023 case ZREF + 9:
5024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 int len;
5026
5027 cleanup_zsubexpr();
5028 no = op - ZREF;
5029 if (re_extmatch_in != NULL
5030 && re_extmatch_in->matches[no] != NULL)
5031 {
5032 len = (int)STRLEN(re_extmatch_in->matches[no]);
5033 if (cstrncmp(re_extmatch_in->matches[no],
5034 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005035 status = RA_NOMATCH;
5036 else
5037 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039 else
5040 {
5041 /* Backref was not set: Match an empty string. */
5042 }
5043 }
5044 break;
5045#endif
5046
5047 case BRANCH:
5048 {
5049 if (OP(next) != BRANCH) /* No choice. */
5050 next = OPERAND(scan); /* Avoid recursion. */
5051 else
5052 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005053 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005054 if (rp == NULL)
5055 status = RA_FAIL;
5056 else
5057 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 }
5059 }
5060 break;
5061
5062 case BRACE_LIMITS:
5063 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 if (OP(next) == BRACE_SIMPLE)
5065 {
5066 bl_minval = OPERAND_MIN(scan);
5067 bl_maxval = OPERAND_MAX(scan);
5068 }
5069 else if (OP(next) >= BRACE_COMPLEX
5070 && OP(next) < BRACE_COMPLEX + 10)
5071 {
5072 no = OP(next) - BRACE_COMPLEX;
5073 brace_min[no] = OPERAND_MIN(scan);
5074 brace_max[no] = OPERAND_MAX(scan);
5075 brace_count[no] = 0;
5076 }
5077 else
5078 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005079 internal_error("BRACE_LIMITS");
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005080 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 }
5082 }
5083 break;
5084
5085 case BRACE_COMPLEX + 0:
5086 case BRACE_COMPLEX + 1:
5087 case BRACE_COMPLEX + 2:
5088 case BRACE_COMPLEX + 3:
5089 case BRACE_COMPLEX + 4:
5090 case BRACE_COMPLEX + 5:
5091 case BRACE_COMPLEX + 6:
5092 case BRACE_COMPLEX + 7:
5093 case BRACE_COMPLEX + 8:
5094 case BRACE_COMPLEX + 9:
5095 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 no = op - BRACE_COMPLEX;
5097 ++brace_count[no];
5098
5099 /* If not matched enough times yet, try one more */
5100 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005101 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005103 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005104 if (rp == NULL)
5105 status = RA_FAIL;
5106 else
5107 {
5108 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005109 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005110 next = OPERAND(scan);
5111 /* We continue and handle the result when done. */
5112 }
5113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
5115
5116 /* If matched enough times, may try matching some more */
5117 if (brace_min[no] <= brace_max[no])
5118 {
5119 /* Range is the normal way around, use longest match */
5120 if (brace_count[no] <= brace_max[no])
5121 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005122 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005123 if (rp == NULL)
5124 status = RA_FAIL;
5125 else
5126 {
5127 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005128 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005129 next = OPERAND(scan);
5130 /* We continue and handle the result when done. */
5131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 }
5133 }
5134 else
5135 {
5136 /* Range is backwards, use shortest match first */
5137 if (brace_count[no] <= brace_min[no])
5138 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005139 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005140 if (rp == NULL)
5141 status = RA_FAIL;
5142 else
5143 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005144 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005145 /* We continue and handle the result when done. */
5146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148 }
5149 }
5150 break;
5151
5152 case BRACE_SIMPLE:
5153 case STAR:
5154 case PLUS:
5155 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005156 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 /*
5159 * Lookahead to avoid useless match attempts when we know
5160 * what character comes next.
5161 */
5162 if (OP(next) == EXACTLY)
5163 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005164 rst.nextb = *OPERAND(next);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005165 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005167 if (MB_ISUPPER(rst.nextb))
5168 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005170 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 }
5172 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005173 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175 else
5176 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005177 rst.nextb = NUL;
5178 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
5180 if (op != BRACE_SIMPLE)
5181 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005182 rst.minval = (op == STAR) ? 0 : 1;
5183 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
5185 else
5186 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005187 rst.minval = bl_minval;
5188 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
5190
5191 /*
5192 * When maxval > minval, try matching as much as possible, up
5193 * to maxval. When maxval < minval, try matching at least the
5194 * minimal number (since the range is backwards, that's also
5195 * maxval!).
5196 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005197 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005200 status = RA_FAIL;
5201 break;
5202 }
5203 if (rst.minval <= rst.maxval
5204 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5205 {
5206 /* It could match. Prepare for trying to match what
5207 * follows. The code is below. Parameters are stored in
5208 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005209 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005210 {
5211 EMSG(_(e_maxmempat));
5212 status = RA_FAIL;
5213 }
5214 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005215 status = RA_FAIL;
5216 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005218 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005219 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005220 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005221 if (rp == NULL)
5222 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005224 {
5225 *(((regstar_T *)rp) - 1) = rst;
5226 status = RA_BREAK; /* skip the restore bits */
5227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 }
5230 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005231 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 }
5234 break;
5235
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005236 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 case MATCH:
5238 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005239 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005240 if (rp == NULL)
5241 status = RA_FAIL;
5242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005244 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005245 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005246 next = OPERAND(scan);
5247 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 }
5249 break;
5250
5251 case BEHIND:
5252 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005253 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005254 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005255 {
5256 EMSG(_(e_maxmempat));
5257 status = RA_FAIL;
5258 }
5259 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005260 status = RA_FAIL;
5261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005263 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005264 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005265 if (rp == NULL)
5266 status = RA_FAIL;
5267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005269 /* Need to save the subexpr to be able to restore them
5270 * when there is a match but we don't use it. */
5271 save_subexpr(((regbehind_T *)rp) - 1);
5272
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005273 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005274 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005275 /* First try if what follows matches. If it does then we
5276 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005279 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
5281 case BHPOS:
5282 if (REG_MULTI)
5283 {
5284 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5285 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005286 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 }
5288 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005289 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 break;
5291
5292 case NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02005293 if ((c != NUL || !REG_MULTI || reglnum > rex.reg_maxline
5294 || rex.reg_line_lbr)
5295 && (c != '\n' || !rex.reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005296 status = RA_NOMATCH;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005297 else if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 ADVANCE_REGINPUT();
5299 else
5300 reg_nextline();
5301 break;
5302
5303 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005304 status = RA_MATCH; /* Success! */
5305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306
5307 default:
5308 EMSG(_(e_re_corr));
5309#ifdef DEBUG
5310 printf("Illegal op code %d\n", op);
5311#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005312 status = RA_FAIL;
5313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 }
5315 }
5316
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005317 /* If we can't continue sequentially, break the inner loop. */
5318 if (status != RA_CONT)
5319 break;
5320
5321 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005322 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005323
5324 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005327 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005328 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005330 while (regstack.ga_len > 0 && status != RA_FAIL)
5331 {
5332 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5333 switch (rp->rs_state)
5334 {
5335 case RS_NOPEN:
5336 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005337 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005338 break;
5339
5340 case RS_MOPEN:
5341 /* Pop the state. Restore pointers when there is no match. */
5342 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005343 restore_se(&rp->rs_un.sesave, &rex.reg_startpos[rp->rs_no],
5344 &rex.reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005345 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005346 break;
5347
5348#ifdef FEAT_SYN_HL
5349 case RS_ZOPEN:
5350 /* Pop the state. Restore pointers when there is no match. */
5351 if (status == RA_NOMATCH)
5352 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5353 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005354 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005355 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005357
5358 case RS_MCLOSE:
5359 /* Pop the state. Restore pointers when there is no match. */
5360 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005361 restore_se(&rp->rs_un.sesave, &rex.reg_endpos[rp->rs_no],
5362 &rex.reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005363 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005364 break;
5365
5366#ifdef FEAT_SYN_HL
5367 case RS_ZCLOSE:
5368 /* Pop the state. Restore pointers when there is no match. */
5369 if (status == RA_NOMATCH)
5370 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5371 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005372 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005373 break;
5374#endif
5375
5376 case RS_BRANCH:
5377 if (status == RA_MATCH)
5378 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005379 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005380 else
5381 {
5382 if (status != RA_BREAK)
5383 {
5384 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005385 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005386 scan = rp->rs_scan;
5387 }
5388 if (scan == NULL || OP(scan) != BRANCH)
5389 {
5390 /* no more branches, didn't find a match */
5391 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005392 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005393 }
5394 else
5395 {
5396 /* Prepare to try a branch. */
5397 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005398 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005399 scan = OPERAND(scan);
5400 }
5401 }
5402 break;
5403
5404 case RS_BRCPLX_MORE:
5405 /* Pop the state. Restore pointers when there is no match. */
5406 if (status == RA_NOMATCH)
5407 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005408 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005409 --brace_count[rp->rs_no]; /* decrement match count */
5410 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005411 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005412 break;
5413
5414 case RS_BRCPLX_LONG:
5415 /* Pop the state. Restore pointers when there is no match. */
5416 if (status == RA_NOMATCH)
5417 {
5418 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005419 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005420 --brace_count[rp->rs_no];
5421 /* continue with the items after "\{}" */
5422 status = RA_CONT;
5423 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005424 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005425 if (status == RA_CONT)
5426 scan = regnext(scan);
5427 break;
5428
5429 case RS_BRCPLX_SHORT:
5430 /* Pop the state. Restore pointers when there is no match. */
5431 if (status == RA_NOMATCH)
5432 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005433 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005434 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005435 if (status == RA_NOMATCH)
5436 {
5437 scan = OPERAND(scan);
5438 status = RA_CONT;
5439 }
5440 break;
5441
5442 case RS_NOMATCH:
5443 /* Pop the state. If the operand matches for NOMATCH or
5444 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5445 * except for SUBPAT, and continue with the next item. */
5446 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5447 status = RA_NOMATCH;
5448 else
5449 {
5450 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005451 if (rp->rs_no != SUBPAT) /* zero-width */
5452 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005453 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005454 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005455 if (status == RA_CONT)
5456 scan = regnext(scan);
5457 break;
5458
5459 case RS_BEHIND1:
5460 if (status == RA_NOMATCH)
5461 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005462 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005463 regstack.ga_len -= sizeof(regbehind_T);
5464 }
5465 else
5466 {
5467 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5468 * the behind part does (not) match before the current
5469 * position in the input. This must be done at every
5470 * position in the input and checking if the match ends at
5471 * the current position. */
5472
5473 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005474 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005475
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005476 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005477 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005478 * result, hitting the start of the line or the previous
5479 * line (for multi-line matching).
5480 * Set behind_pos to where the match should end, BHPOS
5481 * will match it. Save the current value. */
5482 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5483 behind_pos = rp->rs_un.regsave;
5484
5485 rp->rs_state = RS_BEHIND2;
5486
Bram Moolenaar582fd852005-03-28 20:58:01 +00005487 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005488 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005489 }
5490 break;
5491
5492 case RS_BEHIND2:
5493 /*
5494 * Looping for BEHIND / NOBEHIND match.
5495 */
5496 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5497 {
5498 /* found a match that ends where "next" started */
5499 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5500 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005501 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5502 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005503 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005504 {
5505 /* But we didn't want a match. Need to restore the
5506 * subexpr, because what follows matched, so they have
5507 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005508 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005509 restore_subexpr(((regbehind_T *)rp) - 1);
5510 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005511 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005512 regstack.ga_len -= sizeof(regbehind_T);
5513 }
5514 else
5515 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005516 long limit;
5517
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005518 /* No match or a match that doesn't end where we want it: Go
5519 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005520 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005521 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005522 if (REG_MULTI)
5523 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005524 if (limit > 0
5525 && ((rp->rs_un.regsave.rs_u.pos.lnum
5526 < behind_pos.rs_u.pos.lnum
5527 ? (colnr_T)STRLEN(regline)
5528 : behind_pos.rs_u.pos.col)
5529 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5530 no = FAIL;
5531 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005532 {
5533 if (rp->rs_un.regsave.rs_u.pos.lnum
5534 < behind_pos.rs_u.pos.lnum
5535 || reg_getline(
5536 --rp->rs_un.regsave.rs_u.pos.lnum)
5537 == NULL)
5538 no = FAIL;
5539 else
5540 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005541 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005542 rp->rs_un.regsave.rs_u.pos.col =
5543 (colnr_T)STRLEN(regline);
5544 }
5545 }
5546 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005547 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005548#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005549 if (has_mbyte)
5550 rp->rs_un.regsave.rs_u.pos.col -=
5551 (*mb_head_off)(regline, regline
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005552 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005553 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005554#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005555 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005556 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005557 }
5558 else
5559 {
5560 if (rp->rs_un.regsave.rs_u.ptr == regline)
5561 no = FAIL;
5562 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005563 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005564 MB_PTR_BACK(regline, rp->rs_un.regsave.rs_u.ptr);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005565 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5566 - rp->rs_un.regsave.rs_u.ptr) > limit)
5567 no = FAIL;
5568 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005569 }
5570 if (no == OK)
5571 {
5572 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005573 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005574 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005575 if (status == RA_MATCH)
5576 {
5577 /* We did match, so subexpr may have been changed,
5578 * need to restore them for the next try. */
5579 status = RA_NOMATCH;
5580 restore_subexpr(((regbehind_T *)rp) - 1);
5581 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005582 }
5583 else
5584 {
5585 /* Can't advance. For NOBEHIND that's a match. */
5586 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5587 if (rp->rs_no == NOBEHIND)
5588 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005589 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5590 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005591 status = RA_MATCH;
5592 }
5593 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005594 {
5595 /* We do want a proper match. Need to restore the
5596 * subexpr if we had a match, because they may have
5597 * been set. */
5598 if (status == RA_MATCH)
5599 {
5600 status = RA_NOMATCH;
5601 restore_subexpr(((regbehind_T *)rp) - 1);
5602 }
5603 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005604 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005605 regstack.ga_len -= sizeof(regbehind_T);
5606 }
5607 }
5608 break;
5609
5610 case RS_STAR_LONG:
5611 case RS_STAR_SHORT:
5612 {
5613 regstar_T *rst = ((regstar_T *)rp) - 1;
5614
5615 if (status == RA_MATCH)
5616 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005617 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005618 regstack.ga_len -= sizeof(regstar_T);
5619 break;
5620 }
5621
5622 /* Tried once already, restore input pointers. */
5623 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005624 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005625
5626 /* Repeat until we found a position where it could match. */
5627 for (;;)
5628 {
5629 if (status != RA_BREAK)
5630 {
5631 /* Tried first position already, advance. */
5632 if (rp->rs_state == RS_STAR_LONG)
5633 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005634 /* Trying for longest match, but couldn't or
5635 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005636 if (--rst->count < rst->minval)
5637 break;
5638 if (reginput == regline)
5639 {
5640 /* backup to last char of previous line */
5641 --reglnum;
5642 regline = reg_getline(reglnum);
5643 /* Just in case regrepeat() didn't count
5644 * right. */
5645 if (regline == NULL)
5646 break;
5647 reginput = regline + STRLEN(regline);
5648 fast_breakcheck();
5649 }
5650 else
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005651 MB_PTR_BACK(regline, reginput);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005652 }
5653 else
5654 {
5655 /* Range is backwards, use shortest match first.
5656 * Careful: maxval and minval are exchanged!
5657 * Couldn't or didn't match: try advancing one
5658 * char. */
5659 if (rst->count == rst->minval
5660 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5661 break;
5662 ++rst->count;
5663 }
5664 if (got_int)
5665 break;
5666 }
5667 else
5668 status = RA_NOMATCH;
5669
5670 /* If it could match, try it. */
5671 if (rst->nextb == NUL || *reginput == rst->nextb
5672 || *reginput == rst->nextb_ic)
5673 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005674 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005675 scan = regnext(rp->rs_scan);
5676 status = RA_CONT;
5677 break;
5678 }
5679 }
5680 if (status != RA_CONT)
5681 {
5682 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005683 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005684 regstack.ga_len -= sizeof(regstar_T);
5685 status = RA_NOMATCH;
5686 }
5687 }
5688 break;
5689 }
5690
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005691 /* If we want to continue the inner loop or didn't pop a state
5692 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005693 if (status == RA_CONT || rp == (regitem_T *)
5694 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5695 break;
5696 }
5697
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005698 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005699 if (status == RA_CONT)
5700 continue;
5701
5702 /*
5703 * If the regstack is empty or something failed we are done.
5704 */
5705 if (regstack.ga_len == 0 || status == RA_FAIL)
5706 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005707 if (scan == NULL)
5708 {
5709 /*
5710 * We get here only if there's trouble -- normally "case END" is
5711 * the terminating point.
5712 */
5713 EMSG(_(e_re_corr));
5714#ifdef DEBUG
5715 printf("Premature EOL\n");
5716#endif
5717 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005718 if (status == RA_FAIL)
5719 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005720 return (status == RA_MATCH);
5721 }
5722
5723 } /* End of loop until the regstack is empty. */
5724
5725 /* NOTREACHED */
5726}
5727
5728/*
5729 * Push an item onto the regstack.
5730 * Returns pointer to new item. Returns NULL when out of memory.
5731 */
5732 static regitem_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005733regstack_push(regstate_T state, char_u *scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005734{
5735 regitem_T *rp;
5736
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005737 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005738 {
5739 EMSG(_(e_maxmempat));
5740 return NULL;
5741 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005742 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005743 return NULL;
5744
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005745 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005746 rp->rs_state = state;
5747 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005748
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005749 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005750 return rp;
5751}
5752
5753/*
5754 * Pop an item from the regstack.
5755 */
5756 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005757regstack_pop(char_u **scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005758{
5759 regitem_T *rp;
5760
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005761 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005762 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005763
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005764 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005765}
5766
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767/*
5768 * regrepeat - repeatedly match something simple, return how many.
5769 * Advances reginput (and reglnum) to just after the matched chars.
5770 */
5771 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005772regrepeat(
5773 char_u *p,
5774 long maxcount) /* maximum number of matches allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775{
5776 long count = 0;
5777 char_u *scan;
5778 char_u *opnd;
5779 int mask;
5780 int testval = 0;
5781
5782 scan = reginput; /* Make local copy of reginput for speed. */
5783 opnd = OPERAND(p);
5784 switch (OP(p))
5785 {
5786 case ANY:
5787 case ANY + ADD_NL:
5788 while (count < maxcount)
5789 {
5790 /* Matching anything means we continue until end-of-line (or
5791 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5792 while (*scan != NUL && count < maxcount)
5793 {
5794 ++count;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005795 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005797 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5798 || rex.reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800 ++count; /* count the line-break */
5801 reg_nextline();
5802 scan = reginput;
5803 if (got_int)
5804 break;
5805 }
5806 break;
5807
5808 case IDENT:
5809 case IDENT + ADD_NL:
5810 testval = TRUE;
5811 /*FALLTHROUGH*/
5812 case SIDENT:
5813 case SIDENT + ADD_NL:
5814 while (count < maxcount)
5815 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005816 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005818 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
5820 else if (*scan == NUL)
5821 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005822 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5823 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 reg_nextline();
5826 scan = reginput;
5827 if (got_int)
5828 break;
5829 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005830 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 ++scan;
5832 else
5833 break;
5834 ++count;
5835 }
5836 break;
5837
5838 case KWORD:
5839 case KWORD + ADD_NL:
5840 testval = TRUE;
5841 /*FALLTHROUGH*/
5842 case SKWORD:
5843 case SKWORD + ADD_NL:
5844 while (count < maxcount)
5845 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005846 if (vim_iswordp_buf(scan, rex.reg_buf)
Bram Moolenaarf813a182013-01-30 13:59:37 +01005847 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005849 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 }
5851 else if (*scan == NUL)
5852 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005853 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5854 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 break;
5856 reg_nextline();
5857 scan = reginput;
5858 if (got_int)
5859 break;
5860 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005861 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 ++scan;
5863 else
5864 break;
5865 ++count;
5866 }
5867 break;
5868
5869 case FNAME:
5870 case FNAME + ADD_NL:
5871 testval = TRUE;
5872 /*FALLTHROUGH*/
5873 case SFNAME:
5874 case SFNAME + ADD_NL:
5875 while (count < maxcount)
5876 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005877 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005878 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005879 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005880 }
5881 else if (*scan == NUL)
5882 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005883 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5884 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005885 break;
5886 reg_nextline();
5887 scan = reginput;
5888 if (got_int)
5889 break;
5890 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005891 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 ++scan;
5893 else
5894 break;
5895 ++count;
5896 }
5897 break;
5898
5899 case PRINT:
5900 case PRINT + ADD_NL:
5901 testval = TRUE;
5902 /*FALLTHROUGH*/
5903 case SPRINT:
5904 case SPRINT + ADD_NL:
5905 while (count < maxcount)
5906 {
5907 if (*scan == NUL)
5908 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005909 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5910 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 break;
5912 reg_nextline();
5913 scan = reginput;
5914 if (got_int)
5915 break;
5916 }
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005917 else if (vim_isprintc(PTR2CHAR(scan)) == 1
5918 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005919 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005920 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005922 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 ++scan;
5924 else
5925 break;
5926 ++count;
5927 }
5928 break;
5929
5930 case WHITE:
5931 case WHITE + ADD_NL:
5932 testval = mask = RI_WHITE;
5933do_class:
5934 while (count < maxcount)
5935 {
5936#ifdef FEAT_MBYTE
5937 int l;
5938#endif
5939 if (*scan == NUL)
5940 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005941 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5942 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 break;
5944 reg_nextline();
5945 scan = reginput;
5946 if (got_int)
5947 break;
5948 }
5949#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005950 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 {
5952 if (testval != 0)
5953 break;
5954 scan += l;
5955 }
5956#endif
5957 else if ((class_tab[*scan] & mask) == testval)
5958 ++scan;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005959 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 ++scan;
5961 else
5962 break;
5963 ++count;
5964 }
5965 break;
5966
5967 case NWHITE:
5968 case NWHITE + ADD_NL:
5969 mask = RI_WHITE;
5970 goto do_class;
5971 case DIGIT:
5972 case DIGIT + ADD_NL:
5973 testval = mask = RI_DIGIT;
5974 goto do_class;
5975 case NDIGIT:
5976 case NDIGIT + ADD_NL:
5977 mask = RI_DIGIT;
5978 goto do_class;
5979 case HEX:
5980 case HEX + ADD_NL:
5981 testval = mask = RI_HEX;
5982 goto do_class;
5983 case NHEX:
5984 case NHEX + ADD_NL:
5985 mask = RI_HEX;
5986 goto do_class;
5987 case OCTAL:
5988 case OCTAL + ADD_NL:
5989 testval = mask = RI_OCTAL;
5990 goto do_class;
5991 case NOCTAL:
5992 case NOCTAL + ADD_NL:
5993 mask = RI_OCTAL;
5994 goto do_class;
5995 case WORD:
5996 case WORD + ADD_NL:
5997 testval = mask = RI_WORD;
5998 goto do_class;
5999 case NWORD:
6000 case NWORD + ADD_NL:
6001 mask = RI_WORD;
6002 goto do_class;
6003 case HEAD:
6004 case HEAD + ADD_NL:
6005 testval = mask = RI_HEAD;
6006 goto do_class;
6007 case NHEAD:
6008 case NHEAD + ADD_NL:
6009 mask = RI_HEAD;
6010 goto do_class;
6011 case ALPHA:
6012 case ALPHA + ADD_NL:
6013 testval = mask = RI_ALPHA;
6014 goto do_class;
6015 case NALPHA:
6016 case NALPHA + ADD_NL:
6017 mask = RI_ALPHA;
6018 goto do_class;
6019 case LOWER:
6020 case LOWER + ADD_NL:
6021 testval = mask = RI_LOWER;
6022 goto do_class;
6023 case NLOWER:
6024 case NLOWER + ADD_NL:
6025 mask = RI_LOWER;
6026 goto do_class;
6027 case UPPER:
6028 case UPPER + ADD_NL:
6029 testval = mask = RI_UPPER;
6030 goto do_class;
6031 case NUPPER:
6032 case NUPPER + ADD_NL:
6033 mask = RI_UPPER;
6034 goto do_class;
6035
6036 case EXACTLY:
6037 {
6038 int cu, cl;
6039
6040 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006041 * would have been used for it. It does handle single-byte
6042 * characters, such as latin1. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006043 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006045 cu = MB_TOUPPER(*opnd);
6046 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006047 while (count < maxcount && (*scan == cu || *scan == cl))
6048 {
6049 count++;
6050 scan++;
6051 }
6052 }
6053 else
6054 {
6055 cu = *opnd;
6056 while (count < maxcount && *scan == cu)
6057 {
6058 count++;
6059 scan++;
6060 }
6061 }
6062 break;
6063 }
6064
6065#ifdef FEAT_MBYTE
6066 case MULTIBYTECODE:
6067 {
6068 int i, len, cf = 0;
6069
6070 /* Safety check (just in case 'encoding' was changed since
6071 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006072 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006074 if (rex.reg_ic && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 cf = utf_fold(utf_ptr2char(opnd));
Bram Moolenaar069dd082015-05-04 09:56:49 +02006076 while (count < maxcount && (*mb_ptr2len)(scan) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006077 {
6078 for (i = 0; i < len; ++i)
6079 if (opnd[i] != scan[i])
6080 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006081 if (i < len && (!rex.reg_ic || !enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 || utf_fold(utf_ptr2char(scan)) != cf))
6083 break;
6084 scan += len;
6085 ++count;
6086 }
6087 }
6088 }
6089 break;
6090#endif
6091
6092 case ANYOF:
6093 case ANYOF + ADD_NL:
6094 testval = TRUE;
6095 /*FALLTHROUGH*/
6096
6097 case ANYBUT:
6098 case ANYBUT + ADD_NL:
6099 while (count < maxcount)
6100 {
6101#ifdef FEAT_MBYTE
6102 int len;
6103#endif
6104 if (*scan == NUL)
6105 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006106 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
6107 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006108 break;
6109 reg_nextline();
6110 scan = reginput;
6111 if (got_int)
6112 break;
6113 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006114 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 ++scan;
6116#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006117 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006118 {
6119 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6120 break;
6121 scan += len;
6122 }
6123#endif
6124 else
6125 {
6126 if ((cstrchr(opnd, *scan) == NULL) == testval)
6127 break;
6128 ++scan;
6129 }
6130 ++count;
6131 }
6132 break;
6133
6134 case NEWL:
6135 while (count < maxcount
Bram Moolenaar6100d022016-10-02 16:51:57 +02006136 && ((*scan == NUL && reglnum <= rex.reg_maxline
6137 && !rex.reg_line_lbr && REG_MULTI)
6138 || (*scan == '\n' && rex.reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 {
6140 count++;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006141 if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 ADVANCE_REGINPUT();
6143 else
6144 reg_nextline();
6145 scan = reginput;
6146 if (got_int)
6147 break;
6148 }
6149 break;
6150
6151 default: /* Oh dear. Called inappropriately. */
6152 EMSG(_(e_re_corr));
6153#ifdef DEBUG
6154 printf("Called regrepeat with op code %d\n", OP(p));
6155#endif
6156 break;
6157 }
6158
6159 reginput = scan;
6160
6161 return (int)count;
6162}
6163
6164/*
6165 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006166 * Returns NULL when calculating size, when there is no next item and when
6167 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 */
6169 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006170regnext(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006171{
6172 int offset;
6173
Bram Moolenaard3005802009-11-25 17:21:32 +00006174 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006175 return NULL;
6176
6177 offset = NEXT(p);
6178 if (offset == 0)
6179 return NULL;
6180
Bram Moolenaar582fd852005-03-28 20:58:01 +00006181 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006182 return p - offset;
6183 else
6184 return p + offset;
6185}
6186
6187/*
6188 * Check the regexp program for its magic number.
6189 * Return TRUE if it's wrong.
6190 */
6191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006192prog_magic_wrong(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006194 regprog_T *prog;
6195
Bram Moolenaar6100d022016-10-02 16:51:57 +02006196 prog = REG_MULTI ? rex.reg_mmatch->regprog : rex.reg_match->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006197 if (prog->engine == &nfa_regengine)
6198 /* For NFA matcher we don't check the magic */
6199 return FALSE;
6200
6201 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
6203 EMSG(_(e_re_corr));
6204 return TRUE;
6205 }
6206 return FALSE;
6207}
6208
6209/*
6210 * Cleanup the subexpressions, if this wasn't done yet.
6211 * This construction is used to clear the subexpressions only when they are
6212 * used (to increase speed).
6213 */
6214 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006215cleanup_subexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216{
6217 if (need_clear_subexpr)
6218 {
6219 if (REG_MULTI)
6220 {
6221 /* Use 0xff to set lnum to -1 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006222 vim_memset(rex.reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6223 vim_memset(rex.reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006224 }
6225 else
6226 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006227 vim_memset(rex.reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6228 vim_memset(rex.reg_endp, 0, sizeof(char_u *) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006229 }
6230 need_clear_subexpr = FALSE;
6231 }
6232}
6233
6234#ifdef FEAT_SYN_HL
6235 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006236cleanup_zsubexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006237{
6238 if (need_clear_zsubexpr)
6239 {
6240 if (REG_MULTI)
6241 {
6242 /* Use 0xff to set lnum to -1 */
6243 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6244 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6245 }
6246 else
6247 {
6248 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6249 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6250 }
6251 need_clear_zsubexpr = FALSE;
6252 }
6253}
6254#endif
6255
6256/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006257 * Save the current subexpr to "bp", so that they can be restored
6258 * later by restore_subexpr().
6259 */
6260 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006261save_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006262{
6263 int i;
6264
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006265 /* When "need_clear_subexpr" is set we don't need to save the values, only
6266 * remember that this flag needs to be set again when restoring. */
6267 bp->save_need_clear_subexpr = need_clear_subexpr;
6268 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006269 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006270 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006271 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006272 if (REG_MULTI)
6273 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006274 bp->save_start[i].se_u.pos = rex.reg_startpos[i];
6275 bp->save_end[i].se_u.pos = rex.reg_endpos[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006276 }
6277 else
6278 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006279 bp->save_start[i].se_u.ptr = rex.reg_startp[i];
6280 bp->save_end[i].se_u.ptr = rex.reg_endp[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006281 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006282 }
6283 }
6284}
6285
6286/*
6287 * Restore the subexpr from "bp".
6288 */
6289 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006290restore_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006291{
6292 int i;
6293
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006294 /* Only need to restore saved values when they are not to be cleared. */
6295 need_clear_subexpr = bp->save_need_clear_subexpr;
6296 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006297 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006298 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006299 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006300 if (REG_MULTI)
6301 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006302 rex.reg_startpos[i] = bp->save_start[i].se_u.pos;
6303 rex.reg_endpos[i] = bp->save_end[i].se_u.pos;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006304 }
6305 else
6306 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006307 rex.reg_startp[i] = bp->save_start[i].se_u.ptr;
6308 rex.reg_endp[i] = bp->save_end[i].se_u.ptr;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006309 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006310 }
6311 }
6312}
6313
6314/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006315 * Advance reglnum, regline and reginput to the next line.
6316 */
6317 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006318reg_nextline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319{
6320 regline = reg_getline(++reglnum);
6321 reginput = regline;
6322 fast_breakcheck();
6323}
6324
6325/*
6326 * Save the input line and position in a regsave_T.
6327 */
6328 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006329reg_save(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006330{
6331 if (REG_MULTI)
6332 {
6333 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6334 save->rs_u.pos.lnum = reglnum;
6335 }
6336 else
6337 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006338 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006339}
6340
6341/*
6342 * Restore the input line and position from a regsave_T.
6343 */
6344 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006345reg_restore(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006346{
6347 if (REG_MULTI)
6348 {
6349 if (reglnum != save->rs_u.pos.lnum)
6350 {
6351 /* only call reg_getline() when the line number changed to save
6352 * a bit of time */
6353 reglnum = save->rs_u.pos.lnum;
6354 regline = reg_getline(reglnum);
6355 }
6356 reginput = regline + save->rs_u.pos.col;
6357 }
6358 else
6359 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006360 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361}
6362
6363/*
6364 * Return TRUE if current position is equal to saved position.
6365 */
6366 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006367reg_save_equal(regsave_T *save)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006368{
6369 if (REG_MULTI)
6370 return reglnum == save->rs_u.pos.lnum
6371 && reginput == regline + save->rs_u.pos.col;
6372 return reginput == save->rs_u.ptr;
6373}
6374
6375/*
6376 * Tentatively set the sub-expression start to the current position (after
6377 * calling regmatch() they will have changed). Need to save the existing
6378 * values for when there is no match.
6379 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6380 * depending on REG_MULTI.
6381 */
6382 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006383save_se_multi(save_se_T *savep, lpos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006384{
6385 savep->se_u.pos = *posp;
6386 posp->lnum = reglnum;
6387 posp->col = (colnr_T)(reginput - regline);
6388}
6389
6390 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006391save_se_one(save_se_T *savep, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392{
6393 savep->se_u.ptr = *pp;
6394 *pp = reginput;
6395}
6396
6397/*
6398 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6399 */
6400 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006401re_num_cmp(long_u val, char_u *scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402{
6403 long_u n = OPERAND_MIN(scan);
6404
6405 if (OPERAND_CMP(scan) == '>')
6406 return val > n;
6407 if (OPERAND_CMP(scan) == '<')
6408 return val < n;
6409 return val == n;
6410}
6411
Bram Moolenaar580abea2013-06-14 20:31:28 +02006412/*
6413 * Check whether a backreference matches.
6414 * Returns RA_FAIL, RA_NOMATCH or RA_MATCH.
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006415 * If "bytelen" is not NULL, it is set to the byte length of the match in the
6416 * last line.
Bram Moolenaar580abea2013-06-14 20:31:28 +02006417 */
6418 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006419match_with_backref(
6420 linenr_T start_lnum,
6421 colnr_T start_col,
6422 linenr_T end_lnum,
6423 colnr_T end_col,
6424 int *bytelen)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006425{
6426 linenr_T clnum = start_lnum;
6427 colnr_T ccol = start_col;
6428 int len;
6429 char_u *p;
6430
6431 if (bytelen != NULL)
6432 *bytelen = 0;
6433 for (;;)
6434 {
6435 /* Since getting one line may invalidate the other, need to make copy.
6436 * Slow! */
6437 if (regline != reg_tofree)
6438 {
6439 len = (int)STRLEN(regline);
6440 if (reg_tofree == NULL || len >= (int)reg_tofreelen)
6441 {
6442 len += 50; /* get some extra */
6443 vim_free(reg_tofree);
6444 reg_tofree = alloc(len);
6445 if (reg_tofree == NULL)
6446 return RA_FAIL; /* out of memory!*/
6447 reg_tofreelen = len;
6448 }
6449 STRCPY(reg_tofree, regline);
6450 reginput = reg_tofree + (reginput - regline);
6451 regline = reg_tofree;
6452 }
6453
6454 /* Get the line to compare with. */
6455 p = reg_getline(clnum);
6456 if (clnum == end_lnum)
6457 len = end_col - ccol;
6458 else
6459 len = (int)STRLEN(p + ccol);
6460
6461 if (cstrncmp(p + ccol, reginput, &len) != 0)
6462 return RA_NOMATCH; /* doesn't match */
6463 if (bytelen != NULL)
6464 *bytelen += len;
6465 if (clnum == end_lnum)
6466 break; /* match and at end! */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006467 if (reglnum >= rex.reg_maxline)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006468 return RA_NOMATCH; /* text too short */
6469
6470 /* Advance to next line. */
6471 reg_nextline();
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006472 if (bytelen != NULL)
6473 *bytelen = 0;
Bram Moolenaar580abea2013-06-14 20:31:28 +02006474 ++clnum;
6475 ccol = 0;
6476 if (got_int)
6477 return RA_FAIL;
6478 }
6479
6480 /* found a match! Note that regline may now point to a copy of the line,
6481 * that should not matter. */
6482 return RA_MATCH;
6483}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006485#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486
6487/*
6488 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6489 */
6490 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006491regdump(char_u *pattern, bt_regprog_T *r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492{
6493 char_u *s;
6494 int op = EXACTLY; /* Arbitrary non-END op. */
6495 char_u *next;
6496 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006497 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006499#ifdef BT_REGEXP_LOG
6500 f = fopen("bt_regexp_log.log", "a");
6501#else
6502 f = stdout;
6503#endif
6504 if (f == NULL)
6505 return;
6506 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 s = r->program + 1;
6509 /*
6510 * Loop until we find the END that isn't before a referred next (an END
6511 * can also appear in a NOMATCH operand).
6512 */
6513 while (op != END || s <= end)
6514 {
6515 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006516 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 next = regnext(s);
6518 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006519 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006521 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 if (end < next)
6523 end = next;
6524 if (op == BRACE_LIMITS)
6525 {
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006526 /* Two ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006527 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 s += 8;
6529 }
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006530 else if (op == BEHIND || op == NOBEHIND)
6531 {
6532 /* one int */
6533 fprintf(f, " count %ld", OPERAND_MIN(s));
6534 s += 4;
6535 }
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02006536 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
6537 {
6538 /* one int plus comperator */
6539 fprintf(f, " count %ld", OPERAND_MIN(s));
6540 s += 5;
6541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006542 s += 3;
6543 if (op == ANYOF || op == ANYOF + ADD_NL
6544 || op == ANYBUT || op == ANYBUT + ADD_NL
6545 || op == EXACTLY)
6546 {
6547 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006548 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006550 fprintf(f, "%c", *s++);
6551 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 s++;
6553 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006554 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 }
6556
6557 /* Header fields of interest. */
6558 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006559 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006560 ? (char *)transchar(r->regstart)
6561 : "multibyte", r->regstart);
6562 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006563 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006564 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006565 fprintf(f, "must have \"%s\"", r->regmust);
6566 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006568#ifdef BT_REGEXP_LOG
6569 fclose(f);
6570#endif
6571}
6572#endif /* BT_REGEXP_DUMP */
6573
6574#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575/*
6576 * regprop - printable representation of opcode
6577 */
6578 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006579regprop(char_u *op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006581 char *p;
6582 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006584 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006586 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587 {
6588 case BOL:
6589 p = "BOL";
6590 break;
6591 case EOL:
6592 p = "EOL";
6593 break;
6594 case RE_BOF:
6595 p = "BOF";
6596 break;
6597 case RE_EOF:
6598 p = "EOF";
6599 break;
6600 case CURSOR:
6601 p = "CURSOR";
6602 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006603 case RE_VISUAL:
6604 p = "RE_VISUAL";
6605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 case RE_LNUM:
6607 p = "RE_LNUM";
6608 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006609 case RE_MARK:
6610 p = "RE_MARK";
6611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 case RE_COL:
6613 p = "RE_COL";
6614 break;
6615 case RE_VCOL:
6616 p = "RE_VCOL";
6617 break;
6618 case BOW:
6619 p = "BOW";
6620 break;
6621 case EOW:
6622 p = "EOW";
6623 break;
6624 case ANY:
6625 p = "ANY";
6626 break;
6627 case ANY + ADD_NL:
6628 p = "ANY+NL";
6629 break;
6630 case ANYOF:
6631 p = "ANYOF";
6632 break;
6633 case ANYOF + ADD_NL:
6634 p = "ANYOF+NL";
6635 break;
6636 case ANYBUT:
6637 p = "ANYBUT";
6638 break;
6639 case ANYBUT + ADD_NL:
6640 p = "ANYBUT+NL";
6641 break;
6642 case IDENT:
6643 p = "IDENT";
6644 break;
6645 case IDENT + ADD_NL:
6646 p = "IDENT+NL";
6647 break;
6648 case SIDENT:
6649 p = "SIDENT";
6650 break;
6651 case SIDENT + ADD_NL:
6652 p = "SIDENT+NL";
6653 break;
6654 case KWORD:
6655 p = "KWORD";
6656 break;
6657 case KWORD + ADD_NL:
6658 p = "KWORD+NL";
6659 break;
6660 case SKWORD:
6661 p = "SKWORD";
6662 break;
6663 case SKWORD + ADD_NL:
6664 p = "SKWORD+NL";
6665 break;
6666 case FNAME:
6667 p = "FNAME";
6668 break;
6669 case FNAME + ADD_NL:
6670 p = "FNAME+NL";
6671 break;
6672 case SFNAME:
6673 p = "SFNAME";
6674 break;
6675 case SFNAME + ADD_NL:
6676 p = "SFNAME+NL";
6677 break;
6678 case PRINT:
6679 p = "PRINT";
6680 break;
6681 case PRINT + ADD_NL:
6682 p = "PRINT+NL";
6683 break;
6684 case SPRINT:
6685 p = "SPRINT";
6686 break;
6687 case SPRINT + ADD_NL:
6688 p = "SPRINT+NL";
6689 break;
6690 case WHITE:
6691 p = "WHITE";
6692 break;
6693 case WHITE + ADD_NL:
6694 p = "WHITE+NL";
6695 break;
6696 case NWHITE:
6697 p = "NWHITE";
6698 break;
6699 case NWHITE + ADD_NL:
6700 p = "NWHITE+NL";
6701 break;
6702 case DIGIT:
6703 p = "DIGIT";
6704 break;
6705 case DIGIT + ADD_NL:
6706 p = "DIGIT+NL";
6707 break;
6708 case NDIGIT:
6709 p = "NDIGIT";
6710 break;
6711 case NDIGIT + ADD_NL:
6712 p = "NDIGIT+NL";
6713 break;
6714 case HEX:
6715 p = "HEX";
6716 break;
6717 case HEX + ADD_NL:
6718 p = "HEX+NL";
6719 break;
6720 case NHEX:
6721 p = "NHEX";
6722 break;
6723 case NHEX + ADD_NL:
6724 p = "NHEX+NL";
6725 break;
6726 case OCTAL:
6727 p = "OCTAL";
6728 break;
6729 case OCTAL + ADD_NL:
6730 p = "OCTAL+NL";
6731 break;
6732 case NOCTAL:
6733 p = "NOCTAL";
6734 break;
6735 case NOCTAL + ADD_NL:
6736 p = "NOCTAL+NL";
6737 break;
6738 case WORD:
6739 p = "WORD";
6740 break;
6741 case WORD + ADD_NL:
6742 p = "WORD+NL";
6743 break;
6744 case NWORD:
6745 p = "NWORD";
6746 break;
6747 case NWORD + ADD_NL:
6748 p = "NWORD+NL";
6749 break;
6750 case HEAD:
6751 p = "HEAD";
6752 break;
6753 case HEAD + ADD_NL:
6754 p = "HEAD+NL";
6755 break;
6756 case NHEAD:
6757 p = "NHEAD";
6758 break;
6759 case NHEAD + ADD_NL:
6760 p = "NHEAD+NL";
6761 break;
6762 case ALPHA:
6763 p = "ALPHA";
6764 break;
6765 case ALPHA + ADD_NL:
6766 p = "ALPHA+NL";
6767 break;
6768 case NALPHA:
6769 p = "NALPHA";
6770 break;
6771 case NALPHA + ADD_NL:
6772 p = "NALPHA+NL";
6773 break;
6774 case LOWER:
6775 p = "LOWER";
6776 break;
6777 case LOWER + ADD_NL:
6778 p = "LOWER+NL";
6779 break;
6780 case NLOWER:
6781 p = "NLOWER";
6782 break;
6783 case NLOWER + ADD_NL:
6784 p = "NLOWER+NL";
6785 break;
6786 case UPPER:
6787 p = "UPPER";
6788 break;
6789 case UPPER + ADD_NL:
6790 p = "UPPER+NL";
6791 break;
6792 case NUPPER:
6793 p = "NUPPER";
6794 break;
6795 case NUPPER + ADD_NL:
6796 p = "NUPPER+NL";
6797 break;
6798 case BRANCH:
6799 p = "BRANCH";
6800 break;
6801 case EXACTLY:
6802 p = "EXACTLY";
6803 break;
6804 case NOTHING:
6805 p = "NOTHING";
6806 break;
6807 case BACK:
6808 p = "BACK";
6809 break;
6810 case END:
6811 p = "END";
6812 break;
6813 case MOPEN + 0:
6814 p = "MATCH START";
6815 break;
6816 case MOPEN + 1:
6817 case MOPEN + 2:
6818 case MOPEN + 3:
6819 case MOPEN + 4:
6820 case MOPEN + 5:
6821 case MOPEN + 6:
6822 case MOPEN + 7:
6823 case MOPEN + 8:
6824 case MOPEN + 9:
6825 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6826 p = NULL;
6827 break;
6828 case MCLOSE + 0:
6829 p = "MATCH END";
6830 break;
6831 case MCLOSE + 1:
6832 case MCLOSE + 2:
6833 case MCLOSE + 3:
6834 case MCLOSE + 4:
6835 case MCLOSE + 5:
6836 case MCLOSE + 6:
6837 case MCLOSE + 7:
6838 case MCLOSE + 8:
6839 case MCLOSE + 9:
6840 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6841 p = NULL;
6842 break;
6843 case BACKREF + 1:
6844 case BACKREF + 2:
6845 case BACKREF + 3:
6846 case BACKREF + 4:
6847 case BACKREF + 5:
6848 case BACKREF + 6:
6849 case BACKREF + 7:
6850 case BACKREF + 8:
6851 case BACKREF + 9:
6852 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6853 p = NULL;
6854 break;
6855 case NOPEN:
6856 p = "NOPEN";
6857 break;
6858 case NCLOSE:
6859 p = "NCLOSE";
6860 break;
6861#ifdef FEAT_SYN_HL
6862 case ZOPEN + 1:
6863 case ZOPEN + 2:
6864 case ZOPEN + 3:
6865 case ZOPEN + 4:
6866 case ZOPEN + 5:
6867 case ZOPEN + 6:
6868 case ZOPEN + 7:
6869 case ZOPEN + 8:
6870 case ZOPEN + 9:
6871 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6872 p = NULL;
6873 break;
6874 case ZCLOSE + 1:
6875 case ZCLOSE + 2:
6876 case ZCLOSE + 3:
6877 case ZCLOSE + 4:
6878 case ZCLOSE + 5:
6879 case ZCLOSE + 6:
6880 case ZCLOSE + 7:
6881 case ZCLOSE + 8:
6882 case ZCLOSE + 9:
6883 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6884 p = NULL;
6885 break;
6886 case ZREF + 1:
6887 case ZREF + 2:
6888 case ZREF + 3:
6889 case ZREF + 4:
6890 case ZREF + 5:
6891 case ZREF + 6:
6892 case ZREF + 7:
6893 case ZREF + 8:
6894 case ZREF + 9:
6895 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6896 p = NULL;
6897 break;
6898#endif
6899 case STAR:
6900 p = "STAR";
6901 break;
6902 case PLUS:
6903 p = "PLUS";
6904 break;
6905 case NOMATCH:
6906 p = "NOMATCH";
6907 break;
6908 case MATCH:
6909 p = "MATCH";
6910 break;
6911 case BEHIND:
6912 p = "BEHIND";
6913 break;
6914 case NOBEHIND:
6915 p = "NOBEHIND";
6916 break;
6917 case SUBPAT:
6918 p = "SUBPAT";
6919 break;
6920 case BRACE_LIMITS:
6921 p = "BRACE_LIMITS";
6922 break;
6923 case BRACE_SIMPLE:
6924 p = "BRACE_SIMPLE";
6925 break;
6926 case BRACE_COMPLEX + 0:
6927 case BRACE_COMPLEX + 1:
6928 case BRACE_COMPLEX + 2:
6929 case BRACE_COMPLEX + 3:
6930 case BRACE_COMPLEX + 4:
6931 case BRACE_COMPLEX + 5:
6932 case BRACE_COMPLEX + 6:
6933 case BRACE_COMPLEX + 7:
6934 case BRACE_COMPLEX + 8:
6935 case BRACE_COMPLEX + 9:
6936 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6937 p = NULL;
6938 break;
6939#ifdef FEAT_MBYTE
6940 case MULTIBYTECODE:
6941 p = "MULTIBYTECODE";
6942 break;
6943#endif
6944 case NEWL:
6945 p = "NEWL";
6946 break;
6947 default:
6948 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6949 p = NULL;
6950 break;
6951 }
6952 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006953 STRCAT(buf, p);
6954 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006956#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957
Bram Moolenaarfb031402014-09-09 17:18:49 +02006958/*
6959 * Used in a place where no * or \+ can follow.
6960 */
6961 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006962re_mult_next(char *what)
Bram Moolenaarfb031402014-09-09 17:18:49 +02006963{
6964 if (re_multi_type(peekchr()) == MULTI_MULT)
6965 EMSG2_RET_FAIL(_("E888: (NFA regexp) cannot repeat %s"), what);
6966 return OK;
6967}
6968
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006970static void mb_decompose(int c, int *c1, int *c2, int *c3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971
6972typedef struct
6973{
6974 int a, b, c;
6975} decomp_T;
6976
6977
6978/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006979static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980{
6981 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6982 {0x5d0,0,0}, /* 0xfb21 alt alef */
6983 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6984 {0x5d4,0,0}, /* 0xfb23 alt he */
6985 {0x5db,0,0}, /* 0xfb24 alt kaf */
6986 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6987 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6988 {0x5e8,0,0}, /* 0xfb27 alt resh */
6989 {0x5ea,0,0}, /* 0xfb28 alt tav */
6990 {'+', 0, 0}, /* 0xfb29 alt plus */
6991 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6992 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6993 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6994 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6995 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6996 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6997 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6998 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6999 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
7000 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
7001 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
7002 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
7003 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
7004 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
7005 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
7006 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
7007 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
7008 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
7009 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
7010 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
7011 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
7012 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
7013 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
7014 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
7015 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
7016 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
7017 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
7018 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
7019 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7020 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7021 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7022 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7023 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7024 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7025 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7026 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7027 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7028 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7029};
7030
7031 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007032mb_decompose(int c, int *c1, int *c2, int *c3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033{
7034 decomp_T d;
7035
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007036 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 {
7038 d = decomp_table[c - 0xfb20];
7039 *c1 = d.a;
7040 *c2 = d.b;
7041 *c3 = d.c;
7042 }
7043 else
7044 {
7045 *c1 = c;
7046 *c2 = *c3 = 0;
7047 }
7048}
7049#endif
7050
7051/*
Bram Moolenaar6100d022016-10-02 16:51:57 +02007052 * Compare two strings, ignore case if rex.reg_ic set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007053 * Return 0 if strings match, non-zero otherwise.
7054 * Correct the length "*n" when composing characters are ignored.
7055 */
7056 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007057cstrncmp(char_u *s1, char_u *s2, int *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007058{
7059 int result;
7060
Bram Moolenaar6100d022016-10-02 16:51:57 +02007061 if (!rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 result = STRNCMP(s1, s2, *n);
7063 else
7064 result = MB_STRNICMP(s1, s2, *n);
7065
7066#ifdef FEAT_MBYTE
7067 /* if it failed and it's utf8 and we want to combineignore: */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007068 if (result != 0 && enc_utf8 && rex.reg_icombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 {
7070 char_u *str1, *str2;
7071 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 int junk;
7073
7074 /* we have to handle the strcmp ourselves, since it is necessary to
7075 * deal with the composing characters by ignoring them: */
7076 str1 = s1;
7077 str2 = s2;
7078 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007079 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 {
7081 c1 = mb_ptr2char_adv(&str1);
7082 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083
7084 /* decompose the character if necessary, into 'base' characters
7085 * because I don't care about Arabic, I will hard-code the Hebrew
7086 * which I *do* care about! So sue me... */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007087 if (c1 != c2 && (!rex.reg_ic || utf_fold(c1) != utf_fold(c2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 {
7089 /* decomposition necessary? */
7090 mb_decompose(c1, &c11, &junk, &junk);
7091 mb_decompose(c2, &c12, &junk, &junk);
7092 c1 = c11;
7093 c2 = c12;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007094 if (c11 != c12
7095 && (!rex.reg_ic || utf_fold(c11) != utf_fold(c12)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 break;
7097 }
7098 }
7099 result = c2 - c1;
7100 if (result == 0)
7101 *n = (int)(str2 - s2);
7102 }
7103#endif
7104
7105 return result;
7106}
7107
7108/*
7109 * cstrchr: This function is used a lot for simple searches, keep it fast!
7110 */
7111 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007112cstrchr(char_u *s, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113{
7114 char_u *p;
7115 int cc;
7116
Bram Moolenaar6100d022016-10-02 16:51:57 +02007117 if (!rex.reg_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00007118#ifdef FEAT_MBYTE
7119 || (!enc_utf8 && mb_char2len(c) > 1)
7120#endif
7121 )
7122 return vim_strchr(s, c);
7123
7124 /* tolower() and toupper() can be slow, comparing twice should be a lot
7125 * faster (esp. when using MS Visual C++!).
7126 * For UTF-8 need to use folded case. */
7127#ifdef FEAT_MBYTE
7128 if (enc_utf8 && c > 0x80)
7129 cc = utf_fold(c);
7130 else
7131#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007132 if (MB_ISUPPER(c))
7133 cc = MB_TOLOWER(c);
7134 else if (MB_ISLOWER(c))
7135 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136 else
7137 return vim_strchr(s, c);
7138
7139#ifdef FEAT_MBYTE
7140 if (has_mbyte)
7141 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007142 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143 {
7144 if (enc_utf8 && c > 0x80)
7145 {
7146 if (utf_fold(utf_ptr2char(p)) == cc)
7147 return p;
7148 }
7149 else if (*p == c || *p == cc)
7150 return p;
7151 }
7152 }
7153 else
7154#endif
7155 /* Faster version for when there are no multi-byte characters. */
7156 for (p = s; *p != NUL; ++p)
7157 if (*p == c || *p == cc)
7158 return p;
7159
7160 return NULL;
7161}
7162
7163/***************************************************************
7164 * regsub stuff *
7165 ***************************************************************/
7166
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167/*
7168 * We should define ftpr as a pointer to a function returning a pointer to
7169 * a function returning a pointer to a function ...
7170 * This is impossible, so we declare a pointer to a function returning a
7171 * pointer to a function returning void. This should work for all compilers.
7172 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007173typedef void (*(*fptr_T)(int *, int))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007175static fptr_T do_upper(int *, int);
7176static fptr_T do_Upper(int *, int);
7177static fptr_T do_lower(int *, int);
7178static fptr_T do_Lower(int *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007180static int vim_regsub_both(char_u *source, typval_T *expr, char_u *dest, int copy, int magic, int backslash);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007182 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007183do_upper(int *d, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007185 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007187 return (fptr_T)NULL;
7188}
7189
7190 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007191do_Upper(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007192{
7193 *d = MB_TOUPPER(c);
7194
7195 return (fptr_T)do_Upper;
7196}
7197
7198 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007199do_lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007200{
7201 *d = MB_TOLOWER(c);
7202
7203 return (fptr_T)NULL;
7204}
7205
7206 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007207do_Lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007208{
7209 *d = MB_TOLOWER(c);
7210
7211 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007212}
7213
7214/*
7215 * regtilde(): Replace tildes in the pattern by the old pattern.
7216 *
7217 * Short explanation of the tilde: It stands for the previous replacement
7218 * pattern. If that previous pattern also contains a ~ we should go back a
7219 * step further... But we insert the previous pattern into the current one
7220 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007221 * This still does not handle the case where "magic" changes. So require the
7222 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007223 *
7224 * The tildes are parsed once before the first call to vim_regsub().
7225 */
7226 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007227regtilde(char_u *source, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007228{
7229 char_u *newsub = source;
7230 char_u *tmpsub;
7231 char_u *p;
7232 int len;
7233 int prevlen;
7234
7235 for (p = newsub; *p; ++p)
7236 {
7237 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7238 {
7239 if (reg_prev_sub != NULL)
7240 {
7241 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7242 prevlen = (int)STRLEN(reg_prev_sub);
7243 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7244 if (tmpsub != NULL)
7245 {
7246 /* copy prefix */
7247 len = (int)(p - newsub); /* not including ~ */
7248 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007249 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7251 /* copy postfix */
7252 if (!magic)
7253 ++p; /* back off \ */
7254 STRCPY(tmpsub + len + prevlen, p + 1);
7255
7256 if (newsub != source) /* already allocated newsub */
7257 vim_free(newsub);
7258 newsub = tmpsub;
7259 p = newsub + len + prevlen;
7260 }
7261 }
7262 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007263 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007265 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007266 --p;
7267 }
7268 else
7269 {
7270 if (*p == '\\' && p[1]) /* skip escaped characters */
7271 ++p;
7272#ifdef FEAT_MBYTE
7273 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007274 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007275#endif
7276 }
7277 }
7278
7279 vim_free(reg_prev_sub);
7280 if (newsub != source) /* newsub was allocated, just keep it */
7281 reg_prev_sub = newsub;
7282 else /* no ~ found, need to save newsub */
7283 reg_prev_sub = vim_strsave(newsub);
7284 return newsub;
7285}
7286
7287#ifdef FEAT_EVAL
7288static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7289
Bram Moolenaar6100d022016-10-02 16:51:57 +02007290/* These pointers are used for reg_submatch(). Needed for when the
7291 * substitution string is an expression that contains a call to substitute()
7292 * and submatch(). */
7293typedef struct {
7294 regmatch_T *sm_match;
7295 regmmatch_T *sm_mmatch;
7296 linenr_T sm_firstlnum;
7297 linenr_T sm_maxline;
7298 int sm_line_lbr;
7299} regsubmatch_T;
7300
7301static regsubmatch_T rsm; /* can only be used when can_f_submatch is TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302#endif
7303
7304#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007305
7306/*
7307 * Put the submatches in "argv[0]" which is a list passed into call_func() by
7308 * vim_regsub_both().
7309 */
7310 static int
7311fill_submatch_list(int argc UNUSED, typval_T *argv, int argcount)
7312{
7313 listitem_T *li;
7314 int i;
7315 char_u *s;
7316
7317 if (argcount == 0)
7318 /* called function doesn't take an argument */
7319 return 0;
7320
7321 /* Relies on sl_list to be the first item in staticList10_T. */
7322 init_static_list((staticList10_T *)(argv->vval.v_list));
7323
7324 /* There are always 10 list items in staticList10_T. */
7325 li = argv->vval.v_list->lv_first;
7326 for (i = 0; i < 10; ++i)
7327 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007328 s = rsm.sm_match->startp[i];
7329 if (s == NULL || rsm.sm_match->endp[i] == NULL)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007330 s = NULL;
7331 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007332 s = vim_strnsave(s, (int)(rsm.sm_match->endp[i] - s));
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007333 li->li_tv.v_type = VAR_STRING;
7334 li->li_tv.vval.v_string = s;
7335 li = li->li_next;
7336 }
7337 return 1;
7338}
7339
7340 static void
7341clear_submatch_list(staticList10_T *sl)
7342{
7343 int i;
7344
7345 for (i = 0; i < 10; ++i)
7346 vim_free(sl->sl_items[i].li_tv.vval.v_string);
7347}
7348
Bram Moolenaar071d4272004-06-13 20:20:40 +00007349/*
7350 * vim_regsub() - perform substitutions after a vim_regexec() or
7351 * vim_regexec_multi() match.
7352 *
7353 * If "copy" is TRUE really copy into "dest".
7354 * If "copy" is FALSE nothing is copied, this is just to find out the length
7355 * of the result.
7356 *
7357 * If "backslash" is TRUE, a backslash will be removed later, need to double
7358 * them to keep them, and insert a backslash before a CR to avoid it being
7359 * replaced with a line break later.
7360 *
7361 * Note: The matched text must not change between the call of
7362 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7363 * references invalid!
7364 *
7365 * Returns the size of the replacement, including terminating NUL.
7366 */
7367 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007368vim_regsub(
7369 regmatch_T *rmp,
7370 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007371 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007372 char_u *dest,
7373 int copy,
7374 int magic,
7375 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007377 int result;
7378 regexec_T rex_save;
7379 int rex_in_use_save = rex_in_use;
7380
7381 if (rex_in_use)
7382 /* Being called recursively, save the state. */
7383 rex_save = rex;
7384 rex_in_use = TRUE;
7385
7386 rex.reg_match = rmp;
7387 rex.reg_mmatch = NULL;
7388 rex.reg_maxline = 0;
7389 rex.reg_buf = curbuf;
7390 rex.reg_line_lbr = TRUE;
7391 result = vim_regsub_both(source, expr, dest, copy, magic, backslash);
7392
7393 rex_in_use = rex_in_use_save;
7394 if (rex_in_use)
7395 rex = rex_save;
7396
7397 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007398}
7399#endif
7400
7401 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007402vim_regsub_multi(
7403 regmmatch_T *rmp,
7404 linenr_T lnum,
7405 char_u *source,
7406 char_u *dest,
7407 int copy,
7408 int magic,
7409 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007410{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007411 int result;
7412 regexec_T rex_save;
7413 int rex_in_use_save = rex_in_use;
7414
7415 if (rex_in_use)
7416 /* Being called recursively, save the state. */
7417 rex_save = rex;
7418 rex_in_use = TRUE;
7419
7420 rex.reg_match = NULL;
7421 rex.reg_mmatch = rmp;
7422 rex.reg_buf = curbuf; /* always works on the current buffer! */
7423 rex.reg_firstlnum = lnum;
7424 rex.reg_maxline = curbuf->b_ml.ml_line_count - lnum;
7425 rex.reg_line_lbr = FALSE;
7426 result = vim_regsub_both(source, NULL, dest, copy, magic, backslash);
7427
7428 rex_in_use = rex_in_use_save;
7429 if (rex_in_use)
7430 rex = rex_save;
7431
7432 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007433}
7434
7435 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007436vim_regsub_both(
7437 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007438 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007439 char_u *dest,
7440 int copy,
7441 int magic,
7442 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443{
7444 char_u *src;
7445 char_u *dst;
7446 char_u *s;
7447 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007448 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007450 fptr_T func_all = (fptr_T)NULL;
7451 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007452 linenr_T clnum = 0; /* init for GCC */
7453 int len = 0; /* init for GCC */
7454#ifdef FEAT_EVAL
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007455 static char_u *eval_result = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457
7458 /* Be paranoid... */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007459 if ((source == NULL && expr == NULL) || dest == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007460 {
7461 EMSG(_(e_null));
7462 return 0;
7463 }
7464 if (prog_magic_wrong())
7465 return 0;
7466 src = source;
7467 dst = dest;
7468
7469 /*
7470 * When the substitute part starts with "\=" evaluate it as an expression.
7471 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007472 if (expr != NULL || (source[0] == '\\' && source[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007473 {
7474#ifdef FEAT_EVAL
7475 /* To make sure that the length doesn't change between checking the
7476 * length and copying the string, and to speed up things, the
7477 * resulting string is saved from the call with "copy" == FALSE to the
7478 * call with "copy" == TRUE. */
7479 if (copy)
7480 {
7481 if (eval_result != NULL)
7482 {
7483 STRCPY(dest, eval_result);
7484 dst += STRLEN(eval_result);
7485 vim_free(eval_result);
7486 eval_result = NULL;
7487 }
7488 }
7489 else
7490 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007491 int prev_can_f_submatch = can_f_submatch;
7492 regsubmatch_T rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493
7494 vim_free(eval_result);
7495
7496 /* The expression may contain substitute(), which calls us
7497 * recursively. Make sure submatch() gets the text from the first
Bram Moolenaar6100d022016-10-02 16:51:57 +02007498 * level. */
7499 if (can_f_submatch)
7500 rsm_save = rsm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501 can_f_submatch = TRUE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007502 rsm.sm_match = rex.reg_match;
7503 rsm.sm_mmatch = rex.reg_mmatch;
7504 rsm.sm_firstlnum = rex.reg_firstlnum;
7505 rsm.sm_maxline = rex.reg_maxline;
7506 rsm.sm_line_lbr = rex.reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007508 if (expr != NULL)
7509 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007510 typval_T argv[2];
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007511 int dummy;
7512 char_u buf[NUMBUFLEN];
7513 typval_T rettv;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007514 staticList10_T matchList;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007515
7516 rettv.v_type = VAR_STRING;
7517 rettv.vval.v_string = NULL;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007518 argv[0].v_type = VAR_LIST;
7519 argv[0].vval.v_list = &matchList.sl_list;
7520 matchList.sl_list.lv_len = 0;
7521 if (expr->v_type == VAR_FUNC)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007522 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007523 s = expr->vval.v_string;
7524 call_func(s, (int)STRLEN(s), &rettv,
7525 1, argv, fill_submatch_list,
7526 0L, 0L, &dummy, TRUE, NULL, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007527 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007528 else if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007529 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007530 partial_T *partial = expr->vval.v_partial;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007531
Bram Moolenaar6100d022016-10-02 16:51:57 +02007532 s = partial_name(partial);
7533 call_func(s, (int)STRLEN(s), &rettv,
7534 1, argv, fill_submatch_list,
7535 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007536 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007537 if (matchList.sl_list.lv_len > 0)
7538 /* fill_submatch_list() was called */
7539 clear_submatch_list(&matchList);
7540
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007541 eval_result = get_tv_string_buf_chk(&rettv, buf);
7542 if (eval_result != NULL)
7543 eval_result = vim_strsave(eval_result);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007544 clear_tv(&rettv);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007545 }
7546 else
7547 eval_result = eval_to_string(source + 2, NULL, TRUE);
7548
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 if (eval_result != NULL)
7550 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007551 int had_backslash = FALSE;
7552
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007553 for (s = eval_result; *s != NUL; MB_PTR_ADV(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007555 /* Change NL to CR, so that it becomes a line break,
7556 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557 * Skip over a backslashed character. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007558 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007559 *s = CAR;
7560 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007561 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007562 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007563 /* Change NL to CR here too, so that this works:
7564 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7565 * abc\
7566 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007567 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007568 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007569 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007570 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007571 had_backslash = TRUE;
7572 }
7573 }
7574 if (had_backslash && backslash)
7575 {
7576 /* Backslashes will be consumed, need to double them. */
7577 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7578 if (s != NULL)
7579 {
7580 vim_free(eval_result);
7581 eval_result = s;
7582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 }
7584
7585 dst += STRLEN(eval_result);
7586 }
7587
Bram Moolenaar6100d022016-10-02 16:51:57 +02007588 can_f_submatch = prev_can_f_submatch;
7589 if (can_f_submatch)
7590 rsm = rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591 }
7592#endif
7593 }
7594 else
7595 while ((c = *src++) != NUL)
7596 {
7597 if (c == '&' && magic)
7598 no = 0;
7599 else if (c == '\\' && *src != NUL)
7600 {
7601 if (*src == '&' && !magic)
7602 {
7603 ++src;
7604 no = 0;
7605 }
7606 else if ('0' <= *src && *src <= '9')
7607 {
7608 no = *src++ - '0';
7609 }
7610 else if (vim_strchr((char_u *)"uUlLeE", *src))
7611 {
7612 switch (*src++)
7613 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007614 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007616 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007618 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007620 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 continue;
7622 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007623 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 continue;
7625 }
7626 }
7627 }
7628 if (no < 0) /* Ordinary character. */
7629 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007630 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7631 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007632 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007633 if (copy)
7634 {
7635 *dst++ = c;
7636 *dst++ = *src++;
7637 *dst++ = *src++;
7638 }
7639 else
7640 {
7641 dst += 3;
7642 src += 2;
7643 }
7644 continue;
7645 }
7646
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 if (c == '\\' && *src != NUL)
7648 {
7649 /* Check for abbreviations -- webb */
7650 switch (*src)
7651 {
7652 case 'r': c = CAR; ++src; break;
7653 case 'n': c = NL; ++src; break;
7654 case 't': c = TAB; ++src; break;
7655 /* Oh no! \e already has meaning in subst pat :-( */
7656 /* case 'e': c = ESC; ++src; break; */
7657 case 'b': c = Ctrl_H; ++src; break;
7658
7659 /* If "backslash" is TRUE the backslash will be removed
7660 * later. Used to insert a literal CR. */
7661 default: if (backslash)
7662 {
7663 if (copy)
7664 *dst = '\\';
7665 ++dst;
7666 }
7667 c = *src++;
7668 }
7669 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007671 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007672 c = mb_ptr2char(src - 1);
7673#endif
7674
Bram Moolenaardb552d602006-03-23 22:59:57 +00007675 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007676 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007677 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007678 func_one = (fptr_T)(func_one(&cc, c));
7679 else if (func_all != (fptr_T)NULL)
7680 /* Turbo C complains without the typecast */
7681 func_all = (fptr_T)(func_all(&cc, c));
7682 else /* just copy */
7683 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007684
7685#ifdef FEAT_MBYTE
7686 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007687 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007688 int totlen = mb_ptr2len(src - 1);
7689
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007691 mb_char2bytes(cc, dst);
7692 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007693 if (enc_utf8)
7694 {
7695 int clen = utf_ptr2len(src - 1);
7696
7697 /* If the character length is shorter than "totlen", there
7698 * are composing characters; copy them as-is. */
7699 if (clen < totlen)
7700 {
7701 if (copy)
7702 mch_memmove(dst + 1, src - 1 + clen,
7703 (size_t)(totlen - clen));
7704 dst += totlen - clen;
7705 }
7706 }
7707 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007708 }
7709 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710#endif
7711 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007712 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007713 dst++;
7714 }
7715 else
7716 {
7717 if (REG_MULTI)
7718 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007719 clnum = rex.reg_mmatch->startpos[no].lnum;
7720 if (clnum < 0 || rex.reg_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 s = NULL;
7722 else
7723 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007724 s = reg_getline(clnum) + rex.reg_mmatch->startpos[no].col;
7725 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7726 len = rex.reg_mmatch->endpos[no].col
7727 - rex.reg_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 else
7729 len = (int)STRLEN(s);
7730 }
7731 }
7732 else
7733 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007734 s = rex.reg_match->startp[no];
7735 if (rex.reg_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 s = NULL;
7737 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007738 len = (int)(rex.reg_match->endp[no] - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
7740 if (s != NULL)
7741 {
7742 for (;;)
7743 {
7744 if (len == 0)
7745 {
7746 if (REG_MULTI)
7747 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007748 if (rex.reg_mmatch->endpos[no].lnum == clnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 break;
7750 if (copy)
7751 *dst = CAR;
7752 ++dst;
7753 s = reg_getline(++clnum);
Bram Moolenaar6100d022016-10-02 16:51:57 +02007754 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7755 len = rex.reg_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 else
7757 len = (int)STRLEN(s);
7758 }
7759 else
7760 break;
7761 }
7762 else if (*s == NUL) /* we hit NUL. */
7763 {
7764 if (copy)
7765 EMSG(_(e_re_damg));
7766 goto exit;
7767 }
7768 else
7769 {
7770 if (backslash && (*s == CAR || *s == '\\'))
7771 {
7772 /*
7773 * Insert a backslash in front of a CR, otherwise
7774 * it will be replaced by a line break.
7775 * Number of backslashes will be halved later,
7776 * double them here.
7777 */
7778 if (copy)
7779 {
7780 dst[0] = '\\';
7781 dst[1] = *s;
7782 }
7783 dst += 2;
7784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 else
7786 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007787#ifdef FEAT_MBYTE
7788 if (has_mbyte)
7789 c = mb_ptr2char(s);
7790 else
7791#endif
7792 c = *s;
7793
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007794 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007796 func_one = (fptr_T)(func_one(&cc, c));
7797 else if (func_all != (fptr_T)NULL)
7798 /* Turbo C complains without the typecast */
7799 func_all = (fptr_T)(func_all(&cc, c));
7800 else /* just copy */
7801 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007802
7803#ifdef FEAT_MBYTE
7804 if (has_mbyte)
7805 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007806 int l;
7807
7808 /* Copy composing characters separately, one
7809 * at a time. */
7810 if (enc_utf8)
7811 l = utf_ptr2len(s) - 1;
7812 else
7813 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007814
7815 s += l;
7816 len -= l;
7817 if (copy)
7818 mb_char2bytes(cc, dst);
7819 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007821 else
7822#endif
7823 if (copy)
7824 *dst = cc;
7825 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007827
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 ++s;
7829 --len;
7830 }
7831 }
7832 }
7833 no = -1;
7834 }
7835 }
7836 if (copy)
7837 *dst = NUL;
7838
7839exit:
7840 return (int)((dst - dest) + 1);
7841}
7842
7843#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007844static char_u *reg_getline_submatch(linenr_T lnum);
Bram Moolenaard32a3192009-11-26 19:40:49 +00007845
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007847 * Call reg_getline() with the line numbers from the submatch. If a
7848 * substitute() was used the reg_maxline and other values have been
7849 * overwritten.
7850 */
7851 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007852reg_getline_submatch(linenr_T lnum)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007853{
7854 char_u *s;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007855 linenr_T save_first = rex.reg_firstlnum;
7856 linenr_T save_max = rex.reg_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007857
Bram Moolenaar6100d022016-10-02 16:51:57 +02007858 rex.reg_firstlnum = rsm.sm_firstlnum;
7859 rex.reg_maxline = rsm.sm_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007860
7861 s = reg_getline(lnum);
7862
Bram Moolenaar6100d022016-10-02 16:51:57 +02007863 rex.reg_firstlnum = save_first;
7864 rex.reg_maxline = save_max;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007865 return s;
7866}
7867
7868/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007869 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 * allocated memory.
7871 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7872 */
7873 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007874reg_submatch(int no)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875{
7876 char_u *retval = NULL;
7877 char_u *s;
7878 int len;
7879 int round;
7880 linenr_T lnum;
7881
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007882 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007883 return NULL;
7884
Bram Moolenaar6100d022016-10-02 16:51:57 +02007885 if (rsm.sm_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 {
7887 /*
7888 * First round: compute the length and allocate memory.
7889 * Second round: copy the text.
7890 */
7891 for (round = 1; round <= 2; ++round)
7892 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007893 lnum = rsm.sm_mmatch->startpos[no].lnum;
7894 if (lnum < 0 || rsm.sm_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 return NULL;
7896
Bram Moolenaar6100d022016-10-02 16:51:57 +02007897 s = reg_getline_submatch(lnum) + rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007898 if (s == NULL) /* anti-crash check, cannot happen? */
7899 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007900 if (rsm.sm_mmatch->endpos[no].lnum == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 {
7902 /* Within one line: take form start to end col. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007903 len = rsm.sm_mmatch->endpos[no].col
7904 - rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007905 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007906 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 ++len;
7908 }
7909 else
7910 {
7911 /* Multiple lines: take start line from start col, middle
7912 * lines completely and end line up to end col. */
7913 len = (int)STRLEN(s);
7914 if (round == 2)
7915 {
7916 STRCPY(retval, s);
7917 retval[len] = '\n';
7918 }
7919 ++len;
7920 ++lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007921 while (lnum < rsm.sm_mmatch->endpos[no].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007922 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007923 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007924 if (round == 2)
7925 STRCPY(retval + len, s);
7926 len += (int)STRLEN(s);
7927 if (round == 2)
7928 retval[len] = '\n';
7929 ++len;
7930 }
7931 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007932 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar6100d022016-10-02 16:51:57 +02007933 rsm.sm_mmatch->endpos[no].col);
7934 len += rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 if (round == 2)
7936 retval[len] = NUL;
7937 ++len;
7938 }
7939
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007940 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 {
7942 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007943 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007944 return NULL;
7945 }
7946 }
7947 }
7948 else
7949 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007950 s = rsm.sm_match->startp[no];
7951 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 retval = NULL;
7953 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007954 retval = vim_strnsave(s, (int)(rsm.sm_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 }
7956
7957 return retval;
7958}
Bram Moolenaar41571762014-04-02 19:00:58 +02007959
7960/*
7961 * Used for the submatch() function with the optional non-zero argument: get
7962 * the list of strings from the n'th submatch in allocated memory with NULs
7963 * represented in NLs.
7964 * Returns a list of allocated strings. Returns NULL when not in a ":s"
7965 * command, for a non-existing submatch and for any error.
7966 */
7967 list_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007968reg_submatch_list(int no)
Bram Moolenaar41571762014-04-02 19:00:58 +02007969{
7970 char_u *s;
7971 linenr_T slnum;
7972 linenr_T elnum;
7973 colnr_T scol;
7974 colnr_T ecol;
7975 int i;
7976 list_T *list;
7977 int error = FALSE;
7978
7979 if (!can_f_submatch || no < 0)
7980 return NULL;
7981
Bram Moolenaar6100d022016-10-02 16:51:57 +02007982 if (rsm.sm_match == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02007983 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007984 slnum = rsm.sm_mmatch->startpos[no].lnum;
7985 elnum = rsm.sm_mmatch->endpos[no].lnum;
Bram Moolenaar41571762014-04-02 19:00:58 +02007986 if (slnum < 0 || elnum < 0)
7987 return NULL;
7988
Bram Moolenaar6100d022016-10-02 16:51:57 +02007989 scol = rsm.sm_mmatch->startpos[no].col;
7990 ecol = rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar41571762014-04-02 19:00:58 +02007991
7992 list = list_alloc();
7993 if (list == NULL)
7994 return NULL;
7995
7996 s = reg_getline_submatch(slnum) + scol;
7997 if (slnum == elnum)
7998 {
7999 if (list_append_string(list, s, ecol - scol) == FAIL)
8000 error = TRUE;
8001 }
8002 else
8003 {
8004 if (list_append_string(list, s, -1) == FAIL)
8005 error = TRUE;
8006 for (i = 1; i < elnum - slnum; i++)
8007 {
8008 s = reg_getline_submatch(slnum + i);
8009 if (list_append_string(list, s, -1) == FAIL)
8010 error = TRUE;
8011 }
8012 s = reg_getline_submatch(elnum);
8013 if (list_append_string(list, s, ecol) == FAIL)
8014 error = TRUE;
8015 }
8016 }
8017 else
8018 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02008019 s = rsm.sm_match->startp[no];
8020 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008021 return NULL;
8022 list = list_alloc();
8023 if (list == NULL)
8024 return NULL;
8025 if (list_append_string(list, s,
Bram Moolenaar6100d022016-10-02 16:51:57 +02008026 (int)(rsm.sm_match->endp[no] - s)) == FAIL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008027 error = TRUE;
8028 }
8029
8030 if (error)
8031 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008032 list_free(list);
Bram Moolenaar41571762014-04-02 19:00:58 +02008033 return NULL;
8034 }
8035 return list;
8036}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008038
8039static regengine_T bt_regengine =
8040{
8041 bt_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008042 bt_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008043 bt_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008044 bt_regexec_multi,
8045 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008046};
8047
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008048#include "regexp_nfa.c"
8049
8050static regengine_T nfa_regengine =
8051{
8052 nfa_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008053 nfa_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008054 nfa_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008055 nfa_regexec_multi,
8056 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008057};
8058
8059/* Which regexp engine to use? Needed for vim_regcomp().
8060 * Must match with 'regexpengine'. */
8061static int regexp_engine = 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008062
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008063#ifdef DEBUG
8064static char_u regname[][30] = {
8065 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02008066 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008067 "NFA Regexp Engine"
8068 };
8069#endif
8070
8071/*
8072 * Compile a regular expression into internal code.
Bram Moolenaar473de612013-06-08 18:19:48 +02008073 * Returns the program in allocated memory.
8074 * Use vim_regfree() to free the memory.
8075 * Returns NULL for an error.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008076 */
8077 regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01008078vim_regcomp(char_u *expr_arg, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008079{
8080 regprog_T *prog = NULL;
8081 char_u *expr = expr_arg;
8082
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008083 regexp_engine = p_re;
8084
8085 /* Check for prefix "\%#=", that sets the regexp engine */
8086 if (STRNCMP(expr, "\\%#=", 4) == 0)
8087 {
8088 int newengine = expr[4] - '0';
8089
8090 if (newengine == AUTOMATIC_ENGINE
8091 || newengine == BACKTRACKING_ENGINE
8092 || newengine == NFA_ENGINE)
8093 {
8094 regexp_engine = expr[4] - '0';
8095 expr += 5;
8096#ifdef DEBUG
Bram Moolenaar6e132072014-05-13 16:46:32 +02008097 smsg((char_u *)"New regexp mode selected (%d): %s",
8098 regexp_engine, regname[newengine]);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008099#endif
8100 }
8101 else
8102 {
8103 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
8104 regexp_engine = AUTOMATIC_ENGINE;
8105 }
8106 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008107 bt_regengine.expr = expr;
8108 nfa_regengine.expr = expr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008109
8110 /*
8111 * First try the NFA engine, unless backtracking was requested.
8112 */
8113 if (regexp_engine != BACKTRACKING_ENGINE)
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008114 prog = nfa_regengine.regcomp(expr,
8115 re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008116 else
8117 prog = bt_regengine.regcomp(expr, re_flags);
8118
Bram Moolenaarfda37292014-11-05 14:27:36 +01008119 /* Check for error compiling regexp with initial engine. */
8120 if (prog == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008121 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008122#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008123 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
8124 {
8125 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008126 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008127 if (f)
8128 {
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008129 fprintf(f, "Syntax error in \"%s\"\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008130 fclose(f);
8131 }
8132 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008133 EMSG2("(NFA) Could not open \"%s\" to write !!!",
8134 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008135 }
8136#endif
8137 /*
Bram Moolenaarfda37292014-11-05 14:27:36 +01008138 * If the NFA engine failed, try the backtracking engine.
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008139 * The NFA engine also fails for patterns that it can't handle well
8140 * but are still valid patterns, thus a retry should work.
8141 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008142 if (regexp_engine == AUTOMATIC_ENGINE)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008143 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008144 regexp_engine = BACKTRACKING_ENGINE;
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008145 prog = bt_regengine.regcomp(expr, re_flags);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008146 }
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008147 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008148
Bram Moolenaarfda37292014-11-05 14:27:36 +01008149 if (prog != NULL)
8150 {
8151 /* Store the info needed to call regcomp() again when the engine turns
8152 * out to be very slow when executing it. */
8153 prog->re_engine = regexp_engine;
8154 prog->re_flags = re_flags;
8155 }
8156
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008157 return prog;
8158}
8159
8160/*
Bram Moolenaar473de612013-06-08 18:19:48 +02008161 * Free a compiled regexp program, returned by vim_regcomp().
8162 */
8163 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008164vim_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02008165{
8166 if (prog != NULL)
8167 prog->engine->regfree(prog);
8168}
8169
Bram Moolenaarfda37292014-11-05 14:27:36 +01008170#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008171static void report_re_switch(char_u *pat);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008172
8173 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008174report_re_switch(char_u *pat)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008175{
8176 if (p_verbose > 0)
8177 {
8178 verbose_enter();
8179 MSG_PUTS(_("Switching to backtracking RE engine for pattern: "));
8180 MSG_PUTS(pat);
8181 verbose_leave();
8182 }
8183}
8184#endif
8185
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008186static int vim_regexec_both(regmatch_T *rmp, char_u *line, colnr_T col, int nl);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008187
Bram Moolenaar473de612013-06-08 18:19:48 +02008188/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008189 * Match a regexp against a string.
8190 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008191 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008192 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaarfda37292014-11-05 14:27:36 +01008193 * When "nl" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008194 *
8195 * Return TRUE if there is a match, FALSE if not.
8196 */
Bram Moolenaarfda37292014-11-05 14:27:36 +01008197 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008198vim_regexec_both(
8199 regmatch_T *rmp,
8200 char_u *line, /* string to match against */
8201 colnr_T col, /* column to start looking for match */
8202 int nl)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008203{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008204 int result;
8205 regexec_T rex_save;
8206 int rex_in_use_save = rex_in_use;
8207
8208 if (rex_in_use)
8209 /* Being called recursively, save the state. */
8210 rex_save = rex;
8211 rex_in_use = TRUE;
8212 rex.reg_startp = NULL;
8213 rex.reg_endp = NULL;
8214 rex.reg_startpos = NULL;
8215 rex.reg_endpos = NULL;
8216
8217 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008218
8219 /* NFA engine aborted because it's very slow. */
8220 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8221 && result == NFA_TOO_EXPENSIVE)
8222 {
8223 int save_p_re = p_re;
8224 int re_flags = rmp->regprog->re_flags;
8225 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8226
8227 p_re = BACKTRACKING_ENGINE;
8228 vim_regfree(rmp->regprog);
8229 if (pat != NULL)
8230 {
8231#ifdef FEAT_EVAL
8232 report_re_switch(pat);
8233#endif
8234 rmp->regprog = vim_regcomp(pat, re_flags);
8235 if (rmp->regprog != NULL)
8236 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
8237 vim_free(pat);
8238 }
8239
8240 p_re = save_p_re;
8241 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02008242
8243 rex_in_use = rex_in_use_save;
8244 if (rex_in_use)
8245 rex = rex_save;
8246
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008247 return result > 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008248}
8249
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008250/*
8251 * Note: "*prog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008252 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008253 */
8254 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008255vim_regexec_prog(
8256 regprog_T **prog,
8257 int ignore_case,
8258 char_u *line,
8259 colnr_T col)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008260{
8261 int r;
8262 regmatch_T regmatch;
8263
8264 regmatch.regprog = *prog;
8265 regmatch.rm_ic = ignore_case;
8266 r = vim_regexec_both(&regmatch, line, col, FALSE);
8267 *prog = regmatch.regprog;
8268 return r;
8269}
8270
8271/*
8272 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008273 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008274 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008275 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008276vim_regexec(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008277{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008278 return vim_regexec_both(rmp, line, col, FALSE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008279}
8280
8281#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8282 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8283/*
8284 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008285 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008286 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008287 */
8288 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008289vim_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008290{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008291 return vim_regexec_both(rmp, line, col, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008292}
8293#endif
8294
8295/*
8296 * Match a regexp against multiple lines.
8297 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008298 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008299 * Uses curbuf for line count and 'iskeyword'.
8300 *
8301 * Return zero if there is no match. Return number of lines contained in the
8302 * match otherwise.
8303 */
8304 long
Bram Moolenaar05540972016-01-30 20:31:25 +01008305vim_regexec_multi(
8306 regmmatch_T *rmp,
8307 win_T *win, /* window in which to search or NULL */
8308 buf_T *buf, /* buffer in which to search */
8309 linenr_T lnum, /* nr of line to start looking for match */
8310 colnr_T col, /* column to start looking for match */
8311 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008312{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008313 int result;
8314 regexec_T rex_save;
8315 int rex_in_use_save = rex_in_use;
8316
8317 if (rex_in_use)
8318 /* Being called recursively, save the state. */
8319 rex_save = rex;
8320 rex_in_use = TRUE;
8321
8322 result = rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008323
8324 /* NFA engine aborted because it's very slow. */
8325 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8326 && result == NFA_TOO_EXPENSIVE)
8327 {
8328 int save_p_re = p_re;
8329 int re_flags = rmp->regprog->re_flags;
8330 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8331
8332 p_re = BACKTRACKING_ENGINE;
8333 vim_regfree(rmp->regprog);
8334 if (pat != NULL)
8335 {
8336#ifdef FEAT_EVAL
8337 report_re_switch(pat);
8338#endif
8339 rmp->regprog = vim_regcomp(pat, re_flags);
8340 if (rmp->regprog != NULL)
8341 result = rmp->regprog->engine->regexec_multi(
8342 rmp, win, buf, lnum, col, tm);
8343 vim_free(pat);
8344 }
8345 p_re = save_p_re;
8346 }
8347
Bram Moolenaar6100d022016-10-02 16:51:57 +02008348 rex_in_use = rex_in_use_save;
8349 if (rex_in_use)
8350 rex = rex_save;
8351
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008352 return result <= 0 ? 0 : result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008353}