blob: bc13b38ea4b775c5be792058f38c4491f127aefe [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
41#include "vim.h"
42
43#undef DEBUG
44
45/*
46 * The "internal use only" fields in regexp.h are present to pass info from
47 * compile to execute that permits the execute phase to run lots faster on
48 * simple cases. They are:
49 *
50 * regstart char that must begin a match; NUL if none obvious; Can be a
51 * multi-byte character.
52 * reganch is the match anchored (at beginning-of-line only)?
53 * regmust string (pointer into program) that match must include, or NULL
54 * regmlen length of regmust string
55 * regflags RF_ values or'ed together
56 *
57 * Regstart and reganch permit very fast decisions on suitable starting points
58 * for a match, cutting down the work a lot. Regmust permits fast rejection
59 * of lines that cannot possibly match. The regmust tests are costly enough
60 * that vim_regcomp() supplies a regmust only if the r.e. contains something
61 * potentially expensive (at present, the only such thing detected is * or +
62 * at the start of the r.e., which can involve a lot of backup). Regmlen is
63 * supplied because the test in vim_regexec() needs it and vim_regcomp() is
64 * computing it anyway.
65 */
66
67/*
68 * Structure for regexp "program". This is essentially a linear encoding
69 * of a nondeterministic finite-state machine (aka syntax charts or
70 * "railroad normal form" in parsing technology). Each node is an opcode
71 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
72 * all nodes except BRANCH and BRACES_COMPLEX implement concatenation; a "next"
73 * pointer with a BRANCH on both ends of it is connecting two alternatives.
74 * (Here we have one of the subtle syntax dependencies: an individual BRANCH
75 * (as opposed to a collection of them) is never concatenated with anything
76 * because of operator precedence). The "next" pointer of a BRACES_COMPLEX
Bram Moolenaardf177f62005-02-22 08:39:57 +000077 * node points to the node after the stuff to be repeated.
78 * The operand of some types of node is a literal string; for others, it is a
79 * node leading into a sub-FSM. In particular, the operand of a BRANCH node
80 * is the first node of the branch.
81 * (NB this is *not* a tree structure: the tail of the branch connects to the
82 * thing following the set of BRANCHes.)
Bram Moolenaar071d4272004-06-13 20:20:40 +000083 *
84 * pattern is coded like:
85 *
86 * +-----------------+
87 * | V
88 * <aa>\|<bb> BRANCH <aa> BRANCH <bb> --> END
89 * | ^ | ^
90 * +------+ +----------+
91 *
92 *
93 * +------------------+
94 * V |
95 * <aa>* BRANCH BRANCH <aa> --> BACK BRANCH --> NOTHING --> END
96 * | | ^ ^
97 * | +---------------+ |
98 * +---------------------------------------------+
99 *
100 *
Bram Moolenaardf177f62005-02-22 08:39:57 +0000101 * +----------------------+
102 * V |
Bram Moolenaar582fd852005-03-28 20:58:01 +0000103 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000104 * | | ^ ^
105 * | +-----------+ |
106 * +--------------------------------------------------+
Bram Moolenaardf177f62005-02-22 08:39:57 +0000107 *
108 *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000109 * +-------------------------+
110 * V |
111 * <aa>\{} BRANCH BRACE_LIMITS --> BRACE_COMPLEX <aa> --> BACK END
112 * | | ^
113 * | +----------------+
114 * +-----------------------------------------------+
115 *
116 *
117 * <aa>\@!<bb> BRANCH NOMATCH <aa> --> END <bb> --> END
118 * | | ^ ^
119 * | +----------------+ |
120 * +--------------------------------+
121 *
122 * +---------+
123 * | V
124 * \z[abc] BRANCH BRANCH a BRANCH b BRANCH c BRANCH NOTHING --> END
125 * | | | | ^ ^
126 * | | | +-----+ |
127 * | | +----------------+ |
128 * | +---------------------------+ |
129 * +------------------------------------------------------+
130 *
131 * They all start with a BRANCH for "\|" alternaties, even when there is only
132 * one alternative.
133 */
134
135/*
136 * The opcodes are:
137 */
138
139/* definition number opnd? meaning */
140#define END 0 /* End of program or NOMATCH operand. */
141#define BOL 1 /* Match "" at beginning of line. */
142#define EOL 2 /* Match "" at end of line. */
143#define BRANCH 3 /* node Match this alternative, or the
144 * next... */
145#define BACK 4 /* Match "", "next" ptr points backward. */
146#define EXACTLY 5 /* str Match this string. */
147#define NOTHING 6 /* Match empty string. */
148#define STAR 7 /* node Match this (simple) thing 0 or more
149 * times. */
150#define PLUS 8 /* node Match this (simple) thing 1 or more
151 * times. */
152#define MATCH 9 /* node match the operand zero-width */
153#define NOMATCH 10 /* node check for no match with operand */
154#define BEHIND 11 /* node look behind for a match with operand */
155#define NOBEHIND 12 /* node look behind for no match with operand */
156#define SUBPAT 13 /* node match the operand here */
157#define BRACE_SIMPLE 14 /* node Match this (simple) thing between m and
158 * n times (\{m,n\}). */
159#define BOW 15 /* Match "" after [^a-zA-Z0-9_] */
160#define EOW 16 /* Match "" at [^a-zA-Z0-9_] */
161#define BRACE_LIMITS 17 /* nr nr define the min & max for BRACE_SIMPLE
162 * and BRACE_COMPLEX. */
163#define NEWL 18 /* Match line-break */
164#define BHPOS 19 /* End position for BEHIND or NOBEHIND */
165
166
167/* character classes: 20-48 normal, 50-78 include a line-break */
168#define ADD_NL 30
169#define FIRST_NL ANY + ADD_NL
170#define ANY 20 /* Match any one character. */
171#define ANYOF 21 /* str Match any character in this string. */
172#define ANYBUT 22 /* str Match any character not in this
173 * string. */
174#define IDENT 23 /* Match identifier char */
175#define SIDENT 24 /* Match identifier char but no digit */
176#define KWORD 25 /* Match keyword char */
177#define SKWORD 26 /* Match word char but no digit */
178#define FNAME 27 /* Match file name char */
179#define SFNAME 28 /* Match file name char but no digit */
180#define PRINT 29 /* Match printable char */
181#define SPRINT 30 /* Match printable char but no digit */
182#define WHITE 31 /* Match whitespace char */
183#define NWHITE 32 /* Match non-whitespace char */
184#define DIGIT 33 /* Match digit char */
185#define NDIGIT 34 /* Match non-digit char */
186#define HEX 35 /* Match hex char */
187#define NHEX 36 /* Match non-hex char */
188#define OCTAL 37 /* Match octal char */
189#define NOCTAL 38 /* Match non-octal char */
190#define WORD 39 /* Match word char */
191#define NWORD 40 /* Match non-word char */
192#define HEAD 41 /* Match head char */
193#define NHEAD 42 /* Match non-head char */
194#define ALPHA 43 /* Match alpha char */
195#define NALPHA 44 /* Match non-alpha char */
196#define LOWER 45 /* Match lowercase char */
197#define NLOWER 46 /* Match non-lowercase char */
198#define UPPER 47 /* Match uppercase char */
199#define NUPPER 48 /* Match non-uppercase char */
200#define LAST_NL NUPPER + ADD_NL
201#define WITH_NL(op) ((op) >= FIRST_NL && (op) <= LAST_NL)
202
203#define MOPEN 80 /* -89 Mark this point in input as start of
204 * \( subexpr. MOPEN + 0 marks start of
205 * match. */
206#define MCLOSE 90 /* -99 Analogous to MOPEN. MCLOSE + 0 marks
207 * end of match. */
208#define BACKREF 100 /* -109 node Match same string again \1-\9 */
209
210#ifdef FEAT_SYN_HL
211# define ZOPEN 110 /* -119 Mark this point in input as start of
212 * \z( subexpr. */
213# define ZCLOSE 120 /* -129 Analogous to ZOPEN. */
214# define ZREF 130 /* -139 node Match external submatch \z1-\z9 */
215#endif
216
217#define BRACE_COMPLEX 140 /* -149 node Match nodes between m & n times */
218
219#define NOPEN 150 /* Mark this point in input as start of
220 \%( subexpr. */
221#define NCLOSE 151 /* Analogous to NOPEN. */
222
223#define MULTIBYTECODE 200 /* mbc Match one multi-byte character */
224#define RE_BOF 201 /* Match "" at beginning of file. */
225#define RE_EOF 202 /* Match "" at end of file. */
226#define CURSOR 203 /* Match location of cursor. */
227
228#define RE_LNUM 204 /* nr cmp Match line number */
229#define RE_COL 205 /* nr cmp Match column number */
230#define RE_VCOL 206 /* nr cmp Match virtual column number */
231
232/*
233 * Magic characters have a special meaning, they don't match literally.
234 * Magic characters are negative. This separates them from literal characters
235 * (possibly multi-byte). Only ASCII characters can be Magic.
236 */
237#define Magic(x) ((int)(x) - 256)
238#define un_Magic(x) ((x) + 256)
239#define is_Magic(x) ((x) < 0)
240
241static int no_Magic __ARGS((int x));
242static int toggle_Magic __ARGS((int x));
243
244 static int
245no_Magic(x)
246 int x;
247{
248 if (is_Magic(x))
249 return un_Magic(x);
250 return x;
251}
252
253 static int
254toggle_Magic(x)
255 int x;
256{
257 if (is_Magic(x))
258 return un_Magic(x);
259 return Magic(x);
260}
261
262/*
263 * The first byte of the regexp internal "program" is actually this magic
264 * number; the start node begins in the second byte. It's used to catch the
265 * most severe mutilation of the program by the caller.
266 */
267
268#define REGMAGIC 0234
269
270/*
271 * Opcode notes:
272 *
273 * BRANCH The set of branches constituting a single choice are hooked
274 * together with their "next" pointers, since precedence prevents
275 * anything being concatenated to any individual branch. The
276 * "next" pointer of the last BRANCH in a choice points to the
277 * thing following the whole choice. This is also where the
278 * final "next" pointer of each individual branch points; each
279 * branch starts with the operand node of a BRANCH node.
280 *
281 * BACK Normal "next" pointers all implicitly point forward; BACK
282 * exists to make loop structures possible.
283 *
284 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
285 * BRANCH structures using BACK. Simple cases (one character
286 * per match) are implemented with STAR and PLUS for speed
287 * and to minimize recursive plunges.
288 *
289 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
290 * node, and defines the min and max limits to be used for that
291 * node.
292 *
293 * MOPEN,MCLOSE ...are numbered at compile time.
294 * ZOPEN,ZCLOSE ...ditto
295 */
296
297/*
298 * A node is one char of opcode followed by two chars of "next" pointer.
299 * "Next" pointers are stored as two 8-bit bytes, high order first. The
300 * value is a positive offset from the opcode of the node containing it.
301 * An operand, if any, simply follows the node. (Note that much of the
302 * code generation knows about this implicit relationship.)
303 *
304 * Using two bytes for the "next" pointer is vast overkill for most things,
305 * but allows patterns to get big without disasters.
306 */
307#define OP(p) ((int)*(p))
308#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
309#define OPERAND(p) ((p) + 3)
310/* Obtain an operand that was stored as four bytes, MSB first. */
311#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
312 + ((long)(p)[5] << 8) + (long)(p)[6])
313/* Obtain a second operand stored as four bytes. */
314#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
315/* Obtain a second single-byte operand stored after a four bytes operand. */
316#define OPERAND_CMP(p) (p)[7]
317
318/*
319 * Utility definitions.
320 */
321#define UCHARAT(p) ((int)*(char_u *)(p))
322
323/* Used for an error (down from) vim_regcomp(): give the error message, set
324 * rc_did_emsg and return NULL */
Bram Moolenaar45eeb132005-06-06 21:59:07 +0000325#define EMSG_RET_NULL(m) return (EMSG(m), rc_did_emsg = TRUE, NULL)
326#define EMSG_M_RET_NULL(m, c) return (EMSG2((m), (c) ? "" : "\\"), rc_did_emsg = TRUE, NULL)
327#define EMSG_RET_FAIL(m) return (EMSG(m), rc_did_emsg = TRUE, FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328#define EMSG_ONE_RET_NULL EMSG_M_RET_NULL(_("E369: invalid item in %s%%[]"), reg_magic == MAGIC_ALL)
329
330#define MAX_LIMIT (32767L << 16L)
331
332static int re_multi_type __ARGS((int));
333static int cstrncmp __ARGS((char_u *s1, char_u *s2, int *n));
334static char_u *cstrchr __ARGS((char_u *, int));
335
336#ifdef DEBUG
337static void regdump __ARGS((char_u *, regprog_T *));
338static char_u *regprop __ARGS((char_u *));
339#endif
340
341#define NOT_MULTI 0
342#define MULTI_ONE 1
343#define MULTI_MULT 2
344/*
345 * Return NOT_MULTI if c is not a "multi" operator.
346 * Return MULTI_ONE if c is a single "multi" operator.
347 * Return MULTI_MULT if c is a multi "multi" operator.
348 */
349 static int
350re_multi_type(c)
351 int c;
352{
353 if (c == Magic('@') || c == Magic('=') || c == Magic('?'))
354 return MULTI_ONE;
355 if (c == Magic('*') || c == Magic('+') || c == Magic('{'))
356 return MULTI_MULT;
357 return NOT_MULTI;
358}
359
360/*
361 * Flags to be passed up and down.
362 */
363#define HASWIDTH 0x1 /* Known never to match null string. */
364#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
365#define SPSTART 0x4 /* Starts with * or +. */
366#define HASNL 0x8 /* Contains some \n. */
367#define HASLOOKBH 0x10 /* Contains "\@<=" or "\@<!". */
368#define WORST 0 /* Worst case. */
369
370/*
371 * When regcode is set to this value, code is not emitted and size is computed
372 * instead.
373 */
374#define JUST_CALC_SIZE ((char_u *) -1)
375
376static char_u *reg_prev_sub;
377
378/*
379 * REGEXP_INRANGE contains all characters which are always special in a []
380 * range after '\'.
381 * REGEXP_ABBR contains all characters which act as abbreviations after '\'.
382 * These are:
383 * \n - New line (NL).
384 * \r - Carriage Return (CR).
385 * \t - Tab (TAB).
386 * \e - Escape (ESC).
387 * \b - Backspace (Ctrl_H).
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000388 * \d - Character code in decimal, eg \d123
389 * \o - Character code in octal, eg \o80
390 * \x - Character code in hex, eg \x4a
391 * \u - Multibyte character code, eg \u20ac
392 * \U - Long multibyte character code, eg \U12345678
Bram Moolenaar071d4272004-06-13 20:20:40 +0000393 */
394static char_u REGEXP_INRANGE[] = "]^-n\\";
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000395static char_u REGEXP_ABBR[] = "nrtebdoxuU";
Bram Moolenaar071d4272004-06-13 20:20:40 +0000396
397static int backslash_trans __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000398static int get_char_class __ARGS((char_u **pp));
399static int get_equi_class __ARGS((char_u **pp));
400static void reg_equi_class __ARGS((int c));
401static int get_coll_element __ARGS((char_u **pp));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402static char_u *skip_anyof __ARGS((char_u *p));
403static void init_class_tab __ARGS((void));
404
405/*
406 * Translate '\x' to its control character, except "\n", which is Magic.
407 */
408 static int
409backslash_trans(c)
410 int c;
411{
412 switch (c)
413 {
414 case 'r': return CAR;
415 case 't': return TAB;
416 case 'e': return ESC;
417 case 'b': return BS;
418 }
419 return c;
420}
421
422/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000423 * Check for a character class name "[:name:]". "pp" points to the '['.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 * Returns one of the CLASS_ items. CLASS_NONE means that no item was
425 * recognized. Otherwise "pp" is advanced to after the item.
426 */
427 static int
Bram Moolenaardf177f62005-02-22 08:39:57 +0000428get_char_class(pp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 char_u **pp;
430{
431 static const char *(class_names[]) =
432 {
433 "alnum:]",
434#define CLASS_ALNUM 0
435 "alpha:]",
436#define CLASS_ALPHA 1
437 "blank:]",
438#define CLASS_BLANK 2
439 "cntrl:]",
440#define CLASS_CNTRL 3
441 "digit:]",
442#define CLASS_DIGIT 4
443 "graph:]",
444#define CLASS_GRAPH 5
445 "lower:]",
446#define CLASS_LOWER 6
447 "print:]",
448#define CLASS_PRINT 7
449 "punct:]",
450#define CLASS_PUNCT 8
451 "space:]",
452#define CLASS_SPACE 9
453 "upper:]",
454#define CLASS_UPPER 10
455 "xdigit:]",
456#define CLASS_XDIGIT 11
457 "tab:]",
458#define CLASS_TAB 12
459 "return:]",
460#define CLASS_RETURN 13
461 "backspace:]",
462#define CLASS_BACKSPACE 14
463 "escape:]",
464#define CLASS_ESCAPE 15
465 };
466#define CLASS_NONE 99
467 int i;
468
469 if ((*pp)[1] == ':')
470 {
471 for (i = 0; i < sizeof(class_names) / sizeof(*class_names); ++i)
472 if (STRNCMP(*pp + 2, class_names[i], STRLEN(class_names[i])) == 0)
473 {
474 *pp += STRLEN(class_names[i]) + 2;
475 return i;
476 }
477 }
478 return CLASS_NONE;
479}
480
481/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000482 * Specific version of character class functions.
483 * Using a table to keep this fast.
484 */
485static short class_tab[256];
486
487#define RI_DIGIT 0x01
488#define RI_HEX 0x02
489#define RI_OCTAL 0x04
490#define RI_WORD 0x08
491#define RI_HEAD 0x10
492#define RI_ALPHA 0x20
493#define RI_LOWER 0x40
494#define RI_UPPER 0x80
495#define RI_WHITE 0x100
496
497 static void
498init_class_tab()
499{
500 int i;
501 static int done = FALSE;
502
503 if (done)
504 return;
505
506 for (i = 0; i < 256; ++i)
507 {
508 if (i >= '0' && i <= '7')
509 class_tab[i] = RI_DIGIT + RI_HEX + RI_OCTAL + RI_WORD;
510 else if (i >= '8' && i <= '9')
511 class_tab[i] = RI_DIGIT + RI_HEX + RI_WORD;
512 else if (i >= 'a' && i <= 'f')
513 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
514#ifdef EBCDIC
515 else if ((i >= 'g' && i <= 'i') || (i >= 'j' && i <= 'r')
516 || (i >= 's' && i <= 'z'))
517#else
518 else if (i >= 'g' && i <= 'z')
519#endif
520 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_LOWER;
521 else if (i >= 'A' && i <= 'F')
522 class_tab[i] = RI_HEX + RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
523#ifdef EBCDIC
524 else if ((i >= 'G' && i <= 'I') || ( i >= 'J' && i <= 'R')
525 || (i >= 'S' && i <= 'Z'))
526#else
527 else if (i >= 'G' && i <= 'Z')
528#endif
529 class_tab[i] = RI_WORD + RI_HEAD + RI_ALPHA + RI_UPPER;
530 else if (i == '_')
531 class_tab[i] = RI_WORD + RI_HEAD;
532 else
533 class_tab[i] = 0;
534 }
535 class_tab[' '] |= RI_WHITE;
536 class_tab['\t'] |= RI_WHITE;
537 done = TRUE;
538}
539
540#ifdef FEAT_MBYTE
541# define ri_digit(c) (c < 0x100 && (class_tab[c] & RI_DIGIT))
542# define ri_hex(c) (c < 0x100 && (class_tab[c] & RI_HEX))
543# define ri_octal(c) (c < 0x100 && (class_tab[c] & RI_OCTAL))
544# define ri_word(c) (c < 0x100 && (class_tab[c] & RI_WORD))
545# define ri_head(c) (c < 0x100 && (class_tab[c] & RI_HEAD))
546# define ri_alpha(c) (c < 0x100 && (class_tab[c] & RI_ALPHA))
547# define ri_lower(c) (c < 0x100 && (class_tab[c] & RI_LOWER))
548# define ri_upper(c) (c < 0x100 && (class_tab[c] & RI_UPPER))
549# define ri_white(c) (c < 0x100 && (class_tab[c] & RI_WHITE))
550#else
551# define ri_digit(c) (class_tab[c] & RI_DIGIT)
552# define ri_hex(c) (class_tab[c] & RI_HEX)
553# define ri_octal(c) (class_tab[c] & RI_OCTAL)
554# define ri_word(c) (class_tab[c] & RI_WORD)
555# define ri_head(c) (class_tab[c] & RI_HEAD)
556# define ri_alpha(c) (class_tab[c] & RI_ALPHA)
557# define ri_lower(c) (class_tab[c] & RI_LOWER)
558# define ri_upper(c) (class_tab[c] & RI_UPPER)
559# define ri_white(c) (class_tab[c] & RI_WHITE)
560#endif
561
562/* flags for regflags */
563#define RF_ICASE 1 /* ignore case */
564#define RF_NOICASE 2 /* don't ignore case */
565#define RF_HASNL 4 /* can match a NL */
566#define RF_ICOMBINE 8 /* ignore combining characters */
567#define RF_LOOKBH 16 /* uses "\@<=" or "\@<!" */
568
569/*
570 * Global work variables for vim_regcomp().
571 */
572
573static char_u *regparse; /* Input-scan pointer. */
574static int prevchr_len; /* byte length of previous char */
575static int num_complex_braces; /* Complex \{...} count */
576static int regnpar; /* () count. */
577#ifdef FEAT_SYN_HL
578static int regnzpar; /* \z() count. */
579static int re_has_z; /* \z item detected */
580#endif
581static char_u *regcode; /* Code-emit pointer, or JUST_CALC_SIZE */
582static long regsize; /* Code size. */
583static char_u had_endbrace[NSUBEXP]; /* flags, TRUE if end of () found */
584static unsigned regflags; /* RF_ flags for prog */
585static long brace_min[10]; /* Minimums for complex brace repeats */
586static long brace_max[10]; /* Maximums for complex brace repeats */
587static int brace_count[10]; /* Current counts for complex brace repeats */
588#if defined(FEAT_SYN_HL) || defined(PROTO)
589static int had_eol; /* TRUE when EOL found by vim_regcomp() */
590#endif
591static int one_exactly = FALSE; /* only do one char for EXACTLY */
592
593static int reg_magic; /* magicness of the pattern: */
594#define MAGIC_NONE 1 /* "\V" very unmagic */
595#define MAGIC_OFF 2 /* "\M" or 'magic' off */
596#define MAGIC_ON 3 /* "\m" or 'magic' */
597#define MAGIC_ALL 4 /* "\v" very magic */
598
599static int reg_string; /* matching with a string instead of a buffer
600 line */
601
602/*
603 * META contains all characters that may be magic, except '^' and '$'.
604 */
605
606#ifdef EBCDIC
607static char_u META[] = "%&()*+.123456789<=>?@ACDFHIKLMOPSUVWX[_acdfhiklmnopsuvwxz{|~";
608#else
609/* META[] is used often enough to justify turning it into a table. */
610static char_u META_flags[] = {
611 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
612 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
613/* % & ( ) * + . */
614 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0,
615/* 1 2 3 4 5 6 7 8 9 < = > ? */
616 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1,
617/* @ A C D F H I K L M O */
618 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1,
619/* P S U V W X Z [ _ */
620 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 0, 1,
621/* a c d f h i k l m n o */
622 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1,
623/* p s u v w x z { | ~ */
624 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1, 0, 1
625};
626#endif
627
628static int curchr;
629
630/* arguments for reg() */
631#define REG_NOPAREN 0 /* toplevel reg() */
632#define REG_PAREN 1 /* \(\) */
633#define REG_ZPAREN 2 /* \z(\) */
634#define REG_NPAREN 3 /* \%(\) */
635
636/*
637 * Forward declarations for vim_regcomp()'s friends.
638 */
639static void initchr __ARGS((char_u *));
640static int getchr __ARGS((void));
641static void skipchr_keepstart __ARGS((void));
642static int peekchr __ARGS((void));
643static void skipchr __ARGS((void));
644static void ungetchr __ARGS((void));
Bram Moolenaarc0197e22004-09-13 20:26:32 +0000645static int gethexchrs __ARGS((int maxinputlen));
646static int getoctchrs __ARGS((void));
647static int getdecchrs __ARGS((void));
648static int coll_get_char __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000649static void regcomp_start __ARGS((char_u *expr, int flags));
650static char_u *reg __ARGS((int, int *));
651static char_u *regbranch __ARGS((int *flagp));
652static char_u *regconcat __ARGS((int *flagp));
653static char_u *regpiece __ARGS((int *));
654static char_u *regatom __ARGS((int *));
655static char_u *regnode __ARGS((int));
656static int prog_magic_wrong __ARGS((void));
657static char_u *regnext __ARGS((char_u *));
658static void regc __ARGS((int b));
659#ifdef FEAT_MBYTE
660static void regmbc __ARGS((int c));
Bram Moolenaardf177f62005-02-22 08:39:57 +0000661#else
662# define regmbc(c) regc(c)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#endif
664static void reginsert __ARGS((int, char_u *));
665static void reginsert_limits __ARGS((int, long, long, char_u *));
666static char_u *re_put_long __ARGS((char_u *pr, long_u val));
667static int read_limits __ARGS((long *, long *));
668static void regtail __ARGS((char_u *, char_u *));
669static void regoptail __ARGS((char_u *, char_u *));
670
671/*
672 * Return TRUE if compiled regular expression "prog" can match a line break.
673 */
674 int
675re_multiline(prog)
676 regprog_T *prog;
677{
678 return (prog->regflags & RF_HASNL);
679}
680
681/*
682 * Return TRUE if compiled regular expression "prog" looks before the start
683 * position (pattern contains "\@<=" or "\@<!").
684 */
685 int
686re_lookbehind(prog)
687 regprog_T *prog;
688{
689 return (prog->regflags & RF_LOOKBH);
690}
691
692/*
Bram Moolenaardf177f62005-02-22 08:39:57 +0000693 * Check for an equivalence class name "[=a=]". "pp" points to the '['.
694 * Returns a character representing the class. Zero means that no item was
695 * recognized. Otherwise "pp" is advanced to after the item.
696 */
697 static int
698get_equi_class(pp)
699 char_u **pp;
700{
701 int c;
702 int l = 1;
703 char_u *p = *pp;
704
705 if (p[1] == '=')
706 {
707#ifdef FEAT_MBYTE
708 if (has_mbyte)
709 l = mb_ptr2len_check(p + 2);
710#endif
711 if (p[l + 2] == '=' && p[l + 3] == ']')
712 {
713#ifdef FEAT_MBYTE
714 if (has_mbyte)
715 c = mb_ptr2char(p + 2);
716 else
717#endif
718 c = p[2];
719 *pp += l + 4;
720 return c;
721 }
722 }
723 return 0;
724}
725
726/*
727 * Produce the bytes for equivalence class "c".
728 * Currently only handles latin1, latin9 and utf-8.
729 */
730 static void
731reg_equi_class(c)
732 int c;
733{
734#ifdef FEAT_MBYTE
735 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
736 || STRCMP(p_enc, "latin9") == 0)
737#endif
738 {
739 switch (c)
740 {
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000741 case 'A': case '\300': case '\301': case '\302':
742 case '\303': case '\304': case '\305':
743 regmbc('A'); regmbc('\300'); regmbc('\301');
744 regmbc('\302'); regmbc('\303'); regmbc('\304');
745 regmbc('\305');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000746 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000747 case 'C': case '\307':
748 regmbc('C'); regmbc('\307');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000749 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000750 case 'E': case '\310': case '\311': case '\312': case '\313':
751 regmbc('E'); regmbc('\310'); regmbc('\311');
752 regmbc('\312'); regmbc('\313');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000753 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000754 case 'I': case '\314': case '\315': case '\316': case '\317':
755 regmbc('I'); regmbc('\314'); regmbc('\315');
756 regmbc('\316'); regmbc('\317');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000757 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000758 case 'N': case '\321':
759 regmbc('N'); regmbc('\321');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000760 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000761 case 'O': case '\322': case '\323': case '\324': case '\325':
762 case '\326':
763 regmbc('O'); regmbc('\322'); regmbc('\323');
764 regmbc('\324'); regmbc('\325'); regmbc('\326');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000765 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000766 case 'U': case '\331': case '\332': case '\333': case '\334':
767 regmbc('U'); regmbc('\331'); regmbc('\332');
768 regmbc('\333'); regmbc('\334');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000769 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000770 case 'Y': case '\335':
771 regmbc('Y'); regmbc('\335');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000772 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000773 case 'a': case '\340': case '\341': case '\342':
774 case '\343': case '\344': case '\345':
775 regmbc('a'); regmbc('\340'); regmbc('\341');
776 regmbc('\342'); regmbc('\343'); regmbc('\344');
777 regmbc('\345');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000778 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000779 case 'c': case '\347':
780 regmbc('c'); regmbc('\347');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000781 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000782 case 'e': case '\350': case '\351': case '\352': case '\353':
783 regmbc('e'); regmbc('\350'); regmbc('\351');
784 regmbc('\352'); regmbc('\353');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000785 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000786 case 'i': case '\354': case '\355': case '\356': case '\357':
787 regmbc('i'); regmbc('\354'); regmbc('\355');
788 regmbc('\356'); regmbc('\357');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000789 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000790 case 'n': case '\361':
791 regmbc('n'); regmbc('\361');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000792 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000793 case 'o': case '\362': case '\363': case '\364': case '\365':
794 case '\366':
795 regmbc('o'); regmbc('\362'); regmbc('\363');
796 regmbc('\364'); regmbc('\365'); regmbc('\366');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000797 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000798 case 'u': case '\371': case '\372': case '\373': case '\374':
799 regmbc('u'); regmbc('\371'); regmbc('\372');
800 regmbc('\373'); regmbc('\374');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000801 return;
Bram Moolenaar402d2fe2005-04-15 21:00:38 +0000802 case 'y': case '\375': case '\377':
803 regmbc('y'); regmbc('\375'); regmbc('\377');
Bram Moolenaardf177f62005-02-22 08:39:57 +0000804 return;
805 }
806 }
807 regmbc(c);
808}
809
810/*
811 * Check for a collating element "[.a.]". "pp" points to the '['.
812 * Returns a character. Zero means that no item was recognized. Otherwise
813 * "pp" is advanced to after the item.
814 * Currently only single characters are recognized!
815 */
816 static int
817get_coll_element(pp)
818 char_u **pp;
819{
820 int c;
821 int l = 1;
822 char_u *p = *pp;
823
824 if (p[1] == '.')
825 {
826#ifdef FEAT_MBYTE
827 if (has_mbyte)
828 l = mb_ptr2len_check(p + 2);
829#endif
830 if (p[l + 2] == '.' && p[l + 3] == ']')
831 {
832#ifdef FEAT_MBYTE
833 if (has_mbyte)
834 c = mb_ptr2char(p + 2);
835 else
836#endif
837 c = p[2];
838 *pp += l + 4;
839 return c;
840 }
841 }
842 return 0;
843}
844
845
846/*
847 * Skip over a "[]" range.
848 * "p" must point to the character after the '['.
849 * The returned pointer is on the matching ']', or the terminating NUL.
850 */
851 static char_u *
852skip_anyof(p)
853 char_u *p;
854{
855 int cpo_lit; /* 'cpoptions' contains 'l' flag */
856 int cpo_bsl; /* 'cpoptions' contains '\' flag */
857#ifdef FEAT_MBYTE
858 int l;
859#endif
860
861 cpo_lit = (!reg_syn && vim_strchr(p_cpo, CPO_LITERAL) != NULL);
862 cpo_bsl = (!reg_syn && vim_strchr(p_cpo, CPO_BACKSL) != NULL);
863
864 if (*p == '^') /* Complement of range. */
865 ++p;
866 if (*p == ']' || *p == '-')
867 ++p;
868 while (*p != NUL && *p != ']')
869 {
870#ifdef FEAT_MBYTE
871 if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
872 p += l;
873 else
874#endif
875 if (*p == '-')
876 {
877 ++p;
878 if (*p != ']' && *p != NUL)
879 mb_ptr_adv(p);
880 }
881 else if (*p == '\\'
882 && !cpo_bsl
883 && (vim_strchr(REGEXP_INRANGE, p[1]) != NULL
884 || (!cpo_lit && vim_strchr(REGEXP_ABBR, p[1]) != NULL)))
885 p += 2;
886 else if (*p == '[')
887 {
888 if (get_char_class(&p) == CLASS_NONE
889 && get_equi_class(&p) == 0
890 && get_coll_element(&p) == 0)
891 ++p; /* It was not a class name */
892 }
893 else
894 ++p;
895 }
896
897 return p;
898}
899
900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 * Skip past regular expression.
Bram Moolenaar748bf032005-02-02 23:04:36 +0000902 * Stop at end of "startp" or where "dirc" is found ('/', '?', etc).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 * Take care of characters with a backslash in front of it.
904 * Skip strings inside [ and ].
905 * When "newp" is not NULL and "dirc" is '?', make an allocated copy of the
906 * expression and change "\?" to "?". If "*newp" is not NULL the expression
907 * is changed in-place.
908 */
909 char_u *
910skip_regexp(startp, dirc, magic, newp)
911 char_u *startp;
912 int dirc;
913 int magic;
914 char_u **newp;
915{
916 int mymagic;
917 char_u *p = startp;
918
919 if (magic)
920 mymagic = MAGIC_ON;
921 else
922 mymagic = MAGIC_OFF;
923
Bram Moolenaar1cd871b2004-12-19 22:46:22 +0000924 for (; p[0] != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 {
926 if (p[0] == dirc) /* found end of regexp */
927 break;
928 if ((p[0] == '[' && mymagic >= MAGIC_ON)
929 || (p[0] == '\\' && p[1] == '[' && mymagic <= MAGIC_OFF))
930 {
931 p = skip_anyof(p + 1);
932 if (p[0] == NUL)
933 break;
934 }
935 else if (p[0] == '\\' && p[1] != NUL)
936 {
937 if (dirc == '?' && newp != NULL && p[1] == '?')
938 {
939 /* change "\?" to "?", make a copy first. */
940 if (*newp == NULL)
941 {
942 *newp = vim_strsave(startp);
943 if (*newp != NULL)
944 p = *newp + (p - startp);
945 }
946 if (*newp != NULL)
947 mch_memmove(p, p + 1, STRLEN(p));
948 else
949 ++p;
950 }
951 else
952 ++p; /* skip next character */
953 if (*p == 'v')
954 mymagic = MAGIC_ALL;
955 else if (*p == 'V')
956 mymagic = MAGIC_NONE;
957 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000958 }
959 return p;
960}
961
962/*
Bram Moolenaar86b68352004-12-27 21:59:20 +0000963 * vim_regcomp() - compile a regular expression into internal code
964 * Returns the program in allocated space. Returns NULL for an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 *
966 * We can't allocate space until we know how big the compiled form will be,
967 * but we can't compile it (and thus know how big it is) until we've got a
968 * place to put the code. So we cheat: we compile it twice, once with code
969 * generation turned off and size counting turned on, and once "for real".
970 * This also means that we don't allocate space until we are sure that the
971 * thing really will compile successfully, and we never have to move the
972 * code and thus invalidate pointers into it. (Note that it has to be in
973 * one piece because vim_free() must be able to free it all.)
974 *
975 * Whether upper/lower case is to be ignored is decided when executing the
976 * program, it does not matter here.
977 *
978 * Beware that the optimization-preparation code in here knows about some
979 * of the structure of the compiled regexp.
980 * "re_flags": RE_MAGIC and/or RE_STRING.
981 */
982 regprog_T *
983vim_regcomp(expr, re_flags)
984 char_u *expr;
985 int re_flags;
986{
987 regprog_T *r;
988 char_u *scan;
989 char_u *longest;
990 int len;
991 int flags;
992
993 if (expr == NULL)
994 EMSG_RET_NULL(_(e_null));
995
996 init_class_tab();
997
998 /*
999 * First pass: determine size, legality.
1000 */
1001 regcomp_start(expr, re_flags);
1002 regcode = JUST_CALC_SIZE;
1003 regc(REGMAGIC);
1004 if (reg(REG_NOPAREN, &flags) == NULL)
1005 return NULL;
1006
1007 /* Small enough for pointer-storage convention? */
1008#ifdef SMALL_MALLOC /* 16 bit storage allocation */
1009 if (regsize >= 65536L - 256L)
1010 EMSG_RET_NULL(_("E339: Pattern too long"));
1011#endif
1012
1013 /* Allocate space. */
1014 r = (regprog_T *)lalloc(sizeof(regprog_T) + regsize, TRUE);
1015 if (r == NULL)
1016 return NULL;
1017
1018 /*
1019 * Second pass: emit code.
1020 */
1021 regcomp_start(expr, re_flags);
1022 regcode = r->program;
1023 regc(REGMAGIC);
1024 if (reg(REG_NOPAREN, &flags) == NULL)
1025 {
1026 vim_free(r);
1027 return NULL;
1028 }
1029
1030 /* Dig out information for optimizations. */
1031 r->regstart = NUL; /* Worst-case defaults. */
1032 r->reganch = 0;
1033 r->regmust = NULL;
1034 r->regmlen = 0;
1035 r->regflags = regflags;
1036 if (flags & HASNL)
1037 r->regflags |= RF_HASNL;
1038 if (flags & HASLOOKBH)
1039 r->regflags |= RF_LOOKBH;
1040#ifdef FEAT_SYN_HL
1041 /* Remember whether this pattern has any \z specials in it. */
1042 r->reghasz = re_has_z;
1043#endif
1044 scan = r->program + 1; /* First BRANCH. */
1045 if (OP(regnext(scan)) == END) /* Only one top-level choice. */
1046 {
1047 scan = OPERAND(scan);
1048
1049 /* Starting-point info. */
1050 if (OP(scan) == BOL || OP(scan) == RE_BOF)
1051 {
1052 r->reganch++;
1053 scan = regnext(scan);
1054 }
1055
1056 if (OP(scan) == EXACTLY)
1057 {
1058#ifdef FEAT_MBYTE
1059 if (has_mbyte)
1060 r->regstart = (*mb_ptr2char)(OPERAND(scan));
1061 else
1062#endif
1063 r->regstart = *OPERAND(scan);
1064 }
1065 else if ((OP(scan) == BOW
1066 || OP(scan) == EOW
1067 || OP(scan) == NOTHING
1068 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
1069 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
1070 && OP(regnext(scan)) == EXACTLY)
1071 {
1072#ifdef FEAT_MBYTE
1073 if (has_mbyte)
1074 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
1075 else
1076#endif
1077 r->regstart = *OPERAND(regnext(scan));
1078 }
1079
1080 /*
1081 * If there's something expensive in the r.e., find the longest
1082 * literal string that must appear and make it the regmust. Resolve
1083 * ties in favor of later strings, since the regstart check works
1084 * with the beginning of the r.e. and avoiding duplication
1085 * strengthens checking. Not a strong reason, but sufficient in the
1086 * absence of others.
1087 */
1088 /*
1089 * When the r.e. starts with BOW, it is faster to look for a regmust
1090 * first. Used a lot for "#" and "*" commands. (Added by mool).
1091 */
1092 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
1093 && !(flags & HASNL))
1094 {
1095 longest = NULL;
1096 len = 0;
1097 for (; scan != NULL; scan = regnext(scan))
1098 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
1099 {
1100 longest = OPERAND(scan);
1101 len = (int)STRLEN(OPERAND(scan));
1102 }
1103 r->regmust = longest;
1104 r->regmlen = len;
1105 }
1106 }
1107#ifdef DEBUG
1108 regdump(expr, r);
1109#endif
1110 return r;
1111}
1112
1113/*
1114 * Setup to parse the regexp. Used once to get the length and once to do it.
1115 */
1116 static void
1117regcomp_start(expr, re_flags)
1118 char_u *expr;
1119 int re_flags; /* see vim_regcomp() */
1120{
1121 initchr(expr);
1122 if (re_flags & RE_MAGIC)
1123 reg_magic = MAGIC_ON;
1124 else
1125 reg_magic = MAGIC_OFF;
1126 reg_string = (re_flags & RE_STRING);
1127
1128 num_complex_braces = 0;
1129 regnpar = 1;
1130 vim_memset(had_endbrace, 0, sizeof(had_endbrace));
1131#ifdef FEAT_SYN_HL
1132 regnzpar = 1;
1133 re_has_z = 0;
1134#endif
1135 regsize = 0L;
1136 regflags = 0;
1137#if defined(FEAT_SYN_HL) || defined(PROTO)
1138 had_eol = FALSE;
1139#endif
1140}
1141
1142#if defined(FEAT_SYN_HL) || defined(PROTO)
1143/*
1144 * Check if during the previous call to vim_regcomp the EOL item "$" has been
1145 * found. This is messy, but it works fine.
1146 */
1147 int
1148vim_regcomp_had_eol()
1149{
1150 return had_eol;
1151}
1152#endif
1153
1154/*
1155 * reg - regular expression, i.e. main body or parenthesized thing
1156 *
1157 * Caller must absorb opening parenthesis.
1158 *
1159 * Combining parenthesis handling with the base level of regular expression
1160 * is a trifle forced, but the need to tie the tails of the branches to what
1161 * follows makes it hard to avoid.
1162 */
1163 static char_u *
1164reg(paren, flagp)
1165 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */
1166 int *flagp;
1167{
1168 char_u *ret;
1169 char_u *br;
1170 char_u *ender;
1171 int parno = 0;
1172 int flags;
1173
1174 *flagp = HASWIDTH; /* Tentatively. */
1175
1176#ifdef FEAT_SYN_HL
1177 if (paren == REG_ZPAREN)
1178 {
1179 /* Make a ZOPEN node. */
1180 if (regnzpar >= NSUBEXP)
1181 EMSG_RET_NULL(_("E50: Too many \\z("));
1182 parno = regnzpar;
1183 regnzpar++;
1184 ret = regnode(ZOPEN + parno);
1185 }
1186 else
1187#endif
1188 if (paren == REG_PAREN)
1189 {
1190 /* Make a MOPEN node. */
1191 if (regnpar >= NSUBEXP)
1192 EMSG_M_RET_NULL(_("E51: Too many %s("), reg_magic == MAGIC_ALL);
1193 parno = regnpar;
1194 ++regnpar;
1195 ret = regnode(MOPEN + parno);
1196 }
1197 else if (paren == REG_NPAREN)
1198 {
1199 /* Make a NOPEN node. */
1200 ret = regnode(NOPEN);
1201 }
1202 else
1203 ret = NULL;
1204
1205 /* Pick up the branches, linking them together. */
1206 br = regbranch(&flags);
1207 if (br == NULL)
1208 return NULL;
1209 if (ret != NULL)
1210 regtail(ret, br); /* [MZ]OPEN -> first. */
1211 else
1212 ret = br;
1213 /* If one of the branches can be zero-width, the whole thing can.
1214 * If one of the branches has * at start or matches a line-break, the
1215 * whole thing can. */
1216 if (!(flags & HASWIDTH))
1217 *flagp &= ~HASWIDTH;
1218 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1219 while (peekchr() == Magic('|'))
1220 {
1221 skipchr();
1222 br = regbranch(&flags);
1223 if (br == NULL)
1224 return NULL;
1225 regtail(ret, br); /* BRANCH -> BRANCH. */
1226 if (!(flags & HASWIDTH))
1227 *flagp &= ~HASWIDTH;
1228 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
1229 }
1230
1231 /* Make a closing node, and hook it on the end. */
1232 ender = regnode(
1233#ifdef FEAT_SYN_HL
1234 paren == REG_ZPAREN ? ZCLOSE + parno :
1235#endif
1236 paren == REG_PAREN ? MCLOSE + parno :
1237 paren == REG_NPAREN ? NCLOSE : END);
1238 regtail(ret, ender);
1239
1240 /* Hook the tails of the branches to the closing node. */
1241 for (br = ret; br != NULL; br = regnext(br))
1242 regoptail(br, ender);
1243
1244 /* Check for proper termination. */
1245 if (paren != REG_NOPAREN && getchr() != Magic(')'))
1246 {
1247#ifdef FEAT_SYN_HL
1248 if (paren == REG_ZPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001249 EMSG_RET_NULL(_("E52: Unmatched \\z("));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 else
1251#endif
1252 if (paren == REG_NPAREN)
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001253 EMSG_M_RET_NULL(_("E53: Unmatched %s%%("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001255 EMSG_M_RET_NULL(_("E54: Unmatched %s("), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 }
1257 else if (paren == REG_NOPAREN && peekchr() != NUL)
1258 {
1259 if (curchr == Magic(')'))
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001260 EMSG_M_RET_NULL(_("E55: Unmatched %s)"), reg_magic == MAGIC_ALL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 else
Bram Moolenaar45eeb132005-06-06 21:59:07 +00001262 EMSG_RET_NULL(_(e_trailing)); /* "Can't happen". */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 /* NOTREACHED */
1264 }
1265 /*
1266 * Here we set the flag allowing back references to this set of
1267 * parentheses.
1268 */
1269 if (paren == REG_PAREN)
1270 had_endbrace[parno] = TRUE; /* have seen the close paren */
1271 return ret;
1272}
1273
1274/*
1275 * regbranch - one alternative of an | operator
1276 *
1277 * Implements the & operator.
1278 */
1279 static char_u *
1280regbranch(flagp)
1281 int *flagp;
1282{
1283 char_u *ret;
1284 char_u *chain = NULL;
1285 char_u *latest;
1286 int flags;
1287
1288 *flagp = WORST | HASNL; /* Tentatively. */
1289
1290 ret = regnode(BRANCH);
1291 for (;;)
1292 {
1293 latest = regconcat(&flags);
1294 if (latest == NULL)
1295 return NULL;
1296 /* If one of the branches has width, the whole thing has. If one of
1297 * the branches anchors at start-of-line, the whole thing does.
1298 * If one of the branches uses look-behind, the whole thing does. */
1299 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
1300 /* If one of the branches doesn't match a line-break, the whole thing
1301 * doesn't. */
1302 *flagp &= ~HASNL | (flags & HASNL);
1303 if (chain != NULL)
1304 regtail(chain, latest);
1305 if (peekchr() != Magic('&'))
1306 break;
1307 skipchr();
1308 regtail(latest, regnode(END)); /* operand ends */
1309 reginsert(MATCH, latest);
1310 chain = latest;
1311 }
1312
1313 return ret;
1314}
1315
1316/*
1317 * regbranch - one alternative of an | or & operator
1318 *
1319 * Implements the concatenation operator.
1320 */
1321 static char_u *
1322regconcat(flagp)
1323 int *flagp;
1324{
1325 char_u *first = NULL;
1326 char_u *chain = NULL;
1327 char_u *latest;
1328 int flags;
1329 int cont = TRUE;
1330
1331 *flagp = WORST; /* Tentatively. */
1332
1333 while (cont)
1334 {
1335 switch (peekchr())
1336 {
1337 case NUL:
1338 case Magic('|'):
1339 case Magic('&'):
1340 case Magic(')'):
1341 cont = FALSE;
1342 break;
1343 case Magic('Z'):
1344#ifdef FEAT_MBYTE
1345 regflags |= RF_ICOMBINE;
1346#endif
1347 skipchr_keepstart();
1348 break;
1349 case Magic('c'):
1350 regflags |= RF_ICASE;
1351 skipchr_keepstart();
1352 break;
1353 case Magic('C'):
1354 regflags |= RF_NOICASE;
1355 skipchr_keepstart();
1356 break;
1357 case Magic('v'):
1358 reg_magic = MAGIC_ALL;
1359 skipchr_keepstart();
1360 curchr = -1;
1361 break;
1362 case Magic('m'):
1363 reg_magic = MAGIC_ON;
1364 skipchr_keepstart();
1365 curchr = -1;
1366 break;
1367 case Magic('M'):
1368 reg_magic = MAGIC_OFF;
1369 skipchr_keepstart();
1370 curchr = -1;
1371 break;
1372 case Magic('V'):
1373 reg_magic = MAGIC_NONE;
1374 skipchr_keepstart();
1375 curchr = -1;
1376 break;
1377 default:
1378 latest = regpiece(&flags);
1379 if (latest == NULL)
1380 return NULL;
1381 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
1382 if (chain == NULL) /* First piece. */
1383 *flagp |= flags & SPSTART;
1384 else
1385 regtail(chain, latest);
1386 chain = latest;
1387 if (first == NULL)
1388 first = latest;
1389 break;
1390 }
1391 }
1392 if (first == NULL) /* Loop ran zero times. */
1393 first = regnode(NOTHING);
1394 return first;
1395}
1396
1397/*
1398 * regpiece - something followed by possible [*+=]
1399 *
1400 * Note that the branching code sequences used for = and the general cases
1401 * of * and + are somewhat optimized: they use the same NOTHING node as
1402 * both the endmarker for their branch list and the body of the last branch.
1403 * It might seem that this node could be dispensed with entirely, but the
1404 * endmarker role is not redundant.
1405 */
1406 static char_u *
1407regpiece(flagp)
1408 int *flagp;
1409{
1410 char_u *ret;
1411 int op;
1412 char_u *next;
1413 int flags;
1414 long minval;
1415 long maxval;
1416
1417 ret = regatom(&flags);
1418 if (ret == NULL)
1419 return NULL;
1420
1421 op = peekchr();
1422 if (re_multi_type(op) == NOT_MULTI)
1423 {
1424 *flagp = flags;
1425 return ret;
1426 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 /* default flags */
1428 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
1429
1430 skipchr();
1431 switch (op)
1432 {
1433 case Magic('*'):
1434 if (flags & SIMPLE)
1435 reginsert(STAR, ret);
1436 else
1437 {
1438 /* Emit x* as (x&|), where & means "self". */
1439 reginsert(BRANCH, ret); /* Either x */
1440 regoptail(ret, regnode(BACK)); /* and loop */
1441 regoptail(ret, ret); /* back */
1442 regtail(ret, regnode(BRANCH)); /* or */
1443 regtail(ret, regnode(NOTHING)); /* null. */
1444 }
1445 break;
1446
1447 case Magic('+'):
1448 if (flags & SIMPLE)
1449 reginsert(PLUS, ret);
1450 else
1451 {
1452 /* Emit x+ as x(&|), where & means "self". */
1453 next = regnode(BRANCH); /* Either */
1454 regtail(ret, next);
Bram Moolenaar582fd852005-03-28 20:58:01 +00001455 regtail(regnode(BACK), ret); /* loop back */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001456 regtail(next, regnode(BRANCH)); /* or */
1457 regtail(ret, regnode(NOTHING)); /* null. */
1458 }
1459 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1460 break;
1461
1462 case Magic('@'):
1463 {
1464 int lop = END;
1465
1466 switch (no_Magic(getchr()))
1467 {
1468 case '=': lop = MATCH; break; /* \@= */
1469 case '!': lop = NOMATCH; break; /* \@! */
1470 case '>': lop = SUBPAT; break; /* \@> */
1471 case '<': switch (no_Magic(getchr()))
1472 {
1473 case '=': lop = BEHIND; break; /* \@<= */
1474 case '!': lop = NOBEHIND; break; /* \@<! */
1475 }
1476 }
1477 if (lop == END)
1478 EMSG_M_RET_NULL(_("E59: invalid character after %s@"),
1479 reg_magic == MAGIC_ALL);
1480 /* Look behind must match with behind_pos. */
1481 if (lop == BEHIND || lop == NOBEHIND)
1482 {
1483 regtail(ret, regnode(BHPOS));
1484 *flagp |= HASLOOKBH;
1485 }
1486 regtail(ret, regnode(END)); /* operand ends */
1487 reginsert(lop, ret);
1488 break;
1489 }
1490
1491 case Magic('?'):
1492 case Magic('='):
1493 /* Emit x= as (x|) */
1494 reginsert(BRANCH, ret); /* Either x */
1495 regtail(ret, regnode(BRANCH)); /* or */
1496 next = regnode(NOTHING); /* null. */
1497 regtail(ret, next);
1498 regoptail(ret, next);
1499 break;
1500
1501 case Magic('{'):
1502 if (!read_limits(&minval, &maxval))
1503 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 if (flags & SIMPLE)
1505 {
1506 reginsert(BRACE_SIMPLE, ret);
1507 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1508 }
1509 else
1510 {
1511 if (num_complex_braces >= 10)
1512 EMSG_M_RET_NULL(_("E60: Too many complex %s{...}s"),
1513 reg_magic == MAGIC_ALL);
1514 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
1515 regoptail(ret, regnode(BACK));
1516 regoptail(ret, ret);
1517 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
1518 ++num_complex_braces;
1519 }
1520 if (minval > 0 && maxval > 0)
1521 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
1522 break;
1523 }
1524 if (re_multi_type(peekchr()) != NOT_MULTI)
1525 {
1526 /* Can't have a multi follow a multi. */
1527 if (peekchr() == Magic('*'))
1528 sprintf((char *)IObuff, _("E61: Nested %s*"),
1529 reg_magic >= MAGIC_ON ? "" : "\\");
1530 else
1531 sprintf((char *)IObuff, _("E62: Nested %s%c"),
1532 reg_magic == MAGIC_ALL ? "" : "\\", no_Magic(peekchr()));
1533 EMSG_RET_NULL(IObuff);
1534 }
1535
1536 return ret;
1537}
1538
1539/*
1540 * regatom - the lowest level
1541 *
1542 * Optimization: gobbles an entire sequence of ordinary characters so that
1543 * it can turn them into a single node, which is smaller to store and
1544 * faster to run. Don't do this when one_exactly is set.
1545 */
1546 static char_u *
1547regatom(flagp)
1548 int *flagp;
1549{
1550 char_u *ret;
1551 int flags;
1552 int cpo_lit; /* 'cpoptions' contains 'l' flag */
Bram Moolenaardf177f62005-02-22 08:39:57 +00001553 int cpo_bsl; /* 'cpoptions' contains '\' flag */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001554 int c;
1555 static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
1556 static int classcodes[] = {ANY, IDENT, SIDENT, KWORD, SKWORD,
1557 FNAME, SFNAME, PRINT, SPRINT,
1558 WHITE, NWHITE, DIGIT, NDIGIT,
1559 HEX, NHEX, OCTAL, NOCTAL,
1560 WORD, NWORD, HEAD, NHEAD,
1561 ALPHA, NALPHA, LOWER, NLOWER,
1562 UPPER, NUPPER
1563 };
1564 char_u *p;
1565 int extra = 0;
1566
1567 *flagp = WORST; /* Tentatively. */
1568 cpo_lit = (!reg_syn && vim_strchr(p_cpo, CPO_LITERAL) != NULL);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001569 cpo_bsl = (!reg_syn && vim_strchr(p_cpo, CPO_BACKSL) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570
1571 c = getchr();
1572 switch (c)
1573 {
1574 case Magic('^'):
1575 ret = regnode(BOL);
1576 break;
1577
1578 case Magic('$'):
1579 ret = regnode(EOL);
1580#if defined(FEAT_SYN_HL) || defined(PROTO)
1581 had_eol = TRUE;
1582#endif
1583 break;
1584
1585 case Magic('<'):
1586 ret = regnode(BOW);
1587 break;
1588
1589 case Magic('>'):
1590 ret = regnode(EOW);
1591 break;
1592
1593 case Magic('_'):
1594 c = no_Magic(getchr());
1595 if (c == '^') /* "\_^" is start-of-line */
1596 {
1597 ret = regnode(BOL);
1598 break;
1599 }
1600 if (c == '$') /* "\_$" is end-of-line */
1601 {
1602 ret = regnode(EOL);
1603#if defined(FEAT_SYN_HL) || defined(PROTO)
1604 had_eol = TRUE;
1605#endif
1606 break;
1607 }
1608
1609 extra = ADD_NL;
1610 *flagp |= HASNL;
1611
1612 /* "\_[" is character range plus newline */
1613 if (c == '[')
1614 goto collection;
1615
1616 /* "\_x" is character class plus newline */
1617 /*FALLTHROUGH*/
1618
1619 /*
1620 * Character classes.
1621 */
1622 case Magic('.'):
1623 case Magic('i'):
1624 case Magic('I'):
1625 case Magic('k'):
1626 case Magic('K'):
1627 case Magic('f'):
1628 case Magic('F'):
1629 case Magic('p'):
1630 case Magic('P'):
1631 case Magic('s'):
1632 case Magic('S'):
1633 case Magic('d'):
1634 case Magic('D'):
1635 case Magic('x'):
1636 case Magic('X'):
1637 case Magic('o'):
1638 case Magic('O'):
1639 case Magic('w'):
1640 case Magic('W'):
1641 case Magic('h'):
1642 case Magic('H'):
1643 case Magic('a'):
1644 case Magic('A'):
1645 case Magic('l'):
1646 case Magic('L'):
1647 case Magic('u'):
1648 case Magic('U'):
1649 p = vim_strchr(classchars, no_Magic(c));
1650 if (p == NULL)
1651 EMSG_RET_NULL(_("E63: invalid use of \\_"));
1652 ret = regnode(classcodes[p - classchars] + extra);
1653 *flagp |= HASWIDTH | SIMPLE;
1654 break;
1655
1656 case Magic('n'):
1657 if (reg_string)
1658 {
1659 /* In a string "\n" matches a newline character. */
1660 ret = regnode(EXACTLY);
1661 regc(NL);
1662 regc(NUL);
1663 *flagp |= HASWIDTH | SIMPLE;
1664 }
1665 else
1666 {
1667 /* In buffer text "\n" matches the end of a line. */
1668 ret = regnode(NEWL);
1669 *flagp |= HASWIDTH | HASNL;
1670 }
1671 break;
1672
1673 case Magic('('):
1674 if (one_exactly)
1675 EMSG_ONE_RET_NULL;
1676 ret = reg(REG_PAREN, &flags);
1677 if (ret == NULL)
1678 return NULL;
1679 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1680 break;
1681
1682 case NUL:
1683 case Magic('|'):
1684 case Magic('&'):
1685 case Magic(')'):
1686 EMSG_RET_NULL(_(e_internal)); /* Supposed to be caught earlier. */
1687 /* NOTREACHED */
1688
1689 case Magic('='):
1690 case Magic('?'):
1691 case Magic('+'):
1692 case Magic('@'):
1693 case Magic('{'):
1694 case Magic('*'):
1695 c = no_Magic(c);
1696 sprintf((char *)IObuff, _("E64: %s%c follows nothing"),
1697 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL)
1698 ? "" : "\\", c);
1699 EMSG_RET_NULL(IObuff);
1700 /* NOTREACHED */
1701
1702 case Magic('~'): /* previous substitute pattern */
1703 if (reg_prev_sub)
1704 {
1705 char_u *lp;
1706
1707 ret = regnode(EXACTLY);
1708 lp = reg_prev_sub;
1709 while (*lp != NUL)
1710 regc(*lp++);
1711 regc(NUL);
1712 if (*reg_prev_sub != NUL)
1713 {
1714 *flagp |= HASWIDTH;
1715 if ((lp - reg_prev_sub) == 1)
1716 *flagp |= SIMPLE;
1717 }
1718 }
1719 else
1720 EMSG_RET_NULL(_(e_nopresub));
1721 break;
1722
1723 case Magic('1'):
1724 case Magic('2'):
1725 case Magic('3'):
1726 case Magic('4'):
1727 case Magic('5'):
1728 case Magic('6'):
1729 case Magic('7'):
1730 case Magic('8'):
1731 case Magic('9'):
1732 {
1733 int refnum;
1734
1735 refnum = c - Magic('0');
1736 /*
1737 * Check if the back reference is legal. We must have seen the
1738 * close brace.
1739 * TODO: Should also check that we don't refer to something
1740 * that is repeated (+*=): what instance of the repetition
1741 * should we match?
1742 */
1743 if (!had_endbrace[refnum])
1744 {
1745 /* Trick: check if "@<=" or "@<!" follows, in which case
1746 * the \1 can appear before the referenced match. */
1747 for (p = regparse; *p != NUL; ++p)
1748 if (p[0] == '@' && p[1] == '<'
1749 && (p[2] == '!' || p[2] == '='))
1750 break;
1751 if (*p == NUL)
1752 EMSG_RET_NULL(_("E65: Illegal back reference"));
1753 }
1754 ret = regnode(BACKREF + refnum);
1755 }
1756 break;
1757
1758#ifdef FEAT_SYN_HL
1759 case Magic('z'):
1760 {
1761 c = no_Magic(getchr());
1762 switch (c)
1763 {
1764 case '(': if (reg_do_extmatch != REX_SET)
1765 EMSG_RET_NULL(_("E66: \\z( not allowed here"));
1766 if (one_exactly)
1767 EMSG_ONE_RET_NULL;
1768 ret = reg(REG_ZPAREN, &flags);
1769 if (ret == NULL)
1770 return NULL;
1771 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
1772 re_has_z = REX_SET;
1773 break;
1774
1775 case '1':
1776 case '2':
1777 case '3':
1778 case '4':
1779 case '5':
1780 case '6':
1781 case '7':
1782 case '8':
1783 case '9': if (reg_do_extmatch != REX_USE)
1784 EMSG_RET_NULL(_("E67: \\z1 et al. not allowed here"));
1785 ret = regnode(ZREF + c - '0');
1786 re_has_z = REX_USE;
1787 break;
1788
1789 case 's': ret = regnode(MOPEN + 0);
1790 break;
1791
1792 case 'e': ret = regnode(MCLOSE + 0);
1793 break;
1794
1795 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
1796 }
1797 }
1798 break;
1799#endif
1800
1801 case Magic('%'):
1802 {
1803 c = no_Magic(getchr());
1804 switch (c)
1805 {
1806 /* () without a back reference */
1807 case '(':
1808 if (one_exactly)
1809 EMSG_ONE_RET_NULL;
1810 ret = reg(REG_NPAREN, &flags);
1811 if (ret == NULL)
1812 return NULL;
1813 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1814 break;
1815
1816 /* Catch \%^ and \%$ regardless of where they appear in the
1817 * pattern -- regardless of whether or not it makes sense. */
1818 case '^':
1819 ret = regnode(RE_BOF);
1820 break;
1821
1822 case '$':
1823 ret = regnode(RE_EOF);
1824 break;
1825
1826 case '#':
1827 ret = regnode(CURSOR);
1828 break;
1829
1830 /* \%[abc]: Emit as a list of branches, all ending at the last
1831 * branch which matches nothing. */
1832 case '[':
1833 if (one_exactly) /* doesn't nest */
1834 EMSG_ONE_RET_NULL;
1835 {
1836 char_u *lastbranch;
1837 char_u *lastnode = NULL;
1838 char_u *br;
1839
1840 ret = NULL;
1841 while ((c = getchr()) != ']')
1842 {
1843 if (c == NUL)
1844 EMSG_M_RET_NULL(_("E69: Missing ] after %s%%["),
1845 reg_magic == MAGIC_ALL);
1846 br = regnode(BRANCH);
1847 if (ret == NULL)
1848 ret = br;
1849 else
1850 regtail(lastnode, br);
1851
1852 ungetchr();
1853 one_exactly = TRUE;
1854 lastnode = regatom(flagp);
1855 one_exactly = FALSE;
1856 if (lastnode == NULL)
1857 return NULL;
1858 }
1859 if (ret == NULL)
1860 EMSG_M_RET_NULL(_("E70: Empty %s%%[]"),
1861 reg_magic == MAGIC_ALL);
1862 lastbranch = regnode(BRANCH);
1863 br = regnode(NOTHING);
1864 if (ret != JUST_CALC_SIZE)
1865 {
1866 regtail(lastnode, br);
1867 regtail(lastbranch, br);
1868 /* connect all branches to the NOTHING
1869 * branch at the end */
1870 for (br = ret; br != lastnode; )
1871 {
1872 if (OP(br) == BRANCH)
1873 {
1874 regtail(br, lastbranch);
1875 br = OPERAND(br);
1876 }
1877 else
1878 br = regnext(br);
1879 }
1880 }
1881 *flagp &= ~HASWIDTH;
1882 break;
1883 }
1884
Bram Moolenaarc0197e22004-09-13 20:26:32 +00001885 case 'd': /* %d123 decimal */
1886 case 'o': /* %o123 octal */
1887 case 'x': /* %xab hex 2 */
1888 case 'u': /* %uabcd hex 4 */
1889 case 'U': /* %U1234abcd hex 8 */
1890 {
1891 int i;
1892
1893 switch (c)
1894 {
1895 case 'd': i = getdecchrs(); break;
1896 case 'o': i = getoctchrs(); break;
1897 case 'x': i = gethexchrs(2); break;
1898 case 'u': i = gethexchrs(4); break;
1899 case 'U': i = gethexchrs(8); break;
1900 default: i = -1; break;
1901 }
1902
1903 if (i < 0)
1904 EMSG_M_RET_NULL(
1905 _("E678: Invalid character after %s%%[dxouU]"),
1906 reg_magic == MAGIC_ALL);
1907 ret = regnode(EXACTLY);
1908 if (i == 0)
1909 regc(0x0a);
1910 else
1911#ifdef FEAT_MBYTE
1912 regmbc(i);
1913#else
1914 regc(i);
1915#endif
1916 regc(NUL);
1917 *flagp |= HASWIDTH;
1918 break;
1919 }
1920
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 default:
1922 if (VIM_ISDIGIT(c) || c == '<' || c == '>')
1923 {
1924 long_u n = 0;
1925 int cmp;
1926
1927 cmp = c;
1928 if (cmp == '<' || cmp == '>')
1929 c = getchr();
1930 while (VIM_ISDIGIT(c))
1931 {
1932 n = n * 10 + (c - '0');
1933 c = getchr();
1934 }
1935 if (c == 'l' || c == 'c' || c == 'v')
1936 {
1937 if (c == 'l')
1938 ret = regnode(RE_LNUM);
1939 else if (c == 'c')
1940 ret = regnode(RE_COL);
1941 else
1942 ret = regnode(RE_VCOL);
1943 if (ret == JUST_CALC_SIZE)
1944 regsize += 5;
1945 else
1946 {
1947 /* put the number and the optional
1948 * comparator after the opcode */
1949 regcode = re_put_long(regcode, n);
1950 *regcode++ = cmp;
1951 }
1952 break;
1953 }
1954 }
1955
1956 EMSG_M_RET_NULL(_("E71: Invalid character after %s%%"),
1957 reg_magic == MAGIC_ALL);
1958 }
1959 }
1960 break;
1961
1962 case Magic('['):
1963collection:
1964 {
1965 char_u *lp;
1966
1967 /*
1968 * If there is no matching ']', we assume the '[' is a normal
1969 * character. This makes 'incsearch' and ":help [" work.
1970 */
1971 lp = skip_anyof(regparse);
1972 if (*lp == ']') /* there is a matching ']' */
1973 {
1974 int startc = -1; /* > 0 when next '-' is a range */
1975 int endc;
1976
1977 /*
1978 * In a character class, different parsing rules apply.
1979 * Not even \ is special anymore, nothing is.
1980 */
1981 if (*regparse == '^') /* Complement of range. */
1982 {
1983 ret = regnode(ANYBUT + extra);
1984 regparse++;
1985 }
1986 else
1987 ret = regnode(ANYOF + extra);
1988
1989 /* At the start ']' and '-' mean the literal character. */
1990 if (*regparse == ']' || *regparse == '-')
Bram Moolenaardf177f62005-02-22 08:39:57 +00001991 {
1992 startc = *regparse;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 regc(*regparse++);
Bram Moolenaardf177f62005-02-22 08:39:57 +00001994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996 while (*regparse != NUL && *regparse != ']')
1997 {
1998 if (*regparse == '-')
1999 {
2000 ++regparse;
2001 /* The '-' is not used for a range at the end and
2002 * after or before a '\n'. */
2003 if (*regparse == ']' || *regparse == NUL
2004 || startc == -1
2005 || (regparse[0] == '\\' && regparse[1] == 'n'))
2006 {
2007 regc('-');
2008 startc = '-'; /* [--x] is a range */
2009 }
2010 else
2011 {
Bram Moolenaardf177f62005-02-22 08:39:57 +00002012 /* Also accept "a-[.z.]" */
2013 endc = 0;
2014 if (*regparse == '[')
2015 endc = get_coll_element(&regparse);
2016 if (endc == 0)
2017 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018#ifdef FEAT_MBYTE
Bram Moolenaardf177f62005-02-22 08:39:57 +00002019 if (has_mbyte)
2020 endc = mb_ptr2char_adv(&regparse);
2021 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022#endif
Bram Moolenaardf177f62005-02-22 08:39:57 +00002023 endc = *regparse++;
2024 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002025
2026 /* Handle \o40, \x20 and \u20AC style sequences */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002027 if (endc == '\\' && !cpo_lit && !cpo_bsl)
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002028 endc = coll_get_char();
2029
Bram Moolenaar071d4272004-06-13 20:20:40 +00002030 if (startc > endc)
2031 EMSG_RET_NULL(_(e_invrange));
2032#ifdef FEAT_MBYTE
2033 if (has_mbyte && ((*mb_char2len)(startc) > 1
2034 || (*mb_char2len)(endc) > 1))
2035 {
2036 /* Limit to a range of 256 chars */
2037 if (endc > startc + 256)
2038 EMSG_RET_NULL(_(e_invrange));
2039 while (++startc <= endc)
2040 regmbc(startc);
2041 }
2042 else
2043#endif
2044 {
2045#ifdef EBCDIC
2046 int alpha_only = FALSE;
2047
2048 /* for alphabetical range skip the gaps
2049 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */
2050 if (isalpha(startc) && isalpha(endc))
2051 alpha_only = TRUE;
2052#endif
2053 while (++startc <= endc)
2054#ifdef EBCDIC
2055 if (!alpha_only || isalpha(startc))
2056#endif
2057 regc(startc);
2058 }
2059 startc = -1;
2060 }
2061 }
2062 /*
2063 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
2064 * accepts "\t", "\e", etc., but only when the 'l' flag in
2065 * 'cpoptions' is not included.
Bram Moolenaardf177f62005-02-22 08:39:57 +00002066 * Posix doesn't recognize backslash at all.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 */
2068 else if (*regparse == '\\'
Bram Moolenaardf177f62005-02-22 08:39:57 +00002069 && !cpo_bsl
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
2071 || (!cpo_lit
2072 && vim_strchr(REGEXP_ABBR,
2073 regparse[1]) != NULL)))
2074 {
2075 regparse++;
2076 if (*regparse == 'n')
2077 {
2078 /* '\n' in range: also match NL */
2079 if (ret != JUST_CALC_SIZE)
2080 {
2081 if (*ret == ANYBUT)
2082 *ret = ANYBUT + ADD_NL;
2083 else if (*ret == ANYOF)
2084 *ret = ANYOF + ADD_NL;
2085 /* else: must have had a \n already */
2086 }
2087 *flagp |= HASNL;
2088 regparse++;
2089 startc = -1;
2090 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002091 else if (*regparse == 'd'
2092 || *regparse == 'o'
2093 || *regparse == 'x'
2094 || *regparse == 'u'
2095 || *regparse == 'U')
2096 {
2097 startc = coll_get_char();
2098 if (startc == 0)
2099 regc(0x0a);
2100 else
2101#ifdef FEAT_MBYTE
2102 regmbc(startc);
2103#else
2104 regc(startc);
2105#endif
2106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002107 else
2108 {
2109 startc = backslash_trans(*regparse++);
2110 regc(startc);
2111 }
2112 }
2113 else if (*regparse == '[')
2114 {
2115 int c_class;
2116 int cu;
2117
Bram Moolenaardf177f62005-02-22 08:39:57 +00002118 c_class = get_char_class(&regparse);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119 startc = -1;
2120 /* Characters assumed to be 8 bits! */
2121 switch (c_class)
2122 {
2123 case CLASS_NONE:
Bram Moolenaardf177f62005-02-22 08:39:57 +00002124 c_class = get_equi_class(&regparse);
2125 if (c_class != 0)
2126 {
2127 /* produce equivalence class */
2128 reg_equi_class(c_class);
2129 }
2130 else if ((c_class =
2131 get_coll_element(&regparse)) != 0)
2132 {
2133 /* produce a collating element */
2134 regmbc(c_class);
2135 }
2136 else
2137 {
2138 /* literal '[', allow [[-x] as a range */
2139 startc = *regparse++;
2140 regc(startc);
2141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002142 break;
2143 case CLASS_ALNUM:
2144 for (cu = 1; cu <= 255; cu++)
2145 if (isalnum(cu))
2146 regc(cu);
2147 break;
2148 case CLASS_ALPHA:
2149 for (cu = 1; cu <= 255; cu++)
2150 if (isalpha(cu))
2151 regc(cu);
2152 break;
2153 case CLASS_BLANK:
2154 regc(' ');
2155 regc('\t');
2156 break;
2157 case CLASS_CNTRL:
2158 for (cu = 1; cu <= 255; cu++)
2159 if (iscntrl(cu))
2160 regc(cu);
2161 break;
2162 case CLASS_DIGIT:
2163 for (cu = 1; cu <= 255; cu++)
2164 if (VIM_ISDIGIT(cu))
2165 regc(cu);
2166 break;
2167 case CLASS_GRAPH:
2168 for (cu = 1; cu <= 255; cu++)
2169 if (isgraph(cu))
2170 regc(cu);
2171 break;
2172 case CLASS_LOWER:
2173 for (cu = 1; cu <= 255; cu++)
2174 if (islower(cu))
2175 regc(cu);
2176 break;
2177 case CLASS_PRINT:
2178 for (cu = 1; cu <= 255; cu++)
2179 if (vim_isprintc(cu))
2180 regc(cu);
2181 break;
2182 case CLASS_PUNCT:
2183 for (cu = 1; cu <= 255; cu++)
2184 if (ispunct(cu))
2185 regc(cu);
2186 break;
2187 case CLASS_SPACE:
2188 for (cu = 9; cu <= 13; cu++)
2189 regc(cu);
2190 regc(' ');
2191 break;
2192 case CLASS_UPPER:
2193 for (cu = 1; cu <= 255; cu++)
2194 if (isupper(cu))
2195 regc(cu);
2196 break;
2197 case CLASS_XDIGIT:
2198 for (cu = 1; cu <= 255; cu++)
2199 if (vim_isxdigit(cu))
2200 regc(cu);
2201 break;
2202 case CLASS_TAB:
2203 regc('\t');
2204 break;
2205 case CLASS_RETURN:
2206 regc('\r');
2207 break;
2208 case CLASS_BACKSPACE:
2209 regc('\b');
2210 break;
2211 case CLASS_ESCAPE:
2212 regc('\033');
2213 break;
2214 }
2215 }
2216 else
2217 {
2218#ifdef FEAT_MBYTE
2219 if (has_mbyte)
2220 {
2221 int len;
2222
2223 /* produce a multibyte character, including any
2224 * following composing characters */
2225 startc = mb_ptr2char(regparse);
2226 len = (*mb_ptr2len_check)(regparse);
2227 if (enc_utf8 && utf_char2len(startc) != len)
2228 startc = -1; /* composing chars */
2229 while (--len >= 0)
2230 regc(*regparse++);
2231 }
2232 else
2233#endif
2234 {
2235 startc = *regparse++;
2236 regc(startc);
2237 }
2238 }
2239 }
2240 regc(NUL);
2241 prevchr_len = 1; /* last char was the ']' */
2242 if (*regparse != ']')
2243 EMSG_RET_NULL(_(e_toomsbra)); /* Cannot happen? */
2244 skipchr(); /* let's be friends with the lexer again */
2245 *flagp |= HASWIDTH | SIMPLE;
2246 break;
2247 }
2248 }
2249 /* FALLTHROUGH */
2250
2251 default:
2252 {
2253 int len;
2254
2255#ifdef FEAT_MBYTE
2256 /* A multi-byte character is handled as a separate atom if it's
2257 * before a multi. */
2258 if (has_mbyte && (*mb_char2len)(c) > 1
2259 && re_multi_type(peekchr()) != NOT_MULTI)
2260 {
2261 ret = regnode(MULTIBYTECODE);
2262 regmbc(c);
2263 *flagp |= HASWIDTH | SIMPLE;
2264 break;
2265 }
2266#endif
2267
2268 ret = regnode(EXACTLY);
2269
2270 /*
2271 * Append characters as long as:
2272 * - there is no following multi, we then need the character in
2273 * front of it as a single character operand
2274 * - not running into a Magic character
2275 * - "one_exactly" is not set
2276 * But always emit at least one character. Might be a Multi,
2277 * e.g., a "[" without matching "]".
2278 */
2279 for (len = 0; c != NUL && (len == 0
2280 || (re_multi_type(peekchr()) == NOT_MULTI
2281 && !one_exactly
2282 && !is_Magic(c))); ++len)
2283 {
2284 c = no_Magic(c);
2285#ifdef FEAT_MBYTE
2286 if (has_mbyte)
2287 {
2288 regmbc(c);
2289 if (enc_utf8)
2290 {
2291 int off;
2292 int l;
2293
2294 /* Need to get composing character too, directly
2295 * access regparse for that, because skipchr() skips
2296 * over composing chars. */
2297 ungetchr();
2298 if (*regparse == '\\' && regparse[1] != NUL)
2299 off = 1;
2300 else
2301 off = 0;
2302 for (;;)
2303 {
2304 l = utf_ptr2len_check(regparse + off);
2305 if (!UTF_COMPOSINGLIKE(regparse + off,
2306 regparse + off + l))
2307 break;
2308 off += l;
2309 regmbc(utf_ptr2char(regparse + off));
2310 }
2311 skipchr();
2312 }
2313 }
2314 else
2315#endif
2316 regc(c);
2317 c = getchr();
2318 }
2319 ungetchr();
2320
2321 regc(NUL);
2322 *flagp |= HASWIDTH;
2323 if (len == 1)
2324 *flagp |= SIMPLE;
2325 }
2326 break;
2327 }
2328
2329 return ret;
2330}
2331
2332/*
2333 * emit a node
2334 * Return pointer to generated code.
2335 */
2336 static char_u *
2337regnode(op)
2338 int op;
2339{
2340 char_u *ret;
2341
2342 ret = regcode;
2343 if (ret == JUST_CALC_SIZE)
2344 regsize += 3;
2345 else
2346 {
2347 *regcode++ = op;
2348 *regcode++ = NUL; /* Null "next" pointer. */
2349 *regcode++ = NUL;
2350 }
2351 return ret;
2352}
2353
2354/*
2355 * Emit (if appropriate) a byte of code
2356 */
2357 static void
2358regc(b)
2359 int b;
2360{
2361 if (regcode == JUST_CALC_SIZE)
2362 regsize++;
2363 else
2364 *regcode++ = b;
2365}
2366
2367#ifdef FEAT_MBYTE
2368/*
2369 * Emit (if appropriate) a multi-byte character of code
2370 */
2371 static void
2372regmbc(c)
2373 int c;
2374{
2375 if (regcode == JUST_CALC_SIZE)
2376 regsize += (*mb_char2len)(c);
2377 else
2378 regcode += (*mb_char2bytes)(c, regcode);
2379}
2380#endif
2381
2382/*
2383 * reginsert - insert an operator in front of already-emitted operand
2384 *
2385 * Means relocating the operand.
2386 */
2387 static void
2388reginsert(op, opnd)
2389 int op;
2390 char_u *opnd;
2391{
2392 char_u *src;
2393 char_u *dst;
2394 char_u *place;
2395
2396 if (regcode == JUST_CALC_SIZE)
2397 {
2398 regsize += 3;
2399 return;
2400 }
2401 src = regcode;
2402 regcode += 3;
2403 dst = regcode;
2404 while (src > opnd)
2405 *--dst = *--src;
2406
2407 place = opnd; /* Op node, where operand used to be. */
2408 *place++ = op;
2409 *place++ = NUL;
2410 *place = NUL;
2411}
2412
2413/*
2414 * reginsert_limits - insert an operator in front of already-emitted operand.
2415 * The operator has the given limit values as operands. Also set next pointer.
2416 *
2417 * Means relocating the operand.
2418 */
2419 static void
2420reginsert_limits(op, minval, maxval, opnd)
2421 int op;
2422 long minval;
2423 long maxval;
2424 char_u *opnd;
2425{
2426 char_u *src;
2427 char_u *dst;
2428 char_u *place;
2429
2430 if (regcode == JUST_CALC_SIZE)
2431 {
2432 regsize += 11;
2433 return;
2434 }
2435 src = regcode;
2436 regcode += 11;
2437 dst = regcode;
2438 while (src > opnd)
2439 *--dst = *--src;
2440
2441 place = opnd; /* Op node, where operand used to be. */
2442 *place++ = op;
2443 *place++ = NUL;
2444 *place++ = NUL;
2445 place = re_put_long(place, (long_u)minval);
2446 place = re_put_long(place, (long_u)maxval);
2447 regtail(opnd, place);
2448}
2449
2450/*
2451 * Write a long as four bytes at "p" and return pointer to the next char.
2452 */
2453 static char_u *
2454re_put_long(p, val)
2455 char_u *p;
2456 long_u val;
2457{
2458 *p++ = (char_u) ((val >> 24) & 0377);
2459 *p++ = (char_u) ((val >> 16) & 0377);
2460 *p++ = (char_u) ((val >> 8) & 0377);
2461 *p++ = (char_u) (val & 0377);
2462 return p;
2463}
2464
2465/*
2466 * regtail - set the next-pointer at the end of a node chain
2467 */
2468 static void
2469regtail(p, val)
2470 char_u *p;
2471 char_u *val;
2472{
2473 char_u *scan;
2474 char_u *temp;
2475 int offset;
2476
2477 if (p == JUST_CALC_SIZE)
2478 return;
2479
2480 /* Find last node. */
2481 scan = p;
2482 for (;;)
2483 {
2484 temp = regnext(scan);
2485 if (temp == NULL)
2486 break;
2487 scan = temp;
2488 }
2489
Bram Moolenaar582fd852005-03-28 20:58:01 +00002490 if (OP(scan) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 offset = (int)(scan - val);
2492 else
2493 offset = (int)(val - scan);
2494 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
2495 *(scan + 2) = (char_u) (offset & 0377);
2496}
2497
2498/*
2499 * regoptail - regtail on item after a BRANCH; nop if none
2500 */
2501 static void
2502regoptail(p, val)
2503 char_u *p;
2504 char_u *val;
2505{
2506 /* When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless" */
2507 if (p == NULL || p == JUST_CALC_SIZE
2508 || (OP(p) != BRANCH
2509 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
2510 return;
2511 regtail(OPERAND(p), val);
2512}
2513
2514/*
2515 * getchr() - get the next character from the pattern. We know about
2516 * magic and such, so therefore we need a lexical analyzer.
2517 */
2518
2519/* static int curchr; */
2520static int prevprevchr;
2521static int prevchr;
2522static int nextchr; /* used for ungetchr() */
2523/*
2524 * Note: prevchr is sometimes -1 when we are not at the start,
2525 * eg in /[ ^I]^ the pattern was never found even if it existed, because ^ was
2526 * taken to be magic -- webb
2527 */
2528static int at_start; /* True when on the first character */
2529static int prev_at_start; /* True when on the second character */
2530
2531 static void
2532initchr(str)
2533 char_u *str;
2534{
2535 regparse = str;
2536 prevchr_len = 0;
2537 curchr = prevprevchr = prevchr = nextchr = -1;
2538 at_start = TRUE;
2539 prev_at_start = FALSE;
2540}
2541
2542 static int
2543peekchr()
2544{
Bram Moolenaardf177f62005-02-22 08:39:57 +00002545 static int after_slash = FALSE;
2546
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 if (curchr == -1)
2548 {
2549 switch (curchr = regparse[0])
2550 {
2551 case '.':
2552 case '[':
2553 case '~':
2554 /* magic when 'magic' is on */
2555 if (reg_magic >= MAGIC_ON)
2556 curchr = Magic(curchr);
2557 break;
2558 case '(':
2559 case ')':
2560 case '{':
2561 case '%':
2562 case '+':
2563 case '=':
2564 case '?':
2565 case '@':
2566 case '!':
2567 case '&':
2568 case '|':
2569 case '<':
2570 case '>':
2571 case '#': /* future ext. */
2572 case '"': /* future ext. */
2573 case '\'': /* future ext. */
2574 case ',': /* future ext. */
2575 case '-': /* future ext. */
2576 case ':': /* future ext. */
2577 case ';': /* future ext. */
2578 case '`': /* future ext. */
2579 case '/': /* Can't be used in / command */
2580 /* magic only after "\v" */
2581 if (reg_magic == MAGIC_ALL)
2582 curchr = Magic(curchr);
2583 break;
2584 case '*':
Bram Moolenaardf177f62005-02-22 08:39:57 +00002585 /* * is not magic as the very first character, eg "?*ptr", when
2586 * after '^', eg "/^*ptr" and when after "\(", "\|", "\&". But
2587 * "\(\*" is not magic, thus must be magic if "after_slash" */
2588 if (reg_magic >= MAGIC_ON
2589 && !at_start
2590 && !(prev_at_start && prevchr == Magic('^'))
2591 && (after_slash
2592 || (prevchr != Magic('(')
2593 && prevchr != Magic('&')
2594 && prevchr != Magic('|'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 curchr = Magic('*');
2596 break;
2597 case '^':
2598 /* '^' is only magic as the very first character and if it's after
2599 * "\(", "\|", "\&' or "\n" */
2600 if (reg_magic >= MAGIC_OFF
2601 && (at_start
2602 || reg_magic == MAGIC_ALL
2603 || prevchr == Magic('(')
2604 || prevchr == Magic('|')
2605 || prevchr == Magic('&')
2606 || prevchr == Magic('n')
2607 || (no_Magic(prevchr) == '('
2608 && prevprevchr == Magic('%'))))
2609 {
2610 curchr = Magic('^');
2611 at_start = TRUE;
2612 prev_at_start = FALSE;
2613 }
2614 break;
2615 case '$':
2616 /* '$' is only magic as the very last char and if it's in front of
2617 * either "\|", "\)", "\&", or "\n" */
2618 if (reg_magic >= MAGIC_OFF)
2619 {
2620 char_u *p = regparse + 1;
2621
2622 /* ignore \c \C \m and \M after '$' */
2623 while (p[0] == '\\' && (p[1] == 'c' || p[1] == 'C'
2624 || p[1] == 'm' || p[1] == 'M' || p[1] == 'Z'))
2625 p += 2;
2626 if (p[0] == NUL
2627 || (p[0] == '\\'
2628 && (p[1] == '|' || p[1] == '&' || p[1] == ')'
2629 || p[1] == 'n'))
2630 || reg_magic == MAGIC_ALL)
2631 curchr = Magic('$');
2632 }
2633 break;
2634 case '\\':
2635 {
2636 int c = regparse[1];
2637
2638 if (c == NUL)
2639 curchr = '\\'; /* trailing '\' */
2640 else if (
2641#ifdef EBCDIC
2642 vim_strchr(META, c)
2643#else
2644 c <= '~' && META_flags[c]
2645#endif
2646 )
2647 {
2648 /*
2649 * META contains everything that may be magic sometimes,
2650 * except ^ and $ ("\^" and "\$" are only magic after
2651 * "\v"). We now fetch the next character and toggle its
2652 * magicness. Therefore, \ is so meta-magic that it is
2653 * not in META.
2654 */
2655 curchr = -1;
2656 prev_at_start = at_start;
2657 at_start = FALSE; /* be able to say "/\*ptr" */
2658 ++regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002659 ++after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 peekchr();
2661 --regparse;
Bram Moolenaardf177f62005-02-22 08:39:57 +00002662 --after_slash;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002663 curchr = toggle_Magic(curchr);
2664 }
2665 else if (vim_strchr(REGEXP_ABBR, c))
2666 {
2667 /*
2668 * Handle abbreviations, like "\t" for TAB -- webb
2669 */
2670 curchr = backslash_trans(c);
2671 }
2672 else if (reg_magic == MAGIC_NONE && (c == '$' || c == '^'))
2673 curchr = toggle_Magic(c);
2674 else
2675 {
2676 /*
2677 * Next character can never be (made) magic?
2678 * Then backslashing it won't do anything.
2679 */
2680#ifdef FEAT_MBYTE
2681 if (has_mbyte)
2682 curchr = (*mb_ptr2char)(regparse + 1);
2683 else
2684#endif
2685 curchr = c;
2686 }
2687 break;
2688 }
2689
2690#ifdef FEAT_MBYTE
2691 default:
2692 if (has_mbyte)
2693 curchr = (*mb_ptr2char)(regparse);
2694#endif
2695 }
2696 }
2697
2698 return curchr;
2699}
2700
2701/*
2702 * Eat one lexed character. Do this in a way that we can undo it.
2703 */
2704 static void
2705skipchr()
2706{
2707 /* peekchr() eats a backslash, do the same here */
2708 if (*regparse == '\\')
2709 prevchr_len = 1;
2710 else
2711 prevchr_len = 0;
2712 if (regparse[prevchr_len] != NUL)
2713 {
2714#ifdef FEAT_MBYTE
2715 if (has_mbyte)
2716 prevchr_len += (*mb_ptr2len_check)(regparse + prevchr_len);
2717 else
2718#endif
2719 ++prevchr_len;
2720 }
2721 regparse += prevchr_len;
2722 prev_at_start = at_start;
2723 at_start = FALSE;
2724 prevprevchr = prevchr;
2725 prevchr = curchr;
2726 curchr = nextchr; /* use previously unget char, or -1 */
2727 nextchr = -1;
2728}
2729
2730/*
2731 * Skip a character while keeping the value of prev_at_start for at_start.
2732 * prevchr and prevprevchr are also kept.
2733 */
2734 static void
2735skipchr_keepstart()
2736{
2737 int as = prev_at_start;
2738 int pr = prevchr;
2739 int prpr = prevprevchr;
2740
2741 skipchr();
2742 at_start = as;
2743 prevchr = pr;
2744 prevprevchr = prpr;
2745}
2746
2747 static int
2748getchr()
2749{
2750 int chr = peekchr();
2751
2752 skipchr();
2753 return chr;
2754}
2755
2756/*
2757 * put character back. Works only once!
2758 */
2759 static void
2760ungetchr()
2761{
2762 nextchr = curchr;
2763 curchr = prevchr;
2764 prevchr = prevprevchr;
2765 at_start = prev_at_start;
2766 prev_at_start = FALSE;
2767
2768 /* Backup regparse, so that it's at the same position as before the
2769 * getchr(). */
2770 regparse -= prevchr_len;
2771}
2772
2773/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00002774 * Get and return the value of the hex string at the current position.
2775 * Return -1 if there is no valid hex number.
2776 * The position is updated:
Bram Moolenaarc0197e22004-09-13 20:26:32 +00002777 * blahblah\%x20asdf
2778 * before-^ ^-after
2779 * The parameter controls the maximum number of input characters. This will be
2780 * 2 when reading a \%x20 sequence and 4 when reading a \%u20AC sequence.
2781 */
2782 static int
2783gethexchrs(maxinputlen)
2784 int maxinputlen;
2785{
2786 int nr = 0;
2787 int c;
2788 int i;
2789
2790 for (i = 0; i < maxinputlen; ++i)
2791 {
2792 c = regparse[0];
2793 if (!vim_isxdigit(c))
2794 break;
2795 nr <<= 4;
2796 nr |= hex2nr(c);
2797 ++regparse;
2798 }
2799
2800 if (i == 0)
2801 return -1;
2802 return nr;
2803}
2804
2805/*
2806 * get and return the value of the decimal string immediately after the
2807 * current position. Return -1 for invalid. Consumes all digits.
2808 */
2809 static int
2810getdecchrs()
2811{
2812 int nr = 0;
2813 int c;
2814 int i;
2815
2816 for (i = 0; ; ++i)
2817 {
2818 c = regparse[0];
2819 if (c < '0' || c > '9')
2820 break;
2821 nr *= 10;
2822 nr += c - '0';
2823 ++regparse;
2824 }
2825
2826 if (i == 0)
2827 return -1;
2828 return nr;
2829}
2830
2831/*
2832 * get and return the value of the octal string immediately after the current
2833 * position. Return -1 for invalid, or 0-255 for valid. Smart enough to handle
2834 * numbers > 377 correctly (for example, 400 is treated as 40) and doesn't
2835 * treat 8 or 9 as recognised characters. Position is updated:
2836 * blahblah\%o210asdf
2837 * before-^ ^-after
2838 */
2839 static int
2840getoctchrs()
2841{
2842 int nr = 0;
2843 int c;
2844 int i;
2845
2846 for (i = 0; i < 3 && nr < 040; ++i)
2847 {
2848 c = regparse[0];
2849 if (c < '0' || c > '7')
2850 break;
2851 nr <<= 3;
2852 nr |= hex2nr(c);
2853 ++regparse;
2854 }
2855
2856 if (i == 0)
2857 return -1;
2858 return nr;
2859}
2860
2861/*
2862 * Get a number after a backslash that is inside [].
2863 * When nothing is recognized return a backslash.
2864 */
2865 static int
2866coll_get_char()
2867{
2868 int nr = -1;
2869
2870 switch (*regparse++)
2871 {
2872 case 'd': nr = getdecchrs(); break;
2873 case 'o': nr = getoctchrs(); break;
2874 case 'x': nr = gethexchrs(2); break;
2875 case 'u': nr = gethexchrs(4); break;
2876 case 'U': nr = gethexchrs(8); break;
2877 }
2878 if (nr < 0)
2879 {
2880 /* If getting the number fails be backwards compatible: the character
2881 * is a backslash. */
2882 --regparse;
2883 nr = '\\';
2884 }
2885 return nr;
2886}
2887
2888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 * read_limits - Read two integers to be taken as a minimum and maximum.
2890 * If the first character is '-', then the range is reversed.
2891 * Should end with 'end'. If minval is missing, zero is default, if maxval is
2892 * missing, a very big number is the default.
2893 */
2894 static int
2895read_limits(minval, maxval)
2896 long *minval;
2897 long *maxval;
2898{
2899 int reverse = FALSE;
2900 char_u *first_char;
2901 long tmp;
2902
2903 if (*regparse == '-')
2904 {
2905 /* Starts with '-', so reverse the range later */
2906 regparse++;
2907 reverse = TRUE;
2908 }
2909 first_char = regparse;
2910 *minval = getdigits(&regparse);
2911 if (*regparse == ',') /* There is a comma */
2912 {
2913 if (vim_isdigit(*++regparse))
2914 *maxval = getdigits(&regparse);
2915 else
2916 *maxval = MAX_LIMIT;
2917 }
2918 else if (VIM_ISDIGIT(*first_char))
2919 *maxval = *minval; /* It was \{n} or \{-n} */
2920 else
2921 *maxval = MAX_LIMIT; /* It was \{} or \{-} */
2922 if (*regparse == '\\')
2923 regparse++; /* Allow either \{...} or \{...\} */
Bram Moolenaardf177f62005-02-22 08:39:57 +00002924 if (*regparse != '}')
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 {
2926 sprintf((char *)IObuff, _("E554: Syntax error in %s{...}"),
2927 reg_magic == MAGIC_ALL ? "" : "\\");
2928 EMSG_RET_FAIL(IObuff);
2929 }
2930
2931 /*
2932 * Reverse the range if there was a '-', or make sure it is in the right
2933 * order otherwise.
2934 */
2935 if ((!reverse && *minval > *maxval) || (reverse && *minval < *maxval))
2936 {
2937 tmp = *minval;
2938 *minval = *maxval;
2939 *maxval = tmp;
2940 }
2941 skipchr(); /* let's be friends with the lexer again */
2942 return OK;
2943}
2944
2945/*
2946 * vim_regexec and friends
2947 */
2948
2949/*
2950 * Global work variables for vim_regexec().
2951 */
2952
2953/* The current match-position is remembered with these variables: */
2954static linenr_T reglnum; /* line number, relative to first line */
2955static char_u *regline; /* start of current line */
2956static char_u *reginput; /* current input, points into "regline" */
2957
2958static int need_clear_subexpr; /* subexpressions still need to be
2959 * cleared */
2960#ifdef FEAT_SYN_HL
2961static int need_clear_zsubexpr = FALSE; /* extmatch subexpressions
2962 * still need to be cleared */
2963#endif
2964
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965/*
2966 * Structure used to save the current input state, when it needs to be
2967 * restored after trying a match. Used by reg_save() and reg_restore().
Bram Moolenaar582fd852005-03-28 20:58:01 +00002968 * Also stores the length of "backpos".
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 */
2970typedef struct
2971{
2972 union
2973 {
2974 char_u *ptr; /* reginput pointer, for single-line regexp */
2975 lpos_T pos; /* reginput pos, for multi-line regexp */
2976 } rs_u;
Bram Moolenaar582fd852005-03-28 20:58:01 +00002977 int rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978} regsave_T;
2979
2980/* struct to save start/end pointer/position in for \(\) */
2981typedef struct
2982{
2983 union
2984 {
2985 char_u *ptr;
2986 lpos_T pos;
2987 } se_u;
2988} save_se_T;
2989
2990static char_u *reg_getline __ARGS((linenr_T lnum));
2991static long vim_regexec_both __ARGS((char_u *line, colnr_T col));
2992static long regtry __ARGS((regprog_T *prog, colnr_T col));
2993static void cleanup_subexpr __ARGS((void));
2994#ifdef FEAT_SYN_HL
2995static void cleanup_zsubexpr __ARGS((void));
2996#endif
2997static void reg_nextline __ARGS((void));
Bram Moolenaar582fd852005-03-28 20:58:01 +00002998static void reg_save __ARGS((regsave_T *save, garray_T *gap));
2999static void reg_restore __ARGS((regsave_T *save, garray_T *gap));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000static int reg_save_equal __ARGS((regsave_T *save));
3001static void save_se_multi __ARGS((save_se_T *savep, lpos_T *posp));
3002static void save_se_one __ARGS((save_se_T *savep, char_u **pp));
3003
3004/* Save the sub-expressions before attempting a match. */
3005#define save_se(savep, posp, pp) \
3006 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
3007
3008/* After a failed match restore the sub-expressions. */
3009#define restore_se(savep, posp, pp) { \
3010 if (REG_MULTI) \
3011 *(posp) = (savep)->se_u.pos; \
3012 else \
3013 *(pp) = (savep)->se_u.ptr; }
3014
3015static int re_num_cmp __ARGS((long_u val, char_u *scan));
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003016static int regmatch __ARGS((char_u *prog));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017static int regrepeat __ARGS((char_u *p, long maxcount));
3018
3019#ifdef DEBUG
3020int regnarrate = 0;
3021#endif
3022
3023/*
3024 * Internal copy of 'ignorecase'. It is set at each call to vim_regexec().
3025 * Normally it gets the value of "rm_ic" or "rmm_ic", but when the pattern
3026 * contains '\c' or '\C' the value is overruled.
3027 */
3028static int ireg_ic;
3029
3030#ifdef FEAT_MBYTE
3031/*
3032 * Similar to ireg_ic, but only for 'combining' characters. Set with \Z flag
3033 * in the regexp. Defaults to false, always.
3034 */
3035static int ireg_icombine;
3036#endif
3037
3038/*
3039 * Sometimes need to save a copy of a line. Since alloc()/free() is very
3040 * slow, we keep one allocated piece of memory and only re-allocate it when
3041 * it's too small. It's freed in vim_regexec_both() when finished.
3042 */
3043static char_u *reg_tofree;
3044static unsigned reg_tofreelen;
3045
3046/*
3047 * These variables are set when executing a regexp to speed up the execution.
3048 * Which ones are set depends on whethere a single-line or multi-line match is
3049 * done:
3050 * single-line multi-line
3051 * reg_match &regmatch_T NULL
3052 * reg_mmatch NULL &regmmatch_T
3053 * reg_startp reg_match->startp <invalid>
3054 * reg_endp reg_match->endp <invalid>
3055 * reg_startpos <invalid> reg_mmatch->startpos
3056 * reg_endpos <invalid> reg_mmatch->endpos
3057 * reg_win NULL window in which to search
3058 * reg_buf <invalid> buffer in which to search
3059 * reg_firstlnum <invalid> first line in which to search
3060 * reg_maxline 0 last line nr
3061 * reg_line_lbr FALSE or TRUE FALSE
3062 */
3063static regmatch_T *reg_match;
3064static regmmatch_T *reg_mmatch;
3065static char_u **reg_startp = NULL;
3066static char_u **reg_endp = NULL;
3067static lpos_T *reg_startpos = NULL;
3068static lpos_T *reg_endpos = NULL;
3069static win_T *reg_win;
3070static buf_T *reg_buf;
3071static linenr_T reg_firstlnum;
3072static linenr_T reg_maxline;
3073static int reg_line_lbr; /* "\n" in string is line break */
3074
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003075/* Values for rs_state in regitem_T. */
3076typedef enum regstate_E
3077{
3078 RS_NOPEN = 0 /* NOPEN and NCLOSE */
3079 , RS_MOPEN /* MOPEN + [0-9] */
3080 , RS_MCLOSE /* MCLOSE + [0-9] */
3081#ifdef FEAT_SYN_HL
3082 , RS_ZOPEN /* ZOPEN + [0-9] */
3083 , RS_ZCLOSE /* ZCLOSE + [0-9] */
3084#endif
3085 , RS_BRANCH /* BRANCH */
3086 , RS_BRCPLX_MORE /* BRACE_COMPLEX and trying one more match */
3087 , RS_BRCPLX_LONG /* BRACE_COMPLEX and trying longest match */
3088 , RS_BRCPLX_SHORT /* BRACE_COMPLEX and trying shortest match */
3089 , RS_NOMATCH /* NOMATCH */
3090 , RS_BEHIND1 /* BEHIND / NOBEHIND matching rest */
3091 , RS_BEHIND2 /* BEHIND / NOBEHIND matching behind part */
3092 , RS_STAR_LONG /* STAR/PLUS/BRACE_SIMPLE longest match */
3093 , RS_STAR_SHORT /* STAR/PLUS/BRACE_SIMPLE shortest match */
3094} regstate_T;
3095
3096/*
3097 * When there are alternatives a regstate_T is put on the regstack to remember
3098 * what we are doing.
3099 * Before it may be another type of item, depending on rs_state, to remember
3100 * more things.
3101 */
3102typedef struct regitem_S
3103{
3104 regstate_T rs_state; /* what we are doing, one of RS_ above */
3105 char_u *rs_scan; /* current node in program */
3106 union
3107 {
3108 save_se_T sesave;
3109 regsave_T regsave;
3110 } rs_un; /* room for saving reginput */
3111 short rs_no; /* submatch nr */
3112} regitem_T;
3113
3114static regitem_T *regstack_push __ARGS((regstate_T state, char_u *scan));
3115static void regstack_pop __ARGS((char_u **scan));
3116
3117/* used for BEHIND and NOBEHIND matching */
3118typedef struct regbehind_S
3119{
3120 regsave_T save_after;
3121 regsave_T save_behind;
3122} regbehind_T;
3123
3124/* used for STAR, PLUS and BRACE_SIMPLE matching */
3125typedef struct regstar_S
3126{
3127 int nextb; /* next byte */
3128 int nextb_ic; /* next byte reverse case */
3129 long count;
3130 long minval;
3131 long maxval;
3132} regstar_T;
3133
3134/* used to store input position when a BACK was encountered, so that we now if
3135 * we made any progress since the last time. */
3136typedef struct backpos_S
3137{
3138 char_u *bp_scan; /* "scan" where BACK was encountered */
3139 regsave_T bp_pos; /* last input position */
3140} backpos_T;
3141
3142/*
3143 * regstack and backpos are used by regmatch(). They are kept over calls to
3144 * avoid invoking malloc() and free() often.
3145 */
3146static garray_T regstack; /* stack with regitem_T items, sometimes
3147 preceded by regstar_T or regbehind_T. */
3148static garray_T backpos; /* table with backpos_T for BACK */
3149
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150/*
3151 * Get pointer to the line "lnum", which is relative to "reg_firstlnum".
3152 */
3153 static char_u *
3154reg_getline(lnum)
3155 linenr_T lnum;
3156{
3157 /* when looking behind for a match/no-match lnum is negative. But we
3158 * can't go before line 1 */
3159 if (reg_firstlnum + lnum < 1)
3160 return NULL;
3161 return ml_get_buf(reg_buf, reg_firstlnum + lnum, FALSE);
3162}
3163
3164static regsave_T behind_pos;
3165
3166#ifdef FEAT_SYN_HL
3167static char_u *reg_startzp[NSUBEXP]; /* Workspace to mark beginning */
3168static char_u *reg_endzp[NSUBEXP]; /* and end of \z(...\) matches */
3169static lpos_T reg_startzpos[NSUBEXP]; /* idem, beginning pos */
3170static lpos_T reg_endzpos[NSUBEXP]; /* idem, end pos */
3171#endif
3172
3173/* TRUE if using multi-line regexp. */
3174#define REG_MULTI (reg_match == NULL)
3175
3176/*
3177 * Match a regexp against a string.
3178 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3179 * Uses curbuf for line count and 'iskeyword'.
3180 *
3181 * Return TRUE if there is a match, FALSE if not.
3182 */
3183 int
3184vim_regexec(rmp, line, col)
3185 regmatch_T *rmp;
3186 char_u *line; /* string to match against */
3187 colnr_T col; /* column to start looking for match */
3188{
3189 reg_match = rmp;
3190 reg_mmatch = NULL;
3191 reg_maxline = 0;
3192 reg_line_lbr = FALSE;
3193 reg_win = NULL;
3194 ireg_ic = rmp->rm_ic;
3195#ifdef FEAT_MBYTE
3196 ireg_icombine = FALSE;
3197#endif
3198 return (vim_regexec_both(line, col) != 0);
3199}
3200
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003201#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \
3202 || defined(FIND_REPLACE_DIALOG) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203/*
3204 * Like vim_regexec(), but consider a "\n" in "line" to be a line break.
3205 */
3206 int
3207vim_regexec_nl(rmp, line, col)
3208 regmatch_T *rmp;
3209 char_u *line; /* string to match against */
3210 colnr_T col; /* column to start looking for match */
3211{
3212 reg_match = rmp;
3213 reg_mmatch = NULL;
3214 reg_maxline = 0;
3215 reg_line_lbr = TRUE;
3216 reg_win = NULL;
3217 ireg_ic = rmp->rm_ic;
3218#ifdef FEAT_MBYTE
3219 ireg_icombine = FALSE;
3220#endif
3221 return (vim_regexec_both(line, col) != 0);
3222}
3223#endif
3224
3225/*
3226 * Match a regexp against multiple lines.
3227 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
3228 * Uses curbuf for line count and 'iskeyword'.
3229 *
3230 * Return zero if there is no match. Return number of lines contained in the
3231 * match otherwise.
3232 */
3233 long
3234vim_regexec_multi(rmp, win, buf, lnum, col)
3235 regmmatch_T *rmp;
3236 win_T *win; /* window in which to search or NULL */
3237 buf_T *buf; /* buffer in which to search */
3238 linenr_T lnum; /* nr of line to start looking for match */
3239 colnr_T col; /* column to start looking for match */
3240{
3241 long r;
3242 buf_T *save_curbuf = curbuf;
3243
3244 reg_match = NULL;
3245 reg_mmatch = rmp;
3246 reg_buf = buf;
3247 reg_win = win;
3248 reg_firstlnum = lnum;
3249 reg_maxline = reg_buf->b_ml.ml_line_count - lnum;
3250 reg_line_lbr = FALSE;
3251 ireg_ic = rmp->rmm_ic;
3252#ifdef FEAT_MBYTE
3253 ireg_icombine = FALSE;
3254#endif
3255
3256 /* Need to switch to buffer "buf" to make vim_iswordc() work. */
3257 curbuf = buf;
3258 r = vim_regexec_both(NULL, col);
3259 curbuf = save_curbuf;
3260
3261 return r;
3262}
3263
3264/*
3265 * Match a regexp against a string ("line" points to the string) or multiple
3266 * lines ("line" is NULL, use reg_getline()).
3267 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 static long
3269vim_regexec_both(line, col)
3270 char_u *line;
3271 colnr_T col; /* column to start looking for match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003272{
3273 regprog_T *prog;
3274 char_u *s;
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003275 long retval = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276
3277 reg_tofree = NULL;
3278
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003279 /* Init the regstack empty. Use an item size of 1 byte, since we push
3280 * different things onto it. Use a large grow size to avoid reallocating
3281 * it too often. */
3282 ga_init2(&regstack, 1, 10000);
3283
3284 /* Init the backpos table empty. */
3285 ga_init2(&backpos, sizeof(backpos_T), 10);
3286
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 if (REG_MULTI)
3288 {
3289 prog = reg_mmatch->regprog;
3290 line = reg_getline((linenr_T)0);
3291 reg_startpos = reg_mmatch->startpos;
3292 reg_endpos = reg_mmatch->endpos;
3293 }
3294 else
3295 {
3296 prog = reg_match->regprog;
3297 reg_startp = reg_match->startp;
3298 reg_endp = reg_match->endp;
3299 }
3300
3301 /* Be paranoid... */
3302 if (prog == NULL || line == NULL)
3303 {
3304 EMSG(_(e_null));
3305 goto theend;
3306 }
3307
3308 /* Check validity of program. */
3309 if (prog_magic_wrong())
3310 goto theend;
3311
3312 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */
3313 if (prog->regflags & RF_ICASE)
3314 ireg_ic = TRUE;
3315 else if (prog->regflags & RF_NOICASE)
3316 ireg_ic = FALSE;
3317
3318#ifdef FEAT_MBYTE
3319 /* If pattern contains "\Z" overrule value of ireg_icombine */
3320 if (prog->regflags & RF_ICOMBINE)
3321 ireg_icombine = TRUE;
3322#endif
3323
3324 /* If there is a "must appear" string, look for it. */
3325 if (prog->regmust != NULL)
3326 {
3327 int c;
3328
3329#ifdef FEAT_MBYTE
3330 if (has_mbyte)
3331 c = (*mb_ptr2char)(prog->regmust);
3332 else
3333#endif
3334 c = *prog->regmust;
3335 s = line + col;
Bram Moolenaar05159a02005-02-26 23:04:13 +00003336
3337 /*
3338 * This is used very often, esp. for ":global". Use three versions of
3339 * the loop to avoid overhead of conditions.
3340 */
3341 if (!ireg_ic
3342#ifdef FEAT_MBYTE
3343 && !has_mbyte
3344#endif
3345 )
3346 while ((s = vim_strbyte(s, c)) != NULL)
3347 {
3348 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3349 break; /* Found it. */
3350 ++s;
3351 }
3352#ifdef FEAT_MBYTE
3353 else if (!ireg_ic || (!enc_utf8 && mb_char2len(c) > 1))
3354 while ((s = vim_strchr(s, c)) != NULL)
3355 {
3356 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3357 break; /* Found it. */
3358 mb_ptr_adv(s);
3359 }
3360#endif
3361 else
3362 while ((s = cstrchr(s, c)) != NULL)
3363 {
3364 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
3365 break; /* Found it. */
3366 mb_ptr_adv(s);
3367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368 if (s == NULL) /* Not present. */
3369 goto theend;
3370 }
3371
3372 regline = line;
3373 reglnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374
3375 /* Simplest case: Anchored match need be tried only once. */
3376 if (prog->reganch)
3377 {
3378 int c;
3379
3380#ifdef FEAT_MBYTE
3381 if (has_mbyte)
3382 c = (*mb_ptr2char)(regline + col);
3383 else
3384#endif
3385 c = regline[col];
3386 if (prog->regstart == NUL
3387 || prog->regstart == c
3388 || (ireg_ic && ((
3389#ifdef FEAT_MBYTE
3390 (enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
3391 || (c < 255 && prog->regstart < 255 &&
3392#endif
3393 TOLOWER_LOC(prog->regstart) == TOLOWER_LOC(c)))))
3394 retval = regtry(prog, col);
3395 else
3396 retval = 0;
3397 }
3398 else
3399 {
3400 /* Messy cases: unanchored match. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003401 while (!got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 {
3403 if (prog->regstart != NUL)
3404 {
Bram Moolenaar05159a02005-02-26 23:04:13 +00003405 /* Skip until the char we know it must start with.
3406 * Used often, do some work to avoid call overhead. */
3407 if (!ireg_ic
3408#ifdef FEAT_MBYTE
3409 && !has_mbyte
3410#endif
3411 )
3412 s = vim_strbyte(regline + col, prog->regstart);
3413 else
3414 s = cstrchr(regline + col, prog->regstart);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 if (s == NULL)
3416 {
3417 retval = 0;
3418 break;
3419 }
3420 col = (int)(s - regline);
3421 }
3422
3423 retval = regtry(prog, col);
3424 if (retval > 0)
3425 break;
3426
3427 /* if not currently on the first line, get it again */
3428 if (reglnum != 0)
3429 {
3430 regline = reg_getline((linenr_T)0);
3431 reglnum = 0;
3432 }
3433 if (regline[col] == NUL)
3434 break;
3435#ifdef FEAT_MBYTE
3436 if (has_mbyte)
3437 col += (*mb_ptr2len_check)(regline + col);
3438 else
3439#endif
3440 ++col;
3441 }
3442 }
3443
Bram Moolenaar071d4272004-06-13 20:20:40 +00003444theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 vim_free(reg_tofree);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003446 ga_clear(&regstack);
3447 ga_clear(&backpos);
3448
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 return retval;
3450}
3451
3452#ifdef FEAT_SYN_HL
3453static reg_extmatch_T *make_extmatch __ARGS((void));
3454
3455/*
3456 * Create a new extmatch and mark it as referenced once.
3457 */
3458 static reg_extmatch_T *
3459make_extmatch()
3460{
3461 reg_extmatch_T *em;
3462
3463 em = (reg_extmatch_T *)alloc_clear((unsigned)sizeof(reg_extmatch_T));
3464 if (em != NULL)
3465 em->refcnt = 1;
3466 return em;
3467}
3468
3469/*
3470 * Add a reference to an extmatch.
3471 */
3472 reg_extmatch_T *
3473ref_extmatch(em)
3474 reg_extmatch_T *em;
3475{
3476 if (em != NULL)
3477 em->refcnt++;
3478 return em;
3479}
3480
3481/*
3482 * Remove a reference to an extmatch. If there are no references left, free
3483 * the info.
3484 */
3485 void
3486unref_extmatch(em)
3487 reg_extmatch_T *em;
3488{
3489 int i;
3490
3491 if (em != NULL && --em->refcnt <= 0)
3492 {
3493 for (i = 0; i < NSUBEXP; ++i)
3494 vim_free(em->matches[i]);
3495 vim_free(em);
3496 }
3497}
3498#endif
3499
3500/*
3501 * regtry - try match of "prog" with at regline["col"].
3502 * Returns 0 for failure, number of lines contained in the match otherwise.
3503 */
3504 static long
3505regtry(prog, col)
3506 regprog_T *prog;
3507 colnr_T col;
3508{
3509 reginput = regline + col;
3510 need_clear_subexpr = TRUE;
3511#ifdef FEAT_SYN_HL
3512 /* Clear the external match subpointers if necessary. */
3513 if (prog->reghasz == REX_SET)
3514 need_clear_zsubexpr = TRUE;
3515#endif
3516
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003517 if (regmatch(prog->program + 1) == 0)
3518 return 0;
3519
3520 cleanup_subexpr();
3521 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003522 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003523 if (reg_startpos[0].lnum < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003525 reg_startpos[0].lnum = 0;
3526 reg_startpos[0].col = col;
3527 }
3528 if (reg_endpos[0].lnum < 0)
3529 {
3530 reg_endpos[0].lnum = reglnum;
3531 reg_endpos[0].col = (int)(reginput - regline);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 }
3533 else
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003534 /* Use line number of "\ze". */
3535 reglnum = reg_endpos[0].lnum;
3536 }
3537 else
3538 {
3539 if (reg_startp[0] == NULL)
3540 reg_startp[0] = regline + col;
3541 if (reg_endp[0] == NULL)
3542 reg_endp[0] = reginput;
3543 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544#ifdef FEAT_SYN_HL
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003545 /* Package any found \z(...\) matches for export. Default is none. */
3546 unref_extmatch(re_extmatch_out);
3547 re_extmatch_out = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003549 if (prog->reghasz == REX_SET)
3550 {
3551 int i;
3552
3553 cleanup_zsubexpr();
3554 re_extmatch_out = make_extmatch();
3555 for (i = 0; i < NSUBEXP; i++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003557 if (REG_MULTI)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 {
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003559 /* Only accept single line matches. */
3560 if (reg_startzpos[i].lnum >= 0
3561 && reg_endzpos[i].lnum == reg_startzpos[i].lnum)
3562 re_extmatch_out->matches[i] =
3563 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 + reg_startzpos[i].col,
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003565 reg_endzpos[i].col - reg_startzpos[i].col);
3566 }
3567 else
3568 {
3569 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
3570 re_extmatch_out->matches[i] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571 vim_strnsave(reg_startzp[i],
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003572 (int)(reg_endzp[i] - reg_startzp[i]));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 }
3574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00003576#endif
3577 return 1 + reglnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578}
3579
3580#ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581static int reg_prev_class __ARGS((void));
3582
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583/*
3584 * Get class of previous character.
3585 */
3586 static int
3587reg_prev_class()
3588{
3589 if (reginput > regline)
3590 return mb_get_class(reginput - 1
3591 - (*mb_head_off)(regline, reginput - 1));
3592 return -1;
3593}
3594
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595#endif
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00003596#define ADVANCE_REGINPUT() mb_ptr_adv(reginput)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
3598/*
3599 * The arguments from BRACE_LIMITS are stored here. They are actually local
3600 * to regmatch(), but they are here to reduce the amount of stack space used
3601 * (it can be called recursively many times).
3602 */
3603static long bl_minval;
3604static long bl_maxval;
3605
3606/*
3607 * regmatch - main matching routine
3608 *
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003609 * Conceptually the strategy is simple: Check to see whether the current node
3610 * matches, push an item onto the regstack and loop to see whether the rest
3611 * matches, and then act accordingly. In practice we make some effort to
3612 * avoid using the regstack, in particular by going through "ordinary" nodes
3613 * (that don't need to know whether the rest of the match failed) by a nested
3614 * loop.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 *
3616 * Returns TRUE when there is a match. Leaves reginput and reglnum just after
3617 * the last matched character.
3618 * Returns FALSE when there is no match. Leaves reginput and reglnum in an
3619 * undefined state!
3620 */
3621 static int
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003622regmatch(scan)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *scan; /* Current node. */
3624{
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003625 char_u *next; /* Next node. */
3626 int op;
3627 int c;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003628 regitem_T *rp;
3629 int no;
3630 int status; /* one of the RA_ values: */
3631#define RA_FAIL 1 /* something failed, abort */
3632#define RA_CONT 2 /* continue in inner loop */
3633#define RA_BREAK 3 /* break inner loop */
3634#define RA_MATCH 4 /* successful match */
3635#define RA_NOMATCH 5 /* didn't match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636
Bram Moolenaara7fc0102005-05-18 22:17:12 +00003637 /* Init the regstack and backpos table empty. They are initialized and
3638 * freed in vim_regexec_both() to reduce malloc()/free() calls. */
3639 regstack.ga_len = 0;
3640 backpos.ga_len = 0;
Bram Moolenaar582fd852005-03-28 20:58:01 +00003641
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003642 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003643 * Repeat until "regstack" is empty.
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003644 */
3645 for (;;)
3646 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 /* Some patterns my cause a long time to match, even though they are not
3648 * illegal. E.g., "\([a-z]\+\)\+Q". Allow breaking them with CTRL-C. */
3649 fast_breakcheck();
3650
3651#ifdef DEBUG
3652 if (scan != NULL && regnarrate)
3653 {
3654 mch_errmsg(regprop(scan));
3655 mch_errmsg("(\n");
3656 }
3657#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003658
3659 /*
Bram Moolenaar582fd852005-03-28 20:58:01 +00003660 * Repeat for items that can be matched sequentially, without using the
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003661 * regstack.
3662 */
3663 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003665 if (got_int || scan == NULL)
3666 {
3667 status = RA_FAIL;
3668 break;
3669 }
3670 status = RA_CONT;
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672#ifdef DEBUG
3673 if (regnarrate)
3674 {
3675 mch_errmsg(regprop(scan));
3676 mch_errmsg("...\n");
3677# ifdef FEAT_SYN_HL
3678 if (re_extmatch_in != NULL)
3679 {
3680 int i;
3681
3682 mch_errmsg(_("External submatches:\n"));
3683 for (i = 0; i < NSUBEXP; i++)
3684 {
3685 mch_errmsg(" \"");
3686 if (re_extmatch_in->matches[i] != NULL)
3687 mch_errmsg(re_extmatch_in->matches[i]);
3688 mch_errmsg("\"\n");
3689 }
3690 }
3691# endif
3692 }
3693#endif
3694 next = regnext(scan);
3695
3696 op = OP(scan);
3697 /* Check for character class with NL added. */
3698 if (WITH_NL(op) && *reginput == NUL && reglnum < reg_maxline)
3699 {
3700 reg_nextline();
3701 }
3702 else if (reg_line_lbr && WITH_NL(op) && *reginput == '\n')
3703 {
3704 ADVANCE_REGINPUT();
3705 }
3706 else
3707 {
3708 if (WITH_NL(op))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003709 op -= ADD_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710#ifdef FEAT_MBYTE
3711 if (has_mbyte)
3712 c = (*mb_ptr2char)(reginput);
3713 else
3714#endif
3715 c = *reginput;
3716 switch (op)
3717 {
3718 case BOL:
3719 if (reginput != regline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003720 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003721 break;
3722
3723 case EOL:
3724 if (c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003725 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 break;
3727
3728 case RE_BOF:
3729 /* Passing -1 to the getline() function provided for the search
3730 * should always return NULL if the current line is the first
3731 * line of the file. */
3732 if (reglnum != 0 || reginput != regline
3733 || (REG_MULTI && reg_getline((linenr_T)-1) != NULL))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003734 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 break;
3736
3737 case RE_EOF:
3738 if (reglnum != reg_maxline || c != NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003739 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 break;
3741
3742 case CURSOR:
3743 /* Check if the buffer is in a window and compare the
3744 * reg_win->w_cursor position to the match position. */
3745 if (reg_win == NULL
3746 || (reglnum + reg_firstlnum != reg_win->w_cursor.lnum)
3747 || ((colnr_T)(reginput - regline) != reg_win->w_cursor.col))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003748 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 break;
3750
3751 case RE_LNUM:
3752 if (!REG_MULTI || !re_num_cmp((long_u)(reglnum + reg_firstlnum),
3753 scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003754 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 break;
3756
3757 case RE_COL:
3758 if (!re_num_cmp((long_u)(reginput - regline) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003759 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 break;
3761
3762 case RE_VCOL:
3763 if (!re_num_cmp((long_u)win_linetabsize(
3764 reg_win == NULL ? curwin : reg_win,
3765 regline, (colnr_T)(reginput - regline)) + 1, scan))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003766 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003767 break;
3768
3769 case BOW: /* \<word; reginput points to w */
3770 if (c == NUL) /* Can't match at end of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003771 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003773 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 {
3775 int this_class;
3776
3777 /* Get class of current and previous char (if it exists). */
3778 this_class = mb_get_class(reginput);
3779 if (this_class <= 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003780 status = RA_NOMATCH; /* not on a word at all */
3781 else if (reg_prev_class() == this_class)
3782 status = RA_NOMATCH; /* previous char is in same word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784#endif
3785 else
3786 {
3787 if (!vim_iswordc(c)
3788 || (reginput > regline && vim_iswordc(reginput[-1])))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003789 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 break;
3792
3793 case EOW: /* word\>; reginput points after d */
3794 if (reginput == regline) /* Can't match at start of line */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003795 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796#ifdef FEAT_MBYTE
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003797 else if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 {
3799 int this_class, prev_class;
3800
3801 /* Get class of current and previous char (if it exists). */
3802 this_class = mb_get_class(reginput);
3803 prev_class = reg_prev_class();
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003804 if (this_class == prev_class
3805 || prev_class == 0 || prev_class == 1)
3806 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003809 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003811 if (!vim_iswordc(reginput[-1])
3812 || (reginput[0] != NUL && vim_iswordc(c)))
3813 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 }
3815 break; /* Matched with EOW */
3816
3817 case ANY:
3818 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003819 status = RA_NOMATCH;
3820 else
3821 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 break;
3823
3824 case IDENT:
3825 if (!vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003826 status = RA_NOMATCH;
3827 else
3828 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 break;
3830
3831 case SIDENT:
3832 if (VIM_ISDIGIT(*reginput) || !vim_isIDc(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003833 status = RA_NOMATCH;
3834 else
3835 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 break;
3837
3838 case KWORD:
3839 if (!vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003840 status = RA_NOMATCH;
3841 else
3842 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 break;
3844
3845 case SKWORD:
3846 if (VIM_ISDIGIT(*reginput) || !vim_iswordp(reginput))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003847 status = RA_NOMATCH;
3848 else
3849 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 break;
3851
3852 case FNAME:
3853 if (!vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003854 status = RA_NOMATCH;
3855 else
3856 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 break;
3858
3859 case SFNAME:
3860 if (VIM_ISDIGIT(*reginput) || !vim_isfilec(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003861 status = RA_NOMATCH;
3862 else
3863 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 break;
3865
3866 case PRINT:
3867 if (ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003868 status = RA_NOMATCH;
3869 else
3870 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 break;
3872
3873 case SPRINT:
3874 if (VIM_ISDIGIT(*reginput) || ptr2cells(reginput) != 1)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003875 status = RA_NOMATCH;
3876 else
3877 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 break;
3879
3880 case WHITE:
3881 if (!vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003882 status = RA_NOMATCH;
3883 else
3884 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003885 break;
3886
3887 case NWHITE:
3888 if (c == NUL || vim_iswhite(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003889 status = RA_NOMATCH;
3890 else
3891 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 break;
3893
3894 case DIGIT:
3895 if (!ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003896 status = RA_NOMATCH;
3897 else
3898 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 break;
3900
3901 case NDIGIT:
3902 if (c == NUL || ri_digit(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003903 status = RA_NOMATCH;
3904 else
3905 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003906 break;
3907
3908 case HEX:
3909 if (!ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003910 status = RA_NOMATCH;
3911 else
3912 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 break;
3914
3915 case NHEX:
3916 if (c == NUL || ri_hex(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003917 status = RA_NOMATCH;
3918 else
3919 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003920 break;
3921
3922 case OCTAL:
3923 if (!ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003924 status = RA_NOMATCH;
3925 else
3926 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 break;
3928
3929 case NOCTAL:
3930 if (c == NUL || ri_octal(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003931 status = RA_NOMATCH;
3932 else
3933 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 break;
3935
3936 case WORD:
3937 if (!ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003938 status = RA_NOMATCH;
3939 else
3940 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 break;
3942
3943 case NWORD:
3944 if (c == NUL || ri_word(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003945 status = RA_NOMATCH;
3946 else
3947 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 break;
3949
3950 case HEAD:
3951 if (!ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003952 status = RA_NOMATCH;
3953 else
3954 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 break;
3956
3957 case NHEAD:
3958 if (c == NUL || ri_head(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003959 status = RA_NOMATCH;
3960 else
3961 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962 break;
3963
3964 case ALPHA:
3965 if (!ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003966 status = RA_NOMATCH;
3967 else
3968 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003969 break;
3970
3971 case NALPHA:
3972 if (c == NUL || ri_alpha(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003973 status = RA_NOMATCH;
3974 else
3975 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 break;
3977
3978 case LOWER:
3979 if (!ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003980 status = RA_NOMATCH;
3981 else
3982 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 break;
3984
3985 case NLOWER:
3986 if (c == NUL || ri_lower(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003987 status = RA_NOMATCH;
3988 else
3989 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003990 break;
3991
3992 case UPPER:
3993 if (!ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00003994 status = RA_NOMATCH;
3995 else
3996 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 break;
3998
3999 case NUPPER:
4000 if (c == NUL || ri_upper(c))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004001 status = RA_NOMATCH;
4002 else
4003 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004004 break;
4005
4006 case EXACTLY:
4007 {
4008 int len;
4009 char_u *opnd;
4010
4011 opnd = OPERAND(scan);
4012 /* Inline the first byte, for speed. */
4013 if (*opnd != *reginput
4014 && (!ireg_ic || (
4015#ifdef FEAT_MBYTE
4016 !enc_utf8 &&
4017#endif
4018 TOLOWER_LOC(*opnd) != TOLOWER_LOC(*reginput))))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004019 status = RA_NOMATCH;
4020 else if (*opnd == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021 {
4022 /* match empty string always works; happens when "~" is
4023 * empty. */
4024 }
4025 else if (opnd[1] == NUL
4026#ifdef FEAT_MBYTE
4027 && !(enc_utf8 && ireg_ic)
4028#endif
4029 )
4030 ++reginput; /* matched a single char */
4031 else
4032 {
4033 len = (int)STRLEN(opnd);
4034 /* Need to match first byte again for multi-byte. */
4035 if (cstrncmp(opnd, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004036 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037#ifdef FEAT_MBYTE
4038 /* Check for following composing character. */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004039 else if (enc_utf8
4040 && UTF_COMPOSINGLIKE(reginput, reginput + len))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
4042 /* raaron: This code makes a composing character get
4043 * ignored, which is the correct behavior (sometimes)
4044 * for voweled Hebrew texts. */
4045 if (!ireg_icombine)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004046 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004049 else
4050 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
4052 }
4053 break;
4054
4055 case ANYOF:
4056 case ANYBUT:
4057 if (c == NUL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004058 status = RA_NOMATCH;
4059 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
4060 status = RA_NOMATCH;
4061 else
4062 ADVANCE_REGINPUT();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 break;
4064
4065#ifdef FEAT_MBYTE
4066 case MULTIBYTECODE:
4067 if (has_mbyte)
4068 {
4069 int i, len;
4070 char_u *opnd;
4071
4072 opnd = OPERAND(scan);
4073 /* Safety check (just in case 'encoding' was changed since
4074 * compiling the program). */
4075 if ((len = (*mb_ptr2len_check)(opnd)) < 2)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004076 {
4077 status = RA_NOMATCH;
4078 break;
4079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 for (i = 0; i < len; ++i)
4081 if (opnd[i] != reginput[i])
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004082 {
4083 status = RA_NOMATCH;
4084 break;
4085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 reginput += len;
4087 }
4088 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004089 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004090 break;
4091#endif
4092
4093 case NOTHING:
4094 break;
4095
4096 case BACK:
Bram Moolenaar582fd852005-03-28 20:58:01 +00004097 {
4098 int i;
4099 backpos_T *bp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100
Bram Moolenaar582fd852005-03-28 20:58:01 +00004101 /*
4102 * When we run into BACK we need to check if we don't keep
4103 * looping without matching any input. The second and later
4104 * times a BACK is encountered it fails if the input is still
4105 * at the same position as the previous time.
4106 * The positions are stored in "backpos" and found by the
4107 * current value of "scan", the position in the RE program.
4108 */
4109 bp = (backpos_T *)backpos.ga_data;
4110 for (i = 0; i < backpos.ga_len; ++i)
4111 if (bp[i].bp_scan == scan)
4112 break;
4113 if (i == backpos.ga_len)
4114 {
4115 /* First time at this BACK, make room to store the pos. */
4116 if (ga_grow(&backpos, 1) == FAIL)
4117 status = RA_FAIL;
4118 else
4119 {
4120 /* get "ga_data" again, it may have changed */
4121 bp = (backpos_T *)backpos.ga_data;
4122 bp[i].bp_scan = scan;
4123 ++backpos.ga_len;
4124 }
4125 }
4126 else if (reg_save_equal(&bp[i].bp_pos))
4127 /* Still at same position as last time, fail. */
4128 status = RA_NOMATCH;
4129
4130 if (status != RA_FAIL && status != RA_NOMATCH)
4131 reg_save(&bp[i].bp_pos, &backpos);
4132 }
Bram Moolenaar19a09a12005-03-04 23:39:37 +00004133 break;
4134
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 case MOPEN + 0: /* Match start: \zs */
4136 case MOPEN + 1: /* \( */
4137 case MOPEN + 2:
4138 case MOPEN + 3:
4139 case MOPEN + 4:
4140 case MOPEN + 5:
4141 case MOPEN + 6:
4142 case MOPEN + 7:
4143 case MOPEN + 8:
4144 case MOPEN + 9:
4145 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146 no = op - MOPEN;
4147 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004148 rp = regstack_push(RS_MOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004149 if (rp == NULL)
4150 status = RA_FAIL;
4151 else
4152 {
4153 rp->rs_no = no;
4154 save_se(&rp->rs_un.sesave, &reg_startpos[no],
4155 &reg_startp[no]);
4156 /* We simply continue and handle the result when done. */
4157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004159 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160
4161 case NOPEN: /* \%( */
4162 case NCLOSE: /* \) after \%( */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004163 if (regstack_push(RS_NOPEN, scan) == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004164 status = RA_FAIL;
4165 /* We simply continue and handle the result when done. */
4166 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
4168#ifdef FEAT_SYN_HL
4169 case ZOPEN + 1:
4170 case ZOPEN + 2:
4171 case ZOPEN + 3:
4172 case ZOPEN + 4:
4173 case ZOPEN + 5:
4174 case ZOPEN + 6:
4175 case ZOPEN + 7:
4176 case ZOPEN + 8:
4177 case ZOPEN + 9:
4178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004179 no = op - ZOPEN;
4180 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004181 rp = regstack_push(RS_ZOPEN, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004182 if (rp == NULL)
4183 status = RA_FAIL;
4184 else
4185 {
4186 rp->rs_no = no;
4187 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
4188 &reg_startzp[no]);
4189 /* We simply continue and handle the result when done. */
4190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004192 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193#endif
4194
4195 case MCLOSE + 0: /* Match end: \ze */
4196 case MCLOSE + 1: /* \) */
4197 case MCLOSE + 2:
4198 case MCLOSE + 3:
4199 case MCLOSE + 4:
4200 case MCLOSE + 5:
4201 case MCLOSE + 6:
4202 case MCLOSE + 7:
4203 case MCLOSE + 8:
4204 case MCLOSE + 9:
4205 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 no = op - MCLOSE;
4207 cleanup_subexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004208 rp = regstack_push(RS_MCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004209 if (rp == NULL)
4210 status = RA_FAIL;
4211 else
4212 {
4213 rp->rs_no = no;
4214 save_se(&rp->rs_un.sesave, &reg_endpos[no], &reg_endp[no]);
4215 /* We simply continue and handle the result when done. */
4216 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004218 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219
4220#ifdef FEAT_SYN_HL
4221 case ZCLOSE + 1: /* \) after \z( */
4222 case ZCLOSE + 2:
4223 case ZCLOSE + 3:
4224 case ZCLOSE + 4:
4225 case ZCLOSE + 5:
4226 case ZCLOSE + 6:
4227 case ZCLOSE + 7:
4228 case ZCLOSE + 8:
4229 case ZCLOSE + 9:
4230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 no = op - ZCLOSE;
4232 cleanup_zsubexpr();
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004233 rp = regstack_push(RS_ZCLOSE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004234 if (rp == NULL)
4235 status = RA_FAIL;
4236 else
4237 {
4238 rp->rs_no = no;
4239 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
4240 &reg_endzp[no]);
4241 /* We simply continue and handle the result when done. */
4242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004244 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245#endif
4246
4247 case BACKREF + 1:
4248 case BACKREF + 2:
4249 case BACKREF + 3:
4250 case BACKREF + 4:
4251 case BACKREF + 5:
4252 case BACKREF + 6:
4253 case BACKREF + 7:
4254 case BACKREF + 8:
4255 case BACKREF + 9:
4256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 int len;
4258 linenr_T clnum;
4259 colnr_T ccol;
4260 char_u *p;
4261
4262 no = op - BACKREF;
4263 cleanup_subexpr();
4264 if (!REG_MULTI) /* Single-line regexp */
4265 {
4266 if (reg_endp[no] == NULL)
4267 {
4268 /* Backref was not set: Match an empty string. */
4269 len = 0;
4270 }
4271 else
4272 {
4273 /* Compare current input with back-ref in the same
4274 * line. */
4275 len = (int)(reg_endp[no] - reg_startp[no]);
4276 if (cstrncmp(reg_startp[no], reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004277 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 }
4280 else /* Multi-line regexp */
4281 {
4282 if (reg_endpos[no].lnum < 0)
4283 {
4284 /* Backref was not set: Match an empty string. */
4285 len = 0;
4286 }
4287 else
4288 {
4289 if (reg_startpos[no].lnum == reglnum
4290 && reg_endpos[no].lnum == reglnum)
4291 {
4292 /* Compare back-ref within the current line. */
4293 len = reg_endpos[no].col - reg_startpos[no].col;
4294 if (cstrncmp(regline + reg_startpos[no].col,
4295 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004296 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 else
4299 {
4300 /* Messy situation: Need to compare between two
4301 * lines. */
4302 ccol = reg_startpos[no].col;
4303 clnum = reg_startpos[no].lnum;
4304 for (;;)
4305 {
4306 /* Since getting one line may invalidate
4307 * the other, need to make copy. Slow! */
4308 if (regline != reg_tofree)
4309 {
4310 len = (int)STRLEN(regline);
4311 if (reg_tofree == NULL
4312 || len >= (int)reg_tofreelen)
4313 {
4314 len += 50; /* get some extra */
4315 vim_free(reg_tofree);
4316 reg_tofree = alloc(len);
4317 if (reg_tofree == NULL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004318 {
4319 status = RA_FAIL; /* outof memory!*/
4320 break;
4321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 reg_tofreelen = len;
4323 }
4324 STRCPY(reg_tofree, regline);
4325 reginput = reg_tofree
4326 + (reginput - regline);
4327 regline = reg_tofree;
4328 }
4329
4330 /* Get the line to compare with. */
4331 p = reg_getline(clnum);
4332 if (clnum == reg_endpos[no].lnum)
4333 len = reg_endpos[no].col - ccol;
4334 else
4335 len = (int)STRLEN(p + ccol);
4336
4337 if (cstrncmp(p + ccol, reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004338 {
4339 status = RA_NOMATCH; /* doesn't match */
4340 break;
4341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 if (clnum == reg_endpos[no].lnum)
4343 break; /* match and at end! */
4344 if (reglnum == reg_maxline)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004345 {
4346 status = RA_NOMATCH; /* text too short */
4347 break;
4348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
4350 /* Advance to next line. */
4351 reg_nextline();
4352 ++clnum;
4353 ccol = 0;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004354 if (got_int)
4355 {
4356 status = RA_FAIL;
4357 break;
4358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360
4361 /* found a match! Note that regline may now point
4362 * to a copy of the line, that should not matter. */
4363 }
4364 }
4365 }
4366
4367 /* Matched the backref, skip over it. */
4368 reginput += len;
4369 }
4370 break;
4371
4372#ifdef FEAT_SYN_HL
4373 case ZREF + 1:
4374 case ZREF + 2:
4375 case ZREF + 3:
4376 case ZREF + 4:
4377 case ZREF + 5:
4378 case ZREF + 6:
4379 case ZREF + 7:
4380 case ZREF + 8:
4381 case ZREF + 9:
4382 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 int len;
4384
4385 cleanup_zsubexpr();
4386 no = op - ZREF;
4387 if (re_extmatch_in != NULL
4388 && re_extmatch_in->matches[no] != NULL)
4389 {
4390 len = (int)STRLEN(re_extmatch_in->matches[no]);
4391 if (cstrncmp(re_extmatch_in->matches[no],
4392 reginput, &len) != 0)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004393 status = RA_NOMATCH;
4394 else
4395 reginput += len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 }
4397 else
4398 {
4399 /* Backref was not set: Match an empty string. */
4400 }
4401 }
4402 break;
4403#endif
4404
4405 case BRANCH:
4406 {
4407 if (OP(next) != BRANCH) /* No choice. */
4408 next = OPERAND(scan); /* Avoid recursion. */
4409 else
4410 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004411 rp = regstack_push(RS_BRANCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004412 if (rp == NULL)
4413 status = RA_FAIL;
4414 else
4415 status = RA_BREAK; /* rest is below */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 }
4417 }
4418 break;
4419
4420 case BRACE_LIMITS:
4421 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 if (OP(next) == BRACE_SIMPLE)
4423 {
4424 bl_minval = OPERAND_MIN(scan);
4425 bl_maxval = OPERAND_MAX(scan);
4426 }
4427 else if (OP(next) >= BRACE_COMPLEX
4428 && OP(next) < BRACE_COMPLEX + 10)
4429 {
4430 no = OP(next) - BRACE_COMPLEX;
4431 brace_min[no] = OPERAND_MIN(scan);
4432 brace_max[no] = OPERAND_MAX(scan);
4433 brace_count[no] = 0;
4434 }
4435 else
4436 {
4437 EMSG(_(e_internal)); /* Shouldn't happen */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004438 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 }
4440 }
4441 break;
4442
4443 case BRACE_COMPLEX + 0:
4444 case BRACE_COMPLEX + 1:
4445 case BRACE_COMPLEX + 2:
4446 case BRACE_COMPLEX + 3:
4447 case BRACE_COMPLEX + 4:
4448 case BRACE_COMPLEX + 5:
4449 case BRACE_COMPLEX + 6:
4450 case BRACE_COMPLEX + 7:
4451 case BRACE_COMPLEX + 8:
4452 case BRACE_COMPLEX + 9:
4453 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 no = op - BRACE_COMPLEX;
4455 ++brace_count[no];
4456
4457 /* If not matched enough times yet, try one more */
4458 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004459 ? brace_min[no] : brace_max[no]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004461 rp = regstack_push(RS_BRCPLX_MORE, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004462 if (rp == NULL)
4463 status = RA_FAIL;
4464 else
4465 {
4466 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004467 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004468 next = OPERAND(scan);
4469 /* We continue and handle the result when done. */
4470 }
4471 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 }
4473
4474 /* If matched enough times, may try matching some more */
4475 if (brace_min[no] <= brace_max[no])
4476 {
4477 /* Range is the normal way around, use longest match */
4478 if (brace_count[no] <= brace_max[no])
4479 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004480 rp = regstack_push(RS_BRCPLX_LONG, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004481 if (rp == NULL)
4482 status = RA_FAIL;
4483 else
4484 {
4485 rp->rs_no = no;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004486 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004487 next = OPERAND(scan);
4488 /* We continue and handle the result when done. */
4489 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 }
4492 else
4493 {
4494 /* Range is backwards, use shortest match first */
4495 if (brace_count[no] <= brace_min[no])
4496 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004497 rp = regstack_push(RS_BRCPLX_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004498 if (rp == NULL)
4499 status = RA_FAIL;
4500 else
4501 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004502 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004503 /* We continue and handle the result when done. */
4504 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 }
4506 }
4507 }
4508 break;
4509
4510 case BRACE_SIMPLE:
4511 case STAR:
4512 case PLUS:
4513 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004514 regstar_T rst;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515
4516 /*
4517 * Lookahead to avoid useless match attempts when we know
4518 * what character comes next.
4519 */
4520 if (OP(next) == EXACTLY)
4521 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004522 rst.nextb = *OPERAND(next);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (ireg_ic)
4524 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004525 if (isupper(rst.nextb))
4526 rst.nextb_ic = TOLOWER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004528 rst.nextb_ic = TOUPPER_LOC(rst.nextb);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 }
4530 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004531 rst.nextb_ic = rst.nextb;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 }
4533 else
4534 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004535 rst.nextb = NUL;
4536 rst.nextb_ic = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 }
4538 if (op != BRACE_SIMPLE)
4539 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004540 rst.minval = (op == STAR) ? 0 : 1;
4541 rst.maxval = MAX_LIMIT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 else
4544 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004545 rst.minval = bl_minval;
4546 rst.maxval = bl_maxval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548
4549 /*
4550 * When maxval > minval, try matching as much as possible, up
4551 * to maxval. When maxval < minval, try matching at least the
4552 * minimal number (since the range is backwards, that's also
4553 * maxval!).
4554 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004555 rst.count = regrepeat(OPERAND(scan), rst.maxval);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 if (got_int)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004558 status = RA_FAIL;
4559 break;
4560 }
4561 if (rst.minval <= rst.maxval
4562 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4563 {
4564 /* It could match. Prepare for trying to match what
4565 * follows. The code is below. Parameters are stored in
4566 * a regstar_T on the regstack. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004567 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004568 {
4569 EMSG(_(e_maxmempat));
4570 status = RA_FAIL;
4571 }
4572 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004573 status = RA_FAIL;
4574 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004576 regstack.ga_len += sizeof(regstar_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004577 rp = regstack_push(rst.minval <= rst.maxval
Bram Moolenaar582fd852005-03-28 20:58:01 +00004578 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004579 if (rp == NULL)
4580 status = RA_FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004582 {
4583 *(((regstar_T *)rp) - 1) = rst;
4584 status = RA_BREAK; /* skip the restore bits */
4585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 }
4587 }
4588 else
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004589 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
4592 break;
4593
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004594 case NOMATCH:
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 case MATCH:
4596 case SUBPAT:
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004597 rp = regstack_push(RS_NOMATCH, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004598 if (rp == NULL)
4599 status = RA_FAIL;
4600 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004602 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004603 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004604 next = OPERAND(scan);
4605 /* We continue and handle the result when done. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 }
4607 break;
4608
4609 case BEHIND:
4610 case NOBEHIND:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004611 /* Need a bit of room to store extra positions. */
Bram Moolenaar916b7af2005-03-16 09:52:38 +00004612 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00004613 {
4614 EMSG(_(e_maxmempat));
4615 status = RA_FAIL;
4616 }
4617 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004618 status = RA_FAIL;
4619 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004621 regstack.ga_len += sizeof(regbehind_T);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004622 rp = regstack_push(RS_BEHIND1, scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004623 if (rp == NULL)
4624 status = RA_FAIL;
4625 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004627 rp->rs_no = op;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004628 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004629 /* First try if what follows matches. If it does then we
4630 * check the behind match by looping. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 }
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004633 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
4635 case BHPOS:
4636 if (REG_MULTI)
4637 {
4638 if (behind_pos.rs_u.pos.col != (colnr_T)(reginput - regline)
4639 || behind_pos.rs_u.pos.lnum != reglnum)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004640 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
4642 else if (behind_pos.rs_u.ptr != reginput)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004643 status = RA_NOMATCH;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 break;
4645
4646 case NEWL:
4647 if ((c != NUL || reglnum == reg_maxline)
4648 && (c != '\n' || !reg_line_lbr))
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004649 status = RA_NOMATCH;
4650 else if (reg_line_lbr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 ADVANCE_REGINPUT();
4652 else
4653 reg_nextline();
4654 break;
4655
4656 case END:
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004657 status = RA_MATCH; /* Success! */
4658 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659
4660 default:
4661 EMSG(_(e_re_corr));
4662#ifdef DEBUG
4663 printf("Illegal op code %d\n", op);
4664#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004665 status = RA_FAIL;
4666 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 }
4668 }
4669
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004670 /* If we can't continue sequentially, break the inner loop. */
4671 if (status != RA_CONT)
4672 break;
4673
4674 /* Continue in inner loop, advance to next item. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 scan = next;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004676
4677 } /* end of inner loop */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678
4679 /*
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004680 * If there is something on the regstack execute the code for the state.
Bram Moolenaar582fd852005-03-28 20:58:01 +00004681 * If the state is popped then loop and use the older state.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004683 while (regstack.ga_len > 0 && status != RA_FAIL)
4684 {
4685 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4686 switch (rp->rs_state)
4687 {
4688 case RS_NOPEN:
4689 /* Result is passed on as-is, simply pop the state. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004690 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004691 break;
4692
4693 case RS_MOPEN:
4694 /* Pop the state. Restore pointers when there is no match. */
4695 if (status == RA_NOMATCH)
4696 restore_se(&rp->rs_un.sesave, &reg_startpos[rp->rs_no],
4697 &reg_startp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004698 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004699 break;
4700
4701#ifdef FEAT_SYN_HL
4702 case RS_ZOPEN:
4703 /* Pop the state. Restore pointers when there is no match. */
4704 if (status == RA_NOMATCH)
4705 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4706 &reg_startzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004707 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004708 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004710
4711 case RS_MCLOSE:
4712 /* Pop the state. Restore pointers when there is no match. */
4713 if (status == RA_NOMATCH)
4714 restore_se(&rp->rs_un.sesave, &reg_endpos[rp->rs_no],
4715 &reg_endp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004716 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004717 break;
4718
4719#ifdef FEAT_SYN_HL
4720 case RS_ZCLOSE:
4721 /* Pop the state. Restore pointers when there is no match. */
4722 if (status == RA_NOMATCH)
4723 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4724 &reg_endzp[rp->rs_no]);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004725 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004726 break;
4727#endif
4728
4729 case RS_BRANCH:
4730 if (status == RA_MATCH)
4731 /* this branch matched, use it */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004732 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004733 else
4734 {
4735 if (status != RA_BREAK)
4736 {
4737 /* After a non-matching branch: try next one. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004738 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004739 scan = rp->rs_scan;
4740 }
4741 if (scan == NULL || OP(scan) != BRANCH)
4742 {
4743 /* no more branches, didn't find a match */
4744 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004745 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004746 }
4747 else
4748 {
4749 /* Prepare to try a branch. */
4750 rp->rs_scan = regnext(scan);
Bram Moolenaar582fd852005-03-28 20:58:01 +00004751 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004752 scan = OPERAND(scan);
4753 }
4754 }
4755 break;
4756
4757 case RS_BRCPLX_MORE:
4758 /* Pop the state. Restore pointers when there is no match. */
4759 if (status == RA_NOMATCH)
4760 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004761 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004762 --brace_count[rp->rs_no]; /* decrement match count */
4763 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004764 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004765 break;
4766
4767 case RS_BRCPLX_LONG:
4768 /* Pop the state. Restore pointers when there is no match. */
4769 if (status == RA_NOMATCH)
4770 {
4771 /* There was no match, but we did find enough matches. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004772 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004773 --brace_count[rp->rs_no];
4774 /* continue with the items after "\{}" */
4775 status = RA_CONT;
4776 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004777 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004778 if (status == RA_CONT)
4779 scan = regnext(scan);
4780 break;
4781
4782 case RS_BRCPLX_SHORT:
4783 /* Pop the state. Restore pointers when there is no match. */
4784 if (status == RA_NOMATCH)
4785 /* There was no match, try to match one more item. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004786 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004787 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004788 if (status == RA_NOMATCH)
4789 {
4790 scan = OPERAND(scan);
4791 status = RA_CONT;
4792 }
4793 break;
4794
4795 case RS_NOMATCH:
4796 /* Pop the state. If the operand matches for NOMATCH or
4797 * doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4798 * except for SUBPAT, and continue with the next item. */
4799 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4800 status = RA_NOMATCH;
4801 else
4802 {
4803 status = RA_CONT;
Bram Moolenaar582fd852005-03-28 20:58:01 +00004804 if (rp->rs_no != SUBPAT) /* zero-width */
4805 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004806 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004807 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004808 if (status == RA_CONT)
4809 scan = regnext(scan);
4810 break;
4811
4812 case RS_BEHIND1:
4813 if (status == RA_NOMATCH)
4814 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004815 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004816 regstack.ga_len -= sizeof(regbehind_T);
4817 }
4818 else
4819 {
4820 /* The stuff after BEHIND/NOBEHIND matches. Now try if
4821 * the behind part does (not) match before the current
4822 * position in the input. This must be done at every
4823 * position in the input and checking if the match ends at
4824 * the current position. */
4825
4826 /* save the position after the found match for next */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004827 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004828
4829 /* start looking for a match with operand at the current
4830 * postion. Go back one character until we find the
4831 * result, hitting the start of the line or the previous
4832 * line (for multi-line matching).
4833 * Set behind_pos to where the match should end, BHPOS
4834 * will match it. Save the current value. */
4835 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4836 behind_pos = rp->rs_un.regsave;
4837
4838 rp->rs_state = RS_BEHIND2;
4839
Bram Moolenaar582fd852005-03-28 20:58:01 +00004840 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004841 scan = OPERAND(rp->rs_scan);
4842 }
4843 break;
4844
4845 case RS_BEHIND2:
4846 /*
4847 * Looping for BEHIND / NOBEHIND match.
4848 */
4849 if (status == RA_MATCH && reg_save_equal(&behind_pos))
4850 {
4851 /* found a match that ends where "next" started */
4852 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4853 if (rp->rs_no == BEHIND)
Bram Moolenaar582fd852005-03-28 20:58:01 +00004854 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4855 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004856 else
4857 /* But we didn't want a match. */
4858 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004859 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004860 regstack.ga_len -= sizeof(regbehind_T);
4861 }
4862 else
4863 {
4864 /* No match: Go back one character. May go to previous
4865 * line once. */
4866 no = OK;
4867 if (REG_MULTI)
4868 {
4869 if (rp->rs_un.regsave.rs_u.pos.col == 0)
4870 {
4871 if (rp->rs_un.regsave.rs_u.pos.lnum
4872 < behind_pos.rs_u.pos.lnum
4873 || reg_getline(
4874 --rp->rs_un.regsave.rs_u.pos.lnum)
4875 == NULL)
4876 no = FAIL;
4877 else
4878 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004879 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004880 rp->rs_un.regsave.rs_u.pos.col =
4881 (colnr_T)STRLEN(regline);
4882 }
4883 }
4884 else
4885 --rp->rs_un.regsave.rs_u.pos.col;
4886 }
4887 else
4888 {
4889 if (rp->rs_un.regsave.rs_u.ptr == regline)
4890 no = FAIL;
4891 else
4892 --rp->rs_un.regsave.rs_u.ptr;
4893 }
4894 if (no == OK)
4895 {
4896 /* Advanced, prepare for finding match again. */
Bram Moolenaar582fd852005-03-28 20:58:01 +00004897 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004898 scan = OPERAND(rp->rs_scan);
4899 }
4900 else
4901 {
4902 /* Can't advance. For NOBEHIND that's a match. */
4903 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4904 if (rp->rs_no == NOBEHIND)
4905 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004906 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4907 &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004908 status = RA_MATCH;
4909 }
4910 else
4911 status = RA_NOMATCH;
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004912 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004913 regstack.ga_len -= sizeof(regbehind_T);
4914 }
4915 }
4916 break;
4917
4918 case RS_STAR_LONG:
4919 case RS_STAR_SHORT:
4920 {
4921 regstar_T *rst = ((regstar_T *)rp) - 1;
4922
4923 if (status == RA_MATCH)
4924 {
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004925 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004926 regstack.ga_len -= sizeof(regstar_T);
4927 break;
4928 }
4929
4930 /* Tried once already, restore input pointers. */
4931 if (status != RA_BREAK)
Bram Moolenaar582fd852005-03-28 20:58:01 +00004932 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004933
4934 /* Repeat until we found a position where it could match. */
4935 for (;;)
4936 {
4937 if (status != RA_BREAK)
4938 {
4939 /* Tried first position already, advance. */
4940 if (rp->rs_state == RS_STAR_LONG)
4941 {
4942 /* Trying for longest matc, but couldn't or didn't
4943 * match -- back up one char. */
4944 if (--rst->count < rst->minval)
4945 break;
4946 if (reginput == regline)
4947 {
4948 /* backup to last char of previous line */
4949 --reglnum;
4950 regline = reg_getline(reglnum);
4951 /* Just in case regrepeat() didn't count
4952 * right. */
4953 if (regline == NULL)
4954 break;
4955 reginput = regline + STRLEN(regline);
4956 fast_breakcheck();
4957 }
4958 else
4959 mb_ptr_back(regline, reginput);
4960 }
4961 else
4962 {
4963 /* Range is backwards, use shortest match first.
4964 * Careful: maxval and minval are exchanged!
4965 * Couldn't or didn't match: try advancing one
4966 * char. */
4967 if (rst->count == rst->minval
4968 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
4969 break;
4970 ++rst->count;
4971 }
4972 if (got_int)
4973 break;
4974 }
4975 else
4976 status = RA_NOMATCH;
4977
4978 /* If it could match, try it. */
4979 if (rst->nextb == NUL || *reginput == rst->nextb
4980 || *reginput == rst->nextb_ic)
4981 {
Bram Moolenaar582fd852005-03-28 20:58:01 +00004982 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004983 scan = regnext(rp->rs_scan);
4984 status = RA_CONT;
4985 break;
4986 }
4987 }
4988 if (status != RA_CONT)
4989 {
4990 /* Failed. */
Bram Moolenaara7fc0102005-05-18 22:17:12 +00004991 regstack_pop(&scan);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00004992 regstack.ga_len -= sizeof(regstar_T);
4993 status = RA_NOMATCH;
4994 }
4995 }
4996 break;
4997 }
4998
4999 /* If we want to continue the inner loop or didn't pop a state contine
5000 * matching loop */
5001 if (status == RA_CONT || rp == (regitem_T *)
5002 ((char *)regstack.ga_data + regstack.ga_len) - 1)
5003 break;
5004 }
5005
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005006 /* May need to continue with the inner loop, starting at "scan". */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005007 if (status == RA_CONT)
5008 continue;
5009
5010 /*
5011 * If the regstack is empty or something failed we are done.
5012 */
5013 if (regstack.ga_len == 0 || status == RA_FAIL)
5014 {
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005015 if (scan == NULL)
5016 {
5017 /*
5018 * We get here only if there's trouble -- normally "case END" is
5019 * the terminating point.
5020 */
5021 EMSG(_(e_re_corr));
5022#ifdef DEBUG
5023 printf("Premature EOL\n");
5024#endif
5025 }
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005026 if (status == RA_FAIL)
5027 got_int = TRUE;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005028 return (status == RA_MATCH);
5029 }
5030
5031 } /* End of loop until the regstack is empty. */
5032
5033 /* NOTREACHED */
5034}
5035
5036/*
5037 * Push an item onto the regstack.
5038 * Returns pointer to new item. Returns NULL when out of memory.
5039 */
5040 static regitem_T *
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005041regstack_push(state, scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005042 regstate_T state;
5043 char_u *scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005044{
5045 regitem_T *rp;
5046
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005047 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
Bram Moolenaare4efc3b2005-03-07 23:16:51 +00005048 {
5049 EMSG(_(e_maxmempat));
5050 return NULL;
5051 }
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005052 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005053 return NULL;
5054
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005055 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005056 rp->rs_state = state;
5057 rp->rs_scan = scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005058
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005059 regstack.ga_len += sizeof(regitem_T);
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005060 return rp;
5061}
5062
5063/*
5064 * Pop an item from the regstack.
5065 */
5066 static void
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005067regstack_pop(scan)
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005068 char_u **scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005069{
5070 regitem_T *rp;
5071
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005072 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005073 *scan = rp->rs_scan;
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00005074
Bram Moolenaara7fc0102005-05-18 22:17:12 +00005075 regstack.ga_len -= sizeof(regitem_T);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076}
5077
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078/*
5079 * regrepeat - repeatedly match something simple, return how many.
5080 * Advances reginput (and reglnum) to just after the matched chars.
5081 */
5082 static int
5083regrepeat(p, maxcount)
5084 char_u *p;
5085 long maxcount; /* maximum number of matches allowed */
5086{
5087 long count = 0;
5088 char_u *scan;
5089 char_u *opnd;
5090 int mask;
5091 int testval = 0;
5092
5093 scan = reginput; /* Make local copy of reginput for speed. */
5094 opnd = OPERAND(p);
5095 switch (OP(p))
5096 {
5097 case ANY:
5098 case ANY + ADD_NL:
5099 while (count < maxcount)
5100 {
5101 /* Matching anything means we continue until end-of-line (or
5102 * end-of-file for ANY + ADD_NL), only limited by maxcount. */
5103 while (*scan != NUL && count < maxcount)
5104 {
5105 ++count;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005106 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 }
5108 if (!WITH_NL(OP(p)) || reglnum == reg_maxline || count == maxcount)
5109 break;
5110 ++count; /* count the line-break */
5111 reg_nextline();
5112 scan = reginput;
5113 if (got_int)
5114 break;
5115 }
5116 break;
5117
5118 case IDENT:
5119 case IDENT + ADD_NL:
5120 testval = TRUE;
5121 /*FALLTHROUGH*/
5122 case SIDENT:
5123 case SIDENT + ADD_NL:
5124 while (count < maxcount)
5125 {
5126 if (vim_isIDc(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5127 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005128 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 }
5130 else if (*scan == NUL)
5131 {
5132 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5133 break;
5134 reg_nextline();
5135 scan = reginput;
5136 if (got_int)
5137 break;
5138 }
5139 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5140 ++scan;
5141 else
5142 break;
5143 ++count;
5144 }
5145 break;
5146
5147 case KWORD:
5148 case KWORD + ADD_NL:
5149 testval = TRUE;
5150 /*FALLTHROUGH*/
5151 case SKWORD:
5152 case SKWORD + ADD_NL:
5153 while (count < maxcount)
5154 {
5155 if (vim_iswordp(scan) && (testval || !VIM_ISDIGIT(*scan)))
5156 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005157 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 else if (*scan == NUL)
5160 {
5161 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5162 break;
5163 reg_nextline();
5164 scan = reginput;
5165 if (got_int)
5166 break;
5167 }
5168 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5169 ++scan;
5170 else
5171 break;
5172 ++count;
5173 }
5174 break;
5175
5176 case FNAME:
5177 case FNAME + ADD_NL:
5178 testval = TRUE;
5179 /*FALLTHROUGH*/
5180 case SFNAME:
5181 case SFNAME + ADD_NL:
5182 while (count < maxcount)
5183 {
5184 if (vim_isfilec(*scan) && (testval || !VIM_ISDIGIT(*scan)))
5185 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005186 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005187 }
5188 else if (*scan == NUL)
5189 {
5190 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5191 break;
5192 reg_nextline();
5193 scan = reginput;
5194 if (got_int)
5195 break;
5196 }
5197 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5198 ++scan;
5199 else
5200 break;
5201 ++count;
5202 }
5203 break;
5204
5205 case PRINT:
5206 case PRINT + ADD_NL:
5207 testval = TRUE;
5208 /*FALLTHROUGH*/
5209 case SPRINT:
5210 case SPRINT + ADD_NL:
5211 while (count < maxcount)
5212 {
5213 if (*scan == NUL)
5214 {
5215 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5216 break;
5217 reg_nextline();
5218 scan = reginput;
5219 if (got_int)
5220 break;
5221 }
5222 else if (ptr2cells(scan) == 1 && (testval || !VIM_ISDIGIT(*scan)))
5223 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00005224 mb_ptr_adv(scan);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 }
5226 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5227 ++scan;
5228 else
5229 break;
5230 ++count;
5231 }
5232 break;
5233
5234 case WHITE:
5235 case WHITE + ADD_NL:
5236 testval = mask = RI_WHITE;
5237do_class:
5238 while (count < maxcount)
5239 {
5240#ifdef FEAT_MBYTE
5241 int l;
5242#endif
5243 if (*scan == NUL)
5244 {
5245 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5246 break;
5247 reg_nextline();
5248 scan = reginput;
5249 if (got_int)
5250 break;
5251 }
5252#ifdef FEAT_MBYTE
5253 else if (has_mbyte && (l = (*mb_ptr2len_check)(scan)) > 1)
5254 {
5255 if (testval != 0)
5256 break;
5257 scan += l;
5258 }
5259#endif
5260 else if ((class_tab[*scan] & mask) == testval)
5261 ++scan;
5262 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5263 ++scan;
5264 else
5265 break;
5266 ++count;
5267 }
5268 break;
5269
5270 case NWHITE:
5271 case NWHITE + ADD_NL:
5272 mask = RI_WHITE;
5273 goto do_class;
5274 case DIGIT:
5275 case DIGIT + ADD_NL:
5276 testval = mask = RI_DIGIT;
5277 goto do_class;
5278 case NDIGIT:
5279 case NDIGIT + ADD_NL:
5280 mask = RI_DIGIT;
5281 goto do_class;
5282 case HEX:
5283 case HEX + ADD_NL:
5284 testval = mask = RI_HEX;
5285 goto do_class;
5286 case NHEX:
5287 case NHEX + ADD_NL:
5288 mask = RI_HEX;
5289 goto do_class;
5290 case OCTAL:
5291 case OCTAL + ADD_NL:
5292 testval = mask = RI_OCTAL;
5293 goto do_class;
5294 case NOCTAL:
5295 case NOCTAL + ADD_NL:
5296 mask = RI_OCTAL;
5297 goto do_class;
5298 case WORD:
5299 case WORD + ADD_NL:
5300 testval = mask = RI_WORD;
5301 goto do_class;
5302 case NWORD:
5303 case NWORD + ADD_NL:
5304 mask = RI_WORD;
5305 goto do_class;
5306 case HEAD:
5307 case HEAD + ADD_NL:
5308 testval = mask = RI_HEAD;
5309 goto do_class;
5310 case NHEAD:
5311 case NHEAD + ADD_NL:
5312 mask = RI_HEAD;
5313 goto do_class;
5314 case ALPHA:
5315 case ALPHA + ADD_NL:
5316 testval = mask = RI_ALPHA;
5317 goto do_class;
5318 case NALPHA:
5319 case NALPHA + ADD_NL:
5320 mask = RI_ALPHA;
5321 goto do_class;
5322 case LOWER:
5323 case LOWER + ADD_NL:
5324 testval = mask = RI_LOWER;
5325 goto do_class;
5326 case NLOWER:
5327 case NLOWER + ADD_NL:
5328 mask = RI_LOWER;
5329 goto do_class;
5330 case UPPER:
5331 case UPPER + ADD_NL:
5332 testval = mask = RI_UPPER;
5333 goto do_class;
5334 case NUPPER:
5335 case NUPPER + ADD_NL:
5336 mask = RI_UPPER;
5337 goto do_class;
5338
5339 case EXACTLY:
5340 {
5341 int cu, cl;
5342
5343 /* This doesn't do a multi-byte character, because a MULTIBYTECODE
5344 * would have been used for it. */
5345 if (ireg_ic)
5346 {
5347 cu = TOUPPER_LOC(*opnd);
5348 cl = TOLOWER_LOC(*opnd);
5349 while (count < maxcount && (*scan == cu || *scan == cl))
5350 {
5351 count++;
5352 scan++;
5353 }
5354 }
5355 else
5356 {
5357 cu = *opnd;
5358 while (count < maxcount && *scan == cu)
5359 {
5360 count++;
5361 scan++;
5362 }
5363 }
5364 break;
5365 }
5366
5367#ifdef FEAT_MBYTE
5368 case MULTIBYTECODE:
5369 {
5370 int i, len, cf = 0;
5371
5372 /* Safety check (just in case 'encoding' was changed since
5373 * compiling the program). */
5374 if ((len = (*mb_ptr2len_check)(opnd)) > 1)
5375 {
5376 if (ireg_ic && enc_utf8)
5377 cf = utf_fold(utf_ptr2char(opnd));
5378 while (count < maxcount)
5379 {
5380 for (i = 0; i < len; ++i)
5381 if (opnd[i] != scan[i])
5382 break;
5383 if (i < len && (!ireg_ic || !enc_utf8
5384 || utf_fold(utf_ptr2char(scan)) != cf))
5385 break;
5386 scan += len;
5387 ++count;
5388 }
5389 }
5390 }
5391 break;
5392#endif
5393
5394 case ANYOF:
5395 case ANYOF + ADD_NL:
5396 testval = TRUE;
5397 /*FALLTHROUGH*/
5398
5399 case ANYBUT:
5400 case ANYBUT + ADD_NL:
5401 while (count < maxcount)
5402 {
5403#ifdef FEAT_MBYTE
5404 int len;
5405#endif
5406 if (*scan == NUL)
5407 {
5408 if (!WITH_NL(OP(p)) || reglnum == reg_maxline)
5409 break;
5410 reg_nextline();
5411 scan = reginput;
5412 if (got_int)
5413 break;
5414 }
5415 else if (reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
5416 ++scan;
5417#ifdef FEAT_MBYTE
5418 else if (has_mbyte && (len = (*mb_ptr2len_check)(scan)) > 1)
5419 {
5420 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
5421 break;
5422 scan += len;
5423 }
5424#endif
5425 else
5426 {
5427 if ((cstrchr(opnd, *scan) == NULL) == testval)
5428 break;
5429 ++scan;
5430 }
5431 ++count;
5432 }
5433 break;
5434
5435 case NEWL:
5436 while (count < maxcount
5437 && ((*scan == NUL && reglnum < reg_maxline)
5438 || (*scan == '\n' && reg_line_lbr)))
5439 {
5440 count++;
5441 if (reg_line_lbr)
5442 ADVANCE_REGINPUT();
5443 else
5444 reg_nextline();
5445 scan = reginput;
5446 if (got_int)
5447 break;
5448 }
5449 break;
5450
5451 default: /* Oh dear. Called inappropriately. */
5452 EMSG(_(e_re_corr));
5453#ifdef DEBUG
5454 printf("Called regrepeat with op code %d\n", OP(p));
5455#endif
5456 break;
5457 }
5458
5459 reginput = scan;
5460
5461 return (int)count;
5462}
5463
5464/*
5465 * regnext - dig the "next" pointer out of a node
5466 */
5467 static char_u *
5468regnext(p)
5469 char_u *p;
5470{
5471 int offset;
5472
5473 if (p == JUST_CALC_SIZE)
5474 return NULL;
5475
5476 offset = NEXT(p);
5477 if (offset == 0)
5478 return NULL;
5479
Bram Moolenaar582fd852005-03-28 20:58:01 +00005480 if (OP(p) == BACK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 return p - offset;
5482 else
5483 return p + offset;
5484}
5485
5486/*
5487 * Check the regexp program for its magic number.
5488 * Return TRUE if it's wrong.
5489 */
5490 static int
5491prog_magic_wrong()
5492{
5493 if (UCHARAT(REG_MULTI
5494 ? reg_mmatch->regprog->program
5495 : reg_match->regprog->program) != REGMAGIC)
5496 {
5497 EMSG(_(e_re_corr));
5498 return TRUE;
5499 }
5500 return FALSE;
5501}
5502
5503/*
5504 * Cleanup the subexpressions, if this wasn't done yet.
5505 * This construction is used to clear the subexpressions only when they are
5506 * used (to increase speed).
5507 */
5508 static void
5509cleanup_subexpr()
5510{
5511 if (need_clear_subexpr)
5512 {
5513 if (REG_MULTI)
5514 {
5515 /* Use 0xff to set lnum to -1 */
5516 vim_memset(reg_startpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5517 vim_memset(reg_endpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5518 }
5519 else
5520 {
5521 vim_memset(reg_startp, 0, sizeof(char_u *) * NSUBEXP);
5522 vim_memset(reg_endp, 0, sizeof(char_u *) * NSUBEXP);
5523 }
5524 need_clear_subexpr = FALSE;
5525 }
5526}
5527
5528#ifdef FEAT_SYN_HL
5529 static void
5530cleanup_zsubexpr()
5531{
5532 if (need_clear_zsubexpr)
5533 {
5534 if (REG_MULTI)
5535 {
5536 /* Use 0xff to set lnum to -1 */
5537 vim_memset(reg_startzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5538 vim_memset(reg_endzpos, 0xff, sizeof(lpos_T) * NSUBEXP);
5539 }
5540 else
5541 {
5542 vim_memset(reg_startzp, 0, sizeof(char_u *) * NSUBEXP);
5543 vim_memset(reg_endzp, 0, sizeof(char_u *) * NSUBEXP);
5544 }
5545 need_clear_zsubexpr = FALSE;
5546 }
5547}
5548#endif
5549
5550/*
5551 * Advance reglnum, regline and reginput to the next line.
5552 */
5553 static void
5554reg_nextline()
5555{
5556 regline = reg_getline(++reglnum);
5557 reginput = regline;
5558 fast_breakcheck();
5559}
5560
5561/*
5562 * Save the input line and position in a regsave_T.
5563 */
5564 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005565reg_save(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005567 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568{
5569 if (REG_MULTI)
5570 {
5571 save->rs_u.pos.col = (colnr_T)(reginput - regline);
5572 save->rs_u.pos.lnum = reglnum;
5573 }
5574 else
5575 save->rs_u.ptr = reginput;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005576 save->rs_len = gap->ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005577}
5578
5579/*
5580 * Restore the input line and position from a regsave_T.
5581 */
5582 static void
Bram Moolenaar582fd852005-03-28 20:58:01 +00005583reg_restore(save, gap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 regsave_T *save;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005585 garray_T *gap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586{
5587 if (REG_MULTI)
5588 {
5589 if (reglnum != save->rs_u.pos.lnum)
5590 {
5591 /* only call reg_getline() when the line number changed to save
5592 * a bit of time */
5593 reglnum = save->rs_u.pos.lnum;
5594 regline = reg_getline(reglnum);
5595 }
5596 reginput = regline + save->rs_u.pos.col;
5597 }
5598 else
5599 reginput = save->rs_u.ptr;
Bram Moolenaar582fd852005-03-28 20:58:01 +00005600 gap->ga_len = save->rs_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601}
5602
5603/*
5604 * Return TRUE if current position is equal to saved position.
5605 */
5606 static int
5607reg_save_equal(save)
5608 regsave_T *save;
5609{
5610 if (REG_MULTI)
5611 return reglnum == save->rs_u.pos.lnum
5612 && reginput == regline + save->rs_u.pos.col;
5613 return reginput == save->rs_u.ptr;
5614}
5615
5616/*
5617 * Tentatively set the sub-expression start to the current position (after
5618 * calling regmatch() they will have changed). Need to save the existing
5619 * values for when there is no match.
5620 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
5621 * depending on REG_MULTI.
5622 */
5623 static void
5624save_se_multi(savep, posp)
5625 save_se_T *savep;
5626 lpos_T *posp;
5627{
5628 savep->se_u.pos = *posp;
5629 posp->lnum = reglnum;
5630 posp->col = (colnr_T)(reginput - regline);
5631}
5632
5633 static void
5634save_se_one(savep, pp)
5635 save_se_T *savep;
5636 char_u **pp;
5637{
5638 savep->se_u.ptr = *pp;
5639 *pp = reginput;
5640}
5641
5642/*
5643 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5644 */
5645 static int
5646re_num_cmp(val, scan)
5647 long_u val;
5648 char_u *scan;
5649{
5650 long_u n = OPERAND_MIN(scan);
5651
5652 if (OPERAND_CMP(scan) == '>')
5653 return val > n;
5654 if (OPERAND_CMP(scan) == '<')
5655 return val < n;
5656 return val == n;
5657}
5658
5659
5660#ifdef DEBUG
5661
5662/*
5663 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5664 */
5665 static void
5666regdump(pattern, r)
5667 char_u *pattern;
5668 regprog_T *r;
5669{
5670 char_u *s;
5671 int op = EXACTLY; /* Arbitrary non-END op. */
5672 char_u *next;
5673 char_u *end = NULL;
5674
5675 printf("\r\nregcomp(%s):\r\n", pattern);
5676
5677 s = r->program + 1;
5678 /*
5679 * Loop until we find the END that isn't before a referred next (an END
5680 * can also appear in a NOMATCH operand).
5681 */
5682 while (op != END || s <= end)
5683 {
5684 op = OP(s);
5685 printf("%2d%s", (int)(s - r->program), regprop(s)); /* Where, what. */
5686 next = regnext(s);
5687 if (next == NULL) /* Next ptr. */
5688 printf("(0)");
5689 else
5690 printf("(%d)", (int)((s - r->program) + (next - s)));
5691 if (end < next)
5692 end = next;
5693 if (op == BRACE_LIMITS)
5694 {
5695 /* Two short ints */
5696 printf(" minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5697 s += 8;
5698 }
5699 s += 3;
5700 if (op == ANYOF || op == ANYOF + ADD_NL
5701 || op == ANYBUT || op == ANYBUT + ADD_NL
5702 || op == EXACTLY)
5703 {
5704 /* Literal string, where present. */
5705 while (*s != NUL)
5706 printf("%c", *s++);
5707 s++;
5708 }
5709 printf("\r\n");
5710 }
5711
5712 /* Header fields of interest. */
5713 if (r->regstart != NUL)
5714 printf("start `%s' 0x%x; ", r->regstart < 256
5715 ? (char *)transchar(r->regstart)
5716 : "multibyte", r->regstart);
5717 if (r->reganch)
5718 printf("anchored; ");
5719 if (r->regmust != NULL)
5720 printf("must have \"%s\"", r->regmust);
5721 printf("\r\n");
5722}
5723
5724/*
5725 * regprop - printable representation of opcode
5726 */
5727 static char_u *
5728regprop(op)
5729 char_u *op;
5730{
5731 char_u *p;
5732 static char_u buf[50];
5733
5734 (void) strcpy(buf, ":");
5735
5736 switch (OP(op))
5737 {
5738 case BOL:
5739 p = "BOL";
5740 break;
5741 case EOL:
5742 p = "EOL";
5743 break;
5744 case RE_BOF:
5745 p = "BOF";
5746 break;
5747 case RE_EOF:
5748 p = "EOF";
5749 break;
5750 case CURSOR:
5751 p = "CURSOR";
5752 break;
5753 case RE_LNUM:
5754 p = "RE_LNUM";
5755 break;
5756 case RE_COL:
5757 p = "RE_COL";
5758 break;
5759 case RE_VCOL:
5760 p = "RE_VCOL";
5761 break;
5762 case BOW:
5763 p = "BOW";
5764 break;
5765 case EOW:
5766 p = "EOW";
5767 break;
5768 case ANY:
5769 p = "ANY";
5770 break;
5771 case ANY + ADD_NL:
5772 p = "ANY+NL";
5773 break;
5774 case ANYOF:
5775 p = "ANYOF";
5776 break;
5777 case ANYOF + ADD_NL:
5778 p = "ANYOF+NL";
5779 break;
5780 case ANYBUT:
5781 p = "ANYBUT";
5782 break;
5783 case ANYBUT + ADD_NL:
5784 p = "ANYBUT+NL";
5785 break;
5786 case IDENT:
5787 p = "IDENT";
5788 break;
5789 case IDENT + ADD_NL:
5790 p = "IDENT+NL";
5791 break;
5792 case SIDENT:
5793 p = "SIDENT";
5794 break;
5795 case SIDENT + ADD_NL:
5796 p = "SIDENT+NL";
5797 break;
5798 case KWORD:
5799 p = "KWORD";
5800 break;
5801 case KWORD + ADD_NL:
5802 p = "KWORD+NL";
5803 break;
5804 case SKWORD:
5805 p = "SKWORD";
5806 break;
5807 case SKWORD + ADD_NL:
5808 p = "SKWORD+NL";
5809 break;
5810 case FNAME:
5811 p = "FNAME";
5812 break;
5813 case FNAME + ADD_NL:
5814 p = "FNAME+NL";
5815 break;
5816 case SFNAME:
5817 p = "SFNAME";
5818 break;
5819 case SFNAME + ADD_NL:
5820 p = "SFNAME+NL";
5821 break;
5822 case PRINT:
5823 p = "PRINT";
5824 break;
5825 case PRINT + ADD_NL:
5826 p = "PRINT+NL";
5827 break;
5828 case SPRINT:
5829 p = "SPRINT";
5830 break;
5831 case SPRINT + ADD_NL:
5832 p = "SPRINT+NL";
5833 break;
5834 case WHITE:
5835 p = "WHITE";
5836 break;
5837 case WHITE + ADD_NL:
5838 p = "WHITE+NL";
5839 break;
5840 case NWHITE:
5841 p = "NWHITE";
5842 break;
5843 case NWHITE + ADD_NL:
5844 p = "NWHITE+NL";
5845 break;
5846 case DIGIT:
5847 p = "DIGIT";
5848 break;
5849 case DIGIT + ADD_NL:
5850 p = "DIGIT+NL";
5851 break;
5852 case NDIGIT:
5853 p = "NDIGIT";
5854 break;
5855 case NDIGIT + ADD_NL:
5856 p = "NDIGIT+NL";
5857 break;
5858 case HEX:
5859 p = "HEX";
5860 break;
5861 case HEX + ADD_NL:
5862 p = "HEX+NL";
5863 break;
5864 case NHEX:
5865 p = "NHEX";
5866 break;
5867 case NHEX + ADD_NL:
5868 p = "NHEX+NL";
5869 break;
5870 case OCTAL:
5871 p = "OCTAL";
5872 break;
5873 case OCTAL + ADD_NL:
5874 p = "OCTAL+NL";
5875 break;
5876 case NOCTAL:
5877 p = "NOCTAL";
5878 break;
5879 case NOCTAL + ADD_NL:
5880 p = "NOCTAL+NL";
5881 break;
5882 case WORD:
5883 p = "WORD";
5884 break;
5885 case WORD + ADD_NL:
5886 p = "WORD+NL";
5887 break;
5888 case NWORD:
5889 p = "NWORD";
5890 break;
5891 case NWORD + ADD_NL:
5892 p = "NWORD+NL";
5893 break;
5894 case HEAD:
5895 p = "HEAD";
5896 break;
5897 case HEAD + ADD_NL:
5898 p = "HEAD+NL";
5899 break;
5900 case NHEAD:
5901 p = "NHEAD";
5902 break;
5903 case NHEAD + ADD_NL:
5904 p = "NHEAD+NL";
5905 break;
5906 case ALPHA:
5907 p = "ALPHA";
5908 break;
5909 case ALPHA + ADD_NL:
5910 p = "ALPHA+NL";
5911 break;
5912 case NALPHA:
5913 p = "NALPHA";
5914 break;
5915 case NALPHA + ADD_NL:
5916 p = "NALPHA+NL";
5917 break;
5918 case LOWER:
5919 p = "LOWER";
5920 break;
5921 case LOWER + ADD_NL:
5922 p = "LOWER+NL";
5923 break;
5924 case NLOWER:
5925 p = "NLOWER";
5926 break;
5927 case NLOWER + ADD_NL:
5928 p = "NLOWER+NL";
5929 break;
5930 case UPPER:
5931 p = "UPPER";
5932 break;
5933 case UPPER + ADD_NL:
5934 p = "UPPER+NL";
5935 break;
5936 case NUPPER:
5937 p = "NUPPER";
5938 break;
5939 case NUPPER + ADD_NL:
5940 p = "NUPPER+NL";
5941 break;
5942 case BRANCH:
5943 p = "BRANCH";
5944 break;
5945 case EXACTLY:
5946 p = "EXACTLY";
5947 break;
5948 case NOTHING:
5949 p = "NOTHING";
5950 break;
5951 case BACK:
5952 p = "BACK";
5953 break;
5954 case END:
5955 p = "END";
5956 break;
5957 case MOPEN + 0:
5958 p = "MATCH START";
5959 break;
5960 case MOPEN + 1:
5961 case MOPEN + 2:
5962 case MOPEN + 3:
5963 case MOPEN + 4:
5964 case MOPEN + 5:
5965 case MOPEN + 6:
5966 case MOPEN + 7:
5967 case MOPEN + 8:
5968 case MOPEN + 9:
5969 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
5970 p = NULL;
5971 break;
5972 case MCLOSE + 0:
5973 p = "MATCH END";
5974 break;
5975 case MCLOSE + 1:
5976 case MCLOSE + 2:
5977 case MCLOSE + 3:
5978 case MCLOSE + 4:
5979 case MCLOSE + 5:
5980 case MCLOSE + 6:
5981 case MCLOSE + 7:
5982 case MCLOSE + 8:
5983 case MCLOSE + 9:
5984 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
5985 p = NULL;
5986 break;
5987 case BACKREF + 1:
5988 case BACKREF + 2:
5989 case BACKREF + 3:
5990 case BACKREF + 4:
5991 case BACKREF + 5:
5992 case BACKREF + 6:
5993 case BACKREF + 7:
5994 case BACKREF + 8:
5995 case BACKREF + 9:
5996 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
5997 p = NULL;
5998 break;
5999 case NOPEN:
6000 p = "NOPEN";
6001 break;
6002 case NCLOSE:
6003 p = "NCLOSE";
6004 break;
6005#ifdef FEAT_SYN_HL
6006 case ZOPEN + 1:
6007 case ZOPEN + 2:
6008 case ZOPEN + 3:
6009 case ZOPEN + 4:
6010 case ZOPEN + 5:
6011 case ZOPEN + 6:
6012 case ZOPEN + 7:
6013 case ZOPEN + 8:
6014 case ZOPEN + 9:
6015 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
6016 p = NULL;
6017 break;
6018 case ZCLOSE + 1:
6019 case ZCLOSE + 2:
6020 case ZCLOSE + 3:
6021 case ZCLOSE + 4:
6022 case ZCLOSE + 5:
6023 case ZCLOSE + 6:
6024 case ZCLOSE + 7:
6025 case ZCLOSE + 8:
6026 case ZCLOSE + 9:
6027 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
6028 p = NULL;
6029 break;
6030 case ZREF + 1:
6031 case ZREF + 2:
6032 case ZREF + 3:
6033 case ZREF + 4:
6034 case ZREF + 5:
6035 case ZREF + 6:
6036 case ZREF + 7:
6037 case ZREF + 8:
6038 case ZREF + 9:
6039 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
6040 p = NULL;
6041 break;
6042#endif
6043 case STAR:
6044 p = "STAR";
6045 break;
6046 case PLUS:
6047 p = "PLUS";
6048 break;
6049 case NOMATCH:
6050 p = "NOMATCH";
6051 break;
6052 case MATCH:
6053 p = "MATCH";
6054 break;
6055 case BEHIND:
6056 p = "BEHIND";
6057 break;
6058 case NOBEHIND:
6059 p = "NOBEHIND";
6060 break;
6061 case SUBPAT:
6062 p = "SUBPAT";
6063 break;
6064 case BRACE_LIMITS:
6065 p = "BRACE_LIMITS";
6066 break;
6067 case BRACE_SIMPLE:
6068 p = "BRACE_SIMPLE";
6069 break;
6070 case BRACE_COMPLEX + 0:
6071 case BRACE_COMPLEX + 1:
6072 case BRACE_COMPLEX + 2:
6073 case BRACE_COMPLEX + 3:
6074 case BRACE_COMPLEX + 4:
6075 case BRACE_COMPLEX + 5:
6076 case BRACE_COMPLEX + 6:
6077 case BRACE_COMPLEX + 7:
6078 case BRACE_COMPLEX + 8:
6079 case BRACE_COMPLEX + 9:
6080 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
6081 p = NULL;
6082 break;
6083#ifdef FEAT_MBYTE
6084 case MULTIBYTECODE:
6085 p = "MULTIBYTECODE";
6086 break;
6087#endif
6088 case NEWL:
6089 p = "NEWL";
6090 break;
6091 default:
6092 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
6093 p = NULL;
6094 break;
6095 }
6096 if (p != NULL)
6097 (void) strcat(buf, p);
6098 return buf;
6099}
6100#endif
6101
6102#ifdef FEAT_MBYTE
6103static void mb_decompose __ARGS((int c, int *c1, int *c2, int *c3));
6104
6105typedef struct
6106{
6107 int a, b, c;
6108} decomp_T;
6109
6110
6111/* 0xfb20 - 0xfb4f */
Bram Moolenaard6f676d2005-06-01 21:51:55 +00006112static decomp_T decomp_table[0xfb4f-0xfb20+1] =
Bram Moolenaar071d4272004-06-13 20:20:40 +00006113{
6114 {0x5e2,0,0}, /* 0xfb20 alt ayin */
6115 {0x5d0,0,0}, /* 0xfb21 alt alef */
6116 {0x5d3,0,0}, /* 0xfb22 alt dalet */
6117 {0x5d4,0,0}, /* 0xfb23 alt he */
6118 {0x5db,0,0}, /* 0xfb24 alt kaf */
6119 {0x5dc,0,0}, /* 0xfb25 alt lamed */
6120 {0x5dd,0,0}, /* 0xfb26 alt mem-sofit */
6121 {0x5e8,0,0}, /* 0xfb27 alt resh */
6122 {0x5ea,0,0}, /* 0xfb28 alt tav */
6123 {'+', 0, 0}, /* 0xfb29 alt plus */
6124 {0x5e9, 0x5c1, 0}, /* 0xfb2a shin+shin-dot */
6125 {0x5e9, 0x5c2, 0}, /* 0xfb2b shin+sin-dot */
6126 {0x5e9, 0x5c1, 0x5bc}, /* 0xfb2c shin+shin-dot+dagesh */
6127 {0x5e9, 0x5c2, 0x5bc}, /* 0xfb2d shin+sin-dot+dagesh */
6128 {0x5d0, 0x5b7, 0}, /* 0xfb2e alef+patah */
6129 {0x5d0, 0x5b8, 0}, /* 0xfb2f alef+qamats */
6130 {0x5d0, 0x5b4, 0}, /* 0xfb30 alef+hiriq */
6131 {0x5d1, 0x5bc, 0}, /* 0xfb31 bet+dagesh */
6132 {0x5d2, 0x5bc, 0}, /* 0xfb32 gimel+dagesh */
6133 {0x5d3, 0x5bc, 0}, /* 0xfb33 dalet+dagesh */
6134 {0x5d4, 0x5bc, 0}, /* 0xfb34 he+dagesh */
6135 {0x5d5, 0x5bc, 0}, /* 0xfb35 vav+dagesh */
6136 {0x5d6, 0x5bc, 0}, /* 0xfb36 zayin+dagesh */
6137 {0xfb37, 0, 0}, /* 0xfb37 -- UNUSED */
6138 {0x5d8, 0x5bc, 0}, /* 0xfb38 tet+dagesh */
6139 {0x5d9, 0x5bc, 0}, /* 0xfb39 yud+dagesh */
6140 {0x5da, 0x5bc, 0}, /* 0xfb3a kaf sofit+dagesh */
6141 {0x5db, 0x5bc, 0}, /* 0xfb3b kaf+dagesh */
6142 {0x5dc, 0x5bc, 0}, /* 0xfb3c lamed+dagesh */
6143 {0xfb3d, 0, 0}, /* 0xfb3d -- UNUSED */
6144 {0x5de, 0x5bc, 0}, /* 0xfb3e mem+dagesh */
6145 {0xfb3f, 0, 0}, /* 0xfb3f -- UNUSED */
6146 {0x5e0, 0x5bc, 0}, /* 0xfb40 nun+dagesh */
6147 {0x5e1, 0x5bc, 0}, /* 0xfb41 samech+dagesh */
6148 {0xfb42, 0, 0}, /* 0xfb42 -- UNUSED */
6149 {0x5e3, 0x5bc, 0}, /* 0xfb43 pe sofit+dagesh */
6150 {0x5e4, 0x5bc,0}, /* 0xfb44 pe+dagesh */
6151 {0xfb45, 0, 0}, /* 0xfb45 -- UNUSED */
6152 {0x5e6, 0x5bc, 0}, /* 0xfb46 tsadi+dagesh */
6153 {0x5e7, 0x5bc, 0}, /* 0xfb47 qof+dagesh */
6154 {0x5e8, 0x5bc, 0}, /* 0xfb48 resh+dagesh */
6155 {0x5e9, 0x5bc, 0}, /* 0xfb49 shin+dagesh */
6156 {0x5ea, 0x5bc, 0}, /* 0xfb4a tav+dagesh */
6157 {0x5d5, 0x5b9, 0}, /* 0xfb4b vav+holam */
6158 {0x5d1, 0x5bf, 0}, /* 0xfb4c bet+rafe */
6159 {0x5db, 0x5bf, 0}, /* 0xfb4d kaf+rafe */
6160 {0x5e4, 0x5bf, 0}, /* 0xfb4e pe+rafe */
6161 {0x5d0, 0x5dc, 0} /* 0xfb4f alef-lamed */
6162};
6163
6164 static void
6165mb_decompose(c, c1, c2, c3)
6166 int c, *c1, *c2, *c3;
6167{
6168 decomp_T d;
6169
6170 if (c >= 0x4b20 && c <= 0xfb4f)
6171 {
6172 d = decomp_table[c - 0xfb20];
6173 *c1 = d.a;
6174 *c2 = d.b;
6175 *c3 = d.c;
6176 }
6177 else
6178 {
6179 *c1 = c;
6180 *c2 = *c3 = 0;
6181 }
6182}
6183#endif
6184
6185/*
6186 * Compare two strings, ignore case if ireg_ic set.
6187 * Return 0 if strings match, non-zero otherwise.
6188 * Correct the length "*n" when composing characters are ignored.
6189 */
6190 static int
6191cstrncmp(s1, s2, n)
6192 char_u *s1, *s2;
6193 int *n;
6194{
6195 int result;
6196
6197 if (!ireg_ic)
6198 result = STRNCMP(s1, s2, *n);
6199 else
6200 result = MB_STRNICMP(s1, s2, *n);
6201
6202#ifdef FEAT_MBYTE
6203 /* if it failed and it's utf8 and we want to combineignore: */
6204 if (result != 0 && enc_utf8 && ireg_icombine)
6205 {
6206 char_u *str1, *str2;
6207 int c1, c2, c11, c12;
6208 int ix;
6209 int junk;
6210
6211 /* we have to handle the strcmp ourselves, since it is necessary to
6212 * deal with the composing characters by ignoring them: */
6213 str1 = s1;
6214 str2 = s2;
6215 c1 = c2 = 0;
6216 for (ix = 0; ix < *n; )
6217 {
6218 c1 = mb_ptr2char_adv(&str1);
6219 c2 = mb_ptr2char_adv(&str2);
6220 ix += utf_char2len(c1);
6221
6222 /* decompose the character if necessary, into 'base' characters
6223 * because I don't care about Arabic, I will hard-code the Hebrew
6224 * which I *do* care about! So sue me... */
6225 if (c1 != c2 && (!ireg_ic || utf_fold(c1) != utf_fold(c2)))
6226 {
6227 /* decomposition necessary? */
6228 mb_decompose(c1, &c11, &junk, &junk);
6229 mb_decompose(c2, &c12, &junk, &junk);
6230 c1 = c11;
6231 c2 = c12;
6232 if (c11 != c12 && (!ireg_ic || utf_fold(c11) != utf_fold(c12)))
6233 break;
6234 }
6235 }
6236 result = c2 - c1;
6237 if (result == 0)
6238 *n = (int)(str2 - s2);
6239 }
6240#endif
6241
6242 return result;
6243}
6244
6245/*
6246 * cstrchr: This function is used a lot for simple searches, keep it fast!
6247 */
6248 static char_u *
6249cstrchr(s, c)
6250 char_u *s;
6251 int c;
6252{
6253 char_u *p;
6254 int cc;
6255
6256 if (!ireg_ic
6257#ifdef FEAT_MBYTE
6258 || (!enc_utf8 && mb_char2len(c) > 1)
6259#endif
6260 )
6261 return vim_strchr(s, c);
6262
6263 /* tolower() and toupper() can be slow, comparing twice should be a lot
6264 * faster (esp. when using MS Visual C++!).
6265 * For UTF-8 need to use folded case. */
6266#ifdef FEAT_MBYTE
6267 if (enc_utf8 && c > 0x80)
6268 cc = utf_fold(c);
6269 else
6270#endif
6271 if (isupper(c))
6272 cc = TOLOWER_LOC(c);
6273 else if (islower(c))
6274 cc = TOUPPER_LOC(c);
6275 else
6276 return vim_strchr(s, c);
6277
6278#ifdef FEAT_MBYTE
6279 if (has_mbyte)
6280 {
6281 for (p = s; *p != NUL; p += (*mb_ptr2len_check)(p))
6282 {
6283 if (enc_utf8 && c > 0x80)
6284 {
6285 if (utf_fold(utf_ptr2char(p)) == cc)
6286 return p;
6287 }
6288 else if (*p == c || *p == cc)
6289 return p;
6290 }
6291 }
6292 else
6293#endif
6294 /* Faster version for when there are no multi-byte characters. */
6295 for (p = s; *p != NUL; ++p)
6296 if (*p == c || *p == cc)
6297 return p;
6298
6299 return NULL;
6300}
6301
6302/***************************************************************
6303 * regsub stuff *
6304 ***************************************************************/
6305
6306/* This stuff below really confuses cc on an SGI -- webb */
6307#ifdef __sgi
6308# undef __ARGS
6309# define __ARGS(x) ()
6310#endif
6311
6312/*
6313 * We should define ftpr as a pointer to a function returning a pointer to
6314 * a function returning a pointer to a function ...
6315 * This is impossible, so we declare a pointer to a function returning a
6316 * pointer to a function returning void. This should work for all compilers.
6317 */
6318typedef void (*(*fptr) __ARGS((char_u *, int)))();
6319
6320static fptr do_upper __ARGS((char_u *, int));
6321static fptr do_Upper __ARGS((char_u *, int));
6322static fptr do_lower __ARGS((char_u *, int));
6323static fptr do_Lower __ARGS((char_u *, int));
6324
6325static int vim_regsub_both __ARGS((char_u *source, char_u *dest, int copy, int magic, int backslash));
6326
6327 static fptr
6328do_upper(d, c)
6329 char_u *d;
6330 int c;
6331{
6332 *d = TOUPPER_LOC(c);
6333
6334 return (fptr)NULL;
6335}
6336
6337 static fptr
6338do_Upper(d, c)
6339 char_u *d;
6340 int c;
6341{
6342 *d = TOUPPER_LOC(c);
6343
6344 return (fptr)do_Upper;
6345}
6346
6347 static fptr
6348do_lower(d, c)
6349 char_u *d;
6350 int c;
6351{
6352 *d = TOLOWER_LOC(c);
6353
6354 return (fptr)NULL;
6355}
6356
6357 static fptr
6358do_Lower(d, c)
6359 char_u *d;
6360 int c;
6361{
6362 *d = TOLOWER_LOC(c);
6363
6364 return (fptr)do_Lower;
6365}
6366
6367/*
6368 * regtilde(): Replace tildes in the pattern by the old pattern.
6369 *
6370 * Short explanation of the tilde: It stands for the previous replacement
6371 * pattern. If that previous pattern also contains a ~ we should go back a
6372 * step further... But we insert the previous pattern into the current one
6373 * and remember that.
6374 * This still does not handle the case where "magic" changes. TODO?
6375 *
6376 * The tildes are parsed once before the first call to vim_regsub().
6377 */
6378 char_u *
6379regtilde(source, magic)
6380 char_u *source;
6381 int magic;
6382{
6383 char_u *newsub = source;
6384 char_u *tmpsub;
6385 char_u *p;
6386 int len;
6387 int prevlen;
6388
6389 for (p = newsub; *p; ++p)
6390 {
6391 if ((*p == '~' && magic) || (*p == '\\' && *(p + 1) == '~' && !magic))
6392 {
6393 if (reg_prev_sub != NULL)
6394 {
6395 /* length = len(newsub) - 1 + len(prev_sub) + 1 */
6396 prevlen = (int)STRLEN(reg_prev_sub);
6397 tmpsub = alloc((unsigned)(STRLEN(newsub) + prevlen));
6398 if (tmpsub != NULL)
6399 {
6400 /* copy prefix */
6401 len = (int)(p - newsub); /* not including ~ */
6402 mch_memmove(tmpsub, newsub, (size_t)len);
6403 /* interpretate tilde */
6404 mch_memmove(tmpsub + len, reg_prev_sub, (size_t)prevlen);
6405 /* copy postfix */
6406 if (!magic)
6407 ++p; /* back off \ */
6408 STRCPY(tmpsub + len + prevlen, p + 1);
6409
6410 if (newsub != source) /* already allocated newsub */
6411 vim_free(newsub);
6412 newsub = tmpsub;
6413 p = newsub + len + prevlen;
6414 }
6415 }
6416 else if (magic)
6417 STRCPY(p, p + 1); /* remove '~' */
6418 else
6419 STRCPY(p, p + 2); /* remove '\~' */
6420 --p;
6421 }
6422 else
6423 {
6424 if (*p == '\\' && p[1]) /* skip escaped characters */
6425 ++p;
6426#ifdef FEAT_MBYTE
6427 if (has_mbyte)
6428 p += (*mb_ptr2len_check)(p) - 1;
6429#endif
6430 }
6431 }
6432
6433 vim_free(reg_prev_sub);
6434 if (newsub != source) /* newsub was allocated, just keep it */
6435 reg_prev_sub = newsub;
6436 else /* no ~ found, need to save newsub */
6437 reg_prev_sub = vim_strsave(newsub);
6438 return newsub;
6439}
6440
6441#ifdef FEAT_EVAL
6442static int can_f_submatch = FALSE; /* TRUE when submatch() can be used */
6443
6444/* These pointers are used instead of reg_match and reg_mmatch for
6445 * reg_submatch(). Needed for when the substitution string is an expression
6446 * that contains a call to substitute() and submatch(). */
6447static regmatch_T *submatch_match;
6448static regmmatch_T *submatch_mmatch;
6449#endif
6450
6451#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
6452/*
6453 * vim_regsub() - perform substitutions after a vim_regexec() or
6454 * vim_regexec_multi() match.
6455 *
6456 * If "copy" is TRUE really copy into "dest".
6457 * If "copy" is FALSE nothing is copied, this is just to find out the length
6458 * of the result.
6459 *
6460 * If "backslash" is TRUE, a backslash will be removed later, need to double
6461 * them to keep them, and insert a backslash before a CR to avoid it being
6462 * replaced with a line break later.
6463 *
6464 * Note: The matched text must not change between the call of
6465 * vim_regexec()/vim_regexec_multi() and vim_regsub()! It would make the back
6466 * references invalid!
6467 *
6468 * Returns the size of the replacement, including terminating NUL.
6469 */
6470 int
6471vim_regsub(rmp, source, dest, copy, magic, backslash)
6472 regmatch_T *rmp;
6473 char_u *source;
6474 char_u *dest;
6475 int copy;
6476 int magic;
6477 int backslash;
6478{
6479 reg_match = rmp;
6480 reg_mmatch = NULL;
6481 reg_maxline = 0;
6482 return vim_regsub_both(source, dest, copy, magic, backslash);
6483}
6484#endif
6485
6486 int
6487vim_regsub_multi(rmp, lnum, source, dest, copy, magic, backslash)
6488 regmmatch_T *rmp;
6489 linenr_T lnum;
6490 char_u *source;
6491 char_u *dest;
6492 int copy;
6493 int magic;
6494 int backslash;
6495{
6496 reg_match = NULL;
6497 reg_mmatch = rmp;
6498 reg_buf = curbuf; /* always works on the current buffer! */
6499 reg_firstlnum = lnum;
6500 reg_maxline = curbuf->b_ml.ml_line_count - lnum;
6501 return vim_regsub_both(source, dest, copy, magic, backslash);
6502}
6503
6504 static int
6505vim_regsub_both(source, dest, copy, magic, backslash)
6506 char_u *source;
6507 char_u *dest;
6508 int copy;
6509 int magic;
6510 int backslash;
6511{
6512 char_u *src;
6513 char_u *dst;
6514 char_u *s;
6515 int c;
6516 int no = -1;
6517 fptr func = (fptr)NULL;
6518 linenr_T clnum = 0; /* init for GCC */
6519 int len = 0; /* init for GCC */
6520#ifdef FEAT_EVAL
6521 static char_u *eval_result = NULL;
6522#endif
6523#ifdef FEAT_MBYTE
6524 int l;
6525#endif
6526
6527
6528 /* Be paranoid... */
6529 if (source == NULL || dest == NULL)
6530 {
6531 EMSG(_(e_null));
6532 return 0;
6533 }
6534 if (prog_magic_wrong())
6535 return 0;
6536 src = source;
6537 dst = dest;
6538
6539 /*
6540 * When the substitute part starts with "\=" evaluate it as an expression.
6541 */
6542 if (source[0] == '\\' && source[1] == '='
6543#ifdef FEAT_EVAL
6544 && !can_f_submatch /* can't do this recursively */
6545#endif
6546 )
6547 {
6548#ifdef FEAT_EVAL
6549 /* To make sure that the length doesn't change between checking the
6550 * length and copying the string, and to speed up things, the
6551 * resulting string is saved from the call with "copy" == FALSE to the
6552 * call with "copy" == TRUE. */
6553 if (copy)
6554 {
6555 if (eval_result != NULL)
6556 {
6557 STRCPY(dest, eval_result);
6558 dst += STRLEN(eval_result);
6559 vim_free(eval_result);
6560 eval_result = NULL;
6561 }
6562 }
6563 else
6564 {
6565 linenr_T save_reg_maxline;
6566 win_T *save_reg_win;
6567 int save_ireg_ic;
6568
6569 vim_free(eval_result);
6570
6571 /* The expression may contain substitute(), which calls us
6572 * recursively. Make sure submatch() gets the text from the first
6573 * level. Don't need to save "reg_buf", because
6574 * vim_regexec_multi() can't be called recursively. */
6575 submatch_match = reg_match;
6576 submatch_mmatch = reg_mmatch;
6577 save_reg_maxline = reg_maxline;
6578 save_reg_win = reg_win;
6579 save_ireg_ic = ireg_ic;
6580 can_f_submatch = TRUE;
6581
6582 eval_result = eval_to_string(source + 2, NULL);
6583 if (eval_result != NULL)
6584 {
Bram Moolenaar1cd871b2004-12-19 22:46:22 +00006585 for (s = eval_result; *s != NUL; mb_ptr_adv(s))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 {
6587 /* Change NL to CR, so that it becomes a line break.
6588 * Skip over a backslashed character. */
6589 if (*s == NL)
6590 *s = CAR;
6591 else if (*s == '\\' && s[1] != NUL)
6592 ++s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 }
6594
6595 dst += STRLEN(eval_result);
6596 }
6597
6598 reg_match = submatch_match;
6599 reg_mmatch = submatch_mmatch;
6600 reg_maxline = save_reg_maxline;
6601 reg_win = save_reg_win;
6602 ireg_ic = save_ireg_ic;
6603 can_f_submatch = FALSE;
6604 }
6605#endif
6606 }
6607 else
6608 while ((c = *src++) != NUL)
6609 {
6610 if (c == '&' && magic)
6611 no = 0;
6612 else if (c == '\\' && *src != NUL)
6613 {
6614 if (*src == '&' && !magic)
6615 {
6616 ++src;
6617 no = 0;
6618 }
6619 else if ('0' <= *src && *src <= '9')
6620 {
6621 no = *src++ - '0';
6622 }
6623 else if (vim_strchr((char_u *)"uUlLeE", *src))
6624 {
6625 switch (*src++)
6626 {
6627 case 'u': func = (fptr)do_upper;
6628 continue;
6629 case 'U': func = (fptr)do_Upper;
6630 continue;
6631 case 'l': func = (fptr)do_lower;
6632 continue;
6633 case 'L': func = (fptr)do_Lower;
6634 continue;
6635 case 'e':
6636 case 'E': func = (fptr)NULL;
6637 continue;
6638 }
6639 }
6640 }
6641 if (no < 0) /* Ordinary character. */
6642 {
6643 if (c == '\\' && *src != NUL)
6644 {
6645 /* Check for abbreviations -- webb */
6646 switch (*src)
6647 {
6648 case 'r': c = CAR; ++src; break;
6649 case 'n': c = NL; ++src; break;
6650 case 't': c = TAB; ++src; break;
6651 /* Oh no! \e already has meaning in subst pat :-( */
6652 /* case 'e': c = ESC; ++src; break; */
6653 case 'b': c = Ctrl_H; ++src; break;
6654
6655 /* If "backslash" is TRUE the backslash will be removed
6656 * later. Used to insert a literal CR. */
6657 default: if (backslash)
6658 {
6659 if (copy)
6660 *dst = '\\';
6661 ++dst;
6662 }
6663 c = *src++;
6664 }
6665 }
6666
6667 /* Write to buffer, if copy is set. */
6668#ifdef FEAT_MBYTE
6669 if (has_mbyte && (l = (*mb_ptr2len_check)(src - 1)) > 1)
6670 {
6671 /* TODO: should use "func" here. */
6672 if (copy)
6673 mch_memmove(dst, src - 1, l);
6674 dst += l - 1;
6675 src += l - 1;
6676 }
6677 else
6678 {
6679#endif
6680 if (copy)
6681 {
6682 if (func == (fptr)NULL) /* just copy */
6683 *dst = c;
6684 else /* change case */
6685 func = (fptr)(func(dst, c));
6686 /* Turbo C complains without the typecast */
6687 }
6688#ifdef FEAT_MBYTE
6689 }
6690#endif
6691 dst++;
6692 }
6693 else
6694 {
6695 if (REG_MULTI)
6696 {
6697 clnum = reg_mmatch->startpos[no].lnum;
6698 if (clnum < 0 || reg_mmatch->endpos[no].lnum < 0)
6699 s = NULL;
6700 else
6701 {
6702 s = reg_getline(clnum) + reg_mmatch->startpos[no].col;
6703 if (reg_mmatch->endpos[no].lnum == clnum)
6704 len = reg_mmatch->endpos[no].col
6705 - reg_mmatch->startpos[no].col;
6706 else
6707 len = (int)STRLEN(s);
6708 }
6709 }
6710 else
6711 {
6712 s = reg_match->startp[no];
6713 if (reg_match->endp[no] == NULL)
6714 s = NULL;
6715 else
6716 len = (int)(reg_match->endp[no] - s);
6717 }
6718 if (s != NULL)
6719 {
6720 for (;;)
6721 {
6722 if (len == 0)
6723 {
6724 if (REG_MULTI)
6725 {
6726 if (reg_mmatch->endpos[no].lnum == clnum)
6727 break;
6728 if (copy)
6729 *dst = CAR;
6730 ++dst;
6731 s = reg_getline(++clnum);
6732 if (reg_mmatch->endpos[no].lnum == clnum)
6733 len = reg_mmatch->endpos[no].col;
6734 else
6735 len = (int)STRLEN(s);
6736 }
6737 else
6738 break;
6739 }
6740 else if (*s == NUL) /* we hit NUL. */
6741 {
6742 if (copy)
6743 EMSG(_(e_re_damg));
6744 goto exit;
6745 }
6746 else
6747 {
6748 if (backslash && (*s == CAR || *s == '\\'))
6749 {
6750 /*
6751 * Insert a backslash in front of a CR, otherwise
6752 * it will be replaced by a line break.
6753 * Number of backslashes will be halved later,
6754 * double them here.
6755 */
6756 if (copy)
6757 {
6758 dst[0] = '\\';
6759 dst[1] = *s;
6760 }
6761 dst += 2;
6762 }
6763#ifdef FEAT_MBYTE
6764 else if (has_mbyte && (l = (*mb_ptr2len_check)(s)) > 1)
6765 {
6766 /* TODO: should use "func" here. */
6767 if (copy)
6768 mch_memmove(dst, s, l);
6769 dst += l;
6770 s += l - 1;
6771 len -= l - 1;
6772 }
6773#endif
6774 else
6775 {
6776 if (copy)
6777 {
6778 if (func == (fptr)NULL) /* just copy */
6779 *dst = *s;
6780 else /* change case */
6781 func = (fptr)(func(dst, *s));
6782 /* Turbo C complains without the typecast */
6783 }
6784 ++dst;
6785 }
6786 ++s;
6787 --len;
6788 }
6789 }
6790 }
6791 no = -1;
6792 }
6793 }
6794 if (copy)
6795 *dst = NUL;
6796
6797exit:
6798 return (int)((dst - dest) + 1);
6799}
6800
6801#ifdef FEAT_EVAL
6802/*
6803 * Used for the submatch() function: get the string from tne n'th submatch in
6804 * allocated memory.
6805 * Returns NULL when not in a ":s" command and for a non-existing submatch.
6806 */
6807 char_u *
6808reg_submatch(no)
6809 int no;
6810{
6811 char_u *retval = NULL;
6812 char_u *s;
6813 int len;
6814 int round;
6815 linenr_T lnum;
6816
6817 if (!can_f_submatch)
6818 return NULL;
6819
6820 if (submatch_match == NULL)
6821 {
6822 /*
6823 * First round: compute the length and allocate memory.
6824 * Second round: copy the text.
6825 */
6826 for (round = 1; round <= 2; ++round)
6827 {
6828 lnum = submatch_mmatch->startpos[no].lnum;
6829 if (lnum < 0 || submatch_mmatch->endpos[no].lnum < 0)
6830 return NULL;
6831
6832 s = reg_getline(lnum) + submatch_mmatch->startpos[no].col;
6833 if (s == NULL) /* anti-crash check, cannot happen? */
6834 break;
6835 if (submatch_mmatch->endpos[no].lnum == lnum)
6836 {
6837 /* Within one line: take form start to end col. */
6838 len = submatch_mmatch->endpos[no].col
6839 - submatch_mmatch->startpos[no].col;
6840 if (round == 2)
6841 {
6842 STRNCPY(retval, s, len);
6843 retval[len] = NUL;
6844 }
6845 ++len;
6846 }
6847 else
6848 {
6849 /* Multiple lines: take start line from start col, middle
6850 * lines completely and end line up to end col. */
6851 len = (int)STRLEN(s);
6852 if (round == 2)
6853 {
6854 STRCPY(retval, s);
6855 retval[len] = '\n';
6856 }
6857 ++len;
6858 ++lnum;
6859 while (lnum < submatch_mmatch->endpos[no].lnum)
6860 {
6861 s = reg_getline(lnum++);
6862 if (round == 2)
6863 STRCPY(retval + len, s);
6864 len += (int)STRLEN(s);
6865 if (round == 2)
6866 retval[len] = '\n';
6867 ++len;
6868 }
6869 if (round == 2)
6870 STRNCPY(retval + len, reg_getline(lnum),
6871 submatch_mmatch->endpos[no].col);
6872 len += submatch_mmatch->endpos[no].col;
6873 if (round == 2)
6874 retval[len] = NUL;
6875 ++len;
6876 }
6877
6878 if (round == 1)
6879 {
6880 retval = lalloc((long_u)len, TRUE);
6881 if (s == NULL)
6882 return NULL;
6883 }
6884 }
6885 }
6886 else
6887 {
6888 if (submatch_match->endp[no] == NULL)
6889 retval = NULL;
6890 else
6891 {
6892 s = submatch_match->startp[no];
6893 retval = vim_strnsave(s, (int)(submatch_match->endp[no] - s));
6894 }
6895 }
6896
6897 return retval;
6898}
6899#endif