blob: 26fb813c87d73e50a8561c875107c1878ce5fcb9 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * Handling of regular expressions: vim_regcomp(), vim_regexec(), vim_regsub()
4 *
5 * NOTICE:
6 *
7 * This is NOT the original regular expression code as written by Henry
8 * Spencer. This code has been modified specifically for use with the VIM
9 * editor, and should not be used separately from Vim. If you want a good
10 * regular expression library, get the original code. The copyright notice
11 * that follows is from the original.
12 *
13 * END NOTICE
14 *
15 * Copyright (c) 1986 by University of Toronto.
16 * Written by Henry Spencer. Not derived from licensed software.
17 *
18 * Permission is granted to anyone to use this software for any
19 * purpose on any computer system, and to redistribute it freely,
20 * subject to the following restrictions:
21 *
22 * 1. The author is not responsible for the consequences of use of
23 * this software, no matter how awful, even if they arise
24 * from defects in it.
25 *
26 * 2. The origin of this software must not be misrepresented, either
27 * by explicit claim or by omission.
28 *
29 * 3. Altered versions must be plainly marked as such, and must not
30 * be misrepresented as being the original software.
31 *
32 * Beware that some of this code is subtly aware of the way operator
33 * precedence is structured in regular expressions. Serious changes in
34 * regular-expression syntax might require a total rethink.
35 *
Bram 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
258static int no_Magic __ARGS((int x));
259static int toggle_Magic __ARGS((int x));
260
261 static int
262no_Magic(x)
263 int x;
264{
265 if (is_Magic(x))
266 return un_Magic(x);
267 return x;
268}
269
270 static int
271toggle_Magic(x)
272 int x;
273{
274 if (is_Magic(x))
275 return un_Magic(x);
276 return Magic(x);
277}
278
279/*
280 * The first byte of the regexp internal "program" is actually this magic
281 * number; the start node begins in the second byte. It's used to catch the
282 * most severe mutilation of the program by the caller.
283 */
284
285#define REGMAGIC 0234
286
287/*
288 * Opcode notes:
289 *
290 * BRANCH The set of branches constituting a single choice are hooked
291 * together with their "next" pointers, since precedence prevents
292 * anything being concatenated to any individual branch. The
293 * "next" pointer of the last BRANCH in a choice points to the
294 * thing following the whole choice. This is also where the
295 * final "next" pointer of each individual branch points; each
296 * branch starts with the operand node of a BRANCH node.
297 *
298 * BACK Normal "next" pointers all implicitly point forward; BACK
299 * exists to make loop structures possible.
300 *
301 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
302 * BRANCH structures using BACK. Simple cases (one character
303 * per match) are implemented with STAR and PLUS for speed
304 * and to minimize recursive plunges.
305 *
306 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
307 * node, and defines the min and max limits to be used for that
308 * node.
309 *
310 * MOPEN,MCLOSE ...are numbered at compile time.
311 * ZOPEN,ZCLOSE ...ditto
312 */
313
314/*
315 * A node is one char of opcode followed by two chars of "next" pointer.
316 * "Next" pointers are stored as two 8-bit bytes, high order first. The
317 * value is a positive offset from the opcode of the node containing it.
318 * An operand, if any, simply follows the node. (Note that much of the
319 * code generation knows about this implicit relationship.)
320 *
321 * Using two bytes for the "next" pointer is vast overkill for most things,
322 * but allows patterns to get big without disasters.
323 */
324#define OP(p) ((int)*(p))
325#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
326#define OPERAND(p) ((p) + 3)
327/* Obtain an operand that was stored as four bytes, MSB first. */
328#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
329 + ((long)(p)[5] << 8) + (long)(p)[6])
330/* Obtain a second operand stored as four bytes. */
331#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
332/* Obtain a second single-byte operand stored after a four bytes operand. */
333#define OPERAND_CMP(p) (p)[7]
334
335/*
336 * Utility definitions.
337 */
338#define UCHARAT(p) ((int)*(char_u *)(p))
339
340/* Used for an error (down from) vim_regcomp(): give the error message, set
341 * rc_did_emsg and return NULL */
Bram Moolenaar98692072006-02-04 00:57:42 +0000342#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, (void *)NULL)
Bram Moolenaar45eeb132005-06-06 21:59:07 +0000343#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200344#define EMSG2_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, (void *)NULL)
345#define EMSG2_RET_FAIL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, FAIL)
346#define EMSG_ONE_RET_NULL EMSG2_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
348#define MAX_LIMIT (32767L << 16L)
349
350static int re_multi_type __ARGS((int));
351static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n));
352static char_u *cstrchr __ARGS((char_u *, int));
353
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200354#ifdef BT_REGEXP_DUMP
355static void regdump __ARGS((char_u *, bt_regprog_T *));
356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358static char_u *regprop __ARGS((char_u *));
359#endif
360
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200361static char_u e_missingbracket[] = N_("E769: Missing ] after %s[");
362static char_u e_unmatchedpp[] = N_("E53: Unmatched %s%%(");
363static char_u e_unmatchedp[] = N_("E54: Unmatched %s(");
364static char_u e_unmatchedpar[] = N_("E55: Unmatched %s)");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200365#ifdef FEAT_SYN_HL
Bram Moolenaar5de820b2013-06-02 15:01:57 +0200366static char_u e_z_not_allowed[] = N_("E66: \\z( not allowed here");
367static char_u e_z1_not_allowed[] = N_("E67: \\z1 et al. not allowed here");
Bram Moolenaar01d89dd2013-06-03 19:41:06 +0200368#endif
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +0200369static char_u e_missing_sb[] = N_("E69: Missing ] after %s%%[");
Bram Moolenaar2976c022013-06-05 21:30:37 +0200370static char_u e_empty_sb[] = N_("E70: Empty %s%%[]");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000371#define NOT_MULTI 0
372#define MULTI_ONE 1
373#define MULTI_MULT 2
374/*
375 * Return NOT_MULTI if c is not a "multi" operator.
376 * Return MULTI_ONE if c is a single "multi" operator.
377 * Return MULTI_MULT if c is a multi "multi" operator.
378 */
379 static int
380re_multi_type(c)
381 int c;
382{
383 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
384 return MULTI_ONE;
385 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
386 return MULTI_MULT;
387 return NOT_MULTI;
388}
389
390/*
391 * Flags to be passed up and down.
392 */
393#define HASWIDTH 0x1 /* Known never to match null string. */
394#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
395#define SPSTART 0x4 /* Starts with * or +. */
396#define HASNL 0x8 /* Contains some \n. */
397#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
398#define WORST 0 /* Worst case. */
399
400/*
401 * When regcode is set to this value, code is not emitted and size is computed
402 * instead.
403 */
404#define JUST_CALC_SIZE ((char_u *) -1)
405
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000406static char_u *reg_prev_sub = NULL;
407
Bram Moolenaar071d4272004-06-13 20:20:40 +0000408/*
409 * REGEXP_INRANGE contains all characters which are always special in a []
410 * range after '\'.
411 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
412 * These are:
413 * \n - New line (NL).
414 * \r - Carriage Return (CR).
415 * \t - Tab (TAB).
416 * \e - Escape (ESC).
417 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000418 * \d - Character code in decimal, eg \d123
419 * \o - Character code in octal, eg \o80
420 * \x - Character code in hex, eg \x4a
421 * \u - Multibyte character code, eg \u20ac
422 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 */
424static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000425static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000426
427static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000428static int get_char_class __ARGS((char_u **pp));
429static int get_equi_class __ARGS((char_u **pp));
430static void reg_equi_class __ARGS((int c));
431static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432static char_u *skip_anyof __ARGS((char_u *p));
433static void init_class_tab __ARGS((void));
434
435/*
436 * Translate '\x' to its control character, except "\n", which is Magic.
437 */
438 static int
439backslash_trans(c)
440 int c;
441{
442 switch (c)
443 {
444 case 'r': return CAR;
445 case 't': return TAB;
446 case 'e': return ESC;
447 case 'b': return BS;
448 }
449 return c;
450}
451
452/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000453 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000454 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
455 * recognized. Otherwise "pp" is advanced to after the item.
456 */
457 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000458get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459 char_u **pp;
460{
461 static const char *(class_names[]) =
462 {
463 "alnum:]",
464#define CLASS_ALNUM 0
465 "alpha:]",
466#define CLASS_ALPHA 1
467 "blank:]",
468#define CLASS_BLANK 2
469 "cntrl:]",
470#define CLASS_CNTRL 3
471 "digit:]",
472#define CLASS_DIGIT 4
473 "graph:]",
474#define CLASS_GRAPH 5
475 "lower:]",
476#define CLASS_LOWER 6
477 "print:]",
478#define CLASS_PRINT 7
479 "punct:]",
480#define CLASS_PUNCT 8
481 "space:]",
482#define CLASS_SPACE 9
483 "upper:]",
484#define CLASS_UPPER 10
485 "xdigit:]",
486#define CLASS_XDIGIT 11
487 "tab:]",
488#define CLASS_TAB 12
489 "return:]",
490#define CLASS_RETURN 13
491 "backspace:]",
492#define CLASS_BACKSPACE 14
493 "escape:]",
494#define CLASS_ESCAPE 15
495 };
496#define CLASS_NONE 99
497 int i;
498
499 if ((*pp)[1] == ':')
500 {
Bram Moolenaar78a15312009-05-15 19:33:18 +0000501 for (i = 0; i < (int)(sizeof(class_names) / sizeof(*class_names)); ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000502 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
503 {
504 *pp += STRLEN(class_names[i]) + 2;
505 return i;
506 }
507 }
508 return CLASS_NONE;
509}
510
511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 * Specific version of character class functions.
513 * Using a table to keep this fast.
514 */
515static short class_tab[256];
516
517#define RI_DIGIT 0x01
518#define RI_HEX 0x02
519#define RI_OCTAL 0x04
520#define RI_WORD 0x08
521#define RI_HEAD 0x10
522#define RI_ALPHA 0x20
523#define RI_LOWER 0x40
524#define RI_UPPER 0x80
525#define RI_WHITE 0x100
526
527 static void
528init_class_tab()
529{
530 int i;
531 static int done = FALSE;
532
533 if (done)
534 return;
535
536 for (i = 0; i < 256; ++i)
537 {
538 if (i >= '0' && i <= '7')
539 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
540 else if (i >= '8' && i <= '9')
541 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
542 else if (i >= 'a' && i <= 'f')
543 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
544#ifdef EBCDIC
545 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
546 || (i >= 's' && i <= 'z'))
547#else
548 else if (i >= 'g' && i <= 'z')
549#endif
550 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
551 else if (i >= 'A' && i <= 'F')
552 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
553#ifdef EBCDIC
554 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
555 || (i >= 'S' && i <= 'Z'))
556#else
557 else if (i >= 'G' && i <= 'Z')
558#endif
559 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
560 else if (i == '_')
561 class_tab[i] = RI_WORD + RI_HEAD;
562 else
563 class_tab[i] = 0;
564 }
565 class_tab[' '] |= RI_WHITE;
566 class_tab['\t'] |= RI_WHITE;
567 done = TRUE;
568}
569
570#ifdef FEAT_MBYTE
571# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
572# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
573# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
574# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
575# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
576# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
577# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
578# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
579# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
580#else
581# define ri_digit(c) (class_tab[c] & RI_DIGIT)
582# define ri_hex(c) (class_tab[c] & RI_HEX)
583# define ri_octal(c) (class_tab[c] & RI_OCTAL)
584# define ri_word(c) (class_tab[c] & RI_WORD)
585# define ri_head(c) (class_tab[c] & RI_HEAD)
586# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
587# define ri_lower(c) (class_tab[c] & RI_LOWER)
588# define ri_upper(c) (class_tab[c] & RI_UPPER)
589# define ri_white(c) (class_tab[c] & RI_WHITE)
590#endif
591
592/* flags for regflags */
593#define RF_ICASE 1 /* ignore case */
594#define RF_NOICASE 2 /* don't ignore case */
595#define RF_HASNL 4 /* can match a NL */
596#define RF_ICOMBINE 8 /* ignore combining characters */
597#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
598
599/*
600 * Global work variables for vim_regcomp().
601 */
602
603static char_u *regparse; /* Input-scan pointer. */
604static int prevchr_len; /* byte length of previous char */
605static int num_complex_braces; /* Complex \{...} count */
606static int regnpar; /* () count. */
607#ifdef FEAT_SYN_HL
608static int regnzpar; /* \z() count. */
609static int re_has_z; /* \z item detected */
610#endif
611static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
612static long regsize; /* Code size. */
Bram Moolenaard3005802009-11-25 17:21:32 +0000613static int reg_toolong; /* TRUE when offset out of range */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000614static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
615static unsigned regflags; /* RF_ flags for prog */
616static long brace_min[10]; /* Minimums for complex brace repeats */
617static long brace_max[10]; /* Maximums for complex brace repeats */
618static int brace_count[10]; /* Current counts for complex brace repeats */
619#if defined(FEAT_SYN_HL) || defined(PROTO)
620static int had_eol; /* TRUE when EOL found by vim_regcomp() */
621#endif
622static int one_exactly = FALSE; /* only do one char for EXACTLY */
623
624static int reg_magic; /* magicness of the pattern: */
625#define MAGIC_NONE 1 /* "\V" very unmagic */
626#define MAGIC_OFF 2 /* "\M" or 'magic' off */
627#define MAGIC_ON 3 /* "\m" or 'magic' */
628#define MAGIC_ALL 4 /* "\v" very magic */
629
630static int reg_string; /* matching with a string instead of a buffer
631 line */
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000632static int reg_strict; /* "[abc" is illegal */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000633
634/*
635 * META contains all characters that may be magic, except '^' and '$'.
636 */
637
638#ifdef EBCDIC
639static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
640#else
641/* META[] is used often enough to justify turning it into a table. */
642static char_u META_flags[] = {
643 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
644 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
645/* % & ( ) * + . */
646 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
647/* 1 2 3 4 5 6 7 8 9 < = > ? */
648 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
649/* @ A C D F H I K L M O */
650 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
651/* P S U V W X Z [ _ */
652 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
653/* a c d f h i k l m n o */
654 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
655/* p s u v w x z { | ~ */
656 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
657};
658#endif
659
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200660static int curchr; /* currently parsed character */
661/* Previous character. Note: prevchr is sometimes -1 when we are not at the
662 * start, eg in /[ ^I]^ the pattern was never found even if it existed,
663 * because ^ was taken to be magic -- webb */
664static int prevchr;
665static int prevprevchr; /* previous-previous character */
666static int nextchr; /* used for ungetchr() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667
668/* arguments for reg() */
669#define REG_NOPAREN 0 /* toplevel reg() */
670#define REG_PAREN 1 /* \(\) */
671#define REG_ZPAREN 2 /* \z(\) */
672#define REG_NPAREN 3 /* \%(\) */
673
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200674typedef struct
675{
676 char_u *regparse;
677 int prevchr_len;
678 int curchr;
679 int prevchr;
680 int prevprevchr;
681 int nextchr;
682 int at_start;
683 int prev_at_start;
684 int regnpar;
685} parse_state_T;
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687/*
688 * Forward declarations for vim_regcomp()'s friends.
689 */
690static void initchr __ARGS((char_u *));
Bram Moolenaar3737fc12013-06-01 14:42:56 +0200691static void save_parse_state __ARGS((parse_state_T *ps));
692static void restore_parse_state __ARGS((parse_state_T *ps));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693static int getchr __ARGS((void));
694static void skipchr_keepstart __ARGS((void));
695static int peekchr __ARGS((void));
696static void skipchr __ARGS((void));
697static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000698static int gethexchrs __ARGS((int maxinputlen));
699static int getoctchrs __ARGS((void));
700static int getdecchrs __ARGS((void));
701static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702static void regcomp_start __ARGS((char_u *expr, int flags));
703static char_u *reg __ARGS((int, int *));
704static char_u *regbranch __ARGS((int *flagp));
705static char_u *regconcat __ARGS((int *flagp));
706static char_u *regpiece __ARGS((int *));
707static char_u *regatom __ARGS((int *));
708static char_u *regnode __ARGS((int));
Bram Moolenaar362e1a32006-03-06 23:29:24 +0000709#ifdef FEAT_MBYTE
710static int use_multibytecode __ARGS((int c));
711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712static int prog_magic_wrong __ARGS((void));
713static char_u *regnext __ARGS((char_u *));
714static void regc __ARGS((int b));
715#ifdef FEAT_MBYTE
716static void regmbc __ARGS((int c));
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200717# define REGMBC(x) regmbc(x);
718# define CASEMBC(x) case x:
Bram Moolenaardf177f62005-02-22 08:39:57 +0000719#else
720# define regmbc(c) regc(c)
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200721# define REGMBC(x)
722# define CASEMBC(x)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723#endif
724static void reginsert __ARGS((int, char_u *));
Bram Moolenaar75eb1612013-05-29 18:45:11 +0200725static void reginsert_nr __ARGS((int op, long val, char_u *opnd));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726static void reginsert_limits __ARGS((int, long, long, char_u *));
727static char_u *re_put_long __ARGS((char_u *pr, long_u val));
728static int read_limits __ARGS((long *, long *));
729static void regtail __ARGS((char_u *, char_u *));
730static void regoptail __ARGS((char_u *, char_u *));
731
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200732static regengine_T bt_regengine;
733static regengine_T nfa_regengine;
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735/*
736 * Return TRUE if compiled regular expression "prog" can match a line break.
737 */
738 int
739re_multiline(prog)
740 regprog_T *prog;
741{
742 return (prog->regflags & RF_HASNL);
743}
744
745/*
746 * Return TRUE if compiled regular expression "prog" looks before the start
747 * position (pattern contains "\@<=" or "\@<!").
748 */
749 int
750re_lookbehind(prog)
751 regprog_T *prog;
752{
753 return (prog->regflags & RF_LOOKBH);
754}
755
756/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000757 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
758 * Returns a character representing the class. Zero means that no item was
759 * recognized. Otherwise "pp" is advanced to after the item.
760 */
761 static int
762get_equi_class(pp)
763 char_u **pp;
764{
765 int c;
766 int l = 1;
767 char_u *p = *pp;
768
769 if (p[1] == '=')
770 {
771#ifdef FEAT_MBYTE
772 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000773 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +0000774#endif
775 if (p[l + 2] == '=' && p[l + 3] == ']')
776 {
777#ifdef FEAT_MBYTE
778 if (has_mbyte)
779 c = mb_ptr2char(p + 2);
780 else
781#endif
782 c = p[2];
783 *pp += l + 4;
784 return c;
785 }
786 }
787 return 0;
788}
789
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200790#ifdef EBCDIC
791/*
792 * Table for equivalence class "c". (IBM-1047)
793 */
794char *EQUIVAL_CLASS_C[16] = {
795 "A\x62\x63\x64\x65\x66\x67",
796 "C\x68",
797 "E\x71\x72\x73\x74",
798 "I\x75\x76\x77\x78",
799 "N\x69",
800 "O\xEB\xEC\xED\xEE\xEF",
801 "U\xFB\xFC\xFD\xFE",
802 "Y\xBA",
803 "a\x42\x43\x44\x45\x46\x47",
804 "c\x48",
805 "e\x51\x52\x53\x54",
806 "i\x55\x56\x57\x58",
807 "n\x49",
808 "o\xCB\xCC\xCD\xCE\xCF",
809 "u\xDB\xDC\xDD\xDE",
810 "y\x8D\xDF",
811};
812#endif
813
Bram Moolenaardf177f62005-02-22 08:39:57 +0000814/*
815 * Produce the bytes for equivalence class "c".
816 * Currently only handles latin1, latin9 and utf-8.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +0200817 * NOTE: When changing this function, also change nfa_emit_equi_class()
Bram Moolenaardf177f62005-02-22 08:39:57 +0000818 */
819 static void
820reg_equi_class(c)
821 int c;
822{
823#ifdef FEAT_MBYTE
824 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
Bram Moolenaar78622822005-08-23 21:00:13 +0000825 || STRCMP(p_enc, "iso-8859-15") == 0)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000826#endif
827 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200828#ifdef EBCDIC
829 int i;
830
831 /* This might be slower than switch/case below. */
832 for (i = 0; i < 16; i++)
833 {
834 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
835 {
836 char *p = EQUIVAL_CLASS_C[i];
837
838 while (*p != 0)
839 regmbc(*p++);
840 return;
841 }
842 }
843#else
Bram Moolenaardf177f62005-02-22 08:39:57 +0000844 switch (c)
845 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000846 case 'A': case '\300': case '\301': case '\302':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200847 CASEMBC(0x100) CASEMBC(0x102) CASEMBC(0x104) CASEMBC(0x1cd)
848 CASEMBC(0x1de) CASEMBC(0x1e0) CASEMBC(0x1ea2)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000849 case '\303': case '\304': case '\305':
850 regmbc('A'); regmbc('\300'); regmbc('\301');
851 regmbc('\302'); regmbc('\303'); regmbc('\304');
852 regmbc('\305');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200853 REGMBC(0x100) REGMBC(0x102) REGMBC(0x104)
854 REGMBC(0x1cd) REGMBC(0x1de) REGMBC(0x1e0)
855 REGMBC(0x1ea2)
856 return;
857 case 'B': CASEMBC(0x1e02) CASEMBC(0x1e06)
858 regmbc('B'); REGMBC(0x1e02) REGMBC(0x1e06)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000859 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000860 case 'C': case '\307':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200861 CASEMBC(0x106) CASEMBC(0x108) CASEMBC(0x10a) CASEMBC(0x10c)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000862 regmbc('C'); regmbc('\307');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200863 REGMBC(0x106) REGMBC(0x108) REGMBC(0x10a)
864 REGMBC(0x10c)
865 return;
866 case 'D': CASEMBC(0x10e) CASEMBC(0x110) CASEMBC(0x1e0a)
867 CASEMBC(0x1e0e) CASEMBC(0x1e10)
868 regmbc('D'); REGMBC(0x10e) REGMBC(0x110)
869 REGMBC(0x1e0a) REGMBC(0x1e0e) REGMBC(0x1e10)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000870 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000871 case 'E': case '\310': case '\311': case '\312': case '\313':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200872 CASEMBC(0x112) CASEMBC(0x114) CASEMBC(0x116) CASEMBC(0x118)
873 CASEMBC(0x11a) CASEMBC(0x1eba) CASEMBC(0x1ebc)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000874 regmbc('E'); regmbc('\310'); regmbc('\311');
875 regmbc('\312'); regmbc('\313');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200876 REGMBC(0x112) REGMBC(0x114) REGMBC(0x116)
877 REGMBC(0x118) REGMBC(0x11a) REGMBC(0x1eba)
878 REGMBC(0x1ebc)
879 return;
880 case 'F': CASEMBC(0x1e1e)
881 regmbc('F'); REGMBC(0x1e1e)
882 return;
883 case 'G': CASEMBC(0x11c) CASEMBC(0x11e) CASEMBC(0x120)
884 CASEMBC(0x122) CASEMBC(0x1e4) CASEMBC(0x1e6) CASEMBC(0x1f4)
885 CASEMBC(0x1e20)
886 regmbc('G'); REGMBC(0x11c) REGMBC(0x11e)
887 REGMBC(0x120) REGMBC(0x122) REGMBC(0x1e4)
888 REGMBC(0x1e6) REGMBC(0x1f4) REGMBC(0x1e20)
889 return;
890 case 'H': CASEMBC(0x124) CASEMBC(0x126) CASEMBC(0x1e22)
891 CASEMBC(0x1e26) CASEMBC(0x1e28)
892 regmbc('H'); REGMBC(0x124) REGMBC(0x126)
893 REGMBC(0x1e22) REGMBC(0x1e26) REGMBC(0x1e28)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000894 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000895 case 'I': case '\314': case '\315': case '\316': case '\317':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200896 CASEMBC(0x128) CASEMBC(0x12a) CASEMBC(0x12c) CASEMBC(0x12e)
897 CASEMBC(0x130) CASEMBC(0x1cf) CASEMBC(0x1ec8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000898 regmbc('I'); regmbc('\314'); regmbc('\315');
899 regmbc('\316'); regmbc('\317');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200900 REGMBC(0x128) REGMBC(0x12a) REGMBC(0x12c)
901 REGMBC(0x12e) REGMBC(0x130) REGMBC(0x1cf)
902 REGMBC(0x1ec8)
903 return;
904 case 'J': CASEMBC(0x134)
905 regmbc('J'); REGMBC(0x134)
906 return;
907 case 'K': CASEMBC(0x136) CASEMBC(0x1e8) CASEMBC(0x1e30)
908 CASEMBC(0x1e34)
909 regmbc('K'); REGMBC(0x136) REGMBC(0x1e8)
910 REGMBC(0x1e30) REGMBC(0x1e34)
911 return;
912 case 'L': CASEMBC(0x139) CASEMBC(0x13b) CASEMBC(0x13d)
913 CASEMBC(0x13f) CASEMBC(0x141) CASEMBC(0x1e3a)
914 regmbc('L'); REGMBC(0x139) REGMBC(0x13b)
915 REGMBC(0x13d) REGMBC(0x13f) REGMBC(0x141)
916 REGMBC(0x1e3a)
917 return;
918 case 'M': CASEMBC(0x1e3e) CASEMBC(0x1e40)
919 regmbc('M'); REGMBC(0x1e3e) REGMBC(0x1e40)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000920 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000921 case 'N': case '\321':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200922 CASEMBC(0x143) CASEMBC(0x145) CASEMBC(0x147) CASEMBC(0x1e44)
923 CASEMBC(0x1e48)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000924 regmbc('N'); regmbc('\321');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200925 REGMBC(0x143) REGMBC(0x145) REGMBC(0x147)
926 REGMBC(0x1e44) REGMBC(0x1e48)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000927 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000928 case 'O': case '\322': case '\323': case '\324': case '\325':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200929 case '\326': case '\330':
930 CASEMBC(0x14c) CASEMBC(0x14e) CASEMBC(0x150) CASEMBC(0x1a0)
931 CASEMBC(0x1d1) CASEMBC(0x1ea) CASEMBC(0x1ec) CASEMBC(0x1ece)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000932 regmbc('O'); regmbc('\322'); regmbc('\323');
933 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200934 regmbc('\330');
935 REGMBC(0x14c) REGMBC(0x14e) REGMBC(0x150)
936 REGMBC(0x1a0) REGMBC(0x1d1) REGMBC(0x1ea)
937 REGMBC(0x1ec) REGMBC(0x1ece)
938 return;
939 case 'P': case 0x1e54: case 0x1e56:
940 regmbc('P'); REGMBC(0x1e54) REGMBC(0x1e56)
941 return;
942 case 'R': CASEMBC(0x154) CASEMBC(0x156) CASEMBC(0x158)
943 CASEMBC(0x1e58) CASEMBC(0x1e5e)
944 regmbc('R'); REGMBC(0x154) REGMBC(0x156) REGMBC(0x158)
945 REGMBC(0x1e58) REGMBC(0x1e5e)
946 return;
947 case 'S': CASEMBC(0x15a) CASEMBC(0x15c) CASEMBC(0x15e)
948 CASEMBC(0x160) CASEMBC(0x1e60)
949 regmbc('S'); REGMBC(0x15a) REGMBC(0x15c)
950 REGMBC(0x15e) REGMBC(0x160) REGMBC(0x1e60)
951 return;
952 case 'T': CASEMBC(0x162) CASEMBC(0x164) CASEMBC(0x166)
953 CASEMBC(0x1e6a) CASEMBC(0x1e6e)
954 regmbc('T'); REGMBC(0x162) REGMBC(0x164)
955 REGMBC(0x166) REGMBC(0x1e6a) REGMBC(0x1e6e)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000956 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000957 case 'U': case '\331': case '\332': case '\333': case '\334':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200958 CASEMBC(0x168) CASEMBC(0x16a) CASEMBC(0x16c) CASEMBC(0x16e)
959 CASEMBC(0x170) CASEMBC(0x172) CASEMBC(0x1af) CASEMBC(0x1d3)
960 CASEMBC(0x1ee6)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000961 regmbc('U'); regmbc('\331'); regmbc('\332');
962 regmbc('\333'); regmbc('\334');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200963 REGMBC(0x168) REGMBC(0x16a) REGMBC(0x16c)
964 REGMBC(0x16e) REGMBC(0x170) REGMBC(0x172)
965 REGMBC(0x1af) REGMBC(0x1d3) REGMBC(0x1ee6)
966 return;
967 case 'V': CASEMBC(0x1e7c)
968 regmbc('V'); REGMBC(0x1e7c)
969 return;
970 case 'W': CASEMBC(0x174) CASEMBC(0x1e80) CASEMBC(0x1e82)
971 CASEMBC(0x1e84) CASEMBC(0x1e86)
972 regmbc('W'); REGMBC(0x174) REGMBC(0x1e80)
973 REGMBC(0x1e82) REGMBC(0x1e84) REGMBC(0x1e86)
974 return;
975 case 'X': CASEMBC(0x1e8a) CASEMBC(0x1e8c)
976 regmbc('X'); REGMBC(0x1e8a) REGMBC(0x1e8c)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000977 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000978 case 'Y': case '\335':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200979 CASEMBC(0x176) CASEMBC(0x178) CASEMBC(0x1e8e) CASEMBC(0x1ef2)
980 CASEMBC(0x1ef6) CASEMBC(0x1ef8)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000981 regmbc('Y'); regmbc('\335');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200982 REGMBC(0x176) REGMBC(0x178) REGMBC(0x1e8e)
983 REGMBC(0x1ef2) REGMBC(0x1ef6) REGMBC(0x1ef8)
984 return;
985 case 'Z': CASEMBC(0x179) CASEMBC(0x17b) CASEMBC(0x17d)
986 CASEMBC(0x1b5) CASEMBC(0x1e90) CASEMBC(0x1e94)
987 regmbc('Z'); REGMBC(0x179) REGMBC(0x17b)
988 REGMBC(0x17d) REGMBC(0x1b5) REGMBC(0x1e90)
989 REGMBC(0x1e94)
Bram Moolenaardf177f62005-02-22 08:39:57 +0000990 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000991 case 'a': case '\340': case '\341': case '\342':
992 case '\343': case '\344': case '\345':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200993 CASEMBC(0x101) CASEMBC(0x103) CASEMBC(0x105) CASEMBC(0x1ce)
994 CASEMBC(0x1df) CASEMBC(0x1e1) CASEMBC(0x1ea3)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000995 regmbc('a'); regmbc('\340'); regmbc('\341');
996 regmbc('\342'); regmbc('\343'); regmbc('\344');
997 regmbc('\345');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +0200998 REGMBC(0x101) REGMBC(0x103) REGMBC(0x105)
999 REGMBC(0x1ce) REGMBC(0x1df) REGMBC(0x1e1)
1000 REGMBC(0x1ea3)
1001 return;
1002 case 'b': CASEMBC(0x1e03) CASEMBC(0x1e07)
1003 regmbc('b'); REGMBC(0x1e03) REGMBC(0x1e07)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001004 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001005 case 'c': case '\347':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001006 CASEMBC(0x107) CASEMBC(0x109) CASEMBC(0x10b) CASEMBC(0x10d)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001007 regmbc('c'); regmbc('\347');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001008 REGMBC(0x107) REGMBC(0x109) REGMBC(0x10b)
1009 REGMBC(0x10d)
1010 return;
1011 case 'd': CASEMBC(0x10f) CASEMBC(0x111) CASEMBC(0x1d0b)
1012 CASEMBC(0x1e11)
1013 regmbc('d'); REGMBC(0x10f) REGMBC(0x111)
1014 REGMBC(0x1e0b) REGMBC(0x01e0f) REGMBC(0x1e11)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001015 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001016 case 'e': case '\350': case '\351': case '\352': case '\353':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001017 CASEMBC(0x113) CASEMBC(0x115) CASEMBC(0x117) CASEMBC(0x119)
1018 CASEMBC(0x11b) CASEMBC(0x1ebb) CASEMBC(0x1ebd)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001019 regmbc('e'); regmbc('\350'); regmbc('\351');
1020 regmbc('\352'); regmbc('\353');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001021 REGMBC(0x113) REGMBC(0x115) REGMBC(0x117)
1022 REGMBC(0x119) REGMBC(0x11b) REGMBC(0x1ebb)
1023 REGMBC(0x1ebd)
1024 return;
1025 case 'f': CASEMBC(0x1e1f)
1026 regmbc('f'); REGMBC(0x1e1f)
1027 return;
1028 case 'g': CASEMBC(0x11d) CASEMBC(0x11f) CASEMBC(0x121)
1029 CASEMBC(0x123) CASEMBC(0x1e5) CASEMBC(0x1e7) CASEMBC(0x1f5)
1030 CASEMBC(0x1e21)
1031 regmbc('g'); REGMBC(0x11d) REGMBC(0x11f)
1032 REGMBC(0x121) REGMBC(0x123) REGMBC(0x1e5)
1033 REGMBC(0x1e7) REGMBC(0x1f5) REGMBC(0x1e21)
1034 return;
1035 case 'h': CASEMBC(0x125) CASEMBC(0x127) CASEMBC(0x1e23)
1036 CASEMBC(0x1e27) CASEMBC(0x1e29) CASEMBC(0x1e96)
1037 regmbc('h'); REGMBC(0x125) REGMBC(0x127)
1038 REGMBC(0x1e23) REGMBC(0x1e27) REGMBC(0x1e29)
1039 REGMBC(0x1e96)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001040 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001041 case 'i': case '\354': case '\355': case '\356': case '\357':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001042 CASEMBC(0x129) CASEMBC(0x12b) CASEMBC(0x12d) CASEMBC(0x12f)
1043 CASEMBC(0x1d0) CASEMBC(0x1ec9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001044 regmbc('i'); regmbc('\354'); regmbc('\355');
1045 regmbc('\356'); regmbc('\357');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001046 REGMBC(0x129) REGMBC(0x12b) REGMBC(0x12d)
1047 REGMBC(0x12f) REGMBC(0x1d0) REGMBC(0x1ec9)
1048 return;
1049 case 'j': CASEMBC(0x135) CASEMBC(0x1f0)
1050 regmbc('j'); REGMBC(0x135) REGMBC(0x1f0)
1051 return;
1052 case 'k': CASEMBC(0x137) CASEMBC(0x1e9) CASEMBC(0x1e31)
1053 CASEMBC(0x1e35)
1054 regmbc('k'); REGMBC(0x137) REGMBC(0x1e9)
1055 REGMBC(0x1e31) REGMBC(0x1e35)
1056 return;
1057 case 'l': CASEMBC(0x13a) CASEMBC(0x13c) CASEMBC(0x13e)
1058 CASEMBC(0x140) CASEMBC(0x142) CASEMBC(0x1e3b)
1059 regmbc('l'); REGMBC(0x13a) REGMBC(0x13c)
1060 REGMBC(0x13e) REGMBC(0x140) REGMBC(0x142)
1061 REGMBC(0x1e3b)
1062 return;
1063 case 'm': CASEMBC(0x1e3f) CASEMBC(0x1e41)
1064 regmbc('m'); REGMBC(0x1e3f) REGMBC(0x1e41)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001065 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001066 case 'n': case '\361':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001067 CASEMBC(0x144) CASEMBC(0x146) CASEMBC(0x148) CASEMBC(0x149)
1068 CASEMBC(0x1e45) CASEMBC(0x1e49)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001069 regmbc('n'); regmbc('\361');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001070 REGMBC(0x144) REGMBC(0x146) REGMBC(0x148)
1071 REGMBC(0x149) REGMBC(0x1e45) REGMBC(0x1e49)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001072 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001073 case 'o': case '\362': case '\363': case '\364': case '\365':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001074 case '\366': case '\370':
1075 CASEMBC(0x14d) CASEMBC(0x14f) CASEMBC(0x151) CASEMBC(0x1a1)
1076 CASEMBC(0x1d2) CASEMBC(0x1eb) CASEMBC(0x1ed) CASEMBC(0x1ecf)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001077 regmbc('o'); regmbc('\362'); regmbc('\363');
1078 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001079 regmbc('\370');
1080 REGMBC(0x14d) REGMBC(0x14f) REGMBC(0x151)
1081 REGMBC(0x1a1) REGMBC(0x1d2) REGMBC(0x1eb)
1082 REGMBC(0x1ed) REGMBC(0x1ecf)
1083 return;
1084 case 'p': CASEMBC(0x1e55) CASEMBC(0x1e57)
1085 regmbc('p'); REGMBC(0x1e55) REGMBC(0x1e57)
1086 return;
1087 case 'r': CASEMBC(0x155) CASEMBC(0x157) CASEMBC(0x159)
1088 CASEMBC(0x1e59) CASEMBC(0x1e5f)
1089 regmbc('r'); REGMBC(0x155) REGMBC(0x157) REGMBC(0x159)
1090 REGMBC(0x1e59) REGMBC(0x1e5f)
1091 return;
1092 case 's': CASEMBC(0x15b) CASEMBC(0x15d) CASEMBC(0x15f)
1093 CASEMBC(0x161) CASEMBC(0x1e61)
1094 regmbc('s'); REGMBC(0x15b) REGMBC(0x15d)
1095 REGMBC(0x15f) REGMBC(0x161) REGMBC(0x1e61)
1096 return;
1097 case 't': CASEMBC(0x163) CASEMBC(0x165) CASEMBC(0x167)
1098 CASEMBC(0x1e6b) CASEMBC(0x1e6f) CASEMBC(0x1e97)
1099 regmbc('t'); REGMBC(0x163) REGMBC(0x165) REGMBC(0x167)
1100 REGMBC(0x1e6b) REGMBC(0x1e6f) REGMBC(0x1e97)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001101 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001102 case 'u': case '\371': case '\372': case '\373': case '\374':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001103 CASEMBC(0x169) CASEMBC(0x16b) CASEMBC(0x16d) CASEMBC(0x16f)
1104 CASEMBC(0x171) CASEMBC(0x173) CASEMBC(0x1b0) CASEMBC(0x1d4)
1105 CASEMBC(0x1ee7)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001106 regmbc('u'); regmbc('\371'); regmbc('\372');
1107 regmbc('\373'); regmbc('\374');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001108 REGMBC(0x169) REGMBC(0x16b) REGMBC(0x16d)
1109 REGMBC(0x16f) REGMBC(0x171) REGMBC(0x173)
1110 REGMBC(0x1b0) REGMBC(0x1d4) REGMBC(0x1ee7)
1111 return;
1112 case 'v': CASEMBC(0x1e7d)
1113 regmbc('v'); REGMBC(0x1e7d)
1114 return;
1115 case 'w': CASEMBC(0x175) CASEMBC(0x1e81) CASEMBC(0x1e83)
1116 CASEMBC(0x1e85) CASEMBC(0x1e87) CASEMBC(0x1e98)
1117 regmbc('w'); REGMBC(0x175) REGMBC(0x1e81)
1118 REGMBC(0x1e83) REGMBC(0x1e85) REGMBC(0x1e87)
1119 REGMBC(0x1e98)
1120 return;
1121 case 'x': CASEMBC(0x1e8b) CASEMBC(0x1e8d)
1122 regmbc('x'); REGMBC(0x1e8b) REGMBC(0x1e8d)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001123 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001124 case 'y': case '\375': case '\377':
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001125 CASEMBC(0x177) CASEMBC(0x1e8f) CASEMBC(0x1e99)
1126 CASEMBC(0x1ef3) CASEMBC(0x1ef7) CASEMBC(0x1ef9)
Bram Moolenaar402d2fe2005-04-15 21:00:38 +00001127 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02001128 REGMBC(0x177) REGMBC(0x1e8f) REGMBC(0x1e99)
1129 REGMBC(0x1ef3) REGMBC(0x1ef7) REGMBC(0x1ef9)
1130 return;
1131 case 'z': CASEMBC(0x17a) CASEMBC(0x17c) CASEMBC(0x17e)
1132 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95)
1133 regmbc('z'); REGMBC(0x17a) REGMBC(0x17c)
1134 REGMBC(0x17e) REGMBC(0x1b6) REGMBC(0x1e91)
1135 REGMBC(0x1e95)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001136 return;
1137 }
Bram Moolenaar2c704a72010-06-03 21:17:25 +02001138#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00001139 }
1140 regmbc(c);
1141}
1142
1143/*
1144 * Check for a collating element "[.a.]". "pp" points to the '['.
1145 * Returns a character. Zero means that no item was recognized. Otherwise
1146 * "pp" is advanced to after the item.
1147 * Currently only single characters are recognized!
1148 */
1149 static int
1150get_coll_element(pp)
1151 char_u **pp;
1152{
1153 int c;
1154 int l = 1;
1155 char_u *p = *pp;
1156
1157 if (p[1] == '.')
1158 {
1159#ifdef FEAT_MBYTE
1160 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001161 l = (*mb_ptr2len)(p + 2);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001162#endif
1163 if (p[l + 2] == '.' && p[l + 3] == ']')
1164 {
1165#ifdef FEAT_MBYTE
1166 if (has_mbyte)
1167 c = mb_ptr2char(p + 2);
1168 else
1169#endif
1170 c = p[2];
1171 *pp += l + 4;
1172 return c;
1173 }
1174 }
1175 return 0;
1176}
1177
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001178static void get_cpo_flags __ARGS((void));
1179static int reg_cpo_lit; /* 'cpoptions' contains 'l' flag */
1180static int reg_cpo_bsl; /* 'cpoptions' contains '\' flag */
1181
1182 static void
1183get_cpo_flags()
1184{
1185 reg_cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL;
1186 reg_cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL;
1187}
Bram Moolenaardf177f62005-02-22 08:39:57 +00001188
1189/*
1190 * Skip over a "[]" range.
1191 * "p" must point to the character after the '['.
1192 * The returned pointer is on the matching ']', or the terminating NUL.
1193 */
1194 static char_u *
1195skip_anyof(p)
1196 char_u *p;
1197{
Bram Moolenaardf177f62005-02-22 08:39:57 +00001198#ifdef FEAT_MBYTE
1199 int l;
1200#endif
1201
Bram Moolenaardf177f62005-02-22 08:39:57 +00001202 if (*p == '^') /* Complement of range. */
1203 ++p;
1204 if (*p == ']' || *p == '-')
1205 ++p;
1206 while (*p != NUL && *p != ']')
1207 {
1208#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001209 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaardf177f62005-02-22 08:39:57 +00001210 p += l;
1211 else
1212#endif
1213 if (*p == '-')
1214 {
1215 ++p;
1216 if (*p != ']' && *p != NUL)
1217 mb_ptr_adv(p);
1218 }
1219 else if (*p == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001220 && !reg_cpo_bsl
Bram Moolenaardf177f62005-02-22 08:39:57 +00001221 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001222 || (!reg_cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
Bram Moolenaardf177f62005-02-22 08:39:57 +00001223 p += 2;
1224 else if (*p == '[')
1225 {
1226 if (get_char_class(&p) == CLASS_NONE
1227 && get_equi_class(&p) == 0
1228 && get_coll_element(&p) == 0)
1229 ++p; /* It was not a class name */
1230 }
1231 else
1232 ++p;
1233 }
1234
1235 return p;
1236}
1237
1238/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +00001240 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 * Take care of characters with a backslash in front of it.
1242 * Skip strings inside [ and ].
1243 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
1244 * expression and change "\?" to "?". If "*newp" is not NULL the expression
1245 * is changed in-place.
1246 */
1247 char_u *
1248skip_regexp(startp, dirc, magic, newp)
1249 char_u *startp;
1250 int dirc;
1251 int magic;
1252 char_u **newp;
1253{
1254 int mymagic;
1255 char_u *p = startp;
1256
1257 if (magic)
1258 mymagic = MAGIC_ON;
1259 else
1260 mymagic = MAGIC_OFF;
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001261 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00001263 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 {
1265 if (p[0] == dirc) /* found end of regexp */
1266 break;
1267 if ((p[0] == '[' && mymagic >= MAGIC_ON)
1268 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
1269 {
1270 p = skip_anyof(p + 1);
1271 if (p[0] == NUL)
1272 break;
1273 }
1274 else if (p[0] == '\\' && p[1] != NUL)
1275 {
1276 if (dirc == '?' && newp != NULL && p[1] == '?')
1277 {
1278 /* change "\?" to "?", make a copy first. */
1279 if (*newp == NULL)
1280 {
1281 *newp = vim_strsave(startp);
1282 if (*newp != NULL)
1283 p = *newp + (p - startp);
1284 }
1285 if (*newp != NULL)
Bram Moolenaar446cb832008-06-24 21:56:24 +00001286 STRMOVE(p, p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 else
1288 ++p;
1289 }
1290 else
1291 ++p; /* skip next character */
1292 if (*p == 'v')
1293 mymagic = MAGIC_ALL;
1294 else if (*p == 'V')
1295 mymagic = MAGIC_NONE;
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298 return p;
1299}
1300
Bram Moolenaar473de612013-06-08 18:19:48 +02001301static regprog_T *bt_regcomp __ARGS((char_u *expr, int re_flags));
1302static void bt_regfree __ARGS((regprog_T *prog));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001303
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001305 * bt_regcomp() - compile a regular expression into internal code for the
1306 * traditional back track matcher.
Bram Moolenaar86b68352004-12-27 21:59:20 +00001307 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 *
1309 * We can't allocate space until we know how big the compiled form will be,
1310 * but we can't compile it (and thus know how big it is) until we've got a
1311 * place to put the code. So we cheat: we compile it twice, once with code
1312 * generation turned off and size counting turned on, and once "for real".
1313 * This also means that we don't allocate space until we are sure that the
1314 * thing really will compile successfully, and we never have to move the
1315 * code and thus invalidate pointers into it. (Note that it has to be in
1316 * one piece because vim_free() must be able to free it all.)
1317 *
1318 * Whether upper/lower case is to be ignored is decided when executing the
1319 * program, it does not matter here.
1320 *
1321 * Beware that the optimization-preparation code in here knows about some
1322 * of the structure of the compiled regexp.
1323 * "re_flags": RE_MAGIC and/or RE_STRING.
1324 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001325 static regprog_T *
1326bt_regcomp(expr, re_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 char_u *expr;
1328 int re_flags;
1329{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001330 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001331 char_u *scan;
1332 char_u *longest;
1333 int len;
1334 int flags;
1335
1336 if (expr == NULL)
1337 EMSG_RET_NULL(_(e_null));
1338
1339 init_class_tab();
1340
1341 /*
1342 * First pass: determine size, legality.
1343 */
1344 regcomp_start(expr, re_flags);
1345 regcode = JUST_CALC_SIZE;
1346 regc(REGMAGIC);
1347 if (reg(REG_NOPAREN, &flags) == NULL)
1348 return NULL;
1349
1350 /* Small enough for pointer-storage convention? */
1351#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1352 if (regsize >= 65536L - 256L)
1353 EMSG_RET_NULL(_("E339: Pattern too long"));
1354#endif
1355
1356 /* Allocate space. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001357 r = (bt_regprog_T *)lalloc(sizeof(bt_regprog_T) + regsize, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (r == NULL)
1359 return NULL;
1360
1361 /*
1362 * Second pass: emit code.
1363 */
1364 regcomp_start(expr, re_flags);
1365 regcode = r->program;
1366 regc(REGMAGIC);
Bram Moolenaard3005802009-11-25 17:21:32 +00001367 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001368 {
1369 vim_free(r);
Bram Moolenaard3005802009-11-25 17:21:32 +00001370 if (reg_toolong)
1371 EMSG_RET_NULL(_("E339: Pattern too long"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 return NULL;
1373 }
1374
1375 /* Dig out information for optimizations. */
1376 r->regstart = NUL; /* Worst-case defaults. */
1377 r->reganch = 0;
1378 r->regmust = NULL;
1379 r->regmlen = 0;
1380 r->regflags = regflags;
1381 if (flags & HASNL)
1382 r->regflags |= RF_HASNL;
1383 if (flags & HASLOOKBH)
1384 r->regflags |= RF_LOOKBH;
1385#ifdef FEAT_SYN_HL
1386 /* Remember whether this pattern has any \z specials in it. */
1387 r->reghasz = re_has_z;
1388#endif
1389 scan = r->program + 1; /* First BRANCH. */
1390 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1391 {
1392 scan = OPERAND(scan);
1393
1394 /* Starting-point info. */
1395 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1396 {
1397 r->reganch++;
1398 scan = regnext(scan);
1399 }
1400
1401 if (OP(scan) == EXACTLY)
1402 {
1403#ifdef FEAT_MBYTE
1404 if (has_mbyte)
1405 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1406 else
1407#endif
1408 r->regstart = *OPERAND(scan);
1409 }
1410 else if ((OP(scan) == BOW
1411 || OP(scan) == EOW
1412 || OP(scan) == NOTHING
1413 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1414 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1415 && OP(regnext(scan)) == EXACTLY)
1416 {
1417#ifdef FEAT_MBYTE
1418 if (has_mbyte)
1419 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1420 else
1421#endif
1422 r->regstart = *OPERAND(regnext(scan));
1423 }
1424
1425 /*
1426 * If there's something expensive in the r.e., find the longest
1427 * literal string that must appear and make it the regmust. Resolve
1428 * ties in favor of later strings, since the regstart check works
1429 * with the beginning of the r.e. and avoiding duplication
1430 * strengthens checking. Not a strong reason, but sufficient in the
1431 * absence of others.
1432 */
1433 /*
1434 * When the r.e. starts with BOW, it is faster to look for a regmust
1435 * first. Used a lot for "#" and "*" commands. (Added by mool).
1436 */
1437 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1438 && !(flags & HASNL))
1439 {
1440 longest = NULL;
1441 len = 0;
1442 for (; scan != NULL; scan = regnext(scan))
1443 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1444 {
1445 longest = OPERAND(scan);
1446 len = (int)STRLEN(OPERAND(scan));
1447 }
1448 r->regmust = longest;
1449 r->regmlen = len;
1450 }
1451 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001452#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 regdump(expr, r);
1454#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001455 r->engine = &bt_regengine;
1456 return (regprog_T *)r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457}
1458
1459/*
Bram Moolenaar473de612013-06-08 18:19:48 +02001460 * Free a compiled regexp program, returned by bt_regcomp().
1461 */
1462 static void
1463bt_regfree(prog)
1464 regprog_T *prog;
1465{
1466 vim_free(prog);
1467}
1468
1469/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 * Setup to parse the regexp. Used once to get the length and once to do it.
1471 */
1472 static void
1473regcomp_start(expr, re_flags)
1474 char_u *expr;
1475 int re_flags; /* see vim_regcomp() */
1476{
1477 initchr(expr);
1478 if (re_flags & RE_MAGIC)
1479 reg_magic = MAGIC_ON;
1480 else
1481 reg_magic = MAGIC_OFF;
1482 reg_string = (re_flags & RE_STRING);
Bram Moolenaarae5bce12005-08-15 21:41:48 +00001483 reg_strict = (re_flags & RE_STRICT);
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02001484 get_cpo_flags();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
1486 num_complex_braces = 0;
1487 regnpar = 1;
1488 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1489#ifdef FEAT_SYN_HL
1490 regnzpar = 1;
1491 re_has_z = 0;
1492#endif
1493 regsize = 0L;
Bram Moolenaard3005802009-11-25 17:21:32 +00001494 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 regflags = 0;
1496#if defined(FEAT_SYN_HL) || defined(PROTO)
1497 had_eol = FALSE;
1498#endif
1499}
1500
1501#if defined(FEAT_SYN_HL) || defined(PROTO)
1502/*
1503 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1504 * found. This is messy, but it works fine.
1505 */
1506 int
1507vim_regcomp_had_eol()
1508{
1509 return had_eol;
1510}
1511#endif
1512
1513/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001514 * Parse regular expression, i.e. main body or parenthesized thing.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001515 *
1516 * Caller must absorb opening parenthesis.
1517 *
1518 * Combining parenthesis handling with the base level of regular expression
1519 * is a trifle forced, but the need to tie the tails of the branches to what
1520 * follows makes it hard to avoid.
1521 */
1522 static char_u *
1523reg(paren, flagp)
1524 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1525 int *flagp;
1526{
1527 char_u *ret;
1528 char_u *br;
1529 char_u *ender;
1530 int parno = 0;
1531 int flags;
1532
1533 *flagp = HASWIDTH; /* Tentatively. */
1534
1535#ifdef FEAT_SYN_HL
1536 if (paren == REG_ZPAREN)
1537 {
1538 /* Make a ZOPEN node. */
1539 if (regnzpar >= NSUBEXP)
1540 EMSG_RET_NULL(_("E50: Too many \\z("));
1541 parno = regnzpar;
1542 regnzpar++;
1543 ret = regnode(ZOPEN + parno);
1544 }
1545 else
1546#endif
1547 if (paren == REG_PAREN)
1548 {
1549 /* Make a MOPEN node. */
1550 if (regnpar >= NSUBEXP)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001551 EMSG2_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552 parno = regnpar;
1553 ++regnpar;
1554 ret = regnode(MOPEN + parno);
1555 }
1556 else if (paren == REG_NPAREN)
1557 {
1558 /* Make a NOPEN node. */
1559 ret = regnode(NOPEN);
1560 }
1561 else
1562 ret = NULL;
1563
1564 /* Pick up the branches, linking them together. */
1565 br = regbranch(&flags);
1566 if (br == NULL)
1567 return NULL;
1568 if (ret != NULL)
1569 regtail(ret, br); /* [MZ]OPEN -> first. */
1570 else
1571 ret = br;
1572 /* If one of the branches can be zero-width, the whole thing can.
1573 * If one of the branches has * at start or matches a line-break, the
1574 * whole thing can. */
1575 if (!(flags & HASWIDTH))
1576 *flagp &= ~HASWIDTH;
1577 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1578 while (peekchr() == Magic('|'))
1579 {
1580 skipchr();
1581 br = regbranch(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001582 if (br == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583 return NULL;
1584 regtail(ret, br); /* BRANCH -> BRANCH. */
1585 if (!(flags & HASWIDTH))
1586 *flagp &= ~HASWIDTH;
1587 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1588 }
1589
1590 /* Make a closing node, and hook it on the end. */
1591 ender = regnode(
1592#ifdef FEAT_SYN_HL
1593 paren == REG_ZPAREN ? ZCLOSE + parno :
1594#endif
1595 paren == REG_PAREN ? MCLOSE + parno :
1596 paren == REG_NPAREN ? NCLOSE : END);
1597 regtail(ret, ender);
1598
1599 /* Hook the tails of the branches to the closing node. */
1600 for (br = ret; br != NULL; br = regnext(br))
1601 regoptail(br, ender);
1602
1603 /* Check for proper termination. */
1604 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1605 {
1606#ifdef FEAT_SYN_HL
1607 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001608 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 else
1610#endif
1611 if (paren == REG_NPAREN)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001612 EMSG2_RET_NULL(_(e_unmatchedpp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001614 EMSG2_RET_NULL(_(e_unmatchedp), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615 }
1616 else if (paren == REG_NOPAREN && peekchr() != NUL)
1617 {
1618 if (curchr == Magic(')'))
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001619 EMSG2_RET_NULL(_(e_unmatchedpar), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001620 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001621 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 /* NOTREACHED */
1623 }
1624 /*
1625 * Here we set the flag allowing back references to this set of
1626 * parentheses.
1627 */
1628 if (paren == REG_PAREN)
1629 had_endbrace[parno] = TRUE; /* have seen the close paren */
1630 return ret;
1631}
1632
1633/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001634 * Parse one alternative of an | operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 * Implements the & operator.
1636 */
1637 static char_u *
1638regbranch(flagp)
1639 int *flagp;
1640{
1641 char_u *ret;
1642 char_u *chain = NULL;
1643 char_u *latest;
1644 int flags;
1645
1646 *flagp = WORST | HASNL; /* Tentatively. */
1647
1648 ret = regnode(BRANCH);
1649 for (;;)
1650 {
1651 latest = regconcat(&flags);
1652 if (latest == NULL)
1653 return NULL;
1654 /* If one of the branches has width, the whole thing has. If one of
1655 * the branches anchors at start-of-line, the whole thing does.
1656 * If one of the branches uses look-behind, the whole thing does. */
1657 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1658 /* If one of the branches doesn't match a line-break, the whole thing
1659 * doesn't. */
1660 *flagp &= ~HASNL | (flags & HASNL);
1661 if (chain != NULL)
1662 regtail(chain, latest);
1663 if (peekchr() != Magic('&'))
1664 break;
1665 skipchr();
1666 regtail(latest, regnode(END)); /* operand ends */
Bram Moolenaard3005802009-11-25 17:21:32 +00001667 if (reg_toolong)
1668 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 reginsert(MATCH, latest);
1670 chain = latest;
1671 }
1672
1673 return ret;
1674}
1675
1676/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001677 * Parse one alternative of an | or & operator.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 * Implements the concatenation operator.
1679 */
1680 static char_u *
1681regconcat(flagp)
1682 int *flagp;
1683{
1684 char_u *first = NULL;
1685 char_u *chain = NULL;
1686 char_u *latest;
1687 int flags;
1688 int cont = TRUE;
1689
1690 *flagp = WORST; /* Tentatively. */
1691
1692 while (cont)
1693 {
1694 switch (peekchr())
1695 {
1696 case NUL:
1697 case Magic('|'):
1698 case Magic('&'):
1699 case Magic(')'):
1700 cont = FALSE;
1701 break;
1702 case Magic('Z'):
1703#ifdef FEAT_MBYTE
1704 regflags |= RF_ICOMBINE;
1705#endif
1706 skipchr_keepstart();
1707 break;
1708 case Magic('c'):
1709 regflags |= RF_ICASE;
1710 skipchr_keepstart();
1711 break;
1712 case Magic('C'):
1713 regflags |= RF_NOICASE;
1714 skipchr_keepstart();
1715 break;
1716 case Magic('v'):
1717 reg_magic = MAGIC_ALL;
1718 skipchr_keepstart();
1719 curchr = -1;
1720 break;
1721 case Magic('m'):
1722 reg_magic = MAGIC_ON;
1723 skipchr_keepstart();
1724 curchr = -1;
1725 break;
1726 case Magic('M'):
1727 reg_magic = MAGIC_OFF;
1728 skipchr_keepstart();
1729 curchr = -1;
1730 break;
1731 case Magic('V'):
1732 reg_magic = MAGIC_NONE;
1733 skipchr_keepstart();
1734 curchr = -1;
1735 break;
1736 default:
1737 latest = regpiece(&flags);
Bram Moolenaard3005802009-11-25 17:21:32 +00001738 if (latest == NULL || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 return NULL;
1740 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1741 if (chain == NULL) /* First piece. */
1742 *flagp |= flags & SPSTART;
1743 else
1744 regtail(chain, latest);
1745 chain = latest;
1746 if (first == NULL)
1747 first = latest;
1748 break;
1749 }
1750 }
1751 if (first == NULL) /* Loop ran zero times. */
1752 first = regnode(NOTHING);
1753 return first;
1754}
1755
1756/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001757 * Parse something followed by possible [*+=].
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 *
1759 * Note that the branching code sequences used for = and the general cases
1760 * of * and + are somewhat optimized: they use the same NOTHING node as
1761 * both the endmarker for their branch list and the body of the last branch.
1762 * It might seem that this node could be dispensed with entirely, but the
1763 * endmarker role is not redundant.
1764 */
1765 static char_u *
1766regpiece(flagp)
1767 int *flagp;
1768{
1769 char_u *ret;
1770 int op;
1771 char_u *next;
1772 int flags;
1773 long minval;
1774 long maxval;
1775
1776 ret = regatom(&flags);
1777 if (ret == NULL)
1778 return NULL;
1779
1780 op = peekchr();
1781 if (re_multi_type(op) == NOT_MULTI)
1782 {
1783 *flagp = flags;
1784 return ret;
1785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 /* default flags */
1787 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1788
1789 skipchr();
1790 switch (op)
1791 {
1792 case Magic('*'):
1793 if (flags & SIMPLE)
1794 reginsert(STAR, ret);
1795 else
1796 {
1797 /* Emit x* as (x&|), where & means "self". */
1798 reginsert(BRANCH, ret); /* Either x */
1799 regoptail(ret, regnode(BACK)); /* and loop */
1800 regoptail(ret, ret); /* back */
1801 regtail(ret, regnode(BRANCH)); /* or */
1802 regtail(ret, regnode(NOTHING)); /* null. */
1803 }
1804 break;
1805
1806 case Magic('+'):
1807 if (flags & SIMPLE)
1808 reginsert(PLUS, ret);
1809 else
1810 {
1811 /* Emit x+ as x(&|), where & means "self". */
1812 next = regnode(BRANCH); /* Either */
1813 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001814 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 regtail(next, regnode(BRANCH)); /* or */
1816 regtail(ret, regnode(NOTHING)); /* null. */
1817 }
1818 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1819 break;
1820
1821 case Magic('@'):
1822 {
1823 int lop = END;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001824 int nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001826 nr = getdecchrs();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 switch (no_Magic(getchr()))
1828 {
1829 case '=': lop = MATCH; break; /* \@= */
1830 case '!': lop = NOMATCH; break; /* \@! */
1831 case '>': lop = SUBPAT; break; /* \@> */
1832 case '<': switch (no_Magic(getchr()))
1833 {
1834 case '=': lop = BEHIND; break; /* \@<= */
1835 case '!': lop = NOBEHIND; break; /* \@<! */
1836 }
1837 }
1838 if (lop == END)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001839 EMSG2_RET_NULL(_("E59: invalid character after %s@"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 reg_magic == MAGIC_ALL);
1841 /* Look behind must match with behind_pos. */
1842 if (lop == BEHIND || lop == NOBEHIND)
1843 {
1844 regtail(ret, regnode(BHPOS));
1845 *flagp |= HASLOOKBH;
1846 }
1847 regtail(ret, regnode(END)); /* operand ends */
Bram Moolenaar75eb1612013-05-29 18:45:11 +02001848 if (lop == BEHIND || lop == NOBEHIND)
1849 {
1850 if (nr < 0)
1851 nr = 0; /* no limit is same as zero limit */
1852 reginsert_nr(lop, nr, ret);
1853 }
1854 else
1855 reginsert(lop, ret);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 break;
1857 }
1858
1859 case Magic('?'):
1860 case Magic('='):
1861 /* Emit x= as (x|) */
1862 reginsert(BRANCH, ret); /* Either x */
1863 regtail(ret, regnode(BRANCH)); /* or */
1864 next = regnode(NOTHING); /* null. */
1865 regtail(ret, next);
1866 regoptail(ret, next);
1867 break;
1868
1869 case Magic('{'):
1870 if (!read_limits(&minval, &maxval))
1871 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (flags & SIMPLE)
1873 {
1874 reginsert(BRACE_SIMPLE, ret);
1875 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1876 }
1877 else
1878 {
1879 if (num_complex_braces >= 10)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001880 EMSG2_RET_NULL(_("E60: Too many complex %s{...}s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 reg_magic == MAGIC_ALL);
1882 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1883 regoptail(ret, regnode(BACK));
1884 regoptail(ret, ret);
1885 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1886 ++num_complex_braces;
1887 }
1888 if (minval > 0 && maxval > 0)
1889 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1890 break;
1891 }
1892 if (re_multi_type(peekchr()) != NOT_MULTI)
1893 {
1894 /* Can't have a multi follow a multi. */
1895 if (peekchr() == Magic('*'))
1896 sprintf((char *)IObuff, _("E61: Nested %s*"),
1897 reg_magic >= MAGIC_ON ? "" : "\\");
1898 else
1899 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1900 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1901 EMSG_RET_NULL(IObuff);
1902 }
1903
1904 return ret;
1905}
1906
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001907/* When making changes to classchars also change nfa_classcodes. */
1908static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1909static int classcodes[] = {
1910 ANY, IDENT, SIDENT, KWORD, SKWORD,
1911 FNAME, SFNAME, PRINT, SPRINT,
1912 WHITE, NWHITE, DIGIT, NDIGIT,
1913 HEX, NHEX, OCTAL, NOCTAL,
1914 WORD, NWORD, HEAD, NHEAD,
1915 ALPHA, NALPHA, LOWER, NLOWER,
1916 UPPER, NUPPER
1917};
1918
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02001920 * Parse the lowest level.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 *
1922 * Optimization: gobbles an entire sequence of ordinary characters so that
1923 * it can turn them into a single node, which is smaller to store and
1924 * faster to run. Don't do this when one_exactly is set.
1925 */
1926 static char_u *
1927regatom(flagp)
1928 int *flagp;
1929{
1930 char_u *ret;
1931 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 char_u *p;
1934 int extra = 0;
1935
1936 *flagp = WORST; /* Tentatively. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937
1938 c = getchr();
1939 switch (c)
1940 {
1941 case Magic('^'):
1942 ret = regnode(BOL);
1943 break;
1944
1945 case Magic('$'):
1946 ret = regnode(EOL);
1947#if defined(FEAT_SYN_HL) || defined(PROTO)
1948 had_eol = TRUE;
1949#endif
1950 break;
1951
1952 case Magic('<'):
1953 ret = regnode(BOW);
1954 break;
1955
1956 case Magic('>'):
1957 ret = regnode(EOW);
1958 break;
1959
1960 case Magic('_'):
1961 c = no_Magic(getchr());
1962 if (c == '^') /* "\_^" is start-of-line */
1963 {
1964 ret = regnode(BOL);
1965 break;
1966 }
1967 if (c == '$') /* "\_$" is end-of-line */
1968 {
1969 ret = regnode(EOL);
1970#if defined(FEAT_SYN_HL) || defined(PROTO)
1971 had_eol = TRUE;
1972#endif
1973 break;
1974 }
1975
1976 extra = ADD_NL;
1977 *flagp |= HASNL;
1978
1979 /* "\_[" is character range plus newline */
1980 if (c == '[')
1981 goto collection;
1982
1983 /* "\_x" is character class plus newline */
1984 /*FALLTHROUGH*/
1985
1986 /*
1987 * Character classes.
1988 */
1989 case Magic('.'):
1990 case Magic('i'):
1991 case Magic('I'):
1992 case Magic('k'):
1993 case Magic('K'):
1994 case Magic('f'):
1995 case Magic('F'):
1996 case Magic('p'):
1997 case Magic('P'):
1998 case Magic('s'):
1999 case Magic('S'):
2000 case Magic('d'):
2001 case Magic('D'):
2002 case Magic('x'):
2003 case Magic('X'):
2004 case Magic('o'):
2005 case Magic('O'):
2006 case Magic('w'):
2007 case Magic('W'):
2008 case Magic('h'):
2009 case Magic('H'):
2010 case Magic('a'):
2011 case Magic('A'):
2012 case Magic('l'):
2013 case Magic('L'):
2014 case Magic('u'):
2015 case Magic('U'):
2016 p = vim_strchr(classchars, no_Magic(c));
2017 if (p == NULL)
2018 EMSG_RET_NULL(_("E63: invalid use of \\_"));
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002019#ifdef FEAT_MBYTE
2020 /* When '.' is followed by a composing char ignore the dot, so that
2021 * the composing char is matched here. */
2022 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
2023 {
2024 c = getchr();
2025 goto do_multibyte;
2026 }
2027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 ret = regnode(classcodes[p - classchars] + extra);
2029 *flagp |= HASWIDTH | SIMPLE;
2030 break;
2031
2032 case Magic('n'):
2033 if (reg_string)
2034 {
2035 /* In a string "\n" matches a newline character. */
2036 ret = regnode(EXACTLY);
2037 regc(NL);
2038 regc(NUL);
2039 *flagp |= HASWIDTH | SIMPLE;
2040 }
2041 else
2042 {
2043 /* In buffer text "\n" matches the end of a line. */
2044 ret = regnode(NEWL);
2045 *flagp |= HASWIDTH | HASNL;
2046 }
2047 break;
2048
2049 case Magic('('):
2050 if (one_exactly)
2051 EMSG_ONE_RET_NULL;
2052 ret = reg(REG_PAREN, &flags);
2053 if (ret == NULL)
2054 return NULL;
2055 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2056 break;
2057
2058 case NUL:
2059 case Magic('|'):
2060 case Magic('&'):
2061 case Magic(')'):
Bram Moolenaard4210772008-01-02 14:35:30 +00002062 if (one_exactly)
2063 EMSG_ONE_RET_NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
2065 /* NOTREACHED */
2066
2067 case Magic('='):
2068 case Magic('?'):
2069 case Magic('+'):
2070 case Magic('@'):
2071 case Magic('{'):
2072 case Magic('*'):
2073 c = no_Magic(c);
2074 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
2075 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
2076 ? "" : "\\", c);
2077 EMSG_RET_NULL(IObuff);
2078 /* NOTREACHED */
2079
2080 case Magic('~'): /* previous substitute pattern */
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00002081 if (reg_prev_sub != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
2083 char_u *lp;
2084
2085 ret = regnode(EXACTLY);
2086 lp = reg_prev_sub;
2087 while (*lp != NUL)
2088 regc(*lp++);
2089 regc(NUL);
2090 if (*reg_prev_sub != NUL)
2091 {
2092 *flagp |= HASWIDTH;
2093 if ((lp - reg_prev_sub) == 1)
2094 *flagp |= SIMPLE;
2095 }
2096 }
2097 else
2098 EMSG_RET_NULL(_(e_nopresub));
2099 break;
2100
2101 case Magic('1'):
2102 case Magic('2'):
2103 case Magic('3'):
2104 case Magic('4'):
2105 case Magic('5'):
2106 case Magic('6'):
2107 case Magic('7'):
2108 case Magic('8'):
2109 case Magic('9'):
2110 {
2111 int refnum;
2112
2113 refnum = c - Magic('0');
2114 /*
2115 * Check if the back reference is legal. We must have seen the
2116 * close brace.
2117 * TODO: Should also check that we don't refer to something
2118 * that is repeated (+*=): what instance of the repetition
2119 * should we match?
2120 */
2121 if (!had_endbrace[refnum])
2122 {
2123 /* Trick: check if "@<=" or "@<!" follows, in which case
2124 * the \1 can appear before the referenced match. */
2125 for (p = regparse; *p != NUL; ++p)
2126 if (p[0] == '@' && p[1] == '<'
2127 && (p[2] == '!' || p[2] == '='))
2128 break;
2129 if (*p == NUL)
2130 EMSG_RET_NULL(_("E65: Illegal back reference"));
2131 }
2132 ret = regnode(BACKREF + refnum);
2133 }
2134 break;
2135
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 case Magic('z'):
2137 {
2138 c = no_Magic(getchr());
2139 switch (c)
2140 {
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002141#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 case '(': if (reg_do_extmatch != REX_SET)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002143 EMSG_RET_NULL(_(e_z_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002144 if (one_exactly)
2145 EMSG_ONE_RET_NULL;
2146 ret = reg(REG_ZPAREN, &flags);
2147 if (ret == NULL)
2148 return NULL;
2149 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
2150 re_has_z = REX_SET;
2151 break;
2152
2153 case '1':
2154 case '2':
2155 case '3':
2156 case '4':
2157 case '5':
2158 case '6':
2159 case '7':
2160 case '8':
2161 case '9': if (reg_do_extmatch != REX_USE)
Bram Moolenaar5de820b2013-06-02 15:01:57 +02002162 EMSG_RET_NULL(_(e_z1_not_allowed));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 ret = regnode(ZREF + c - '0');
2164 re_has_z = REX_USE;
2165 break;
Bram Moolenaarc4956c82006-03-12 21:58:43 +00002166#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167
2168 case 's': ret = regnode(MOPEN + 0);
2169 break;
2170
2171 case 'e': ret = regnode(MCLOSE + 0);
2172 break;
2173
2174 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
2175 }
2176 }
2177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178
2179 case Magic('%'):
2180 {
2181 c = no_Magic(getchr());
2182 switch (c)
2183 {
2184 /* () without a back reference */
2185 case '(':
2186 if (one_exactly)
2187 EMSG_ONE_RET_NULL;
2188 ret = reg(REG_NPAREN, &flags);
2189 if (ret == NULL)
2190 return NULL;
2191 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
2192 break;
2193
2194 /* Catch \%^ and \%$ regardless of where they appear in the
2195 * pattern -- regardless of whether or not it makes sense. */
2196 case '^':
2197 ret = regnode(RE_BOF);
2198 break;
2199
2200 case '$':
2201 ret = regnode(RE_EOF);
2202 break;
2203
2204 case '#':
2205 ret = regnode(CURSOR);
2206 break;
2207
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002208 case 'V':
2209 ret = regnode(RE_VISUAL);
2210 break;
2211
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02002212 case 'C':
2213 ret = regnode(RE_COMPOSING);
2214 break;
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 /* \%[abc]: Emit as a list of branches, all ending at the last
2217 * branch which matches nothing. */
2218 case '[':
2219 if (one_exactly) /* doesn't nest */
2220 EMSG_ONE_RET_NULL;
2221 {
2222 char_u *lastbranch;
2223 char_u *lastnode = NULL;
2224 char_u *br;
2225
2226 ret = NULL;
2227 while ((c = getchr()) != ']')
2228 {
2229 if (c == NUL)
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002230 EMSG2_RET_NULL(_(e_missing_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 reg_magic == MAGIC_ALL);
2232 br = regnode(BRANCH);
2233 if (ret == NULL)
2234 ret = br;
2235 else
2236 regtail(lastnode, br);
2237
2238 ungetchr();
2239 one_exactly = TRUE;
2240 lastnode = regatom(flagp);
2241 one_exactly = FALSE;
2242 if (lastnode == NULL)
2243 return NULL;
2244 }
2245 if (ret == NULL)
Bram Moolenaar2976c022013-06-05 21:30:37 +02002246 EMSG2_RET_NULL(_(e_empty_sb),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 reg_magic == MAGIC_ALL);
2248 lastbranch = regnode(BRANCH);
2249 br = regnode(NOTHING);
2250 if (ret != JUST_CALC_SIZE)
2251 {
2252 regtail(lastnode, br);
2253 regtail(lastbranch, br);
2254 /* connect all branches to the NOTHING
2255 * branch at the end */
2256 for (br = ret; br != lastnode; )
2257 {
2258 if (OP(br) == BRANCH)
2259 {
2260 regtail(br, lastbranch);
2261 br = OPERAND(br);
2262 }
2263 else
2264 br = regnext(br);
2265 }
2266 }
Bram Moolenaara6404a42008-08-08 11:45:39 +00002267 *flagp &= ~(HASWIDTH | SIMPLE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 break;
2269 }
2270
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002271 case 'd': /* %d123 decimal */
2272 case 'o': /* %o123 octal */
2273 case 'x': /* %xab hex 2 */
2274 case 'u': /* %uabcd hex 4 */
2275 case 'U': /* %U1234abcd hex 8 */
2276 {
2277 int i;
2278
2279 switch (c)
2280 {
2281 case 'd': i = getdecchrs(); break;
2282 case 'o': i = getoctchrs(); break;
2283 case 'x': i = gethexchrs(2); break;
2284 case 'u': i = gethexchrs(4); break;
2285 case 'U': i = gethexchrs(8); break;
2286 default: i = -1; break;
2287 }
2288
2289 if (i < 0)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002290 EMSG2_RET_NULL(
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002291 _("E678: Invalid character after %s%%[dxouU]"),
2292 reg_magic == MAGIC_ALL);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002293#ifdef FEAT_MBYTE
2294 if (use_multibytecode(i))
2295 ret = regnode(MULTIBYTECODE);
2296 else
2297#endif
2298 ret = regnode(EXACTLY);
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002299 if (i == 0)
2300 regc(0x0a);
2301 else
2302#ifdef FEAT_MBYTE
2303 regmbc(i);
2304#else
2305 regc(i);
2306#endif
2307 regc(NUL);
2308 *flagp |= HASWIDTH;
2309 break;
2310 }
2311
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 default:
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002313 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
2314 || c == '\'')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 {
2316 long_u n = 0;
2317 int cmp;
2318
2319 cmp = c;
2320 if (cmp == '<' || cmp == '>')
2321 c = getchr();
2322 while (VIM_ISDIGIT(c))
2323 {
2324 n = n * 10 + (c - '0');
2325 c = getchr();
2326 }
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00002327 if (c == '\'' && n == 0)
2328 {
2329 /* "\%'m", "\%<'m" and "\%>'m": Mark */
2330 c = getchr();
2331 ret = regnode(RE_MARK);
2332 if (ret == JUST_CALC_SIZE)
2333 regsize += 2;
2334 else
2335 {
2336 *regcode++ = c;
2337 *regcode++ = cmp;
2338 }
2339 break;
2340 }
2341 else if (c == 'l' || c == 'c' || c == 'v')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 {
2343 if (c == 'l')
2344 ret = regnode(RE_LNUM);
2345 else if (c == 'c')
2346 ret = regnode(RE_COL);
2347 else
2348 ret = regnode(RE_VCOL);
2349 if (ret == JUST_CALC_SIZE)
2350 regsize += 5;
2351 else
2352 {
2353 /* put the number and the optional
2354 * comparator after the opcode */
2355 regcode = re_put_long(regcode, n);
2356 *regcode++ = cmp;
2357 }
2358 break;
2359 }
2360 }
2361
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002362 EMSG2_RET_NULL(_("E71: Invalid character after %s%%"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 reg_magic == MAGIC_ALL);
2364 }
2365 }
2366 break;
2367
2368 case Magic('['):
2369collection:
2370 {
2371 char_u *lp;
2372
2373 /*
2374 * If there is no matching ']', we assume the '[' is a normal
2375 * character. This makes 'incsearch' and ":help [" work.
2376 */
2377 lp = skip_anyof(regparse);
2378 if (*lp == ']') /* there is a matching ']' */
2379 {
2380 int startc = -1; /* > 0 when next '-' is a range */
2381 int endc;
2382
2383 /*
2384 * In a character class, different parsing rules apply.
2385 * Not even \ is special anymore, nothing is.
2386 */
2387 if (*regparse == '^') /* Complement of range. */
2388 {
2389 ret = regnode(ANYBUT + extra);
2390 regparse++;
2391 }
2392 else
2393 ret = regnode(ANYOF + extra);
2394
2395 /* At the start ']' and '-' mean the literal character. */
2396 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00002397 {
2398 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00002400 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401
2402 while (*regparse != NUL && *regparse != ']')
2403 {
2404 if (*regparse == '-')
2405 {
2406 ++regparse;
2407 /* The '-' is not used for a range at the end and
2408 * after or before a '\n'. */
2409 if (*regparse == ']' || *regparse == NUL
2410 || startc == -1
2411 || (regparse[0] == '\\' && regparse[1] == 'n'))
2412 {
2413 regc('-');
2414 startc = '-'; /* [--x] is a range */
2415 }
2416 else
2417 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002418 /* Also accept "a-[.z.]" */
2419 endc = 0;
2420 if (*regparse == '[')
2421 endc = get_coll_element(&regparse);
2422 if (endc == 0)
2423 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002425 if (has_mbyte)
2426 endc = mb_ptr2char_adv(&regparse);
2427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002429 endc = *regparse++;
2430 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002431
2432 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002433 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002434 endc = coll_get_char();
2435
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 if (startc > endc)
2437 EMSG_RET_NULL(_(e_invrange));
2438#ifdef FEAT_MBYTE
2439 if (has_mbyte && ((*mb_char2len)(startc) > 1
2440 || (*mb_char2len)(endc) > 1))
2441 {
2442 /* Limit to a range of 256 chars */
2443 if (endc > startc + 256)
2444 EMSG_RET_NULL(_(e_invrange));
2445 while (++startc <= endc)
2446 regmbc(startc);
2447 }
2448 else
2449#endif
2450 {
2451#ifdef EBCDIC
2452 int alpha_only = FALSE;
2453
2454 /* for alphabetical range skip the gaps
2455 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2456 if (isalpha(startc) && isalpha(endc))
2457 alpha_only = TRUE;
2458#endif
2459 while (++startc <= endc)
2460#ifdef EBCDIC
2461 if (!alpha_only || isalpha(startc))
2462#endif
2463 regc(startc);
2464 }
2465 startc = -1;
2466 }
2467 }
2468 /*
2469 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2470 * accepts "\t", "\e", etc., but only when the 'l' flag in
2471 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002472 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 */
2474 else if (*regparse == '\\'
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002475 && !reg_cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
Bram Moolenaar1cd3f2c2013-06-05 12:43:09 +02002477 || (!reg_cpo_lit
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 && vim_strchr(REGEXP_ABBR,
2479 regparse[1]) != NULL)))
2480 {
2481 regparse++;
2482 if (*regparse == 'n')
2483 {
2484 /* '\n' in range: also match NL */
2485 if (ret != JUST_CALC_SIZE)
2486 {
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002487 /* Using \n inside [^] does not change what
2488 * matches. "[^\n]" is the same as ".". */
2489 if (*ret == ANYOF)
2490 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 *ret = ANYOF + ADD_NL;
Bram Moolenaare337e5f2013-01-30 18:21:51 +01002492 *flagp |= HASNL;
2493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 /* else: must have had a \n already */
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 regparse++;
2497 startc = -1;
2498 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002499 else if (*regparse == 'd'
2500 || *regparse == 'o'
2501 || *regparse == 'x'
2502 || *regparse == 'u'
2503 || *regparse == 'U')
2504 {
2505 startc = coll_get_char();
2506 if (startc == 0)
2507 regc(0x0a);
2508 else
2509#ifdef FEAT_MBYTE
2510 regmbc(startc);
2511#else
2512 regc(startc);
2513#endif
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 else
2516 {
2517 startc = backslash_trans(*regparse++);
2518 regc(startc);
2519 }
2520 }
2521 else if (*regparse == '[')
2522 {
2523 int c_class;
2524 int cu;
2525
Bram Moolenaardf177f62005-02-22 08:39:57 +00002526 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 startc = -1;
2528 /* Characters assumed to be 8 bits! */
2529 switch (c_class)
2530 {
2531 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002532 c_class = get_equi_class(&regparse);
2533 if (c_class != 0)
2534 {
2535 /* produce equivalence class */
2536 reg_equi_class(c_class);
2537 }
2538 else if ((c_class =
2539 get_coll_element(&regparse)) != 0)
2540 {
2541 /* produce a collating element */
2542 regmbc(c_class);
2543 }
2544 else
2545 {
2546 /* literal '[', allow [[-x] as a range */
2547 startc = *regparse++;
2548 regc(startc);
2549 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 break;
2551 case CLASS_ALNUM:
2552 for (cu = 1; cu <= 255; cu++)
2553 if (isalnum(cu))
2554 regc(cu);
2555 break;
2556 case CLASS_ALPHA:
2557 for (cu = 1; cu <= 255; cu++)
2558 if (isalpha(cu))
2559 regc(cu);
2560 break;
2561 case CLASS_BLANK:
2562 regc(' ');
2563 regc('\t');
2564 break;
2565 case CLASS_CNTRL:
2566 for (cu = 1; cu <= 255; cu++)
2567 if (iscntrl(cu))
2568 regc(cu);
2569 break;
2570 case CLASS_DIGIT:
2571 for (cu = 1; cu <= 255; cu++)
2572 if (VIM_ISDIGIT(cu))
2573 regc(cu);
2574 break;
2575 case CLASS_GRAPH:
2576 for (cu = 1; cu <= 255; cu++)
2577 if (isgraph(cu))
2578 regc(cu);
2579 break;
2580 case CLASS_LOWER:
2581 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002582 if (MB_ISLOWER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 regc(cu);
2584 break;
2585 case CLASS_PRINT:
2586 for (cu = 1; cu <= 255; cu++)
2587 if (vim_isprintc(cu))
2588 regc(cu);
2589 break;
2590 case CLASS_PUNCT:
2591 for (cu = 1; cu <= 255; cu++)
2592 if (ispunct(cu))
2593 regc(cu);
2594 break;
2595 case CLASS_SPACE:
2596 for (cu = 9; cu <= 13; cu++)
2597 regc(cu);
2598 regc(' ');
2599 break;
2600 case CLASS_UPPER:
2601 for (cu = 1; cu <= 255; cu++)
Bram Moolenaara245a5b2007-08-11 11:58:23 +00002602 if (MB_ISUPPER(cu))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 regc(cu);
2604 break;
2605 case CLASS_XDIGIT:
2606 for (cu = 1; cu <= 255; cu++)
2607 if (vim_isxdigit(cu))
2608 regc(cu);
2609 break;
2610 case CLASS_TAB:
2611 regc('\t');
2612 break;
2613 case CLASS_RETURN:
2614 regc('\r');
2615 break;
2616 case CLASS_BACKSPACE:
2617 regc('\b');
2618 break;
2619 case CLASS_ESCAPE:
2620 regc('\033');
2621 break;
2622 }
2623 }
2624 else
2625 {
2626#ifdef FEAT_MBYTE
2627 if (has_mbyte)
2628 {
2629 int len;
2630
2631 /* produce a multibyte character, including any
2632 * following composing characters */
2633 startc = mb_ptr2char(regparse);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002634 len = (*mb_ptr2len)(regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002635 if (enc_utf8 && utf_char2len(startc) != len)
2636 startc = -1; /* composing chars */
2637 while (--len >= 0)
2638 regc(*regparse++);
2639 }
2640 else
2641#endif
2642 {
2643 startc = *regparse++;
2644 regc(startc);
2645 }
2646 }
2647 }
2648 regc(NUL);
2649 prevchr_len = 1; /* last char was the ']' */
2650 if (*regparse != ']')
2651 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2652 skipchr(); /* let's be friends with the lexer again */
2653 *flagp |= HASWIDTH | SIMPLE;
2654 break;
2655 }
Bram Moolenaarae5bce12005-08-15 21:41:48 +00002656 else if (reg_strict)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002657 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659 /* FALLTHROUGH */
2660
2661 default:
2662 {
2663 int len;
2664
2665#ifdef FEAT_MBYTE
2666 /* A multi-byte character is handled as a separate atom if it's
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002667 * before a multi and when it's a composing char. */
2668 if (use_multibytecode(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002670do_multibyte:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 ret = regnode(MULTIBYTECODE);
2672 regmbc(c);
2673 *flagp |= HASWIDTH | SIMPLE;
2674 break;
2675 }
2676#endif
2677
2678 ret = regnode(EXACTLY);
2679
2680 /*
2681 * Append characters as long as:
2682 * - there is no following multi, we then need the character in
2683 * front of it as a single character operand
2684 * - not running into a Magic character
2685 * - "one_exactly" is not set
2686 * But always emit at least one character. Might be a Multi,
2687 * e.g., a "[" without matching "]".
2688 */
2689 for (len = 0; c != NUL && (len == 0
2690 || (re_multi_type(peekchr()) == NOT_MULTI
2691 && !one_exactly
2692 && !is_Magic(c))); ++len)
2693 {
2694 c = no_Magic(c);
2695#ifdef FEAT_MBYTE
2696 if (has_mbyte)
2697 {
2698 regmbc(c);
2699 if (enc_utf8)
2700 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 int l;
2702
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002703 /* Need to get composing character too. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 for (;;)
2705 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002706 l = utf_ptr2len(regparse);
2707 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 break;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002709 regmbc(utf_ptr2char(regparse));
2710 skipchr();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 }
2713 }
2714 else
2715#endif
2716 regc(c);
2717 c = getchr();
2718 }
2719 ungetchr();
2720
2721 regc(NUL);
2722 *flagp |= HASWIDTH;
2723 if (len == 1)
2724 *flagp |= SIMPLE;
2725 }
2726 break;
2727 }
2728
2729 return ret;
2730}
2731
Bram Moolenaar362e1a32006-03-06 23:29:24 +00002732#ifdef FEAT_MBYTE
2733/*
2734 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
2735 * character "c".
2736 */
2737 static int
2738use_multibytecode(c)
2739 int c;
2740{
2741 return has_mbyte && (*mb_char2len)(c) > 1
2742 && (re_multi_type(peekchr()) != NOT_MULTI
2743 || (enc_utf8 && utf_iscomposing(c)));
2744}
2745#endif
2746
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002748 * Emit a node.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002749 * Return pointer to generated code.
2750 */
2751 static char_u *
2752regnode(op)
2753 int op;
2754{
2755 char_u *ret;
2756
2757 ret = regcode;
2758 if (ret == JUST_CALC_SIZE)
2759 regsize += 3;
2760 else
2761 {
2762 *regcode++ = op;
2763 *regcode++ = NUL; /* Null "next" pointer. */
2764 *regcode++ = NUL;
2765 }
2766 return ret;
2767}
2768
2769/*
2770 * Emit (if appropriate) a byte of code
2771 */
2772 static void
2773regc(b)
2774 int b;
2775{
2776 if (regcode == JUST_CALC_SIZE)
2777 regsize++;
2778 else
2779 *regcode++ = b;
2780}
2781
2782#ifdef FEAT_MBYTE
2783/*
2784 * Emit (if appropriate) a multi-byte character of code
2785 */
2786 static void
2787regmbc(c)
2788 int c;
2789{
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002790 if (!has_mbyte && c > 0xff)
2791 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002792 if (regcode == JUST_CALC_SIZE)
2793 regsize += (*mb_char2len)(c);
2794 else
2795 regcode += (*mb_char2bytes)(c, regcode);
2796}
2797#endif
2798
2799/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002800 * Insert an operator in front of already-emitted operand
Bram Moolenaar071d4272004-06-13 20:20:40 +00002801 *
2802 * Means relocating the operand.
2803 */
2804 static void
2805reginsert(op, opnd)
2806 int op;
2807 char_u *opnd;
2808{
2809 char_u *src;
2810 char_u *dst;
2811 char_u *place;
2812
2813 if (regcode == JUST_CALC_SIZE)
2814 {
2815 regsize += 3;
2816 return;
2817 }
2818 src = regcode;
2819 regcode += 3;
2820 dst = regcode;
2821 while (src > opnd)
2822 *--dst = *--src;
2823
2824 place = opnd; /* Op node, where operand used to be. */
2825 *place++ = op;
2826 *place++ = NUL;
2827 *place = NUL;
2828}
2829
2830/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002831 * Insert an operator in front of already-emitted operand.
Bram Moolenaar75eb1612013-05-29 18:45:11 +02002832 * Add a number to the operator.
2833 */
2834 static void
2835reginsert_nr(op, val, opnd)
2836 int op;
2837 long val;
2838 char_u *opnd;
2839{
2840 char_u *src;
2841 char_u *dst;
2842 char_u *place;
2843
2844 if (regcode == JUST_CALC_SIZE)
2845 {
2846 regsize += 7;
2847 return;
2848 }
2849 src = regcode;
2850 regcode += 7;
2851 dst = regcode;
2852 while (src > opnd)
2853 *--dst = *--src;
2854
2855 place = opnd; /* Op node, where operand used to be. */
2856 *place++ = op;
2857 *place++ = NUL;
2858 *place++ = NUL;
2859 place = re_put_long(place, (long_u)val);
2860}
2861
2862/*
2863 * Insert an operator in front of already-emitted operand.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002864 * The operator has the given limit values as operands. Also set next pointer.
2865 *
2866 * Means relocating the operand.
2867 */
2868 static void
2869reginsert_limits(op, minval, maxval, opnd)
2870 int op;
2871 long minval;
2872 long maxval;
2873 char_u *opnd;
2874{
2875 char_u *src;
2876 char_u *dst;
2877 char_u *place;
2878
2879 if (regcode == JUST_CALC_SIZE)
2880 {
2881 regsize += 11;
2882 return;
2883 }
2884 src = regcode;
2885 regcode += 11;
2886 dst = regcode;
2887 while (src > opnd)
2888 *--dst = *--src;
2889
2890 place = opnd; /* Op node, where operand used to be. */
2891 *place++ = op;
2892 *place++ = NUL;
2893 *place++ = NUL;
2894 place = re_put_long(place, (long_u)minval);
2895 place = re_put_long(place, (long_u)maxval);
2896 regtail(opnd, place);
2897}
2898
2899/*
2900 * Write a long as four bytes at "p" and return pointer to the next char.
2901 */
2902 static char_u *
2903re_put_long(p, val)
2904 char_u *p;
2905 long_u val;
2906{
2907 *p++ = (char_u) ((val >> 24) & 0377);
2908 *p++ = (char_u) ((val >> 16) & 0377);
2909 *p++ = (char_u) ((val >> 8) & 0377);
2910 *p++ = (char_u) (val & 0377);
2911 return p;
2912}
2913
2914/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002915 * Set the next-pointer at the end of a node chain.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 */
2917 static void
2918regtail(p, val)
2919 char_u *p;
2920 char_u *val;
2921{
2922 char_u *scan;
2923 char_u *temp;
2924 int offset;
2925
2926 if (p == JUST_CALC_SIZE)
2927 return;
2928
2929 /* Find last node. */
2930 scan = p;
2931 for (;;)
2932 {
2933 temp = regnext(scan);
2934 if (temp == NULL)
2935 break;
2936 scan = temp;
2937 }
2938
Bram Moolenaar582fd852005-03-28 20:58:01 +00002939 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002940 offset = (int)(scan - val);
2941 else
2942 offset = (int)(val - scan);
Bram Moolenaard3005802009-11-25 17:21:32 +00002943 /* When the offset uses more than 16 bits it can no longer fit in the two
Bram Moolenaar522f9ae2011-07-20 17:58:20 +02002944 * bytes available. Use a global flag to avoid having to check return
Bram Moolenaard3005802009-11-25 17:21:32 +00002945 * values in too many places. */
2946 if (offset > 0xffff)
2947 reg_toolong = TRUE;
2948 else
2949 {
2950 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2951 *(scan + 2) = (char_u) (offset & 0377);
2952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953}
2954
2955/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002956 * Like regtail, on item after a BRANCH; nop if none.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002957 */
2958 static void
2959regoptail(p, val)
2960 char_u *p;
2961 char_u *val;
2962{
2963 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2964 if (p == NULL || p == JUST_CALC_SIZE
2965 || (OP(p) != BRANCH
2966 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2967 return;
2968 regtail(OPERAND(p), val);
2969}
2970
2971/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002972 * Functions for getting characters from the regexp input.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 */
2974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975static int at_start; /* True when on the first character */
2976static int prev_at_start; /* True when on the second character */
2977
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002978/*
2979 * Start parsing at "str".
2980 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002981 static void
2982initchr(str)
2983 char_u *str;
2984{
2985 regparse = str;
2986 prevchr_len = 0;
2987 curchr = prevprevchr = prevchr = nextchr = -1;
2988 at_start = TRUE;
2989 prev_at_start = FALSE;
2990}
2991
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02002992/*
Bram Moolenaar3737fc12013-06-01 14:42:56 +02002993 * Save the current parse state, so that it can be restored and parsing
2994 * starts in the same state again.
2995 */
2996 static void
2997save_parse_state(ps)
2998 parse_state_T *ps;
2999{
3000 ps->regparse = regparse;
3001 ps->prevchr_len = prevchr_len;
3002 ps->curchr = curchr;
3003 ps->prevchr = prevchr;
3004 ps->prevprevchr = prevprevchr;
3005 ps->nextchr = nextchr;
3006 ps->at_start = at_start;
3007 ps->prev_at_start = prev_at_start;
3008 ps->regnpar = regnpar;
3009}
3010
3011/*
3012 * Restore a previously saved parse state.
3013 */
3014 static void
3015restore_parse_state(ps)
3016 parse_state_T *ps;
3017{
3018 regparse = ps->regparse;
3019 prevchr_len = ps->prevchr_len;
3020 curchr = ps->curchr;
3021 prevchr = ps->prevchr;
3022 prevprevchr = ps->prevprevchr;
3023 nextchr = ps->nextchr;
3024 at_start = ps->at_start;
3025 prev_at_start = ps->prev_at_start;
3026 regnpar = ps->regnpar;
3027}
3028
3029
3030/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003031 * Get the next character without advancing.
3032 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 static int
3034peekchr()
3035{
Bram Moolenaardf177f62005-02-22 08:39:57 +00003036 static int after_slash = FALSE;
3037
Bram Moolenaar071d4272004-06-13 20:20:40 +00003038 if (curchr == -1)
3039 {
3040 switch (curchr = regparse[0])
3041 {
3042 case '.':
3043 case '[':
3044 case '~':
3045 /* magic when 'magic' is on */
3046 if (reg_magic >= MAGIC_ON)
3047 curchr = Magic(curchr);
3048 break;
3049 case '(':
3050 case ')':
3051 case '{':
3052 case '%':
3053 case '+':
3054 case '=':
3055 case '?':
3056 case '@':
3057 case '!':
3058 case '&':
3059 case '|':
3060 case '<':
3061 case '>':
3062 case '#': /* future ext. */
3063 case '"': /* future ext. */
3064 case '\'': /* future ext. */
3065 case ',': /* future ext. */
3066 case '-': /* future ext. */
3067 case ':': /* future ext. */
3068 case ';': /* future ext. */
3069 case '`': /* future ext. */
3070 case '/': /* Can't be used in / command */
3071 /* magic only after "\v" */
3072 if (reg_magic == MAGIC_ALL)
3073 curchr = Magic(curchr);
3074 break;
3075 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00003076 /* * is not magic as the very first character, eg "?*ptr", when
3077 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
3078 * "\(\*" is not magic, thus must be magic if "after_slash" */
3079 if (reg_magic >= MAGIC_ON
3080 && !at_start
3081 && !(prev_at_start && prevchr == Magic('^'))
3082 && (after_slash
3083 || (prevchr != Magic('(')
3084 && prevchr != Magic('&')
3085 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 curchr = Magic('*');
3087 break;
3088 case '^':
3089 /* '^' is only magic as the very first character and if it's after
3090 * "\(", "\|", "\&' or "\n" */
3091 if (reg_magic >= MAGIC_OFF
3092 && (at_start
3093 || reg_magic == MAGIC_ALL
3094 || prevchr == Magic('(')
3095 || prevchr == Magic('|')
3096 || prevchr == Magic('&')
3097 || prevchr == Magic('n')
3098 || (no_Magic(prevchr) == '('
3099 && prevprevchr == Magic('%'))))
3100 {
3101 curchr = Magic('^');
3102 at_start = TRUE;
3103 prev_at_start = FALSE;
3104 }
3105 break;
3106 case '$':
3107 /* '$' is only magic as the very last char and if it's in front of
3108 * either "\|", "\)", "\&", or "\n" */
3109 if (reg_magic >= MAGIC_OFF)
3110 {
3111 char_u *p = regparse + 1;
3112
3113 /* ignore \c \C \m and \M after '$' */
3114 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
3115 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
3116 p += 2;
3117 if (p[0] == NUL
3118 || (p[0] == '\\'
3119 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
3120 || p[1] == 'n'))
3121 || reg_magic == MAGIC_ALL)
3122 curchr = Magic('$');
3123 }
3124 break;
3125 case '\\':
3126 {
3127 int c = regparse[1];
3128
3129 if (c == NUL)
3130 curchr = '\\'; /* trailing '\' */
3131 else if (
3132#ifdef EBCDIC
3133 vim_strchr(META, c)
3134#else
3135 c <= '~' && META_flags[c]
3136#endif
3137 )
3138 {
3139 /*
3140 * META contains everything that may be magic sometimes,
3141 * except ^ and $ ("\^" and "\$" are only magic after
3142 * "\v"). We now fetch the next character and toggle its
3143 * magicness. Therefore, \ is so meta-magic that it is
3144 * not in META.
3145 */
3146 curchr = -1;
3147 prev_at_start = at_start;
3148 at_start = FALSE; /* be able to say "/\*ptr" */
3149 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003150 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151 peekchr();
3152 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00003153 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 curchr = toggle_Magic(curchr);
3155 }
3156 else if (vim_strchr(REGEXP_ABBR, c))
3157 {
3158 /*
3159 * Handle abbreviations, like "\t" for TAB -- webb
3160 */
3161 curchr = backslash_trans(c);
3162 }
3163 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
3164 curchr = toggle_Magic(c);
3165 else
3166 {
3167 /*
3168 * Next character can never be (made) magic?
3169 * Then backslashing it won't do anything.
3170 */
3171#ifdef FEAT_MBYTE
3172 if (has_mbyte)
3173 curchr = (*mb_ptr2char)(regparse + 1);
3174 else
3175#endif
3176 curchr = c;
3177 }
3178 break;
3179 }
3180
3181#ifdef FEAT_MBYTE
3182 default:
3183 if (has_mbyte)
3184 curchr = (*mb_ptr2char)(regparse);
3185#endif
3186 }
3187 }
3188
3189 return curchr;
3190}
3191
3192/*
3193 * Eat one lexed character. Do this in a way that we can undo it.
3194 */
3195 static void
3196skipchr()
3197{
3198 /* peekchr() eats a backslash, do the same here */
3199 if (*regparse == '\\')
3200 prevchr_len = 1;
3201 else
3202 prevchr_len = 0;
3203 if (regparse[prevchr_len] != NUL)
3204 {
3205#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003206 if (enc_utf8)
Bram Moolenaar8f5c5782007-11-29 20:27:21 +00003207 /* exclude composing chars that mb_ptr2len does include */
3208 prevchr_len += utf_ptr2len(regparse + prevchr_len);
Bram Moolenaar362e1a32006-03-06 23:29:24 +00003209 else if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003210 prevchr_len += (*mb_ptr2len)(regparse + prevchr_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003211 else
3212#endif
3213 ++prevchr_len;
3214 }
3215 regparse += prevchr_len;
3216 prev_at_start = at_start;
3217 at_start = FALSE;
3218 prevprevchr = prevchr;
3219 prevchr = curchr;
3220 curchr = nextchr; /* use previously unget char, or -1 */
3221 nextchr = -1;
3222}
3223
3224/*
3225 * Skip a character while keeping the value of prev_at_start for at_start.
3226 * prevchr and prevprevchr are also kept.
3227 */
3228 static void
3229skipchr_keepstart()
3230{
3231 int as = prev_at_start;
3232 int pr = prevchr;
3233 int prpr = prevprevchr;
3234
3235 skipchr();
3236 at_start = as;
3237 prevchr = pr;
3238 prevprevchr = prpr;
3239}
3240
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003241/*
3242 * Get the next character from the pattern. We know about magic and such, so
3243 * therefore we need a lexical analyzer.
3244 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 static int
3246getchr()
3247{
3248 int chr = peekchr();
3249
3250 skipchr();
3251 return chr;
3252}
3253
3254/*
3255 * put character back. Works only once!
3256 */
3257 static void
3258ungetchr()
3259{
3260 nextchr = curchr;
3261 curchr = prevchr;
3262 prevchr = prevprevchr;
3263 at_start = prev_at_start;
3264 prev_at_start = FALSE;
3265
3266 /* Backup regparse, so that it's at the same position as before the
3267 * getchr(). */
3268 regparse -= prevchr_len;
3269}
3270
3271/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00003272 * Get and return the value of the hex string at the current position.
3273 * Return -1 if there is no valid hex number.
3274 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003275 * blahblah\%x20asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003276 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003277 * The parameter controls the maximum number of input characters. This will be
3278 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
3279 */
3280 static int
3281gethexchrs(maxinputlen)
3282 int maxinputlen;
3283{
3284 int nr = 0;
3285 int c;
3286 int i;
3287
3288 for (i = 0; i < maxinputlen; ++i)
3289 {
3290 c = regparse[0];
3291 if (!vim_isxdigit(c))
3292 break;
3293 nr <<= 4;
3294 nr |= hex2nr(c);
3295 ++regparse;
3296 }
3297
3298 if (i == 0)
3299 return -1;
3300 return nr;
3301}
3302
3303/*
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003304 * Get and return the value of the decimal string immediately after the
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003305 * current position. Return -1 for invalid. Consumes all digits.
3306 */
3307 static int
3308getdecchrs()
3309{
3310 int nr = 0;
3311 int c;
3312 int i;
3313
3314 for (i = 0; ; ++i)
3315 {
3316 c = regparse[0];
3317 if (c < '0' || c > '9')
3318 break;
3319 nr *= 10;
3320 nr += c - '0';
3321 ++regparse;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02003322 curchr = -1; /* no longer valid */
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003323 }
3324
3325 if (i == 0)
3326 return -1;
3327 return nr;
3328}
3329
3330/*
3331 * get and return the value of the octal string immediately after the current
3332 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
3333 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
3334 * treat 8 or 9 as recognised characters. Position is updated:
3335 * blahblah\%o210asdf
Bram Moolenaarc9b4b052006-04-30 18:54:39 +00003336 * before-^ ^-after
Bram Moolenaarc0197e22004-09-13 20:26:32 +00003337 */
3338 static int
3339getoctchrs()
3340{
3341 int nr = 0;
3342 int c;
3343 int i;
3344
3345 for (i = 0; i < 3 && nr < 040; ++i)
3346 {
3347 c = regparse[0];
3348 if (c < '0' || c > '7')
3349 break;
3350 nr <<= 3;
3351 nr |= hex2nr(c);
3352 ++regparse;
3353 }
3354
3355 if (i == 0)
3356 return -1;
3357 return nr;
3358}
3359
3360/*
3361 * Get a number after a backslash that is inside [].
3362 * When nothing is recognized return a backslash.
3363 */
3364 static int
3365coll_get_char()
3366{
3367 int nr = -1;
3368
3369 switch (*regparse++)
3370 {
3371 case 'd': nr = getdecchrs(); break;
3372 case 'o': nr = getoctchrs(); break;
3373 case 'x': nr = gethexchrs(2); break;
3374 case 'u': nr = gethexchrs(4); break;
3375 case 'U': nr = gethexchrs(8); break;
3376 }
3377 if (nr < 0)
3378 {
3379 /* If getting the number fails be backwards compatible: the character
3380 * is a backslash. */
3381 --regparse;
3382 nr = '\\';
3383 }
3384 return nr;
3385}
3386
3387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003388 * read_limits - Read two integers to be taken as a minimum and maximum.
3389 * If the first character is '-', then the range is reversed.
3390 * Should end with 'end'. If minval is missing, zero is default, if maxval is
3391 * missing, a very big number is the default.
3392 */
3393 static int
3394read_limits(minval, maxval)
3395 long *minval;
3396 long *maxval;
3397{
3398 int reverse = FALSE;
3399 char_u *first_char;
3400 long tmp;
3401
3402 if (*regparse == '-')
3403 {
3404 /* Starts with '-', so reverse the range later */
3405 regparse++;
3406 reverse = TRUE;
3407 }
3408 first_char = regparse;
3409 *minval = getdigits(&regparse);
3410 if (*regparse == ',') /* There is a comma */
3411 {
3412 if (vim_isdigit(*++regparse))
3413 *maxval = getdigits(&regparse);
3414 else
3415 *maxval = MAX_LIMIT;
3416 }
3417 else if (VIM_ISDIGIT(*first_char))
3418 *maxval = *minval; /* It was \{n} or \{-n} */
3419 else
3420 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
3421 if (*regparse == '\\')
3422 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00003423 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 {
3425 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
3426 reg_magic == MAGIC_ALL ? "" : "\\");
3427 EMSG_RET_FAIL(IObuff);
3428 }
3429
3430 /*
3431 * Reverse the range if there was a '-', or make sure it is in the right
3432 * order otherwise.
3433 */
3434 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
3435 {
3436 tmp = *minval;
3437 *minval = *maxval;
3438 *maxval = tmp;
3439 }
3440 skipchr(); /* let's be friends with the lexer again */
3441 return OK;
3442}
3443
3444/*
3445 * vim_regexec and friends
3446 */
3447
3448/*
3449 * Global work variables for vim_regexec().
3450 */
3451
3452/* The current match-position is remembered with these variables: */
3453static linenr_T reglnum; /* line number, relative to first line */
3454static char_u *regline; /* start of current line */
3455static char_u *reginput; /* current input, points into "regline" */
3456
3457static int need_clear_subexpr; /* subexpressions still need to be
3458 * cleared */
3459#ifdef FEAT_SYN_HL
3460static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
3461 * still need to be cleared */
3462#endif
3463
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464/*
3465 * Structure used to save the current input state, when it needs to be
3466 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00003467 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 */
3469typedef struct
3470{
3471 union
3472 {
3473 char_u *ptr; /* reginput pointer, for single-line regexp */
3474 lpos_T pos; /* reginput pos, for multi-line regexp */
3475 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003476 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003477} regsave_T;
3478
3479/* struct to save start/end pointer/position in for \(\) */
3480typedef struct
3481{
3482 union
3483 {
3484 char_u *ptr;
3485 lpos_T pos;
3486 } se_u;
3487} save_se_T;
3488
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003489/* used for BEHIND and NOBEHIND matching */
3490typedef struct regbehind_S
3491{
3492 regsave_T save_after;
3493 regsave_T save_behind;
Bram Moolenaarfde483c2008-06-15 12:21:50 +00003494 int save_need_clear_subexpr;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003495 save_se_T save_start[NSUBEXP];
3496 save_se_T save_end[NSUBEXP];
3497} regbehind_T;
3498
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499static char_u *reg_getline __ARGS((linenr_T lnum));
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003500static long bt_regexec_both __ARGS((char_u *line, colnr_T col, proftime_T *tm));
3501static long regtry __ARGS((bt_regprog_T *prog, colnr_T col));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502static void cleanup_subexpr __ARGS((void));
3503#ifdef FEAT_SYN_HL
3504static void cleanup_zsubexpr __ARGS((void));
3505#endif
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003506static void save_subexpr __ARGS((regbehind_T *bp));
3507static void restore_subexpr __ARGS((regbehind_T *bp));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00003509static void reg_save __ARGS((regsave_T *save, garray_T *gap));
3510static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511static int reg_save_equal __ARGS((regsave_T *save));
3512static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3513static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3514
3515/* Save the sub-expressions before attempting a match. */
3516#define save_se(savep, posp, pp) \
3517 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3518
3519/* After a failed match restore the sub-expressions. */
3520#define restore_se(savep, posp, pp) { \
3521 if (REG_MULTI) \
3522 *(posp) = (savep)->se_u.pos; \
3523 else \
3524 *(pp) = (savep)->se_u.ptr; }
3525
3526static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaar580abea2013-06-14 20:31:28 +02003527static int match_with_backref __ARGS((linenr_T start_lnum, colnr_T start_col, linenr_T end_lnum, colnr_T end_col, int *bytelen));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003528static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003529static int regrepeat __ARGS((char_u *p, long maxcount));
3530
3531#ifdef DEBUG
3532int regnarrate = 0;
3533#endif
3534
3535/*
3536 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3537 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3538 * contains '\c' or '\C' the value is overruled.
3539 */
3540static int ireg_ic;
3541
3542#ifdef FEAT_MBYTE
3543/*
3544 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3545 * in the regexp. Defaults to false, always.
3546 */
3547static int ireg_icombine;
3548#endif
3549
3550/*
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003551 * Copy of "rmm_maxcol": maximum column to search for a match. Zero when
3552 * there is no maximum.
3553 */
Bram Moolenaarbbebc852005-07-18 21:47:53 +00003554static colnr_T ireg_maxcol;
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003555
3556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3558 * slow, we keep one allocated piece of memory and only re-allocate it when
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003559 * it's too small. It's freed in bt_regexec_both() when finished.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 */
Bram Moolenaard4210772008-01-02 14:35:30 +00003561static char_u *reg_tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562static unsigned reg_tofreelen;
3563
3564/*
3565 * These variables are set when executing a regexp to speed up the execution.
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00003566 * Which ones are set depends on whether a single-line or multi-line match is
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 * done:
3568 * single-line multi-line
3569 * reg_match &regmatch_T NULL
3570 * reg_mmatch NULL &regmmatch_T
3571 * reg_startp reg_match->startp <invalid>
3572 * reg_endp reg_match->endp <invalid>
3573 * reg_startpos <invalid> reg_mmatch->startpos
3574 * reg_endpos <invalid> reg_mmatch->endpos
3575 * reg_win NULL window in which to search
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003576 * reg_buf curbuf buffer in which to search
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 * reg_firstlnum <invalid> first line in which to search
3578 * reg_maxline 0 last line nr
3579 * reg_line_lbr FALSE or TRUE FALSE
3580 */
3581static regmatch_T *reg_match;
3582static regmmatch_T *reg_mmatch;
3583static char_u **reg_startp = NULL;
3584static char_u **reg_endp = NULL;
3585static lpos_T *reg_startpos = NULL;
3586static lpos_T *reg_endpos = NULL;
3587static win_T *reg_win;
3588static buf_T *reg_buf;
3589static linenr_T reg_firstlnum;
3590static linenr_T reg_maxline;
3591static int reg_line_lbr; /* "\n" in string is line break */
3592
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003593/* Values for rs_state in regitem_T. */
3594typedef enum regstate_E
3595{
3596 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3597 , RS_MOPEN /* MOPEN + [0-9] */
3598 , RS_MCLOSE /* MCLOSE + [0-9] */
3599#ifdef FEAT_SYN_HL
3600 , RS_ZOPEN /* ZOPEN + [0-9] */
3601 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3602#endif
3603 , RS_BRANCH /* BRANCH */
3604 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3605 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3606 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3607 , RS_NOMATCH /* NOMATCH */
3608 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3609 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3610 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3611 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3612} regstate_T;
3613
3614/*
3615 * When there are alternatives a regstate_T is put on the regstack to remember
3616 * what we are doing.
3617 * Before it may be another type of item, depending on rs_state, to remember
3618 * more things.
3619 */
3620typedef struct regitem_S
3621{
3622 regstate_T rs_state; /* what we are doing, one of RS_ above */
3623 char_u *rs_scan; /* current node in program */
3624 union
3625 {
3626 save_se_T sesave;
3627 regsave_T regsave;
3628 } rs_un; /* room for saving reginput */
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00003629 short rs_no; /* submatch nr or BEHIND/NOBEHIND */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003630} regitem_T;
3631
3632static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3633static void regstack_pop __ARGS((char_u **scan));
3634
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003635/* used for STAR, PLUS and BRACE_SIMPLE matching */
3636typedef struct regstar_S
3637{
3638 int nextb; /* next byte */
3639 int nextb_ic; /* next byte reverse case */
3640 long count;
3641 long minval;
3642 long maxval;
3643} regstar_T;
3644
3645/* used to store input position when a BACK was encountered, so that we now if
3646 * we made any progress since the last time. */
3647typedef struct backpos_S
3648{
3649 char_u *bp_scan; /* "scan" where BACK was encountered */
3650 regsave_T bp_pos; /* last input position */
3651} backpos_T;
3652
3653/*
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003654 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
3655 * to avoid invoking malloc() and free() often.
3656 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
3657 * or regbehind_T.
3658 * "backpos_T" is a table with backpos_T for BACK
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003659 */
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003660static garray_T regstack = {0, 0, 0, 0, NULL};
3661static garray_T backpos = {0, 0, 0, 0, NULL};
3662
3663/*
3664 * Both for regstack and backpos tables we use the following strategy of
3665 * allocation (to reduce malloc/free calls):
3666 * - Initial size is fairly small.
3667 * - When needed, the tables are grown bigger (8 times at first, double after
3668 * that).
3669 * - After executing the match we free the memory only if the array has grown.
3670 * Thus the memory is kept allocated when it's at the initial size.
3671 * This makes it fast while not keeping a lot of memory allocated.
3672 * A three times speed increase was observed when using many simple patterns.
3673 */
3674#define REGSTACK_INITIAL 2048
3675#define BACKPOS_INITIAL 64
3676
3677#if defined(EXITFREE) || defined(PROTO)
3678 void
3679free_regexp_stuff()
3680{
3681 ga_clear(&regstack);
3682 ga_clear(&backpos);
3683 vim_free(reg_tofree);
3684 vim_free(reg_prev_sub);
3685}
3686#endif
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003687
Bram Moolenaar071d4272004-06-13 20:20:40 +00003688/*
3689 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3690 */
3691 static char_u *
3692reg_getline(lnum)
3693 linenr_T lnum;
3694{
3695 /* when looking behind for a match/no-match lnum is negative. But we
3696 * can't go before line 1 */
3697 if (reg_firstlnum + lnum < 1)
3698 return NULL;
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +00003699 if (lnum > reg_maxline)
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003700 /* Must have matched the "\n" in the last line. */
3701 return (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3703}
3704
3705static regsave_T behind_pos;
3706
3707#ifdef FEAT_SYN_HL
3708static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3709static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3710static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3711static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3712#endif
3713
3714/* TRUE if using multi-line regexp. */
3715#define REG_MULTI (reg_match == NULL)
3716
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003717static int bt_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col, int line_lbr));
3718
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003719
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720/*
3721 * Match a regexp against a string.
3722 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3723 * Uses curbuf for line count and 'iskeyword'.
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003724 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 *
3726 * Return TRUE if there is a match, FALSE if not.
3727 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003728 static int
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003729bt_regexec_nl(rmp, line, col, line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 regmatch_T *rmp;
3731 char_u *line; /* string to match against */
3732 colnr_T col; /* column to start looking for match */
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003733 int line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734{
3735 reg_match = rmp;
3736 reg_mmatch = NULL;
3737 reg_maxline = 0;
Bram Moolenaar2af78a12014-04-23 19:06:37 +02003738 reg_line_lbr = line_lbr;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01003739 reg_buf = curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 reg_win = NULL;
3741 ireg_ic = rmp->rm_ic;
3742#ifdef FEAT_MBYTE
3743 ireg_icombine = FALSE;
3744#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003745 ireg_maxcol = 0;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003746 return (bt_regexec_both(line, col, NULL) != 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747}
3748
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003749static long bt_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm));
3750
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751/*
3752 * Match a regexp against multiple lines.
3753 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3754 * Uses curbuf for line count and 'iskeyword'.
3755 *
3756 * Return zero if there is no match. Return number of lines contained in the
3757 * match otherwise.
3758 */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003759 static long
3760bt_regexec_multi(rmp, win, buf, lnum, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 regmmatch_T *rmp;
3762 win_T *win; /* window in which to search or NULL */
3763 buf_T *buf; /* buffer in which to search */
3764 linenr_T lnum; /* nr of line to start looking for match */
3765 colnr_T col; /* column to start looking for match */
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003766 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767{
3768 long r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769
3770 reg_match = NULL;
3771 reg_mmatch = rmp;
3772 reg_buf = buf;
3773 reg_win = win;
3774 reg_firstlnum = lnum;
3775 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3776 reg_line_lbr = FALSE;
3777 ireg_ic = rmp->rmm_ic;
3778#ifdef FEAT_MBYTE
3779 ireg_icombine = FALSE;
3780#endif
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003781 ireg_maxcol = rmp->rmm_maxcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003783 r = bt_regexec_both(NULL, col, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784
3785 return r;
3786}
3787
3788/*
3789 * Match a regexp against a string ("line" points to the string) or multiple
3790 * lines ("line" is NULL, use reg_getline()).
3791 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 static long
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003793bt_regexec_both(line, col, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 char_u *line;
3795 colnr_T col; /* column to start looking for match */
Bram Moolenaar78a15312009-05-15 19:33:18 +00003796 proftime_T *tm UNUSED; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003798 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003800 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003802 /* Create "regstack" and "backpos" if they are not allocated yet.
3803 * We allocate *_INITIAL amount of bytes first and then set the grow size
3804 * to much bigger value to avoid many malloc calls in case of deep regular
3805 * expressions. */
3806 if (regstack.ga_data == NULL)
3807 {
3808 /* Use an item size of 1 byte, since we push different things
3809 * onto the regstack. */
3810 ga_init2(&regstack, 1, REGSTACK_INITIAL);
3811 ga_grow(&regstack, REGSTACK_INITIAL);
3812 regstack.ga_growsize = REGSTACK_INITIAL * 8;
3813 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00003815 if (backpos.ga_data == NULL)
3816 {
3817 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
3818 ga_grow(&backpos, BACKPOS_INITIAL);
3819 backpos.ga_growsize = BACKPOS_INITIAL * 8;
3820 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003821
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (REG_MULTI)
3823 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003824 prog = (bt_regprog_T *)reg_mmatch->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 line = reg_getline((linenr_T)0);
3826 reg_startpos = reg_mmatch->startpos;
3827 reg_endpos = reg_mmatch->endpos;
3828 }
3829 else
3830 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02003831 prog = (bt_regprog_T *)reg_match->regprog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 reg_startp = reg_match->startp;
3833 reg_endp = reg_match->endp;
3834 }
3835
3836 /* Be paranoid... */
3837 if (prog == NULL || line == NULL)
3838 {
3839 EMSG(_(e_null));
3840 goto theend;
3841 }
3842
3843 /* Check validity of program. */
3844 if (prog_magic_wrong())
3845 goto theend;
3846
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003847 /* If the start column is past the maximum column: no need to try. */
3848 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3849 goto theend;
3850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3852 if (prog->regflags & RF_ICASE)
3853 ireg_ic = TRUE;
3854 else if (prog->regflags & RF_NOICASE)
3855 ireg_ic = FALSE;
3856
3857#ifdef FEAT_MBYTE
3858 /* If pattern contains "\Z" overrule value of ireg_icombine */
3859 if (prog->regflags & RF_ICOMBINE)
3860 ireg_icombine = TRUE;
3861#endif
3862
3863 /* If there is a "must appear" string, look for it. */
3864 if (prog->regmust != NULL)
3865 {
3866 int c;
3867
3868#ifdef FEAT_MBYTE
3869 if (has_mbyte)
3870 c = (*mb_ptr2char)(prog->regmust);
3871 else
3872#endif
3873 c = *prog->regmust;
3874 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003875
3876 /*
3877 * This is used very often, esp. for ":global". Use three versions of
3878 * the loop to avoid overhead of conditions.
3879 */
3880 if (!ireg_ic
3881#ifdef FEAT_MBYTE
3882 && !has_mbyte
3883#endif
3884 )
3885 while ((s = vim_strbyte(s, c)) != NULL)
3886 {
3887 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3888 break; /* Found it. */
3889 ++s;
3890 }
3891#ifdef FEAT_MBYTE
3892 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3893 while ((s = vim_strchr(s, c)) != NULL)
3894 {
3895 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3896 break; /* Found it. */
3897 mb_ptr_adv(s);
3898 }
3899#endif
3900 else
3901 while ((s = cstrchr(s, c)) != NULL)
3902 {
3903 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3904 break; /* Found it. */
3905 mb_ptr_adv(s);
3906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 if (s == NULL) /* Not present. */
3908 goto theend;
3909 }
3910
3911 regline = line;
3912 reglnum = 0;
Bram Moolenaar73a92fe2010-09-14 10:55:47 +02003913 reg_toolong = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
3915 /* Simplest case: Anchored match need be tried only once. */
3916 if (prog->reganch)
3917 {
3918 int c;
3919
3920#ifdef FEAT_MBYTE
3921 if (has_mbyte)
3922 c = (*mb_ptr2char)(regline + col);
3923 else
3924#endif
3925 c = regline[col];
3926 if (prog->regstart == NUL
3927 || prog->regstart == c
3928 || (ireg_ic && ((
3929#ifdef FEAT_MBYTE
3930 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3931 || (c < 255 && prog->regstart < 255 &&
3932#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00003933 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 retval = regtry(prog, col);
3935 else
3936 retval = 0;
3937 }
3938 else
3939 {
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003940#ifdef FEAT_RELTIME
3941 int tm_count = 0;
3942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003944 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 {
3946 if (prog->regstart != NUL)
3947 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003948 /* Skip until the char we know it must start with.
3949 * Used often, do some work to avoid call overhead. */
3950 if (!ireg_ic
3951#ifdef FEAT_MBYTE
3952 && !has_mbyte
3953#endif
3954 )
3955 s = vim_strbyte(regline + col, prog->regstart);
3956 else
3957 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 if (s == NULL)
3959 {
3960 retval = 0;
3961 break;
3962 }
3963 col = (int)(s - regline);
3964 }
3965
Bram Moolenaar3b56eb32005-07-11 22:40:32 +00003966 /* Check for maximum column to try. */
3967 if (ireg_maxcol > 0 && col >= ireg_maxcol)
3968 {
3969 retval = 0;
3970 break;
3971 }
3972
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 retval = regtry(prog, col);
3974 if (retval > 0)
3975 break;
3976
3977 /* if not currently on the first line, get it again */
3978 if (reglnum != 0)
3979 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 reglnum = 0;
Bram Moolenaarae5bce12005-08-15 21:41:48 +00003981 regline = reg_getline((linenr_T)0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003982 }
3983 if (regline[col] == NUL)
3984 break;
3985#ifdef FEAT_MBYTE
3986 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003987 col += (*mb_ptr2len)(regline + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 else
3989#endif
3990 ++col;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00003991#ifdef FEAT_RELTIME
3992 /* Check for timeout once in a twenty times to avoid overhead. */
3993 if (tm != NULL && ++tm_count == 20)
3994 {
3995 tm_count = 0;
3996 if (profile_passed_limit(tm))
3997 break;
3998 }
3999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000 }
4001 }
4002
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003theend:
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004004 /* Free "reg_tofree" when it's a bit big.
4005 * Free regstack and backpos if they are bigger than their initial size. */
4006 if (reg_tofreelen > 400)
4007 {
4008 vim_free(reg_tofree);
4009 reg_tofree = NULL;
4010 }
4011 if (regstack.ga_maxlen > REGSTACK_INITIAL)
4012 ga_clear(&regstack);
4013 if (backpos.ga_maxlen > BACKPOS_INITIAL)
4014 ga_clear(&backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004015
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 return retval;
4017}
4018
4019#ifdef FEAT_SYN_HL
4020static reg_extmatch_T *make_extmatch __ARGS((void));
4021
4022/*
4023 * Create a new extmatch and mark it as referenced once.
4024 */
4025 static reg_extmatch_T *
4026make_extmatch()
4027{
4028 reg_extmatch_T *em;
4029
4030 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
4031 if (em != NULL)
4032 em->refcnt = 1;
4033 return em;
4034}
4035
4036/*
4037 * Add a reference to an extmatch.
4038 */
4039 reg_extmatch_T *
4040ref_extmatch(em)
4041 reg_extmatch_T *em;
4042{
4043 if (em != NULL)
4044 em->refcnt++;
4045 return em;
4046}
4047
4048/*
4049 * Remove a reference to an extmatch. If there are no references left, free
4050 * the info.
4051 */
4052 void
4053unref_extmatch(em)
4054 reg_extmatch_T *em;
4055{
4056 int i;
4057
4058 if (em != NULL && --em->refcnt <= 0)
4059 {
4060 for (i = 0; i < NSUBEXP; ++i)
4061 vim_free(em->matches[i]);
4062 vim_free(em);
4063 }
4064}
4065#endif
4066
4067/*
4068 * regtry - try match of "prog" with at regline["col"].
4069 * Returns 0 for failure, number of lines contained in the match otherwise.
4070 */
4071 static long
4072regtry(prog, col)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004073 bt_regprog_T *prog;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 colnr_T col;
4075{
4076 reginput = regline + col;
4077 need_clear_subexpr = TRUE;
4078#ifdef FEAT_SYN_HL
4079 /* Clear the external match subpointers if necessary. */
4080 if (prog->reghasz == REX_SET)
4081 need_clear_zsubexpr = TRUE;
4082#endif
4083
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004084 if (regmatch(prog->program + 1) == 0)
4085 return 0;
4086
4087 cleanup_subexpr();
4088 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004090 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004092 reg_startpos[0].lnum = 0;
4093 reg_startpos[0].col = col;
4094 }
4095 if (reg_endpos[0].lnum < 0)
4096 {
4097 reg_endpos[0].lnum = reglnum;
4098 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 }
4100 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004101 /* Use line number of "\ze". */
4102 reglnum = reg_endpos[0].lnum;
4103 }
4104 else
4105 {
4106 if (reg_startp[0] == NULL)
4107 reg_startp[0] = regline + col;
4108 if (reg_endp[0] == NULL)
4109 reg_endp[0] = reginput;
4110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004112 /* Package any found \z(...\) matches for export. Default is none. */
4113 unref_extmatch(re_extmatch_out);
4114 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004116 if (prog->reghasz == REX_SET)
4117 {
4118 int i;
4119
4120 cleanup_zsubexpr();
4121 re_extmatch_out = make_extmatch();
4122 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004124 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004126 /* Only accept single line matches. */
4127 if (reg_startzpos[i].lnum >= 0
Bram Moolenaar5a4e1602014-04-06 21:34:04 +02004128 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4129 && reg_endzpos[i].col >= reg_startzpos[i].col)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004130 re_extmatch_out->matches[i] =
4131 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004133 reg_endzpos[i].col - reg_startzpos[i].col);
4134 }
4135 else
4136 {
4137 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4138 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004140 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 }
4142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004144#endif
4145 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146}
4147
4148#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149static int reg_prev_class __ARGS((void));
4150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151/*
4152 * Get class of previous character.
4153 */
4154 static int
4155reg_prev_class()
4156{
4157 if (reginput > regline)
Bram Moolenaarf813a182013-01-30 13:59:37 +01004158 return mb_get_class_buf(reginput - 1
4159 - (*mb_head_off)(regline, reginput - 1), reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 return -1;
4161}
Bram Moolenaar071d4272004-06-13 20:20:40 +00004162#endif
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01004163
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004164static int reg_match_visual __ARGS((void));
4165
4166/*
4167 * Return TRUE if the current reginput position matches the Visual area.
4168 */
4169 static int
4170reg_match_visual()
4171{
4172 pos_T top, bot;
4173 linenr_T lnum;
4174 colnr_T col;
4175 win_T *wp = reg_win == NULL ? curwin : reg_win;
4176 int mode;
4177 colnr_T start, end;
4178 colnr_T start2, end2;
4179 colnr_T cols;
4180
4181 /* Check if the buffer is the current buffer. */
4182 if (reg_buf != curbuf || VIsual.lnum == 0)
4183 return FALSE;
4184
4185 if (VIsual_active)
4186 {
4187 if (lt(VIsual, wp->w_cursor))
4188 {
4189 top = VIsual;
4190 bot = wp->w_cursor;
4191 }
4192 else
4193 {
4194 top = wp->w_cursor;
4195 bot = VIsual;
4196 }
4197 mode = VIsual_mode;
4198 }
4199 else
4200 {
4201 if (lt(curbuf->b_visual.vi_start, curbuf->b_visual.vi_end))
4202 {
4203 top = curbuf->b_visual.vi_start;
4204 bot = curbuf->b_visual.vi_end;
4205 }
4206 else
4207 {
4208 top = curbuf->b_visual.vi_end;
4209 bot = curbuf->b_visual.vi_start;
4210 }
4211 mode = curbuf->b_visual.vi_mode;
4212 }
4213 lnum = reglnum + reg_firstlnum;
4214 if (lnum < top.lnum || lnum > bot.lnum)
4215 return FALSE;
4216
4217 if (mode == 'v')
4218 {
4219 col = (colnr_T)(reginput - regline);
4220 if ((lnum == top.lnum && col < top.col)
4221 || (lnum == bot.lnum && col >= bot.col + (*p_sel != 'e')))
4222 return FALSE;
4223 }
4224 else if (mode == Ctrl_V)
4225 {
4226 getvvcol(wp, &top, &start, NULL, &end);
4227 getvvcol(wp, &bot, &start2, NULL, &end2);
4228 if (start2 < start)
4229 start = start2;
4230 if (end2 > end)
4231 end = end2;
4232 if (top.col == MAXCOL || bot.col == MAXCOL)
4233 end = MAXCOL;
4234 cols = win_linetabsize(wp, regline, (colnr_T)(reginput - regline));
4235 if (cols < start || cols > end - (*p_sel == 'e'))
4236 return FALSE;
4237 }
4238 return TRUE;
4239}
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004240
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00004241#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242
4243/*
4244 * The arguments from BRACE_LIMITS are stored here. They are actually local
4245 * to regmatch(), but they are here to reduce the amount of stack space used
4246 * (it can be called recursively many times).
4247 */
4248static long bl_minval;
4249static long bl_maxval;
4250
4251/*
4252 * regmatch - main matching routine
4253 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004254 * Conceptually the strategy is simple: Check to see whether the current node
4255 * matches, push an item onto the regstack and loop to see whether the rest
4256 * matches, and then act accordingly. In practice we make some effort to
4257 * avoid using the regstack, in particular by going through "ordinary" nodes
4258 * (that don't need to know whether the rest of the match failed) by a nested
4259 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 *
4261 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
4262 * the last matched character.
4263 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
4264 * undefined state!
4265 */
4266 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004267regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 char_u *scan; /* Current node. */
4269{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004270 char_u *next; /* Next node. */
4271 int op;
4272 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004273 regitem_T *rp;
4274 int no;
4275 int status; /* one of the RA_ values: */
4276#define RA_FAIL 1 /* something failed, abort */
4277#define RA_CONT 2 /* continue in inner loop */
4278#define RA_BREAK 3 /* break inner loop */
4279#define RA_MATCH 4 /* successful match */
4280#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281
Bram Moolenaar4bad6c82008-01-18 19:37:23 +00004282 /* Make "regstack" and "backpos" empty. They are allocated and freed in
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004283 * bt_regexec_both() to reduce malloc()/free() calls. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004284 regstack.ga_len = 0;
4285 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004286
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004287 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004288 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004289 */
4290 for (;;)
4291 {
Bram Moolenaar41f12052013-08-25 17:01:42 +02004292 /* Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
4293 * Allow interrupting them with CTRL-C. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 fast_breakcheck();
4295
4296#ifdef DEBUG
4297 if (scan != NULL && regnarrate)
4298 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004299 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 mch_errmsg("(\n");
4301 }
4302#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004303
4304 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00004305 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004306 * regstack.
4307 */
4308 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004310 if (got_int || scan == NULL)
4311 {
4312 status = RA_FAIL;
4313 break;
4314 }
4315 status = RA_CONT;
4316
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317#ifdef DEBUG
4318 if (regnarrate)
4319 {
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004320 mch_errmsg((char *)regprop(scan));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 mch_errmsg("...\n");
4322# ifdef FEAT_SYN_HL
4323 if (re_extmatch_in != NULL)
4324 {
4325 int i;
4326
4327 mch_errmsg(_("External submatches:\n"));
4328 for (i = 0; i < NSUBEXP; i++)
4329 {
4330 mch_errmsg(" \"");
4331 if (re_extmatch_in->matches[i] != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02004332 mch_errmsg((char *)re_extmatch_in->matches[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 mch_errmsg("\"\n");
4334 }
4335 }
4336# endif
4337 }
4338#endif
4339 next = regnext(scan);
4340
4341 op = OP(scan);
4342 /* Check for character class with NL added. */
Bram Moolenaar640009d2006-10-17 16:48:26 +00004343 if (!reg_line_lbr && WITH_NL(op) && REG_MULTI
4344 && *reginput == NUL && reglnum <= reg_maxline)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 {
4346 reg_nextline();
4347 }
4348 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
4349 {
4350 ADVANCE_REGINPUT();
4351 }
4352 else
4353 {
4354 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004355 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356#ifdef FEAT_MBYTE
4357 if (has_mbyte)
4358 c = (*mb_ptr2char)(reginput);
4359 else
4360#endif
4361 c = *reginput;
4362 switch (op)
4363 {
4364 case BOL:
4365 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004366 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 break;
4368
4369 case EOL:
4370 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004371 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 break;
4373
4374 case RE_BOF:
Bram Moolenaara7139332007-12-09 18:26:22 +00004375 /* We're not at the beginning of the file when below the first
4376 * line where we started, not at the start of the line or we
4377 * didn't start at the first line of the buffer. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 if (reglnum != 0 || reginput != regline
Bram Moolenaara7139332007-12-09 18:26:22 +00004379 || (REG_MULTI && reg_firstlnum > 1))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004380 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 break;
4382
4383 case RE_EOF:
4384 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004385 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 break;
4387
4388 case CURSOR:
4389 /* Check if the buffer is in a window and compare the
4390 * reg_win->w_cursor position to the match position. */
4391 if (reg_win == NULL
4392 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
4393 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004394 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 break;
4396
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004397 case RE_MARK:
Bram Moolenaar044aa292013-06-04 21:27:38 +02004398 /* Compare the mark position to the match position. */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004399 {
4400 int mark = OPERAND(scan)[0];
4401 int cmp = OPERAND(scan)[1];
4402 pos_T *pos;
4403
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004404 pos = getmark_buf(reg_buf, mark, FALSE);
Bram Moolenaare9400a42007-05-06 13:04:32 +00004405 if (pos == NULL /* mark doesn't exist */
Bram Moolenaar044aa292013-06-04 21:27:38 +02004406 || pos->lnum <= 0 /* mark isn't set in reg_buf */
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004407 || (pos->lnum == reglnum + reg_firstlnum
4408 ? (pos->col == (colnr_T)(reginput - regline)
4409 ? (cmp == '<' || cmp == '>')
4410 : (pos->col < (colnr_T)(reginput - regline)
4411 ? cmp != '>'
4412 : cmp != '<'))
4413 : (pos->lnum < reglnum + reg_firstlnum
4414 ? cmp != '>'
4415 : cmp != '<')))
4416 status = RA_NOMATCH;
4417 }
4418 break;
4419
4420 case RE_VISUAL:
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004421 if (!reg_match_visual())
Bram Moolenaardacd7de2013-06-04 18:28:48 +02004422 status = RA_NOMATCH;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00004423 break;
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 case RE_LNUM:
4426 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
4427 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004428 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 break;
4430
4431 case RE_COL:
4432 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004433 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 break;
4435
4436 case RE_VCOL:
4437 if (!re_num_cmp((long_u)win_linetabsize(
4438 reg_win == NULL ? curwin : reg_win,
4439 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004440 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 break;
4442
4443 case BOW: /* \<word; reginput points to w */
4444 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004445 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004447 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 int this_class;
4450
4451 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004452 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004454 status = RA_NOMATCH; /* not on a word at all */
4455 else if (reg_prev_class() == this_class)
4456 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458#endif
4459 else
4460 {
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01004461 if (!vim_iswordc_buf(c, reg_buf) || (reginput > regline
4462 && vim_iswordc_buf(reginput[-1], reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004463 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 }
4465 break;
4466
4467 case EOW: /* word\>; reginput points after d */
4468 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004469 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004471 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 int this_class, prev_class;
4474
4475 /* Get class of current and previous char (if it exists). */
Bram Moolenaarf813a182013-01-30 13:59:37 +01004476 this_class = mb_get_class_buf(reginput, reg_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004478 if (this_class == prev_class
4479 || prev_class == 0 || prev_class == 1)
4480 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004483 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +01004485 if (!vim_iswordc_buf(reginput[-1], reg_buf)
4486 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004487 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 }
4489 break; /* Matched with EOW */
4490
4491 case ANY:
Bram Moolenaare337e5f2013-01-30 18:21:51 +01004492 /* ANY does not match new lines. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004494 status = RA_NOMATCH;
4495 else
4496 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 break;
4498
4499 case IDENT:
4500 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004501 status = RA_NOMATCH;
4502 else
4503 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 break;
4505
4506 case SIDENT:
4507 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004508 status = RA_NOMATCH;
4509 else
4510 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 break;
4512
4513 case KWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004514 if (!vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004515 status = RA_NOMATCH;
4516 else
4517 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 break;
4519
4520 case SKWORD:
Bram Moolenaarf813a182013-01-30 13:59:37 +01004521 if (VIM_ISDIGIT(*reginput) || !vim_iswordp_buf(reginput, reg_buf))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004522 status = RA_NOMATCH;
4523 else
4524 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 break;
4526
4527 case FNAME:
4528 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004529 status = RA_NOMATCH;
4530 else
4531 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 break;
4533
4534 case SFNAME:
4535 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004536 status = RA_NOMATCH;
4537 else
4538 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 break;
4540
4541 case PRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004542 if (!vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004543 status = RA_NOMATCH;
4544 else
4545 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 break;
4547
4548 case SPRINT:
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02004549 if (VIM_ISDIGIT(*reginput) || !vim_isprintc(PTR2CHAR(reginput)))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004550 status = RA_NOMATCH;
4551 else
4552 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 break;
4554
4555 case WHITE:
4556 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004557 status = RA_NOMATCH;
4558 else
4559 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 break;
4561
4562 case NWHITE:
4563 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004564 status = RA_NOMATCH;
4565 else
4566 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 break;
4568
4569 case DIGIT:
4570 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004571 status = RA_NOMATCH;
4572 else
4573 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 break;
4575
4576 case NDIGIT:
4577 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004578 status = RA_NOMATCH;
4579 else
4580 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 break;
4582
4583 case HEX:
4584 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004585 status = RA_NOMATCH;
4586 else
4587 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 break;
4589
4590 case NHEX:
4591 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004592 status = RA_NOMATCH;
4593 else
4594 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 break;
4596
4597 case OCTAL:
4598 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004599 status = RA_NOMATCH;
4600 else
4601 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 break;
4603
4604 case NOCTAL:
4605 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004606 status = RA_NOMATCH;
4607 else
4608 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 break;
4610
4611 case WORD:
4612 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004613 status = RA_NOMATCH;
4614 else
4615 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 break;
4617
4618 case NWORD:
4619 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004620 status = RA_NOMATCH;
4621 else
4622 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 break;
4624
4625 case HEAD:
4626 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004627 status = RA_NOMATCH;
4628 else
4629 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 break;
4631
4632 case NHEAD:
4633 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004634 status = RA_NOMATCH;
4635 else
4636 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 break;
4638
4639 case ALPHA:
4640 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004641 status = RA_NOMATCH;
4642 else
4643 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 break;
4645
4646 case NALPHA:
4647 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004648 status = RA_NOMATCH;
4649 else
4650 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 break;
4652
4653 case LOWER:
4654 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004655 status = RA_NOMATCH;
4656 else
4657 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 break;
4659
4660 case NLOWER:
4661 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004662 status = RA_NOMATCH;
4663 else
4664 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 break;
4666
4667 case UPPER:
4668 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004669 status = RA_NOMATCH;
4670 else
4671 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 break;
4673
4674 case NUPPER:
4675 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004676 status = RA_NOMATCH;
4677 else
4678 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 break;
4680
4681 case EXACTLY:
4682 {
4683 int len;
4684 char_u *opnd;
4685
4686 opnd = OPERAND(scan);
4687 /* Inline the first byte, for speed. */
4688 if (*opnd != *reginput
4689 && (!ireg_ic || (
4690#ifdef FEAT_MBYTE
4691 !enc_utf8 &&
4692#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00004693 MB_TOLOWER(*opnd) != MB_TOLOWER(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004694 status = RA_NOMATCH;
4695 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 {
4697 /* match empty string always works; happens when "~" is
4698 * empty. */
4699 }
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004700 else
4701 {
4702 if (opnd[1] == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703#ifdef FEAT_MBYTE
4704 && !(enc_utf8 && ireg_ic)
4705#endif
4706 )
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004707 {
4708 len = 1; /* matched a single byte above */
4709 }
4710 else
4711 {
4712 /* Need to match first byte again for multi-byte. */
4713 len = (int)STRLEN(opnd);
4714 if (cstrncmp(opnd, reginput, &len) != 0)
4715 status = RA_NOMATCH;
4716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004717#ifdef FEAT_MBYTE
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004718 /* Check for following composing character, unless %C
4719 * follows (skips over all composing chars). */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004720 if (status != RA_NOMATCH
4721 && enc_utf8
4722 && UTF_COMPOSINGLIKE(reginput, reginput + len)
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004723 && !ireg_icombine
4724 && OP(next) != RE_COMPOSING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004725 {
4726 /* raaron: This code makes a composing character get
4727 * ignored, which is the correct behavior (sometimes)
4728 * for voweled Hebrew texts. */
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004729 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731#endif
Bram Moolenaar6082bea2014-05-13 18:04:00 +02004732 if (status != RA_NOMATCH)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004733 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 }
4735 }
4736 break;
4737
4738 case ANYOF:
4739 case ANYBUT:
4740 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004741 status = RA_NOMATCH;
4742 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4743 status = RA_NOMATCH;
4744 else
4745 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 break;
4747
4748#ifdef FEAT_MBYTE
4749 case MULTIBYTECODE:
4750 if (has_mbyte)
4751 {
4752 int i, len;
4753 char_u *opnd;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004754 int opndc = 0, inpc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
4756 opnd = OPERAND(scan);
4757 /* Safety check (just in case 'encoding' was changed since
4758 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004759 if ((len = (*mb_ptr2len)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004760 {
4761 status = RA_NOMATCH;
4762 break;
4763 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004764 if (enc_utf8)
4765 opndc = mb_ptr2char(opnd);
4766 if (enc_utf8 && utf_iscomposing(opndc))
4767 {
4768 /* When only a composing char is given match at any
4769 * position where that composing char appears. */
4770 status = RA_NOMATCH;
4771 for (i = 0; reginput[i] != NUL; i += utf_char2len(inpc))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004772 {
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004773 inpc = mb_ptr2char(reginput + i);
4774 if (!utf_iscomposing(inpc))
4775 {
4776 if (i > 0)
4777 break;
4778 }
4779 else if (opndc == inpc)
4780 {
4781 /* Include all following composing chars. */
4782 len = i + mb_ptr2len(reginput + i);
4783 status = RA_MATCH;
4784 break;
4785 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004786 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00004787 }
4788 else
4789 for (i = 0; i < len; ++i)
4790 if (opnd[i] != reginput[i])
4791 {
4792 status = RA_NOMATCH;
4793 break;
4794 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 reginput += len;
4796 }
4797 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004798 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 break;
4800#endif
Bram Moolenaar8df5acf2014-05-13 19:37:29 +02004801 case RE_COMPOSING:
4802#ifdef FEAT_MBYTE
4803 if (enc_utf8)
4804 {
4805 /* Skip composing characters. */
4806 while (utf_iscomposing(utf_ptr2char(reginput)))
4807 mb_cptr_adv(reginput);
4808 }
4809#endif
4810 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811
4812 case NOTHING:
4813 break;
4814
4815 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004816 {
4817 int i;
4818 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819
Bram Moolenaar582fd852005-03-28 20:58:01 +00004820 /*
4821 * When we run into BACK we need to check if we don't keep
4822 * looping without matching any input. The second and later
4823 * times a BACK is encountered it fails if the input is still
4824 * at the same position as the previous time.
4825 * The positions are stored in "backpos" and found by the
4826 * current value of "scan", the position in the RE program.
4827 */
4828 bp = (backpos_T *)backpos.ga_data;
4829 for (i = 0; i < backpos.ga_len; ++i)
4830 if (bp[i].bp_scan == scan)
4831 break;
4832 if (i == backpos.ga_len)
4833 {
4834 /* First time at this BACK, make room to store the pos. */
4835 if (ga_grow(&backpos, 1) == FAIL)
4836 status = RA_FAIL;
4837 else
4838 {
4839 /* get "ga_data" again, it may have changed */
4840 bp = (backpos_T *)backpos.ga_data;
4841 bp[i].bp_scan = scan;
4842 ++backpos.ga_len;
4843 }
4844 }
4845 else if (reg_save_equal(&bp[i].bp_pos))
4846 /* Still at same position as last time, fail. */
4847 status = RA_NOMATCH;
4848
4849 if (status != RA_FAIL && status != RA_NOMATCH)
4850 reg_save(&bp[i].bp_pos, &backpos);
4851 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004852 break;
4853
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 case MOPEN + 0: /* Match start: \zs */
4855 case MOPEN + 1: /* \( */
4856 case MOPEN + 2:
4857 case MOPEN + 3:
4858 case MOPEN + 4:
4859 case MOPEN + 5:
4860 case MOPEN + 6:
4861 case MOPEN + 7:
4862 case MOPEN + 8:
4863 case MOPEN + 9:
4864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 no = op - MOPEN;
4866 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004867 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004868 if (rp == NULL)
4869 status = RA_FAIL;
4870 else
4871 {
4872 rp->rs_no = no;
4873 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4874 &reg_startp[no]);
4875 /* We simply continue and handle the result when done. */
4876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004878 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879
4880 case NOPEN: /* \%( */
4881 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004882 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004883 status = RA_FAIL;
4884 /* We simply continue and handle the result when done. */
4885 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887#ifdef FEAT_SYN_HL
4888 case ZOPEN + 1:
4889 case ZOPEN + 2:
4890 case ZOPEN + 3:
4891 case ZOPEN + 4:
4892 case ZOPEN + 5:
4893 case ZOPEN + 6:
4894 case ZOPEN + 7:
4895 case ZOPEN + 8:
4896 case ZOPEN + 9:
4897 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 no = op - ZOPEN;
4899 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004900 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004901 if (rp == NULL)
4902 status = RA_FAIL;
4903 else
4904 {
4905 rp->rs_no = no;
4906 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4907 &reg_startzp[no]);
4908 /* We simply continue and handle the result when done. */
4909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004911 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912#endif
4913
4914 case MCLOSE + 0: /* Match end: \ze */
4915 case MCLOSE + 1: /* \) */
4916 case MCLOSE + 2:
4917 case MCLOSE + 3:
4918 case MCLOSE + 4:
4919 case MCLOSE + 5:
4920 case MCLOSE + 6:
4921 case MCLOSE + 7:
4922 case MCLOSE + 8:
4923 case MCLOSE + 9:
4924 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 no = op - MCLOSE;
4926 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004927 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004928 if (rp == NULL)
4929 status = RA_FAIL;
4930 else
4931 {
4932 rp->rs_no = no;
4933 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4934 /* We simply continue and handle the result when done. */
4935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004937 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938
4939#ifdef FEAT_SYN_HL
4940 case ZCLOSE + 1: /* \) after \z( */
4941 case ZCLOSE + 2:
4942 case ZCLOSE + 3:
4943 case ZCLOSE + 4:
4944 case ZCLOSE + 5:
4945 case ZCLOSE + 6:
4946 case ZCLOSE + 7:
4947 case ZCLOSE + 8:
4948 case ZCLOSE + 9:
4949 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 no = op - ZCLOSE;
4951 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004952 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004953 if (rp == NULL)
4954 status = RA_FAIL;
4955 else
4956 {
4957 rp->rs_no = no;
4958 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4959 &reg_endzp[no]);
4960 /* We simply continue and handle the result when done. */
4961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004963 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964#endif
4965
4966 case BACKREF + 1:
4967 case BACKREF + 2:
4968 case BACKREF + 3:
4969 case BACKREF + 4:
4970 case BACKREF + 5:
4971 case BACKREF + 6:
4972 case BACKREF + 7:
4973 case BACKREF + 8:
4974 case BACKREF + 9:
4975 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
4978 no = op - BACKREF;
4979 cleanup_subexpr();
4980 if (!REG_MULTI) /* Single-line regexp */
4981 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004982 if (reg_startp[no] == NULL || reg_endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 /* Backref was not set: Match an empty string. */
4985 len = 0;
4986 }
4987 else
4988 {
4989 /* Compare current input with back-ref in the same
4990 * line. */
4991 len = (int)(reg_endp[no] - reg_startp[no]);
4992 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004993 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 }
4995 }
4996 else /* Multi-line regexp */
4997 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00004998 if (reg_startpos[no].lnum < 0 || reg_endpos[no].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
5000 /* Backref was not set: Match an empty string. */
5001 len = 0;
5002 }
5003 else
5004 {
5005 if (reg_startpos[no].lnum == reglnum
5006 && reg_endpos[no].lnum == reglnum)
5007 {
5008 /* Compare back-ref within the current line. */
5009 len = reg_endpos[no].col - reg_startpos[no].col;
5010 if (cstrncmp(regline + reg_startpos[no].col,
5011 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005012 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else
5015 {
5016 /* Messy situation: Need to compare between two
5017 * lines. */
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02005018 int r = match_with_backref(
Bram Moolenaar580abea2013-06-14 20:31:28 +02005019 reg_startpos[no].lnum,
5020 reg_startpos[no].col,
5021 reg_endpos[no].lnum,
5022 reg_endpos[no].col,
Bram Moolenaar4cff8fa2013-06-14 22:48:54 +02005023 &len);
Bram Moolenaar141f6bb2013-06-15 15:09:50 +02005024
5025 if (r != RA_MATCH)
5026 status = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 }
5029 }
5030
5031 /* Matched the backref, skip over it. */
5032 reginput += len;
5033 }
5034 break;
5035
5036#ifdef FEAT_SYN_HL
5037 case ZREF + 1:
5038 case ZREF + 2:
5039 case ZREF + 3:
5040 case ZREF + 4:
5041 case ZREF + 5:
5042 case ZREF + 6:
5043 case ZREF + 7:
5044 case ZREF + 8:
5045 case ZREF + 9:
5046 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 int len;
5048
5049 cleanup_zsubexpr();
5050 no = op - ZREF;
5051 if (re_extmatch_in != NULL
5052 && re_extmatch_in->matches[no] != NULL)
5053 {
5054 len = (int)STRLEN(re_extmatch_in->matches[no]);
5055 if (cstrncmp(re_extmatch_in->matches[no],
5056 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005057 status = RA_NOMATCH;
5058 else
5059 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 }
5061 else
5062 {
5063 /* Backref was not set: Match an empty string. */
5064 }
5065 }
5066 break;
5067#endif
5068
5069 case BRANCH:
5070 {
5071 if (OP(next) != BRANCH) /* No choice. */
5072 next = OPERAND(scan); /* Avoid recursion. */
5073 else
5074 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005075 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005076 if (rp == NULL)
5077 status = RA_FAIL;
5078 else
5079 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081 }
5082 break;
5083
5084 case BRACE_LIMITS:
5085 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 if (OP(next) == BRACE_SIMPLE)
5087 {
5088 bl_minval = OPERAND_MIN(scan);
5089 bl_maxval = OPERAND_MAX(scan);
5090 }
5091 else if (OP(next) >= BRACE_COMPLEX
5092 && OP(next) < BRACE_COMPLEX + 10)
5093 {
5094 no = OP(next) - BRACE_COMPLEX;
5095 brace_min[no] = OPERAND_MIN(scan);
5096 brace_max[no] = OPERAND_MAX(scan);
5097 brace_count[no] = 0;
5098 }
5099 else
5100 {
5101 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005102 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
5104 }
5105 break;
5106
5107 case BRACE_COMPLEX + 0:
5108 case BRACE_COMPLEX + 1:
5109 case BRACE_COMPLEX + 2:
5110 case BRACE_COMPLEX + 3:
5111 case BRACE_COMPLEX + 4:
5112 case BRACE_COMPLEX + 5:
5113 case BRACE_COMPLEX + 6:
5114 case BRACE_COMPLEX + 7:
5115 case BRACE_COMPLEX + 8:
5116 case BRACE_COMPLEX + 9:
5117 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 no = op - BRACE_COMPLEX;
5119 ++brace_count[no];
5120
5121 /* If not matched enough times yet, try one more */
5122 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005123 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005125 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005126 if (rp == NULL)
5127 status = RA_FAIL;
5128 else
5129 {
5130 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005131 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005132 next = OPERAND(scan);
5133 /* We continue and handle the result when done. */
5134 }
5135 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137
5138 /* If matched enough times, may try matching some more */
5139 if (brace_min[no] <= brace_max[no])
5140 {
5141 /* Range is the normal way around, use longest match */
5142 if (brace_count[no] <= brace_max[no])
5143 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005144 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005145 if (rp == NULL)
5146 status = RA_FAIL;
5147 else
5148 {
5149 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005150 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005151 next = OPERAND(scan);
5152 /* We continue and handle the result when done. */
5153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 }
5155 }
5156 else
5157 {
5158 /* Range is backwards, use shortest match first */
5159 if (brace_count[no] <= brace_min[no])
5160 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005161 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005162 if (rp == NULL)
5163 status = RA_FAIL;
5164 else
5165 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005166 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005167 /* We continue and handle the result when done. */
5168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 }
5171 }
5172 break;
5173
5174 case BRACE_SIMPLE:
5175 case STAR:
5176 case PLUS:
5177 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005178 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179
5180 /*
5181 * Lookahead to avoid useless match attempts when we know
5182 * what character comes next.
5183 */
5184 if (OP(next) == EXACTLY)
5185 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005186 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 if (ireg_ic)
5188 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005189 if (MB_ISUPPER(rst.nextb))
5190 rst.nextb_ic = MB_TOLOWER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 else
Bram Moolenaara245a5b2007-08-11 11:58:23 +00005192 rst.nextb_ic = MB_TOUPPER(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005195 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 }
5197 else
5198 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005199 rst.nextb = NUL;
5200 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 if (op != BRACE_SIMPLE)
5203 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005204 rst.minval = (op == STAR) ? 0 : 1;
5205 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005206 }
5207 else
5208 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005209 rst.minval = bl_minval;
5210 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
5212
5213 /*
5214 * When maxval > minval, try matching as much as possible, up
5215 * to maxval. When maxval < minval, try matching at least the
5216 * minimal number (since the range is backwards, that's also
5217 * maxval!).
5218 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005219 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005222 status = RA_FAIL;
5223 break;
5224 }
5225 if (rst.minval <= rst.maxval
5226 ? rst.count >= rst.minval : rst.count >= rst.maxval)
5227 {
5228 /* It could match. Prepare for trying to match what
5229 * follows. The code is below. Parameters are stored in
5230 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005231 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005232 {
5233 EMSG(_(e_maxmempat));
5234 status = RA_FAIL;
5235 }
5236 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005237 status = RA_FAIL;
5238 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005240 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005241 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00005242 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005243 if (rp == NULL)
5244 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005246 {
5247 *(((regstar_T *)rp) - 1) = rst;
5248 status = RA_BREAK; /* skip the restore bits */
5249 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 }
5251 }
5252 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005253 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 }
5256 break;
5257
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005258 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 case MATCH:
5260 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005261 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005262 if (rp == NULL)
5263 status = RA_FAIL;
5264 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005266 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005267 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005268 next = OPERAND(scan);
5269 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
5271 break;
5272
5273 case BEHIND:
5274 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005275 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00005276 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005277 {
5278 EMSG(_(e_maxmempat));
5279 status = RA_FAIL;
5280 }
5281 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005282 status = RA_FAIL;
5283 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005285 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005286 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005287 if (rp == NULL)
5288 status = RA_FAIL;
5289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00005290 {
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005291 /* Need to save the subexpr to be able to restore them
5292 * when there is a match but we don't use it. */
5293 save_subexpr(((regbehind_T *)rp) - 1);
5294
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005295 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005296 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005297 /* First try if what follows matches. If it does then we
5298 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005301 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
5303 case BHPOS:
5304 if (REG_MULTI)
5305 {
5306 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
5307 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005308 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
5310 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005311 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005312 break;
5313
5314 case NEWL:
Bram Moolenaar640009d2006-10-17 16:48:26 +00005315 if ((c != NUL || !REG_MULTI || reglnum > reg_maxline
5316 || reg_line_lbr) && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005317 status = RA_NOMATCH;
5318 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319 ADVANCE_REGINPUT();
5320 else
5321 reg_nextline();
5322 break;
5323
5324 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005325 status = RA_MATCH; /* Success! */
5326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327
5328 default:
5329 EMSG(_(e_re_corr));
5330#ifdef DEBUG
5331 printf("Illegal op code %d\n", op);
5332#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005333 status = RA_FAIL;
5334 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336 }
5337
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005338 /* If we can't continue sequentially, break the inner loop. */
5339 if (status != RA_CONT)
5340 break;
5341
5342 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005344
5345 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
5347 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005348 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00005349 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005351 while (regstack.ga_len > 0 && status != RA_FAIL)
5352 {
5353 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
5354 switch (rp->rs_state)
5355 {
5356 case RS_NOPEN:
5357 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005358 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005359 break;
5360
5361 case RS_MOPEN:
5362 /* Pop the state. Restore pointers when there is no match. */
5363 if (status == RA_NOMATCH)
5364 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
5365 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005366 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005367 break;
5368
5369#ifdef FEAT_SYN_HL
5370 case RS_ZOPEN:
5371 /* Pop the state. Restore pointers when there is no match. */
5372 if (status == RA_NOMATCH)
5373 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
5374 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005375 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005376 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005377#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005378
5379 case RS_MCLOSE:
5380 /* Pop the state. Restore pointers when there is no match. */
5381 if (status == RA_NOMATCH)
5382 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
5383 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005384 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005385 break;
5386
5387#ifdef FEAT_SYN_HL
5388 case RS_ZCLOSE:
5389 /* Pop the state. Restore pointers when there is no match. */
5390 if (status == RA_NOMATCH)
5391 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
5392 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005393 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005394 break;
5395#endif
5396
5397 case RS_BRANCH:
5398 if (status == RA_MATCH)
5399 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005400 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005401 else
5402 {
5403 if (status != RA_BREAK)
5404 {
5405 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005406 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005407 scan = rp->rs_scan;
5408 }
5409 if (scan == NULL || OP(scan) != BRANCH)
5410 {
5411 /* no more branches, didn't find a match */
5412 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005413 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005414 }
5415 else
5416 {
5417 /* Prepare to try a branch. */
5418 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00005419 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005420 scan = OPERAND(scan);
5421 }
5422 }
5423 break;
5424
5425 case RS_BRCPLX_MORE:
5426 /* Pop the state. Restore pointers when there is no match. */
5427 if (status == RA_NOMATCH)
5428 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005429 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005430 --brace_count[rp->rs_no]; /* decrement match count */
5431 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005432 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005433 break;
5434
5435 case RS_BRCPLX_LONG:
5436 /* Pop the state. Restore pointers when there is no match. */
5437 if (status == RA_NOMATCH)
5438 {
5439 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005440 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005441 --brace_count[rp->rs_no];
5442 /* continue with the items after "\{}" */
5443 status = RA_CONT;
5444 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005445 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005446 if (status == RA_CONT)
5447 scan = regnext(scan);
5448 break;
5449
5450 case RS_BRCPLX_SHORT:
5451 /* Pop the state. Restore pointers when there is no match. */
5452 if (status == RA_NOMATCH)
5453 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005454 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005455 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005456 if (status == RA_NOMATCH)
5457 {
5458 scan = OPERAND(scan);
5459 status = RA_CONT;
5460 }
5461 break;
5462
5463 case RS_NOMATCH:
5464 /* Pop the state. If the operand matches for NOMATCH or
5465 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
5466 * except for SUBPAT, and continue with the next item. */
5467 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
5468 status = RA_NOMATCH;
5469 else
5470 {
5471 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005472 if (rp->rs_no != SUBPAT) /* zero-width */
5473 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005474 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005475 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005476 if (status == RA_CONT)
5477 scan = regnext(scan);
5478 break;
5479
5480 case RS_BEHIND1:
5481 if (status == RA_NOMATCH)
5482 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005483 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005484 regstack.ga_len -= sizeof(regbehind_T);
5485 }
5486 else
5487 {
5488 /* The stuff after BEHIND/NOBEHIND matches. Now try if
5489 * the behind part does (not) match before the current
5490 * position in the input. This must be done at every
5491 * position in the input and checking if the match ends at
5492 * the current position. */
5493
5494 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005495 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005496
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005497 /* Start looking for a match with operand at the current
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00005498 * position. Go back one character until we find the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005499 * result, hitting the start of the line or the previous
5500 * line (for multi-line matching).
5501 * Set behind_pos to where the match should end, BHPOS
5502 * will match it. Save the current value. */
5503 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
5504 behind_pos = rp->rs_un.regsave;
5505
5506 rp->rs_state = RS_BEHIND2;
5507
Bram Moolenaar582fd852005-03-28 20:58:01 +00005508 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005509 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005510 }
5511 break;
5512
5513 case RS_BEHIND2:
5514 /*
5515 * Looping for BEHIND / NOBEHIND match.
5516 */
5517 if (status == RA_MATCH && reg_save_equal(&behind_pos))
5518 {
5519 /* found a match that ends where "next" started */
5520 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5521 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005522 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5523 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005524 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005525 {
5526 /* But we didn't want a match. Need to restore the
5527 * subexpr, because what follows matched, so they have
5528 * been set. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005529 status = RA_NOMATCH;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005530 restore_subexpr(((regbehind_T *)rp) - 1);
5531 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005532 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005533 regstack.ga_len -= sizeof(regbehind_T);
5534 }
5535 else
5536 {
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005537 long limit;
5538
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005539 /* No match or a match that doesn't end where we want it: Go
5540 * back one character. May go to previous line once. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005541 no = OK;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005542 limit = OPERAND_MIN(rp->rs_scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005543 if (REG_MULTI)
5544 {
Bram Moolenaar61602c52013-06-01 19:54:43 +02005545 if (limit > 0
5546 && ((rp->rs_un.regsave.rs_u.pos.lnum
5547 < behind_pos.rs_u.pos.lnum
5548 ? (colnr_T)STRLEN(regline)
5549 : behind_pos.rs_u.pos.col)
5550 - rp->rs_un.regsave.rs_u.pos.col >= limit))
5551 no = FAIL;
5552 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005553 {
5554 if (rp->rs_un.regsave.rs_u.pos.lnum
5555 < behind_pos.rs_u.pos.lnum
5556 || reg_getline(
5557 --rp->rs_un.regsave.rs_u.pos.lnum)
5558 == NULL)
5559 no = FAIL;
5560 else
5561 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005562 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005563 rp->rs_un.regsave.rs_u.pos.col =
5564 (colnr_T)STRLEN(regline);
5565 }
5566 }
5567 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005568 {
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005569#ifdef FEAT_MBYTE
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005570 if (has_mbyte)
5571 rp->rs_un.regsave.rs_u.pos.col -=
5572 (*mb_head_off)(regline, regline
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005573 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005574 else
Bram Moolenaarf5e44a72013-02-26 18:46:01 +01005575#endif
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005576 --rp->rs_un.regsave.rs_u.pos.col;
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005577 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005578 }
5579 else
5580 {
5581 if (rp->rs_un.regsave.rs_u.ptr == regline)
5582 no = FAIL;
5583 else
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005584 {
5585 mb_ptr_back(regline, rp->rs_un.regsave.rs_u.ptr);
5586 if (limit > 0 && (long)(behind_pos.rs_u.ptr
5587 - rp->rs_un.regsave.rs_u.ptr) > limit)
5588 no = FAIL;
5589 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005590 }
5591 if (no == OK)
5592 {
5593 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00005594 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar75eb1612013-05-29 18:45:11 +02005595 scan = OPERAND(rp->rs_scan) + 4;
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005596 if (status == RA_MATCH)
5597 {
5598 /* We did match, so subexpr may have been changed,
5599 * need to restore them for the next try. */
5600 status = RA_NOMATCH;
5601 restore_subexpr(((regbehind_T *)rp) - 1);
5602 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005603 }
5604 else
5605 {
5606 /* Can't advance. For NOBEHIND that's a match. */
5607 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
5608 if (rp->rs_no == NOBEHIND)
5609 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005610 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
5611 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005612 status = RA_MATCH;
5613 }
5614 else
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00005615 {
5616 /* We do want a proper match. Need to restore the
5617 * subexpr if we had a match, because they may have
5618 * been set. */
5619 if (status == RA_MATCH)
5620 {
5621 status = RA_NOMATCH;
5622 restore_subexpr(((regbehind_T *)rp) - 1);
5623 }
5624 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005625 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005626 regstack.ga_len -= sizeof(regbehind_T);
5627 }
5628 }
5629 break;
5630
5631 case RS_STAR_LONG:
5632 case RS_STAR_SHORT:
5633 {
5634 regstar_T *rst = ((regstar_T *)rp) - 1;
5635
5636 if (status == RA_MATCH)
5637 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005638 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005639 regstack.ga_len -= sizeof(regstar_T);
5640 break;
5641 }
5642
5643 /* Tried once already, restore input pointers. */
5644 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00005645 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005646
5647 /* Repeat until we found a position where it could match. */
5648 for (;;)
5649 {
5650 if (status != RA_BREAK)
5651 {
5652 /* Tried first position already, advance. */
5653 if (rp->rs_state == RS_STAR_LONG)
5654 {
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005655 /* Trying for longest match, but couldn't or
5656 * didn't match -- back up one char. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005657 if (--rst->count < rst->minval)
5658 break;
5659 if (reginput == regline)
5660 {
5661 /* backup to last char of previous line */
5662 --reglnum;
5663 regline = reg_getline(reglnum);
5664 /* Just in case regrepeat() didn't count
5665 * right. */
5666 if (regline == NULL)
5667 break;
5668 reginput = regline + STRLEN(regline);
5669 fast_breakcheck();
5670 }
5671 else
5672 mb_ptr_back(regline, reginput);
5673 }
5674 else
5675 {
5676 /* Range is backwards, use shortest match first.
5677 * Careful: maxval and minval are exchanged!
5678 * Couldn't or didn't match: try advancing one
5679 * char. */
5680 if (rst->count == rst->minval
5681 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
5682 break;
5683 ++rst->count;
5684 }
5685 if (got_int)
5686 break;
5687 }
5688 else
5689 status = RA_NOMATCH;
5690
5691 /* If it could match, try it. */
5692 if (rst->nextb == NUL || *reginput == rst->nextb
5693 || *reginput == rst->nextb_ic)
5694 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00005695 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005696 scan = regnext(rp->rs_scan);
5697 status = RA_CONT;
5698 break;
5699 }
5700 }
5701 if (status != RA_CONT)
5702 {
5703 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005704 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005705 regstack.ga_len -= sizeof(regstar_T);
5706 status = RA_NOMATCH;
5707 }
5708 }
5709 break;
5710 }
5711
Bram Moolenaar32466aa2006-02-24 23:53:04 +00005712 /* If we want to continue the inner loop or didn't pop a state
5713 * continue matching loop */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005714 if (status == RA_CONT || rp == (regitem_T *)
5715 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5716 break;
5717 }
5718
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005719 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005720 if (status == RA_CONT)
5721 continue;
5722
5723 /*
5724 * If the regstack is empty or something failed we are done.
5725 */
5726 if (regstack.ga_len == 0 || status == RA_FAIL)
5727 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005728 if (scan == NULL)
5729 {
5730 /*
5731 * We get here only if there's trouble -- normally "case END" is
5732 * the terminating point.
5733 */
5734 EMSG(_(e_re_corr));
5735#ifdef DEBUG
5736 printf("Premature EOL\n");
5737#endif
5738 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005739 if (status == RA_FAIL)
5740 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005741 return (status == RA_MATCH);
5742 }
5743
5744 } /* End of loop until the regstack is empty. */
5745
5746 /* NOTREACHED */
5747}
5748
5749/*
5750 * Push an item onto the regstack.
5751 * Returns pointer to new item. Returns NULL when out of memory.
5752 */
5753 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005754regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005755 regstate_T state;
5756 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005757{
5758 regitem_T *rp;
5759
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005760 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005761 {
5762 EMSG(_(e_maxmempat));
5763 return NULL;
5764 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005765 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005766 return NULL;
5767
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005768 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005769 rp->rs_state = state;
5770 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005771
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005772 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005773 return rp;
5774}
5775
5776/*
5777 * Pop an item from the regstack.
5778 */
5779 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005780regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005781 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005782{
5783 regitem_T *rp;
5784
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005785 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005786 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005787
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005788 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789}
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791/*
5792 * regrepeat - repeatedly match something simple, return how many.
5793 * Advances reginput (and reglnum) to just after the matched chars.
5794 */
5795 static int
5796regrepeat(p, maxcount)
5797 char_u *p;
5798 long maxcount; /* maximum number of matches allowed */
5799{
5800 long count = 0;
5801 char_u *scan;
5802 char_u *opnd;
5803 int mask;
5804 int testval = 0;
5805
5806 scan = reginput; /* Make local copy of reginput for speed. */
5807 opnd = OPERAND(p);
5808 switch (OP(p))
5809 {
5810 case ANY:
5811 case ANY + ADD_NL:
5812 while (count < maxcount)
5813 {
5814 /* Matching anything means we continue until end-of-line (or
5815 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5816 while (*scan != NUL && count < maxcount)
5817 {
5818 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005819 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005820 }
Bram Moolenaar640009d2006-10-17 16:48:26 +00005821 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5822 || reg_line_lbr || count == maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824 ++count; /* count the line-break */
5825 reg_nextline();
5826 scan = reginput;
5827 if (got_int)
5828 break;
5829 }
5830 break;
5831
5832 case IDENT:
5833 case IDENT + ADD_NL:
5834 testval = TRUE;
5835 /*FALLTHROUGH*/
5836 case SIDENT:
5837 case SIDENT + ADD_NL:
5838 while (count < maxcount)
5839 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005840 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005842 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
5844 else if (*scan == NUL)
5845 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005846 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5847 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 break;
5849 reg_nextline();
5850 scan = reginput;
5851 if (got_int)
5852 break;
5853 }
5854 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5855 ++scan;
5856 else
5857 break;
5858 ++count;
5859 }
5860 break;
5861
5862 case KWORD:
5863 case KWORD + ADD_NL:
5864 testval = TRUE;
5865 /*FALLTHROUGH*/
5866 case SKWORD:
5867 case SKWORD + ADD_NL:
5868 while (count < maxcount)
5869 {
Bram Moolenaarf813a182013-01-30 13:59:37 +01005870 if (vim_iswordp_buf(scan, reg_buf)
5871 && (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 Moolenaar640009d2006-10-17 16:48:26 +00005877 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5878 || 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 }
5885 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5886 ++scan;
5887 else
5888 break;
5889 ++count;
5890 }
5891 break;
5892
5893 case FNAME:
5894 case FNAME + ADD_NL:
5895 testval = TRUE;
5896 /*FALLTHROUGH*/
5897 case SFNAME:
5898 case SFNAME + ADD_NL:
5899 while (count < maxcount)
5900 {
Bram Moolenaar09ea9fc2013-05-21 00:03:02 +02005901 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005902 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005903 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 }
5905 else if (*scan == NUL)
5906 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005907 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5908 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005909 break;
5910 reg_nextline();
5911 scan = reginput;
5912 if (got_int)
5913 break;
5914 }
5915 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5916 ++scan;
5917 else
5918 break;
5919 ++count;
5920 }
5921 break;
5922
5923 case PRINT:
5924 case PRINT + ADD_NL:
5925 testval = TRUE;
5926 /*FALLTHROUGH*/
5927 case SPRINT:
5928 case SPRINT + ADD_NL:
5929 while (count < maxcount)
5930 {
5931 if (*scan == NUL)
5932 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005933 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5934 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 break;
5936 reg_nextline();
5937 scan = reginput;
5938 if (got_int)
5939 break;
5940 }
Bram Moolenaarac7c33e2013-07-21 17:06:00 +02005941 else if (vim_isprintc(PTR2CHAR(scan)) == 1
5942 && (testval || !VIM_ISDIGIT(*scan)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005944 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 }
5946 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5947 ++scan;
5948 else
5949 break;
5950 ++count;
5951 }
5952 break;
5953
5954 case WHITE:
5955 case WHITE + ADD_NL:
5956 testval = mask = RI_WHITE;
5957do_class:
5958 while (count < maxcount)
5959 {
5960#ifdef FEAT_MBYTE
5961 int l;
5962#endif
5963 if (*scan == NUL)
5964 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00005965 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
5966 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 break;
5968 reg_nextline();
5969 scan = reginput;
5970 if (got_int)
5971 break;
5972 }
5973#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005974 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005975 {
5976 if (testval != 0)
5977 break;
5978 scan += l;
5979 }
5980#endif
5981 else if ((class_tab[*scan] & mask) == testval)
5982 ++scan;
5983 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5984 ++scan;
5985 else
5986 break;
5987 ++count;
5988 }
5989 break;
5990
5991 case NWHITE:
5992 case NWHITE + ADD_NL:
5993 mask = RI_WHITE;
5994 goto do_class;
5995 case DIGIT:
5996 case DIGIT + ADD_NL:
5997 testval = mask = RI_DIGIT;
5998 goto do_class;
5999 case NDIGIT:
6000 case NDIGIT + ADD_NL:
6001 mask = RI_DIGIT;
6002 goto do_class;
6003 case HEX:
6004 case HEX + ADD_NL:
6005 testval = mask = RI_HEX;
6006 goto do_class;
6007 case NHEX:
6008 case NHEX + ADD_NL:
6009 mask = RI_HEX;
6010 goto do_class;
6011 case OCTAL:
6012 case OCTAL + ADD_NL:
6013 testval = mask = RI_OCTAL;
6014 goto do_class;
6015 case NOCTAL:
6016 case NOCTAL + ADD_NL:
6017 mask = RI_OCTAL;
6018 goto do_class;
6019 case WORD:
6020 case WORD + ADD_NL:
6021 testval = mask = RI_WORD;
6022 goto do_class;
6023 case NWORD:
6024 case NWORD + ADD_NL:
6025 mask = RI_WORD;
6026 goto do_class;
6027 case HEAD:
6028 case HEAD + ADD_NL:
6029 testval = mask = RI_HEAD;
6030 goto do_class;
6031 case NHEAD:
6032 case NHEAD + ADD_NL:
6033 mask = RI_HEAD;
6034 goto do_class;
6035 case ALPHA:
6036 case ALPHA + ADD_NL:
6037 testval = mask = RI_ALPHA;
6038 goto do_class;
6039 case NALPHA:
6040 case NALPHA + ADD_NL:
6041 mask = RI_ALPHA;
6042 goto do_class;
6043 case LOWER:
6044 case LOWER + ADD_NL:
6045 testval = mask = RI_LOWER;
6046 goto do_class;
6047 case NLOWER:
6048 case NLOWER + ADD_NL:
6049 mask = RI_LOWER;
6050 goto do_class;
6051 case UPPER:
6052 case UPPER + ADD_NL:
6053 testval = mask = RI_UPPER;
6054 goto do_class;
6055 case NUPPER:
6056 case NUPPER + ADD_NL:
6057 mask = RI_UPPER;
6058 goto do_class;
6059
6060 case EXACTLY:
6061 {
6062 int cu, cl;
6063
6064 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006065 * would have been used for it. It does handle single-byte
6066 * characters, such as latin1. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006067 if (ireg_ic)
6068 {
Bram Moolenaara245a5b2007-08-11 11:58:23 +00006069 cu = MB_TOUPPER(*opnd);
6070 cl = MB_TOLOWER(*opnd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 while (count < maxcount && (*scan == cu || *scan == cl))
6072 {
6073 count++;
6074 scan++;
6075 }
6076 }
6077 else
6078 {
6079 cu = *opnd;
6080 while (count < maxcount && *scan == cu)
6081 {
6082 count++;
6083 scan++;
6084 }
6085 }
6086 break;
6087 }
6088
6089#ifdef FEAT_MBYTE
6090 case MULTIBYTECODE:
6091 {
6092 int i, len, cf = 0;
6093
6094 /* Safety check (just in case 'encoding' was changed since
6095 * compiling the program). */
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006096 if ((len = (*mb_ptr2len)(opnd)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006097 {
6098 if (ireg_ic && enc_utf8)
6099 cf = utf_fold(utf_ptr2char(opnd));
6100 while (count < maxcount)
6101 {
6102 for (i = 0; i < len; ++i)
6103 if (opnd[i] != scan[i])
6104 break;
6105 if (i < len && (!ireg_ic || !enc_utf8
6106 || utf_fold(utf_ptr2char(scan)) != cf))
6107 break;
6108 scan += len;
6109 ++count;
6110 }
6111 }
6112 }
6113 break;
6114#endif
6115
6116 case ANYOF:
6117 case ANYOF + ADD_NL:
6118 testval = TRUE;
6119 /*FALLTHROUGH*/
6120
6121 case ANYBUT:
6122 case ANYBUT + ADD_NL:
6123 while (count < maxcount)
6124 {
6125#ifdef FEAT_MBYTE
6126 int len;
6127#endif
6128 if (*scan == NUL)
6129 {
Bram Moolenaar640009d2006-10-17 16:48:26 +00006130 if (!REG_MULTI || !WITH_NL(OP(p)) || reglnum > reg_maxline
6131 || reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 break;
6133 reg_nextline();
6134 scan = reginput;
6135 if (got_int)
6136 break;
6137 }
6138 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
6139 ++scan;
6140#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00006141 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 {
6143 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
6144 break;
6145 scan += len;
6146 }
6147#endif
6148 else
6149 {
6150 if ((cstrchr(opnd, *scan) == NULL) == testval)
6151 break;
6152 ++scan;
6153 }
6154 ++count;
6155 }
6156 break;
6157
6158 case NEWL:
6159 while (count < maxcount
Bram Moolenaar640009d2006-10-17 16:48:26 +00006160 && ((*scan == NUL && reglnum <= reg_maxline && !reg_line_lbr
6161 && REG_MULTI) || (*scan == '\n' && reg_line_lbr)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162 {
6163 count++;
6164 if (reg_line_lbr)
6165 ADVANCE_REGINPUT();
6166 else
6167 reg_nextline();
6168 scan = reginput;
6169 if (got_int)
6170 break;
6171 }
6172 break;
6173
6174 default: /* Oh dear. Called inappropriately. */
6175 EMSG(_(e_re_corr));
6176#ifdef DEBUG
6177 printf("Called regrepeat with op code %d\n", OP(p));
6178#endif
6179 break;
6180 }
6181
6182 reginput = scan;
6183
6184 return (int)count;
6185}
6186
6187/*
6188 * regnext - dig the "next" pointer out of a node
Bram Moolenaard3005802009-11-25 17:21:32 +00006189 * Returns NULL when calculating size, when there is no next item and when
6190 * there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006191 */
6192 static char_u *
6193regnext(p)
6194 char_u *p;
6195{
6196 int offset;
6197
Bram Moolenaard3005802009-11-25 17:21:32 +00006198 if (p == JUST_CALC_SIZE || reg_toolong)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 return NULL;
6200
6201 offset = NEXT(p);
6202 if (offset == 0)
6203 return NULL;
6204
Bram Moolenaar582fd852005-03-28 20:58:01 +00006205 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006206 return p - offset;
6207 else
6208 return p + offset;
6209}
6210
6211/*
6212 * Check the regexp program for its magic number.
6213 * Return TRUE if it's wrong.
6214 */
6215 static int
6216prog_magic_wrong()
6217{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006218 regprog_T *prog;
6219
6220 prog = REG_MULTI ? reg_mmatch->regprog : reg_match->regprog;
6221 if (prog->engine == &nfa_regengine)
6222 /* For NFA matcher we don't check the magic */
6223 return FALSE;
6224
6225 if (UCHARAT(((bt_regprog_T *)prog)->program) != REGMAGIC)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226 {
6227 EMSG(_(e_re_corr));
6228 return TRUE;
6229 }
6230 return FALSE;
6231}
6232
6233/*
6234 * Cleanup the subexpressions, if this wasn't done yet.
6235 * This construction is used to clear the subexpressions only when they are
6236 * used (to increase speed).
6237 */
6238 static void
6239cleanup_subexpr()
6240{
6241 if (need_clear_subexpr)
6242 {
6243 if (REG_MULTI)
6244 {
6245 /* Use 0xff to set lnum to -1 */
6246 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6247 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6248 }
6249 else
6250 {
6251 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
6252 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
6253 }
6254 need_clear_subexpr = FALSE;
6255 }
6256}
6257
6258#ifdef FEAT_SYN_HL
6259 static void
6260cleanup_zsubexpr()
6261{
6262 if (need_clear_zsubexpr)
6263 {
6264 if (REG_MULTI)
6265 {
6266 /* Use 0xff to set lnum to -1 */
6267 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6268 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
6269 }
6270 else
6271 {
6272 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
6273 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
6274 }
6275 need_clear_zsubexpr = FALSE;
6276 }
6277}
6278#endif
6279
6280/*
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006281 * Save the current subexpr to "bp", so that they can be restored
6282 * later by restore_subexpr().
6283 */
6284 static void
6285save_subexpr(bp)
6286 regbehind_T *bp;
6287{
6288 int i;
6289
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006290 /* When "need_clear_subexpr" is set we don't need to save the values, only
6291 * remember that this flag needs to be set again when restoring. */
6292 bp->save_need_clear_subexpr = need_clear_subexpr;
6293 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006294 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006295 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006296 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006297 if (REG_MULTI)
6298 {
6299 bp->save_start[i].se_u.pos = reg_startpos[i];
6300 bp->save_end[i].se_u.pos = reg_endpos[i];
6301 }
6302 else
6303 {
6304 bp->save_start[i].se_u.ptr = reg_startp[i];
6305 bp->save_end[i].se_u.ptr = reg_endp[i];
6306 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006307 }
6308 }
6309}
6310
6311/*
6312 * Restore the subexpr from "bp".
6313 */
6314 static void
6315restore_subexpr(bp)
6316 regbehind_T *bp;
6317{
6318 int i;
6319
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006320 /* Only need to restore saved values when they are not to be cleared. */
6321 need_clear_subexpr = bp->save_need_clear_subexpr;
6322 if (!need_clear_subexpr)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006323 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006324 for (i = 0; i < NSUBEXP; ++i)
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006325 {
Bram Moolenaarfde483c2008-06-15 12:21:50 +00006326 if (REG_MULTI)
6327 {
6328 reg_startpos[i] = bp->save_start[i].se_u.pos;
6329 reg_endpos[i] = bp->save_end[i].se_u.pos;
6330 }
6331 else
6332 {
6333 reg_startp[i] = bp->save_start[i].se_u.ptr;
6334 reg_endp[i] = bp->save_end[i].se_u.ptr;
6335 }
Bram Moolenaar34cbfdf2008-04-09 10:16:02 +00006336 }
6337 }
6338}
6339
6340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341 * Advance reglnum, regline and reginput to the next line.
6342 */
6343 static void
6344reg_nextline()
6345{
6346 regline = reg_getline(++reglnum);
6347 reginput = regline;
6348 fast_breakcheck();
6349}
6350
6351/*
6352 * Save the input line and position in a regsave_T.
6353 */
6354 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006355reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006356 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006357 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
6359 if (REG_MULTI)
6360 {
6361 save->rs_u.pos.col = (colnr_T)(reginput - regline);
6362 save->rs_u.pos.lnum = reglnum;
6363 }
6364 else
6365 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006366 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006367}
6368
6369/*
6370 * Restore the input line and position from a regsave_T.
6371 */
6372 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00006373reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006375 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006376{
6377 if (REG_MULTI)
6378 {
6379 if (reglnum != save->rs_u.pos.lnum)
6380 {
6381 /* only call reg_getline() when the line number changed to save
6382 * a bit of time */
6383 reglnum = save->rs_u.pos.lnum;
6384 regline = reg_getline(reglnum);
6385 }
6386 reginput = regline + save->rs_u.pos.col;
6387 }
6388 else
6389 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00006390 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391}
6392
6393/*
6394 * Return TRUE if current position is equal to saved position.
6395 */
6396 static int
6397reg_save_equal(save)
6398 regsave_T *save;
6399{
6400 if (REG_MULTI)
6401 return reglnum == save->rs_u.pos.lnum
6402 && reginput == regline + save->rs_u.pos.col;
6403 return reginput == save->rs_u.ptr;
6404}
6405
6406/*
6407 * Tentatively set the sub-expression start to the current position (after
6408 * calling regmatch() they will have changed). Need to save the existing
6409 * values for when there is no match.
6410 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
6411 * depending on REG_MULTI.
6412 */
6413 static void
6414save_se_multi(savep, posp)
6415 save_se_T *savep;
6416 lpos_T *posp;
6417{
6418 savep->se_u.pos = *posp;
6419 posp->lnum = reglnum;
6420 posp->col = (colnr_T)(reginput - regline);
6421}
6422
6423 static void
6424save_se_one(savep, pp)
6425 save_se_T *savep;
6426 char_u **pp;
6427{
6428 savep->se_u.ptr = *pp;
6429 *pp = reginput;
6430}
6431
6432/*
6433 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
6434 */
6435 static int
6436re_num_cmp(val, scan)
6437 long_u val;
6438 char_u *scan;
6439{
6440 long_u n = OPERAND_MIN(scan);
6441
6442 if (OPERAND_CMP(scan) == '>')
6443 return val > n;
6444 if (OPERAND_CMP(scan) == '<')
6445 return val < n;
6446 return val == n;
6447}
6448
Bram Moolenaar580abea2013-06-14 20:31:28 +02006449/*
6450 * Check whether a backreference matches.
6451 * Returns RA_FAIL, RA_NOMATCH or RA_MATCH.
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006452 * If "bytelen" is not NULL, it is set to the byte length of the match in the
6453 * last line.
Bram Moolenaar580abea2013-06-14 20:31:28 +02006454 */
6455 static int
6456match_with_backref(start_lnum, start_col, end_lnum, end_col, bytelen)
6457 linenr_T start_lnum;
6458 colnr_T start_col;
6459 linenr_T end_lnum;
6460 colnr_T end_col;
6461 int *bytelen;
6462{
6463 linenr_T clnum = start_lnum;
6464 colnr_T ccol = start_col;
6465 int len;
6466 char_u *p;
6467
6468 if (bytelen != NULL)
6469 *bytelen = 0;
6470 for (;;)
6471 {
6472 /* Since getting one line may invalidate the other, need to make copy.
6473 * Slow! */
6474 if (regline != reg_tofree)
6475 {
6476 len = (int)STRLEN(regline);
6477 if (reg_tofree == NULL || len >= (int)reg_tofreelen)
6478 {
6479 len += 50; /* get some extra */
6480 vim_free(reg_tofree);
6481 reg_tofree = alloc(len);
6482 if (reg_tofree == NULL)
6483 return RA_FAIL; /* out of memory!*/
6484 reg_tofreelen = len;
6485 }
6486 STRCPY(reg_tofree, regline);
6487 reginput = reg_tofree + (reginput - regline);
6488 regline = reg_tofree;
6489 }
6490
6491 /* Get the line to compare with. */
6492 p = reg_getline(clnum);
6493 if (clnum == end_lnum)
6494 len = end_col - ccol;
6495 else
6496 len = (int)STRLEN(p + ccol);
6497
6498 if (cstrncmp(p + ccol, reginput, &len) != 0)
6499 return RA_NOMATCH; /* doesn't match */
6500 if (bytelen != NULL)
6501 *bytelen += len;
6502 if (clnum == end_lnum)
6503 break; /* match and at end! */
6504 if (reglnum >= reg_maxline)
6505 return RA_NOMATCH; /* text too short */
6506
6507 /* Advance to next line. */
6508 reg_nextline();
Bram Moolenaar438ee5b2013-11-21 17:13:00 +01006509 if (bytelen != NULL)
6510 *bytelen = 0;
Bram Moolenaar580abea2013-06-14 20:31:28 +02006511 ++clnum;
6512 ccol = 0;
6513 if (got_int)
6514 return RA_FAIL;
6515 }
6516
6517 /* found a match! Note that regline may now point to a copy of the line,
6518 * that should not matter. */
6519 return RA_MATCH;
6520}
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006522#ifdef BT_REGEXP_DUMP
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523
6524/*
6525 * regdump - dump a regexp onto stdout in vaguely comprehensible form
6526 */
6527 static void
6528regdump(pattern, r)
6529 char_u *pattern;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006530 bt_regprog_T *r;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531{
6532 char_u *s;
6533 int op = EXACTLY; /* Arbitrary non-END op. */
6534 char_u *next;
6535 char_u *end = NULL;
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006536 FILE *f;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006538#ifdef BT_REGEXP_LOG
6539 f = fopen("bt_regexp_log.log", "a");
6540#else
6541 f = stdout;
6542#endif
6543 if (f == NULL)
6544 return;
6545 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006546
6547 s = r->program + 1;
6548 /*
6549 * Loop until we find the END that isn't before a referred next (an END
6550 * can also appear in a NOMATCH operand).
6551 */
6552 while (op != END || s <= end)
6553 {
6554 op = OP(s);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006555 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006556 next = regnext(s);
6557 if (next == NULL) /* Next ptr. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006558 fprintf(f, "(0)");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 else
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006560 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 if (end < next)
6562 end = next;
6563 if (op == BRACE_LIMITS)
6564 {
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006565 /* Two ints */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006566 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 s += 8;
6568 }
Bram Moolenaar5b84ddc2013-06-05 16:33:10 +02006569 else if (op == BEHIND || op == NOBEHIND)
6570 {
6571 /* one int */
6572 fprintf(f, " count %ld", OPERAND_MIN(s));
6573 s += 4;
6574 }
Bram Moolenaar6d3a5d72013-06-06 18:04:51 +02006575 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
6576 {
6577 /* one int plus comperator */
6578 fprintf(f, " count %ld", OPERAND_MIN(s));
6579 s += 5;
6580 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581 s += 3;
6582 if (op == ANYOF || op == ANYOF + ADD_NL
6583 || op == ANYBUT || op == ANYBUT + ADD_NL
6584 || op == EXACTLY)
6585 {
6586 /* Literal string, where present. */
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006587 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 while (*s != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006589 fprintf(f, "%c", *s++);
6590 fprintf(f, "\nxxxxxxxxx\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 s++;
6592 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006593 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594 }
6595
6596 /* Header fields of interest. */
6597 if (r->regstart != NUL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006598 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599 ? (char *)transchar(r->regstart)
6600 : "multibyte", r->regstart);
6601 if (r->reganch)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006602 fprintf(f, "anchored; ");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 if (r->regmust != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006604 fprintf(f, "must have \"%s\"", r->regmust);
6605 fprintf(f, "\r\n");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006607#ifdef BT_REGEXP_LOG
6608 fclose(f);
6609#endif
6610}
6611#endif /* BT_REGEXP_DUMP */
6612
6613#ifdef DEBUG
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614/*
6615 * regprop - printable representation of opcode
6616 */
6617 static char_u *
6618regprop(op)
6619 char_u *op;
6620{
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006621 char *p;
6622 static char buf[50];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006624 STRCPY(buf, ":");
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006626 switch ((int) OP(op))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 {
6628 case BOL:
6629 p = "BOL";
6630 break;
6631 case EOL:
6632 p = "EOL";
6633 break;
6634 case RE_BOF:
6635 p = "BOF";
6636 break;
6637 case RE_EOF:
6638 p = "EOF";
6639 break;
6640 case CURSOR:
6641 p = "CURSOR";
6642 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006643 case RE_VISUAL:
6644 p = "RE_VISUAL";
6645 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 case RE_LNUM:
6647 p = "RE_LNUM";
6648 break;
Bram Moolenaar71fe80d2006-01-22 23:25:56 +00006649 case RE_MARK:
6650 p = "RE_MARK";
6651 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 case RE_COL:
6653 p = "RE_COL";
6654 break;
6655 case RE_VCOL:
6656 p = "RE_VCOL";
6657 break;
6658 case BOW:
6659 p = "BOW";
6660 break;
6661 case EOW:
6662 p = "EOW";
6663 break;
6664 case ANY:
6665 p = "ANY";
6666 break;
6667 case ANY + ADD_NL:
6668 p = "ANY+NL";
6669 break;
6670 case ANYOF:
6671 p = "ANYOF";
6672 break;
6673 case ANYOF + ADD_NL:
6674 p = "ANYOF+NL";
6675 break;
6676 case ANYBUT:
6677 p = "ANYBUT";
6678 break;
6679 case ANYBUT + ADD_NL:
6680 p = "ANYBUT+NL";
6681 break;
6682 case IDENT:
6683 p = "IDENT";
6684 break;
6685 case IDENT + ADD_NL:
6686 p = "IDENT+NL";
6687 break;
6688 case SIDENT:
6689 p = "SIDENT";
6690 break;
6691 case SIDENT + ADD_NL:
6692 p = "SIDENT+NL";
6693 break;
6694 case KWORD:
6695 p = "KWORD";
6696 break;
6697 case KWORD + ADD_NL:
6698 p = "KWORD+NL";
6699 break;
6700 case SKWORD:
6701 p = "SKWORD";
6702 break;
6703 case SKWORD + ADD_NL:
6704 p = "SKWORD+NL";
6705 break;
6706 case FNAME:
6707 p = "FNAME";
6708 break;
6709 case FNAME + ADD_NL:
6710 p = "FNAME+NL";
6711 break;
6712 case SFNAME:
6713 p = "SFNAME";
6714 break;
6715 case SFNAME + ADD_NL:
6716 p = "SFNAME+NL";
6717 break;
6718 case PRINT:
6719 p = "PRINT";
6720 break;
6721 case PRINT + ADD_NL:
6722 p = "PRINT+NL";
6723 break;
6724 case SPRINT:
6725 p = "SPRINT";
6726 break;
6727 case SPRINT + ADD_NL:
6728 p = "SPRINT+NL";
6729 break;
6730 case WHITE:
6731 p = "WHITE";
6732 break;
6733 case WHITE + ADD_NL:
6734 p = "WHITE+NL";
6735 break;
6736 case NWHITE:
6737 p = "NWHITE";
6738 break;
6739 case NWHITE + ADD_NL:
6740 p = "NWHITE+NL";
6741 break;
6742 case DIGIT:
6743 p = "DIGIT";
6744 break;
6745 case DIGIT + ADD_NL:
6746 p = "DIGIT+NL";
6747 break;
6748 case NDIGIT:
6749 p = "NDIGIT";
6750 break;
6751 case NDIGIT + ADD_NL:
6752 p = "NDIGIT+NL";
6753 break;
6754 case HEX:
6755 p = "HEX";
6756 break;
6757 case HEX + ADD_NL:
6758 p = "HEX+NL";
6759 break;
6760 case NHEX:
6761 p = "NHEX";
6762 break;
6763 case NHEX + ADD_NL:
6764 p = "NHEX+NL";
6765 break;
6766 case OCTAL:
6767 p = "OCTAL";
6768 break;
6769 case OCTAL + ADD_NL:
6770 p = "OCTAL+NL";
6771 break;
6772 case NOCTAL:
6773 p = "NOCTAL";
6774 break;
6775 case NOCTAL + ADD_NL:
6776 p = "NOCTAL+NL";
6777 break;
6778 case WORD:
6779 p = "WORD";
6780 break;
6781 case WORD + ADD_NL:
6782 p = "WORD+NL";
6783 break;
6784 case NWORD:
6785 p = "NWORD";
6786 break;
6787 case NWORD + ADD_NL:
6788 p = "NWORD+NL";
6789 break;
6790 case HEAD:
6791 p = "HEAD";
6792 break;
6793 case HEAD + ADD_NL:
6794 p = "HEAD+NL";
6795 break;
6796 case NHEAD:
6797 p = "NHEAD";
6798 break;
6799 case NHEAD + ADD_NL:
6800 p = "NHEAD+NL";
6801 break;
6802 case ALPHA:
6803 p = "ALPHA";
6804 break;
6805 case ALPHA + ADD_NL:
6806 p = "ALPHA+NL";
6807 break;
6808 case NALPHA:
6809 p = "NALPHA";
6810 break;
6811 case NALPHA + ADD_NL:
6812 p = "NALPHA+NL";
6813 break;
6814 case LOWER:
6815 p = "LOWER";
6816 break;
6817 case LOWER + ADD_NL:
6818 p = "LOWER+NL";
6819 break;
6820 case NLOWER:
6821 p = "NLOWER";
6822 break;
6823 case NLOWER + ADD_NL:
6824 p = "NLOWER+NL";
6825 break;
6826 case UPPER:
6827 p = "UPPER";
6828 break;
6829 case UPPER + ADD_NL:
6830 p = "UPPER+NL";
6831 break;
6832 case NUPPER:
6833 p = "NUPPER";
6834 break;
6835 case NUPPER + ADD_NL:
6836 p = "NUPPER+NL";
6837 break;
6838 case BRANCH:
6839 p = "BRANCH";
6840 break;
6841 case EXACTLY:
6842 p = "EXACTLY";
6843 break;
6844 case NOTHING:
6845 p = "NOTHING";
6846 break;
6847 case BACK:
6848 p = "BACK";
6849 break;
6850 case END:
6851 p = "END";
6852 break;
6853 case MOPEN + 0:
6854 p = "MATCH START";
6855 break;
6856 case MOPEN + 1:
6857 case MOPEN + 2:
6858 case MOPEN + 3:
6859 case MOPEN + 4:
6860 case MOPEN + 5:
6861 case MOPEN + 6:
6862 case MOPEN + 7:
6863 case MOPEN + 8:
6864 case MOPEN + 9:
6865 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
6866 p = NULL;
6867 break;
6868 case MCLOSE + 0:
6869 p = "MATCH END";
6870 break;
6871 case MCLOSE + 1:
6872 case MCLOSE + 2:
6873 case MCLOSE + 3:
6874 case MCLOSE + 4:
6875 case MCLOSE + 5:
6876 case MCLOSE + 6:
6877 case MCLOSE + 7:
6878 case MCLOSE + 8:
6879 case MCLOSE + 9:
6880 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
6881 p = NULL;
6882 break;
6883 case BACKREF + 1:
6884 case BACKREF + 2:
6885 case BACKREF + 3:
6886 case BACKREF + 4:
6887 case BACKREF + 5:
6888 case BACKREF + 6:
6889 case BACKREF + 7:
6890 case BACKREF + 8:
6891 case BACKREF + 9:
6892 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
6893 p = NULL;
6894 break;
6895 case NOPEN:
6896 p = "NOPEN";
6897 break;
6898 case NCLOSE:
6899 p = "NCLOSE";
6900 break;
6901#ifdef FEAT_SYN_HL
6902 case ZOPEN + 1:
6903 case ZOPEN + 2:
6904 case ZOPEN + 3:
6905 case ZOPEN + 4:
6906 case ZOPEN + 5:
6907 case ZOPEN + 6:
6908 case ZOPEN + 7:
6909 case ZOPEN + 8:
6910 case ZOPEN + 9:
6911 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6912 p = NULL;
6913 break;
6914 case ZCLOSE + 1:
6915 case ZCLOSE + 2:
6916 case ZCLOSE + 3:
6917 case ZCLOSE + 4:
6918 case ZCLOSE + 5:
6919 case ZCLOSE + 6:
6920 case ZCLOSE + 7:
6921 case ZCLOSE + 8:
6922 case ZCLOSE + 9:
6923 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6924 p = NULL;
6925 break;
6926 case ZREF + 1:
6927 case ZREF + 2:
6928 case ZREF + 3:
6929 case ZREF + 4:
6930 case ZREF + 5:
6931 case ZREF + 6:
6932 case ZREF + 7:
6933 case ZREF + 8:
6934 case ZREF + 9:
6935 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6936 p = NULL;
6937 break;
6938#endif
6939 case STAR:
6940 p = "STAR";
6941 break;
6942 case PLUS:
6943 p = "PLUS";
6944 break;
6945 case NOMATCH:
6946 p = "NOMATCH";
6947 break;
6948 case MATCH:
6949 p = "MATCH";
6950 break;
6951 case BEHIND:
6952 p = "BEHIND";
6953 break;
6954 case NOBEHIND:
6955 p = "NOBEHIND";
6956 break;
6957 case SUBPAT:
6958 p = "SUBPAT";
6959 break;
6960 case BRACE_LIMITS:
6961 p = "BRACE_LIMITS";
6962 break;
6963 case BRACE_SIMPLE:
6964 p = "BRACE_SIMPLE";
6965 break;
6966 case BRACE_COMPLEX + 0:
6967 case BRACE_COMPLEX + 1:
6968 case BRACE_COMPLEX + 2:
6969 case BRACE_COMPLEX + 3:
6970 case BRACE_COMPLEX + 4:
6971 case BRACE_COMPLEX + 5:
6972 case BRACE_COMPLEX + 6:
6973 case BRACE_COMPLEX + 7:
6974 case BRACE_COMPLEX + 8:
6975 case BRACE_COMPLEX + 9:
6976 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6977 p = NULL;
6978 break;
6979#ifdef FEAT_MBYTE
6980 case MULTIBYTECODE:
6981 p = "MULTIBYTECODE";
6982 break;
6983#endif
6984 case NEWL:
6985 p = "NEWL";
6986 break;
6987 default:
6988 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6989 p = NULL;
6990 break;
6991 }
6992 if (p != NULL)
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006993 STRCAT(buf, p);
6994 return (char_u *)buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006995}
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02006996#endif /* DEBUG */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
6998#ifdef FEAT_MBYTE
6999static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
7000
7001typedef struct
7002{
7003 int a, b, c;
7004} decomp_T;
7005
7006
7007/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00007008static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009{
7010 {0x5e2,0,0}, /* 0xfb20 alt ayin */
7011 {0x5d0,0,0}, /* 0xfb21 alt alef */
7012 {0x5d3,0,0}, /* 0xfb22 alt dalet */
7013 {0x5d4,0,0}, /* 0xfb23 alt he */
7014 {0x5db,0,0}, /* 0xfb24 alt kaf */
7015 {0x5dc,0,0}, /* 0xfb25 alt lamed */
7016 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
7017 {0x5e8,0,0}, /* 0xfb27 alt resh */
7018 {0x5ea,0,0}, /* 0xfb28 alt tav */
7019 {'+', 0, 0}, /* 0xfb29 alt plus */
7020 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
7021 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
7022 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
7023 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
7024 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
7025 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
7026 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
7027 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
7028 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
7029 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
7030 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
7031 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
7032 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
7033 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
7034 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
7035 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
7036 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
7037 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
7038 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
7039 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
7040 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
7041 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
7042 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
7043 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
7044 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
7045 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
7046 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
7047 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
7048 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
7049 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
7050 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
7051 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
7052 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
7053 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
7054 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
7055 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
7056 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
7057 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
7058};
7059
7060 static void
7061mb_decompose(c, c1, c2, c3)
7062 int c, *c1, *c2, *c3;
7063{
7064 decomp_T d;
7065
Bram Moolenaar2eec59e2013-05-21 21:37:20 +02007066 if (c >= 0xfb20 && c <= 0xfb4f)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 {
7068 d = decomp_table[c - 0xfb20];
7069 *c1 = d.a;
7070 *c2 = d.b;
7071 *c3 = d.c;
7072 }
7073 else
7074 {
7075 *c1 = c;
7076 *c2 = *c3 = 0;
7077 }
7078}
7079#endif
7080
7081/*
7082 * Compare two strings, ignore case if ireg_ic set.
7083 * Return 0 if strings match, non-zero otherwise.
7084 * Correct the length "*n" when composing characters are ignored.
7085 */
7086 static int
7087cstrncmp(s1, s2, n)
7088 char_u *s1, *s2;
7089 int *n;
7090{
7091 int result;
7092
7093 if (!ireg_ic)
7094 result = STRNCMP(s1, s2, *n);
7095 else
7096 result = MB_STRNICMP(s1, s2, *n);
7097
7098#ifdef FEAT_MBYTE
7099 /* if it failed and it's utf8 and we want to combineignore: */
7100 if (result != 0 && enc_utf8 && ireg_icombine)
7101 {
7102 char_u *str1, *str2;
7103 int c1, c2, c11, c12;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 int junk;
7105
7106 /* we have to handle the strcmp ourselves, since it is necessary to
7107 * deal with the composing characters by ignoring them: */
7108 str1 = s1;
7109 str2 = s2;
7110 c1 = c2 = 0;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00007111 while ((int)(str1 - s1) < *n)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007112 {
7113 c1 = mb_ptr2char_adv(&str1);
7114 c2 = mb_ptr2char_adv(&str2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007115
7116 /* decompose the character if necessary, into 'base' characters
7117 * because I don't care about Arabic, I will hard-code the Hebrew
7118 * which I *do* care about! So sue me... */
7119 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
7120 {
7121 /* decomposition necessary? */
7122 mb_decompose(c1, &c11, &junk, &junk);
7123 mb_decompose(c2, &c12, &junk, &junk);
7124 c1 = c11;
7125 c2 = c12;
7126 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
7127 break;
7128 }
7129 }
7130 result = c2 - c1;
7131 if (result == 0)
7132 *n = (int)(str2 - s2);
7133 }
7134#endif
7135
7136 return result;
7137}
7138
7139/*
7140 * cstrchr: This function is used a lot for simple searches, keep it fast!
7141 */
7142 static char_u *
7143cstrchr(s, c)
7144 char_u *s;
7145 int c;
7146{
7147 char_u *p;
7148 int cc;
7149
7150 if (!ireg_ic
7151#ifdef FEAT_MBYTE
7152 || (!enc_utf8 && mb_char2len(c) > 1)
7153#endif
7154 )
7155 return vim_strchr(s, c);
7156
7157 /* tolower() and toupper() can be slow, comparing twice should be a lot
7158 * faster (esp. when using MS Visual C++!).
7159 * For UTF-8 need to use folded case. */
7160#ifdef FEAT_MBYTE
7161 if (enc_utf8 && c > 0x80)
7162 cc = utf_fold(c);
7163 else
7164#endif
Bram Moolenaara245a5b2007-08-11 11:58:23 +00007165 if (MB_ISUPPER(c))
7166 cc = MB_TOLOWER(c);
7167 else if (MB_ISLOWER(c))
7168 cc = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 else
7170 return vim_strchr(s, c);
7171
7172#ifdef FEAT_MBYTE
7173 if (has_mbyte)
7174 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007175 for (p = s; *p != NUL; p += (*mb_ptr2len)(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176 {
7177 if (enc_utf8 && c > 0x80)
7178 {
7179 if (utf_fold(utf_ptr2char(p)) == cc)
7180 return p;
7181 }
7182 else if (*p == c || *p == cc)
7183 return p;
7184 }
7185 }
7186 else
7187#endif
7188 /* Faster version for when there are no multi-byte characters. */
7189 for (p = s; *p != NUL; ++p)
7190 if (*p == c || *p == cc)
7191 return p;
7192
7193 return NULL;
7194}
7195
7196/***************************************************************
7197 * regsub stuff *
7198 ***************************************************************/
7199
7200/* This stuff below really confuses cc on an SGI -- webb */
7201#ifdef __sgi
7202# undef __ARGS
7203# define __ARGS(x) ()
7204#endif
7205
7206/*
7207 * We should define ftpr as a pointer to a function returning a pointer to
7208 * a function returning a pointer to a function ...
7209 * This is impossible, so we declare a pointer to a function returning a
7210 * pointer to a function returning void. This should work for all compilers.
7211 */
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007212typedef void (*(*fptr_T) __ARGS((int *, int)))();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007214static fptr_T do_upper __ARGS((int *, int));
7215static fptr_T do_Upper __ARGS((int *, int));
7216static fptr_T do_lower __ARGS((int *, int));
7217static fptr_T do_Lower __ARGS((int *, int));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218
7219static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
7220
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007221 static fptr_T
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222do_upper(d, c)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007223 int *d;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224 int c;
7225{
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007226 *d = MB_TOUPPER(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007228 return (fptr_T)NULL;
7229}
7230
7231 static fptr_T
7232do_Upper(d, c)
7233 int *d;
7234 int c;
7235{
7236 *d = MB_TOUPPER(c);
7237
7238 return (fptr_T)do_Upper;
7239}
7240
7241 static fptr_T
7242do_lower(d, c)
7243 int *d;
7244 int c;
7245{
7246 *d = MB_TOLOWER(c);
7247
7248 return (fptr_T)NULL;
7249}
7250
7251 static fptr_T
7252do_Lower(d, c)
7253 int *d;
7254 int c;
7255{
7256 *d = MB_TOLOWER(c);
7257
7258 return (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007259}
7260
7261/*
7262 * regtilde(): Replace tildes in the pattern by the old pattern.
7263 *
7264 * Short explanation of the tilde: It stands for the previous replacement
7265 * pattern. If that previous pattern also contains a ~ we should go back a
7266 * step further... But we insert the previous pattern into the current one
7267 * and remember that.
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007268 * This still does not handle the case where "magic" changes. So require the
7269 * user to keep his hands off of "magic".
Bram Moolenaar071d4272004-06-13 20:20:40 +00007270 *
7271 * The tildes are parsed once before the first call to vim_regsub().
7272 */
7273 char_u *
7274regtilde(source, magic)
7275 char_u *source;
7276 int magic;
7277{
7278 char_u *newsub = source;
7279 char_u *tmpsub;
7280 char_u *p;
7281 int len;
7282 int prevlen;
7283
7284 for (p = newsub; *p; ++p)
7285 {
7286 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
7287 {
7288 if (reg_prev_sub != NULL)
7289 {
7290 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
7291 prevlen = (int)STRLEN(reg_prev_sub);
7292 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
7293 if (tmpsub != NULL)
7294 {
7295 /* copy prefix */
7296 len = (int)(p - newsub); /* not including ~ */
7297 mch_memmove(tmpsub, newsub, (size_t)len);
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007298 /* interpret tilde */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007299 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
7300 /* copy postfix */
7301 if (!magic)
7302 ++p; /* back off \ */
7303 STRCPY(tmpsub + len + prevlen, p + 1);
7304
7305 if (newsub != source) /* already allocated newsub */
7306 vim_free(newsub);
7307 newsub = tmpsub;
7308 p = newsub + len + prevlen;
7309 }
7310 }
7311 else if (magic)
Bram Moolenaar446cb832008-06-24 21:56:24 +00007312 STRMOVE(p, p + 1); /* remove '~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007313 else
Bram Moolenaar446cb832008-06-24 21:56:24 +00007314 STRMOVE(p, p + 2); /* remove '\~' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315 --p;
7316 }
7317 else
7318 {
7319 if (*p == '\\' && p[1]) /* skip escaped characters */
7320 ++p;
7321#ifdef FEAT_MBYTE
7322 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007323 p += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324#endif
7325 }
7326 }
7327
7328 vim_free(reg_prev_sub);
7329 if (newsub != source) /* newsub was allocated, just keep it */
7330 reg_prev_sub = newsub;
7331 else /* no ~ found, need to save newsub */
7332 reg_prev_sub = vim_strsave(newsub);
7333 return newsub;
7334}
7335
7336#ifdef FEAT_EVAL
7337static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
7338
7339/* These pointers are used instead of reg_match and reg_mmatch for
7340 * reg_submatch(). Needed for when the substitution string is an expression
7341 * that contains a call to substitute() and submatch(). */
7342static regmatch_T *submatch_match;
7343static regmmatch_T *submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007344static linenr_T submatch_firstlnum;
7345static linenr_T submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007346static int submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347#endif
7348
7349#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
7350/*
7351 * vim_regsub() - perform substitutions after a vim_regexec() or
7352 * vim_regexec_multi() match.
7353 *
7354 * If "copy" is TRUE really copy into "dest".
7355 * If "copy" is FALSE nothing is copied, this is just to find out the length
7356 * of the result.
7357 *
7358 * If "backslash" is TRUE, a backslash will be removed later, need to double
7359 * them to keep them, and insert a backslash before a CR to avoid it being
7360 * replaced with a line break later.
7361 *
7362 * Note: The matched text must not change between the call of
7363 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
7364 * references invalid!
7365 *
7366 * Returns the size of the replacement, including terminating NUL.
7367 */
7368 int
7369vim_regsub(rmp, source, dest, copy, magic, backslash)
7370 regmatch_T *rmp;
7371 char_u *source;
7372 char_u *dest;
7373 int copy;
7374 int magic;
7375 int backslash;
7376{
7377 reg_match = rmp;
7378 reg_mmatch = NULL;
7379 reg_maxline = 0;
Bram Moolenaar2f315ab2013-01-25 20:11:01 +01007380 reg_buf = curbuf;
Bram Moolenaar93fc4812014-04-23 18:48:47 +02007381 reg_line_lbr = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 return vim_regsub_both(source, dest, copy, magic, backslash);
7383}
7384#endif
7385
7386 int
7387vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
7388 regmmatch_T *rmp;
7389 linenr_T lnum;
7390 char_u *source;
7391 char_u *dest;
7392 int copy;
7393 int magic;
7394 int backslash;
7395{
7396 reg_match = NULL;
7397 reg_mmatch = rmp;
7398 reg_buf = curbuf; /* always works on the current buffer! */
7399 reg_firstlnum = lnum;
7400 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
Bram Moolenaar93fc4812014-04-23 18:48:47 +02007401 reg_line_lbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402 return vim_regsub_both(source, dest, copy, magic, backslash);
7403}
7404
7405 static int
7406vim_regsub_both(source, dest, copy, magic, backslash)
7407 char_u *source;
7408 char_u *dest;
7409 int copy;
7410 int magic;
7411 int backslash;
7412{
7413 char_u *src;
7414 char_u *dst;
7415 char_u *s;
7416 int c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007417 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007418 int no = -1;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007419 fptr_T func_all = (fptr_T)NULL;
7420 fptr_T func_one = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421 linenr_T clnum = 0; /* init for GCC */
7422 int len = 0; /* init for GCC */
7423#ifdef FEAT_EVAL
7424 static char_u *eval_result = NULL;
7425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007426
7427 /* Be paranoid... */
7428 if (source == NULL || dest == NULL)
7429 {
7430 EMSG(_(e_null));
7431 return 0;
7432 }
7433 if (prog_magic_wrong())
7434 return 0;
7435 src = source;
7436 dst = dest;
7437
7438 /*
7439 * When the substitute part starts with "\=" evaluate it as an expression.
7440 */
7441 if (source[0] == '\\' && source[1] == '='
7442#ifdef FEAT_EVAL
7443 && !can_f_submatch /* can't do this recursively */
7444#endif
7445 )
7446 {
7447#ifdef FEAT_EVAL
7448 /* To make sure that the length doesn't change between checking the
7449 * length and copying the string, and to speed up things, the
7450 * resulting string is saved from the call with "copy" == FALSE to the
7451 * call with "copy" == TRUE. */
7452 if (copy)
7453 {
7454 if (eval_result != NULL)
7455 {
7456 STRCPY(dest, eval_result);
7457 dst += STRLEN(eval_result);
7458 vim_free(eval_result);
7459 eval_result = NULL;
7460 }
7461 }
7462 else
7463 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007464 win_T *save_reg_win;
7465 int save_ireg_ic;
7466
7467 vim_free(eval_result);
7468
7469 /* The expression may contain substitute(), which calls us
7470 * recursively. Make sure submatch() gets the text from the first
7471 * level. Don't need to save "reg_buf", because
7472 * vim_regexec_multi() can't be called recursively. */
7473 submatch_match = reg_match;
7474 submatch_mmatch = reg_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007475 submatch_firstlnum = reg_firstlnum;
7476 submatch_maxline = reg_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007477 submatch_line_lbr = reg_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478 save_reg_win = reg_win;
7479 save_ireg_ic = ireg_ic;
7480 can_f_submatch = TRUE;
7481
Bram Moolenaar362e1a32006-03-06 23:29:24 +00007482 eval_result = eval_to_string(source + 2, NULL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483 if (eval_result != NULL)
7484 {
Bram Moolenaar06975a42010-03-23 16:27:22 +01007485 int had_backslash = FALSE;
7486
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00007487 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007488 {
Bram Moolenaar978287b2011-06-19 04:32:15 +02007489 /* Change NL to CR, so that it becomes a line break,
7490 * unless called from vim_regexec_nl().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007491 * Skip over a backslashed character. */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007492 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007493 *s = CAR;
7494 else if (*s == '\\' && s[1] != NUL)
Bram Moolenaar06975a42010-03-23 16:27:22 +01007495 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00007496 ++s;
Bram Moolenaar60190782010-05-21 13:08:58 +02007497 /* Change NL to CR here too, so that this works:
7498 * :s/abc\\\ndef/\="aaa\\\nbbb"/ on text:
7499 * abc\
7500 * def
Bram Moolenaar978287b2011-06-19 04:32:15 +02007501 * Not when called from vim_regexec_nl().
Bram Moolenaar60190782010-05-21 13:08:58 +02007502 */
Bram Moolenaar978287b2011-06-19 04:32:15 +02007503 if (*s == NL && !submatch_line_lbr)
Bram Moolenaar60190782010-05-21 13:08:58 +02007504 *s = CAR;
Bram Moolenaar06975a42010-03-23 16:27:22 +01007505 had_backslash = TRUE;
7506 }
7507 }
7508 if (had_backslash && backslash)
7509 {
7510 /* Backslashes will be consumed, need to double them. */
7511 s = vim_strsave_escaped(eval_result, (char_u *)"\\");
7512 if (s != NULL)
7513 {
7514 vim_free(eval_result);
7515 eval_result = s;
7516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517 }
7518
7519 dst += STRLEN(eval_result);
7520 }
7521
7522 reg_match = submatch_match;
7523 reg_mmatch = submatch_mmatch;
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007524 reg_firstlnum = submatch_firstlnum;
7525 reg_maxline = submatch_maxline;
Bram Moolenaar978287b2011-06-19 04:32:15 +02007526 reg_line_lbr = submatch_line_lbr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007527 reg_win = save_reg_win;
7528 ireg_ic = save_ireg_ic;
7529 can_f_submatch = FALSE;
7530 }
7531#endif
7532 }
7533 else
7534 while ((c = *src++) != NUL)
7535 {
7536 if (c == '&' && magic)
7537 no = 0;
7538 else if (c == '\\' && *src != NUL)
7539 {
7540 if (*src == '&' && !magic)
7541 {
7542 ++src;
7543 no = 0;
7544 }
7545 else if ('0' <= *src && *src <= '9')
7546 {
7547 no = *src++ - '0';
7548 }
7549 else if (vim_strchr((char_u *)"uUlLeE", *src))
7550 {
7551 switch (*src++)
7552 {
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007553 case 'u': func_one = (fptr_T)do_upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007554 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007555 case 'U': func_all = (fptr_T)do_Upper;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007557 case 'l': func_one = (fptr_T)do_lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007558 continue;
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007559 case 'L': func_all = (fptr_T)do_Lower;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007560 continue;
7561 case 'e':
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007562 case 'E': func_one = func_all = (fptr_T)NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007563 continue;
7564 }
7565 }
7566 }
7567 if (no < 0) /* Ordinary character. */
7568 {
Bram Moolenaardb552d602006-03-23 22:59:57 +00007569 if (c == K_SPECIAL && src[0] != NUL && src[1] != NUL)
7570 {
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007571 /* Copy a special key as-is. */
Bram Moolenaardb552d602006-03-23 22:59:57 +00007572 if (copy)
7573 {
7574 *dst++ = c;
7575 *dst++ = *src++;
7576 *dst++ = *src++;
7577 }
7578 else
7579 {
7580 dst += 3;
7581 src += 2;
7582 }
7583 continue;
7584 }
7585
Bram Moolenaar071d4272004-06-13 20:20:40 +00007586 if (c == '\\' && *src != NUL)
7587 {
7588 /* Check for abbreviations -- webb */
7589 switch (*src)
7590 {
7591 case 'r': c = CAR; ++src; break;
7592 case 'n': c = NL; ++src; break;
7593 case 't': c = TAB; ++src; break;
7594 /* Oh no! \e already has meaning in subst pat :-( */
7595 /* case 'e': c = ESC; ++src; break; */
7596 case 'b': c = Ctrl_H; ++src; break;
7597
7598 /* If "backslash" is TRUE the backslash will be removed
7599 * later. Used to insert a literal CR. */
7600 default: if (backslash)
7601 {
7602 if (copy)
7603 *dst = '\\';
7604 ++dst;
7605 }
7606 c = *src++;
7607 }
7608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609#ifdef FEAT_MBYTE
Bram Moolenaardb552d602006-03-23 22:59:57 +00007610 else if (has_mbyte)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007611 c = mb_ptr2char(src - 1);
7612#endif
7613
Bram Moolenaardb552d602006-03-23 22:59:57 +00007614 /* Write to buffer, if copy is set. */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007615 if (func_one != (fptr_T)NULL)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007616 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007617 func_one = (fptr_T)(func_one(&cc, c));
7618 else if (func_all != (fptr_T)NULL)
7619 /* Turbo C complains without the typecast */
7620 func_all = (fptr_T)(func_all(&cc, c));
7621 else /* just copy */
7622 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007623
7624#ifdef FEAT_MBYTE
7625 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007626 {
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007627 int totlen = mb_ptr2len(src - 1);
7628
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007630 mb_char2bytes(cc, dst);
7631 dst += mb_char2len(cc) - 1;
Bram Moolenaar0c56c602010-07-12 22:42:33 +02007632 if (enc_utf8)
7633 {
7634 int clen = utf_ptr2len(src - 1);
7635
7636 /* If the character length is shorter than "totlen", there
7637 * are composing characters; copy them as-is. */
7638 if (clen < totlen)
7639 {
7640 if (copy)
7641 mch_memmove(dst + 1, src - 1 + clen,
7642 (size_t)(totlen - clen));
7643 dst += totlen - clen;
7644 }
7645 }
7646 src += totlen - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647 }
7648 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007649#endif
7650 if (copy)
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007651 *dst = cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652 dst++;
7653 }
7654 else
7655 {
7656 if (REG_MULTI)
7657 {
7658 clnum = reg_mmatch->startpos[no].lnum;
7659 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
7660 s = NULL;
7661 else
7662 {
7663 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
7664 if (reg_mmatch->endpos[no].lnum == clnum)
7665 len = reg_mmatch->endpos[no].col
7666 - reg_mmatch->startpos[no].col;
7667 else
7668 len = (int)STRLEN(s);
7669 }
7670 }
7671 else
7672 {
7673 s = reg_match->startp[no];
7674 if (reg_match->endp[no] == NULL)
7675 s = NULL;
7676 else
7677 len = (int)(reg_match->endp[no] - s);
7678 }
7679 if (s != NULL)
7680 {
7681 for (;;)
7682 {
7683 if (len == 0)
7684 {
7685 if (REG_MULTI)
7686 {
7687 if (reg_mmatch->endpos[no].lnum == clnum)
7688 break;
7689 if (copy)
7690 *dst = CAR;
7691 ++dst;
7692 s = reg_getline(++clnum);
7693 if (reg_mmatch->endpos[no].lnum == clnum)
7694 len = reg_mmatch->endpos[no].col;
7695 else
7696 len = (int)STRLEN(s);
7697 }
7698 else
7699 break;
7700 }
7701 else if (*s == NUL) /* we hit NUL. */
7702 {
7703 if (copy)
7704 EMSG(_(e_re_damg));
7705 goto exit;
7706 }
7707 else
7708 {
7709 if (backslash && (*s == CAR || *s == '\\'))
7710 {
7711 /*
7712 * Insert a backslash in front of a CR, otherwise
7713 * it will be replaced by a line break.
7714 * Number of backslashes will be halved later,
7715 * double them here.
7716 */
7717 if (copy)
7718 {
7719 dst[0] = '\\';
7720 dst[1] = *s;
7721 }
7722 dst += 2;
7723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724 else
7725 {
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007726#ifdef FEAT_MBYTE
7727 if (has_mbyte)
7728 c = mb_ptr2char(s);
7729 else
7730#endif
7731 c = *s;
7732
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007733 if (func_one != (fptr_T)NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734 /* Turbo C complains without the typecast */
Bram Moolenaarc2c355d2013-03-19 17:42:15 +01007735 func_one = (fptr_T)(func_one(&cc, c));
7736 else if (func_all != (fptr_T)NULL)
7737 /* Turbo C complains without the typecast */
7738 func_all = (fptr_T)(func_all(&cc, c));
7739 else /* just copy */
7740 cc = c;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007741
7742#ifdef FEAT_MBYTE
7743 if (has_mbyte)
7744 {
Bram Moolenaar9225efb2007-07-30 20:32:53 +00007745 int l;
7746
7747 /* Copy composing characters separately, one
7748 * at a time. */
7749 if (enc_utf8)
7750 l = utf_ptr2len(s) - 1;
7751 else
7752 l = mb_ptr2len(s) - 1;
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007753
7754 s += l;
7755 len -= l;
7756 if (copy)
7757 mb_char2bytes(cc, dst);
7758 dst += mb_char2len(cc) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007759 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007760 else
7761#endif
7762 if (copy)
7763 *dst = cc;
7764 dst++;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 }
Bram Moolenaarefd2bf12006-03-16 21:41:35 +00007766
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767 ++s;
7768 --len;
7769 }
7770 }
7771 }
7772 no = -1;
7773 }
7774 }
7775 if (copy)
7776 *dst = NUL;
7777
7778exit:
7779 return (int)((dst - dest) + 1);
7780}
7781
7782#ifdef FEAT_EVAL
Bram Moolenaard32a3192009-11-26 19:40:49 +00007783static char_u *reg_getline_submatch __ARGS((linenr_T lnum));
7784
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785/*
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007786 * Call reg_getline() with the line numbers from the submatch. If a
7787 * substitute() was used the reg_maxline and other values have been
7788 * overwritten.
7789 */
7790 static char_u *
7791reg_getline_submatch(lnum)
7792 linenr_T lnum;
7793{
7794 char_u *s;
7795 linenr_T save_first = reg_firstlnum;
7796 linenr_T save_max = reg_maxline;
7797
7798 reg_firstlnum = submatch_firstlnum;
7799 reg_maxline = submatch_maxline;
7800
7801 s = reg_getline(lnum);
7802
7803 reg_firstlnum = save_first;
7804 reg_maxline = save_max;
7805 return s;
7806}
7807
7808/*
Bram Moolenaar7aa9f6a2007-05-10 18:00:30 +00007809 * Used for the submatch() function: get the string from the n'th submatch in
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810 * allocated memory.
7811 * Returns NULL when not in a ":s" command and for a non-existing submatch.
7812 */
7813 char_u *
7814reg_submatch(no)
7815 int no;
7816{
7817 char_u *retval = NULL;
7818 char_u *s;
7819 int len;
7820 int round;
7821 linenr_T lnum;
7822
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007823 if (!can_f_submatch || no < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 return NULL;
7825
7826 if (submatch_match == NULL)
7827 {
7828 /*
7829 * First round: compute the length and allocate memory.
7830 * Second round: copy the text.
7831 */
7832 for (round = 1; round <= 2; ++round)
7833 {
7834 lnum = submatch_mmatch->startpos[no].lnum;
7835 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
7836 return NULL;
7837
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007838 s = reg_getline_submatch(lnum) + submatch_mmatch->startpos[no].col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 if (s == NULL) /* anti-crash check, cannot happen? */
7840 break;
7841 if (submatch_mmatch->endpos[no].lnum == lnum)
7842 {
7843 /* Within one line: take form start to end col. */
7844 len = submatch_mmatch->endpos[no].col
7845 - submatch_mmatch->startpos[no].col;
7846 if (round == 2)
Bram Moolenaarbbebc852005-07-18 21:47:53 +00007847 vim_strncpy(retval, s, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 ++len;
7849 }
7850 else
7851 {
7852 /* Multiple lines: take start line from start col, middle
7853 * lines completely and end line up to end col. */
7854 len = (int)STRLEN(s);
7855 if (round == 2)
7856 {
7857 STRCPY(retval, s);
7858 retval[len] = '\n';
7859 }
7860 ++len;
7861 ++lnum;
7862 while (lnum < submatch_mmatch->endpos[no].lnum)
7863 {
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007864 s = reg_getline_submatch(lnum++);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 if (round == 2)
7866 STRCPY(retval + len, s);
7867 len += (int)STRLEN(s);
7868 if (round == 2)
7869 retval[len] = '\n';
7870 ++len;
7871 }
7872 if (round == 2)
Bram Moolenaar5ea08a82009-11-25 18:51:24 +00007873 STRNCPY(retval + len, reg_getline_submatch(lnum),
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 submatch_mmatch->endpos[no].col);
7875 len += submatch_mmatch->endpos[no].col;
7876 if (round == 2)
7877 retval[len] = NUL;
7878 ++len;
7879 }
7880
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007881 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007882 {
7883 retval = lalloc((long_u)len, TRUE);
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007884 if (retval == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007885 return NULL;
7886 }
7887 }
7888 }
7889 else
7890 {
Bram Moolenaar7670fa02009-02-21 21:04:20 +00007891 s = submatch_match->startp[no];
7892 if (s == NULL || submatch_match->endp[no] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 retval = NULL;
7894 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 }
7897
7898 return retval;
7899}
Bram Moolenaar41571762014-04-02 19:00:58 +02007900
7901/*
7902 * Used for the submatch() function with the optional non-zero argument: get
7903 * the list of strings from the n'th submatch in allocated memory with NULs
7904 * represented in NLs.
7905 * Returns a list of allocated strings. Returns NULL when not in a ":s"
7906 * command, for a non-existing submatch and for any error.
7907 */
7908 list_T *
7909reg_submatch_list(no)
7910 int no;
7911{
7912 char_u *s;
7913 linenr_T slnum;
7914 linenr_T elnum;
7915 colnr_T scol;
7916 colnr_T ecol;
7917 int i;
7918 list_T *list;
7919 int error = FALSE;
7920
7921 if (!can_f_submatch || no < 0)
7922 return NULL;
7923
7924 if (submatch_match == NULL)
7925 {
7926 slnum = submatch_mmatch->startpos[no].lnum;
7927 elnum = submatch_mmatch->endpos[no].lnum;
7928 if (slnum < 0 || elnum < 0)
7929 return NULL;
7930
7931 scol = submatch_mmatch->startpos[no].col;
7932 ecol = submatch_mmatch->endpos[no].col;
7933
7934 list = list_alloc();
7935 if (list == NULL)
7936 return NULL;
7937
7938 s = reg_getline_submatch(slnum) + scol;
7939 if (slnum == elnum)
7940 {
7941 if (list_append_string(list, s, ecol - scol) == FAIL)
7942 error = TRUE;
7943 }
7944 else
7945 {
7946 if (list_append_string(list, s, -1) == FAIL)
7947 error = TRUE;
7948 for (i = 1; i < elnum - slnum; i++)
7949 {
7950 s = reg_getline_submatch(slnum + i);
7951 if (list_append_string(list, s, -1) == FAIL)
7952 error = TRUE;
7953 }
7954 s = reg_getline_submatch(elnum);
7955 if (list_append_string(list, s, ecol) == FAIL)
7956 error = TRUE;
7957 }
7958 }
7959 else
7960 {
7961 s = submatch_match->startp[no];
7962 if (s == NULL || submatch_match->endp[no] == NULL)
7963 return NULL;
7964 list = list_alloc();
7965 if (list == NULL)
7966 return NULL;
7967 if (list_append_string(list, s,
7968 (int)(submatch_match->endp[no] - s)) == FAIL)
7969 error = TRUE;
7970 }
7971
7972 if (error)
7973 {
7974 list_free(list, TRUE);
7975 return NULL;
7976 }
7977 return list;
7978}
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979#endif
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007980
7981static regengine_T bt_regengine =
7982{
7983 bt_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02007984 bt_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007985 bt_regexec_nl,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007986 bt_regexec_multi
7987#ifdef DEBUG
7988 ,(char_u *)""
7989#endif
7990};
7991
7992
7993#include "regexp_nfa.c"
7994
7995static regengine_T nfa_regengine =
7996{
7997 nfa_regcomp,
Bram Moolenaar473de612013-06-08 18:19:48 +02007998 nfa_regfree,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02007999 nfa_regexec_nl,
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008000 nfa_regexec_multi
8001#ifdef DEBUG
8002 ,(char_u *)""
8003#endif
8004};
8005
8006/* Which regexp engine to use? Needed for vim_regcomp().
8007 * Must match with 'regexpengine'. */
8008static int regexp_engine = 0;
8009#define AUTOMATIC_ENGINE 0
8010#define BACKTRACKING_ENGINE 1
8011#define NFA_ENGINE 2
8012#ifdef DEBUG
8013static char_u regname[][30] = {
8014 "AUTOMATIC Regexp Engine",
Bram Moolenaar75eb1612013-05-29 18:45:11 +02008015 "BACKTRACKING Regexp Engine",
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008016 "NFA Regexp Engine"
8017 };
8018#endif
8019
8020/*
8021 * Compile a regular expression into internal code.
Bram Moolenaar473de612013-06-08 18:19:48 +02008022 * Returns the program in allocated memory.
8023 * Use vim_regfree() to free the memory.
8024 * Returns NULL for an error.
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008025 */
8026 regprog_T *
8027vim_regcomp(expr_arg, re_flags)
8028 char_u *expr_arg;
8029 int re_flags;
8030{
8031 regprog_T *prog = NULL;
8032 char_u *expr = expr_arg;
8033
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008034 regexp_engine = p_re;
8035
8036 /* Check for prefix "\%#=", that sets the regexp engine */
8037 if (STRNCMP(expr, "\\%#=", 4) == 0)
8038 {
8039 int newengine = expr[4] - '0';
8040
8041 if (newengine == AUTOMATIC_ENGINE
8042 || newengine == BACKTRACKING_ENGINE
8043 || newengine == NFA_ENGINE)
8044 {
8045 regexp_engine = expr[4] - '0';
8046 expr += 5;
8047#ifdef DEBUG
Bram Moolenaar6e132072014-05-13 16:46:32 +02008048 smsg((char_u *)"New regexp mode selected (%d): %s",
8049 regexp_engine, regname[newengine]);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008050#endif
8051 }
8052 else
8053 {
8054 EMSG(_("E864: \\%#= can only be followed by 0, 1, or 2. The automatic engine will be used "));
8055 regexp_engine = AUTOMATIC_ENGINE;
8056 }
8057 }
8058#ifdef DEBUG
8059 bt_regengine.expr = expr;
8060 nfa_regengine.expr = expr;
8061#endif
8062
8063 /*
8064 * First try the NFA engine, unless backtracking was requested.
8065 */
8066 if (regexp_engine != BACKTRACKING_ENGINE)
8067 prog = nfa_regengine.regcomp(expr, re_flags);
8068 else
8069 prog = bt_regengine.regcomp(expr, re_flags);
8070
8071 if (prog == NULL) /* error compiling regexp with initial engine */
8072 {
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008073#ifdef BT_REGEXP_DEBUG_LOG
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008074 if (regexp_engine != BACKTRACKING_ENGINE) /* debugging log for NFA */
8075 {
8076 FILE *f;
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008077 f = fopen(BT_REGEXP_DEBUG_LOG_NAME, "a");
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008078 if (f)
8079 {
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008080 fprintf(f, "Syntax error in \"%s\"\n", expr);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008081 fclose(f);
8082 }
8083 else
Bram Moolenaar7fcff1f2013-05-20 21:49:13 +02008084 EMSG2("(NFA) Could not open \"%s\" to write !!!",
8085 BT_REGEXP_DEBUG_LOG_NAME);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008086 }
8087#endif
8088 /*
Bram Moolenaar917789f2013-09-19 17:04:01 +02008089 * If the NFA engine failed, the backtracking engine won't work either.
8090 *
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008091 if (regexp_engine == AUTOMATIC_ENGINE)
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008092 prog = bt_regengine.regcomp(expr, re_flags);
Bram Moolenaar917789f2013-09-19 17:04:01 +02008093 */
Bram Moolenaarcd2d8bb2013-06-05 21:42:53 +02008094 }
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008095
8096 return prog;
8097}
8098
8099/*
Bram Moolenaar473de612013-06-08 18:19:48 +02008100 * Free a compiled regexp program, returned by vim_regcomp().
8101 */
8102 void
8103vim_regfree(prog)
8104 regprog_T *prog;
8105{
8106 if (prog != NULL)
8107 prog->engine->regfree(prog);
8108}
8109
8110/*
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008111 * Match a regexp against a string.
8112 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
8113 * Uses curbuf for line count and 'iskeyword'.
8114 *
8115 * Return TRUE if there is a match, FALSE if not.
8116 */
8117 int
8118vim_regexec(rmp, line, col)
8119 regmatch_T *rmp;
8120 char_u *line; /* string to match against */
8121 colnr_T col; /* column to start looking for match */
8122{
Bram Moolenaar2af78a12014-04-23 19:06:37 +02008123 return rmp->regprog->engine->regexec_nl(rmp, line, col, FALSE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008124}
8125
8126#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
8127 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
8128/*
8129 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
8130 */
8131 int
8132vim_regexec_nl(rmp, line, col)
8133 regmatch_T *rmp;
8134 char_u *line;
8135 colnr_T col;
8136{
Bram Moolenaar2af78a12014-04-23 19:06:37 +02008137 return rmp->regprog->engine->regexec_nl(rmp, line, col, TRUE);
Bram Moolenaarfbc0d2e2013-05-19 19:40:29 +02008138}
8139#endif
8140
8141/*
8142 * Match a regexp against multiple lines.
8143 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
8144 * Uses curbuf for line count and 'iskeyword'.
8145 *
8146 * Return zero if there is no match. Return number of lines contained in the
8147 * match otherwise.
8148 */
8149 long
8150vim_regexec_multi(rmp, win, buf, lnum, col, tm)
8151 regmmatch_T *rmp;
8152 win_T *win; /* window in which to search or NULL */
8153 buf_T *buf; /* buffer in which to search */
8154 linenr_T lnum; /* nr of line to start looking for match */
8155 colnr_T col; /* column to start looking for match */
8156 proftime_T *tm; /* timeout limit or NULL */
8157{
8158 return rmp->regprog->engine->regexec_multi(rmp, win, buf, lnum, col, tm);
8159}