blob: 8d63d5a2698b95e49c6beba0abf2defafee7d855 [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 Moolenaar45eeb132005-06-06 21:59:07 +0000338#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200339#define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
340#define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL)
341#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
343#define MAX_LIMIT (32767L << 16L)
344
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100345static int re_multi_type(int);
346static int cstrncmp(char_u *s1, char_u *s2, int *n);
347static char_u *cstrchr(char_u *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200349#ifdef BT_REGEXP_DUMP
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100350static void regdump(char_u *, bt_regprog_T *);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352#ifdef DEBUG
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100353static char_u *regprop(char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354#endif
355
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100356static int re_mult_next(char *what);
Bram Moolenaarfb031402014-09-09 17:18:49 +0200357
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200358static char_u e_missingbracket[] = N_("E769: Missing ] after %s[");
359static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%(");
360static char_u e_unmatchedp[] = N_("E54: Unmatched %s(");
361static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200362#ifdef FEAT_SYN_HL
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200363static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here");
364static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200365#endif
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +0200366static char_u e_missing_sb[] = N_("E69: Missing ] after %s%%[");
Bram Moolenaar2976c022013-06-05 21:30:37 +0200367static char_u e_empty_sb[] = N_("E70: Empty %s%%[]");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368#define NOT_MULTI 0
369#define MULTI_ONE 1
370#define MULTI_MULT 2
371/*
372 * Return NOT_MULTI if c is not a "multi" operator.
373 * Return MULTI_ONE if c is a single "multi" operator.
374 * Return MULTI_MULT if c is a multi "multi" operator.
375 */
376 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100377re_multi_type(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000378{
379 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
380 return MULTI_ONE;
381 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
382 return MULTI_MULT;
383 return NOT_MULTI;
384}
385
386/*
387 * Flags to be passed up and down.
388 */
389#define HASWIDTH 0x1 /* Known never to match null string. */
390#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
391#define SPSTART 0x4 /* Starts with * or +. */
392#define HASNL 0x8 /* Contains some \n. */
393#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
394#define WORST 0 /* Worst case. */
395
396/*
397 * When regcode is set to this value, code is not emitted and size is computed
398 * instead.
399 */
400#define JUST_CALC_SIZE ((char_u *) -1)
401
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000402static char_u *reg_prev_sub = NULL;
403
Bram Moolenaar071d4272004-06-13 20:20:40 +0000404/*
405 * REGEXP_INRANGE contains all characters which are always special in a []
406 * range after '\'.
407 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
408 * These are:
409 * \n - New line (NL).
410 * \r - Carriage Return (CR).
411 * \t - Tab (TAB).
412 * \e - Escape (ESC).
413 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000414 * \d - Character code in decimal, eg \d123
415 * \o - Character code in octal, eg \o80
416 * \x - Character code in hex, eg \x4a
417 * \u - Multibyte character code, eg \u20ac
418 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419 */
420static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000421static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000422
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100423static int backslash_trans(int c);
424static int get_char_class(char_u **pp);
425static int get_equi_class(char_u **pp);
426static void reg_equi_class(int c);
427static int get_coll_element(char_u **pp);
428static char_u *skip_anyof(char_u *p);
429static void init_class_tab(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000430
431/*
432 * Translate '\x' to its control character, except "\n", which is Magic.
433 */
434 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100435backslash_trans(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436{
437 switch (c)
438 {
439 case 'r': return CAR;
440 case 't': return TAB;
441 case 'e': return ESC;
442 case 'b': return BS;
443 }
444 return c;
445}
446
447/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000448 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
450 * recognized. Otherwise "pp" is advanced to after the item.
451 */
452 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100453get_char_class(char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454{
455 static const char *(class_names[]) =
456 {
457 "alnum:]",
458#define CLASS_ALNUM 0
459 "alpha:]",
460#define CLASS_ALPHA 1
461 "blank:]",
462#define CLASS_BLANK 2
463 "cntrl:]",
464#define CLASS_CNTRL 3
465 "digit:]",
466#define CLASS_DIGIT 4
467 "graph:]",
468#define CLASS_GRAPH 5
469 "lower:]",
470#define CLASS_LOWER 6
471 "print:]",
472#define CLASS_PRINT 7
473 "punct:]",
474#define CLASS_PUNCT 8
475 "space:]",
476#define CLASS_SPACE 9
477 "upper:]",
478#define CLASS_UPPER 10
479 "xdigit:]",
480#define CLASS_XDIGIT 11
481 "tab:]",
482#define CLASS_TAB 12
483 "return:]",
484#define CLASS_RETURN 13
485 "backspace:]",
486#define CLASS_BACKSPACE 14
487 "escape:]",
488#define CLASS_ESCAPE 15
489 };
490#define CLASS_NONE 99
491 int i;
492
493 if ((*pp)[1] == ':')
494 {
Bram Moolenaar78a15312009-05-15 19:33:18 +0000495 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
497 {
498 *pp += STRLEN(class_names[i]) + 2;
499 return i;
500 }
501 }
502 return CLASS_NONE;
503}
504
505/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 * Specific version of character class functions.
507 * Using a table to keep this fast.
508 */
509static short class_tab[256];
510
511#define RI_DIGIT 0x01
512#define RI_HEX 0x02
513#define RI_OCTAL 0x04
514#define RI_WORD 0x08
515#define RI_HEAD 0x10
516#define RI_ALPHA 0x20
517#define RI_LOWER 0x40
518#define RI_UPPER 0x80
519#define RI_WHITE 0x100
520
521 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100522init_class_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523{
524 int i;
525 static int done = FALSE;
526
527 if (done)
528 return;
529
530 for (i = 0; i < 256; ++i)
531 {
532 if (i >= '0' && i <= '7')
533 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
534 else if (i >= '8' && i <= '9')
535 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
536 else if (i >= 'a' && i <= 'f')
537 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
538#ifdef EBCDIC
539 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
540 || (i >= 's' && i <= 'z'))
541#else
542 else if (i >= 'g' && i <= 'z')
543#endif
544 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
545 else if (i >= 'A' && i <= 'F')
546 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
547#ifdef EBCDIC
548 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
549 || (i >= 'S' && i <= 'Z'))
550#else
551 else if (i >= 'G' && i <= 'Z')
552#endif
553 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
554 else if (i == '_')
555 class_tab[i] = RI_WORD + RI_HEAD;
556 else
557 class_tab[i] = 0;
558 }
559 class_tab[' '] |= RI_WHITE;
560 class_tab['\t'] |= RI_WHITE;
561 done = TRUE;
562}
563
564#ifdef FEAT_MBYTE
565# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
566# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
567# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
568# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
569# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
570# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
571# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
572# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
573# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
574#else
575# define ri_digit(c) (class_tab[c] & RI_DIGIT)
576# define ri_hex(c) (class_tab[c] & RI_HEX)
577# define ri_octal(c) (class_tab[c] & RI_OCTAL)
578# define ri_word(c) (class_tab[c] & RI_WORD)
579# define ri_head(c) (class_tab[c] & RI_HEAD)
580# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
581# define ri_lower(c) (class_tab[c] & RI_LOWER)
582# define ri_upper(c) (class_tab[c] & RI_UPPER)
583# define ri_white(c) (class_tab[c] & RI_WHITE)
584#endif
585
586/* flags for regflags */
587#define RF_ICASE 1 /* ignore case */
588#define RF_NOICASE 2 /* don't ignore case */
589#define RF_HASNL 4 /* can match a NL */
590#define RF_ICOMBINE 8 /* ignore combining characters */
591#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
592
593/*
594 * Global work variables for vim_regcomp().
595 */
596
597static char_u *regparse; /* Input-scan pointer. */
598static int prevchr_len; /* byte length of previous char */
599static int num_complex_braces; /* Complex \{...} count */
600static int regnpar; /* () count. */
601#ifdef FEAT_SYN_HL
602static int regnzpar; /* \z() count. */
603static int re_has_z; /* \z item detected */
604#endif
605static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
606static long regsize; /* Code size. */
Bram Moolenaard3005802009-11-25 17:21:32 +0000607static int reg_toolong; /* TRUE when offset out of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
609static unsigned regflags; /* RF_ flags for prog */
610static long brace_min[10]; /* Minimums for complex brace repeats */
611static long brace_max[10]; /* Maximums for complex brace repeats */
612static int brace_count[10]; /* Current counts for complex brace repeats */
613#if defined(FEAT_SYN_HL) || defined(PROTO)
614static int had_eol; /* TRUE when EOL found by vim_regcomp() */
615#endif
616static int one_exactly = FALSE; /* only do one char for EXACTLY */
617
618static int reg_magic; /* magicness of the pattern: */
619#define MAGIC_NONE 1 /* "\V" very unmagic */
620#define MAGIC_OFF 2 /* "\M" or 'magic' off */
621#define MAGIC_ON 3 /* "\m" or 'magic' */
622#define MAGIC_ALL 4 /* "\v" very magic */
623
624static int reg_string; /* matching with a string instead of a buffer
625 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000626static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627
628/*
629 * META contains all characters that may be magic, except '^' and '$'.
630 */
631
632#ifdef EBCDIC
633static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
634#else
635/* META[] is used often enough to justify turning it into a table. */
636static char_u META_flags[] = {
637 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
638 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
639/* % & ( ) * + . */
640 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
641/* 1 2 3 4 5 6 7 8 9 < = > ? */
642 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
643/* @ A C D F H I K L M O */
644 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
645/* P S U V W X Z [ _ */
646 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
647/* a c d f h i k l m n o */
648 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
649/* p s u v w x z { | ~ */
650 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
651};
652#endif
653
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200654static int curchr; /* currently parsed character */
655/* Previous character. Note: prevchr is sometimes -1 when we are not at the
656 * start, eg in /[ ^I]^ the pattern was never found even if it existed,
657 * because ^ was taken to be magic -- webb */
658static int prevchr;
659static int prevprevchr; /* previous-previous character */
660static int nextchr; /* used for ungetchr() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661
662/* arguments for reg() */
663#define REG_NOPAREN 0 /* toplevel reg() */
664#define REG_PAREN 1 /* \(\) */
665#define REG_ZPAREN 2 /* \z(\) */
666#define REG_NPAREN 3 /* \%(\) */
667
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200668typedef struct
669{
670 char_u *regparse;
671 int prevchr_len;
672 int curchr;
673 int prevchr;
674 int prevprevchr;
675 int nextchr;
676 int at_start;
677 int prev_at_start;
678 int regnpar;
679} parse_state_T;
680
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681/*
682 * Forward declarations for vim_regcomp()'s friends.
683 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100684static void initchr(char_u *);
685static void save_parse_state(parse_state_T *ps);
686static void restore_parse_state(parse_state_T *ps);
687static int getchr(void);
688static void skipchr_keepstart(void);
689static int peekchr(void);
690static void skipchr(void);
691static void ungetchr(void);
692static int gethexchrs(int maxinputlen);
693static int getoctchrs(void);
694static int getdecchrs(void);
695static int coll_get_char(void);
696static void regcomp_start(char_u *expr, int flags);
697static char_u *reg(int, int *);
698static char_u *regbranch(int *flagp);
699static char_u *regconcat(int *flagp);
700static char_u *regpiece(int *);
701static char_u *regatom(int *);
702static char_u *regnode(int);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000703#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100704static int use_multibytecode(int c);
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000705#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100706static int prog_magic_wrong(void);
707static char_u *regnext(char_u *);
708static void regc(int b);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100710static void regmbc(int c);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200711# define REGMBC(x) regmbc(x);
712# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000713#else
714# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200715# define REGMBC(x)
716# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +0100718static void reginsert(int, char_u *);
719static void reginsert_nr(int op, long val, char_u *opnd);
720static void reginsert_limits(int, long, long, char_u *);
721static char_u *re_put_long(char_u *pr, long_u val);
722static int read_limits(long *, long *);
723static void regtail(char_u *, char_u *);
724static void regoptail(char_u *, char_u *);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200726static regengine_T bt_regengine;
727static regengine_T nfa_regengine;
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729/*
730 * Return TRUE if compiled regular expression "prog" can match a line break.
731 */
732 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100733re_multiline(regprog_T *prog)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734{
735 return (prog->regflags & RF_HASNL);
736}
737
738/*
739 * Return TRUE if compiled regular expression "prog" looks before the start
740 * position (pattern contains "\@<=" or "\@<!").
741 */
742 int
Bram Moolenaar05540972016-01-30 20:31:25 +0100743re_lookbehind(regprog_T *prog)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744{
745 return (prog->regflags & RF_LOOKBH);
746}
747
748/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000749 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
750 * Returns a character representing the class. Zero means that no item was
751 * recognized. Otherwise "pp" is advanced to after the item.
752 */
753 static int
Bram Moolenaar05540972016-01-30 20:31:25 +0100754get_equi_class(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000755{
756 int c;
757 int l = 1;
758 char_u *p = *pp;
759
760 if (p[1] == '=')
761 {
762#ifdef FEAT_MBYTE
763 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000764 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000765#endif
766 if (p[l + 2] == '=' && p[l + 3] == ']')
767 {
768#ifdef FEAT_MBYTE
769 if (has_mbyte)
770 c = mb_ptr2char(p + 2);
771 else
772#endif
773 c = p[2];
774 *pp += l + 4;
775 return c;
776 }
777 }
778 return 0;
779}
780
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200781#ifdef EBCDIC
782/*
783 * Table for equivalence class "c". (IBM-1047)
784 */
785char *EQUIVAL_CLASS_C[16] = {
786 "A\x62\x63\x64\x65\x66\x67",
787 "C\x68",
788 "E\x71\x72\x73\x74",
789 "I\x75\x76\x77\x78",
790 "N\x69",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200791 "O\xEB\xEC\xED\xEE\xEF\x80",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200792 "U\xFB\xFC\xFD\xFE",
793 "Y\xBA",
794 "a\x42\x43\x44\x45\x46\x47",
795 "c\x48",
796 "e\x51\x52\x53\x54",
797 "i\x55\x56\x57\x58",
798 "n\x49",
Bram Moolenaar22e42152016-04-03 14:02:02 +0200799 "o\xCB\xCC\xCD\xCE\xCF\x70",
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200800 "u\xDB\xDC\xDD\xDE",
801 "y\x8D\xDF",
802};
803#endif
804
Bram Moolenaardf177f62005-02-22 08:39:57 +0000805/*
806 * Produce the bytes for equivalence class "c".
807 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200808 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000809 */
810 static void
Bram Moolenaar05540972016-01-30 20:31:25 +0100811reg_equi_class(int c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000812{
813#ifdef FEAT_MBYTE
814 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000815 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000816#endif
817 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200818#ifdef EBCDIC
819 int i;
820
821 /* This might be slower than switch/case below. */
822 for (i = 0; i < 16; i++)
823 {
824 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
825 {
826 char *p = EQUIVAL_CLASS_C[i];
827
828 while (*p != 0)
829 regmbc(*p++);
830 return;
831 }
832 }
833#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000834 switch (c)
835 {
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200836 /* Do not use '\300' style, it results in a negative number. */
837 case 'A': case 0xc0: case 0xc1: case 0xc2:
838 case 0xc3: case 0xc4: case 0xc5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200839 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
840 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200841 regmbc('A'); regmbc(0xc0); regmbc(0xc1);
842 regmbc(0xc2); regmbc(0xc3); regmbc(0xc4);
843 regmbc(0xc5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200844 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
845 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
846 REGMBC(0x1ea2)
847 return;
848 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
849 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000850 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200851 case 'C': case 0xc7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200852 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200853 regmbc('C'); regmbc(0xc7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200854 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
855 REGMBC(0x10c)
856 return;
857 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
858 CASEMBC(0x1e0e) CASEMBC(0x1e10)
859 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
860 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000861 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200862 case 'E': case 0xc8: case 0xc9: case 0xca: case 0xcb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200863 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
864 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200865 regmbc('E'); regmbc(0xc8); regmbc(0xc9);
866 regmbc(0xca); regmbc(0xcb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200867 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
868 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
869 REGMBC(0x1ebc)
870 return;
871 case 'F': CASEMBC(0x1e1e)
872 regmbc('F'); REGMBC(0x1e1e)
873 return;
874 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
875 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
876 CASEMBC(0x1e20)
877 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
878 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
879 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
880 return;
881 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
882 CASEMBC(0x1e26) CASEMBC(0x1e28)
883 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
884 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000885 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200886 case 'I': case 0xcc: case 0xcd: case 0xce: case 0xcf:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200887 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
888 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200889 regmbc('I'); regmbc(0xcc); regmbc(0xcd);
890 regmbc(0xce); regmbc(0xcf);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200891 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
892 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
893 REGMBC(0x1ec8)
894 return;
895 case 'J': CASEMBC(0x134)
896 regmbc('J'); REGMBC(0x134)
897 return;
898 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
899 CASEMBC(0x1e34)
900 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
901 REGMBC(0x1e30) REGMBC(0x1e34)
902 return;
903 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
904 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
905 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
906 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
907 REGMBC(0x1e3a)
908 return;
909 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
910 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000911 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200912 case 'N': case 0xd1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200913 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
914 CASEMBC(0x1e48)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200915 regmbc('N'); regmbc(0xd1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200916 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
917 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000918 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200919 case 'O': case 0xd2: case 0xd3: case 0xd4: case 0xd5:
920 case 0xd6: case 0xd8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200921 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
922 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200923 regmbc('O'); regmbc(0xd2); regmbc(0xd3);
924 regmbc(0xd4); regmbc(0xd5); regmbc(0xd6);
925 regmbc(0xd8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200926 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
927 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
928 REGMBC(0x1ec) REGMBC(0x1ece)
929 return;
930 case 'P': case 0x1e54: case 0x1e56:
931 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
932 return;
933 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
934 CASEMBC(0x1e58) CASEMBC(0x1e5e)
935 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
936 REGMBC(0x1e58) REGMBC(0x1e5e)
937 return;
938 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
939 CASEMBC(0x160) CASEMBC(0x1e60)
940 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
941 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
942 return;
943 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
944 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
945 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
946 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000947 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200948 case 'U': case 0xd9: case 0xda: case 0xdb: case 0xdc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200949 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
950 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
951 CASEMBC(0x1ee6)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200952 regmbc('U'); regmbc(0xd9); regmbc(0xda);
953 regmbc(0xdb); regmbc(0xdc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200954 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
955 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
956 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
957 return;
958 case 'V': CASEMBC(0x1e7c)
959 regmbc('V'); REGMBC(0x1e7c)
960 return;
961 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
962 CASEMBC(0x1e84) CASEMBC(0x1e86)
963 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
964 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
965 return;
966 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
967 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000968 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200969 case 'Y': case 0xdd:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200970 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
971 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200972 regmbc('Y'); regmbc(0xdd);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200973 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
974 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
975 return;
976 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
977 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
978 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
979 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
980 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000981 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200982 case 'a': case 0xe0: case 0xe1: case 0xe2:
983 case 0xe3: case 0xe4: case 0xe5:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200984 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
985 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200986 regmbc('a'); regmbc(0xe0); regmbc(0xe1);
987 regmbc(0xe2); regmbc(0xe3); regmbc(0xe4);
988 regmbc(0xe5);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200989 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
990 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
991 REGMBC(0x1ea3)
992 return;
993 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
994 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000995 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200996 case 'c': case 0xe7:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200997 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaard82a2a92015-04-21 14:02:35 +0200998 regmbc('c'); regmbc(0xe7);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200999 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
1000 REGMBC(0x10d)
1001 return;
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001002 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1e0b)
1003 CASEMBC(0x1e0f) CASEMBC(0x1e11)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001004 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
Bram Moolenaar2c61ec62015-07-10 19:16:34 +02001005 REGMBC(0x1e0b) REGMBC(0x1e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001006 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001007 case 'e': case 0xe8: case 0xe9: case 0xea: case 0xeb:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001008 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
1009 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001010 regmbc('e'); regmbc(0xe8); regmbc(0xe9);
1011 regmbc(0xea); regmbc(0xeb);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001012 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1013 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1014 REGMBC(0x1ebd)
1015 return;
1016 case 'f': CASEMBC(0x1e1f)
1017 regmbc('f'); REGMBC(0x1e1f)
1018 return;
1019 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1020 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1021 CASEMBC(0x1e21)
1022 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1023 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1024 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1025 return;
1026 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1027 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1028 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1029 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1030 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001031 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001032 case 'i': case 0xec: case 0xed: case 0xee: case 0xef:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001033 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1034 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001035 regmbc('i'); regmbc(0xec); regmbc(0xed);
1036 regmbc(0xee); regmbc(0xef);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001037 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1038 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1039 return;
1040 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1041 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1042 return;
1043 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1044 CASEMBC(0x1e35)
1045 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1046 REGMBC(0x1e31) REGMBC(0x1e35)
1047 return;
1048 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1049 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1050 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1051 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1052 REGMBC(0x1e3b)
1053 return;
1054 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1055 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001056 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001057 case 'n': case 0xf1:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001058 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1059 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001060 regmbc('n'); regmbc(0xf1);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001061 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1062 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001063 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001064 case 'o': case 0xf2: case 0xf3: case 0xf4: case 0xf5:
1065 case 0xf6: case 0xf8:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001066 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1067 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001068 regmbc('o'); regmbc(0xf2); regmbc(0xf3);
1069 regmbc(0xf4); regmbc(0xf5); regmbc(0xf6);
1070 regmbc(0xf8);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001071 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1072 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1073 REGMBC(0x1ed) REGMBC(0x1ecf)
1074 return;
1075 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1076 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1077 return;
1078 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1079 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1080 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1081 REGMBC(0x1e59) REGMBC(0x1e5f)
1082 return;
1083 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1084 CASEMBC(0x161) CASEMBC(0x1e61)
1085 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1086 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1087 return;
1088 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1089 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1090 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1091 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001092 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001093 case 'u': case 0xf9: case 0xfa: case 0xfb: case 0xfc:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001094 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1095 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1096 CASEMBC(0x1ee7)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001097 regmbc('u'); regmbc(0xf9); regmbc(0xfa);
1098 regmbc(0xfb); regmbc(0xfc);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001099 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1100 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1101 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1102 return;
1103 case 'v': CASEMBC(0x1e7d)
1104 regmbc('v'); REGMBC(0x1e7d)
1105 return;
1106 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1107 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1108 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1109 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1110 REGMBC(0x1e98)
1111 return;
1112 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1113 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001114 return;
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001115 case 'y': case 0xfd: case 0xff:
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001116 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1117 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaard82a2a92015-04-21 14:02:35 +02001118 regmbc('y'); regmbc(0xfd); regmbc(0xff);
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001119 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1120 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1121 return;
1122 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1123 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1124 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1125 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1126 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001127 return;
1128 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001129#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001130 }
1131 regmbc(c);
1132}
1133
1134/*
1135 * Check for a collating element "[.a.]". "pp" points to the '['.
1136 * Returns a character. Zero means that no item was recognized. Otherwise
1137 * "pp" is advanced to after the item.
1138 * Currently only single characters are recognized!
1139 */
1140 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01001141get_coll_element(char_u **pp)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001142{
1143 int c;
1144 int l = 1;
1145 char_u *p = *pp;
1146
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001147 if (p[0] != NUL && p[1] == '.')
Bram Moolenaardf177f62005-02-22 08:39:57 +00001148 {
1149#ifdef FEAT_MBYTE
1150 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001151 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001152#endif
1153 if (p[l + 2] == '.' && p[l + 3] == ']')
1154 {
1155#ifdef FEAT_MBYTE
1156 if (has_mbyte)
1157 c = mb_ptr2char(p + 2);
1158 else
1159#endif
1160 c = p[2];
1161 *pp += l + 4;
1162 return c;
1163 }
1164 }
1165 return 0;
1166}
1167
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001168static void get_cpo_flags(void);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001169static int reg_cpo_lit; /* 'cpoptions' contains 'l' flag */
1170static int reg_cpo_bsl; /* 'cpoptions' contains '\' flag */
1171
1172 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001173get_cpo_flags(void)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001174{
1175 reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1176 reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
1177}
Bram Moolenaardf177f62005-02-22 08:39:57 +00001178
1179/*
1180 * Skip over a "[]" range.
1181 * "p" must point to the character after the '['.
1182 * The returned pointer is on the matching ']', or the terminating NUL.
1183 */
1184 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001185skip_anyof(char_u *p)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001186{
Bram Moolenaardf177f62005-02-22 08:39:57 +00001187#ifdef FEAT_MBYTE
1188 int l;
1189#endif
1190
Bram Moolenaardf177f62005-02-22 08:39:57 +00001191 if (*p == '^') /* Complement of range. */
1192 ++p;
1193 if (*p == ']' || *p == '-')
1194 ++p;
1195 while (*p != NUL && *p != ']')
1196 {
1197#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001198 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001199 p += l;
1200 else
1201#endif
1202 if (*p == '-')
1203 {
1204 ++p;
1205 if (*p != ']' && *p != NUL)
1206 mb_ptr_adv(p);
1207 }
1208 else if (*p == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001209 && !reg_cpo_bsl
Bram Moolenaardf177f62005-02-22 08:39:57 +00001210 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001211 || (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001212 p += 2;
1213 else if (*p == '[')
1214 {
1215 if (get_char_class(&p) == CLASS_NONE
1216 && get_equi_class(&p) == 0
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02001217 && get_coll_element(&p) == 0
1218 && *p != NUL)
1219 ++p; /* it is not a class name and not NUL */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001220 }
1221 else
1222 ++p;
1223 }
1224
1225 return p;
1226}
1227
1228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001230 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 * Take care of characters with a backslash in front of it.
1232 * Skip strings inside [ and ].
1233 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1234 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1235 * is changed in-place.
1236 */
1237 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001238skip_regexp(
1239 char_u *startp,
1240 int dirc,
1241 int magic,
1242 char_u **newp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243{
1244 int mymagic;
1245 char_u *p = startp;
1246
1247 if (magic)
1248 mymagic = MAGIC_ON;
1249 else
1250 mymagic = MAGIC_OFF;
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001251 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001253 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 {
1255 if (p[0] == dirc) /* found end of regexp */
1256 break;
1257 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1258 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1259 {
1260 p = skip_anyof(p + 1);
1261 if (p[0] == NUL)
1262 break;
1263 }
1264 else if (p[0] == '\\' && p[1] != NUL)
1265 {
1266 if (dirc == '?' && newp != NULL && p[1] == '?')
1267 {
1268 /* change "\?" to "?", make a copy first. */
1269 if (*newp == NULL)
1270 {
1271 *newp = vim_strsave(startp);
1272 if (*newp != NULL)
1273 p = *newp + (p - startp);
1274 }
1275 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001276 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 else
1278 ++p;
1279 }
1280 else
1281 ++p; /* skip next character */
1282 if (*p == 'v')
1283 mymagic = MAGIC_ALL;
1284 else if (*p == 'V')
1285 mymagic = MAGIC_NONE;
1286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 }
1288 return p;
1289}
1290
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01001291static regprog_T *bt_regcomp(char_u *expr, int re_flags);
1292static void bt_regfree(regprog_T *prog);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001293
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001295 * bt_regcomp() - compile a regular expression into internal code for the
1296 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001297 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 *
1299 * We can't allocate space until we know how big the compiled form will be,
1300 * but we can't compile it (and thus know how big it is) until we've got a
1301 * place to put the code. So we cheat: we compile it twice, once with code
1302 * generation turned off and size counting turned on, and once "for real".
1303 * This also means that we don't allocate space until we are sure that the
1304 * thing really will compile successfully, and we never have to move the
1305 * code and thus invalidate pointers into it. (Note that it has to be in
1306 * one piece because vim_free() must be able to free it all.)
1307 *
1308 * Whether upper/lower case is to be ignored is decided when executing the
1309 * program, it does not matter here.
1310 *
1311 * Beware that the optimization-preparation code in here knows about some
1312 * of the structure of the compiled regexp.
1313 * "re_flags": RE_MAGIC and/or RE_STRING.
1314 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001315 static regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01001316bt_regcomp(char_u *expr, int re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001318 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 char_u *scan;
1320 char_u *longest;
1321 int len;
1322 int flags;
1323
1324 if (expr == NULL)
1325 EMSG_RET_NULL(_(e_null));
1326
1327 init_class_tab();
1328
1329 /*
1330 * First pass: determine size, legality.
1331 */
1332 regcomp_start(expr, re_flags);
1333 regcode = JUST_CALC_SIZE;
1334 regc(REGMAGIC);
1335 if (reg(REG_NOPAREN, &flags) == NULL)
1336 return NULL;
1337
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001339 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 if (r == NULL)
1341 return NULL;
1342
1343 /*
1344 * Second pass: emit code.
1345 */
1346 regcomp_start(expr, re_flags);
1347 regcode = r->program;
1348 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001349 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 {
1351 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001352 if (reg_toolong)
1353 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001354 return NULL;
1355 }
1356
1357 /* Dig out information for optimizations. */
1358 r->regstart = NUL; /* Worst-case defaults. */
1359 r->reganch = 0;
1360 r->regmust = NULL;
1361 r->regmlen = 0;
1362 r->regflags = regflags;
1363 if (flags & HASNL)
1364 r->regflags |= RF_HASNL;
1365 if (flags & HASLOOKBH)
1366 r->regflags |= RF_LOOKBH;
1367#ifdef FEAT_SYN_HL
1368 /* Remember whether this pattern has any \z specials in it. */
1369 r->reghasz = re_has_z;
1370#endif
1371 scan = r->program + 1; /* First BRANCH. */
1372 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1373 {
1374 scan = OPERAND(scan);
1375
1376 /* Starting-point info. */
1377 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1378 {
1379 r->reganch++;
1380 scan = regnext(scan);
1381 }
1382
1383 if (OP(scan) == EXACTLY)
1384 {
1385#ifdef FEAT_MBYTE
1386 if (has_mbyte)
1387 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1388 else
1389#endif
1390 r->regstart = *OPERAND(scan);
1391 }
1392 else if ((OP(scan) == BOW
1393 || OP(scan) == EOW
1394 || OP(scan) == NOTHING
1395 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1396 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1397 && OP(regnext(scan)) == EXACTLY)
1398 {
1399#ifdef FEAT_MBYTE
1400 if (has_mbyte)
1401 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1402 else
1403#endif
1404 r->regstart = *OPERAND(regnext(scan));
1405 }
1406
1407 /*
1408 * If there's something expensive in the r.e., find the longest
1409 * literal string that must appear and make it the regmust. Resolve
1410 * ties in favor of later strings, since the regstart check works
1411 * with the beginning of the r.e. and avoiding duplication
1412 * strengthens checking. Not a strong reason, but sufficient in the
1413 * absence of others.
1414 */
1415 /*
1416 * When the r.e. starts with BOW, it is faster to look for a regmust
1417 * first. Used a lot for "#" and "*" commands. (Added by mool).
1418 */
1419 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1420 && !(flags & HASNL))
1421 {
1422 longest = NULL;
1423 len = 0;
1424 for (; scan != NULL; scan = regnext(scan))
1425 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1426 {
1427 longest = OPERAND(scan);
1428 len = (int)STRLEN(OPERAND(scan));
1429 }
1430 r->regmust = longest;
1431 r->regmlen = len;
1432 }
1433 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001434#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 regdump(expr, r);
1436#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001437 r->engine = &bt_regengine;
1438 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001439}
1440
1441/*
Bram Moolenaar473de612013-06-08 18:19:48 +02001442 * Free a compiled regexp program, returned by bt_regcomp().
1443 */
1444 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001445bt_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02001446{
1447 vim_free(prog);
1448}
1449
1450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 * Setup to parse the regexp. Used once to get the length and once to do it.
1452 */
1453 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01001454regcomp_start(
1455 char_u *expr,
1456 int re_flags) /* see vim_regcomp() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457{
1458 initchr(expr);
1459 if (re_flags & RE_MAGIC)
1460 reg_magic = MAGIC_ON;
1461 else
1462 reg_magic = MAGIC_OFF;
1463 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001464 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001465 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466
1467 num_complex_braces = 0;
1468 regnpar = 1;
1469 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1470#ifdef FEAT_SYN_HL
1471 regnzpar = 1;
1472 re_has_z = 0;
1473#endif
1474 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001475 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 regflags = 0;
1477#if defined(FEAT_SYN_HL) || defined(PROTO)
1478 had_eol = FALSE;
1479#endif
1480}
1481
1482#if defined(FEAT_SYN_HL) || defined(PROTO)
1483/*
1484 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1485 * found. This is messy, but it works fine.
1486 */
1487 int
Bram Moolenaar05540972016-01-30 20:31:25 +01001488vim_regcomp_had_eol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489{
1490 return had_eol;
1491}
1492#endif
1493
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001494/* variables for parsing reginput */
1495static int at_start; /* True when on the first character */
1496static int prev_at_start; /* True when on the second character */
1497
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001499 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 *
1501 * Caller must absorb opening parenthesis.
1502 *
1503 * Combining parenthesis handling with the base level of regular expression
1504 * is a trifle forced, but the need to tie the tails of the branches to what
1505 * follows makes it hard to avoid.
1506 */
1507 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001508reg(
1509 int paren, /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1510 int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511{
1512 char_u *ret;
1513 char_u *br;
1514 char_u *ender;
1515 int parno = 0;
1516 int flags;
1517
1518 *flagp = HASWIDTH; /* Tentatively. */
1519
1520#ifdef FEAT_SYN_HL
1521 if (paren == REG_ZPAREN)
1522 {
1523 /* Make a ZOPEN node. */
1524 if (regnzpar >= NSUBEXP)
1525 EMSG_RET_NULL(_("E50: Too many \\z("));
1526 parno = regnzpar;
1527 regnzpar++;
1528 ret = regnode(ZOPEN + parno);
1529 }
1530 else
1531#endif
1532 if (paren == REG_PAREN)
1533 {
1534 /* Make a MOPEN node. */
1535 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001536 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 parno = regnpar;
1538 ++regnpar;
1539 ret = regnode(MOPEN + parno);
1540 }
1541 else if (paren == REG_NPAREN)
1542 {
1543 /* Make a NOPEN node. */
1544 ret = regnode(NOPEN);
1545 }
1546 else
1547 ret = NULL;
1548
1549 /* Pick up the branches, linking them together. */
1550 br = regbranch(&flags);
1551 if (br == NULL)
1552 return NULL;
1553 if (ret != NULL)
1554 regtail(ret, br); /* [MZ]OPEN -> first. */
1555 else
1556 ret = br;
1557 /* If one of the branches can be zero-width, the whole thing can.
1558 * If one of the branches has * at start or matches a line-break, the
1559 * whole thing can. */
1560 if (!(flags & HASWIDTH))
1561 *flagp &= ~HASWIDTH;
1562 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1563 while (peekchr() == Magic('|'))
1564 {
1565 skipchr();
1566 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001567 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 return NULL;
1569 regtail(ret, br); /* BRANCH -> BRANCH. */
1570 if (!(flags & HASWIDTH))
1571 *flagp &= ~HASWIDTH;
1572 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1573 }
1574
1575 /* Make a closing node, and hook it on the end. */
1576 ender = regnode(
1577#ifdef FEAT_SYN_HL
1578 paren == REG_ZPAREN ? ZCLOSE + parno :
1579#endif
1580 paren == REG_PAREN ? MCLOSE + parno :
1581 paren == REG_NPAREN ? NCLOSE : END);
1582 regtail(ret, ender);
1583
1584 /* Hook the tails of the branches to the closing node. */
1585 for (br = ret; br != NULL; br = regnext(br))
1586 regoptail(br, ender);
1587
1588 /* Check for proper termination. */
1589 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1590 {
1591#ifdef FEAT_SYN_HL
1592 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001593 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 else
1595#endif
1596 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001597 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001599 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 }
1601 else if (paren == REG_NOPAREN && peekchr() != NUL)
1602 {
1603 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001604 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001606 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001607 /* NOTREACHED */
1608 }
1609 /*
1610 * Here we set the flag allowing back references to this set of
1611 * parentheses.
1612 */
1613 if (paren == REG_PAREN)
1614 had_endbrace[parno] = TRUE; /* have seen the close paren */
1615 return ret;
1616}
1617
1618/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 * Implements the & operator.
1621 */
1622 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001623regbranch(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624{
1625 char_u *ret;
1626 char_u *chain = NULL;
1627 char_u *latest;
1628 int flags;
1629
1630 *flagp = WORST | HASNL; /* Tentatively. */
1631
1632 ret = regnode(BRANCH);
1633 for (;;)
1634 {
1635 latest = regconcat(&flags);
1636 if (latest == NULL)
1637 return NULL;
1638 /* If one of the branches has width, the whole thing has. If one of
1639 * the branches anchors at start-of-line, the whole thing does.
1640 * If one of the branches uses look-behind, the whole thing does. */
1641 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1642 /* If one of the branches doesn't match a line-break, the whole thing
1643 * doesn't. */
1644 *flagp &= ~HASNL | (flags & HASNL);
1645 if (chain != NULL)
1646 regtail(chain, latest);
1647 if (peekchr() != Magic('&'))
1648 break;
1649 skipchr();
1650 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001651 if (reg_toolong)
1652 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 reginsert(MATCH, latest);
1654 chain = latest;
1655 }
1656
1657 return ret;
1658}
1659
1660/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001661 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 * Implements the concatenation operator.
1663 */
1664 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001665regconcat(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666{
1667 char_u *first = NULL;
1668 char_u *chain = NULL;
1669 char_u *latest;
1670 int flags;
1671 int cont = TRUE;
1672
1673 *flagp = WORST; /* Tentatively. */
1674
1675 while (cont)
1676 {
1677 switch (peekchr())
1678 {
1679 case NUL:
1680 case Magic('|'):
1681 case Magic('&'):
1682 case Magic(')'):
1683 cont = FALSE;
1684 break;
1685 case Magic('Z'):
1686#ifdef FEAT_MBYTE
1687 regflags |= RF_ICOMBINE;
1688#endif
1689 skipchr_keepstart();
1690 break;
1691 case Magic('c'):
1692 regflags |= RF_ICASE;
1693 skipchr_keepstart();
1694 break;
1695 case Magic('C'):
1696 regflags |= RF_NOICASE;
1697 skipchr_keepstart();
1698 break;
1699 case Magic('v'):
1700 reg_magic = MAGIC_ALL;
1701 skipchr_keepstart();
1702 curchr = -1;
1703 break;
1704 case Magic('m'):
1705 reg_magic = MAGIC_ON;
1706 skipchr_keepstart();
1707 curchr = -1;
1708 break;
1709 case Magic('M'):
1710 reg_magic = MAGIC_OFF;
1711 skipchr_keepstart();
1712 curchr = -1;
1713 break;
1714 case Magic('V'):
1715 reg_magic = MAGIC_NONE;
1716 skipchr_keepstart();
1717 curchr = -1;
1718 break;
1719 default:
1720 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001721 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 return NULL;
1723 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1724 if (chain == NULL) /* First piece. */
1725 *flagp |= flags & SPSTART;
1726 else
1727 regtail(chain, latest);
1728 chain = latest;
1729 if (first == NULL)
1730 first = latest;
1731 break;
1732 }
1733 }
1734 if (first == NULL) /* Loop ran zero times. */
1735 first = regnode(NOTHING);
1736 return first;
1737}
1738
1739/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001740 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741 *
1742 * Note that the branching code sequences used for = and the general cases
1743 * of * and + are somewhat optimized: they use the same NOTHING node as
1744 * both the endmarker for their branch list and the body of the last branch.
1745 * It might seem that this node could be dispensed with entirely, but the
1746 * endmarker role is not redundant.
1747 */
1748 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001749regpiece(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750{
1751 char_u *ret;
1752 int op;
1753 char_u *next;
1754 int flags;
1755 long minval;
1756 long maxval;
1757
1758 ret = regatom(&flags);
1759 if (ret == NULL)
1760 return NULL;
1761
1762 op = peekchr();
1763 if (re_multi_type(op) == NOT_MULTI)
1764 {
1765 *flagp = flags;
1766 return ret;
1767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 /* default flags */
1769 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1770
1771 skipchr();
1772 switch (op)
1773 {
1774 case Magic('*'):
1775 if (flags & SIMPLE)
1776 reginsert(STAR, ret);
1777 else
1778 {
1779 /* Emit x* as (x&|), where & means "self". */
1780 reginsert(BRANCH, ret); /* Either x */
1781 regoptail(ret, regnode(BACK)); /* and loop */
1782 regoptail(ret, ret); /* back */
1783 regtail(ret, regnode(BRANCH)); /* or */
1784 regtail(ret, regnode(NOTHING)); /* null. */
1785 }
1786 break;
1787
1788 case Magic('+'):
1789 if (flags & SIMPLE)
1790 reginsert(PLUS, ret);
1791 else
1792 {
1793 /* Emit x+ as x(&|), where & means "self". */
1794 next = regnode(BRANCH); /* Either */
1795 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001796 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 regtail(next, regnode(BRANCH)); /* or */
1798 regtail(ret, regnode(NOTHING)); /* null. */
1799 }
1800 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1801 break;
1802
1803 case Magic('@'):
1804 {
1805 int lop = END;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001806 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001808 nr = getdecchrs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 switch (no_Magic(getchr()))
1810 {
1811 case '=': lop = MATCH; break; /* \@= */
1812 case '!': lop = NOMATCH; break; /* \@! */
1813 case '>': lop = SUBPAT; break; /* \@> */
1814 case '<': switch (no_Magic(getchr()))
1815 {
1816 case '=': lop = BEHIND; break; /* \@<= */
1817 case '!': lop = NOBEHIND; break; /* \@<! */
1818 }
1819 }
1820 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001821 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 reg_magic == MAGIC_ALL);
1823 /* Look behind must match with behind_pos. */
1824 if (lop == BEHIND || lop == NOBEHIND)
1825 {
1826 regtail(ret, regnode(BHPOS));
1827 *flagp |= HASLOOKBH;
1828 }
1829 regtail(ret, regnode(END)); /* operand ends */
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001830 if (lop == BEHIND || lop == NOBEHIND)
1831 {
1832 if (nr < 0)
1833 nr = 0; /* no limit is same as zero limit */
1834 reginsert_nr(lop, nr, ret);
1835 }
1836 else
1837 reginsert(lop, ret);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001838 break;
1839 }
1840
1841 case Magic('?'):
1842 case Magic('='):
1843 /* Emit x= as (x|) */
1844 reginsert(BRANCH, ret); /* Either x */
1845 regtail(ret, regnode(BRANCH)); /* or */
1846 next = regnode(NOTHING); /* null. */
1847 regtail(ret, next);
1848 regoptail(ret, next);
1849 break;
1850
1851 case Magic('{'):
1852 if (!read_limits(&minval, &maxval))
1853 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 if (flags & SIMPLE)
1855 {
1856 reginsert(BRACE_SIMPLE, ret);
1857 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1858 }
1859 else
1860 {
1861 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001862 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001863 reg_magic == MAGIC_ALL);
1864 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1865 regoptail(ret, regnode(BACK));
1866 regoptail(ret, ret);
1867 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1868 ++num_complex_braces;
1869 }
1870 if (minval > 0 && maxval > 0)
1871 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1872 break;
1873 }
1874 if (re_multi_type(peekchr()) != NOT_MULTI)
1875 {
1876 /* Can't have a multi follow a multi. */
1877 if (peekchr() == Magic('*'))
1878 sprintf((char *)IObuff, _("E61: Nested %s*"),
1879 reg_magic >= MAGIC_ON ? "" : "\\");
1880 else
1881 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1882 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1883 EMSG_RET_NULL(IObuff);
1884 }
1885
1886 return ret;
1887}
1888
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001889/* When making changes to classchars also change nfa_classcodes. */
1890static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1891static int classcodes[] = {
1892 ANY, IDENT, SIDENT, KWORD, SKWORD,
1893 FNAME, SFNAME, PRINT, SPRINT,
1894 WHITE, NWHITE, DIGIT, NDIGIT,
1895 HEX, NHEX, OCTAL, NOCTAL,
1896 WORD, NWORD, HEAD, NHEAD,
1897 ALPHA, NALPHA, LOWER, NLOWER,
1898 UPPER, NUPPER
1899};
1900
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001902 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 *
1904 * Optimization: gobbles an entire sequence of ordinary characters so that
1905 * it can turn them into a single node, which is smaller to store and
1906 * faster to run. Don't do this when one_exactly is set.
1907 */
1908 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01001909regatom(int *flagp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910{
1911 char_u *ret;
1912 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 char_u *p;
1915 int extra = 0;
Bram Moolenaar7c29f382016-02-12 19:08:15 +01001916 int save_prev_at_start = prev_at_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917
1918 *flagp = WORST; /* Tentatively. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919
1920 c = getchr();
1921 switch (c)
1922 {
1923 case Magic('^'):
1924 ret = regnode(BOL);
1925 break;
1926
1927 case Magic('$'):
1928 ret = regnode(EOL);
1929#if defined(FEAT_SYN_HL) || defined(PROTO)
1930 had_eol = TRUE;
1931#endif
1932 break;
1933
1934 case Magic('<'):
1935 ret = regnode(BOW);
1936 break;
1937
1938 case Magic('>'):
1939 ret = regnode(EOW);
1940 break;
1941
1942 case Magic('_'):
1943 c = no_Magic(getchr());
1944 if (c == '^') /* "\_^" is start-of-line */
1945 {
1946 ret = regnode(BOL);
1947 break;
1948 }
1949 if (c == '$') /* "\_$" is end-of-line */
1950 {
1951 ret = regnode(EOL);
1952#if defined(FEAT_SYN_HL) || defined(PROTO)
1953 had_eol = TRUE;
1954#endif
1955 break;
1956 }
1957
1958 extra = ADD_NL;
1959 *flagp |= HASNL;
1960
1961 /* "\_[" is character range plus newline */
1962 if (c == '[')
1963 goto collection;
1964
1965 /* "\_x" is character class plus newline */
1966 /*FALLTHROUGH*/
1967
1968 /*
1969 * Character classes.
1970 */
1971 case Magic('.'):
1972 case Magic('i'):
1973 case Magic('I'):
1974 case Magic('k'):
1975 case Magic('K'):
1976 case Magic('f'):
1977 case Magic('F'):
1978 case Magic('p'):
1979 case Magic('P'):
1980 case Magic('s'):
1981 case Magic('S'):
1982 case Magic('d'):
1983 case Magic('D'):
1984 case Magic('x'):
1985 case Magic('X'):
1986 case Magic('o'):
1987 case Magic('O'):
1988 case Magic('w'):
1989 case Magic('W'):
1990 case Magic('h'):
1991 case Magic('H'):
1992 case Magic('a'):
1993 case Magic('A'):
1994 case Magic('l'):
1995 case Magic('L'):
1996 case Magic('u'):
1997 case Magic('U'):
1998 p = vim_strchr(classchars, no_Magic(c));
1999 if (p == NULL)
2000 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002001#ifdef FEAT_MBYTE
2002 /* When '.' is followed by a composing char ignore the dot, so that
2003 * the composing char is matched here. */
2004 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
2005 {
2006 c = getchr();
2007 goto do_multibyte;
2008 }
2009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 ret = regnode(classcodes[p - classchars] + extra);
2011 *flagp |= HASWIDTH | SIMPLE;
2012 break;
2013
2014 case Magic('n'):
2015 if (reg_string)
2016 {
2017 /* In a string "\n" matches a newline character. */
2018 ret = regnode(EXACTLY);
2019 regc(NL);
2020 regc(NUL);
2021 *flagp |= HASWIDTH | SIMPLE;
2022 }
2023 else
2024 {
2025 /* In buffer text "\n" matches the end of a line. */
2026 ret = regnode(NEWL);
2027 *flagp |= HASWIDTH | HASNL;
2028 }
2029 break;
2030
2031 case Magic('('):
2032 if (one_exactly)
2033 EMSG_ONE_RET_NULL;
2034 ret = reg(REG_PAREN, &flags);
2035 if (ret == NULL)
2036 return NULL;
2037 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2038 break;
2039
2040 case NUL:
2041 case Magic('|'):
2042 case Magic('&'):
2043 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002044 if (one_exactly)
2045 EMSG_ONE_RET_NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
2047 /* NOTREACHED */
2048
2049 case Magic('='):
2050 case Magic('?'):
2051 case Magic('+'):
2052 case Magic('@'):
2053 case Magic('{'):
2054 case Magic('*'):
2055 c = no_Magic(c);
2056 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
2057 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
2058 ? "" : "\\", c);
2059 EMSG_RET_NULL(IObuff);
2060 /* NOTREACHED */
2061
2062 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002063 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 {
2065 char_u *lp;
2066
2067 ret = regnode(EXACTLY);
2068 lp = reg_prev_sub;
2069 while (*lp != NUL)
2070 regc(*lp++);
2071 regc(NUL);
2072 if (*reg_prev_sub != NUL)
2073 {
2074 *flagp |= HASWIDTH;
2075 if ((lp - reg_prev_sub) == 1)
2076 *flagp |= SIMPLE;
2077 }
2078 }
2079 else
2080 EMSG_RET_NULL(_(e_nopresub));
2081 break;
2082
2083 case Magic('1'):
2084 case Magic('2'):
2085 case Magic('3'):
2086 case Magic('4'):
2087 case Magic('5'):
2088 case Magic('6'):
2089 case Magic('7'):
2090 case Magic('8'):
2091 case Magic('9'):
2092 {
2093 int refnum;
2094
2095 refnum = c - Magic('0');
2096 /*
2097 * Check if the back reference is legal. We must have seen the
2098 * close brace.
2099 * TODO: Should also check that we don't refer to something
2100 * that is repeated (+*=): what instance of the repetition
2101 * should we match?
2102 */
2103 if (!had_endbrace[refnum])
2104 {
2105 /* Trick: check if "@<=" or "@<!" follows, in which case
2106 * the \1 can appear before the referenced match. */
2107 for (p = regparse; *p != NUL; ++p)
2108 if (p[0] == '@' && p[1] == '<'
2109 && (p[2] == '!' || p[2] == '='))
2110 break;
2111 if (*p == NUL)
2112 EMSG_RET_NULL(_("E65: Illegal back reference"));
2113 }
2114 ret = regnode(BACKREF + refnum);
2115 }
2116 break;
2117
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 case Magic('z'):
2119 {
2120 c = no_Magic(getchr());
2121 switch (c)
2122 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002123#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002124 case '(': if (reg_do_extmatch != REX_SET)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002125 EMSG_RET_NULL(_(e_z_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002126 if (one_exactly)
2127 EMSG_ONE_RET_NULL;
2128 ret = reg(REG_ZPAREN, &flags);
2129 if (ret == NULL)
2130 return NULL;
2131 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2132 re_has_z = REX_SET;
2133 break;
2134
2135 case '1':
2136 case '2':
2137 case '3':
2138 case '4':
2139 case '5':
2140 case '6':
2141 case '7':
2142 case '8':
2143 case '9': if (reg_do_extmatch != REX_USE)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002144 EMSG_RET_NULL(_(e_z1_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 ret = regnode(ZREF + c - '0');
2146 re_has_z = REX_USE;
2147 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002148#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149
2150 case 's': ret = regnode(MOPEN + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002151 if (re_mult_next("\\zs") == FAIL)
2152 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153 break;
2154
2155 case 'e': ret = regnode(MCLOSE + 0);
Bram Moolenaarfb031402014-09-09 17:18:49 +02002156 if (re_mult_next("\\ze") == FAIL)
2157 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158 break;
2159
2160 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2161 }
2162 }
2163 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 case Magic('%'):
2166 {
2167 c = no_Magic(getchr());
2168 switch (c)
2169 {
2170 /* () without a back reference */
2171 case '(':
2172 if (one_exactly)
2173 EMSG_ONE_RET_NULL;
2174 ret = reg(REG_NPAREN, &flags);
2175 if (ret == NULL)
2176 return NULL;
2177 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2178 break;
2179
2180 /* Catch \%^ and \%$ regardless of where they appear in the
2181 * pattern -- regardless of whether or not it makes sense. */
2182 case '^':
2183 ret = regnode(RE_BOF);
2184 break;
2185
2186 case '$':
2187 ret = regnode(RE_EOF);
2188 break;
2189
2190 case '#':
2191 ret = regnode(CURSOR);
2192 break;
2193
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002194 case 'V':
2195 ret = regnode(RE_VISUAL);
2196 break;
2197
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002198 case 'C':
2199 ret = regnode(RE_COMPOSING);
2200 break;
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 /* \%[abc]: Emit as a list of branches, all ending at the last
2203 * branch which matches nothing. */
2204 case '[':
2205 if (one_exactly) /* doesn't nest */
2206 EMSG_ONE_RET_NULL;
2207 {
2208 char_u *lastbranch;
2209 char_u *lastnode = NULL;
2210 char_u *br;
2211
2212 ret = NULL;
2213 while ((c = getchr()) != ']')
2214 {
2215 if (c == NUL)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002216 EMSG2_RET_NULL(_(e_missing_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 reg_magic == MAGIC_ALL);
2218 br = regnode(BRANCH);
2219 if (ret == NULL)
2220 ret = br;
2221 else
2222 regtail(lastnode, br);
2223
2224 ungetchr();
2225 one_exactly = TRUE;
2226 lastnode = regatom(flagp);
2227 one_exactly = FALSE;
2228 if (lastnode == NULL)
2229 return NULL;
2230 }
2231 if (ret == NULL)
Bram Moolenaar2976c022013-06-05 21:30:37 +02002232 EMSG2_RET_NULL(_(e_empty_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 reg_magic == MAGIC_ALL);
2234 lastbranch = regnode(BRANCH);
2235 br = regnode(NOTHING);
2236 if (ret != JUST_CALC_SIZE)
2237 {
2238 regtail(lastnode, br);
2239 regtail(lastbranch, br);
2240 /* connect all branches to the NOTHING
2241 * branch at the end */
2242 for (br = ret; br != lastnode; )
2243 {
2244 if (OP(br) == BRANCH)
2245 {
2246 regtail(br, lastbranch);
2247 br = OPERAND(br);
2248 }
2249 else
2250 br = regnext(br);
2251 }
2252 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002253 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 break;
2255 }
2256
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002257 case 'd': /* %d123 decimal */
2258 case 'o': /* %o123 octal */
2259 case 'x': /* %xab hex 2 */
2260 case 'u': /* %uabcd hex 4 */
2261 case 'U': /* %U1234abcd hex 8 */
2262 {
2263 int i;
2264
2265 switch (c)
2266 {
2267 case 'd': i = getdecchrs(); break;
2268 case 'o': i = getoctchrs(); break;
2269 case 'x': i = gethexchrs(2); break;
2270 case 'u': i = gethexchrs(4); break;
2271 case 'U': i = gethexchrs(8); break;
2272 default: i = -1; break;
2273 }
2274
2275 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002276 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002277 _("E678: Invalid character after %s%%[dxouU]"),
2278 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002279#ifdef FEAT_MBYTE
2280 if (use_multibytecode(i))
2281 ret = regnode(MULTIBYTECODE);
2282 else
2283#endif
2284 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002285 if (i == 0)
2286 regc(0x0a);
2287 else
2288#ifdef FEAT_MBYTE
2289 regmbc(i);
2290#else
2291 regc(i);
2292#endif
2293 regc(NUL);
2294 *flagp |= HASWIDTH;
2295 break;
2296 }
2297
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002299 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2300 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
2302 long_u n = 0;
2303 int cmp;
2304
2305 cmp = c;
2306 if (cmp == '<' || cmp == '>')
2307 c = getchr();
2308 while (VIM_ISDIGIT(c))
2309 {
2310 n = n * 10 + (c - '0');
2311 c = getchr();
2312 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002313 if (c == '\'' && n == 0)
2314 {
2315 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2316 c = getchr();
2317 ret = regnode(RE_MARK);
2318 if (ret == JUST_CALC_SIZE)
2319 regsize += 2;
2320 else
2321 {
2322 *regcode++ = c;
2323 *regcode++ = cmp;
2324 }
2325 break;
2326 }
2327 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
2329 if (c == 'l')
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002330 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 ret = regnode(RE_LNUM);
Bram Moolenaar7c29f382016-02-12 19:08:15 +01002332 if (save_prev_at_start)
2333 at_start = TRUE;
2334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 else if (c == 'c')
2336 ret = regnode(RE_COL);
2337 else
2338 ret = regnode(RE_VCOL);
2339 if (ret == JUST_CALC_SIZE)
2340 regsize += 5;
2341 else
2342 {
2343 /* put the number and the optional
2344 * comparator after the opcode */
2345 regcode = re_put_long(regcode, n);
2346 *regcode++ = cmp;
2347 }
2348 break;
2349 }
2350 }
2351
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002352 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 reg_magic == MAGIC_ALL);
2354 }
2355 }
2356 break;
2357
2358 case Magic('['):
2359collection:
2360 {
2361 char_u *lp;
2362
2363 /*
2364 * If there is no matching ']', we assume the '[' is a normal
2365 * character. This makes 'incsearch' and ":help [" work.
2366 */
2367 lp = skip_anyof(regparse);
2368 if (*lp == ']') /* there is a matching ']' */
2369 {
2370 int startc = -1; /* > 0 when next '-' is a range */
2371 int endc;
2372
2373 /*
2374 * In a character class, different parsing rules apply.
2375 * Not even \ is special anymore, nothing is.
2376 */
2377 if (*regparse == '^') /* Complement of range. */
2378 {
2379 ret = regnode(ANYBUT + extra);
2380 regparse++;
2381 }
2382 else
2383 ret = regnode(ANYOF + extra);
2384
2385 /* At the start ']' and '-' mean the literal character. */
2386 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002387 {
2388 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391
2392 while (*regparse != NUL && *regparse != ']')
2393 {
2394 if (*regparse == '-')
2395 {
2396 ++regparse;
2397 /* The '-' is not used for a range at the end and
2398 * after or before a '\n'. */
2399 if (*regparse == ']' || *regparse == NUL
2400 || startc == -1
2401 || (regparse[0] == '\\' && regparse[1] == 'n'))
2402 {
2403 regc('-');
2404 startc = '-'; /* [--x] is a range */
2405 }
2406 else
2407 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002408 /* Also accept "a-[.z.]" */
2409 endc = 0;
2410 if (*regparse == '[')
2411 endc = get_coll_element(&regparse);
2412 if (endc == 0)
2413 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002415 if (has_mbyte)
2416 endc = mb_ptr2char_adv(&regparse);
2417 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002419 endc = *regparse++;
2420 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002421
2422 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002423 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002424 endc = coll_get_char();
2425
Bram Moolenaar071d4272004-06-13 20:20:40 +00002426 if (startc > endc)
2427 EMSG_RET_NULL(_(e_invrange));
2428#ifdef FEAT_MBYTE
2429 if (has_mbyte && ((*mb_char2len)(startc) > 1
2430 || (*mb_char2len)(endc) > 1))
2431 {
2432 /* Limit to a range of 256 chars */
2433 if (endc > startc + 256)
2434 EMSG_RET_NULL(_(e_invrange));
2435 while (++startc <= endc)
2436 regmbc(startc);
2437 }
2438 else
2439#endif
2440 {
2441#ifdef EBCDIC
2442 int alpha_only = FALSE;
2443
2444 /* for alphabetical range skip the gaps
2445 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2446 if (isalpha(startc) && isalpha(endc))
2447 alpha_only = TRUE;
2448#endif
2449 while (++startc <= endc)
2450#ifdef EBCDIC
2451 if (!alpha_only || isalpha(startc))
2452#endif
2453 regc(startc);
2454 }
2455 startc = -1;
2456 }
2457 }
2458 /*
2459 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2460 * accepts "\t", "\e", etc., but only when the 'l' flag in
2461 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002462 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 */
2464 else if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002465 && !reg_cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002467 || (!reg_cpo_lit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468 && vim_strchr(REGEXP_ABBR,
2469 regparse[1]) != NULL)))
2470 {
2471 regparse++;
2472 if (*regparse == 'n')
2473 {
2474 /* '\n' in range: also match NL */
2475 if (ret != JUST_CALC_SIZE)
2476 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002477 /* Using \n inside [^] does not change what
2478 * matches. "[^\n]" is the same as ".". */
2479 if (*ret == ANYOF)
2480 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002482 *flagp |= HASNL;
2483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 /* else: must have had a \n already */
2485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 regparse++;
2487 startc = -1;
2488 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002489 else if (*regparse == 'd'
2490 || *regparse == 'o'
2491 || *regparse == 'x'
2492 || *regparse == 'u'
2493 || *regparse == 'U')
2494 {
2495 startc = coll_get_char();
2496 if (startc == 0)
2497 regc(0x0a);
2498 else
2499#ifdef FEAT_MBYTE
2500 regmbc(startc);
2501#else
2502 regc(startc);
2503#endif
2504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 else
2506 {
2507 startc = backslash_trans(*regparse++);
2508 regc(startc);
2509 }
2510 }
2511 else if (*regparse == '[')
2512 {
2513 int c_class;
2514 int cu;
2515
Bram Moolenaardf177f62005-02-22 08:39:57 +00002516 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517 startc = -1;
2518 /* Characters assumed to be 8 bits! */
2519 switch (c_class)
2520 {
2521 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002522 c_class = get_equi_class(&regparse);
2523 if (c_class != 0)
2524 {
2525 /* produce equivalence class */
2526 reg_equi_class(c_class);
2527 }
2528 else if ((c_class =
2529 get_coll_element(&regparse)) != 0)
2530 {
2531 /* produce a collating element */
2532 regmbc(c_class);
2533 }
2534 else
2535 {
2536 /* literal '[', allow [[-x] as a range */
2537 startc = *regparse++;
2538 regc(startc);
2539 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 break;
2541 case CLASS_ALNUM:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002542 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 if (isalnum(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002544 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 break;
2546 case CLASS_ALPHA:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002547 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (isalpha(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002549 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 break;
2551 case CLASS_BLANK:
2552 regc(' ');
2553 regc('\t');
2554 break;
2555 case CLASS_CNTRL:
2556 for (cu = 1; cu <= 255; cu++)
2557 if (iscntrl(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002558 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 break;
2560 case CLASS_DIGIT:
2561 for (cu = 1; cu <= 255; cu++)
2562 if (VIM_ISDIGIT(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002563 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002564 break;
2565 case CLASS_GRAPH:
2566 for (cu = 1; cu <= 255; cu++)
2567 if (isgraph(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002568 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 break;
2570 case CLASS_LOWER:
2571 for (cu = 1; cu <= 255; cu++)
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002572 if (MB_ISLOWER(cu) && cu != 170
2573 && cu != 186)
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002574 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 break;
2576 case CLASS_PRINT:
2577 for (cu = 1; cu <= 255; cu++)
2578 if (vim_isprintc(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002579 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 break;
2581 case CLASS_PUNCT:
Bram Moolenaare8aee7d2016-04-26 21:39:13 +02002582 for (cu = 1; cu < 128; cu++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 if (ispunct(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002584 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 break;
2586 case CLASS_SPACE:
2587 for (cu = 9; cu <= 13; cu++)
2588 regc(cu);
2589 regc(' ');
2590 break;
2591 case CLASS_UPPER:
2592 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002593 if (MB_ISUPPER(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002594 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 break;
2596 case CLASS_XDIGIT:
2597 for (cu = 1; cu <= 255; cu++)
2598 if (vim_isxdigit(cu))
Bram Moolenaaraf98a492016-04-24 14:40:12 +02002599 regmbc(cu);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 break;
2601 case CLASS_TAB:
2602 regc('\t');
2603 break;
2604 case CLASS_RETURN:
2605 regc('\r');
2606 break;
2607 case CLASS_BACKSPACE:
2608 regc('\b');
2609 break;
2610 case CLASS_ESCAPE:
2611 regc('\033');
2612 break;
2613 }
2614 }
2615 else
2616 {
2617#ifdef FEAT_MBYTE
2618 if (has_mbyte)
2619 {
2620 int len;
2621
2622 /* produce a multibyte character, including any
2623 * following composing characters */
2624 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002625 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 if (enc_utf8 && utf_char2len(startc) != len)
2627 startc = -1; /* composing chars */
2628 while (--len >= 0)
2629 regc(*regparse++);
2630 }
2631 else
2632#endif
2633 {
2634 startc = *regparse++;
2635 regc(startc);
2636 }
2637 }
2638 }
2639 regc(NUL);
2640 prevchr_len = 1; /* last char was the ']' */
2641 if (*regparse != ']')
2642 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2643 skipchr(); /* let's be friends with the lexer again */
2644 *flagp |= HASWIDTH | SIMPLE;
2645 break;
2646 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002647 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002648 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002649 }
2650 /* FALLTHROUGH */
2651
2652 default:
2653 {
2654 int len;
2655
2656#ifdef FEAT_MBYTE
2657 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002658 * before a multi and when it's a composing char. */
2659 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002661do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002662 ret = regnode(MULTIBYTECODE);
2663 regmbc(c);
2664 *flagp |= HASWIDTH | SIMPLE;
2665 break;
2666 }
2667#endif
2668
2669 ret = regnode(EXACTLY);
2670
2671 /*
2672 * Append characters as long as:
2673 * - there is no following multi, we then need the character in
2674 * front of it as a single character operand
2675 * - not running into a Magic character
2676 * - "one_exactly" is not set
2677 * But always emit at least one character. Might be a Multi,
2678 * e.g., a "[" without matching "]".
2679 */
2680 for (len = 0; c != NUL && (len == 0
2681 || (re_multi_type(peekchr()) == NOT_MULTI
2682 && !one_exactly
2683 && !is_Magic(c))); ++len)
2684 {
2685 c = no_Magic(c);
2686#ifdef FEAT_MBYTE
2687 if (has_mbyte)
2688 {
2689 regmbc(c);
2690 if (enc_utf8)
2691 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 int l;
2693
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002694 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002695 for (;;)
2696 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002697 l = utf_ptr2len(regparse);
2698 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002700 regmbc(utf_ptr2char(regparse));
2701 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 }
2704 }
2705 else
2706#endif
2707 regc(c);
2708 c = getchr();
2709 }
2710 ungetchr();
2711
2712 regc(NUL);
2713 *flagp |= HASWIDTH;
2714 if (len == 1)
2715 *flagp |= SIMPLE;
2716 }
2717 break;
2718 }
2719
2720 return ret;
2721}
2722
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002723#ifdef FEAT_MBYTE
2724/*
2725 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2726 * character "c".
2727 */
2728 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01002729use_multibytecode(int c)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002730{
2731 return has_mbyte && (*mb_char2len)(c) > 1
2732 && (re_multi_type(peekchr()) != NOT_MULTI
2733 || (enc_utf8 && utf_iscomposing(c)));
2734}
2735#endif
2736
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002738 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 * Return pointer to generated code.
2740 */
2741 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002742regnode(int op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743{
2744 char_u *ret;
2745
2746 ret = regcode;
2747 if (ret == JUST_CALC_SIZE)
2748 regsize += 3;
2749 else
2750 {
2751 *regcode++ = op;
2752 *regcode++ = NUL; /* Null "next" pointer. */
2753 *regcode++ = NUL;
2754 }
2755 return ret;
2756}
2757
2758/*
2759 * Emit (if appropriate) a byte of code
2760 */
2761 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002762regc(int b)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763{
2764 if (regcode == JUST_CALC_SIZE)
2765 regsize++;
2766 else
2767 *regcode++ = b;
2768}
2769
2770#ifdef FEAT_MBYTE
2771/*
2772 * Emit (if appropriate) a multi-byte character of code
2773 */
2774 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002775regmbc(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002776{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002777 if (!has_mbyte && c > 0xff)
2778 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002779 if (regcode == JUST_CALC_SIZE)
2780 regsize += (*mb_char2len)(c);
2781 else
2782 regcode += (*mb_char2bytes)(c, regcode);
2783}
2784#endif
2785
2786/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002787 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002788 *
2789 * Means relocating the operand.
2790 */
2791 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002792reginsert(int op, char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002793{
2794 char_u *src;
2795 char_u *dst;
2796 char_u *place;
2797
2798 if (regcode == JUST_CALC_SIZE)
2799 {
2800 regsize += 3;
2801 return;
2802 }
2803 src = regcode;
2804 regcode += 3;
2805 dst = regcode;
2806 while (src > opnd)
2807 *--dst = *--src;
2808
2809 place = opnd; /* Op node, where operand used to be. */
2810 *place++ = op;
2811 *place++ = NUL;
2812 *place = NUL;
2813}
2814
2815/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002816 * Insert an operator in front of already-emitted operand.
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002817 * Add a number to the operator.
2818 */
2819 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002820reginsert_nr(int op, long val, char_u *opnd)
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002821{
2822 char_u *src;
2823 char_u *dst;
2824 char_u *place;
2825
2826 if (regcode == JUST_CALC_SIZE)
2827 {
2828 regsize += 7;
2829 return;
2830 }
2831 src = regcode;
2832 regcode += 7;
2833 dst = regcode;
2834 while (src > opnd)
2835 *--dst = *--src;
2836
2837 place = opnd; /* Op node, where operand used to be. */
2838 *place++ = op;
2839 *place++ = NUL;
2840 *place++ = NUL;
2841 place = re_put_long(place, (long_u)val);
2842}
2843
2844/*
2845 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 * The operator has the given limit values as operands. Also set next pointer.
2847 *
2848 * Means relocating the operand.
2849 */
2850 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002851reginsert_limits(
2852 int op,
2853 long minval,
2854 long maxval,
2855 char_u *opnd)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
2857 char_u *src;
2858 char_u *dst;
2859 char_u *place;
2860
2861 if (regcode == JUST_CALC_SIZE)
2862 {
2863 regsize += 11;
2864 return;
2865 }
2866 src = regcode;
2867 regcode += 11;
2868 dst = regcode;
2869 while (src > opnd)
2870 *--dst = *--src;
2871
2872 place = opnd; /* Op node, where operand used to be. */
2873 *place++ = op;
2874 *place++ = NUL;
2875 *place++ = NUL;
2876 place = re_put_long(place, (long_u)minval);
2877 place = re_put_long(place, (long_u)maxval);
2878 regtail(opnd, place);
2879}
2880
2881/*
2882 * Write a long as four bytes at "p" and return pointer to the next char.
2883 */
2884 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01002885re_put_long(char_u *p, long_u val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002886{
2887 *p++ = (char_u) ((val >> 24) & 0377);
2888 *p++ = (char_u) ((val >> 16) & 0377);
2889 *p++ = (char_u) ((val >> 8) & 0377);
2890 *p++ = (char_u) (val & 0377);
2891 return p;
2892}
2893
2894/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002895 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896 */
2897 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002898regtail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
2900 char_u *scan;
2901 char_u *temp;
2902 int offset;
2903
2904 if (p == JUST_CALC_SIZE)
2905 return;
2906
2907 /* Find last node. */
2908 scan = p;
2909 for (;;)
2910 {
2911 temp = regnext(scan);
2912 if (temp == NULL)
2913 break;
2914 scan = temp;
2915 }
2916
Bram Moolenaar582fd852005-03-28 20:58:01 +00002917 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918 offset = (int)(scan - val);
2919 else
2920 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002921 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002922 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002923 * values in too many places. */
2924 if (offset > 0xffff)
2925 reg_toolong = TRUE;
2926 else
2927 {
2928 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2929 *(scan + 2) = (char_u) (offset & 0377);
2930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931}
2932
2933/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002934 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 */
2936 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002937regoptail(char_u *p, char_u *val)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938{
2939 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2940 if (p == NULL || p == JUST_CALC_SIZE
2941 || (OP(p) != BRANCH
2942 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2943 return;
2944 regtail(OPERAND(p), val);
2945}
2946
2947/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002948 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002950/*
2951 * Start parsing at "str".
2952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002954initchr(char_u *str)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955{
2956 regparse = str;
2957 prevchr_len = 0;
2958 curchr = prevprevchr = prevchr = nextchr = -1;
2959 at_start = TRUE;
2960 prev_at_start = FALSE;
2961}
2962
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002963/*
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002964 * Save the current parse state, so that it can be restored and parsing
2965 * starts in the same state again.
2966 */
2967 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002968save_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002969{
2970 ps->regparse = regparse;
2971 ps->prevchr_len = prevchr_len;
2972 ps->curchr = curchr;
2973 ps->prevchr = prevchr;
2974 ps->prevprevchr = prevprevchr;
2975 ps->nextchr = nextchr;
2976 ps->at_start = at_start;
2977 ps->prev_at_start = prev_at_start;
2978 ps->regnpar = regnpar;
2979}
2980
2981/*
2982 * Restore a previously saved parse state.
2983 */
2984 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01002985restore_parse_state(parse_state_T *ps)
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002986{
2987 regparse = ps->regparse;
2988 prevchr_len = ps->prevchr_len;
2989 curchr = ps->curchr;
2990 prevchr = ps->prevchr;
2991 prevprevchr = ps->prevprevchr;
2992 nextchr = ps->nextchr;
2993 at_start = ps->at_start;
2994 prev_at_start = ps->prev_at_start;
2995 regnpar = ps->regnpar;
2996}
2997
2998
2999/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003000 * Get the next character without advancing.
3001 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003003peekchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003005 static int after_slash = FALSE;
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 if (curchr == -1)
3008 {
3009 switch (curchr = regparse[0])
3010 {
3011 case '.':
3012 case '[':
3013 case '~':
3014 /* magic when 'magic' is on */
3015 if (reg_magic >= MAGIC_ON)
3016 curchr = Magic(curchr);
3017 break;
3018 case '(':
3019 case ')':
3020 case '{':
3021 case '%':
3022 case '+':
3023 case '=':
3024 case '?':
3025 case '@':
3026 case '!':
3027 case '&':
3028 case '|':
3029 case '<':
3030 case '>':
3031 case '#': /* future ext. */
3032 case '"': /* future ext. */
3033 case '\'': /* future ext. */
3034 case ',': /* future ext. */
3035 case '-': /* future ext. */
3036 case ':': /* future ext. */
3037 case ';': /* future ext. */
3038 case '`': /* future ext. */
3039 case '/': /* Can't be used in / command */
3040 /* magic only after "\v" */
3041 if (reg_magic == MAGIC_ALL)
3042 curchr = Magic(curchr);
3043 break;
3044 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00003045 /* * is not magic as the very first character, eg "?*ptr", when
3046 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
3047 * "\(\*" is not magic, thus must be magic if "after_slash" */
3048 if (reg_magic >= MAGIC_ON
3049 && !at_start
3050 && !(prev_at_start && prevchr == Magic('^'))
3051 && (after_slash
3052 || (prevchr != Magic('(')
3053 && prevchr != Magic('&')
3054 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 curchr = Magic('*');
3056 break;
3057 case '^':
3058 /* '^' is only magic as the very first character and if it's after
3059 * "\(", "\|", "\&' or "\n" */
3060 if (reg_magic >= MAGIC_OFF
3061 && (at_start
3062 || reg_magic == MAGIC_ALL
3063 || prevchr == Magic('(')
3064 || prevchr == Magic('|')
3065 || prevchr == Magic('&')
3066 || prevchr == Magic('n')
3067 || (no_Magic(prevchr) == '('
3068 && prevprevchr == Magic('%'))))
3069 {
3070 curchr = Magic('^');
3071 at_start = TRUE;
3072 prev_at_start = FALSE;
3073 }
3074 break;
3075 case '$':
3076 /* '$' is only magic as the very last char and if it's in front of
3077 * either "\|", "\)", "\&", or "\n" */
3078 if (reg_magic >= MAGIC_OFF)
3079 {
3080 char_u *p = regparse + 1;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003081 int is_magic_all = (reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003082
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003083 /* ignore \c \C \m \M \v \V and \Z after '$' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003085 || p[1] == 'm' || p[1] == 'M'
3086 || p[1] == 'v' || p[1] == 'V' || p[1] == 'Z'))
3087 {
3088 if (p[1] == 'v')
3089 is_magic_all = TRUE;
3090 else if (p[1] == 'm' || p[1] == 'M' || p[1] == 'V')
3091 is_magic_all = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 p += 2;
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 if (p[0] == NUL
3095 || (p[0] == '\\'
3096 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3097 || p[1] == 'n'))
Bram Moolenaarff65ac82014-07-09 19:32:34 +02003098 || (is_magic_all
3099 && (p[0] == '|' || p[0] == '&' || p[0] == ')'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100 || reg_magic == MAGIC_ALL)
3101 curchr = Magic('$');
3102 }
3103 break;
3104 case '\\':
3105 {
3106 int c = regparse[1];
3107
3108 if (c == NUL)
3109 curchr = '\\'; /* trailing '\' */
3110 else if (
3111#ifdef EBCDIC
3112 vim_strchr(META, c)
3113#else
3114 c <= '~' && META_flags[c]
3115#endif
3116 )
3117 {
3118 /*
3119 * META contains everything that may be magic sometimes,
3120 * except ^ and $ ("\^" and "\$" are only magic after
Bram Moolenaarb878bbb2015-06-09 20:39:24 +02003121 * "\V"). We now fetch the next character and toggle its
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 * magicness. Therefore, \ is so meta-magic that it is
3123 * not in META.
3124 */
3125 curchr = -1;
3126 prev_at_start = at_start;
3127 at_start = FALSE; /* be able to say "/\*ptr" */
3128 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003129 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 peekchr();
3131 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003132 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003133 curchr = toggle_Magic(curchr);
3134 }
3135 else if (vim_strchr(REGEXP_ABBR, c))
3136 {
3137 /*
3138 * Handle abbreviations, like "\t" for TAB -- webb
3139 */
3140 curchr = backslash_trans(c);
3141 }
3142 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3143 curchr = toggle_Magic(c);
3144 else
3145 {
3146 /*
3147 * Next character can never be (made) magic?
3148 * Then backslashing it won't do anything.
3149 */
3150#ifdef FEAT_MBYTE
3151 if (has_mbyte)
3152 curchr = (*mb_ptr2char)(regparse + 1);
3153 else
3154#endif
3155 curchr = c;
3156 }
3157 break;
3158 }
3159
3160#ifdef FEAT_MBYTE
3161 default:
3162 if (has_mbyte)
3163 curchr = (*mb_ptr2char)(regparse);
3164#endif
3165 }
3166 }
3167
3168 return curchr;
3169}
3170
3171/*
3172 * Eat one lexed character. Do this in a way that we can undo it.
3173 */
3174 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003175skipchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176{
3177 /* peekchr() eats a backslash, do the same here */
3178 if (*regparse == '\\')
3179 prevchr_len = 1;
3180 else
3181 prevchr_len = 0;
3182 if (regparse[prevchr_len] != NUL)
3183 {
3184#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003185 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003186 /* exclude composing chars that mb_ptr2len does include */
3187 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003188 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003189 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 else
3191#endif
3192 ++prevchr_len;
3193 }
3194 regparse += prevchr_len;
3195 prev_at_start = at_start;
3196 at_start = FALSE;
3197 prevprevchr = prevchr;
3198 prevchr = curchr;
3199 curchr = nextchr; /* use previously unget char, or -1 */
3200 nextchr = -1;
3201}
3202
3203/*
3204 * Skip a character while keeping the value of prev_at_start for at_start.
3205 * prevchr and prevprevchr are also kept.
3206 */
3207 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003208skipchr_keepstart(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209{
3210 int as = prev_at_start;
3211 int pr = prevchr;
3212 int prpr = prevprevchr;
3213
3214 skipchr();
3215 at_start = as;
3216 prevchr = pr;
3217 prevprevchr = prpr;
3218}
3219
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003220/*
3221 * Get the next character from the pattern. We know about magic and such, so
3222 * therefore we need a lexical analyzer.
3223 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003225getchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226{
3227 int chr = peekchr();
3228
3229 skipchr();
3230 return chr;
3231}
3232
3233/*
3234 * put character back. Works only once!
3235 */
3236 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01003237ungetchr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238{
3239 nextchr = curchr;
3240 curchr = prevchr;
3241 prevchr = prevprevchr;
3242 at_start = prev_at_start;
3243 prev_at_start = FALSE;
3244
3245 /* Backup regparse, so that it's at the same position as before the
3246 * getchr(). */
3247 regparse -= prevchr_len;
3248}
3249
3250/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003251 * Get and return the value of the hex string at the current position.
3252 * Return -1 if there is no valid hex number.
3253 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003254 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003255 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003256 * The parameter controls the maximum number of input characters. This will be
3257 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3258 */
3259 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003260gethexchrs(int maxinputlen)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003261{
3262 int nr = 0;
3263 int c;
3264 int i;
3265
3266 for (i = 0; i < maxinputlen; ++i)
3267 {
3268 c = regparse[0];
3269 if (!vim_isxdigit(c))
3270 break;
3271 nr <<= 4;
3272 nr |= hex2nr(c);
3273 ++regparse;
3274 }
3275
3276 if (i == 0)
3277 return -1;
3278 return nr;
3279}
3280
3281/*
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003282 * Get and return the value of the decimal string immediately after the
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003283 * current position. Return -1 for invalid. Consumes all digits.
3284 */
3285 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003286getdecchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003287{
3288 int nr = 0;
3289 int c;
3290 int i;
3291
3292 for (i = 0; ; ++i)
3293 {
3294 c = regparse[0];
3295 if (c < '0' || c > '9')
3296 break;
3297 nr *= 10;
3298 nr += c - '0';
3299 ++regparse;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003300 curchr = -1; /* no longer valid */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003301 }
3302
3303 if (i == 0)
3304 return -1;
3305 return nr;
3306}
3307
3308/*
3309 * get and return the value of the octal string immediately after the current
3310 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3311 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3312 * treat 8 or 9 as recognised characters. Position is updated:
3313 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003314 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003315 */
3316 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003317getoctchrs(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003318{
3319 int nr = 0;
3320 int c;
3321 int i;
3322
3323 for (i = 0; i < 3 && nr < 040; ++i)
3324 {
3325 c = regparse[0];
3326 if (c < '0' || c > '7')
3327 break;
3328 nr <<= 3;
3329 nr |= hex2nr(c);
3330 ++regparse;
3331 }
3332
3333 if (i == 0)
3334 return -1;
3335 return nr;
3336}
3337
3338/*
3339 * Get a number after a backslash that is inside [].
3340 * When nothing is recognized return a backslash.
3341 */
3342 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003343coll_get_char(void)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003344{
3345 int nr = -1;
3346
3347 switch (*regparse++)
3348 {
3349 case 'd': nr = getdecchrs(); break;
3350 case 'o': nr = getoctchrs(); break;
3351 case 'x': nr = gethexchrs(2); break;
3352 case 'u': nr = gethexchrs(4); break;
3353 case 'U': nr = gethexchrs(8); break;
3354 }
3355 if (nr < 0)
3356 {
3357 /* If getting the number fails be backwards compatible: the character
3358 * is a backslash. */
3359 --regparse;
3360 nr = '\\';
3361 }
3362 return nr;
3363}
3364
3365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 * read_limits - Read two integers to be taken as a minimum and maximum.
3367 * If the first character is '-', then the range is reversed.
3368 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3369 * missing, a very big number is the default.
3370 */
3371 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003372read_limits(long *minval, long *maxval)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373{
3374 int reverse = FALSE;
3375 char_u *first_char;
3376 long tmp;
3377
3378 if (*regparse == '-')
3379 {
3380 /* Starts with '-', so reverse the range later */
3381 regparse++;
3382 reverse = TRUE;
3383 }
3384 first_char = regparse;
3385 *minval = getdigits(&regparse);
3386 if (*regparse == ',') /* There is a comma */
3387 {
3388 if (vim_isdigit(*++regparse))
3389 *maxval = getdigits(&regparse);
3390 else
3391 *maxval = MAX_LIMIT;
3392 }
3393 else if (VIM_ISDIGIT(*first_char))
3394 *maxval = *minval; /* It was \{n} or \{-n} */
3395 else
3396 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3397 if (*regparse == '\\')
3398 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003399 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003400 {
3401 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
3402 reg_magic == MAGIC_ALL ? "" : "\\");
3403 EMSG_RET_FAIL(IObuff);
3404 }
3405
3406 /*
3407 * Reverse the range if there was a '-', or make sure it is in the right
3408 * order otherwise.
3409 */
3410 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3411 {
3412 tmp = *minval;
3413 *minval = *maxval;
3414 *maxval = tmp;
3415 }
3416 skipchr(); /* let's be friends with the lexer again */
3417 return OK;
3418}
3419
3420/*
3421 * vim_regexec and friends
3422 */
3423
3424/*
3425 * Global work variables for vim_regexec().
3426 */
3427
3428/* The current match-position is remembered with these variables: */
3429static linenr_T reglnum; /* line number, relative to first line */
3430static char_u *regline; /* start of current line */
3431static char_u *reginput; /* current input, points into "regline" */
3432
3433static int need_clear_subexpr; /* subexpressions still need to be
3434 * cleared */
3435#ifdef FEAT_SYN_HL
3436static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3437 * still need to be cleared */
3438#endif
3439
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440/*
3441 * Structure used to save the current input state, when it needs to be
3442 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003443 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444 */
3445typedef struct
3446{
3447 union
3448 {
3449 char_u *ptr; /* reginput pointer, for single-line regexp */
3450 lpos_T pos; /* reginput pos, for multi-line regexp */
3451 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003452 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453} regsave_T;
3454
3455/* struct to save start/end pointer/position in for \(\) */
3456typedef struct
3457{
3458 union
3459 {
3460 char_u *ptr;
3461 lpos_T pos;
3462 } se_u;
3463} save_se_T;
3464
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003465/* used for BEHIND and NOBEHIND matching */
3466typedef struct regbehind_S
3467{
3468 regsave_T save_after;
3469 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003470 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003471 save_se_T save_start[NSUBEXP];
3472 save_se_T save_end[NSUBEXP];
3473} regbehind_T;
3474
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003475static char_u *reg_getline(linenr_T lnum);
3476static long bt_regexec_both(char_u *line, colnr_T col, proftime_T *tm);
3477static long regtry(bt_regprog_T *prog, colnr_T col);
3478static void cleanup_subexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003480static void cleanup_zsubexpr(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481#endif
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003482static void save_subexpr(regbehind_T *bp);
3483static void restore_subexpr(regbehind_T *bp);
3484static void reg_nextline(void);
3485static void reg_save(regsave_T *save, garray_T *gap);
3486static void reg_restore(regsave_T *save, garray_T *gap);
3487static int reg_save_equal(regsave_T *save);
3488static void save_se_multi(save_se_T *savep, lpos_T *posp);
3489static void save_se_one(save_se_T *savep, char_u **pp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490
3491/* Save the sub-expressions before attempting a match. */
3492#define save_se(savep, posp, pp) \
3493 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3494
3495/* After a failed match restore the sub-expressions. */
3496#define restore_se(savep, posp, pp) { \
3497 if (REG_MULTI) \
3498 *(posp) = (savep)->se_u.pos; \
3499 else \
3500 *(pp) = (savep)->se_u.ptr; }
3501
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003502static int re_num_cmp(long_u val, char_u *scan);
3503static int match_with_backref(linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum, colnr_T end_col, int *bytelen);
3504static int regmatch(char_u *prog);
3505static int regrepeat(char_u *p, long maxcount);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506
3507#ifdef DEBUG
3508int regnarrate = 0;
3509#endif
3510
3511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003512 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3513 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003514 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003515 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003516static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517static unsigned reg_tofreelen;
3518
3519/*
Bram Moolenaar6100d022016-10-02 16:51:57 +02003520 * Structure used to store the execution state of the regex engine.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003521 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 * done:
3523 * single-line multi-line
3524 * reg_match &regmatch_T NULL
3525 * reg_mmatch NULL &regmmatch_T
3526 * reg_startp reg_match->startp <invalid>
3527 * reg_endp reg_match->endp <invalid>
3528 * reg_startpos <invalid> reg_mmatch->startpos
3529 * reg_endpos <invalid> reg_mmatch->endpos
3530 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003531 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 * reg_firstlnum <invalid> first line in which to search
3533 * reg_maxline 0 last line nr
3534 * reg_line_lbr FALSE or TRUE FALSE
3535 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003536typedef struct {
3537 regmatch_T *reg_match;
3538 regmmatch_T *reg_mmatch;
3539 char_u **reg_startp;
3540 char_u **reg_endp;
3541 lpos_T *reg_startpos;
3542 lpos_T *reg_endpos;
3543 win_T *reg_win;
3544 buf_T *reg_buf;
3545 linenr_T reg_firstlnum;
3546 linenr_T reg_maxline;
3547 int reg_line_lbr; /* "\n" in string is line break */
3548
3549 /* Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3550 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3551 * contains '\c' or '\C' the value is overruled. */
3552 int reg_ic;
3553
3554#ifdef FEAT_MBYTE
3555 /* Similar to rex.reg_ic, but only for 'combining' characters. Set with \Z
3556 * flag in the regexp. Defaults to false, always. */
3557 int reg_icombine;
3558#endif
3559
3560 /* Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3561 * there is no maximum. */
3562 colnr_T reg_maxcol;
3563} regexec_T;
3564
3565static regexec_T rex;
3566static int rex_in_use = FALSE;
3567
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003569/* Values for rs_state in regitem_T. */
3570typedef enum regstate_E
3571{
3572 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3573 , RS_MOPEN /* MOPEN + [0-9] */
3574 , RS_MCLOSE /* MCLOSE + [0-9] */
3575#ifdef FEAT_SYN_HL
3576 , RS_ZOPEN /* ZOPEN + [0-9] */
3577 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3578#endif
3579 , RS_BRANCH /* BRANCH */
3580 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3581 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3582 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3583 , RS_NOMATCH /* NOMATCH */
3584 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3585 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3586 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3587 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3588} regstate_T;
3589
3590/*
3591 * When there are alternatives a regstate_T is put on the regstack to remember
3592 * what we are doing.
3593 * Before it may be another type of item, depending on rs_state, to remember
3594 * more things.
3595 */
3596typedef struct regitem_S
3597{
3598 regstate_T rs_state; /* what we are doing, one of RS_ above */
3599 char_u *rs_scan; /* current node in program */
3600 union
3601 {
3602 save_se_T sesave;
3603 regsave_T regsave;
3604 } rs_un; /* room for saving reginput */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003605 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003606} regitem_T;
3607
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003608static regitem_T *regstack_push(regstate_T state, char_u *scan);
3609static void regstack_pop(char_u **scan);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003610
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003611/* used for STAR, PLUS and BRACE_SIMPLE matching */
3612typedef struct regstar_S
3613{
3614 int nextb; /* next byte */
3615 int nextb_ic; /* next byte reverse case */
3616 long count;
3617 long minval;
3618 long maxval;
3619} regstar_T;
3620
3621/* used to store input position when a BACK was encountered, so that we now if
3622 * we made any progress since the last time. */
3623typedef struct backpos_S
3624{
3625 char_u *bp_scan; /* "scan" where BACK was encountered */
3626 regsave_T bp_pos; /* last input position */
3627} backpos_T;
3628
3629/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003630 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3631 * to avoid invoking malloc() and free() often.
3632 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3633 * or regbehind_T.
3634 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003635 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003636static garray_T regstack = {0, 0, 0, 0, NULL};
3637static garray_T backpos = {0, 0, 0, 0, NULL};
3638
3639/*
3640 * Both for regstack and backpos tables we use the following strategy of
3641 * allocation (to reduce malloc/free calls):
3642 * - Initial size is fairly small.
3643 * - When needed, the tables are grown bigger (8 times at first, double after
3644 * that).
3645 * - After executing the match we free the memory only if the array has grown.
3646 * Thus the memory is kept allocated when it's at the initial size.
3647 * This makes it fast while not keeping a lot of memory allocated.
3648 * A three times speed increase was observed when using many simple patterns.
3649 */
3650#define REGSTACK_INITIAL 2048
3651#define BACKPOS_INITIAL 64
3652
3653#if defined(EXITFREE) || defined(PROTO)
3654 void
Bram Moolenaar05540972016-01-30 20:31:25 +01003655free_regexp_stuff(void)
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003656{
3657 ga_clear(&regstack);
3658 ga_clear(&backpos);
3659 vim_free(reg_tofree);
3660 vim_free(reg_prev_sub);
3661}
3662#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664/*
3665 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3666 */
3667 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01003668reg_getline(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003669{
3670 /* when looking behind for a match/no-match lnum is negative. But we
3671 * can't go before line 1 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003672 if (rex.reg_firstlnum + lnum < 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 return NULL;
Bram Moolenaar6100d022016-10-02 16:51:57 +02003674 if (lnum > rex.reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003675 /* Must have matched the "\n" in the last line. */
3676 return (char_u *)"";
Bram Moolenaar6100d022016-10-02 16:51:57 +02003677 return ml_get_buf(rex.reg_buf, rex.reg_firstlnum + lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678}
3679
3680static regsave_T behind_pos;
3681
3682#ifdef FEAT_SYN_HL
3683static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3684static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3685static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3686static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3687#endif
3688
3689/* TRUE if using multi-line regexp. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003690#define REG_MULTI (rex.reg_match == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692/*
3693 * Match a regexp against a string.
3694 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3695 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003696 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 *
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003698 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003700 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01003701bt_regexec_nl(
3702 regmatch_T *rmp,
3703 char_u *line, /* string to match against */
3704 colnr_T col, /* column to start looking for match */
3705 int line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003706{
Bram Moolenaar6100d022016-10-02 16:51:57 +02003707 rex.reg_match = rmp;
3708 rex.reg_mmatch = NULL;
3709 rex.reg_maxline = 0;
3710 rex.reg_line_lbr = line_lbr;
3711 rex.reg_buf = curbuf;
3712 rex.reg_win = NULL;
3713 rex.reg_ic = rmp->rm_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003715 rex.reg_icombine = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003717 rex.reg_maxcol = 0;
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003718
3719 return bt_regexec_both(line, col, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720}
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722/*
3723 * Match a regexp against multiple lines.
3724 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3725 * Uses curbuf for line count and 'iskeyword'.
3726 *
3727 * Return zero if there is no match. Return number of lines contained in the
3728 * match otherwise.
3729 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003730 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003731bt_regexec_multi(
3732 regmmatch_T *rmp,
3733 win_T *win, /* window in which to search or NULL */
3734 buf_T *buf, /* buffer in which to search */
3735 linenr_T lnum, /* nr of line to start looking for match */
3736 colnr_T col, /* column to start looking for match */
3737 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738{
Bram Moolenaar6100d022016-10-02 16:51:57 +02003739 rex.reg_match = NULL;
3740 rex.reg_mmatch = rmp;
3741 rex.reg_buf = buf;
3742 rex.reg_win = win;
3743 rex.reg_firstlnum = lnum;
3744 rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
3745 rex.reg_line_lbr = FALSE;
3746 rex.reg_ic = rmp->rmm_ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003748 rex.reg_icombine = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749#endif
Bram Moolenaar6100d022016-10-02 16:51:57 +02003750 rex.reg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003752 return bt_regexec_both(NULL, col, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753}
3754
3755/*
3756 * Match a regexp against a string ("line" points to the string) or multiple
3757 * lines ("line" is NULL, use reg_getline()).
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003758 * Returns 0 for failure, number of lines contained in the match otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01003761bt_regexec_both(
3762 char_u *line,
3763 colnr_T col, /* column to start looking for match */
3764 proftime_T *tm UNUSED) /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765{
Bram Moolenaar66a3e792014-11-20 23:07:05 +01003766 bt_regprog_T *prog;
3767 char_u *s;
3768 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003770 /* Create "regstack" and "backpos" if they are not allocated yet.
3771 * We allocate *_INITIAL amount of bytes first and then set the grow size
3772 * to much bigger value to avoid many malloc calls in case of deep regular
3773 * expressions. */
3774 if (regstack.ga_data == NULL)
3775 {
3776 /* Use an item size of 1 byte, since we push different things
3777 * onto the regstack. */
3778 ga_init2(&regstack, 1, REGSTACK_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003779 (void)ga_grow(&regstack, REGSTACK_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003780 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003783 if (backpos.ga_data == NULL)
3784 {
3785 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
Bram Moolenaarcde88542015-08-11 19:14:00 +02003786 (void)ga_grow(&backpos, BACKPOS_INITIAL);
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003787 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3788 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003789
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 if (REG_MULTI)
3791 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003792 prog = (bt_regprog_T *)rex.reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 line = reg_getline((linenr_T)0);
Bram Moolenaar6100d022016-10-02 16:51:57 +02003794 rex.reg_startpos = rex.reg_mmatch->startpos;
3795 rex.reg_endpos = rex.reg_mmatch->endpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797 else
3798 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02003799 prog = (bt_regprog_T *)rex.reg_match->regprog;
3800 rex.reg_startp = rex.reg_match->startp;
3801 rex.reg_endp = rex.reg_match->endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 }
3803
3804 /* Be paranoid... */
3805 if (prog == NULL || line == NULL)
3806 {
3807 EMSG(_(e_null));
3808 goto theend;
3809 }
3810
3811 /* Check validity of program. */
3812 if (prog_magic_wrong())
3813 goto theend;
3814
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003815 /* If the start column is past the maximum column: no need to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003816 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003817 goto theend;
3818
Bram Moolenaar6100d022016-10-02 16:51:57 +02003819 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 if (prog->regflags & RF_ICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003821 rex.reg_ic = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 else if (prog->regflags & RF_NOICASE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003823 rex.reg_ic = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824
3825#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003826 /* If pattern contains "\Z" overrule value of rex.reg_icombine */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 if (prog->regflags & RF_ICOMBINE)
Bram Moolenaar6100d022016-10-02 16:51:57 +02003828 rex.reg_icombine = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829#endif
3830
3831 /* If there is a "must appear" string, look for it. */
3832 if (prog->regmust != NULL)
3833 {
3834 int c;
3835
3836#ifdef FEAT_MBYTE
3837 if (has_mbyte)
3838 c = (*mb_ptr2char)(prog->regmust);
3839 else
3840#endif
3841 c = *prog->regmust;
3842 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003843
3844 /*
3845 * This is used very often, esp. for ":global". Use three versions of
3846 * the loop to avoid overhead of conditions.
3847 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003848 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003849#ifdef FEAT_MBYTE
3850 && !has_mbyte
3851#endif
3852 )
3853 while ((s = vim_strbyte(s, c)) != NULL)
3854 {
3855 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3856 break; /* Found it. */
3857 ++s;
3858 }
3859#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02003860 else if (!rex.reg_ic || (!enc_utf8 && mb_char2len(c) > 1))
Bram Moolenaar05159a02005-02-26 23:04:13 +00003861 while ((s = vim_strchr(s, c)) != NULL)
3862 {
3863 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3864 break; /* Found it. */
3865 mb_ptr_adv(s);
3866 }
3867#endif
3868 else
3869 while ((s = cstrchr(s, c)) != NULL)
3870 {
3871 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3872 break; /* Found it. */
3873 mb_ptr_adv(s);
3874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 if (s == NULL) /* Not present. */
3876 goto theend;
3877 }
3878
3879 regline = line;
3880 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003881 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
3883 /* Simplest case: Anchored match need be tried only once. */
3884 if (prog->reganch)
3885 {
3886 int c;
3887
3888#ifdef FEAT_MBYTE
3889 if (has_mbyte)
3890 c = (*mb_ptr2char)(regline + col);
3891 else
3892#endif
3893 c = regline[col];
3894 if (prog->regstart == NUL
3895 || prog->regstart == c
Bram Moolenaar6100d022016-10-02 16:51:57 +02003896 || (rex.reg_ic && ((
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897#ifdef FEAT_MBYTE
3898 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3899 || (c < 255 && prog->regstart < 255 &&
3900#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003901 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 retval = regtry(prog, col);
3903 else
3904 retval = 0;
3905 }
3906 else
3907 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003908#ifdef FEAT_RELTIME
3909 int tm_count = 0;
3910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003911 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003912 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 {
3914 if (prog->regstart != NUL)
3915 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003916 /* Skip until the char we know it must start with.
3917 * Used often, do some work to avoid call overhead. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003918 if (!rex.reg_ic
Bram Moolenaar05159a02005-02-26 23:04:13 +00003919#ifdef FEAT_MBYTE
3920 && !has_mbyte
3921#endif
3922 )
3923 s = vim_strbyte(regline + col, prog->regstart);
3924 else
3925 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 if (s == NULL)
3927 {
3928 retval = 0;
3929 break;
3930 }
3931 col = (int)(s - regline);
3932 }
3933
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003934 /* Check for maximum column to try. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02003935 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003936 {
3937 retval = 0;
3938 break;
3939 }
3940
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 retval = regtry(prog, col);
3942 if (retval > 0)
3943 break;
3944
3945 /* if not currently on the first line, get it again */
3946 if (reglnum != 0)
3947 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003949 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950 }
3951 if (regline[col] == NUL)
3952 break;
3953#ifdef FEAT_MBYTE
3954 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003955 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 else
3957#endif
3958 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003959#ifdef FEAT_RELTIME
3960 /* Check for timeout once in a twenty times to avoid overhead. */
3961 if (tm != NULL && ++tm_count == 20)
3962 {
3963 tm_count = 0;
3964 if (profile_passed_limit(tm))
3965 break;
3966 }
3967#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 }
3969 }
3970
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003972 /* Free "reg_tofree" when it's a bit big.
3973 * Free regstack and backpos if they are bigger than their initial size. */
3974 if (reg_tofreelen > 400)
3975 {
3976 vim_free(reg_tofree);
3977 reg_tofree = NULL;
3978 }
3979 if (regstack.ga_maxlen > REGSTACK_INITIAL)
3980 ga_clear(&regstack);
3981 if (backpos.ga_maxlen > BACKPOS_INITIAL)
3982 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003983
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984 return retval;
3985}
3986
3987#ifdef FEAT_SYN_HL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01003988static reg_extmatch_T *make_extmatch(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989
3990/*
3991 * Create a new extmatch and mark it as referenced once.
3992 */
3993 static reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01003994make_extmatch(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
3996 reg_extmatch_T *em;
3997
3998 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3999 if (em != NULL)
4000 em->refcnt = 1;
4001 return em;
4002}
4003
4004/*
4005 * Add a reference to an extmatch.
4006 */
4007 reg_extmatch_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01004008ref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009{
4010 if (em != NULL)
4011 em->refcnt++;
4012 return em;
4013}
4014
4015/*
4016 * Remove a reference to an extmatch. If there are no references left, free
4017 * the info.
4018 */
4019 void
Bram Moolenaar05540972016-01-30 20:31:25 +01004020unref_extmatch(reg_extmatch_T *em)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021{
4022 int i;
4023
4024 if (em != NULL && --em->refcnt <= 0)
4025 {
4026 for (i = 0; i < NSUBEXP; ++i)
4027 vim_free(em->matches[i]);
4028 vim_free(em);
4029 }
4030}
4031#endif
4032
4033/*
4034 * regtry - try match of "prog" with at regline["col"].
4035 * Returns 0 for failure, number of lines contained in the match otherwise.
4036 */
4037 static long
Bram Moolenaar05540972016-01-30 20:31:25 +01004038regtry(bt_regprog_T *prog, colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039{
4040 reginput = regline + col;
4041 need_clear_subexpr = TRUE;
4042#ifdef FEAT_SYN_HL
4043 /* Clear the external match subpointers if necessary. */
4044 if (prog->reghasz == REX_SET)
4045 need_clear_zsubexpr = TRUE;
4046#endif
4047
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004048 if (regmatch(prog->program + 1) == 0)
4049 return 0;
4050
4051 cleanup_subexpr();
4052 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004054 if (rex.reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004056 rex.reg_startpos[0].lnum = 0;
4057 rex.reg_startpos[0].col = col;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004058 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004059 if (rex.reg_endpos[0].lnum < 0)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004060 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004061 rex.reg_endpos[0].lnum = reglnum;
4062 rex.reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 }
4064 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004065 /* Use line number of "\ze". */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004066 reglnum = rex.reg_endpos[0].lnum;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004067 }
4068 else
4069 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004070 if (rex.reg_startp[0] == NULL)
4071 rex.reg_startp[0] = regline + col;
4072 if (rex.reg_endp[0] == NULL)
4073 rex.reg_endp[0] = reginput;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004076 /* Package any found \z(...\) matches for export. Default is none. */
4077 unref_extmatch(re_extmatch_out);
4078 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004080 if (prog->reghasz == REX_SET)
4081 {
4082 int i;
4083
4084 cleanup_zsubexpr();
4085 re_extmatch_out = make_extmatch();
4086 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004088 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004090 /* Only accept single line matches. */
4091 if (reg_startzpos[i].lnum >= 0
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02004092 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4093 && reg_endzpos[i].col >= reg_startzpos[i].col)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004094 re_extmatch_out->matches[i] =
4095 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004097 reg_endzpos[i].col - reg_startzpos[i].col);
4098 }
4099 else
4100 {
4101 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4102 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004103 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004104 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004108#endif
4109 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110}
4111
4112#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004113static int reg_prev_class(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004114
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115/*
4116 * Get class of previous character.
4117 */
4118 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004119reg_prev_class(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004120{
4121 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004122 return mb_get_class_buf(reginput - 1
Bram Moolenaar6100d022016-10-02 16:51:57 +02004123 - (*mb_head_off)(regline, reginput - 1), rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 return -1;
4125}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004127
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01004128static int reg_match_visual(void);
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004129
4130/*
4131 * Return TRUE if the current reginput position matches the Visual area.
4132 */
4133 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004134reg_match_visual(void)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004135{
4136 pos_T top, bot;
4137 linenr_T lnum;
4138 colnr_T col;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004139 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004140 int mode;
4141 colnr_T start, end;
4142 colnr_T start2, end2;
4143 colnr_T cols;
4144
4145 /* Check if the buffer is the current buffer. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004146 if (rex.reg_buf != curbuf || VIsual.lnum == 0)
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004147 return FALSE;
4148
4149 if (VIsual_active)
4150 {
4151 if (lt(VIsual, wp->w_cursor))
4152 {
4153 top = VIsual;
4154 bot = wp->w_cursor;
4155 }
4156 else
4157 {
4158 top = wp->w_cursor;
4159 bot = VIsual;
4160 }
4161 mode = VIsual_mode;
4162 }
4163 else
4164 {
4165 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
4166 {
4167 top = curbuf->b_visual.vi_start;
4168 bot = curbuf->b_visual.vi_end;
4169 }
4170 else
4171 {
4172 top = curbuf->b_visual.vi_end;
4173 bot = curbuf->b_visual.vi_start;
4174 }
4175 mode = curbuf->b_visual.vi_mode;
4176 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004177 lnum = reglnum + rex.reg_firstlnum;
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004178 if (lnum < top.lnum || lnum > bot.lnum)
4179 return FALSE;
4180
4181 if (mode == 'v')
4182 {
4183 col = (colnr_T)(reginput - regline);
4184 if ((lnum == top.lnum && col < top.col)
4185 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e')))
4186 return FALSE;
4187 }
4188 else if (mode == Ctrl_V)
4189 {
4190 getvvcol(wp, &top, &start, NULL, &end);
4191 getvvcol(wp, &bot, &start2, NULL, &end2);
4192 if (start2 < start)
4193 start = start2;
4194 if (end2 > end)
4195 end = end2;
4196 if (top.col == MAXCOL || bot.col == MAXCOL)
4197 end = MAXCOL;
4198 cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline));
4199 if (cols < start || cols > end - (*p_sel == 'e'))
4200 return FALSE;
4201 }
4202 return TRUE;
4203}
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004204
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004205#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
4207/*
4208 * The arguments from BRACE_LIMITS are stored here. They are actually local
4209 * to regmatch(), but they are here to reduce the amount of stack space used
4210 * (it can be called recursively many times).
4211 */
4212static long bl_minval;
4213static long bl_maxval;
4214
4215/*
4216 * regmatch - main matching routine
4217 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004218 * Conceptually the strategy is simple: Check to see whether the current node
4219 * matches, push an item onto the regstack and loop to see whether the rest
4220 * matches, and then act accordingly. In practice we make some effort to
4221 * avoid using the regstack, in particular by going through "ordinary" nodes
4222 * (that don't need to know whether the rest of the match failed) by a nested
4223 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 *
4225 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4226 * the last matched character.
4227 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4228 * undefined state!
4229 */
4230 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01004231regmatch(
4232 char_u *scan) /* Current node. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004234 char_u *next; /* Next node. */
4235 int op;
4236 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004237 regitem_T *rp;
4238 int no;
4239 int status; /* one of the RA_ values: */
4240#define RA_FAIL 1 /* something failed, abort */
4241#define RA_CONT 2 /* continue in inner loop */
4242#define RA_BREAK 3 /* break inner loop */
4243#define RA_MATCH 4 /* successful match */
4244#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004246 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004247 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004248 regstack.ga_len = 0;
4249 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004250
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004251 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004252 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004253 */
4254 for (;;)
4255 {
Bram Moolenaar41f12052013-08-25 17:01:42 +02004256 /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
4257 * Allow interrupting them with CTRL-C. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 fast_breakcheck();
4259
4260#ifdef DEBUG
4261 if (scan != NULL && regnarrate)
4262 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004263 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 mch_errmsg("(\n");
4265 }
4266#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004267
4268 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004269 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004270 * regstack.
4271 */
4272 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004274 if (got_int || scan == NULL)
4275 {
4276 status = RA_FAIL;
4277 break;
4278 }
4279 status = RA_CONT;
4280
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281#ifdef DEBUG
4282 if (regnarrate)
4283 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004284 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 mch_errmsg("...\n");
4286# ifdef FEAT_SYN_HL
4287 if (re_extmatch_in != NULL)
4288 {
4289 int i;
4290
4291 mch_errmsg(_("External submatches:\n"));
4292 for (i = 0; i < NSUBEXP; i++)
4293 {
4294 mch_errmsg(" \"");
4295 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004296 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 mch_errmsg("\"\n");
4298 }
4299 }
4300# endif
4301 }
4302#endif
4303 next = regnext(scan);
4304
4305 op = OP(scan);
4306 /* Check for character class with NL added. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004307 if (!rex.reg_line_lbr && WITH_NL(op) && REG_MULTI
4308 && *reginput == NUL && reglnum <= rex.reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
4310 reg_nextline();
4311 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02004312 else if (rex.reg_line_lbr && WITH_NL(op) && *reginput == '\n')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 {
4314 ADVANCE_REGINPUT();
4315 }
4316 else
4317 {
4318 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004319 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320#ifdef FEAT_MBYTE
4321 if (has_mbyte)
4322 c = (*mb_ptr2char)(reginput);
4323 else
4324#endif
4325 c = *reginput;
4326 switch (op)
4327 {
4328 case BOL:
4329 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004330 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 break;
4332
4333 case EOL:
4334 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004335 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004336 break;
4337
4338 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004339 /* We're not at the beginning of the file when below the first
4340 * line where we started, not at the start of the line or we
4341 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 if (reglnum != 0 || reginput != regline
Bram Moolenaar6100d022016-10-02 16:51:57 +02004343 || (REG_MULTI && rex.reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004344 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 break;
4346
4347 case RE_EOF:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004348 if (reglnum != rex.reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004349 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 break;
4351
4352 case CURSOR:
4353 /* Check if the buffer is in a window and compare the
Bram Moolenaar6100d022016-10-02 16:51:57 +02004354 * rex.reg_win->w_cursor position to the match position. */
4355 if (rex.reg_win == NULL
4356 || (reglnum + rex.reg_firstlnum
4357 != rex.reg_win->w_cursor.lnum)
4358 || ((colnr_T)(reginput - regline)
4359 != rex.reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004360 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 break;
4362
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004363 case RE_MARK:
Bram Moolenaar044aa292013-06-04 21:27:38 +02004364 /* Compare the mark position to the match position. */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004365 {
4366 int mark = OPERAND(scan)[0];
4367 int cmp = OPERAND(scan)[1];
4368 pos_T *pos;
4369
Bram Moolenaar6100d022016-10-02 16:51:57 +02004370 pos = getmark_buf(rex.reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004371 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar044aa292013-06-04 21:27:38 +02004372 || pos->lnum <= 0 /* mark isn't set in reg_buf */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004373 || (pos->lnum == reglnum + rex.reg_firstlnum
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004374 ? (pos->col == (colnr_T)(reginput - regline)
4375 ? (cmp == '<' || cmp == '>')
4376 : (pos->col < (colnr_T)(reginput - regline)
4377 ? cmp != '>'
4378 : cmp != '<'))
Bram Moolenaar6100d022016-10-02 16:51:57 +02004379 : (pos->lnum < reglnum + rex.reg_firstlnum
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004380 ? cmp != '>'
4381 : cmp != '<')))
4382 status = RA_NOMATCH;
4383 }
4384 break;
4385
4386 case RE_VISUAL:
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004387 if (!reg_match_visual())
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004388 status = RA_NOMATCH;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004389 break;
4390
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 case RE_LNUM:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004392 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + rex.reg_firstlnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004394 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 break;
4396
4397 case RE_COL:
4398 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004399 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 break;
4401
4402 case RE_VCOL:
4403 if (!re_num_cmp((long_u)win_linetabsize(
Bram Moolenaar6100d022016-10-02 16:51:57 +02004404 rex.reg_win == NULL ? curwin : rex.reg_win,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004406 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 break;
4408
4409 case BOW: /* \<word; reginput points to w */
4410 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004411 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004413 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 {
4415 int this_class;
4416
4417 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004418 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004420 status = RA_NOMATCH; /* not on a word at all */
4421 else if (reg_prev_class() == this_class)
4422 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 }
4424#endif
4425 else
4426 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004427 if (!vim_iswordc_buf(c, rex.reg_buf) || (reginput > regline
4428 && vim_iswordc_buf(reginput[-1], rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004429 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 }
4431 break;
4432
4433 case EOW: /* word\>; reginput points after d */
4434 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004435 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004437 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 {
4439 int this_class, prev_class;
4440
4441 /* Get class of current and previous char (if it exists). */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004442 this_class = mb_get_class_buf(reginput, rex.reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004444 if (this_class == prev_class
4445 || prev_class == 0 || prev_class == 1)
4446 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004449 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004451 if (!vim_iswordc_buf(reginput[-1], rex.reg_buf)
4452 || (reginput[0] != NUL
4453 && vim_iswordc_buf(c, rex.reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004454 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 }
4456 break; /* Matched with EOW */
4457
4458 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004459 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004461 status = RA_NOMATCH;
4462 else
4463 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 break;
4465
4466 case IDENT:
4467 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004468 status = RA_NOMATCH;
4469 else
4470 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 break;
4472
4473 case SIDENT:
4474 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004475 status = RA_NOMATCH;
4476 else
4477 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 break;
4479
4480 case KWORD:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004481 if (!vim_iswordp_buf(reginput, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004482 status = RA_NOMATCH;
4483 else
4484 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 break;
4486
4487 case SKWORD:
Bram Moolenaar6100d022016-10-02 16:51:57 +02004488 if (VIM_ISDIGIT(*reginput)
4489 || !vim_iswordp_buf(reginput, rex.reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004490 status = RA_NOMATCH;
4491 else
4492 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 break;
4494
4495 case FNAME:
4496 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004497 status = RA_NOMATCH;
4498 else
4499 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 break;
4501
4502 case SFNAME:
4503 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004504 status = RA_NOMATCH;
4505 else
4506 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 break;
4508
4509 case PRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004510 if (!vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004511 status = RA_NOMATCH;
4512 else
4513 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 break;
4515
4516 case SPRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004517 if (VIM_ISDIGIT(*reginput) || !vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004518 status = RA_NOMATCH;
4519 else
4520 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 break;
4522
4523 case WHITE:
4524 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004525 status = RA_NOMATCH;
4526 else
4527 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 break;
4529
4530 case NWHITE:
4531 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004532 status = RA_NOMATCH;
4533 else
4534 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 break;
4536
4537 case DIGIT:
4538 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004539 status = RA_NOMATCH;
4540 else
4541 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 break;
4543
4544 case NDIGIT:
4545 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004546 status = RA_NOMATCH;
4547 else
4548 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 break;
4550
4551 case HEX:
4552 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004553 status = RA_NOMATCH;
4554 else
4555 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 break;
4557
4558 case NHEX:
4559 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004560 status = RA_NOMATCH;
4561 else
4562 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 break;
4564
4565 case OCTAL:
4566 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004567 status = RA_NOMATCH;
4568 else
4569 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 break;
4571
4572 case NOCTAL:
4573 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004574 status = RA_NOMATCH;
4575 else
4576 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 break;
4578
4579 case WORD:
4580 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004581 status = RA_NOMATCH;
4582 else
4583 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004584 break;
4585
4586 case NWORD:
4587 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004588 status = RA_NOMATCH;
4589 else
4590 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 break;
4592
4593 case HEAD:
4594 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004595 status = RA_NOMATCH;
4596 else
4597 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598 break;
4599
4600 case NHEAD:
4601 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004602 status = RA_NOMATCH;
4603 else
4604 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 break;
4606
4607 case ALPHA:
4608 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004609 status = RA_NOMATCH;
4610 else
4611 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 break;
4613
4614 case NALPHA:
4615 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004616 status = RA_NOMATCH;
4617 else
4618 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 break;
4620
4621 case LOWER:
4622 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004623 status = RA_NOMATCH;
4624 else
4625 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 break;
4627
4628 case NLOWER:
4629 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004630 status = RA_NOMATCH;
4631 else
4632 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 break;
4634
4635 case UPPER:
4636 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004637 status = RA_NOMATCH;
4638 else
4639 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 break;
4641
4642 case NUPPER:
4643 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004644 status = RA_NOMATCH;
4645 else
4646 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004647 break;
4648
4649 case EXACTLY:
4650 {
4651 int len;
4652 char_u *opnd;
4653
4654 opnd = OPERAND(scan);
4655 /* Inline the first byte, for speed. */
4656 if (*opnd != *reginput
Bram Moolenaar6100d022016-10-02 16:51:57 +02004657 && (!rex.reg_ic || (
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658#ifdef FEAT_MBYTE
4659 !enc_utf8 &&
4660#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004661 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004662 status = RA_NOMATCH;
4663 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004664 {
4665 /* match empty string always works; happens when "~" is
4666 * empty. */
4667 }
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004668 else
4669 {
4670 if (opnd[1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671#ifdef FEAT_MBYTE
Bram Moolenaar6100d022016-10-02 16:51:57 +02004672 && !(enc_utf8 && rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673#endif
4674 )
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004675 {
4676 len = 1; /* matched a single byte above */
4677 }
4678 else
4679 {
4680 /* Need to match first byte again for multi-byte. */
4681 len = (int)STRLEN(opnd);
4682 if (cstrncmp(opnd, reginput, &len) != 0)
4683 status = RA_NOMATCH;
4684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004686 /* Check for following composing character, unless %C
4687 * follows (skips over all composing chars). */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004688 if (status != RA_NOMATCH
4689 && enc_utf8
4690 && UTF_COMPOSINGLIKE(reginput, reginput + len)
Bram Moolenaar6100d022016-10-02 16:51:57 +02004691 && !rex.reg_icombine
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004692 && OP(next) != RE_COMPOSING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
4694 /* raaron: This code makes a composing character get
4695 * ignored, which is the correct behavior (sometimes)
4696 * for voweled Hebrew texts. */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004697 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699#endif
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004700 if (status != RA_NOMATCH)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004701 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 }
4703 }
4704 break;
4705
4706 case ANYOF:
4707 case ANYBUT:
4708 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004709 status = RA_NOMATCH;
4710 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4711 status = RA_NOMATCH;
4712 else
4713 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 break;
4715
4716#ifdef FEAT_MBYTE
4717 case MULTIBYTECODE:
4718 if (has_mbyte)
4719 {
4720 int i, len;
4721 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004722 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723
4724 opnd = OPERAND(scan);
4725 /* Safety check (just in case 'encoding' was changed since
4726 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004727 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004728 {
4729 status = RA_NOMATCH;
4730 break;
4731 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004732 if (enc_utf8)
4733 opndc = mb_ptr2char(opnd);
4734 if (enc_utf8 && utf_iscomposing(opndc))
4735 {
4736 /* When only a composing char is given match at any
4737 * position where that composing char appears. */
4738 status = RA_NOMATCH;
Bram Moolenaar0e462412015-03-31 14:17:31 +02004739 for (i = 0; reginput[i] != NUL;
4740 i += utf_ptr2len(reginput + i))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004741 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004742 inpc = mb_ptr2char(reginput + i);
4743 if (!utf_iscomposing(inpc))
4744 {
4745 if (i > 0)
4746 break;
4747 }
4748 else if (opndc == inpc)
4749 {
4750 /* Include all following composing chars. */
4751 len = i + mb_ptr2len(reginput + i);
4752 status = RA_MATCH;
4753 break;
4754 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004755 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004756 }
4757 else
4758 for (i = 0; i < len; ++i)
4759 if (opnd[i] != reginput[i])
4760 {
4761 status = RA_NOMATCH;
4762 break;
4763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 reginput += len;
4765 }
4766 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004767 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 break;
4769#endif
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004770 case RE_COMPOSING:
4771#ifdef FEAT_MBYTE
4772 if (enc_utf8)
4773 {
4774 /* Skip composing characters. */
4775 while (utf_iscomposing(utf_ptr2char(reginput)))
4776 mb_cptr_adv(reginput);
4777 }
4778#endif
4779 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
4781 case NOTHING:
4782 break;
4783
4784 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004785 {
4786 int i;
4787 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788
Bram Moolenaar582fd852005-03-28 20:58:01 +00004789 /*
4790 * When we run into BACK we need to check if we don't keep
4791 * looping without matching any input. The second and later
4792 * times a BACK is encountered it fails if the input is still
4793 * at the same position as the previous time.
4794 * The positions are stored in "backpos" and found by the
4795 * current value of "scan", the position in the RE program.
4796 */
4797 bp = (backpos_T *)backpos.ga_data;
4798 for (i = 0; i < backpos.ga_len; ++i)
4799 if (bp[i].bp_scan == scan)
4800 break;
4801 if (i == backpos.ga_len)
4802 {
4803 /* First time at this BACK, make room to store the pos. */
4804 if (ga_grow(&backpos, 1) == FAIL)
4805 status = RA_FAIL;
4806 else
4807 {
4808 /* get "ga_data" again, it may have changed */
4809 bp = (backpos_T *)backpos.ga_data;
4810 bp[i].bp_scan = scan;
4811 ++backpos.ga_len;
4812 }
4813 }
4814 else if (reg_save_equal(&bp[i].bp_pos))
4815 /* Still at same position as last time, fail. */
4816 status = RA_NOMATCH;
4817
4818 if (status != RA_FAIL && status != RA_NOMATCH)
4819 reg_save(&bp[i].bp_pos, &backpos);
4820 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004821 break;
4822
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 case MOPEN + 0: /* Match start: \zs */
4824 case MOPEN + 1: /* \( */
4825 case MOPEN + 2:
4826 case MOPEN + 3:
4827 case MOPEN + 4:
4828 case MOPEN + 5:
4829 case MOPEN + 6:
4830 case MOPEN + 7:
4831 case MOPEN + 8:
4832 case MOPEN + 9:
4833 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 no = op - MOPEN;
4835 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004836 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004837 if (rp == NULL)
4838 status = RA_FAIL;
4839 else
4840 {
4841 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004842 save_se(&rp->rs_un.sesave, &rex.reg_startpos[no],
4843 &rex.reg_startp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004844 /* We simply continue and handle the result when done. */
4845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004847 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848
4849 case NOPEN: /* \%( */
4850 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004851 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004852 status = RA_FAIL;
4853 /* We simply continue and handle the result when done. */
4854 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855
4856#ifdef FEAT_SYN_HL
4857 case ZOPEN + 1:
4858 case ZOPEN + 2:
4859 case ZOPEN + 3:
4860 case ZOPEN + 4:
4861 case ZOPEN + 5:
4862 case ZOPEN + 6:
4863 case ZOPEN + 7:
4864 case ZOPEN + 8:
4865 case ZOPEN + 9:
4866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 no = op - ZOPEN;
4868 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004869 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004870 if (rp == NULL)
4871 status = RA_FAIL;
4872 else
4873 {
4874 rp->rs_no = no;
4875 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4876 &reg_startzp[no]);
4877 /* We simply continue and handle the result when done. */
4878 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004880 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881#endif
4882
4883 case MCLOSE + 0: /* Match end: \ze */
4884 case MCLOSE + 1: /* \) */
4885 case MCLOSE + 2:
4886 case MCLOSE + 3:
4887 case MCLOSE + 4:
4888 case MCLOSE + 5:
4889 case MCLOSE + 6:
4890 case MCLOSE + 7:
4891 case MCLOSE + 8:
4892 case MCLOSE + 9:
4893 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 no = op - MCLOSE;
4895 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004896 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004897 if (rp == NULL)
4898 status = RA_FAIL;
4899 else
4900 {
4901 rp->rs_no = no;
Bram Moolenaar6100d022016-10-02 16:51:57 +02004902 save_se(&rp->rs_un.sesave, &rex.reg_endpos[no],
4903 &rex.reg_endp[no]);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004904 /* We simply continue and handle the result when done. */
4905 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004907 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
4909#ifdef FEAT_SYN_HL
4910 case ZCLOSE + 1: /* \) after \z( */
4911 case ZCLOSE + 2:
4912 case ZCLOSE + 3:
4913 case ZCLOSE + 4:
4914 case ZCLOSE + 5:
4915 case ZCLOSE + 6:
4916 case ZCLOSE + 7:
4917 case ZCLOSE + 8:
4918 case ZCLOSE + 9:
4919 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 no = op - ZCLOSE;
4921 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004922 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004923 if (rp == NULL)
4924 status = RA_FAIL;
4925 else
4926 {
4927 rp->rs_no = no;
4928 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4929 &reg_endzp[no]);
4930 /* We simply continue and handle the result when done. */
4931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004933 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934#endif
4935
4936 case BACKREF + 1:
4937 case BACKREF + 2:
4938 case BACKREF + 3:
4939 case BACKREF + 4:
4940 case BACKREF + 5:
4941 case BACKREF + 6:
4942 case BACKREF + 7:
4943 case BACKREF + 8:
4944 case BACKREF + 9:
4945 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
4948 no = op - BACKREF;
4949 cleanup_subexpr();
4950 if (!REG_MULTI) /* Single-line regexp */
4951 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004952 if (rex.reg_startp[no] == NULL || rex.reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
4954 /* Backref was not set: Match an empty string. */
4955 len = 0;
4956 }
4957 else
4958 {
4959 /* Compare current input with back-ref in the same
4960 * line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004961 len = (int)(rex.reg_endp[no] - rex.reg_startp[no]);
4962 if (cstrncmp(rex.reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004963 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 }
4965 }
4966 else /* Multi-line regexp */
4967 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004968 if (rex.reg_startpos[no].lnum < 0
4969 || rex.reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
4971 /* Backref was not set: Match an empty string. */
4972 len = 0;
4973 }
4974 else
4975 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02004976 if (rex.reg_startpos[no].lnum == reglnum
4977 && rex.reg_endpos[no].lnum == reglnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
4979 /* Compare back-ref within the current line. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02004980 len = rex.reg_endpos[no].col
4981 - rex.reg_startpos[no].col;
4982 if (cstrncmp(regline + rex.reg_startpos[no].col,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004984 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 }
4986 else
4987 {
4988 /* Messy situation: Need to compare between two
4989 * lines. */
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02004990 int r = match_with_backref(
Bram Moolenaar6100d022016-10-02 16:51:57 +02004991 rex.reg_startpos[no].lnum,
4992 rex.reg_startpos[no].col,
4993 rex.reg_endpos[no].lnum,
4994 rex.reg_endpos[no].col,
Bram Moolenaar4cff8fa2013-06-14 22:48:54 +02004995 &len);
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02004996
4997 if (r != RA_MATCH)
4998 status = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
5000 }
5001 }
5002
5003 /* Matched the backref, skip over it. */
5004 reginput += len;
5005 }
5006 break;
5007
5008#ifdef FEAT_SYN_HL
5009 case ZREF + 1:
5010 case ZREF + 2:
5011 case ZREF + 3:
5012 case ZREF + 4:
5013 case ZREF + 5:
5014 case ZREF + 6:
5015 case ZREF + 7:
5016 case ZREF + 8:
5017 case ZREF + 9:
5018 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 int len;
5020
5021 cleanup_zsubexpr();
5022 no = op - ZREF;
5023 if (re_extmatch_in != NULL
5024 && re_extmatch_in->matches[no] != NULL)
5025 {
5026 len = (int)STRLEN(re_extmatch_in->matches[no]);
5027 if (cstrncmp(re_extmatch_in->matches[no],
5028 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005029 status = RA_NOMATCH;
5030 else
5031 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
5033 else
5034 {
5035 /* Backref was not set: Match an empty string. */
5036 }
5037 }
5038 break;
5039#endif
5040
5041 case BRANCH:
5042 {
5043 if (OP(next) != BRANCH) /* No choice. */
5044 next = OPERAND(scan); /* Avoid recursion. */
5045 else
5046 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005047 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005048 if (rp == NULL)
5049 status = RA_FAIL;
5050 else
5051 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053 }
5054 break;
5055
5056 case BRACE_LIMITS:
5057 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 if (OP(next) == BRACE_SIMPLE)
5059 {
5060 bl_minval = OPERAND_MIN(scan);
5061 bl_maxval = OPERAND_MAX(scan);
5062 }
5063 else if (OP(next) >= BRACE_COMPLEX
5064 && OP(next) < BRACE_COMPLEX + 10)
5065 {
5066 no = OP(next) - BRACE_COMPLEX;
5067 brace_min[no] = OPERAND_MIN(scan);
5068 brace_max[no] = OPERAND_MAX(scan);
5069 brace_count[no] = 0;
5070 }
5071 else
5072 {
5073 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005074 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 }
5076 }
5077 break;
5078
5079 case BRACE_COMPLEX + 0:
5080 case BRACE_COMPLEX + 1:
5081 case BRACE_COMPLEX + 2:
5082 case BRACE_COMPLEX + 3:
5083 case BRACE_COMPLEX + 4:
5084 case BRACE_COMPLEX + 5:
5085 case BRACE_COMPLEX + 6:
5086 case BRACE_COMPLEX + 7:
5087 case BRACE_COMPLEX + 8:
5088 case BRACE_COMPLEX + 9:
5089 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 no = op - BRACE_COMPLEX;
5091 ++brace_count[no];
5092
5093 /* If not matched enough times yet, try one more */
5094 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005095 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005097 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005098 if (rp == NULL)
5099 status = RA_FAIL;
5100 else
5101 {
5102 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005103 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005104 next = OPERAND(scan);
5105 /* We continue and handle the result when done. */
5106 }
5107 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108 }
5109
5110 /* If matched enough times, may try matching some more */
5111 if (brace_min[no] <= brace_max[no])
5112 {
5113 /* Range is the normal way around, use longest match */
5114 if (brace_count[no] <= brace_max[no])
5115 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005116 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005117 if (rp == NULL)
5118 status = RA_FAIL;
5119 else
5120 {
5121 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005122 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005123 next = OPERAND(scan);
5124 /* We continue and handle the result when done. */
5125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 }
5128 else
5129 {
5130 /* Range is backwards, use shortest match first */
5131 if (brace_count[no] <= brace_min[no])
5132 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005133 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005134 if (rp == NULL)
5135 status = RA_FAIL;
5136 else
5137 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005138 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005139 /* We continue and handle the result when done. */
5140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 }
5142 }
5143 }
5144 break;
5145
5146 case BRACE_SIMPLE:
5147 case STAR:
5148 case PLUS:
5149 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005150 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151
5152 /*
5153 * Lookahead to avoid useless match attempts when we know
5154 * what character comes next.
5155 */
5156 if (OP(next) == EXACTLY)
5157 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005158 rst.nextb = *OPERAND(next);
Bram Moolenaar6100d022016-10-02 16:51:57 +02005159 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005161 if (MB_ISUPPER(rst.nextb))
5162 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005164 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005167 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
5169 else
5170 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005171 rst.nextb = NUL;
5172 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174 if (op != BRACE_SIMPLE)
5175 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005176 rst.minval = (op == STAR) ? 0 : 1;
5177 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 }
5179 else
5180 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005181 rst.minval = bl_minval;
5182 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184
5185 /*
5186 * When maxval > minval, try matching as much as possible, up
5187 * to maxval. When maxval < minval, try matching at least the
5188 * minimal number (since the range is backwards, that's also
5189 * maxval!).
5190 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005191 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005194 status = RA_FAIL;
5195 break;
5196 }
5197 if (rst.minval <= rst.maxval
5198 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5199 {
5200 /* It could match. Prepare for trying to match what
5201 * follows. The code is below. Parameters are stored in
5202 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005203 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005204 {
5205 EMSG(_(e_maxmempat));
5206 status = RA_FAIL;
5207 }
5208 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005209 status = RA_FAIL;
5210 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005212 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005213 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005214 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005215 if (rp == NULL)
5216 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005218 {
5219 *(((regstar_T *)rp) - 1) = rst;
5220 status = RA_BREAK; /* skip the restore bits */
5221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 }
5223 }
5224 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005225 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 }
5228 break;
5229
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005230 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 case MATCH:
5232 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005233 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005234 if (rp == NULL)
5235 status = RA_FAIL;
5236 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005238 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005239 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005240 next = OPERAND(scan);
5241 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005242 }
5243 break;
5244
5245 case BEHIND:
5246 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005247 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005248 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005249 {
5250 EMSG(_(e_maxmempat));
5251 status = RA_FAIL;
5252 }
5253 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005254 status = RA_FAIL;
5255 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005257 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005258 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005259 if (rp == NULL)
5260 status = RA_FAIL;
5261 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005263 /* Need to save the subexpr to be able to restore them
5264 * when there is a match but we don't use it. */
5265 save_subexpr(((regbehind_T *)rp) - 1);
5266
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005267 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005268 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005269 /* First try if what follows matches. If it does then we
5270 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005273 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005274
5275 case BHPOS:
5276 if (REG_MULTI)
5277 {
5278 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5279 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005280 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005283 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 break;
5285
5286 case NEWL:
Bram Moolenaar6100d022016-10-02 16:51:57 +02005287 if ((c != NUL || !REG_MULTI || reglnum > rex.reg_maxline
5288 || rex.reg_line_lbr)
5289 && (c != '\n' || !rex.reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005290 status = RA_NOMATCH;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005291 else if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 ADVANCE_REGINPUT();
5293 else
5294 reg_nextline();
5295 break;
5296
5297 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005298 status = RA_MATCH; /* Success! */
5299 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 default:
5302 EMSG(_(e_re_corr));
5303#ifdef DEBUG
5304 printf("Illegal op code %d\n", op);
5305#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005306 status = RA_FAIL;
5307 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 }
5310
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005311 /* If we can't continue sequentially, break the inner loop. */
5312 if (status != RA_CONT)
5313 break;
5314
5315 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005317
5318 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
5320 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005321 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005322 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005324 while (regstack.ga_len > 0 && status != RA_FAIL)
5325 {
5326 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5327 switch (rp->rs_state)
5328 {
5329 case RS_NOPEN:
5330 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005331 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005332 break;
5333
5334 case RS_MOPEN:
5335 /* Pop the state. Restore pointers when there is no match. */
5336 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005337 restore_se(&rp->rs_un.sesave, &rex.reg_startpos[rp->rs_no],
5338 &rex.reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005339 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005340 break;
5341
5342#ifdef FEAT_SYN_HL
5343 case RS_ZOPEN:
5344 /* Pop the state. Restore pointers when there is no match. */
5345 if (status == RA_NOMATCH)
5346 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5347 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005348 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005351
5352 case RS_MCLOSE:
5353 /* Pop the state. Restore pointers when there is no match. */
5354 if (status == RA_NOMATCH)
Bram Moolenaar6100d022016-10-02 16:51:57 +02005355 restore_se(&rp->rs_un.sesave, &rex.reg_endpos[rp->rs_no],
5356 &rex.reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005357 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005358 break;
5359
5360#ifdef FEAT_SYN_HL
5361 case RS_ZCLOSE:
5362 /* Pop the state. Restore pointers when there is no match. */
5363 if (status == RA_NOMATCH)
5364 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5365 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005366 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005367 break;
5368#endif
5369
5370 case RS_BRANCH:
5371 if (status == RA_MATCH)
5372 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005373 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005374 else
5375 {
5376 if (status != RA_BREAK)
5377 {
5378 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005379 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005380 scan = rp->rs_scan;
5381 }
5382 if (scan == NULL || OP(scan) != BRANCH)
5383 {
5384 /* no more branches, didn't find a match */
5385 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005386 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005387 }
5388 else
5389 {
5390 /* Prepare to try a branch. */
5391 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005392 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005393 scan = OPERAND(scan);
5394 }
5395 }
5396 break;
5397
5398 case RS_BRCPLX_MORE:
5399 /* Pop the state. Restore pointers when there is no match. */
5400 if (status == RA_NOMATCH)
5401 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005402 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005403 --brace_count[rp->rs_no]; /* decrement match count */
5404 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005405 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005406 break;
5407
5408 case RS_BRCPLX_LONG:
5409 /* Pop the state. Restore pointers when there is no match. */
5410 if (status == RA_NOMATCH)
5411 {
5412 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005413 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005414 --brace_count[rp->rs_no];
5415 /* continue with the items after "\{}" */
5416 status = RA_CONT;
5417 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005418 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005419 if (status == RA_CONT)
5420 scan = regnext(scan);
5421 break;
5422
5423 case RS_BRCPLX_SHORT:
5424 /* Pop the state. Restore pointers when there is no match. */
5425 if (status == RA_NOMATCH)
5426 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005427 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005428 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005429 if (status == RA_NOMATCH)
5430 {
5431 scan = OPERAND(scan);
5432 status = RA_CONT;
5433 }
5434 break;
5435
5436 case RS_NOMATCH:
5437 /* Pop the state. If the operand matches for NOMATCH or
5438 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5439 * except for SUBPAT, and continue with the next item. */
5440 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5441 status = RA_NOMATCH;
5442 else
5443 {
5444 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005445 if (rp->rs_no != SUBPAT) /* zero-width */
5446 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005447 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005448 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005449 if (status == RA_CONT)
5450 scan = regnext(scan);
5451 break;
5452
5453 case RS_BEHIND1:
5454 if (status == RA_NOMATCH)
5455 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005456 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005457 regstack.ga_len -= sizeof(regbehind_T);
5458 }
5459 else
5460 {
5461 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5462 * the behind part does (not) match before the current
5463 * position in the input. This must be done at every
5464 * position in the input and checking if the match ends at
5465 * the current position. */
5466
5467 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005468 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005469
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005470 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005471 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005472 * result, hitting the start of the line or the previous
5473 * line (for multi-line matching).
5474 * Set behind_pos to where the match should end, BHPOS
5475 * will match it. Save the current value. */
5476 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5477 behind_pos = rp->rs_un.regsave;
5478
5479 rp->rs_state = RS_BEHIND2;
5480
Bram Moolenaar582fd852005-03-28 20:58:01 +00005481 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005482 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005483 }
5484 break;
5485
5486 case RS_BEHIND2:
5487 /*
5488 * Looping for BEHIND / NOBEHIND match.
5489 */
5490 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5491 {
5492 /* found a match that ends where "next" started */
5493 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5494 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005495 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5496 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005497 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005498 {
5499 /* But we didn't want a match. Need to restore the
5500 * subexpr, because what follows matched, so they have
5501 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005502 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005503 restore_subexpr(((regbehind_T *)rp) - 1);
5504 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005505 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005506 regstack.ga_len -= sizeof(regbehind_T);
5507 }
5508 else
5509 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005510 long limit;
5511
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005512 /* No match or a match that doesn't end where we want it: Go
5513 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005514 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005515 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005516 if (REG_MULTI)
5517 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005518 if (limit > 0
5519 && ((rp->rs_un.regsave.rs_u.pos.lnum
5520 < behind_pos.rs_u.pos.lnum
5521 ? (colnr_T)STRLEN(regline)
5522 : behind_pos.rs_u.pos.col)
5523 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5524 no = FAIL;
5525 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005526 {
5527 if (rp->rs_un.regsave.rs_u.pos.lnum
5528 < behind_pos.rs_u.pos.lnum
5529 || reg_getline(
5530 --rp->rs_un.regsave.rs_u.pos.lnum)
5531 == NULL)
5532 no = FAIL;
5533 else
5534 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005535 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005536 rp->rs_un.regsave.rs_u.pos.col =
5537 (colnr_T)STRLEN(regline);
5538 }
5539 }
5540 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005541 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005542#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005543 if (has_mbyte)
5544 rp->rs_un.regsave.rs_u.pos.col -=
5545 (*mb_head_off)(regline, regline
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005546 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005547 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005548#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005549 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005550 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005551 }
5552 else
5553 {
5554 if (rp->rs_un.regsave.rs_u.ptr == regline)
5555 no = FAIL;
5556 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005557 {
5558 mb_ptr_back(regline, rp->rs_un.regsave.rs_u.ptr);
5559 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5560 - rp->rs_un.regsave.rs_u.ptr) > limit)
5561 no = FAIL;
5562 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005563 }
5564 if (no == OK)
5565 {
5566 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005567 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005568 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005569 if (status == RA_MATCH)
5570 {
5571 /* We did match, so subexpr may have been changed,
5572 * need to restore them for the next try. */
5573 status = RA_NOMATCH;
5574 restore_subexpr(((regbehind_T *)rp) - 1);
5575 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005576 }
5577 else
5578 {
5579 /* Can't advance. For NOBEHIND that's a match. */
5580 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5581 if (rp->rs_no == NOBEHIND)
5582 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005583 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5584 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005585 status = RA_MATCH;
5586 }
5587 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005588 {
5589 /* We do want a proper match. Need to restore the
5590 * subexpr if we had a match, because they may have
5591 * been set. */
5592 if (status == RA_MATCH)
5593 {
5594 status = RA_NOMATCH;
5595 restore_subexpr(((regbehind_T *)rp) - 1);
5596 }
5597 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005598 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005599 regstack.ga_len -= sizeof(regbehind_T);
5600 }
5601 }
5602 break;
5603
5604 case RS_STAR_LONG:
5605 case RS_STAR_SHORT:
5606 {
5607 regstar_T *rst = ((regstar_T *)rp) - 1;
5608
5609 if (status == RA_MATCH)
5610 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005611 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005612 regstack.ga_len -= sizeof(regstar_T);
5613 break;
5614 }
5615
5616 /* Tried once already, restore input pointers. */
5617 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005618 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005619
5620 /* Repeat until we found a position where it could match. */
5621 for (;;)
5622 {
5623 if (status != RA_BREAK)
5624 {
5625 /* Tried first position already, advance. */
5626 if (rp->rs_state == RS_STAR_LONG)
5627 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005628 /* Trying for longest match, but couldn't or
5629 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005630 if (--rst->count < rst->minval)
5631 break;
5632 if (reginput == regline)
5633 {
5634 /* backup to last char of previous line */
5635 --reglnum;
5636 regline = reg_getline(reglnum);
5637 /* Just in case regrepeat() didn't count
5638 * right. */
5639 if (regline == NULL)
5640 break;
5641 reginput = regline + STRLEN(regline);
5642 fast_breakcheck();
5643 }
5644 else
5645 mb_ptr_back(regline, reginput);
5646 }
5647 else
5648 {
5649 /* Range is backwards, use shortest match first.
5650 * Careful: maxval and minval are exchanged!
5651 * Couldn't or didn't match: try advancing one
5652 * char. */
5653 if (rst->count == rst->minval
5654 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5655 break;
5656 ++rst->count;
5657 }
5658 if (got_int)
5659 break;
5660 }
5661 else
5662 status = RA_NOMATCH;
5663
5664 /* If it could match, try it. */
5665 if (rst->nextb == NUL || *reginput == rst->nextb
5666 || *reginput == rst->nextb_ic)
5667 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005668 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005669 scan = regnext(rp->rs_scan);
5670 status = RA_CONT;
5671 break;
5672 }
5673 }
5674 if (status != RA_CONT)
5675 {
5676 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005677 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005678 regstack.ga_len -= sizeof(regstar_T);
5679 status = RA_NOMATCH;
5680 }
5681 }
5682 break;
5683 }
5684
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005685 /* If we want to continue the inner loop or didn't pop a state
5686 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005687 if (status == RA_CONT || rp == (regitem_T *)
5688 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5689 break;
5690 }
5691
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005692 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005693 if (status == RA_CONT)
5694 continue;
5695
5696 /*
5697 * If the regstack is empty or something failed we are done.
5698 */
5699 if (regstack.ga_len == 0 || status == RA_FAIL)
5700 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005701 if (scan == NULL)
5702 {
5703 /*
5704 * We get here only if there's trouble -- normally "case END" is
5705 * the terminating point.
5706 */
5707 EMSG(_(e_re_corr));
5708#ifdef DEBUG
5709 printf("Premature EOL\n");
5710#endif
5711 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005712 if (status == RA_FAIL)
5713 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005714 return (status == RA_MATCH);
5715 }
5716
5717 } /* End of loop until the regstack is empty. */
5718
5719 /* NOTREACHED */
5720}
5721
5722/*
5723 * Push an item onto the regstack.
5724 * Returns pointer to new item. Returns NULL when out of memory.
5725 */
5726 static regitem_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01005727regstack_push(regstate_T state, char_u *scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005728{
5729 regitem_T *rp;
5730
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005731 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005732 {
5733 EMSG(_(e_maxmempat));
5734 return NULL;
5735 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005736 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005737 return NULL;
5738
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005739 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005740 rp->rs_state = state;
5741 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005742
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005743 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005744 return rp;
5745}
5746
5747/*
5748 * Pop an item from the regstack.
5749 */
5750 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01005751regstack_pop(char_u **scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005752{
5753 regitem_T *rp;
5754
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005755 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005756 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005757
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005758 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759}
5760
Bram Moolenaar071d4272004-06-13 20:20:40 +00005761/*
5762 * regrepeat - repeatedly match something simple, return how many.
5763 * Advances reginput (and reglnum) to just after the matched chars.
5764 */
5765 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01005766regrepeat(
5767 char_u *p,
5768 long maxcount) /* maximum number of matches allowed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769{
5770 long count = 0;
5771 char_u *scan;
5772 char_u *opnd;
5773 int mask;
5774 int testval = 0;
5775
5776 scan = reginput; /* Make local copy of reginput for speed. */
5777 opnd = OPERAND(p);
5778 switch (OP(p))
5779 {
5780 case ANY:
5781 case ANY + ADD_NL:
5782 while (count < maxcount)
5783 {
5784 /* Matching anything means we continue until end-of-line (or
5785 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5786 while (*scan != NUL && count < maxcount)
5787 {
5788 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005789 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005791 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5792 || rex.reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793 break;
5794 ++count; /* count the line-break */
5795 reg_nextline();
5796 scan = reginput;
5797 if (got_int)
5798 break;
5799 }
5800 break;
5801
5802 case IDENT:
5803 case IDENT + ADD_NL:
5804 testval = TRUE;
5805 /*FALLTHROUGH*/
5806 case SIDENT:
5807 case SIDENT + ADD_NL:
5808 while (count < maxcount)
5809 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005810 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005812 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 }
5814 else if (*scan == NUL)
5815 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005816 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5817 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 break;
5819 reg_nextline();
5820 scan = reginput;
5821 if (got_int)
5822 break;
5823 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005824 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 ++scan;
5826 else
5827 break;
5828 ++count;
5829 }
5830 break;
5831
5832 case KWORD:
5833 case KWORD + ADD_NL:
5834 testval = TRUE;
5835 /*FALLTHROUGH*/
5836 case SKWORD:
5837 case SKWORD + ADD_NL:
5838 while (count < maxcount)
5839 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005840 if (vim_iswordp_buf(scan, rex.reg_buf)
Bram Moolenaarf813a182013-01-30 13:59:37 +01005841 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005843 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 }
5845 else if (*scan == NUL)
5846 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005847 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5848 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849 break;
5850 reg_nextline();
5851 scan = reginput;
5852 if (got_int)
5853 break;
5854 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005855 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856 ++scan;
5857 else
5858 break;
5859 ++count;
5860 }
5861 break;
5862
5863 case FNAME:
5864 case FNAME + ADD_NL:
5865 testval = TRUE;
5866 /*FALLTHROUGH*/
5867 case SFNAME:
5868 case SFNAME + ADD_NL:
5869 while (count < maxcount)
5870 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005871 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005873 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874 }
5875 else if (*scan == NUL)
5876 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005877 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5878 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 break;
5880 reg_nextline();
5881 scan = reginput;
5882 if (got_int)
5883 break;
5884 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005885 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 ++scan;
5887 else
5888 break;
5889 ++count;
5890 }
5891 break;
5892
5893 case PRINT:
5894 case PRINT + ADD_NL:
5895 testval = TRUE;
5896 /*FALLTHROUGH*/
5897 case SPRINT:
5898 case SPRINT + ADD_NL:
5899 while (count < maxcount)
5900 {
5901 if (*scan == NUL)
5902 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005903 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5904 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005905 break;
5906 reg_nextline();
5907 scan = reginput;
5908 if (got_int)
5909 break;
5910 }
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005911 else if (vim_isprintc(PTR2CHAR(scan)) == 1
5912 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005914 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02005916 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 ++scan;
5918 else
5919 break;
5920 ++count;
5921 }
5922 break;
5923
5924 case WHITE:
5925 case WHITE + ADD_NL:
5926 testval = mask = RI_WHITE;
5927do_class:
5928 while (count < maxcount)
5929 {
5930#ifdef FEAT_MBYTE
5931 int l;
5932#endif
5933 if (*scan == NUL)
5934 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02005935 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
5936 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 break;
5938 reg_nextline();
5939 scan = reginput;
5940 if (got_int)
5941 break;
5942 }
5943#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005944 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 {
5946 if (testval != 0)
5947 break;
5948 scan += l;
5949 }
5950#endif
5951 else if ((class_tab[*scan] & mask) == testval)
5952 ++scan;
Bram Moolenaar6100d022016-10-02 16:51:57 +02005953 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005954 ++scan;
5955 else
5956 break;
5957 ++count;
5958 }
5959 break;
5960
5961 case NWHITE:
5962 case NWHITE + ADD_NL:
5963 mask = RI_WHITE;
5964 goto do_class;
5965 case DIGIT:
5966 case DIGIT + ADD_NL:
5967 testval = mask = RI_DIGIT;
5968 goto do_class;
5969 case NDIGIT:
5970 case NDIGIT + ADD_NL:
5971 mask = RI_DIGIT;
5972 goto do_class;
5973 case HEX:
5974 case HEX + ADD_NL:
5975 testval = mask = RI_HEX;
5976 goto do_class;
5977 case NHEX:
5978 case NHEX + ADD_NL:
5979 mask = RI_HEX;
5980 goto do_class;
5981 case OCTAL:
5982 case OCTAL + ADD_NL:
5983 testval = mask = RI_OCTAL;
5984 goto do_class;
5985 case NOCTAL:
5986 case NOCTAL + ADD_NL:
5987 mask = RI_OCTAL;
5988 goto do_class;
5989 case WORD:
5990 case WORD + ADD_NL:
5991 testval = mask = RI_WORD;
5992 goto do_class;
5993 case NWORD:
5994 case NWORD + ADD_NL:
5995 mask = RI_WORD;
5996 goto do_class;
5997 case HEAD:
5998 case HEAD + ADD_NL:
5999 testval = mask = RI_HEAD;
6000 goto do_class;
6001 case NHEAD:
6002 case NHEAD + ADD_NL:
6003 mask = RI_HEAD;
6004 goto do_class;
6005 case ALPHA:
6006 case ALPHA + ADD_NL:
6007 testval = mask = RI_ALPHA;
6008 goto do_class;
6009 case NALPHA:
6010 case NALPHA + ADD_NL:
6011 mask = RI_ALPHA;
6012 goto do_class;
6013 case LOWER:
6014 case LOWER + ADD_NL:
6015 testval = mask = RI_LOWER;
6016 goto do_class;
6017 case NLOWER:
6018 case NLOWER + ADD_NL:
6019 mask = RI_LOWER;
6020 goto do_class;
6021 case UPPER:
6022 case UPPER + ADD_NL:
6023 testval = mask = RI_UPPER;
6024 goto do_class;
6025 case NUPPER:
6026 case NUPPER + ADD_NL:
6027 mask = RI_UPPER;
6028 goto do_class;
6029
6030 case EXACTLY:
6031 {
6032 int cu, cl;
6033
6034 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006035 * would have been used for it. It does handle single-byte
6036 * characters, such as latin1. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006037 if (rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006038 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006039 cu = MB_TOUPPER(*opnd);
6040 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006041 while (count < maxcount && (*scan == cu || *scan == cl))
6042 {
6043 count++;
6044 scan++;
6045 }
6046 }
6047 else
6048 {
6049 cu = *opnd;
6050 while (count < maxcount && *scan == cu)
6051 {
6052 count++;
6053 scan++;
6054 }
6055 }
6056 break;
6057 }
6058
6059#ifdef FEAT_MBYTE
6060 case MULTIBYTECODE:
6061 {
6062 int i, len, cf = 0;
6063
6064 /* Safety check (just in case 'encoding' was changed since
6065 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006066 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006068 if (rex.reg_ic && enc_utf8)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006069 cf = utf_fold(utf_ptr2char(opnd));
Bram Moolenaar069dd082015-05-04 09:56:49 +02006070 while (count < maxcount && (*mb_ptr2len)(scan) >= len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 {
6072 for (i = 0; i < len; ++i)
6073 if (opnd[i] != scan[i])
6074 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006075 if (i < len && (!rex.reg_ic || !enc_utf8
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 || utf_fold(utf_ptr2char(scan)) != cf))
6077 break;
6078 scan += len;
6079 ++count;
6080 }
6081 }
6082 }
6083 break;
6084#endif
6085
6086 case ANYOF:
6087 case ANYOF + ADD_NL:
6088 testval = TRUE;
6089 /*FALLTHROUGH*/
6090
6091 case ANYBUT:
6092 case ANYBUT + ADD_NL:
6093 while (count < maxcount)
6094 {
6095#ifdef FEAT_MBYTE
6096 int len;
6097#endif
6098 if (*scan == NUL)
6099 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006100 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > rex.reg_maxline
6101 || rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006102 break;
6103 reg_nextline();
6104 scan = reginput;
6105 if (got_int)
6106 break;
6107 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02006108 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006109 ++scan;
6110#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006111 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006112 {
6113 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6114 break;
6115 scan += len;
6116 }
6117#endif
6118 else
6119 {
6120 if ((cstrchr(opnd, *scan) == NULL) == testval)
6121 break;
6122 ++scan;
6123 }
6124 ++count;
6125 }
6126 break;
6127
6128 case NEWL:
6129 while (count < maxcount
Bram Moolenaar6100d022016-10-02 16:51:57 +02006130 && ((*scan == NUL && reglnum <= rex.reg_maxline
6131 && !rex.reg_line_lbr && REG_MULTI)
6132 || (*scan == '\n' && rex.reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006133 {
6134 count++;
Bram Moolenaar6100d022016-10-02 16:51:57 +02006135 if (rex.reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 ADVANCE_REGINPUT();
6137 else
6138 reg_nextline();
6139 scan = reginput;
6140 if (got_int)
6141 break;
6142 }
6143 break;
6144
6145 default: /* Oh dear. Called inappropriately. */
6146 EMSG(_(e_re_corr));
6147#ifdef DEBUG
6148 printf("Called regrepeat with op code %d\n", OP(p));
6149#endif
6150 break;
6151 }
6152
6153 reginput = scan;
6154
6155 return (int)count;
6156}
6157
6158/*
6159 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006160 * Returns NULL when calculating size, when there is no next item and when
6161 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 */
6163 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006164regnext(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006165{
6166 int offset;
6167
Bram Moolenaard3005802009-11-25 17:21:32 +00006168 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006169 return NULL;
6170
6171 offset = NEXT(p);
6172 if (offset == 0)
6173 return NULL;
6174
Bram Moolenaar582fd852005-03-28 20:58:01 +00006175 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006176 return p - offset;
6177 else
6178 return p + offset;
6179}
6180
6181/*
6182 * Check the regexp program for its magic number.
6183 * Return TRUE if it's wrong.
6184 */
6185 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006186prog_magic_wrong(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006187{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006188 regprog_T *prog;
6189
Bram Moolenaar6100d022016-10-02 16:51:57 +02006190 prog = REG_MULTI ? rex.reg_mmatch->regprog : rex.reg_match->regprog;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006191 if (prog->engine == &nfa_regengine)
6192 /* For NFA matcher we don't check the magic */
6193 return FALSE;
6194
6195 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006196 {
6197 EMSG(_(e_re_corr));
6198 return TRUE;
6199 }
6200 return FALSE;
6201}
6202
6203/*
6204 * Cleanup the subexpressions, if this wasn't done yet.
6205 * This construction is used to clear the subexpressions only when they are
6206 * used (to increase speed).
6207 */
6208 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006209cleanup_subexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006210{
6211 if (need_clear_subexpr)
6212 {
6213 if (REG_MULTI)
6214 {
6215 /* Use 0xff to set lnum to -1 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006216 vim_memset(rex.reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6217 vim_memset(rex.reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006218 }
6219 else
6220 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006221 vim_memset(rex.reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6222 vim_memset(rex.reg_endp, 0, sizeof(char_u *) * NSUBEXP);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006223 }
6224 need_clear_subexpr = FALSE;
6225 }
6226}
6227
6228#ifdef FEAT_SYN_HL
6229 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006230cleanup_zsubexpr(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231{
6232 if (need_clear_zsubexpr)
6233 {
6234 if (REG_MULTI)
6235 {
6236 /* Use 0xff to set lnum to -1 */
6237 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6238 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6239 }
6240 else
6241 {
6242 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6243 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6244 }
6245 need_clear_zsubexpr = FALSE;
6246 }
6247}
6248#endif
6249
6250/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006251 * Save the current subexpr to "bp", so that they can be restored
6252 * later by restore_subexpr().
6253 */
6254 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006255save_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006256{
6257 int i;
6258
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006259 /* When "need_clear_subexpr" is set we don't need to save the values, only
6260 * remember that this flag needs to be set again when restoring. */
6261 bp->save_need_clear_subexpr = need_clear_subexpr;
6262 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006263 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006264 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006265 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006266 if (REG_MULTI)
6267 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006268 bp->save_start[i].se_u.pos = rex.reg_startpos[i];
6269 bp->save_end[i].se_u.pos = rex.reg_endpos[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006270 }
6271 else
6272 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006273 bp->save_start[i].se_u.ptr = rex.reg_startp[i];
6274 bp->save_end[i].se_u.ptr = rex.reg_endp[i];
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006275 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006276 }
6277 }
6278}
6279
6280/*
6281 * Restore the subexpr from "bp".
6282 */
6283 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006284restore_subexpr(regbehind_T *bp)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006285{
6286 int i;
6287
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006288 /* Only need to restore saved values when they are not to be cleared. */
6289 need_clear_subexpr = bp->save_need_clear_subexpr;
6290 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006291 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006292 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006293 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006294 if (REG_MULTI)
6295 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006296 rex.reg_startpos[i] = bp->save_start[i].se_u.pos;
6297 rex.reg_endpos[i] = bp->save_end[i].se_u.pos;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006298 }
6299 else
6300 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02006301 rex.reg_startp[i] = bp->save_start[i].se_u.ptr;
6302 rex.reg_endp[i] = bp->save_end[i].se_u.ptr;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006303 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006304 }
6305 }
6306}
6307
6308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 * Advance reglnum, regline and reginput to the next line.
6310 */
6311 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006312reg_nextline(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313{
6314 regline = reg_getline(++reglnum);
6315 reginput = regline;
6316 fast_breakcheck();
6317}
6318
6319/*
6320 * Save the input line and position in a regsave_T.
6321 */
6322 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006323reg_save(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324{
6325 if (REG_MULTI)
6326 {
6327 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6328 save->rs_u.pos.lnum = reglnum;
6329 }
6330 else
6331 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006332 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006333}
6334
6335/*
6336 * Restore the input line and position from a regsave_T.
6337 */
6338 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006339reg_restore(regsave_T *save, garray_T *gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006340{
6341 if (REG_MULTI)
6342 {
6343 if (reglnum != save->rs_u.pos.lnum)
6344 {
6345 /* only call reg_getline() when the line number changed to save
6346 * a bit of time */
6347 reglnum = save->rs_u.pos.lnum;
6348 regline = reg_getline(reglnum);
6349 }
6350 reginput = regline + save->rs_u.pos.col;
6351 }
6352 else
6353 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006354 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006355}
6356
6357/*
6358 * Return TRUE if current position is equal to saved position.
6359 */
6360 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006361reg_save_equal(regsave_T *save)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 if (REG_MULTI)
6364 return reglnum == save->rs_u.pos.lnum
6365 && reginput == regline + save->rs_u.pos.col;
6366 return reginput == save->rs_u.ptr;
6367}
6368
6369/*
6370 * Tentatively set the sub-expression start to the current position (after
6371 * calling regmatch() they will have changed). Need to save the existing
6372 * values for when there is no match.
6373 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6374 * depending on REG_MULTI.
6375 */
6376 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006377save_se_multi(save_se_T *savep, lpos_T *posp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006378{
6379 savep->se_u.pos = *posp;
6380 posp->lnum = reglnum;
6381 posp->col = (colnr_T)(reginput - regline);
6382}
6383
6384 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006385save_se_one(save_se_T *savep, char_u **pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006386{
6387 savep->se_u.ptr = *pp;
6388 *pp = reginput;
6389}
6390
6391/*
6392 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6393 */
6394 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006395re_num_cmp(long_u val, char_u *scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396{
6397 long_u n = OPERAND_MIN(scan);
6398
6399 if (OPERAND_CMP(scan) == '>')
6400 return val > n;
6401 if (OPERAND_CMP(scan) == '<')
6402 return val < n;
6403 return val == n;
6404}
6405
Bram Moolenaar580abea2013-06-14 20:31:28 +02006406/*
6407 * Check whether a backreference matches.
6408 * Returns RA_FAIL, RA_NOMATCH or RA_MATCH.
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006409 * If "bytelen" is not NULL, it is set to the byte length of the match in the
6410 * last line.
Bram Moolenaar580abea2013-06-14 20:31:28 +02006411 */
6412 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006413match_with_backref(
6414 linenr_T start_lnum,
6415 colnr_T start_col,
6416 linenr_T end_lnum,
6417 colnr_T end_col,
6418 int *bytelen)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006419{
6420 linenr_T clnum = start_lnum;
6421 colnr_T ccol = start_col;
6422 int len;
6423 char_u *p;
6424
6425 if (bytelen != NULL)
6426 *bytelen = 0;
6427 for (;;)
6428 {
6429 /* Since getting one line may invalidate the other, need to make copy.
6430 * Slow! */
6431 if (regline != reg_tofree)
6432 {
6433 len = (int)STRLEN(regline);
6434 if (reg_tofree == NULL || len >= (int)reg_tofreelen)
6435 {
6436 len += 50; /* get some extra */
6437 vim_free(reg_tofree);
6438 reg_tofree = alloc(len);
6439 if (reg_tofree == NULL)
6440 return RA_FAIL; /* out of memory!*/
6441 reg_tofreelen = len;
6442 }
6443 STRCPY(reg_tofree, regline);
6444 reginput = reg_tofree + (reginput - regline);
6445 regline = reg_tofree;
6446 }
6447
6448 /* Get the line to compare with. */
6449 p = reg_getline(clnum);
6450 if (clnum == end_lnum)
6451 len = end_col - ccol;
6452 else
6453 len = (int)STRLEN(p + ccol);
6454
6455 if (cstrncmp(p + ccol, reginput, &len) != 0)
6456 return RA_NOMATCH; /* doesn't match */
6457 if (bytelen != NULL)
6458 *bytelen += len;
6459 if (clnum == end_lnum)
6460 break; /* match and at end! */
Bram Moolenaar6100d022016-10-02 16:51:57 +02006461 if (reglnum >= rex.reg_maxline)
Bram Moolenaar580abea2013-06-14 20:31:28 +02006462 return RA_NOMATCH; /* text too short */
6463
6464 /* Advance to next line. */
6465 reg_nextline();
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006466 if (bytelen != NULL)
6467 *bytelen = 0;
Bram Moolenaar580abea2013-06-14 20:31:28 +02006468 ++clnum;
6469 ccol = 0;
6470 if (got_int)
6471 return RA_FAIL;
6472 }
6473
6474 /* found a match! Note that regline may now point to a copy of the line,
6475 * that should not matter. */
6476 return RA_MATCH;
6477}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006479#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
6481/*
6482 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6483 */
6484 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01006485regdump(char_u *pattern, bt_regprog_T *r)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486{
6487 char_u *s;
6488 int op = EXACTLY; /* Arbitrary non-END op. */
6489 char_u *next;
6490 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006491 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006492
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006493#ifdef BT_REGEXP_LOG
6494 f = fopen("bt_regexp_log.log", "a");
6495#else
6496 f = stdout;
6497#endif
6498 if (f == NULL)
6499 return;
6500 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
6502 s = r->program + 1;
6503 /*
6504 * Loop until we find the END that isn't before a referred next (an END
6505 * can also appear in a NOMATCH operand).
6506 */
6507 while (op != END || s <= end)
6508 {
6509 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006510 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006511 next = regnext(s);
6512 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006513 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006515 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (end < next)
6517 end = next;
6518 if (op == BRACE_LIMITS)
6519 {
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006520 /* Two ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006521 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 s += 8;
6523 }
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006524 else if (op == BEHIND || op == NOBEHIND)
6525 {
6526 /* one int */
6527 fprintf(f, " count %ld", OPERAND_MIN(s));
6528 s += 4;
6529 }
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02006530 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
6531 {
6532 /* one int plus comperator */
6533 fprintf(f, " count %ld", OPERAND_MIN(s));
6534 s += 5;
6535 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536 s += 3;
6537 if (op == ANYOF || op == ANYOF + ADD_NL
6538 || op == ANYBUT || op == ANYBUT + ADD_NL
6539 || op == EXACTLY)
6540 {
6541 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006542 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006544 fprintf(f, "%c", *s++);
6545 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546 s++;
6547 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006548 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 }
6550
6551 /* Header fields of interest. */
6552 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006553 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006554 ? (char *)transchar(r->regstart)
6555 : "multibyte", r->regstart);
6556 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006557 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006559 fprintf(f, "must have \"%s\"", r->regmust);
6560 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006562#ifdef BT_REGEXP_LOG
6563 fclose(f);
6564#endif
6565}
6566#endif /* BT_REGEXP_DUMP */
6567
6568#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006569/*
6570 * regprop - printable representation of opcode
6571 */
6572 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01006573regprop(char_u *op)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006574{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006575 char *p;
6576 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006578 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006579
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006580 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 {
6582 case BOL:
6583 p = "BOL";
6584 break;
6585 case EOL:
6586 p = "EOL";
6587 break;
6588 case RE_BOF:
6589 p = "BOF";
6590 break;
6591 case RE_EOF:
6592 p = "EOF";
6593 break;
6594 case CURSOR:
6595 p = "CURSOR";
6596 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006597 case RE_VISUAL:
6598 p = "RE_VISUAL";
6599 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 case RE_LNUM:
6601 p = "RE_LNUM";
6602 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006603 case RE_MARK:
6604 p = "RE_MARK";
6605 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 case RE_COL:
6607 p = "RE_COL";
6608 break;
6609 case RE_VCOL:
6610 p = "RE_VCOL";
6611 break;
6612 case BOW:
6613 p = "BOW";
6614 break;
6615 case EOW:
6616 p = "EOW";
6617 break;
6618 case ANY:
6619 p = "ANY";
6620 break;
6621 case ANY + ADD_NL:
6622 p = "ANY+NL";
6623 break;
6624 case ANYOF:
6625 p = "ANYOF";
6626 break;
6627 case ANYOF + ADD_NL:
6628 p = "ANYOF+NL";
6629 break;
6630 case ANYBUT:
6631 p = "ANYBUT";
6632 break;
6633 case ANYBUT + ADD_NL:
6634 p = "ANYBUT+NL";
6635 break;
6636 case IDENT:
6637 p = "IDENT";
6638 break;
6639 case IDENT + ADD_NL:
6640 p = "IDENT+NL";
6641 break;
6642 case SIDENT:
6643 p = "SIDENT";
6644 break;
6645 case SIDENT + ADD_NL:
6646 p = "SIDENT+NL";
6647 break;
6648 case KWORD:
6649 p = "KWORD";
6650 break;
6651 case KWORD + ADD_NL:
6652 p = "KWORD+NL";
6653 break;
6654 case SKWORD:
6655 p = "SKWORD";
6656 break;
6657 case SKWORD + ADD_NL:
6658 p = "SKWORD+NL";
6659 break;
6660 case FNAME:
6661 p = "FNAME";
6662 break;
6663 case FNAME + ADD_NL:
6664 p = "FNAME+NL";
6665 break;
6666 case SFNAME:
6667 p = "SFNAME";
6668 break;
6669 case SFNAME + ADD_NL:
6670 p = "SFNAME+NL";
6671 break;
6672 case PRINT:
6673 p = "PRINT";
6674 break;
6675 case PRINT + ADD_NL:
6676 p = "PRINT+NL";
6677 break;
6678 case SPRINT:
6679 p = "SPRINT";
6680 break;
6681 case SPRINT + ADD_NL:
6682 p = "SPRINT+NL";
6683 break;
6684 case WHITE:
6685 p = "WHITE";
6686 break;
6687 case WHITE + ADD_NL:
6688 p = "WHITE+NL";
6689 break;
6690 case NWHITE:
6691 p = "NWHITE";
6692 break;
6693 case NWHITE + ADD_NL:
6694 p = "NWHITE+NL";
6695 break;
6696 case DIGIT:
6697 p = "DIGIT";
6698 break;
6699 case DIGIT + ADD_NL:
6700 p = "DIGIT+NL";
6701 break;
6702 case NDIGIT:
6703 p = "NDIGIT";
6704 break;
6705 case NDIGIT + ADD_NL:
6706 p = "NDIGIT+NL";
6707 break;
6708 case HEX:
6709 p = "HEX";
6710 break;
6711 case HEX + ADD_NL:
6712 p = "HEX+NL";
6713 break;
6714 case NHEX:
6715 p = "NHEX";
6716 break;
6717 case NHEX + ADD_NL:
6718 p = "NHEX+NL";
6719 break;
6720 case OCTAL:
6721 p = "OCTAL";
6722 break;
6723 case OCTAL + ADD_NL:
6724 p = "OCTAL+NL";
6725 break;
6726 case NOCTAL:
6727 p = "NOCTAL";
6728 break;
6729 case NOCTAL + ADD_NL:
6730 p = "NOCTAL+NL";
6731 break;
6732 case WORD:
6733 p = "WORD";
6734 break;
6735 case WORD + ADD_NL:
6736 p = "WORD+NL";
6737 break;
6738 case NWORD:
6739 p = "NWORD";
6740 break;
6741 case NWORD + ADD_NL:
6742 p = "NWORD+NL";
6743 break;
6744 case HEAD:
6745 p = "HEAD";
6746 break;
6747 case HEAD + ADD_NL:
6748 p = "HEAD+NL";
6749 break;
6750 case NHEAD:
6751 p = "NHEAD";
6752 break;
6753 case NHEAD + ADD_NL:
6754 p = "NHEAD+NL";
6755 break;
6756 case ALPHA:
6757 p = "ALPHA";
6758 break;
6759 case ALPHA + ADD_NL:
6760 p = "ALPHA+NL";
6761 break;
6762 case NALPHA:
6763 p = "NALPHA";
6764 break;
6765 case NALPHA + ADD_NL:
6766 p = "NALPHA+NL";
6767 break;
6768 case LOWER:
6769 p = "LOWER";
6770 break;
6771 case LOWER + ADD_NL:
6772 p = "LOWER+NL";
6773 break;
6774 case NLOWER:
6775 p = "NLOWER";
6776 break;
6777 case NLOWER + ADD_NL:
6778 p = "NLOWER+NL";
6779 break;
6780 case UPPER:
6781 p = "UPPER";
6782 break;
6783 case UPPER + ADD_NL:
6784 p = "UPPER+NL";
6785 break;
6786 case NUPPER:
6787 p = "NUPPER";
6788 break;
6789 case NUPPER + ADD_NL:
6790 p = "NUPPER+NL";
6791 break;
6792 case BRANCH:
6793 p = "BRANCH";
6794 break;
6795 case EXACTLY:
6796 p = "EXACTLY";
6797 break;
6798 case NOTHING:
6799 p = "NOTHING";
6800 break;
6801 case BACK:
6802 p = "BACK";
6803 break;
6804 case END:
6805 p = "END";
6806 break;
6807 case MOPEN + 0:
6808 p = "MATCH START";
6809 break;
6810 case MOPEN + 1:
6811 case MOPEN + 2:
6812 case MOPEN + 3:
6813 case MOPEN + 4:
6814 case MOPEN + 5:
6815 case MOPEN + 6:
6816 case MOPEN + 7:
6817 case MOPEN + 8:
6818 case MOPEN + 9:
6819 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6820 p = NULL;
6821 break;
6822 case MCLOSE + 0:
6823 p = "MATCH END";
6824 break;
6825 case MCLOSE + 1:
6826 case MCLOSE + 2:
6827 case MCLOSE + 3:
6828 case MCLOSE + 4:
6829 case MCLOSE + 5:
6830 case MCLOSE + 6:
6831 case MCLOSE + 7:
6832 case MCLOSE + 8:
6833 case MCLOSE + 9:
6834 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6835 p = NULL;
6836 break;
6837 case BACKREF + 1:
6838 case BACKREF + 2:
6839 case BACKREF + 3:
6840 case BACKREF + 4:
6841 case BACKREF + 5:
6842 case BACKREF + 6:
6843 case BACKREF + 7:
6844 case BACKREF + 8:
6845 case BACKREF + 9:
6846 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6847 p = NULL;
6848 break;
6849 case NOPEN:
6850 p = "NOPEN";
6851 break;
6852 case NCLOSE:
6853 p = "NCLOSE";
6854 break;
6855#ifdef FEAT_SYN_HL
6856 case ZOPEN + 1:
6857 case ZOPEN + 2:
6858 case ZOPEN + 3:
6859 case ZOPEN + 4:
6860 case ZOPEN + 5:
6861 case ZOPEN + 6:
6862 case ZOPEN + 7:
6863 case ZOPEN + 8:
6864 case ZOPEN + 9:
6865 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6866 p = NULL;
6867 break;
6868 case ZCLOSE + 1:
6869 case ZCLOSE + 2:
6870 case ZCLOSE + 3:
6871 case ZCLOSE + 4:
6872 case ZCLOSE + 5:
6873 case ZCLOSE + 6:
6874 case ZCLOSE + 7:
6875 case ZCLOSE + 8:
6876 case ZCLOSE + 9:
6877 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6878 p = NULL;
6879 break;
6880 case ZREF + 1:
6881 case ZREF + 2:
6882 case ZREF + 3:
6883 case ZREF + 4:
6884 case ZREF + 5:
6885 case ZREF + 6:
6886 case ZREF + 7:
6887 case ZREF + 8:
6888 case ZREF + 9:
6889 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6890 p = NULL;
6891 break;
6892#endif
6893 case STAR:
6894 p = "STAR";
6895 break;
6896 case PLUS:
6897 p = "PLUS";
6898 break;
6899 case NOMATCH:
6900 p = "NOMATCH";
6901 break;
6902 case MATCH:
6903 p = "MATCH";
6904 break;
6905 case BEHIND:
6906 p = "BEHIND";
6907 break;
6908 case NOBEHIND:
6909 p = "NOBEHIND";
6910 break;
6911 case SUBPAT:
6912 p = "SUBPAT";
6913 break;
6914 case BRACE_LIMITS:
6915 p = "BRACE_LIMITS";
6916 break;
6917 case BRACE_SIMPLE:
6918 p = "BRACE_SIMPLE";
6919 break;
6920 case BRACE_COMPLEX + 0:
6921 case BRACE_COMPLEX + 1:
6922 case BRACE_COMPLEX + 2:
6923 case BRACE_COMPLEX + 3:
6924 case BRACE_COMPLEX + 4:
6925 case BRACE_COMPLEX + 5:
6926 case BRACE_COMPLEX + 6:
6927 case BRACE_COMPLEX + 7:
6928 case BRACE_COMPLEX + 8:
6929 case BRACE_COMPLEX + 9:
6930 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6931 p = NULL;
6932 break;
6933#ifdef FEAT_MBYTE
6934 case MULTIBYTECODE:
6935 p = "MULTIBYTECODE";
6936 break;
6937#endif
6938 case NEWL:
6939 p = "NEWL";
6940 break;
6941 default:
6942 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6943 p = NULL;
6944 break;
6945 }
6946 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006947 STRCAT(buf, p);
6948 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006950#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaarfb031402014-09-09 17:18:49 +02006952/*
6953 * Used in a place where no * or \+ can follow.
6954 */
6955 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01006956re_mult_next(char *what)
Bram Moolenaarfb031402014-09-09 17:18:49 +02006957{
6958 if (re_multi_type(peekchr()) == MULTI_MULT)
6959 EMSG2_RET_FAIL(_("E888: (NFA regexp) cannot repeat %s"), what);
6960 return OK;
6961}
6962
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963#ifdef FEAT_MBYTE
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01006964static void mb_decompose(int c, int *c1, int *c2, int *c3);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965
6966typedef struct
6967{
6968 int a, b, c;
6969} decomp_T;
6970
6971
6972/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006973static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006974{
6975 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6976 {0x5d0,0,0}, /* 0xfb21 alt alef */
6977 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6978 {0x5d4,0,0}, /* 0xfb23 alt he */
6979 {0x5db,0,0}, /* 0xfb24 alt kaf */
6980 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6981 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6982 {0x5e8,0,0}, /* 0xfb27 alt resh */
6983 {0x5ea,0,0}, /* 0xfb28 alt tav */
6984 {'+', 0, 0}, /* 0xfb29 alt plus */
6985 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6986 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6987 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6988 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6989 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6990 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6991 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6992 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6993 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6994 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6995 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6996 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6997 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6998 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6999 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
7000 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
7001 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
7002 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
7003 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
7004 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
7005 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
7006 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
7007 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
7008 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
7009 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
7010 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
7011 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
7012 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
7013 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7014 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7015 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7016 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7017 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7018 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7019 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7020 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7021 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7022 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7023};
7024
7025 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01007026mb_decompose(int c, int *c1, int *c2, int *c3)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027{
7028 decomp_T d;
7029
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007030 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
7032 d = decomp_table[c - 0xfb20];
7033 *c1 = d.a;
7034 *c2 = d.b;
7035 *c3 = d.c;
7036 }
7037 else
7038 {
7039 *c1 = c;
7040 *c2 = *c3 = 0;
7041 }
7042}
7043#endif
7044
7045/*
Bram Moolenaar6100d022016-10-02 16:51:57 +02007046 * Compare two strings, ignore case if rex.reg_ic set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 * Return 0 if strings match, non-zero otherwise.
7048 * Correct the length "*n" when composing characters are ignored.
7049 */
7050 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007051cstrncmp(char_u *s1, char_u *s2, int *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007052{
7053 int result;
7054
Bram Moolenaar6100d022016-10-02 16:51:57 +02007055 if (!rex.reg_ic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 result = STRNCMP(s1, s2, *n);
7057 else
7058 result = MB_STRNICMP(s1, s2, *n);
7059
7060#ifdef FEAT_MBYTE
7061 /* if it failed and it's utf8 and we want to combineignore: */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007062 if (result != 0 && enc_utf8 && rex.reg_icombine)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 {
7064 char_u *str1, *str2;
7065 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 int junk;
7067
7068 /* we have to handle the strcmp ourselves, since it is necessary to
7069 * deal with the composing characters by ignoring them: */
7070 str1 = s1;
7071 str2 = s2;
7072 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007073 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 {
7075 c1 = mb_ptr2char_adv(&str1);
7076 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077
7078 /* decompose the character if necessary, into 'base' characters
7079 * because I don't care about Arabic, I will hard-code the Hebrew
7080 * which I *do* care about! So sue me... */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007081 if (c1 != c2 && (!rex.reg_ic || utf_fold(c1) != utf_fold(c2)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082 {
7083 /* decomposition necessary? */
7084 mb_decompose(c1, &c11, &junk, &junk);
7085 mb_decompose(c2, &c12, &junk, &junk);
7086 c1 = c11;
7087 c2 = c12;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007088 if (c11 != c12
7089 && (!rex.reg_ic || utf_fold(c11) != utf_fold(c12)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090 break;
7091 }
7092 }
7093 result = c2 - c1;
7094 if (result == 0)
7095 *n = (int)(str2 - s2);
7096 }
7097#endif
7098
7099 return result;
7100}
7101
7102/*
7103 * cstrchr: This function is used a lot for simple searches, keep it fast!
7104 */
7105 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007106cstrchr(char_u *s, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107{
7108 char_u *p;
7109 int cc;
7110
Bram Moolenaar6100d022016-10-02 16:51:57 +02007111 if (!rex.reg_ic
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112#ifdef FEAT_MBYTE
7113 || (!enc_utf8 && mb_char2len(c) > 1)
7114#endif
7115 )
7116 return vim_strchr(s, c);
7117
7118 /* tolower() and toupper() can be slow, comparing twice should be a lot
7119 * faster (esp. when using MS Visual C++!).
7120 * For UTF-8 need to use folded case. */
7121#ifdef FEAT_MBYTE
7122 if (enc_utf8 && c > 0x80)
7123 cc = utf_fold(c);
7124 else
7125#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007126 if (MB_ISUPPER(c))
7127 cc = MB_TOLOWER(c);
7128 else if (MB_ISLOWER(c))
7129 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007130 else
7131 return vim_strchr(s, c);
7132
7133#ifdef FEAT_MBYTE
7134 if (has_mbyte)
7135 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007136 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137 {
7138 if (enc_utf8 && c > 0x80)
7139 {
7140 if (utf_fold(utf_ptr2char(p)) == cc)
7141 return p;
7142 }
7143 else if (*p == c || *p == cc)
7144 return p;
7145 }
7146 }
7147 else
7148#endif
7149 /* Faster version for when there are no multi-byte characters. */
7150 for (p = s; *p != NUL; ++p)
7151 if (*p == c || *p == cc)
7152 return p;
7153
7154 return NULL;
7155}
7156
7157/***************************************************************
7158 * regsub stuff *
7159 ***************************************************************/
7160
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161/*
7162 * We should define ftpr as a pointer to a function returning a pointer to
7163 * a function returning a pointer to a function ...
7164 * This is impossible, so we declare a pointer to a function returning a
7165 * pointer to a function returning void. This should work for all compilers.
7166 */
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007167typedef void (*(*fptr_T)(int *, int))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007169static fptr_T do_upper(int *, int);
7170static fptr_T do_Upper(int *, int);
7171static fptr_T do_lower(int *, int);
7172static fptr_T do_Lower(int *, int);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007174static 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 +00007175
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007176 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007177do_upper(int *d, int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007179 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007181 return (fptr_T)NULL;
7182}
7183
7184 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007185do_Upper(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007186{
7187 *d = MB_TOUPPER(c);
7188
7189 return (fptr_T)do_Upper;
7190}
7191
7192 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007193do_lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007194{
7195 *d = MB_TOLOWER(c);
7196
7197 return (fptr_T)NULL;
7198}
7199
7200 static fptr_T
Bram Moolenaar05540972016-01-30 20:31:25 +01007201do_Lower(int *d, int c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007202{
7203 *d = MB_TOLOWER(c);
7204
7205 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206}
7207
7208/*
7209 * regtilde(): Replace tildes in the pattern by the old pattern.
7210 *
7211 * Short explanation of the tilde: It stands for the previous replacement
7212 * pattern. If that previous pattern also contains a ~ we should go back a
7213 * step further... But we insert the previous pattern into the current one
7214 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007215 * This still does not handle the case where "magic" changes. So require the
7216 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 *
7218 * The tildes are parsed once before the first call to vim_regsub().
7219 */
7220 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007221regtilde(char_u *source, int magic)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222{
7223 char_u *newsub = source;
7224 char_u *tmpsub;
7225 char_u *p;
7226 int len;
7227 int prevlen;
7228
7229 for (p = newsub; *p; ++p)
7230 {
7231 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7232 {
7233 if (reg_prev_sub != NULL)
7234 {
7235 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7236 prevlen = (int)STRLEN(reg_prev_sub);
7237 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7238 if (tmpsub != NULL)
7239 {
7240 /* copy prefix */
7241 len = (int)(p - newsub); /* not including ~ */
7242 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007243 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007244 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7245 /* copy postfix */
7246 if (!magic)
7247 ++p; /* back off \ */
7248 STRCPY(tmpsub + len + prevlen, p + 1);
7249
7250 if (newsub != source) /* already allocated newsub */
7251 vim_free(newsub);
7252 newsub = tmpsub;
7253 p = newsub + len + prevlen;
7254 }
7255 }
7256 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007257 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007259 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007260 --p;
7261 }
7262 else
7263 {
7264 if (*p == '\\' && p[1]) /* skip escaped characters */
7265 ++p;
7266#ifdef FEAT_MBYTE
7267 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007268 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007269#endif
7270 }
7271 }
7272
7273 vim_free(reg_prev_sub);
7274 if (newsub != source) /* newsub was allocated, just keep it */
7275 reg_prev_sub = newsub;
7276 else /* no ~ found, need to save newsub */
7277 reg_prev_sub = vim_strsave(newsub);
7278 return newsub;
7279}
7280
7281#ifdef FEAT_EVAL
7282static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7283
Bram Moolenaar6100d022016-10-02 16:51:57 +02007284/* These pointers are used for reg_submatch(). Needed for when the
7285 * substitution string is an expression that contains a call to substitute()
7286 * and submatch(). */
7287typedef struct {
7288 regmatch_T *sm_match;
7289 regmmatch_T *sm_mmatch;
7290 linenr_T sm_firstlnum;
7291 linenr_T sm_maxline;
7292 int sm_line_lbr;
7293} regsubmatch_T;
7294
7295static regsubmatch_T rsm; /* can only be used when can_f_submatch is TRUE */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296#endif
7297
7298#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007299
7300/*
7301 * Put the submatches in "argv[0]" which is a list passed into call_func() by
7302 * vim_regsub_both().
7303 */
7304 static int
7305fill_submatch_list(int argc UNUSED, typval_T *argv, int argcount)
7306{
7307 listitem_T *li;
7308 int i;
7309 char_u *s;
7310
7311 if (argcount == 0)
7312 /* called function doesn't take an argument */
7313 return 0;
7314
7315 /* Relies on sl_list to be the first item in staticList10_T. */
7316 init_static_list((staticList10_T *)(argv->vval.v_list));
7317
7318 /* There are always 10 list items in staticList10_T. */
7319 li = argv->vval.v_list->lv_first;
7320 for (i = 0; i < 10; ++i)
7321 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007322 s = rsm.sm_match->startp[i];
7323 if (s == NULL || rsm.sm_match->endp[i] == NULL)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007324 s = NULL;
7325 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007326 s = vim_strnsave(s, (int)(rsm.sm_match->endp[i] - s));
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007327 li->li_tv.v_type = VAR_STRING;
7328 li->li_tv.vval.v_string = s;
7329 li = li->li_next;
7330 }
7331 return 1;
7332}
7333
7334 static void
7335clear_submatch_list(staticList10_T *sl)
7336{
7337 int i;
7338
7339 for (i = 0; i < 10; ++i)
7340 vim_free(sl->sl_items[i].li_tv.vval.v_string);
7341}
7342
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343/*
7344 * vim_regsub() - perform substitutions after a vim_regexec() or
7345 * vim_regexec_multi() match.
7346 *
7347 * If "copy" is TRUE really copy into "dest".
7348 * If "copy" is FALSE nothing is copied, this is just to find out the length
7349 * of the result.
7350 *
7351 * If "backslash" is TRUE, a backslash will be removed later, need to double
7352 * them to keep them, and insert a backslash before a CR to avoid it being
7353 * replaced with a line break later.
7354 *
7355 * Note: The matched text must not change between the call of
7356 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7357 * references invalid!
7358 *
7359 * Returns the size of the replacement, including terminating NUL.
7360 */
7361 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007362vim_regsub(
7363 regmatch_T *rmp,
7364 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007365 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007366 char_u *dest,
7367 int copy,
7368 int magic,
7369 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007370{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007371 int result;
7372 regexec_T rex_save;
7373 int rex_in_use_save = rex_in_use;
7374
7375 if (rex_in_use)
7376 /* Being called recursively, save the state. */
7377 rex_save = rex;
7378 rex_in_use = TRUE;
7379
7380 rex.reg_match = rmp;
7381 rex.reg_mmatch = NULL;
7382 rex.reg_maxline = 0;
7383 rex.reg_buf = curbuf;
7384 rex.reg_line_lbr = TRUE;
7385 result = vim_regsub_both(source, expr, dest, copy, magic, backslash);
7386
7387 rex_in_use = rex_in_use_save;
7388 if (rex_in_use)
7389 rex = rex_save;
7390
7391 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007392}
7393#endif
7394
7395 int
Bram Moolenaar05540972016-01-30 20:31:25 +01007396vim_regsub_multi(
7397 regmmatch_T *rmp,
7398 linenr_T lnum,
7399 char_u *source,
7400 char_u *dest,
7401 int copy,
7402 int magic,
7403 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007404{
Bram Moolenaar6100d022016-10-02 16:51:57 +02007405 int result;
7406 regexec_T rex_save;
7407 int rex_in_use_save = rex_in_use;
7408
7409 if (rex_in_use)
7410 /* Being called recursively, save the state. */
7411 rex_save = rex;
7412 rex_in_use = TRUE;
7413
7414 rex.reg_match = NULL;
7415 rex.reg_mmatch = rmp;
7416 rex.reg_buf = curbuf; /* always works on the current buffer! */
7417 rex.reg_firstlnum = lnum;
7418 rex.reg_maxline = curbuf->b_ml.ml_line_count - lnum;
7419 rex.reg_line_lbr = FALSE;
7420 result = vim_regsub_both(source, NULL, dest, copy, magic, backslash);
7421
7422 rex_in_use = rex_in_use_save;
7423 if (rex_in_use)
7424 rex = rex_save;
7425
7426 return result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007427}
7428
7429 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01007430vim_regsub_both(
7431 char_u *source,
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007432 typval_T *expr,
Bram Moolenaar05540972016-01-30 20:31:25 +01007433 char_u *dest,
7434 int copy,
7435 int magic,
7436 int backslash)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007437{
7438 char_u *src;
7439 char_u *dst;
7440 char_u *s;
7441 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007442 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007444 fptr_T func_all = (fptr_T)NULL;
7445 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007446 linenr_T clnum = 0; /* init for GCC */
7447 int len = 0; /* init for GCC */
7448#ifdef FEAT_EVAL
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007449 static char_u *eval_result = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007450#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451
7452 /* Be paranoid... */
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007453 if ((source == NULL && expr == NULL) || dest == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454 {
7455 EMSG(_(e_null));
7456 return 0;
7457 }
7458 if (prog_magic_wrong())
7459 return 0;
7460 src = source;
7461 dst = dest;
7462
7463 /*
7464 * When the substitute part starts with "\=" evaluate it as an expression.
7465 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007466 if (expr != NULL || (source[0] == '\\' && source[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007467 {
7468#ifdef FEAT_EVAL
7469 /* To make sure that the length doesn't change between checking the
7470 * length and copying the string, and to speed up things, the
7471 * resulting string is saved from the call with "copy" == FALSE to the
7472 * call with "copy" == TRUE. */
7473 if (copy)
7474 {
7475 if (eval_result != NULL)
7476 {
7477 STRCPY(dest, eval_result);
7478 dst += STRLEN(eval_result);
7479 vim_free(eval_result);
7480 eval_result = NULL;
7481 }
7482 }
7483 else
7484 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007485 int prev_can_f_submatch = can_f_submatch;
7486 regsubmatch_T rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007487
7488 vim_free(eval_result);
7489
7490 /* The expression may contain substitute(), which calls us
7491 * recursively. Make sure submatch() gets the text from the first
Bram Moolenaar6100d022016-10-02 16:51:57 +02007492 * level. */
7493 if (can_f_submatch)
7494 rsm_save = rsm;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007495 can_f_submatch = TRUE;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007496 rsm.sm_match = rex.reg_match;
7497 rsm.sm_mmatch = rex.reg_mmatch;
7498 rsm.sm_firstlnum = rex.reg_firstlnum;
7499 rsm.sm_maxline = rex.reg_maxline;
7500 rsm.sm_line_lbr = rex.reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007501
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007502 if (expr != NULL)
7503 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007504 typval_T argv[2];
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007505 int dummy;
7506 char_u buf[NUMBUFLEN];
7507 typval_T rettv;
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007508 staticList10_T matchList;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007509
7510 rettv.v_type = VAR_STRING;
7511 rettv.vval.v_string = NULL;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007512 argv[0].v_type = VAR_LIST;
7513 argv[0].vval.v_list = &matchList.sl_list;
7514 matchList.sl_list.lv_len = 0;
7515 if (expr->v_type == VAR_FUNC)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007516 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007517 s = expr->vval.v_string;
7518 call_func(s, (int)STRLEN(s), &rettv,
7519 1, argv, fill_submatch_list,
7520 0L, 0L, &dummy, TRUE, NULL, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007521 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007522 else if (expr->v_type == VAR_PARTIAL)
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007523 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007524 partial_T *partial = expr->vval.v_partial;
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007525
Bram Moolenaar6100d022016-10-02 16:51:57 +02007526 s = partial_name(partial);
7527 call_func(s, (int)STRLEN(s), &rettv,
7528 1, argv, fill_submatch_list,
7529 0L, 0L, &dummy, TRUE, partial, NULL);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007530 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02007531 if (matchList.sl_list.lv_len > 0)
7532 /* fill_submatch_list() was called */
7533 clear_submatch_list(&matchList);
7534
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007535 eval_result = get_tv_string_buf_chk(&rettv, buf);
7536 if (eval_result != NULL)
7537 eval_result = vim_strsave(eval_result);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02007538 clear_tv(&rettv);
Bram Moolenaar72ab7292016-07-19 19:10:51 +02007539 }
7540 else
7541 eval_result = eval_to_string(source + 2, NULL, TRUE);
7542
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543 if (eval_result != NULL)
7544 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007545 int had_backslash = FALSE;
7546
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007547 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007549 /* Change NL to CR, so that it becomes a line break,
7550 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007551 * Skip over a backslashed character. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007552 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553 *s = CAR;
7554 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007555 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007557 /* Change NL to CR here too, so that this works:
7558 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7559 * abc\
7560 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007561 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007562 */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007563 if (*s == NL && !rsm.sm_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007564 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007565 had_backslash = TRUE;
7566 }
7567 }
7568 if (had_backslash && backslash)
7569 {
7570 /* Backslashes will be consumed, need to double them. */
7571 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7572 if (s != NULL)
7573 {
7574 vim_free(eval_result);
7575 eval_result = s;
7576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 }
7578
7579 dst += STRLEN(eval_result);
7580 }
7581
Bram Moolenaar6100d022016-10-02 16:51:57 +02007582 can_f_submatch = prev_can_f_submatch;
7583 if (can_f_submatch)
7584 rsm = rsm_save;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007585 }
7586#endif
7587 }
7588 else
7589 while ((c = *src++) != NUL)
7590 {
7591 if (c == '&' && magic)
7592 no = 0;
7593 else if (c == '\\' && *src != NUL)
7594 {
7595 if (*src == '&' && !magic)
7596 {
7597 ++src;
7598 no = 0;
7599 }
7600 else if ('0' <= *src && *src <= '9')
7601 {
7602 no = *src++ - '0';
7603 }
7604 else if (vim_strchr((char_u *)"uUlLeE", *src))
7605 {
7606 switch (*src++)
7607 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007608 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007610 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007612 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007613 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007614 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 continue;
7616 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007617 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007618 continue;
7619 }
7620 }
7621 }
7622 if (no < 0) /* Ordinary character. */
7623 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007624 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7625 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007626 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007627 if (copy)
7628 {
7629 *dst++ = c;
7630 *dst++ = *src++;
7631 *dst++ = *src++;
7632 }
7633 else
7634 {
7635 dst += 3;
7636 src += 2;
7637 }
7638 continue;
7639 }
7640
Bram Moolenaar071d4272004-06-13 20:20:40 +00007641 if (c == '\\' && *src != NUL)
7642 {
7643 /* Check for abbreviations -- webb */
7644 switch (*src)
7645 {
7646 case 'r': c = CAR; ++src; break;
7647 case 'n': c = NL; ++src; break;
7648 case 't': c = TAB; ++src; break;
7649 /* Oh no! \e already has meaning in subst pat :-( */
7650 /* case 'e': c = ESC; ++src; break; */
7651 case 'b': c = Ctrl_H; ++src; break;
7652
7653 /* If "backslash" is TRUE the backslash will be removed
7654 * later. Used to insert a literal CR. */
7655 default: if (backslash)
7656 {
7657 if (copy)
7658 *dst = '\\';
7659 ++dst;
7660 }
7661 c = *src++;
7662 }
7663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007664#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007665 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007666 c = mb_ptr2char(src - 1);
7667#endif
7668
Bram Moolenaardb552d602006-03-23 22:59:57 +00007669 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007670 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007671 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007672 func_one = (fptr_T)(func_one(&cc, c));
7673 else if (func_all != (fptr_T)NULL)
7674 /* Turbo C complains without the typecast */
7675 func_all = (fptr_T)(func_all(&cc, c));
7676 else /* just copy */
7677 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007678
7679#ifdef FEAT_MBYTE
7680 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007682 int totlen = mb_ptr2len(src - 1);
7683
Bram Moolenaar071d4272004-06-13 20:20:40 +00007684 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007685 mb_char2bytes(cc, dst);
7686 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007687 if (enc_utf8)
7688 {
7689 int clen = utf_ptr2len(src - 1);
7690
7691 /* If the character length is shorter than "totlen", there
7692 * are composing characters; copy them as-is. */
7693 if (clen < totlen)
7694 {
7695 if (copy)
7696 mch_memmove(dst + 1, src - 1 + clen,
7697 (size_t)(totlen - clen));
7698 dst += totlen - clen;
7699 }
7700 }
7701 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702 }
7703 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007704#endif
7705 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007706 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 dst++;
7708 }
7709 else
7710 {
7711 if (REG_MULTI)
7712 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007713 clnum = rex.reg_mmatch->startpos[no].lnum;
7714 if (clnum < 0 || rex.reg_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 s = NULL;
7716 else
7717 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007718 s = reg_getline(clnum) + rex.reg_mmatch->startpos[no].col;
7719 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7720 len = rex.reg_mmatch->endpos[no].col
7721 - rex.reg_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007722 else
7723 len = (int)STRLEN(s);
7724 }
7725 }
7726 else
7727 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007728 s = rex.reg_match->startp[no];
7729 if (rex.reg_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 s = NULL;
7731 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007732 len = (int)(rex.reg_match->endp[no] - s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 }
7734 if (s != NULL)
7735 {
7736 for (;;)
7737 {
7738 if (len == 0)
7739 {
7740 if (REG_MULTI)
7741 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007742 if (rex.reg_mmatch->endpos[no].lnum == clnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 break;
7744 if (copy)
7745 *dst = CAR;
7746 ++dst;
7747 s = reg_getline(++clnum);
Bram Moolenaar6100d022016-10-02 16:51:57 +02007748 if (rex.reg_mmatch->endpos[no].lnum == clnum)
7749 len = rex.reg_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750 else
7751 len = (int)STRLEN(s);
7752 }
7753 else
7754 break;
7755 }
7756 else if (*s == NUL) /* we hit NUL. */
7757 {
7758 if (copy)
7759 EMSG(_(e_re_damg));
7760 goto exit;
7761 }
7762 else
7763 {
7764 if (backslash && (*s == CAR || *s == '\\'))
7765 {
7766 /*
7767 * Insert a backslash in front of a CR, otherwise
7768 * it will be replaced by a line break.
7769 * Number of backslashes will be halved later,
7770 * double them here.
7771 */
7772 if (copy)
7773 {
7774 dst[0] = '\\';
7775 dst[1] = *s;
7776 }
7777 dst += 2;
7778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 else
7780 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007781#ifdef FEAT_MBYTE
7782 if (has_mbyte)
7783 c = mb_ptr2char(s);
7784 else
7785#endif
7786 c = *s;
7787
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007788 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007790 func_one = (fptr_T)(func_one(&cc, c));
7791 else if (func_all != (fptr_T)NULL)
7792 /* Turbo C complains without the typecast */
7793 func_all = (fptr_T)(func_all(&cc, c));
7794 else /* just copy */
7795 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007796
7797#ifdef FEAT_MBYTE
7798 if (has_mbyte)
7799 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007800 int l;
7801
7802 /* Copy composing characters separately, one
7803 * at a time. */
7804 if (enc_utf8)
7805 l = utf_ptr2len(s) - 1;
7806 else
7807 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007808
7809 s += l;
7810 len -= l;
7811 if (copy)
7812 mb_char2bytes(cc, dst);
7813 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007815 else
7816#endif
7817 if (copy)
7818 *dst = cc;
7819 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007821
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 ++s;
7823 --len;
7824 }
7825 }
7826 }
7827 no = -1;
7828 }
7829 }
7830 if (copy)
7831 *dst = NUL;
7832
7833exit:
7834 return (int)((dst - dest) + 1);
7835}
7836
7837#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01007838static char_u *reg_getline_submatch(linenr_T lnum);
Bram Moolenaard32a3192009-11-26 19:40:49 +00007839
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007841 * Call reg_getline() with the line numbers from the submatch. If a
7842 * substitute() was used the reg_maxline and other values have been
7843 * overwritten.
7844 */
7845 static char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007846reg_getline_submatch(linenr_T lnum)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007847{
7848 char_u *s;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007849 linenr_T save_first = rex.reg_firstlnum;
7850 linenr_T save_max = rex.reg_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007851
Bram Moolenaar6100d022016-10-02 16:51:57 +02007852 rex.reg_firstlnum = rsm.sm_firstlnum;
7853 rex.reg_maxline = rsm.sm_maxline;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007854
7855 s = reg_getline(lnum);
7856
Bram Moolenaar6100d022016-10-02 16:51:57 +02007857 rex.reg_firstlnum = save_first;
7858 rex.reg_maxline = save_max;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007859 return s;
7860}
7861
7862/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007863 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 * allocated memory.
7865 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7866 */
7867 char_u *
Bram Moolenaar05540972016-01-30 20:31:25 +01007868reg_submatch(int no)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007869{
7870 char_u *retval = NULL;
7871 char_u *s;
7872 int len;
7873 int round;
7874 linenr_T lnum;
7875
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007876 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 return NULL;
7878
Bram Moolenaar6100d022016-10-02 16:51:57 +02007879 if (rsm.sm_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 {
7881 /*
7882 * First round: compute the length and allocate memory.
7883 * Second round: copy the text.
7884 */
7885 for (round = 1; round <= 2; ++round)
7886 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007887 lnum = rsm.sm_mmatch->startpos[no].lnum;
7888 if (lnum < 0 || rsm.sm_mmatch->endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889 return NULL;
7890
Bram Moolenaar6100d022016-10-02 16:51:57 +02007891 s = reg_getline_submatch(lnum) + rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892 if (s == NULL) /* anti-crash check, cannot happen? */
7893 break;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007894 if (rsm.sm_mmatch->endpos[no].lnum == lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 {
7896 /* Within one line: take form start to end col. */
Bram Moolenaar6100d022016-10-02 16:51:57 +02007897 len = rsm.sm_mmatch->endpos[no].col
7898 - rsm.sm_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007900 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007901 ++len;
7902 }
7903 else
7904 {
7905 /* Multiple lines: take start line from start col, middle
7906 * lines completely and end line up to end col. */
7907 len = (int)STRLEN(s);
7908 if (round == 2)
7909 {
7910 STRCPY(retval, s);
7911 retval[len] = '\n';
7912 }
7913 ++len;
7914 ++lnum;
Bram Moolenaar6100d022016-10-02 16:51:57 +02007915 while (lnum < rsm.sm_mmatch->endpos[no].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007917 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007918 if (round == 2)
7919 STRCPY(retval + len, s);
7920 len += (int)STRLEN(s);
7921 if (round == 2)
7922 retval[len] = '\n';
7923 ++len;
7924 }
7925 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007926 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar6100d022016-10-02 16:51:57 +02007927 rsm.sm_mmatch->endpos[no].col);
7928 len += rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 if (round == 2)
7930 retval[len] = NUL;
7931 ++len;
7932 }
7933
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007934 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 {
7936 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007937 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 return NULL;
7939 }
7940 }
7941 }
7942 else
7943 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007944 s = rsm.sm_match->startp[no];
7945 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007946 retval = NULL;
7947 else
Bram Moolenaar6100d022016-10-02 16:51:57 +02007948 retval = vim_strnsave(s, (int)(rsm.sm_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007949 }
7950
7951 return retval;
7952}
Bram Moolenaar41571762014-04-02 19:00:58 +02007953
7954/*
7955 * Used for the submatch() function with the optional non-zero argument: get
7956 * the list of strings from the n'th submatch in allocated memory with NULs
7957 * represented in NLs.
7958 * Returns a list of allocated strings. Returns NULL when not in a ":s"
7959 * command, for a non-existing submatch and for any error.
7960 */
7961 list_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01007962reg_submatch_list(int no)
Bram Moolenaar41571762014-04-02 19:00:58 +02007963{
7964 char_u *s;
7965 linenr_T slnum;
7966 linenr_T elnum;
7967 colnr_T scol;
7968 colnr_T ecol;
7969 int i;
7970 list_T *list;
7971 int error = FALSE;
7972
7973 if (!can_f_submatch || no < 0)
7974 return NULL;
7975
Bram Moolenaar6100d022016-10-02 16:51:57 +02007976 if (rsm.sm_match == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02007977 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02007978 slnum = rsm.sm_mmatch->startpos[no].lnum;
7979 elnum = rsm.sm_mmatch->endpos[no].lnum;
Bram Moolenaar41571762014-04-02 19:00:58 +02007980 if (slnum < 0 || elnum < 0)
7981 return NULL;
7982
Bram Moolenaar6100d022016-10-02 16:51:57 +02007983 scol = rsm.sm_mmatch->startpos[no].col;
7984 ecol = rsm.sm_mmatch->endpos[no].col;
Bram Moolenaar41571762014-04-02 19:00:58 +02007985
7986 list = list_alloc();
7987 if (list == NULL)
7988 return NULL;
7989
7990 s = reg_getline_submatch(slnum) + scol;
7991 if (slnum == elnum)
7992 {
7993 if (list_append_string(list, s, ecol - scol) == FAIL)
7994 error = TRUE;
7995 }
7996 else
7997 {
7998 if (list_append_string(list, s, -1) == FAIL)
7999 error = TRUE;
8000 for (i = 1; i < elnum - slnum; i++)
8001 {
8002 s = reg_getline_submatch(slnum + i);
8003 if (list_append_string(list, s, -1) == FAIL)
8004 error = TRUE;
8005 }
8006 s = reg_getline_submatch(elnum);
8007 if (list_append_string(list, s, ecol) == FAIL)
8008 error = TRUE;
8009 }
8010 }
8011 else
8012 {
Bram Moolenaar6100d022016-10-02 16:51:57 +02008013 s = rsm.sm_match->startp[no];
8014 if (s == NULL || rsm.sm_match->endp[no] == NULL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008015 return NULL;
8016 list = list_alloc();
8017 if (list == NULL)
8018 return NULL;
8019 if (list_append_string(list, s,
Bram Moolenaar6100d022016-10-02 16:51:57 +02008020 (int)(rsm.sm_match->endp[no] - s)) == FAIL)
Bram Moolenaar41571762014-04-02 19:00:58 +02008021 error = TRUE;
8022 }
8023
8024 if (error)
8025 {
Bram Moolenaar107e1ee2016-04-08 17:07:19 +02008026 list_free(list);
Bram Moolenaar41571762014-04-02 19:00:58 +02008027 return NULL;
8028 }
8029 return list;
8030}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008031#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008032
8033static regengine_T bt_regengine =
8034{
8035 bt_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008036 bt_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008037 bt_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008038 bt_regexec_multi,
8039 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008040};
8041
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008042#include "regexp_nfa.c"
8043
8044static regengine_T nfa_regengine =
8045{
8046 nfa_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02008047 nfa_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008048 nfa_regexec_nl,
Bram Moolenaarfda37292014-11-05 14:27:36 +01008049 nfa_regexec_multi,
8050 (char_u *)""
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008051};
8052
8053/* Which regexp engine to use? Needed for vim_regcomp().
8054 * Must match with 'regexpengine'. */
8055static int regexp_engine = 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008056
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008057#ifdef DEBUG
8058static char_u regname[][30] = {
8059 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02008060 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008061 "NFA Regexp Engine"
8062 };
8063#endif
8064
8065/*
8066 * Compile a regular expression into internal code.
Bram Moolenaar473de612013-06-08 18:19:48 +02008067 * Returns the program in allocated memory.
8068 * Use vim_regfree() to free the memory.
8069 * Returns NULL for an error.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008070 */
8071 regprog_T *
Bram Moolenaar05540972016-01-30 20:31:25 +01008072vim_regcomp(char_u *expr_arg, int re_flags)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008073{
8074 regprog_T *prog = NULL;
8075 char_u *expr = expr_arg;
8076
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008077 regexp_engine = p_re;
8078
8079 /* Check for prefix "\%#=", that sets the regexp engine */
8080 if (STRNCMP(expr, "\\%#=", 4) == 0)
8081 {
8082 int newengine = expr[4] - '0';
8083
8084 if (newengine == AUTOMATIC_ENGINE
8085 || newengine == BACKTRACKING_ENGINE
8086 || newengine == NFA_ENGINE)
8087 {
8088 regexp_engine = expr[4] - '0';
8089 expr += 5;
8090#ifdef DEBUG
Bram Moolenaar6e132072014-05-13 16:46:32 +02008091 smsg((char_u *)"New regexp mode selected (%d): %s",
8092 regexp_engine, regname[newengine]);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008093#endif
8094 }
8095 else
8096 {
8097 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
8098 regexp_engine = AUTOMATIC_ENGINE;
8099 }
8100 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008101 bt_regengine.expr = expr;
8102 nfa_regengine.expr = expr;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008103
8104 /*
8105 * First try the NFA engine, unless backtracking was requested.
8106 */
8107 if (regexp_engine != BACKTRACKING_ENGINE)
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008108 prog = nfa_regengine.regcomp(expr,
8109 re_flags + (regexp_engine == AUTOMATIC_ENGINE ? RE_AUTO : 0));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008110 else
8111 prog = bt_regengine.regcomp(expr, re_flags);
8112
Bram Moolenaarfda37292014-11-05 14:27:36 +01008113 /* Check for error compiling regexp with initial engine. */
8114 if (prog == NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008115 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008116#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008117 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
8118 {
8119 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008120 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008121 if (f)
8122 {
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008123 fprintf(f, "Syntax error in \"%s\"\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008124 fclose(f);
8125 }
8126 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008127 EMSG2("(NFA) Could not open \"%s\" to write !!!",
8128 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008129 }
8130#endif
8131 /*
Bram Moolenaarfda37292014-11-05 14:27:36 +01008132 * If the NFA engine failed, try the backtracking engine.
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008133 * The NFA engine also fails for patterns that it can't handle well
8134 * but are still valid patterns, thus a retry should work.
8135 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008136 if (regexp_engine == AUTOMATIC_ENGINE)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008137 {
Bram Moolenaare0ad3652015-01-27 12:59:55 +01008138 regexp_engine = BACKTRACKING_ENGINE;
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008139 prog = bt_regengine.regcomp(expr, re_flags);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008140 }
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008141 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008142
Bram Moolenaarfda37292014-11-05 14:27:36 +01008143 if (prog != NULL)
8144 {
8145 /* Store the info needed to call regcomp() again when the engine turns
8146 * out to be very slow when executing it. */
8147 prog->re_engine = regexp_engine;
8148 prog->re_flags = re_flags;
8149 }
8150
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008151 return prog;
8152}
8153
8154/*
Bram Moolenaar473de612013-06-08 18:19:48 +02008155 * Free a compiled regexp program, returned by vim_regcomp().
8156 */
8157 void
Bram Moolenaar05540972016-01-30 20:31:25 +01008158vim_regfree(regprog_T *prog)
Bram Moolenaar473de612013-06-08 18:19:48 +02008159{
8160 if (prog != NULL)
8161 prog->engine->regfree(prog);
8162}
8163
Bram Moolenaarfda37292014-11-05 14:27:36 +01008164#ifdef FEAT_EVAL
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008165static void report_re_switch(char_u *pat);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008166
8167 static void
Bram Moolenaar05540972016-01-30 20:31:25 +01008168report_re_switch(char_u *pat)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008169{
8170 if (p_verbose > 0)
8171 {
8172 verbose_enter();
8173 MSG_PUTS(_("Switching to backtracking RE engine for pattern: "));
8174 MSG_PUTS(pat);
8175 verbose_leave();
8176 }
8177}
8178#endif
8179
Bram Moolenaarbaaa7e92016-01-29 22:47:03 +01008180static int vim_regexec_both(regmatch_T *rmp, char_u *line, colnr_T col, int nl);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008181
Bram Moolenaar473de612013-06-08 18:19:48 +02008182/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008183 * Match a regexp against a string.
8184 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008185 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008186 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaarfda37292014-11-05 14:27:36 +01008187 * When "nl" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008188 *
8189 * Return TRUE if there is a match, FALSE if not.
8190 */
Bram Moolenaarfda37292014-11-05 14:27:36 +01008191 static int
Bram Moolenaar05540972016-01-30 20:31:25 +01008192vim_regexec_both(
8193 regmatch_T *rmp,
8194 char_u *line, /* string to match against */
8195 colnr_T col, /* column to start looking for match */
8196 int nl)
Bram Moolenaarfda37292014-11-05 14:27:36 +01008197{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008198 int result;
8199 regexec_T rex_save;
8200 int rex_in_use_save = rex_in_use;
8201
8202 if (rex_in_use)
8203 /* Being called recursively, save the state. */
8204 rex_save = rex;
8205 rex_in_use = TRUE;
8206 rex.reg_startp = NULL;
8207 rex.reg_endp = NULL;
8208 rex.reg_startpos = NULL;
8209 rex.reg_endpos = NULL;
8210
8211 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008212
8213 /* NFA engine aborted because it's very slow. */
8214 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8215 && result == NFA_TOO_EXPENSIVE)
8216 {
8217 int save_p_re = p_re;
8218 int re_flags = rmp->regprog->re_flags;
8219 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8220
8221 p_re = BACKTRACKING_ENGINE;
8222 vim_regfree(rmp->regprog);
8223 if (pat != NULL)
8224 {
8225#ifdef FEAT_EVAL
8226 report_re_switch(pat);
8227#endif
8228 rmp->regprog = vim_regcomp(pat, re_flags);
8229 if (rmp->regprog != NULL)
8230 result = rmp->regprog->engine->regexec_nl(rmp, line, col, nl);
8231 vim_free(pat);
8232 }
8233
8234 p_re = save_p_re;
8235 }
Bram Moolenaar6100d022016-10-02 16:51:57 +02008236
8237 rex_in_use = rex_in_use_save;
8238 if (rex_in_use)
8239 rex = rex_save;
8240
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008241 return result > 0;
Bram Moolenaarfda37292014-11-05 14:27:36 +01008242}
8243
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008244/*
8245 * Note: "*prog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008246 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008247 */
8248 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008249vim_regexec_prog(
8250 regprog_T **prog,
8251 int ignore_case,
8252 char_u *line,
8253 colnr_T col)
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008254{
8255 int r;
8256 regmatch_T regmatch;
8257
8258 regmatch.regprog = *prog;
8259 regmatch.rm_ic = ignore_case;
8260 r = vim_regexec_both(&regmatch, line, col, FALSE);
8261 *prog = regmatch.regprog;
8262 return r;
8263}
8264
8265/*
8266 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008267 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008268 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008269 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008270vim_regexec(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008271{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008272 return vim_regexec_both(rmp, line, col, FALSE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008273}
8274
8275#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8276 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8277/*
8278 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008279 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008280 * Return TRUE if there is a match, FALSE if not.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008281 */
8282 int
Bram Moolenaar05540972016-01-30 20:31:25 +01008283vim_regexec_nl(regmatch_T *rmp, char_u *line, colnr_T col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008284{
Bram Moolenaarfda37292014-11-05 14:27:36 +01008285 return vim_regexec_both(rmp, line, col, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008286}
8287#endif
8288
8289/*
8290 * Match a regexp against multiple lines.
8291 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
Bram Moolenaardffa5b82014-11-19 16:38:07 +01008292 * Note: "rmp->regprog" may be freed and changed.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008293 * Uses curbuf for line count and 'iskeyword'.
8294 *
8295 * Return zero if there is no match. Return number of lines contained in the
8296 * match otherwise.
8297 */
8298 long
Bram Moolenaar05540972016-01-30 20:31:25 +01008299vim_regexec_multi(
8300 regmmatch_T *rmp,
8301 win_T *win, /* window in which to search or NULL */
8302 buf_T *buf, /* buffer in which to search */
8303 linenr_T lnum, /* nr of line to start looking for match */
8304 colnr_T col, /* column to start looking for match */
8305 proftime_T *tm) /* timeout limit or NULL */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008306{
Bram Moolenaar6100d022016-10-02 16:51:57 +02008307 int result;
8308 regexec_T rex_save;
8309 int rex_in_use_save = rex_in_use;
8310
8311 if (rex_in_use)
8312 /* Being called recursively, save the state. */
8313 rex_save = rex;
8314 rex_in_use = TRUE;
8315
8316 result = rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm);
Bram Moolenaarfda37292014-11-05 14:27:36 +01008317
8318 /* NFA engine aborted because it's very slow. */
8319 if (rmp->regprog->re_engine == AUTOMATIC_ENGINE
8320 && result == NFA_TOO_EXPENSIVE)
8321 {
8322 int save_p_re = p_re;
8323 int re_flags = rmp->regprog->re_flags;
8324 char_u *pat = vim_strsave(((nfa_regprog_T *)rmp->regprog)->pattern);
8325
8326 p_re = BACKTRACKING_ENGINE;
8327 vim_regfree(rmp->regprog);
8328 if (pat != NULL)
8329 {
8330#ifdef FEAT_EVAL
8331 report_re_switch(pat);
8332#endif
8333 rmp->regprog = vim_regcomp(pat, re_flags);
8334 if (rmp->regprog != NULL)
8335 result = rmp->regprog->engine->regexec_multi(
8336 rmp, win, buf, lnum, col, tm);
8337 vim_free(pat);
8338 }
8339 p_re = save_p_re;
8340 }
8341
Bram Moolenaar6100d022016-10-02 16:51:57 +02008342 rex_in_use = rex_in_use_save;
8343 if (rex_in_use)
8344 rex = rex_save;
8345
Bram Moolenaar66a3e792014-11-20 23:07:05 +01008346 return result <= 0 ? 0 : result;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008347}