blob: 74a73c44083ed6482ceaa0f3b1437877b24aa76f [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);
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003482static long bt_regexec_both(char_u *line, colnr_T col, proftime_T *tm, int *timed_out);
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003483static 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
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003725 return bt_regexec_both(line, col, NULL, 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 */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003743 proftime_T *tm, /* timeout limit or NULL */
3744 int *timed_out) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745{
Bram Moolenaar6100d022016-10-02 16:51:57 +02003746 rex.reg_match = NULL;
3747 rex.reg_mmatch = rmp;
3748 rex.reg_buf = buf;
3749 rex.reg_win = win;
3750 rex.reg_firstlnum = lnum;
3751 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
3752 rex.reg_line_lbr = FALSE;
3753 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003755 rex.reg_icombine = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003757 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003759 return bt_regexec_both(NULL, col, tm, timed_out);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760}
3761
3762/*
3763 * Match a regexp against a string ("line" points to the string) or multiple
3764 * lines ("line" is NULL, use reg_getline()).
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003765 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003768bt_regexec_both(
3769 char_u *line,
3770 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003771 proftime_T *tm UNUSED, /* timeout limit or NULL */
3772 int *timed_out UNUSED) /* flag set on timeout or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773{
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003774 bt_regprog_T *prog;
3775 char_u *s;
3776 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003778 /* Create "regstack" and "backpos" if they are not allocated yet.
3779 * We allocate *_INITIAL amount of bytes first and then set the grow size
3780 * to much bigger value to avoid many malloc calls in case of deep regular
3781 * expressions. */
3782 if (regstack.ga_data == NULL)
3783 {
3784 /* Use an item size of 1 byte, since we push different things
3785 * onto the regstack. */
3786 ga_init2(&regstack, 1, REGSTACK_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003787 (void)ga_grow(&regstack, REGSTACK_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003788 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003791 if (backpos.ga_data == NULL)
3792 {
3793 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003794 (void)ga_grow(&backpos, BACKPOS_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003795 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3796 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003797
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (REG_MULTI)
3799 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003800 prog = (bt_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 line = reg_getline((linenr_T)0);
Bram Moolenaar6100d022016-10-02 16:51:57 +02003802 rex.reg_startpos = rex.reg_mmatch->startpos;
3803 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 }
3805 else
3806 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003807 prog = (bt_regprog_T *)rex.reg_match->regprog;
3808 rex.reg_startp = rex.reg_match->startp;
3809 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 }
3811
3812 /* Be paranoid... */
3813 if (prog == NULL || line == NULL)
3814 {
3815 EMSG(_(e_null));
3816 goto theend;
3817 }
3818
3819 /* Check validity of program. */
3820 if (prog_magic_wrong())
3821 goto theend;
3822
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003823 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003824 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003825 goto theend;
3826
Bram Moolenaar6100d022016-10-02 16:51:57 +02003827 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003829 rex.reg_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003831 rex.reg_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832
3833#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003834 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003836 rex.reg_icombine = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837#endif
3838
3839 /* If there is a "must appear" string, look for it. */
3840 if (prog->regmust != NULL)
3841 {
3842 int c;
3843
3844#ifdef FEAT_MBYTE
3845 if (has_mbyte)
3846 c = (*mb_ptr2char)(prog->regmust);
3847 else
3848#endif
3849 c = *prog->regmust;
3850 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003851
3852 /*
3853 * This is used very often, esp. for ":global". Use three versions of
3854 * the loop to avoid overhead of conditions.
3855 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003856 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003857#ifdef FEAT_MBYTE
3858 && !has_mbyte
3859#endif
3860 )
3861 while ((s = vim_strbyte(s, c)) != NULL)
3862 {
3863 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3864 break; /* Found it. */
3865 ++s;
3866 }
3867#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003868 else if (!rex.reg_ic || (!enc_utf8 && mb_char2len(c) > 1))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003869 while ((s = vim_strchr(s, c)) != NULL)
3870 {
3871 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3872 break; /* Found it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003873 MB_PTR_ADV(s);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003874 }
3875#endif
3876 else
3877 while ((s = cstrchr(s, c)) != NULL)
3878 {
3879 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3880 break; /* Found it. */
Bram Moolenaar91acfff2017-03-12 19:22:36 +01003881 MB_PTR_ADV(s);
Bram Moolenaar05159a02005-02-26 23:04:13 +00003882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 if (s == NULL) /* Not present. */
3884 goto theend;
3885 }
3886
3887 regline = line;
3888 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003889 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 /* Simplest case: Anchored match need be tried only once. */
3892 if (prog->reganch)
3893 {
3894 int c;
3895
3896#ifdef FEAT_MBYTE
3897 if (has_mbyte)
3898 c = (*mb_ptr2char)(regline + col);
3899 else
3900#endif
3901 c = regline[col];
3902 if (prog->regstart == NUL
3903 || prog->regstart == c
Bram Moolenaar6100d022016-10-02 16:51:57 +02003904 || (rex.reg_ic && ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905#ifdef FEAT_MBYTE
3906 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3907 || (c < 255 && prog->regstart < 255 &&
3908#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003909 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910 retval = regtry(prog, col);
3911 else
3912 retval = 0;
3913 }
3914 else
3915 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003916#ifdef FEAT_RELTIME
3917 int tm_count = 0;
3918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003920 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921 {
3922 if (prog->regstart != NUL)
3923 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003924 /* Skip until the char we know it must start with.
3925 * Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003926 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003927#ifdef FEAT_MBYTE
3928 && !has_mbyte
3929#endif
3930 )
3931 s = vim_strbyte(regline + col, prog->regstart);
3932 else
3933 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 if (s == NULL)
3935 {
3936 retval = 0;
3937 break;
3938 }
3939 col = (int)(s - regline);
3940 }
3941
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003942 /* Check for maximum column to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003943 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003944 {
3945 retval = 0;
3946 break;
3947 }
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949 retval = regtry(prog, col);
3950 if (retval > 0)
3951 break;
3952
3953 /* if not currently on the first line, get it again */
3954 if (reglnum != 0)
3955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003957 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 }
3959 if (regline[col] == NUL)
3960 break;
3961#ifdef FEAT_MBYTE
3962 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003963 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 else
3965#endif
3966 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003967#ifdef FEAT_RELTIME
3968 /* Check for timeout once in a twenty times to avoid overhead. */
3969 if (tm != NULL && ++tm_count == 20)
3970 {
3971 tm_count = 0;
3972 if (profile_passed_limit(tm))
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003973 {
3974 if (timed_out != NULL)
3975 *timed_out = TRUE;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003976 break;
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02003977 }
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003978 }
3979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 }
3982
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003984 /* Free "reg_tofree" when it's a bit big.
3985 * Free regstack and backpos if they are bigger than their initial size. */
3986 if (reg_tofreelen > 400)
3987 {
3988 vim_free(reg_tofree);
3989 reg_tofree = NULL;
3990 }
3991 if (regstack.ga_maxlen > REGSTACK_INITIAL)
3992 ga_clear(&regstack);
3993 if (backpos.ga_maxlen > BACKPOS_INITIAL)
3994 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003995
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996 return retval;
3997}
3998
3999#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004000static reg_extmatch_T *make_extmatch(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001
4002/*
4003 * Create a new extmatch and mark it as referenced once.
4004 */
4005 static reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004006make_extmatch(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007{
4008 reg_extmatch_T *em;
4009
4010 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
4011 if (em != NULL)
4012 em->refcnt = 1;
4013 return em;
4014}
4015
4016/*
4017 * Add a reference to an extmatch.
4018 */
4019 reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004020ref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021{
4022 if (em != NULL)
4023 em->refcnt++;
4024 return em;
4025}
4026
4027/*
4028 * Remove a reference to an extmatch. If there are no references left, free
4029 * the info.
4030 */
4031 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004032unref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004033{
4034 int i;
4035
4036 if (em != NULL && --em->refcnt <= 0)
4037 {
4038 for (i = 0; i < NSUBEXP; ++i)
4039 vim_free(em->matches[i]);
4040 vim_free(em);
4041 }
4042}
4043#endif
4044
4045/*
4046 * regtry - try match of "prog" with at regline["col"].
4047 * Returns 0 for failure, number of lines contained in the match otherwise.
4048 */
4049 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01004050regtry(bt_regprog_T *prog, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051{
4052 reginput = regline + col;
4053 need_clear_subexpr = TRUE;
4054#ifdef FEAT_SYN_HL
4055 /* Clear the external match subpointers if necessary. */
4056 if (prog->reghasz == REX_SET)
4057 need_clear_zsubexpr = TRUE;
4058#endif
4059
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004060 if (regmatch(prog->program + 1) == 0)
4061 return 0;
4062
4063 cleanup_subexpr();
4064 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004066 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004068 rex.reg_startpos[0].lnum = 0;
4069 rex.reg_startpos[0].col = col;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004070 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004071 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004072 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004073 rex.reg_endpos[0].lnum = reglnum;
4074 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004077 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004078 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004079 }
4080 else
4081 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004082 if (rex.reg_startp[0] == NULL)
4083 rex.reg_startp[0] = regline + col;
4084 if (rex.reg_endp[0] == NULL)
4085 rex.reg_endp[0] = reginput;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004088 /* Package any found \z(...\) matches for export. Default is none. */
4089 unref_extmatch(re_extmatch_out);
4090 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004092 if (prog->reghasz == REX_SET)
4093 {
4094 int i;
4095
4096 cleanup_zsubexpr();
4097 re_extmatch_out = make_extmatch();
4098 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004100 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004102 /* Only accept single line matches. */
4103 if (reg_startzpos[i].lnum >= 0
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02004104 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4105 && reg_endzpos[i].col >= reg_startzpos[i].col)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004106 re_extmatch_out->matches[i] =
4107 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004109 reg_endzpos[i].col - reg_startzpos[i].col);
4110 }
4111 else
4112 {
4113 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4114 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004116 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 }
4118 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004120#endif
4121 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122}
4123
4124#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004125static int reg_prev_class(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127/*
4128 * Get class of previous character.
4129 */
4130 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004131reg_prev_class(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132{
4133 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004134 return mb_get_class_buf(reginput - 1
Bram Moolenaar6100d022016-10-02 16:51:57 +02004135 - (*mb_head_off)(regline, reginput - 1), rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 return -1;
4137}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004139
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004140static int reg_match_visual(void);
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004141
4142/*
4143 * Return TRUE if the current reginput position matches the Visual area.
4144 */
4145 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004146reg_match_visual(void)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004147{
4148 pos_T top, bot;
4149 linenr_T lnum;
4150 colnr_T col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004151 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004152 int mode;
4153 colnr_T start, end;
4154 colnr_T start2, end2;
4155 colnr_T cols;
4156
4157 /* Check if the buffer is the current buffer. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004158 if (rex.reg_buf != curbuf || VIsual.lnum == 0)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004159 return FALSE;
4160
4161 if (VIsual_active)
4162 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004163 if (LT_POS(VIsual, wp->w_cursor))
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004164 {
4165 top = VIsual;
4166 bot = wp->w_cursor;
4167 }
4168 else
4169 {
4170 top = wp->w_cursor;
4171 bot = VIsual;
4172 }
4173 mode = VIsual_mode;
4174 }
4175 else
4176 {
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01004177 if (LT_POS(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004178 {
4179 top = curbuf->b_visual.vi_start;
4180 bot = curbuf->b_visual.vi_end;
4181 }
4182 else
4183 {
4184 top = curbuf->b_visual.vi_end;
4185 bot = curbuf->b_visual.vi_start;
4186 }
4187 mode = curbuf->b_visual.vi_mode;
4188 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004189 lnum = reglnum + rex.reg_firstlnum;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004190 if (lnum < top.lnum || lnum > bot.lnum)
4191 return FALSE;
4192
4193 if (mode == 'v')
4194 {
4195 col = (colnr_T)(reginput - regline);
4196 if ((lnum == top.lnum && col < top.col)
4197 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e')))
4198 return FALSE;
4199 }
4200 else if (mode == Ctrl_V)
4201 {
4202 getvvcol(wp, &top, &start, NULL, &end);
4203 getvvcol(wp, &bot, &start2, NULL, &end2);
4204 if (start2 < start)
4205 start = start2;
4206 if (end2 > end)
4207 end = end2;
4208 if (top.col == MAXCOL || bot.col == MAXCOL)
4209 end = MAXCOL;
4210 cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline));
4211 if (cols < start || cols > end - (*p_sel == 'e'))
4212 return FALSE;
4213 }
4214 return TRUE;
4215}
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004216
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004217#define ADVANCE_REGINPUT() MB_PTR_ADV(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
4219/*
4220 * The arguments from BRACE_LIMITS are stored here. They are actually local
4221 * to regmatch(), but they are here to reduce the amount of stack space used
4222 * (it can be called recursively many times).
4223 */
4224static long bl_minval;
4225static long bl_maxval;
4226
4227/*
4228 * regmatch - main matching routine
4229 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004230 * Conceptually the strategy is simple: Check to see whether the current node
4231 * matches, push an item onto the regstack and loop to see whether the rest
4232 * matches, and then act accordingly. In practice we make some effort to
4233 * avoid using the regstack, in particular by going through "ordinary" nodes
4234 * (that don't need to know whether the rest of the match failed) by a nested
4235 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 *
4237 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4238 * the last matched character.
4239 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4240 * undefined state!
4241 */
4242 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004243regmatch(
4244 char_u *scan) /* Current node. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004246 char_u *next; /* Next node. */
4247 int op;
4248 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004249 regitem_T *rp;
4250 int no;
4251 int status; /* one of the RA_ values: */
4252#define RA_FAIL 1 /* something failed, abort */
4253#define RA_CONT 2 /* continue in inner loop */
4254#define RA_BREAK 3 /* break inner loop */
4255#define RA_MATCH 4 /* successful match */
4256#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004258 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004259 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004260 regstack.ga_len = 0;
4261 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004262
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004263 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004264 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004265 */
4266 for (;;)
4267 {
Bram Moolenaar41f12052013-08-25 17:01:42 +02004268 /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
4269 * Allow interrupting them with CTRL-C. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004270 fast_breakcheck();
4271
4272#ifdef DEBUG
4273 if (scan != NULL && regnarrate)
4274 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004275 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 mch_errmsg("(\n");
4277 }
4278#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004279
4280 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004281 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004282 * regstack.
4283 */
4284 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004286 if (got_int || scan == NULL)
4287 {
4288 status = RA_FAIL;
4289 break;
4290 }
4291 status = RA_CONT;
4292
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293#ifdef DEBUG
4294 if (regnarrate)
4295 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004296 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 mch_errmsg("...\n");
4298# ifdef FEAT_SYN_HL
4299 if (re_extmatch_in != NULL)
4300 {
4301 int i;
4302
4303 mch_errmsg(_("External submatches:\n"));
4304 for (i = 0; i < NSUBEXP; i++)
4305 {
4306 mch_errmsg(" \"");
4307 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004308 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 mch_errmsg("\"\n");
4310 }
4311 }
4312# endif
4313 }
4314#endif
4315 next = regnext(scan);
4316
4317 op = OP(scan);
4318 /* Check for character class with NL added. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004319 if (!rex.reg_line_lbr && WITH_NL(op) && REG_MULTI
4320 && *reginput == NUL && reglnum <= rex.reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 {
4322 reg_nextline();
4323 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004324 else if (rex.reg_line_lbr && WITH_NL(op) && *reginput == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 {
4326 ADVANCE_REGINPUT();
4327 }
4328 else
4329 {
4330 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004331 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332#ifdef FEAT_MBYTE
4333 if (has_mbyte)
4334 c = (*mb_ptr2char)(reginput);
4335 else
4336#endif
4337 c = *reginput;
4338 switch (op)
4339 {
4340 case BOL:
4341 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004342 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 break;
4344
4345 case EOL:
4346 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004347 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 break;
4349
4350 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004351 /* We're not at the beginning of the file when below the first
4352 * line where we started, not at the start of the line or we
4353 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 if (reglnum != 0 || reginput != regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02004355 || (REG_MULTI && rex.reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004356 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 break;
4358
4359 case RE_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004360 if (reglnum != rex.reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004361 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 break;
4363
4364 case CURSOR:
4365 /* Check if the buffer is in a window and compare the
Bram Moolenaar6100d022016-10-02 16:51:57 +02004366 * rex.reg_win->w_cursor position to the match position. */
4367 if (rex.reg_win == NULL
4368 || (reglnum + rex.reg_firstlnum
4369 != rex.reg_win->w_cursor.lnum)
4370 || ((colnr_T)(reginput - regline)
4371 != rex.reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004372 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 break;
4374
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004375 case RE_MARK:
Bram Moolenaar044aa292013-06-04 21:27:38 +02004376 /* Compare the mark position to the match position. */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004377 {
4378 int mark = OPERAND(scan)[0];
4379 int cmp = OPERAND(scan)[1];
4380 pos_T *pos;
4381
Bram Moolenaar6100d022016-10-02 16:51:57 +02004382 pos = getmark_buf(rex.reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004383 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar044aa292013-06-04 21:27:38 +02004384 || pos->lnum <= 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004385 || (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004386 ? (pos->col == (colnr_T)(reginput - regline)
4387 ? (cmp == '<' || cmp == '>')
4388 : (pos->col < (colnr_T)(reginput - regline)
4389 ? cmp != '>'
4390 : cmp != '<'))
Bram Moolenaar6100d022016-10-02 16:51:57 +02004391 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004392 ? cmp != '>'
4393 : cmp != '<')))
4394 status = RA_NOMATCH;
4395 }
4396 break;
4397
4398 case RE_VISUAL:
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004399 if (!reg_match_visual())
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004400 status = RA_NOMATCH;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004401 break;
4402
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 case RE_LNUM:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004404 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + rex.reg_firstlnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004406 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 break;
4408
4409 case RE_COL:
4410 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004411 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 break;
4413
4414 case RE_VCOL:
4415 if (!re_num_cmp((long_u)win_linetabsize(
Bram Moolenaar6100d022016-10-02 16:51:57 +02004416 rex.reg_win == NULL ? curwin : rex.reg_win,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004418 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 break;
4420
4421 case BOW: /* \<word; reginput points to w */
4422 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004423 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004425 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 {
4427 int this_class;
4428
4429 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004430 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004432 status = RA_NOMATCH; /* not on a word at all */
4433 else if (reg_prev_class() == this_class)
4434 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 }
4436#endif
4437 else
4438 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004439 if (!vim_iswordc_buf(c, rex.reg_buf) || (reginput > regline
4440 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004441 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 }
4443 break;
4444
4445 case EOW: /* word\>; reginput points after d */
4446 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004447 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004449 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
4451 int this_class, prev_class;
4452
4453 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004454 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004456 if (this_class == prev_class
4457 || prev_class == 0 || prev_class == 1)
4458 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004461 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004463 if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
4464 || (reginput[0] != NUL
4465 && vim_iswordc_buf(c, rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004466 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 break; /* Matched with EOW */
4469
4470 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004471 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004473 status = RA_NOMATCH;
4474 else
4475 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 break;
4477
4478 case IDENT:
4479 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004480 status = RA_NOMATCH;
4481 else
4482 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 break;
4484
4485 case SIDENT:
4486 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004487 status = RA_NOMATCH;
4488 else
4489 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 break;
4491
4492 case KWORD:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004493 if (!vim_iswordp_buf(reginput, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004494 status = RA_NOMATCH;
4495 else
4496 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 break;
4498
4499 case SKWORD:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004500 if (VIM_ISDIGIT(*reginput)
4501 || !vim_iswordp_buf(reginput, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004502 status = RA_NOMATCH;
4503 else
4504 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 break;
4506
4507 case FNAME:
4508 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004509 status = RA_NOMATCH;
4510 else
4511 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 break;
4513
4514 case SFNAME:
4515 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004516 status = RA_NOMATCH;
4517 else
4518 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 break;
4520
4521 case PRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004522 if (!vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004523 status = RA_NOMATCH;
4524 else
4525 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 break;
4527
4528 case SPRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004529 if (VIM_ISDIGIT(*reginput) || !vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004530 status = RA_NOMATCH;
4531 else
4532 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 break;
4534
4535 case WHITE:
Bram Moolenaar1c465442017-03-12 20:10:05 +01004536 if (!VIM_ISWHITE(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004537 status = RA_NOMATCH;
4538 else
4539 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 break;
4541
4542 case NWHITE:
Bram Moolenaar1c465442017-03-12 20:10:05 +01004543 if (c == NUL || VIM_ISWHITE(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004544 status = RA_NOMATCH;
4545 else
4546 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 break;
4548
4549 case DIGIT:
4550 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004551 status = RA_NOMATCH;
4552 else
4553 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 break;
4555
4556 case NDIGIT:
4557 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004558 status = RA_NOMATCH;
4559 else
4560 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 break;
4562
4563 case HEX:
4564 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004565 status = RA_NOMATCH;
4566 else
4567 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 break;
4569
4570 case NHEX:
4571 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004572 status = RA_NOMATCH;
4573 else
4574 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 break;
4576
4577 case OCTAL:
4578 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004579 status = RA_NOMATCH;
4580 else
4581 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 break;
4583
4584 case NOCTAL:
4585 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004586 status = RA_NOMATCH;
4587 else
4588 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 break;
4590
4591 case WORD:
4592 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004593 status = RA_NOMATCH;
4594 else
4595 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 break;
4597
4598 case NWORD:
4599 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004600 status = RA_NOMATCH;
4601 else
4602 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 break;
4604
4605 case HEAD:
4606 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004607 status = RA_NOMATCH;
4608 else
4609 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 break;
4611
4612 case NHEAD:
4613 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004614 status = RA_NOMATCH;
4615 else
4616 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 break;
4618
4619 case ALPHA:
4620 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621 status = RA_NOMATCH;
4622 else
4623 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 break;
4625
4626 case NALPHA:
4627 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004628 status = RA_NOMATCH;
4629 else
4630 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 break;
4632
4633 case LOWER:
4634 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004635 status = RA_NOMATCH;
4636 else
4637 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 break;
4639
4640 case NLOWER:
4641 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004642 status = RA_NOMATCH;
4643 else
4644 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 break;
4646
4647 case UPPER:
4648 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004649 status = RA_NOMATCH;
4650 else
4651 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 break;
4653
4654 case NUPPER:
4655 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004656 status = RA_NOMATCH;
4657 else
4658 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 break;
4660
4661 case EXACTLY:
4662 {
4663 int len;
4664 char_u *opnd;
4665
4666 opnd = OPERAND(scan);
4667 /* Inline the first byte, for speed. */
4668 if (*opnd != *reginput
Bram Moolenaar6100d022016-10-02 16:51:57 +02004669 && (!rex.reg_ic || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670#ifdef FEAT_MBYTE
4671 !enc_utf8 &&
4672#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004673 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004674 status = RA_NOMATCH;
4675 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 {
4677 /* match empty string always works; happens when "~" is
4678 * empty. */
4679 }
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004680 else
4681 {
4682 if (opnd[1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02004684 && !(enc_utf8 && rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#endif
4686 )
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004687 {
4688 len = 1; /* matched a single byte above */
4689 }
4690 else
4691 {
4692 /* Need to match first byte again for multi-byte. */
4693 len = (int)STRLEN(opnd);
4694 if (cstrncmp(opnd, reginput, &len) != 0)
4695 status = RA_NOMATCH;
4696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004698 /* Check for following composing character, unless %C
4699 * follows (skips over all composing chars). */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004700 if (status != RA_NOMATCH
4701 && enc_utf8
4702 && UTF_COMPOSINGLIKE(reginput, reginput + len)
Bram Moolenaar6100d022016-10-02 16:51:57 +02004703 && !rex.reg_icombine
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004704 && OP(next) != RE_COMPOSING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 {
4706 /* raaron: This code makes a composing character get
4707 * ignored, which is the correct behavior (sometimes)
4708 * for voweled Hebrew texts. */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004709 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#endif
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004712 if (status != RA_NOMATCH)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004713 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 }
4715 }
4716 break;
4717
4718 case ANYOF:
4719 case ANYBUT:
4720 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004721 status = RA_NOMATCH;
4722 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4723 status = RA_NOMATCH;
4724 else
4725 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 break;
4727
4728#ifdef FEAT_MBYTE
4729 case MULTIBYTECODE:
4730 if (has_mbyte)
4731 {
4732 int i, len;
4733 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004734 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735
4736 opnd = OPERAND(scan);
4737 /* Safety check (just in case 'encoding' was changed since
4738 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004739 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004740 {
4741 status = RA_NOMATCH;
4742 break;
4743 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004744 if (enc_utf8)
Bram Moolenaarace95982017-03-29 17:30:27 +02004745 opndc = utf_ptr2char(opnd);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004746 if (enc_utf8 && utf_iscomposing(opndc))
4747 {
4748 /* When only a composing char is given match at any
4749 * position where that composing char appears. */
4750 status = RA_NOMATCH;
Bram Moolenaar0e462412015-03-31 14:17:31 +02004751 for (i = 0; reginput[i] != NUL;
4752 i += utf_ptr2len(reginput + i))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004753 {
Bram Moolenaarace95982017-03-29 17:30:27 +02004754 inpc = utf_ptr2char(reginput + i);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004755 if (!utf_iscomposing(inpc))
4756 {
4757 if (i > 0)
4758 break;
4759 }
4760 else if (opndc == inpc)
4761 {
4762 /* Include all following composing chars. */
Bram Moolenaarace95982017-03-29 17:30:27 +02004763 len = i + utfc_ptr2len(reginput + i);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004764 status = RA_MATCH;
4765 break;
4766 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004767 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004768 }
4769 else
4770 for (i = 0; i < len; ++i)
4771 if (opnd[i] != reginput[i])
4772 {
4773 status = RA_NOMATCH;
4774 break;
4775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 reginput += len;
4777 }
4778 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004779 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 break;
4781#endif
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004782 case RE_COMPOSING:
4783#ifdef FEAT_MBYTE
4784 if (enc_utf8)
4785 {
4786 /* Skip composing characters. */
4787 while (utf_iscomposing(utf_ptr2char(reginput)))
Bram Moolenaar91acfff2017-03-12 19:22:36 +01004788 MB_CPTR_ADV(reginput);
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004789 }
4790#endif
4791 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792
4793 case NOTHING:
4794 break;
4795
4796 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004797 {
4798 int i;
4799 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800
Bram Moolenaar582fd852005-03-28 20:58:01 +00004801 /*
4802 * When we run into BACK we need to check if we don't keep
4803 * looping without matching any input. The second and later
4804 * times a BACK is encountered it fails if the input is still
4805 * at the same position as the previous time.
4806 * The positions are stored in "backpos" and found by the
4807 * current value of "scan", the position in the RE program.
4808 */
4809 bp = (backpos_T *)backpos.ga_data;
4810 for (i = 0; i < backpos.ga_len; ++i)
4811 if (bp[i].bp_scan == scan)
4812 break;
4813 if (i == backpos.ga_len)
4814 {
4815 /* First time at this BACK, make room to store the pos. */
4816 if (ga_grow(&backpos, 1) == FAIL)
4817 status = RA_FAIL;
4818 else
4819 {
4820 /* get "ga_data" again, it may have changed */
4821 bp = (backpos_T *)backpos.ga_data;
4822 bp[i].bp_scan = scan;
4823 ++backpos.ga_len;
4824 }
4825 }
4826 else if (reg_save_equal(&bp[i].bp_pos))
4827 /* Still at same position as last time, fail. */
4828 status = RA_NOMATCH;
4829
4830 if (status != RA_FAIL && status != RA_NOMATCH)
4831 reg_save(&bp[i].bp_pos, &backpos);
4832 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004833 break;
4834
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 case MOPEN + 0: /* Match start: \zs */
4836 case MOPEN + 1: /* \( */
4837 case MOPEN + 2:
4838 case MOPEN + 3:
4839 case MOPEN + 4:
4840 case MOPEN + 5:
4841 case MOPEN + 6:
4842 case MOPEN + 7:
4843 case MOPEN + 8:
4844 case MOPEN + 9:
4845 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 no = op - MOPEN;
4847 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004848 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004849 if (rp == NULL)
4850 status = RA_FAIL;
4851 else
4852 {
4853 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004854 save_se(&rp->rs_un.sesave, &rex.reg_startpos[no],
4855 &rex.reg_startp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004856 /* We simply continue and handle the result when done. */
4857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004859 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860
4861 case NOPEN: /* \%( */
4862 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004863 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004864 status = RA_FAIL;
4865 /* We simply continue and handle the result when done. */
4866 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867
4868#ifdef FEAT_SYN_HL
4869 case ZOPEN + 1:
4870 case ZOPEN + 2:
4871 case ZOPEN + 3:
4872 case ZOPEN + 4:
4873 case ZOPEN + 5:
4874 case ZOPEN + 6:
4875 case ZOPEN + 7:
4876 case ZOPEN + 8:
4877 case ZOPEN + 9:
4878 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 no = op - ZOPEN;
4880 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004881 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004882 if (rp == NULL)
4883 status = RA_FAIL;
4884 else
4885 {
4886 rp->rs_no = no;
4887 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4888 &reg_startzp[no]);
4889 /* We simply continue and handle the result when done. */
4890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004892 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#endif
4894
4895 case MCLOSE + 0: /* Match end: \ze */
4896 case MCLOSE + 1: /* \) */
4897 case MCLOSE + 2:
4898 case MCLOSE + 3:
4899 case MCLOSE + 4:
4900 case MCLOSE + 5:
4901 case MCLOSE + 6:
4902 case MCLOSE + 7:
4903 case MCLOSE + 8:
4904 case MCLOSE + 9:
4905 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 no = op - MCLOSE;
4907 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004908 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004909 if (rp == NULL)
4910 status = RA_FAIL;
4911 else
4912 {
4913 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004914 save_se(&rp->rs_un.sesave, &rex.reg_endpos[no],
4915 &rex.reg_endp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004916 /* We simply continue and handle the result when done. */
4917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004919 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920
4921#ifdef FEAT_SYN_HL
4922 case ZCLOSE + 1: /* \) after \z( */
4923 case ZCLOSE + 2:
4924 case ZCLOSE + 3:
4925 case ZCLOSE + 4:
4926 case ZCLOSE + 5:
4927 case ZCLOSE + 6:
4928 case ZCLOSE + 7:
4929 case ZCLOSE + 8:
4930 case ZCLOSE + 9:
4931 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 no = op - ZCLOSE;
4933 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004934 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004935 if (rp == NULL)
4936 status = RA_FAIL;
4937 else
4938 {
4939 rp->rs_no = no;
4940 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4941 &reg_endzp[no]);
4942 /* We simply continue and handle the result when done. */
4943 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004945 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946#endif
4947
4948 case BACKREF + 1:
4949 case BACKREF + 2:
4950 case BACKREF + 3:
4951 case BACKREF + 4:
4952 case BACKREF + 5:
4953 case BACKREF + 6:
4954 case BACKREF + 7:
4955 case BACKREF + 8:
4956 case BACKREF + 9:
4957 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960 no = op - BACKREF;
4961 cleanup_subexpr();
4962 if (!REG_MULTI) /* Single-line regexp */
4963 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004964 if (rex.reg_startp[no] == NULL || rex.reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 /* Backref was not set: Match an empty string. */
4967 len = 0;
4968 }
4969 else
4970 {
4971 /* Compare current input with back-ref in the same
4972 * line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004973 len = (int)(rex.reg_endp[no] - rex.reg_startp[no]);
4974 if (cstrncmp(rex.reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004975 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
4977 }
4978 else /* Multi-line regexp */
4979 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004980 if (rex.reg_startpos[no].lnum < 0
4981 || rex.reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
4983 /* Backref was not set: Match an empty string. */
4984 len = 0;
4985 }
4986 else
4987 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004988 if (rex.reg_startpos[no].lnum == reglnum
4989 && rex.reg_endpos[no].lnum == reglnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 /* Compare back-ref within the current line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004992 len = rex.reg_endpos[no].col
4993 - rex.reg_startpos[no].col;
4994 if (cstrncmp(regline + rex.reg_startpos[no].col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004996 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 else
4999 {
5000 /* Messy situation: Need to compare between two
5001 * lines. */
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02005002 int r = match_with_backref(
Bram Moolenaar6100d022016-10-02 16:51:57 +02005003 rex.reg_startpos[no].lnum,
5004 rex.reg_startpos[no].col,
5005 rex.reg_endpos[no].lnum,
5006 rex.reg_endpos[no].col,
Bram Moolenaar4cff8fa2013-06-14 22:48:54 +02005007 &len);
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02005008
5009 if (r != RA_MATCH)
5010 status = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012 }
5013 }
5014
5015 /* Matched the backref, skip over it. */
5016 reginput += len;
5017 }
5018 break;
5019
5020#ifdef FEAT_SYN_HL
5021 case ZREF + 1:
5022 case ZREF + 2:
5023 case ZREF + 3:
5024 case ZREF + 4:
5025 case ZREF + 5:
5026 case ZREF + 6:
5027 case ZREF + 7:
5028 case ZREF + 8:
5029 case ZREF + 9:
5030 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 int len;
5032
5033 cleanup_zsubexpr();
5034 no = op - ZREF;
5035 if (re_extmatch_in != NULL
5036 && re_extmatch_in->matches[no] != NULL)
5037 {
5038 len = (int)STRLEN(re_extmatch_in->matches[no]);
5039 if (cstrncmp(re_extmatch_in->matches[no],
5040 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005041 status = RA_NOMATCH;
5042 else
5043 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 else
5046 {
5047 /* Backref was not set: Match an empty string. */
5048 }
5049 }
5050 break;
5051#endif
5052
5053 case BRANCH:
5054 {
5055 if (OP(next) != BRANCH) /* No choice. */
5056 next = OPERAND(scan); /* Avoid recursion. */
5057 else
5058 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005059 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005060 if (rp == NULL)
5061 status = RA_FAIL;
5062 else
5063 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 }
5065 }
5066 break;
5067
5068 case BRACE_LIMITS:
5069 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 if (OP(next) == BRACE_SIMPLE)
5071 {
5072 bl_minval = OPERAND_MIN(scan);
5073 bl_maxval = OPERAND_MAX(scan);
5074 }
5075 else if (OP(next) >= BRACE_COMPLEX
5076 && OP(next) < BRACE_COMPLEX + 10)
5077 {
5078 no = OP(next) - BRACE_COMPLEX;
5079 brace_min[no] = OPERAND_MIN(scan);
5080 brace_max[no] = OPERAND_MAX(scan);
5081 brace_count[no] = 0;
5082 }
5083 else
5084 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005085 internal_error("BRACE_LIMITS");
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005086 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 }
5089 break;
5090
5091 case BRACE_COMPLEX + 0:
5092 case BRACE_COMPLEX + 1:
5093 case BRACE_COMPLEX + 2:
5094 case BRACE_COMPLEX + 3:
5095 case BRACE_COMPLEX + 4:
5096 case BRACE_COMPLEX + 5:
5097 case BRACE_COMPLEX + 6:
5098 case BRACE_COMPLEX + 7:
5099 case BRACE_COMPLEX + 8:
5100 case BRACE_COMPLEX + 9:
5101 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 no = op - BRACE_COMPLEX;
5103 ++brace_count[no];
5104
5105 /* If not matched enough times yet, try one more */
5106 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005107 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005109 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005110 if (rp == NULL)
5111 status = RA_FAIL;
5112 else
5113 {
5114 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005115 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005116 next = OPERAND(scan);
5117 /* We continue and handle the result when done. */
5118 }
5119 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121
5122 /* If matched enough times, may try matching some more */
5123 if (brace_min[no] <= brace_max[no])
5124 {
5125 /* Range is the normal way around, use longest match */
5126 if (brace_count[no] <= brace_max[no])
5127 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005128 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005129 if (rp == NULL)
5130 status = RA_FAIL;
5131 else
5132 {
5133 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005134 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005135 next = OPERAND(scan);
5136 /* We continue and handle the result when done. */
5137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 }
5140 else
5141 {
5142 /* Range is backwards, use shortest match first */
5143 if (brace_count[no] <= brace_min[no])
5144 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005145 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005146 if (rp == NULL)
5147 status = RA_FAIL;
5148 else
5149 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005150 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005151 /* We continue and handle the result when done. */
5152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 }
5155 }
5156 break;
5157
5158 case BRACE_SIMPLE:
5159 case STAR:
5160 case PLUS:
5161 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005162 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
5164 /*
5165 * Lookahead to avoid useless match attempts when we know
5166 * what character comes next.
5167 */
5168 if (OP(next) == EXACTLY)
5169 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005170 rst.nextb = *OPERAND(next);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005171 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005173 if (MB_ISUPPER(rst.nextb))
5174 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005176 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 }
5178 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005179 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 else
5182 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005183 rst.nextb = NUL;
5184 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 }
5186 if (op != BRACE_SIMPLE)
5187 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005188 rst.minval = (op == STAR) ? 0 : 1;
5189 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 else
5192 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005193 rst.minval = bl_minval;
5194 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 }
5196
5197 /*
5198 * When maxval > minval, try matching as much as possible, up
5199 * to maxval. When maxval < minval, try matching at least the
5200 * minimal number (since the range is backwards, that's also
5201 * maxval!).
5202 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005203 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005206 status = RA_FAIL;
5207 break;
5208 }
5209 if (rst.minval <= rst.maxval
5210 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5211 {
5212 /* It could match. Prepare for trying to match what
5213 * follows. The code is below. Parameters are stored in
5214 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005215 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005216 {
5217 EMSG(_(e_maxmempat));
5218 status = RA_FAIL;
5219 }
5220 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005221 status = RA_FAIL;
5222 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005224 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005225 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005226 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005227 if (rp == NULL)
5228 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005230 {
5231 *(((regstar_T *)rp) - 1) = rst;
5232 status = RA_BREAK; /* skip the restore bits */
5233 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235 }
5236 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005237 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
5240 break;
5241
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005242 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 case MATCH:
5244 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005245 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005246 if (rp == NULL)
5247 status = RA_FAIL;
5248 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005250 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005251 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005252 next = OPERAND(scan);
5253 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 break;
5256
5257 case BEHIND:
5258 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005259 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005260 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005261 {
5262 EMSG(_(e_maxmempat));
5263 status = RA_FAIL;
5264 }
5265 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005266 status = RA_FAIL;
5267 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005269 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005270 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005271 if (rp == NULL)
5272 status = RA_FAIL;
5273 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005275 /* Need to save the subexpr to be able to restore them
5276 * when there is a match but we don't use it. */
5277 save_subexpr(((regbehind_T *)rp) - 1);
5278
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005279 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005280 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005281 /* First try if what follows matches. If it does then we
5282 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005285 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286
5287 case BHPOS:
5288 if (REG_MULTI)
5289 {
5290 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5291 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005292 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 }
5294 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005295 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 break;
5297
5298 case NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02005299 if ((c != NUL || !REG_MULTI || reglnum > rex.reg_maxline
5300 || rex.reg_line_lbr)
5301 && (c != '\n' || !rex.reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005302 status = RA_NOMATCH;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005303 else if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 ADVANCE_REGINPUT();
5305 else
5306 reg_nextline();
5307 break;
5308
5309 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005310 status = RA_MATCH; /* Success! */
5311 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312
5313 default:
5314 EMSG(_(e_re_corr));
5315#ifdef DEBUG
5316 printf("Illegal op code %d\n", op);
5317#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005318 status = RA_FAIL;
5319 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
5321 }
5322
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005323 /* If we can't continue sequentially, break the inner loop. */
5324 if (status != RA_CONT)
5325 break;
5326
5327 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005329
5330 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
5332 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005333 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005334 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005336 while (regstack.ga_len > 0 && status != RA_FAIL)
5337 {
5338 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5339 switch (rp->rs_state)
5340 {
5341 case RS_NOPEN:
5342 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005343 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005344 break;
5345
5346 case RS_MOPEN:
5347 /* Pop the state. Restore pointers when there is no match. */
5348 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005349 restore_se(&rp->rs_un.sesave, &rex.reg_startpos[rp->rs_no],
5350 &rex.reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005351 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005352 break;
5353
5354#ifdef FEAT_SYN_HL
5355 case RS_ZOPEN:
5356 /* Pop the state. Restore pointers when there is no match. */
5357 if (status == RA_NOMATCH)
5358 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5359 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005360 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005363
5364 case RS_MCLOSE:
5365 /* Pop the state. Restore pointers when there is no match. */
5366 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005367 restore_se(&rp->rs_un.sesave, &rex.reg_endpos[rp->rs_no],
5368 &rex.reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005369 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005370 break;
5371
5372#ifdef FEAT_SYN_HL
5373 case RS_ZCLOSE:
5374 /* Pop the state. Restore pointers when there is no match. */
5375 if (status == RA_NOMATCH)
5376 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5377 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005378 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005379 break;
5380#endif
5381
5382 case RS_BRANCH:
5383 if (status == RA_MATCH)
5384 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005385 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005386 else
5387 {
5388 if (status != RA_BREAK)
5389 {
5390 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005391 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005392 scan = rp->rs_scan;
5393 }
5394 if (scan == NULL || OP(scan) != BRANCH)
5395 {
5396 /* no more branches, didn't find a match */
5397 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005398 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005399 }
5400 else
5401 {
5402 /* Prepare to try a branch. */
5403 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005404 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005405 scan = OPERAND(scan);
5406 }
5407 }
5408 break;
5409
5410 case RS_BRCPLX_MORE:
5411 /* Pop the state. Restore pointers when there is no match. */
5412 if (status == RA_NOMATCH)
5413 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005414 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005415 --brace_count[rp->rs_no]; /* decrement match count */
5416 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005417 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005418 break;
5419
5420 case RS_BRCPLX_LONG:
5421 /* Pop the state. Restore pointers when there is no match. */
5422 if (status == RA_NOMATCH)
5423 {
5424 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005425 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005426 --brace_count[rp->rs_no];
5427 /* continue with the items after "\{}" */
5428 status = RA_CONT;
5429 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005430 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005431 if (status == RA_CONT)
5432 scan = regnext(scan);
5433 break;
5434
5435 case RS_BRCPLX_SHORT:
5436 /* Pop the state. Restore pointers when there is no match. */
5437 if (status == RA_NOMATCH)
5438 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005439 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005440 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005441 if (status == RA_NOMATCH)
5442 {
5443 scan = OPERAND(scan);
5444 status = RA_CONT;
5445 }
5446 break;
5447
5448 case RS_NOMATCH:
5449 /* Pop the state. If the operand matches for NOMATCH or
5450 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5451 * except for SUBPAT, and continue with the next item. */
5452 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5453 status = RA_NOMATCH;
5454 else
5455 {
5456 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005457 if (rp->rs_no != SUBPAT) /* zero-width */
5458 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005459 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005460 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005461 if (status == RA_CONT)
5462 scan = regnext(scan);
5463 break;
5464
5465 case RS_BEHIND1:
5466 if (status == RA_NOMATCH)
5467 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005468 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005469 regstack.ga_len -= sizeof(regbehind_T);
5470 }
5471 else
5472 {
5473 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5474 * the behind part does (not) match before the current
5475 * position in the input. This must be done at every
5476 * position in the input and checking if the match ends at
5477 * the current position. */
5478
5479 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005480 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005481
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005482 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005483 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005484 * result, hitting the start of the line or the previous
5485 * line (for multi-line matching).
5486 * Set behind_pos to where the match should end, BHPOS
5487 * will match it. Save the current value. */
5488 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5489 behind_pos = rp->rs_un.regsave;
5490
5491 rp->rs_state = RS_BEHIND2;
5492
Bram Moolenaar582fd852005-03-28 20:58:01 +00005493 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005494 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005495 }
5496 break;
5497
5498 case RS_BEHIND2:
5499 /*
5500 * Looping for BEHIND / NOBEHIND match.
5501 */
5502 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5503 {
5504 /* found a match that ends where "next" started */
5505 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5506 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005507 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5508 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005509 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005510 {
5511 /* But we didn't want a match. Need to restore the
5512 * subexpr, because what follows matched, so they have
5513 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005514 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005515 restore_subexpr(((regbehind_T *)rp) - 1);
5516 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005517 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005518 regstack.ga_len -= sizeof(regbehind_T);
5519 }
5520 else
5521 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005522 long limit;
5523
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005524 /* No match or a match that doesn't end where we want it: Go
5525 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005526 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005527 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005528 if (REG_MULTI)
5529 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005530 if (limit > 0
5531 && ((rp->rs_un.regsave.rs_u.pos.lnum
5532 < behind_pos.rs_u.pos.lnum
5533 ? (colnr_T)STRLEN(regline)
5534 : behind_pos.rs_u.pos.col)
5535 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5536 no = FAIL;
5537 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005538 {
5539 if (rp->rs_un.regsave.rs_u.pos.lnum
5540 < behind_pos.rs_u.pos.lnum
5541 || reg_getline(
5542 --rp->rs_un.regsave.rs_u.pos.lnum)
5543 == NULL)
5544 no = FAIL;
5545 else
5546 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005547 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005548 rp->rs_un.regsave.rs_u.pos.col =
5549 (colnr_T)STRLEN(regline);
5550 }
5551 }
5552 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005553 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005554#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005555 if (has_mbyte)
5556 rp->rs_un.regsave.rs_u.pos.col -=
5557 (*mb_head_off)(regline, regline
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005558 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005559 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005560#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005561 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005562 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005563 }
5564 else
5565 {
5566 if (rp->rs_un.regsave.rs_u.ptr == regline)
5567 no = FAIL;
5568 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005569 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005570 MB_PTR_BACK(regline, rp->rs_un.regsave.rs_u.ptr);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005571 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5572 - rp->rs_un.regsave.rs_u.ptr) > limit)
5573 no = FAIL;
5574 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005575 }
5576 if (no == OK)
5577 {
5578 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005579 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005580 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005581 if (status == RA_MATCH)
5582 {
5583 /* We did match, so subexpr may have been changed,
5584 * need to restore them for the next try. */
5585 status = RA_NOMATCH;
5586 restore_subexpr(((regbehind_T *)rp) - 1);
5587 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005588 }
5589 else
5590 {
5591 /* Can't advance. For NOBEHIND that's a match. */
5592 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5593 if (rp->rs_no == NOBEHIND)
5594 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005595 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5596 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005597 status = RA_MATCH;
5598 }
5599 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005600 {
5601 /* We do want a proper match. Need to restore the
5602 * subexpr if we had a match, because they may have
5603 * been set. */
5604 if (status == RA_MATCH)
5605 {
5606 status = RA_NOMATCH;
5607 restore_subexpr(((regbehind_T *)rp) - 1);
5608 }
5609 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005610 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005611 regstack.ga_len -= sizeof(regbehind_T);
5612 }
5613 }
5614 break;
5615
5616 case RS_STAR_LONG:
5617 case RS_STAR_SHORT:
5618 {
5619 regstar_T *rst = ((regstar_T *)rp) - 1;
5620
5621 if (status == RA_MATCH)
5622 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005623 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005624 regstack.ga_len -= sizeof(regstar_T);
5625 break;
5626 }
5627
5628 /* Tried once already, restore input pointers. */
5629 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005630 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005631
5632 /* Repeat until we found a position where it could match. */
5633 for (;;)
5634 {
5635 if (status != RA_BREAK)
5636 {
5637 /* Tried first position already, advance. */
5638 if (rp->rs_state == RS_STAR_LONG)
5639 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005640 /* Trying for longest match, but couldn't or
5641 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005642 if (--rst->count < rst->minval)
5643 break;
5644 if (reginput == regline)
5645 {
5646 /* backup to last char of previous line */
5647 --reglnum;
5648 regline = reg_getline(reglnum);
5649 /* Just in case regrepeat() didn't count
5650 * right. */
5651 if (regline == NULL)
5652 break;
5653 reginput = regline + STRLEN(regline);
5654 fast_breakcheck();
5655 }
5656 else
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005657 MB_PTR_BACK(regline, reginput);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005658 }
5659 else
5660 {
5661 /* Range is backwards, use shortest match first.
5662 * Careful: maxval and minval are exchanged!
5663 * Couldn't or didn't match: try advancing one
5664 * char. */
5665 if (rst->count == rst->minval
5666 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5667 break;
5668 ++rst->count;
5669 }
5670 if (got_int)
5671 break;
5672 }
5673 else
5674 status = RA_NOMATCH;
5675
5676 /* If it could match, try it. */
5677 if (rst->nextb == NUL || *reginput == rst->nextb
5678 || *reginput == rst->nextb_ic)
5679 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005680 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005681 scan = regnext(rp->rs_scan);
5682 status = RA_CONT;
5683 break;
5684 }
5685 }
5686 if (status != RA_CONT)
5687 {
5688 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005689 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005690 regstack.ga_len -= sizeof(regstar_T);
5691 status = RA_NOMATCH;
5692 }
5693 }
5694 break;
5695 }
5696
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005697 /* If we want to continue the inner loop or didn't pop a state
5698 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005699 if (status == RA_CONT || rp == (regitem_T *)
5700 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5701 break;
5702 }
5703
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005704 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005705 if (status == RA_CONT)
5706 continue;
5707
5708 /*
5709 * If the regstack is empty or something failed we are done.
5710 */
5711 if (regstack.ga_len == 0 || status == RA_FAIL)
5712 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005713 if (scan == NULL)
5714 {
5715 /*
5716 * We get here only if there's trouble -- normally "case END" is
5717 * the terminating point.
5718 */
5719 EMSG(_(e_re_corr));
5720#ifdef DEBUG
5721 printf("Premature EOL\n");
5722#endif
5723 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005724 if (status == RA_FAIL)
5725 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005726 return (status == RA_MATCH);
5727 }
5728
5729 } /* End of loop until the regstack is empty. */
5730
5731 /* NOTREACHED */
5732}
5733
5734/*
5735 * Push an item onto the regstack.
5736 * Returns pointer to new item. Returns NULL when out of memory.
5737 */
5738 static regitem_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005739regstack_push(regstate_T state, char_u *scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005740{
5741 regitem_T *rp;
5742
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005743 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005744 {
5745 EMSG(_(e_maxmempat));
5746 return NULL;
5747 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005748 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005749 return NULL;
5750
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005751 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005752 rp->rs_state = state;
5753 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005754
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005755 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005756 return rp;
5757}
5758
5759/*
5760 * Pop an item from the regstack.
5761 */
5762 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005763regstack_pop(char_u **scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005764{
5765 regitem_T *rp;
5766
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005767 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005768 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005769
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005770 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771}
5772
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773/*
5774 * regrepeat - repeatedly match something simple, return how many.
5775 * Advances reginput (and reglnum) to just after the matched chars.
5776 */
5777 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005778regrepeat(
5779 char_u *p,
5780 long maxcount) /* maximum number of matches allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005781{
5782 long count = 0;
5783 char_u *scan;
5784 char_u *opnd;
5785 int mask;
5786 int testval = 0;
5787
5788 scan = reginput; /* Make local copy of reginput for speed. */
5789 opnd = OPERAND(p);
5790 switch (OP(p))
5791 {
5792 case ANY:
5793 case ANY + ADD_NL:
5794 while (count < maxcount)
5795 {
5796 /* Matching anything means we continue until end-of-line (or
5797 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5798 while (*scan != NUL && count < maxcount)
5799 {
5800 ++count;
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005801 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005803 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5804 || rex.reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 break;
5806 ++count; /* count the line-break */
5807 reg_nextline();
5808 scan = reginput;
5809 if (got_int)
5810 break;
5811 }
5812 break;
5813
5814 case IDENT:
5815 case IDENT + ADD_NL:
5816 testval = TRUE;
5817 /*FALLTHROUGH*/
5818 case SIDENT:
5819 case SIDENT + ADD_NL:
5820 while (count < maxcount)
5821 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005822 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005824 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 }
5826 else if (*scan == NUL)
5827 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005828 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5829 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 reg_nextline();
5832 scan = reginput;
5833 if (got_int)
5834 break;
5835 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005836 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 ++scan;
5838 else
5839 break;
5840 ++count;
5841 }
5842 break;
5843
5844 case KWORD:
5845 case KWORD + ADD_NL:
5846 testval = TRUE;
5847 /*FALLTHROUGH*/
5848 case SKWORD:
5849 case SKWORD + ADD_NL:
5850 while (count < maxcount)
5851 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005852 if (vim_iswordp_buf(scan, rex.reg_buf)
Bram Moolenaarf813a182013-01-30 13:59:37 +01005853 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005855 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 }
5857 else if (*scan == NUL)
5858 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005859 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5860 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 break;
5862 reg_nextline();
5863 scan = reginput;
5864 if (got_int)
5865 break;
5866 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005867 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 ++scan;
5869 else
5870 break;
5871 ++count;
5872 }
5873 break;
5874
5875 case FNAME:
5876 case FNAME + ADD_NL:
5877 testval = TRUE;
5878 /*FALLTHROUGH*/
5879 case SFNAME:
5880 case SFNAME + ADD_NL:
5881 while (count < maxcount)
5882 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005883 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005884 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005885 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 }
5887 else if (*scan == NUL)
5888 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005889 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5890 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005891 break;
5892 reg_nextline();
5893 scan = reginput;
5894 if (got_int)
5895 break;
5896 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005897 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 ++scan;
5899 else
5900 break;
5901 ++count;
5902 }
5903 break;
5904
5905 case PRINT:
5906 case PRINT + ADD_NL:
5907 testval = TRUE;
5908 /*FALLTHROUGH*/
5909 case SPRINT:
5910 case SPRINT + ADD_NL:
5911 while (count < maxcount)
5912 {
5913 if (*scan == NUL)
5914 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005915 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5916 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 break;
5918 reg_nextline();
5919 scan = reginput;
5920 if (got_int)
5921 break;
5922 }
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005923 else if (vim_isprintc(PTR2CHAR(scan)) == 1
5924 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 {
Bram Moolenaar91acfff2017-03-12 19:22:36 +01005926 MB_PTR_ADV(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005928 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005929 ++scan;
5930 else
5931 break;
5932 ++count;
5933 }
5934 break;
5935
5936 case WHITE:
5937 case WHITE + ADD_NL:
5938 testval = mask = RI_WHITE;
5939do_class:
5940 while (count < maxcount)
5941 {
5942#ifdef FEAT_MBYTE
5943 int l;
5944#endif
5945 if (*scan == NUL)
5946 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005947 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5948 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 break;
5950 reg_nextline();
5951 scan = reginput;
5952 if (got_int)
5953 break;
5954 }
5955#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005956 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005957 {
5958 if (testval != 0)
5959 break;
5960 scan += l;
5961 }
5962#endif
5963 else if ((class_tab[*scan] & mask) == testval)
5964 ++scan;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005965 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005966 ++scan;
5967 else
5968 break;
5969 ++count;
5970 }
5971 break;
5972
5973 case NWHITE:
5974 case NWHITE + ADD_NL:
5975 mask = RI_WHITE;
5976 goto do_class;
5977 case DIGIT:
5978 case DIGIT + ADD_NL:
5979 testval = mask = RI_DIGIT;
5980 goto do_class;
5981 case NDIGIT:
5982 case NDIGIT + ADD_NL:
5983 mask = RI_DIGIT;
5984 goto do_class;
5985 case HEX:
5986 case HEX + ADD_NL:
5987 testval = mask = RI_HEX;
5988 goto do_class;
5989 case NHEX:
5990 case NHEX + ADD_NL:
5991 mask = RI_HEX;
5992 goto do_class;
5993 case OCTAL:
5994 case OCTAL + ADD_NL:
5995 testval = mask = RI_OCTAL;
5996 goto do_class;
5997 case NOCTAL:
5998 case NOCTAL + ADD_NL:
5999 mask = RI_OCTAL;
6000 goto do_class;
6001 case WORD:
6002 case WORD + ADD_NL:
6003 testval = mask = RI_WORD;
6004 goto do_class;
6005 case NWORD:
6006 case NWORD + ADD_NL:
6007 mask = RI_WORD;
6008 goto do_class;
6009 case HEAD:
6010 case HEAD + ADD_NL:
6011 testval = mask = RI_HEAD;
6012 goto do_class;
6013 case NHEAD:
6014 case NHEAD + ADD_NL:
6015 mask = RI_HEAD;
6016 goto do_class;
6017 case ALPHA:
6018 case ALPHA + ADD_NL:
6019 testval = mask = RI_ALPHA;
6020 goto do_class;
6021 case NALPHA:
6022 case NALPHA + ADD_NL:
6023 mask = RI_ALPHA;
6024 goto do_class;
6025 case LOWER:
6026 case LOWER + ADD_NL:
6027 testval = mask = RI_LOWER;
6028 goto do_class;
6029 case NLOWER:
6030 case NLOWER + ADD_NL:
6031 mask = RI_LOWER;
6032 goto do_class;
6033 case UPPER:
6034 case UPPER + ADD_NL:
6035 testval = mask = RI_UPPER;
6036 goto do_class;
6037 case NUPPER:
6038 case NUPPER + ADD_NL:
6039 mask = RI_UPPER;
6040 goto do_class;
6041
6042 case EXACTLY:
6043 {
6044 int cu, cl;
6045
6046 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006047 * would have been used for it. It does handle single-byte
6048 * characters, such as latin1. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006049 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006050 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006051 cu = MB_TOUPPER(*opnd);
6052 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006053 while (count < maxcount && (*scan == cu || *scan == cl))
6054 {
6055 count++;
6056 scan++;
6057 }
6058 }
6059 else
6060 {
6061 cu = *opnd;
6062 while (count < maxcount && *scan == cu)
6063 {
6064 count++;
6065 scan++;
6066 }
6067 }
6068 break;
6069 }
6070
6071#ifdef FEAT_MBYTE
6072 case MULTIBYTECODE:
6073 {
6074 int i, len, cf = 0;
6075
6076 /* Safety check (just in case 'encoding' was changed since
6077 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006078 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006080 if (rex.reg_ic && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 cf = utf_fold(utf_ptr2char(opnd));
Bram Moolenaar069dd082015-05-04 09:56:49 +02006082 while (count < maxcount && (*mb_ptr2len)(scan) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006083 {
6084 for (i = 0; i < len; ++i)
6085 if (opnd[i] != scan[i])
6086 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006087 if (i < len && (!rex.reg_ic || !enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00006088 || utf_fold(utf_ptr2char(scan)) != cf))
6089 break;
6090 scan += len;
6091 ++count;
6092 }
6093 }
6094 }
6095 break;
6096#endif
6097
6098 case ANYOF:
6099 case ANYOF + ADD_NL:
6100 testval = TRUE;
6101 /*FALLTHROUGH*/
6102
6103 case ANYBUT:
6104 case ANYBUT + ADD_NL:
6105 while (count < maxcount)
6106 {
6107#ifdef FEAT_MBYTE
6108 int len;
6109#endif
6110 if (*scan == NUL)
6111 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006112 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
6113 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 break;
6115 reg_nextline();
6116 scan = reginput;
6117 if (got_int)
6118 break;
6119 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006120 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006121 ++scan;
6122#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006123 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006124 {
6125 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6126 break;
6127 scan += len;
6128 }
6129#endif
6130 else
6131 {
6132 if ((cstrchr(opnd, *scan) == NULL) == testval)
6133 break;
6134 ++scan;
6135 }
6136 ++count;
6137 }
6138 break;
6139
6140 case NEWL:
6141 while (count < maxcount
Bram Moolenaar6100d022016-10-02 16:51:57 +02006142 && ((*scan == NUL && reglnum <= rex.reg_maxline
6143 && !rex.reg_line_lbr && REG_MULTI)
6144 || (*scan == '\n' && rex.reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 count++;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006147 if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006148 ADVANCE_REGINPUT();
6149 else
6150 reg_nextline();
6151 scan = reginput;
6152 if (got_int)
6153 break;
6154 }
6155 break;
6156
6157 default: /* Oh dear. Called inappropriately. */
6158 EMSG(_(e_re_corr));
6159#ifdef DEBUG
6160 printf("Called regrepeat with op code %d\n", OP(p));
6161#endif
6162 break;
6163 }
6164
6165 reginput = scan;
6166
6167 return (int)count;
6168}
6169
6170/*
6171 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006172 * Returns NULL when calculating size, when there is no next item and when
6173 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 */
6175 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006176regnext(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177{
6178 int offset;
6179
Bram Moolenaard3005802009-11-25 17:21:32 +00006180 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 return NULL;
6182
6183 offset = NEXT(p);
6184 if (offset == 0)
6185 return NULL;
6186
Bram Moolenaar582fd852005-03-28 20:58:01 +00006187 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006188 return p - offset;
6189 else
6190 return p + offset;
6191}
6192
6193/*
6194 * Check the regexp program for its magic number.
6195 * Return TRUE if it's wrong.
6196 */
6197 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006198prog_magic_wrong(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006200 regprog_T *prog;
6201
Bram Moolenaar6100d022016-10-02 16:51:57 +02006202 prog = REG_MULTI ? rex.reg_mmatch->regprog : rex.reg_match->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006203 if (prog->engine == &nfa_regengine)
6204 /* For NFA matcher we don't check the magic */
6205 return FALSE;
6206
6207 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006208 {
6209 EMSG(_(e_re_corr));
6210 return TRUE;
6211 }
6212 return FALSE;
6213}
6214
6215/*
6216 * Cleanup the subexpressions, if this wasn't done yet.
6217 * This construction is used to clear the subexpressions only when they are
6218 * used (to increase speed).
6219 */
6220 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006221cleanup_subexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222{
6223 if (need_clear_subexpr)
6224 {
6225 if (REG_MULTI)
6226 {
6227 /* Use 0xff to set lnum to -1 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006228 vim_memset(rex.reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6229 vim_memset(rex.reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006230 }
6231 else
6232 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006233 vim_memset(rex.reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6234 vim_memset(rex.reg_endp, 0, sizeof(char_u *) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 }
6236 need_clear_subexpr = FALSE;
6237 }
6238}
6239
6240#ifdef FEAT_SYN_HL
6241 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006242cleanup_zsubexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006243{
6244 if (need_clear_zsubexpr)
6245 {
6246 if (REG_MULTI)
6247 {
6248 /* Use 0xff to set lnum to -1 */
6249 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6250 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6251 }
6252 else
6253 {
6254 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6255 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6256 }
6257 need_clear_zsubexpr = FALSE;
6258 }
6259}
6260#endif
6261
6262/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006263 * Save the current subexpr to "bp", so that they can be restored
6264 * later by restore_subexpr().
6265 */
6266 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006267save_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006268{
6269 int i;
6270
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006271 /* When "need_clear_subexpr" is set we don't need to save the values, only
6272 * remember that this flag needs to be set again when restoring. */
6273 bp->save_need_clear_subexpr = need_clear_subexpr;
6274 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006275 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006276 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006277 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006278 if (REG_MULTI)
6279 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006280 bp->save_start[i].se_u.pos = rex.reg_startpos[i];
6281 bp->save_end[i].se_u.pos = rex.reg_endpos[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006282 }
6283 else
6284 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006285 bp->save_start[i].se_u.ptr = rex.reg_startp[i];
6286 bp->save_end[i].se_u.ptr = rex.reg_endp[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006287 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006288 }
6289 }
6290}
6291
6292/*
6293 * Restore the subexpr from "bp".
6294 */
6295 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006296restore_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006297{
6298 int i;
6299
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006300 /* Only need to restore saved values when they are not to be cleared. */
6301 need_clear_subexpr = bp->save_need_clear_subexpr;
6302 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006303 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006304 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006305 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006306 if (REG_MULTI)
6307 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006308 rex.reg_startpos[i] = bp->save_start[i].se_u.pos;
6309 rex.reg_endpos[i] = bp->save_end[i].se_u.pos;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006310 }
6311 else
6312 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006313 rex.reg_startp[i] = bp->save_start[i].se_u.ptr;
6314 rex.reg_endp[i] = bp->save_end[i].se_u.ptr;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006315 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006316 }
6317 }
6318}
6319
6320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006321 * Advance reglnum, regline and reginput to the next line.
6322 */
6323 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006324reg_nextline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006325{
6326 regline = reg_getline(++reglnum);
6327 reginput = regline;
6328 fast_breakcheck();
6329}
6330
6331/*
6332 * Save the input line and position in a regsave_T.
6333 */
6334 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006335reg_save(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336{
6337 if (REG_MULTI)
6338 {
6339 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6340 save->rs_u.pos.lnum = reglnum;
6341 }
6342 else
6343 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006344 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006345}
6346
6347/*
6348 * Restore the input line and position from a regsave_T.
6349 */
6350 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006351reg_restore(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006352{
6353 if (REG_MULTI)
6354 {
6355 if (reglnum != save->rs_u.pos.lnum)
6356 {
6357 /* only call reg_getline() when the line number changed to save
6358 * a bit of time */
6359 reglnum = save->rs_u.pos.lnum;
6360 regline = reg_getline(reglnum);
6361 }
6362 reginput = regline + save->rs_u.pos.col;
6363 }
6364 else
6365 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006366 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367}
6368
6369/*
6370 * Return TRUE if current position is equal to saved position.
6371 */
6372 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006373reg_save_equal(regsave_T *save)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374{
6375 if (REG_MULTI)
6376 return reglnum == save->rs_u.pos.lnum
6377 && reginput == regline + save->rs_u.pos.col;
6378 return reginput == save->rs_u.ptr;
6379}
6380
6381/*
6382 * Tentatively set the sub-expression start to the current position (after
6383 * calling regmatch() they will have changed). Need to save the existing
6384 * values for when there is no match.
6385 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6386 * depending on REG_MULTI.
6387 */
6388 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006389save_se_multi(save_se_T *savep, lpos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390{
6391 savep->se_u.pos = *posp;
6392 posp->lnum = reglnum;
6393 posp->col = (colnr_T)(reginput - regline);
6394}
6395
6396 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006397save_se_one(save_se_T *savep, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398{
6399 savep->se_u.ptr = *pp;
6400 *pp = reginput;
6401}
6402
6403/*
6404 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6405 */
6406 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006407re_num_cmp(long_u val, char_u *scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408{
6409 long_u n = OPERAND_MIN(scan);
6410
6411 if (OPERAND_CMP(scan) == '>')
6412 return val > n;
6413 if (OPERAND_CMP(scan) == '<')
6414 return val < n;
6415 return val == n;
6416}
6417
Bram Moolenaar580abea2013-06-14 20:31:28 +02006418/*
6419 * Check whether a backreference matches.
6420 * Returns RA_FAIL, RA_NOMATCH or RA_MATCH.
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006421 * If "bytelen" is not NULL, it is set to the byte length of the match in the
6422 * last line.
Bram Moolenaar580abea2013-06-14 20:31:28 +02006423 */
6424 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006425match_with_backref(
6426 linenr_T start_lnum,
6427 colnr_T start_col,
6428 linenr_T end_lnum,
6429 colnr_T end_col,
6430 int *bytelen)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006431{
6432 linenr_T clnum = start_lnum;
6433 colnr_T ccol = start_col;
6434 int len;
6435 char_u *p;
6436
6437 if (bytelen != NULL)
6438 *bytelen = 0;
6439 for (;;)
6440 {
6441 /* Since getting one line may invalidate the other, need to make copy.
6442 * Slow! */
6443 if (regline != reg_tofree)
6444 {
6445 len = (int)STRLEN(regline);
6446 if (reg_tofree == NULL || len >= (int)reg_tofreelen)
6447 {
6448 len += 50; /* get some extra */
6449 vim_free(reg_tofree);
6450 reg_tofree = alloc(len);
6451 if (reg_tofree == NULL)
6452 return RA_FAIL; /* out of memory!*/
6453 reg_tofreelen = len;
6454 }
6455 STRCPY(reg_tofree, regline);
6456 reginput = reg_tofree + (reginput - regline);
6457 regline = reg_tofree;
6458 }
6459
6460 /* Get the line to compare with. */
6461 p = reg_getline(clnum);
6462 if (clnum == end_lnum)
6463 len = end_col - ccol;
6464 else
6465 len = (int)STRLEN(p + ccol);
6466
6467 if (cstrncmp(p + ccol, reginput, &len) != 0)
6468 return RA_NOMATCH; /* doesn't match */
6469 if (bytelen != NULL)
6470 *bytelen += len;
6471 if (clnum == end_lnum)
6472 break; /* match and at end! */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006473 if (reglnum >= rex.reg_maxline)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006474 return RA_NOMATCH; /* text too short */
6475
6476 /* Advance to next line. */
6477 reg_nextline();
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006478 if (bytelen != NULL)
6479 *bytelen = 0;
Bram Moolenaar580abea2013-06-14 20:31:28 +02006480 ++clnum;
6481 ccol = 0;
6482 if (got_int)
6483 return RA_FAIL;
6484 }
6485
6486 /* found a match! Note that regline may now point to a copy of the line,
6487 * that should not matter. */
6488 return RA_MATCH;
6489}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006491#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
6493/*
6494 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6495 */
6496 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006497regdump(char_u *pattern, bt_regprog_T *r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006498{
6499 char_u *s;
6500 int op = EXACTLY; /* Arbitrary non-END op. */
6501 char_u *next;
6502 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006503 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006504
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006505#ifdef BT_REGEXP_LOG
6506 f = fopen("bt_regexp_log.log", "a");
6507#else
6508 f = stdout;
6509#endif
6510 if (f == NULL)
6511 return;
6512 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513
6514 s = r->program + 1;
6515 /*
6516 * Loop until we find the END that isn't before a referred next (an END
6517 * can also appear in a NOMATCH operand).
6518 */
6519 while (op != END || s <= end)
6520 {
6521 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006522 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 next = regnext(s);
6524 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006525 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006527 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006528 if (end < next)
6529 end = next;
6530 if (op == BRACE_LIMITS)
6531 {
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006532 /* Two ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006533 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006534 s += 8;
6535 }
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006536 else if (op == BEHIND || op == NOBEHIND)
6537 {
6538 /* one int */
6539 fprintf(f, " count %ld", OPERAND_MIN(s));
6540 s += 4;
6541 }
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02006542 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
6543 {
6544 /* one int plus comperator */
6545 fprintf(f, " count %ld", OPERAND_MIN(s));
6546 s += 5;
6547 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006548 s += 3;
6549 if (op == ANYOF || op == ANYOF + ADD_NL
6550 || op == ANYBUT || op == ANYBUT + ADD_NL
6551 || op == EXACTLY)
6552 {
6553 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006554 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006556 fprintf(f, "%c", *s++);
6557 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 s++;
6559 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006560 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 }
6562
6563 /* Header fields of interest. */
6564 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006565 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 ? (char *)transchar(r->regstart)
6567 : "multibyte", r->regstart);
6568 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006569 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006571 fprintf(f, "must have \"%s\"", r->regmust);
6572 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006574#ifdef BT_REGEXP_LOG
6575 fclose(f);
6576#endif
6577}
6578#endif /* BT_REGEXP_DUMP */
6579
6580#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581/*
6582 * regprop - printable representation of opcode
6583 */
6584 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006585regprop(char_u *op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006587 char *p;
6588 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006589
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006590 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006592 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 {
6594 case BOL:
6595 p = "BOL";
6596 break;
6597 case EOL:
6598 p = "EOL";
6599 break;
6600 case RE_BOF:
6601 p = "BOF";
6602 break;
6603 case RE_EOF:
6604 p = "EOF";
6605 break;
6606 case CURSOR:
6607 p = "CURSOR";
6608 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006609 case RE_VISUAL:
6610 p = "RE_VISUAL";
6611 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 case RE_LNUM:
6613 p = "RE_LNUM";
6614 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006615 case RE_MARK:
6616 p = "RE_MARK";
6617 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 case RE_COL:
6619 p = "RE_COL";
6620 break;
6621 case RE_VCOL:
6622 p = "RE_VCOL";
6623 break;
6624 case BOW:
6625 p = "BOW";
6626 break;
6627 case EOW:
6628 p = "EOW";
6629 break;
6630 case ANY:
6631 p = "ANY";
6632 break;
6633 case ANY + ADD_NL:
6634 p = "ANY+NL";
6635 break;
6636 case ANYOF:
6637 p = "ANYOF";
6638 break;
6639 case ANYOF + ADD_NL:
6640 p = "ANYOF+NL";
6641 break;
6642 case ANYBUT:
6643 p = "ANYBUT";
6644 break;
6645 case ANYBUT + ADD_NL:
6646 p = "ANYBUT+NL";
6647 break;
6648 case IDENT:
6649 p = "IDENT";
6650 break;
6651 case IDENT + ADD_NL:
6652 p = "IDENT+NL";
6653 break;
6654 case SIDENT:
6655 p = "SIDENT";
6656 break;
6657 case SIDENT + ADD_NL:
6658 p = "SIDENT+NL";
6659 break;
6660 case KWORD:
6661 p = "KWORD";
6662 break;
6663 case KWORD + ADD_NL:
6664 p = "KWORD+NL";
6665 break;
6666 case SKWORD:
6667 p = "SKWORD";
6668 break;
6669 case SKWORD + ADD_NL:
6670 p = "SKWORD+NL";
6671 break;
6672 case FNAME:
6673 p = "FNAME";
6674 break;
6675 case FNAME + ADD_NL:
6676 p = "FNAME+NL";
6677 break;
6678 case SFNAME:
6679 p = "SFNAME";
6680 break;
6681 case SFNAME + ADD_NL:
6682 p = "SFNAME+NL";
6683 break;
6684 case PRINT:
6685 p = "PRINT";
6686 break;
6687 case PRINT + ADD_NL:
6688 p = "PRINT+NL";
6689 break;
6690 case SPRINT:
6691 p = "SPRINT";
6692 break;
6693 case SPRINT + ADD_NL:
6694 p = "SPRINT+NL";
6695 break;
6696 case WHITE:
6697 p = "WHITE";
6698 break;
6699 case WHITE + ADD_NL:
6700 p = "WHITE+NL";
6701 break;
6702 case NWHITE:
6703 p = "NWHITE";
6704 break;
6705 case NWHITE + ADD_NL:
6706 p = "NWHITE+NL";
6707 break;
6708 case DIGIT:
6709 p = "DIGIT";
6710 break;
6711 case DIGIT + ADD_NL:
6712 p = "DIGIT+NL";
6713 break;
6714 case NDIGIT:
6715 p = "NDIGIT";
6716 break;
6717 case NDIGIT + ADD_NL:
6718 p = "NDIGIT+NL";
6719 break;
6720 case HEX:
6721 p = "HEX";
6722 break;
6723 case HEX + ADD_NL:
6724 p = "HEX+NL";
6725 break;
6726 case NHEX:
6727 p = "NHEX";
6728 break;
6729 case NHEX + ADD_NL:
6730 p = "NHEX+NL";
6731 break;
6732 case OCTAL:
6733 p = "OCTAL";
6734 break;
6735 case OCTAL + ADD_NL:
6736 p = "OCTAL+NL";
6737 break;
6738 case NOCTAL:
6739 p = "NOCTAL";
6740 break;
6741 case NOCTAL + ADD_NL:
6742 p = "NOCTAL+NL";
6743 break;
6744 case WORD:
6745 p = "WORD";
6746 break;
6747 case WORD + ADD_NL:
6748 p = "WORD+NL";
6749 break;
6750 case NWORD:
6751 p = "NWORD";
6752 break;
6753 case NWORD + ADD_NL:
6754 p = "NWORD+NL";
6755 break;
6756 case HEAD:
6757 p = "HEAD";
6758 break;
6759 case HEAD + ADD_NL:
6760 p = "HEAD+NL";
6761 break;
6762 case NHEAD:
6763 p = "NHEAD";
6764 break;
6765 case NHEAD + ADD_NL:
6766 p = "NHEAD+NL";
6767 break;
6768 case ALPHA:
6769 p = "ALPHA";
6770 break;
6771 case ALPHA + ADD_NL:
6772 p = "ALPHA+NL";
6773 break;
6774 case NALPHA:
6775 p = "NALPHA";
6776 break;
6777 case NALPHA + ADD_NL:
6778 p = "NALPHA+NL";
6779 break;
6780 case LOWER:
6781 p = "LOWER";
6782 break;
6783 case LOWER + ADD_NL:
6784 p = "LOWER+NL";
6785 break;
6786 case NLOWER:
6787 p = "NLOWER";
6788 break;
6789 case NLOWER + ADD_NL:
6790 p = "NLOWER+NL";
6791 break;
6792 case UPPER:
6793 p = "UPPER";
6794 break;
6795 case UPPER + ADD_NL:
6796 p = "UPPER+NL";
6797 break;
6798 case NUPPER:
6799 p = "NUPPER";
6800 break;
6801 case NUPPER + ADD_NL:
6802 p = "NUPPER+NL";
6803 break;
6804 case BRANCH:
6805 p = "BRANCH";
6806 break;
6807 case EXACTLY:
6808 p = "EXACTLY";
6809 break;
6810 case NOTHING:
6811 p = "NOTHING";
6812 break;
6813 case BACK:
6814 p = "BACK";
6815 break;
6816 case END:
6817 p = "END";
6818 break;
6819 case MOPEN + 0:
6820 p = "MATCH START";
6821 break;
6822 case MOPEN + 1:
6823 case MOPEN + 2:
6824 case MOPEN + 3:
6825 case MOPEN + 4:
6826 case MOPEN + 5:
6827 case MOPEN + 6:
6828 case MOPEN + 7:
6829 case MOPEN + 8:
6830 case MOPEN + 9:
6831 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6832 p = NULL;
6833 break;
6834 case MCLOSE + 0:
6835 p = "MATCH END";
6836 break;
6837 case MCLOSE + 1:
6838 case MCLOSE + 2:
6839 case MCLOSE + 3:
6840 case MCLOSE + 4:
6841 case MCLOSE + 5:
6842 case MCLOSE + 6:
6843 case MCLOSE + 7:
6844 case MCLOSE + 8:
6845 case MCLOSE + 9:
6846 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6847 p = NULL;
6848 break;
6849 case BACKREF + 1:
6850 case BACKREF + 2:
6851 case BACKREF + 3:
6852 case BACKREF + 4:
6853 case BACKREF + 5:
6854 case BACKREF + 6:
6855 case BACKREF + 7:
6856 case BACKREF + 8:
6857 case BACKREF + 9:
6858 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6859 p = NULL;
6860 break;
6861 case NOPEN:
6862 p = "NOPEN";
6863 break;
6864 case NCLOSE:
6865 p = "NCLOSE";
6866 break;
6867#ifdef FEAT_SYN_HL
6868 case ZOPEN + 1:
6869 case ZOPEN + 2:
6870 case ZOPEN + 3:
6871 case ZOPEN + 4:
6872 case ZOPEN + 5:
6873 case ZOPEN + 6:
6874 case ZOPEN + 7:
6875 case ZOPEN + 8:
6876 case ZOPEN + 9:
6877 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6878 p = NULL;
6879 break;
6880 case ZCLOSE + 1:
6881 case ZCLOSE + 2:
6882 case ZCLOSE + 3:
6883 case ZCLOSE + 4:
6884 case ZCLOSE + 5:
6885 case ZCLOSE + 6:
6886 case ZCLOSE + 7:
6887 case ZCLOSE + 8:
6888 case ZCLOSE + 9:
6889 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6890 p = NULL;
6891 break;
6892 case ZREF + 1:
6893 case ZREF + 2:
6894 case ZREF + 3:
6895 case ZREF + 4:
6896 case ZREF + 5:
6897 case ZREF + 6:
6898 case ZREF + 7:
6899 case ZREF + 8:
6900 case ZREF + 9:
6901 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6902 p = NULL;
6903 break;
6904#endif
6905 case STAR:
6906 p = "STAR";
6907 break;
6908 case PLUS:
6909 p = "PLUS";
6910 break;
6911 case NOMATCH:
6912 p = "NOMATCH";
6913 break;
6914 case MATCH:
6915 p = "MATCH";
6916 break;
6917 case BEHIND:
6918 p = "BEHIND";
6919 break;
6920 case NOBEHIND:
6921 p = "NOBEHIND";
6922 break;
6923 case SUBPAT:
6924 p = "SUBPAT";
6925 break;
6926 case BRACE_LIMITS:
6927 p = "BRACE_LIMITS";
6928 break;
6929 case BRACE_SIMPLE:
6930 p = "BRACE_SIMPLE";
6931 break;
6932 case BRACE_COMPLEX + 0:
6933 case BRACE_COMPLEX + 1:
6934 case BRACE_COMPLEX + 2:
6935 case BRACE_COMPLEX + 3:
6936 case BRACE_COMPLEX + 4:
6937 case BRACE_COMPLEX + 5:
6938 case BRACE_COMPLEX + 6:
6939 case BRACE_COMPLEX + 7:
6940 case BRACE_COMPLEX + 8:
6941 case BRACE_COMPLEX + 9:
6942 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6943 p = NULL;
6944 break;
6945#ifdef FEAT_MBYTE
6946 case MULTIBYTECODE:
6947 p = "MULTIBYTECODE";
6948 break;
6949#endif
6950 case NEWL:
6951 p = "NEWL";
6952 break;
6953 default:
6954 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6955 p = NULL;
6956 break;
6957 }
6958 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006959 STRCAT(buf, p);
6960 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006962#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963
Bram Moolenaarfb031402014-09-09 17:18:49 +02006964/*
6965 * Used in a place where no * or \+ can follow.
6966 */
6967 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006968re_mult_next(char *what)
Bram Moolenaarfb031402014-09-09 17:18:49 +02006969{
6970 if (re_multi_type(peekchr()) == MULTI_MULT)
6971 EMSG2_RET_FAIL(_("E888: (NFA regexp) cannot repeat %s"), what);
6972 return OK;
6973}
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006976static void mb_decompose(int c, int *c1, int *c2, int *c3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978typedef struct
6979{
6980 int a, b, c;
6981} decomp_T;
6982
6983
6984/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006985static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986{
6987 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6988 {0x5d0,0,0}, /* 0xfb21 alt alef */
6989 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6990 {0x5d4,0,0}, /* 0xfb23 alt he */
6991 {0x5db,0,0}, /* 0xfb24 alt kaf */
6992 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6993 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6994 {0x5e8,0,0}, /* 0xfb27 alt resh */
6995 {0x5ea,0,0}, /* 0xfb28 alt tav */
6996 {'+', 0, 0}, /* 0xfb29 alt plus */
6997 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6998 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6999 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
7000 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
7001 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
7002 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
7003 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
7004 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
7005 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
7006 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
7007 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
7008 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
7009 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
7010 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
7011 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
7012 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
7013 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
7014 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
7015 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
7016 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
7017 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
7018 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
7019 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
7020 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
7021 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
7022 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
7023 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
7024 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
7025 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7026 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7027 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7028 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7029 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7030 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7031 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7032 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7033 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7034 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7035};
7036
7037 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007038mb_decompose(int c, int *c1, int *c2, int *c3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007039{
7040 decomp_T d;
7041
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007042 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 {
7044 d = decomp_table[c - 0xfb20];
7045 *c1 = d.a;
7046 *c2 = d.b;
7047 *c3 = d.c;
7048 }
7049 else
7050 {
7051 *c1 = c;
7052 *c2 = *c3 = 0;
7053 }
7054}
7055#endif
7056
7057/*
Bram Moolenaar6100d022016-10-02 16:51:57 +02007058 * Compare two strings, ignore case if rex.reg_ic set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 * Return 0 if strings match, non-zero otherwise.
7060 * Correct the length "*n" when composing characters are ignored.
7061 */
7062 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007063cstrncmp(char_u *s1, char_u *s2, int *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064{
7065 int result;
7066
Bram Moolenaar6100d022016-10-02 16:51:57 +02007067 if (!rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 result = STRNCMP(s1, s2, *n);
7069 else
7070 result = MB_STRNICMP(s1, s2, *n);
7071
7072#ifdef FEAT_MBYTE
7073 /* if it failed and it's utf8 and we want to combineignore: */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007074 if (result != 0 && enc_utf8 && rex.reg_icombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 {
7076 char_u *str1, *str2;
7077 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 int junk;
7079
7080 /* we have to handle the strcmp ourselves, since it is necessary to
7081 * deal with the composing characters by ignoring them: */
7082 str1 = s1;
7083 str2 = s2;
7084 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007085 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 {
7087 c1 = mb_ptr2char_adv(&str1);
7088 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007089
7090 /* decompose the character if necessary, into 'base' characters
7091 * because I don't care about Arabic, I will hard-code the Hebrew
7092 * which I *do* care about! So sue me... */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007093 if (c1 != c2 && (!rex.reg_ic || utf_fold(c1) != utf_fold(c2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 {
7095 /* decomposition necessary? */
7096 mb_decompose(c1, &c11, &junk, &junk);
7097 mb_decompose(c2, &c12, &junk, &junk);
7098 c1 = c11;
7099 c2 = c12;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007100 if (c11 != c12
7101 && (!rex.reg_ic || utf_fold(c11) != utf_fold(c12)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007102 break;
7103 }
7104 }
7105 result = c2 - c1;
7106 if (result == 0)
7107 *n = (int)(str2 - s2);
7108 }
7109#endif
7110
7111 return result;
7112}
7113
7114/*
7115 * cstrchr: This function is used a lot for simple searches, keep it fast!
7116 */
7117 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007118cstrchr(char_u *s, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007119{
7120 char_u *p;
7121 int cc;
7122
Bram Moolenaar6100d022016-10-02 16:51:57 +02007123 if (!rex.reg_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00007124#ifdef FEAT_MBYTE
7125 || (!enc_utf8 && mb_char2len(c) > 1)
7126#endif
7127 )
7128 return vim_strchr(s, c);
7129
7130 /* tolower() and toupper() can be slow, comparing twice should be a lot
7131 * faster (esp. when using MS Visual C++!).
7132 * For UTF-8 need to use folded case. */
7133#ifdef FEAT_MBYTE
7134 if (enc_utf8 && c > 0x80)
7135 cc = utf_fold(c);
7136 else
7137#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007138 if (MB_ISUPPER(c))
7139 cc = MB_TOLOWER(c);
7140 else if (MB_ISLOWER(c))
7141 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007142 else
7143 return vim_strchr(s, c);
7144
7145#ifdef FEAT_MBYTE
7146 if (has_mbyte)
7147 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007148 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 {
7150 if (enc_utf8 && c > 0x80)
7151 {
7152 if (utf_fold(utf_ptr2char(p)) == cc)
7153 return p;
7154 }
7155 else if (*p == c || *p == cc)
7156 return p;
7157 }
7158 }
7159 else
7160#endif
7161 /* Faster version for when there are no multi-byte characters. */
7162 for (p = s; *p != NUL; ++p)
7163 if (*p == c || *p == cc)
7164 return p;
7165
7166 return NULL;
7167}
7168
7169/***************************************************************
7170 * regsub stuff *
7171 ***************************************************************/
7172
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173/*
7174 * We should define ftpr as a pointer to a function returning a pointer to
7175 * a function returning a pointer to a function ...
7176 * This is impossible, so we declare a pointer to a function returning a
7177 * pointer to a function returning void. This should work for all compilers.
7178 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007179typedef void (*(*fptr_T)(int *, int))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007181static fptr_T do_upper(int *, int);
7182static fptr_T do_Upper(int *, int);
7183static fptr_T do_lower(int *, int);
7184static fptr_T do_Lower(int *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007186static 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 +00007187
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007188 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007189do_upper(int *d, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007190{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007191 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007193 return (fptr_T)NULL;
7194}
7195
7196 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007197do_Upper(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007198{
7199 *d = MB_TOUPPER(c);
7200
7201 return (fptr_T)do_Upper;
7202}
7203
7204 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007205do_lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007206{
7207 *d = MB_TOLOWER(c);
7208
7209 return (fptr_T)NULL;
7210}
7211
7212 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007213do_Lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007214{
7215 *d = MB_TOLOWER(c);
7216
7217 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218}
7219
7220/*
7221 * regtilde(): Replace tildes in the pattern by the old pattern.
7222 *
7223 * Short explanation of the tilde: It stands for the previous replacement
7224 * pattern. If that previous pattern also contains a ~ we should go back a
7225 * step further... But we insert the previous pattern into the current one
7226 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007227 * This still does not handle the case where "magic" changes. So require the
7228 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 *
7230 * The tildes are parsed once before the first call to vim_regsub().
7231 */
7232 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007233regtilde(char_u *source, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007234{
7235 char_u *newsub = source;
7236 char_u *tmpsub;
7237 char_u *p;
7238 int len;
7239 int prevlen;
7240
7241 for (p = newsub; *p; ++p)
7242 {
7243 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7244 {
7245 if (reg_prev_sub != NULL)
7246 {
7247 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7248 prevlen = (int)STRLEN(reg_prev_sub);
7249 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7250 if (tmpsub != NULL)
7251 {
7252 /* copy prefix */
7253 len = (int)(p - newsub); /* not including ~ */
7254 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007255 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7257 /* copy postfix */
7258 if (!magic)
7259 ++p; /* back off \ */
7260 STRCPY(tmpsub + len + prevlen, p + 1);
7261
7262 if (newsub != source) /* already allocated newsub */
7263 vim_free(newsub);
7264 newsub = tmpsub;
7265 p = newsub + len + prevlen;
7266 }
7267 }
7268 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007269 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007271 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007272 --p;
7273 }
7274 else
7275 {
7276 if (*p == '\\' && p[1]) /* skip escaped characters */
7277 ++p;
7278#ifdef FEAT_MBYTE
7279 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007280 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007281#endif
7282 }
7283 }
7284
7285 vim_free(reg_prev_sub);
7286 if (newsub != source) /* newsub was allocated, just keep it */
7287 reg_prev_sub = newsub;
7288 else /* no ~ found, need to save newsub */
7289 reg_prev_sub = vim_strsave(newsub);
7290 return newsub;
7291}
7292
7293#ifdef FEAT_EVAL
7294static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7295
Bram Moolenaar6100d022016-10-02 16:51:57 +02007296/* These pointers are used for reg_submatch(). Needed for when the
7297 * substitution string is an expression that contains a call to substitute()
7298 * and submatch(). */
7299typedef struct {
7300 regmatch_T *sm_match;
7301 regmmatch_T *sm_mmatch;
7302 linenr_T sm_firstlnum;
7303 linenr_T sm_maxline;
7304 int sm_line_lbr;
7305} regsubmatch_T;
7306
7307static regsubmatch_T rsm; /* can only be used when can_f_submatch is TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308#endif
7309
7310#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007311
7312/*
7313 * Put the submatches in "argv[0]" which is a list passed into call_func() by
7314 * vim_regsub_both().
7315 */
7316 static int
7317fill_submatch_list(int argc UNUSED, typval_T *argv, int argcount)
7318{
7319 listitem_T *li;
7320 int i;
7321 char_u *s;
7322
7323 if (argcount == 0)
7324 /* called function doesn't take an argument */
7325 return 0;
7326
7327 /* Relies on sl_list to be the first item in staticList10_T. */
7328 init_static_list((staticList10_T *)(argv->vval.v_list));
7329
7330 /* There are always 10 list items in staticList10_T. */
7331 li = argv->vval.v_list->lv_first;
7332 for (i = 0; i < 10; ++i)
7333 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007334 s = rsm.sm_match->startp[i];
7335 if (s == NULL || rsm.sm_match->endp[i] == NULL)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007336 s = NULL;
7337 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007338 s = vim_strnsave(s, (int)(rsm.sm_match->endp[i] - s));
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007339 li->li_tv.v_type = VAR_STRING;
7340 li->li_tv.vval.v_string = s;
7341 li = li->li_next;
7342 }
7343 return 1;
7344}
7345
7346 static void
7347clear_submatch_list(staticList10_T *sl)
7348{
7349 int i;
7350
7351 for (i = 0; i < 10; ++i)
7352 vim_free(sl->sl_items[i].li_tv.vval.v_string);
7353}
7354
Bram Moolenaar071d4272004-06-13 20:20:40 +00007355/*
7356 * vim_regsub() - perform substitutions after a vim_regexec() or
7357 * vim_regexec_multi() match.
7358 *
7359 * If "copy" is TRUE really copy into "dest".
7360 * If "copy" is FALSE nothing is copied, this is just to find out the length
7361 * of the result.
7362 *
7363 * If "backslash" is TRUE, a backslash will be removed later, need to double
7364 * them to keep them, and insert a backslash before a CR to avoid it being
7365 * replaced with a line break later.
7366 *
7367 * Note: The matched text must not change between the call of
7368 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7369 * references invalid!
7370 *
7371 * Returns the size of the replacement, including terminating NUL.
7372 */
7373 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007374vim_regsub(
7375 regmatch_T *rmp,
7376 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007377 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007378 char_u *dest,
7379 int copy,
7380 int magic,
7381 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007383 int result;
7384 regexec_T rex_save;
7385 int rex_in_use_save = rex_in_use;
7386
7387 if (rex_in_use)
7388 /* Being called recursively, save the state. */
7389 rex_save = rex;
7390 rex_in_use = TRUE;
7391
7392 rex.reg_match = rmp;
7393 rex.reg_mmatch = NULL;
7394 rex.reg_maxline = 0;
7395 rex.reg_buf = curbuf;
7396 rex.reg_line_lbr = TRUE;
7397 result = vim_regsub_both(source, expr, dest, copy, magic, backslash);
7398
7399 rex_in_use = rex_in_use_save;
7400 if (rex_in_use)
7401 rex = rex_save;
7402
7403 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404}
7405#endif
7406
7407 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007408vim_regsub_multi(
7409 regmmatch_T *rmp,
7410 linenr_T lnum,
7411 char_u *source,
7412 char_u *dest,
7413 int copy,
7414 int magic,
7415 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007417 int result;
7418 regexec_T rex_save;
7419 int rex_in_use_save = rex_in_use;
7420
7421 if (rex_in_use)
7422 /* Being called recursively, save the state. */
7423 rex_save = rex;
7424 rex_in_use = TRUE;
7425
7426 rex.reg_match = NULL;
7427 rex.reg_mmatch = rmp;
7428 rex.reg_buf = curbuf; /* always works on the current buffer! */
7429 rex.reg_firstlnum = lnum;
7430 rex.reg_maxline = curbuf->b_ml.ml_line_count - lnum;
7431 rex.reg_line_lbr = FALSE;
7432 result = vim_regsub_both(source, NULL, dest, copy, magic, backslash);
7433
7434 rex_in_use = rex_in_use_save;
7435 if (rex_in_use)
7436 rex = rex_save;
7437
7438 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007439}
7440
7441 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007442vim_regsub_both(
7443 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007444 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007445 char_u *dest,
7446 int copy,
7447 int magic,
7448 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007449{
7450 char_u *src;
7451 char_u *dst;
7452 char_u *s;
7453 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007454 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007456 fptr_T func_all = (fptr_T)NULL;
7457 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007458 linenr_T clnum = 0; /* init for GCC */
7459 int len = 0; /* init for GCC */
7460#ifdef FEAT_EVAL
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007461 static char_u *eval_result = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463
7464 /* Be paranoid... */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007465 if ((source == NULL && expr == NULL) || dest == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007466 {
7467 EMSG(_(e_null));
7468 return 0;
7469 }
7470 if (prog_magic_wrong())
7471 return 0;
7472 src = source;
7473 dst = dest;
7474
7475 /*
7476 * When the substitute part starts with "\=" evaluate it as an expression.
7477 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007478 if (expr != NULL || (source[0] == '\\' && source[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479 {
7480#ifdef FEAT_EVAL
7481 /* To make sure that the length doesn't change between checking the
7482 * length and copying the string, and to speed up things, the
7483 * resulting string is saved from the call with "copy" == FALSE to the
7484 * call with "copy" == TRUE. */
7485 if (copy)
7486 {
7487 if (eval_result != NULL)
7488 {
7489 STRCPY(dest, eval_result);
7490 dst += STRLEN(eval_result);
7491 vim_free(eval_result);
7492 eval_result = NULL;
7493 }
7494 }
7495 else
7496 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007497 int prev_can_f_submatch = can_f_submatch;
7498 regsubmatch_T rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007499
7500 vim_free(eval_result);
7501
7502 /* The expression may contain substitute(), which calls us
7503 * recursively. Make sure submatch() gets the text from the first
Bram Moolenaar6100d022016-10-02 16:51:57 +02007504 * level. */
7505 if (can_f_submatch)
7506 rsm_save = rsm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007507 can_f_submatch = TRUE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007508 rsm.sm_match = rex.reg_match;
7509 rsm.sm_mmatch = rex.reg_mmatch;
7510 rsm.sm_firstlnum = rex.reg_firstlnum;
7511 rsm.sm_maxline = rex.reg_maxline;
7512 rsm.sm_line_lbr = rex.reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007513
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007514 if (expr != NULL)
7515 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007516 typval_T argv[2];
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007517 int dummy;
7518 char_u buf[NUMBUFLEN];
7519 typval_T rettv;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007520 staticList10_T matchList;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007521
7522 rettv.v_type = VAR_STRING;
7523 rettv.vval.v_string = NULL;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007524 argv[0].v_type = VAR_LIST;
7525 argv[0].vval.v_list = &matchList.sl_list;
7526 matchList.sl_list.lv_len = 0;
7527 if (expr->v_type == VAR_FUNC)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007528 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007529 s = expr->vval.v_string;
7530 call_func(s, (int)STRLEN(s), &rettv,
7531 1, argv, fill_submatch_list,
7532 0L, 0L, &dummy, TRUE, NULL, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007533 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007534 else if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007535 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007536 partial_T *partial = expr->vval.v_partial;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007537
Bram Moolenaar6100d022016-10-02 16:51:57 +02007538 s = partial_name(partial);
7539 call_func(s, (int)STRLEN(s), &rettv,
7540 1, argv, fill_submatch_list,
7541 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007542 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007543 if (matchList.sl_list.lv_len > 0)
7544 /* fill_submatch_list() was called */
7545 clear_submatch_list(&matchList);
7546
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007547 eval_result = get_tv_string_buf_chk(&rettv, buf);
7548 if (eval_result != NULL)
7549 eval_result = vim_strsave(eval_result);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007550 clear_tv(&rettv);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007551 }
7552 else
7553 eval_result = eval_to_string(source + 2, NULL, TRUE);
7554
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 if (eval_result != NULL)
7556 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007557 int had_backslash = FALSE;
7558
Bram Moolenaar91acfff2017-03-12 19:22:36 +01007559 for (s = eval_result; *s != NUL; MB_PTR_ADV(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007561 /* Change NL to CR, so that it becomes a line break,
7562 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 * Skip over a backslashed character. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007564 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007565 *s = CAR;
7566 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007567 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007569 /* Change NL to CR here too, so that this works:
7570 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7571 * abc\
7572 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007573 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007574 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007575 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007576 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007577 had_backslash = TRUE;
7578 }
7579 }
7580 if (had_backslash && backslash)
7581 {
7582 /* Backslashes will be consumed, need to double them. */
7583 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7584 if (s != NULL)
7585 {
7586 vim_free(eval_result);
7587 eval_result = s;
7588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589 }
7590
7591 dst += STRLEN(eval_result);
7592 }
7593
Bram Moolenaar6100d022016-10-02 16:51:57 +02007594 can_f_submatch = prev_can_f_submatch;
7595 if (can_f_submatch)
7596 rsm = rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 }
7598#endif
7599 }
7600 else
7601 while ((c = *src++) != NUL)
7602 {
7603 if (c == '&' && magic)
7604 no = 0;
7605 else if (c == '\\' && *src != NUL)
7606 {
7607 if (*src == '&' && !magic)
7608 {
7609 ++src;
7610 no = 0;
7611 }
7612 else if ('0' <= *src && *src <= '9')
7613 {
7614 no = *src++ - '0';
7615 }
7616 else if (vim_strchr((char_u *)"uUlLeE", *src))
7617 {
7618 switch (*src++)
7619 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007620 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007621 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007622 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007624 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007625 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007626 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007627 continue;
7628 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007629 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630 continue;
7631 }
7632 }
7633 }
7634 if (no < 0) /* Ordinary character. */
7635 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007636 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7637 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007638 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007639 if (copy)
7640 {
7641 *dst++ = c;
7642 *dst++ = *src++;
7643 *dst++ = *src++;
7644 }
7645 else
7646 {
7647 dst += 3;
7648 src += 2;
7649 }
7650 continue;
7651 }
7652
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653 if (c == '\\' && *src != NUL)
7654 {
7655 /* Check for abbreviations -- webb */
7656 switch (*src)
7657 {
7658 case 'r': c = CAR; ++src; break;
7659 case 'n': c = NL; ++src; break;
7660 case 't': c = TAB; ++src; break;
7661 /* Oh no! \e already has meaning in subst pat :-( */
7662 /* case 'e': c = ESC; ++src; break; */
7663 case 'b': c = Ctrl_H; ++src; break;
7664
7665 /* If "backslash" is TRUE the backslash will be removed
7666 * later. Used to insert a literal CR. */
7667 default: if (backslash)
7668 {
7669 if (copy)
7670 *dst = '\\';
7671 ++dst;
7672 }
7673 c = *src++;
7674 }
7675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007677 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007678 c = mb_ptr2char(src - 1);
7679#endif
7680
Bram Moolenaardb552d602006-03-23 22:59:57 +00007681 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007682 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007683 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007684 func_one = (fptr_T)(func_one(&cc, c));
7685 else if (func_all != (fptr_T)NULL)
7686 /* Turbo C complains without the typecast */
7687 func_all = (fptr_T)(func_all(&cc, c));
7688 else /* just copy */
7689 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007690
7691#ifdef FEAT_MBYTE
7692 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007693 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007694 int totlen = mb_ptr2len(src - 1);
7695
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007697 mb_char2bytes(cc, dst);
7698 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007699 if (enc_utf8)
7700 {
7701 int clen = utf_ptr2len(src - 1);
7702
7703 /* If the character length is shorter than "totlen", there
7704 * are composing characters; copy them as-is. */
7705 if (clen < totlen)
7706 {
7707 if (copy)
7708 mch_memmove(dst + 1, src - 1 + clen,
7709 (size_t)(totlen - clen));
7710 dst += totlen - clen;
7711 }
7712 }
7713 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714 }
7715 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716#endif
7717 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007718 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007719 dst++;
7720 }
7721 else
7722 {
7723 if (REG_MULTI)
7724 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007725 clnum = rex.reg_mmatch->startpos[no].lnum;
7726 if (clnum < 0 || rex.reg_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 s = NULL;
7728 else
7729 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007730 s = reg_getline(clnum) + rex.reg_mmatch->startpos[no].col;
7731 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7732 len = rex.reg_mmatch->endpos[no].col
7733 - rex.reg_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 else
7735 len = (int)STRLEN(s);
7736 }
7737 }
7738 else
7739 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007740 s = rex.reg_match->startp[no];
7741 if (rex.reg_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 s = NULL;
7743 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007744 len = (int)(rex.reg_match->endp[no] - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 }
7746 if (s != NULL)
7747 {
7748 for (;;)
7749 {
7750 if (len == 0)
7751 {
7752 if (REG_MULTI)
7753 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007754 if (rex.reg_mmatch->endpos[no].lnum == clnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755 break;
7756 if (copy)
7757 *dst = CAR;
7758 ++dst;
7759 s = reg_getline(++clnum);
Bram Moolenaar6100d022016-10-02 16:51:57 +02007760 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7761 len = rex.reg_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762 else
7763 len = (int)STRLEN(s);
7764 }
7765 else
7766 break;
7767 }
7768 else if (*s == NUL) /* we hit NUL. */
7769 {
7770 if (copy)
7771 EMSG(_(e_re_damg));
7772 goto exit;
7773 }
7774 else
7775 {
7776 if (backslash && (*s == CAR || *s == '\\'))
7777 {
7778 /*
7779 * Insert a backslash in front of a CR, otherwise
7780 * it will be replaced by a line break.
7781 * Number of backslashes will be halved later,
7782 * double them here.
7783 */
7784 if (copy)
7785 {
7786 dst[0] = '\\';
7787 dst[1] = *s;
7788 }
7789 dst += 2;
7790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791 else
7792 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007793#ifdef FEAT_MBYTE
7794 if (has_mbyte)
7795 c = mb_ptr2char(s);
7796 else
7797#endif
7798 c = *s;
7799
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007800 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007802 func_one = (fptr_T)(func_one(&cc, c));
7803 else if (func_all != (fptr_T)NULL)
7804 /* Turbo C complains without the typecast */
7805 func_all = (fptr_T)(func_all(&cc, c));
7806 else /* just copy */
7807 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007808
7809#ifdef FEAT_MBYTE
7810 if (has_mbyte)
7811 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007812 int l;
7813
7814 /* Copy composing characters separately, one
7815 * at a time. */
7816 if (enc_utf8)
7817 l = utf_ptr2len(s) - 1;
7818 else
7819 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007820
7821 s += l;
7822 len -= l;
7823 if (copy)
7824 mb_char2bytes(cc, dst);
7825 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007827 else
7828#endif
7829 if (copy)
7830 *dst = cc;
7831 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007833
Bram Moolenaar071d4272004-06-13 20:20:40 +00007834 ++s;
7835 --len;
7836 }
7837 }
7838 }
7839 no = -1;
7840 }
7841 }
7842 if (copy)
7843 *dst = NUL;
7844
7845exit:
7846 return (int)((dst - dest) + 1);
7847}
7848
7849#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007850static char_u *reg_getline_submatch(linenr_T lnum);
Bram Moolenaard32a3192009-11-26 19:40:49 +00007851
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007853 * Call reg_getline() with the line numbers from the submatch. If a
7854 * substitute() was used the reg_maxline and other values have been
7855 * overwritten.
7856 */
7857 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007858reg_getline_submatch(linenr_T lnum)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007859{
7860 char_u *s;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007861 linenr_T save_first = rex.reg_firstlnum;
7862 linenr_T save_max = rex.reg_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007863
Bram Moolenaar6100d022016-10-02 16:51:57 +02007864 rex.reg_firstlnum = rsm.sm_firstlnum;
7865 rex.reg_maxline = rsm.sm_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007866
7867 s = reg_getline(lnum);
7868
Bram Moolenaar6100d022016-10-02 16:51:57 +02007869 rex.reg_firstlnum = save_first;
7870 rex.reg_maxline = save_max;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007871 return s;
7872}
7873
7874/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007875 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 * allocated memory.
7877 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7878 */
7879 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007880reg_submatch(int no)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881{
7882 char_u *retval = NULL;
7883 char_u *s;
7884 int len;
7885 int round;
7886 linenr_T lnum;
7887
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007888 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 return NULL;
7890
Bram Moolenaar6100d022016-10-02 16:51:57 +02007891 if (rsm.sm_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 {
7893 /*
7894 * First round: compute the length and allocate memory.
7895 * Second round: copy the text.
7896 */
7897 for (round = 1; round <= 2; ++round)
7898 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007899 lnum = rsm.sm_mmatch->startpos[no].lnum;
7900 if (lnum < 0 || rsm.sm_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 return NULL;
7902
Bram Moolenaar6100d022016-10-02 16:51:57 +02007903 s = reg_getline_submatch(lnum) + rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007904 if (s == NULL) /* anti-crash check, cannot happen? */
7905 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007906 if (rsm.sm_mmatch->endpos[no].lnum == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907 {
7908 /* Within one line: take form start to end col. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007909 len = rsm.sm_mmatch->endpos[no].col
7910 - rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007911 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007912 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 ++len;
7914 }
7915 else
7916 {
7917 /* Multiple lines: take start line from start col, middle
7918 * lines completely and end line up to end col. */
7919 len = (int)STRLEN(s);
7920 if (round == 2)
7921 {
7922 STRCPY(retval, s);
7923 retval[len] = '\n';
7924 }
7925 ++len;
7926 ++lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007927 while (lnum < rsm.sm_mmatch->endpos[no].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007928 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007929 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 if (round == 2)
7931 STRCPY(retval + len, s);
7932 len += (int)STRLEN(s);
7933 if (round == 2)
7934 retval[len] = '\n';
7935 ++len;
7936 }
7937 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007938 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar6100d022016-10-02 16:51:57 +02007939 rsm.sm_mmatch->endpos[no].col);
7940 len += rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007941 if (round == 2)
7942 retval[len] = NUL;
7943 ++len;
7944 }
7945
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007946 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 {
7948 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007949 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 return NULL;
7951 }
7952 }
7953 }
7954 else
7955 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007956 s = rsm.sm_match->startp[no];
7957 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007958 retval = NULL;
7959 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007960 retval = vim_strnsave(s, (int)(rsm.sm_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007961 }
7962
7963 return retval;
7964}
Bram Moolenaar41571762014-04-02 19:00:58 +02007965
7966/*
7967 * Used for the submatch() function with the optional non-zero argument: get
7968 * the list of strings from the n'th submatch in allocated memory with NULs
7969 * represented in NLs.
7970 * Returns a list of allocated strings. Returns NULL when not in a ":s"
7971 * command, for a non-existing submatch and for any error.
7972 */
7973 list_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007974reg_submatch_list(int no)
Bram Moolenaar41571762014-04-02 19:00:58 +02007975{
7976 char_u *s;
7977 linenr_T slnum;
7978 linenr_T elnum;
7979 colnr_T scol;
7980 colnr_T ecol;
7981 int i;
7982 list_T *list;
7983 int error = FALSE;
7984
7985 if (!can_f_submatch || no < 0)
7986 return NULL;
7987
Bram Moolenaar6100d022016-10-02 16:51:57 +02007988 if (rsm.sm_match == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02007989 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007990 slnum = rsm.sm_mmatch->startpos[no].lnum;
7991 elnum = rsm.sm_mmatch->endpos[no].lnum;
Bram Moolenaar41571762014-04-02 19:00:58 +02007992 if (slnum < 0 || elnum < 0)
7993 return NULL;
7994
Bram Moolenaar6100d022016-10-02 16:51:57 +02007995 scol = rsm.sm_mmatch->startpos[no].col;
7996 ecol = rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar41571762014-04-02 19:00:58 +02007997
7998 list = list_alloc();
7999 if (list == NULL)
8000 return NULL;
8001
8002 s = reg_getline_submatch(slnum) + scol;
8003 if (slnum == elnum)
8004 {
8005 if (list_append_string(list, s, ecol - scol) == FAIL)
8006 error = TRUE;
8007 }
8008 else
8009 {
8010 if (list_append_string(list, s, -1) == FAIL)
8011 error = TRUE;
8012 for (i = 1; i < elnum - slnum; i++)
8013 {
8014 s = reg_getline_submatch(slnum + i);
8015 if (list_append_string(list, s, -1) == FAIL)
8016 error = TRUE;
8017 }
8018 s = reg_getline_submatch(elnum);
8019 if (list_append_string(list, s, ecol) == FAIL)
8020 error = TRUE;
8021 }
8022 }
8023 else
8024 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02008025 s = rsm.sm_match->startp[no];
8026 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008027 return NULL;
8028 list = list_alloc();
8029 if (list == NULL)
8030 return NULL;
8031 if (list_append_string(list, s,
Bram Moolenaar6100d022016-10-02 16:51:57 +02008032 (int)(rsm.sm_match->endp[no] - s)) == FAIL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008033 error = TRUE;
8034 }
8035
8036 if (error)
8037 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008038 list_free(list);
Bram Moolenaar41571762014-04-02 19:00:58 +02008039 return NULL;
8040 }
8041 return list;
8042}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008044
8045static regengine_T bt_regengine =
8046{
8047 bt_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008048 bt_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008049 bt_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008050 bt_regexec_multi,
8051 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008052};
8053
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008054#include "regexp_nfa.c"
8055
8056static regengine_T nfa_regengine =
8057{
8058 nfa_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008059 nfa_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008060 nfa_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008061 nfa_regexec_multi,
8062 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008063};
8064
8065/* Which regexp engine to use? Needed for vim_regcomp().
8066 * Must match with 'regexpengine'. */
8067static int regexp_engine = 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008068
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008069#ifdef DEBUG
8070static char_u regname[][30] = {
8071 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02008072 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008073 "NFA Regexp Engine"
8074 };
8075#endif
8076
8077/*
8078 * Compile a regular expression into internal code.
Bram Moolenaar473de612013-06-08 18:19:48 +02008079 * Returns the program in allocated memory.
8080 * Use vim_regfree() to free the memory.
8081 * Returns NULL for an error.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008082 */
8083 regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01008084vim_regcomp(char_u *expr_arg, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008085{
8086 regprog_T *prog = NULL;
8087 char_u *expr = expr_arg;
8088
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008089 regexp_engine = p_re;
8090
8091 /* Check for prefix "\%#=", that sets the regexp engine */
8092 if (STRNCMP(expr, "\\%#=", 4) == 0)
8093 {
8094 int newengine = expr[4] - '0';
8095
8096 if (newengine == AUTOMATIC_ENGINE
8097 || newengine == BACKTRACKING_ENGINE
8098 || newengine == NFA_ENGINE)
8099 {
8100 regexp_engine = expr[4] - '0';
8101 expr += 5;
8102#ifdef DEBUG
Bram Moolenaar6e132072014-05-13 16:46:32 +02008103 smsg((char_u *)"New regexp mode selected (%d): %s",
8104 regexp_engine, regname[newengine]);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008105#endif
8106 }
8107 else
8108 {
8109 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
8110 regexp_engine = AUTOMATIC_ENGINE;
8111 }
8112 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008113 bt_regengine.expr = expr;
8114 nfa_regengine.expr = expr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008115
8116 /*
8117 * First try the NFA engine, unless backtracking was requested.
8118 */
8119 if (regexp_engine != BACKTRACKING_ENGINE)
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008120 prog = nfa_regengine.regcomp(expr,
8121 re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008122 else
8123 prog = bt_regengine.regcomp(expr, re_flags);
8124
Bram Moolenaarfda37292014-11-05 14:27:36 +01008125 /* Check for error compiling regexp with initial engine. */
8126 if (prog == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008127 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008128#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008129 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
8130 {
8131 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008132 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008133 if (f)
8134 {
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008135 fprintf(f, "Syntax error in \"%s\"\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008136 fclose(f);
8137 }
8138 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008139 EMSG2("(NFA) Could not open \"%s\" to write !!!",
8140 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008141 }
8142#endif
8143 /*
Bram Moolenaarfda37292014-11-05 14:27:36 +01008144 * If the NFA engine failed, try the backtracking engine.
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008145 * The NFA engine also fails for patterns that it can't handle well
8146 * but are still valid patterns, thus a retry should work.
8147 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008148 if (regexp_engine == AUTOMATIC_ENGINE)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008149 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008150 regexp_engine = BACKTRACKING_ENGINE;
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008151 prog = bt_regengine.regcomp(expr, re_flags);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008152 }
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008153 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008154
Bram Moolenaarfda37292014-11-05 14:27:36 +01008155 if (prog != NULL)
8156 {
8157 /* Store the info needed to call regcomp() again when the engine turns
8158 * out to be very slow when executing it. */
8159 prog->re_engine = regexp_engine;
8160 prog->re_flags = re_flags;
8161 }
8162
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008163 return prog;
8164}
8165
8166/*
Bram Moolenaar473de612013-06-08 18:19:48 +02008167 * Free a compiled regexp program, returned by vim_regcomp().
8168 */
8169 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008170vim_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02008171{
8172 if (prog != NULL)
8173 prog->engine->regfree(prog);
8174}
8175
Bram Moolenaarfda37292014-11-05 14:27:36 +01008176#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008177static void report_re_switch(char_u *pat);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008178
8179 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008180report_re_switch(char_u *pat)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008181{
8182 if (p_verbose > 0)
8183 {
8184 verbose_enter();
8185 MSG_PUTS(_("Switching to backtracking RE engine for pattern: "));
8186 MSG_PUTS(pat);
8187 verbose_leave();
8188 }
8189}
8190#endif
8191
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008192static int vim_regexec_both(regmatch_T *rmp, char_u *line, colnr_T col, int nl);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008193
Bram Moolenaar473de612013-06-08 18:19:48 +02008194/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008195 * Match a regexp against a string.
8196 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008197 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008198 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaarfda37292014-11-05 14:27:36 +01008199 * When "nl" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008200 *
8201 * Return TRUE if there is a match, FALSE if not.
8202 */
Bram Moolenaarfda37292014-11-05 14:27:36 +01008203 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008204vim_regexec_both(
8205 regmatch_T *rmp,
8206 char_u *line, /* string to match against */
8207 colnr_T col, /* column to start looking for match */
8208 int nl)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008209{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008210 int result;
8211 regexec_T rex_save;
8212 int rex_in_use_save = rex_in_use;
8213
8214 if (rex_in_use)
8215 /* Being called recursively, save the state. */
8216 rex_save = rex;
8217 rex_in_use = TRUE;
8218 rex.reg_startp = NULL;
8219 rex.reg_endp = NULL;
8220 rex.reg_startpos = NULL;
8221 rex.reg_endpos = NULL;
8222
8223 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008224
8225 /* NFA engine aborted because it's very slow. */
8226 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8227 && result == NFA_TOO_EXPENSIVE)
8228 {
8229 int save_p_re = p_re;
8230 int re_flags = rmp->regprog->re_flags;
8231 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8232
8233 p_re = BACKTRACKING_ENGINE;
8234 vim_regfree(rmp->regprog);
8235 if (pat != NULL)
8236 {
8237#ifdef FEAT_EVAL
8238 report_re_switch(pat);
8239#endif
8240 rmp->regprog = vim_regcomp(pat, re_flags);
8241 if (rmp->regprog != NULL)
8242 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
8243 vim_free(pat);
8244 }
8245
8246 p_re = save_p_re;
8247 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02008248
8249 rex_in_use = rex_in_use_save;
8250 if (rex_in_use)
8251 rex = rex_save;
8252
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008253 return result > 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008254}
8255
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008256/*
8257 * Note: "*prog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008258 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008259 */
8260 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008261vim_regexec_prog(
8262 regprog_T **prog,
8263 int ignore_case,
8264 char_u *line,
8265 colnr_T col)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008266{
8267 int r;
8268 regmatch_T regmatch;
8269
8270 regmatch.regprog = *prog;
8271 regmatch.rm_ic = ignore_case;
8272 r = vim_regexec_both(&regmatch, line, col, FALSE);
8273 *prog = regmatch.regprog;
8274 return r;
8275}
8276
8277/*
8278 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008279 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008280 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008281 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008282vim_regexec(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008283{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008284 return vim_regexec_both(rmp, line, col, FALSE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008285}
8286
8287#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8288 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8289/*
8290 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008291 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008292 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008293 */
8294 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008295vim_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008296{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008297 return vim_regexec_both(rmp, line, col, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008298}
8299#endif
8300
8301/*
8302 * Match a regexp against multiple lines.
8303 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008304 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008305 * Uses curbuf for line count and 'iskeyword'.
8306 *
8307 * Return zero if there is no match. Return number of lines contained in the
8308 * match otherwise.
8309 */
8310 long
Bram Moolenaar05540972016-01-30 20:31:25 +01008311vim_regexec_multi(
8312 regmmatch_T *rmp,
8313 win_T *win, /* window in which to search or NULL */
8314 buf_T *buf, /* buffer in which to search */
8315 linenr_T lnum, /* nr of line to start looking for match */
8316 colnr_T col, /* column to start looking for match */
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008317 proftime_T *tm, /* timeout limit or NULL */
8318 int *timed_out) /* flag is set when timeout limit reached */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008319{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008320 int result;
8321 regexec_T rex_save;
8322 int rex_in_use_save = rex_in_use;
8323
8324 if (rex_in_use)
8325 /* Being called recursively, save the state. */
8326 rex_save = rex;
8327 rex_in_use = TRUE;
8328
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008329 result = rmp->regprog->engine->regexec_multi(
8330 rmp, win, buf, lnum, col, tm, timed_out);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008331
8332 /* NFA engine aborted because it's very slow. */
8333 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8334 && result == NFA_TOO_EXPENSIVE)
8335 {
8336 int save_p_re = p_re;
8337 int re_flags = rmp->regprog->re_flags;
8338 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8339
8340 p_re = BACKTRACKING_ENGINE;
8341 vim_regfree(rmp->regprog);
8342 if (pat != NULL)
8343 {
8344#ifdef FEAT_EVAL
8345 report_re_switch(pat);
8346#endif
8347 rmp->regprog = vim_regcomp(pat, re_flags);
8348 if (rmp->regprog != NULL)
8349 result = rmp->regprog->engine->regexec_multi(
Bram Moolenaarfbd0b0a2017-06-17 18:44:21 +02008350 rmp, win, buf, lnum, col, tm, timed_out);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008351 vim_free(pat);
8352 }
8353 p_re = save_p_re;
8354 }
8355
Bram Moolenaar6100d022016-10-02 16:51:57 +02008356 rex_in_use = rex_in_use_save;
8357 if (rex_in_use)
8358 rex = rex_save;
8359
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008360 return result <= 0 ? 0 : result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008361}