blob: 2af1f56bf63f280e5389bb49621a9e8369d9679a [file] [log] [blame]
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * Backtracking regular expression implementation.
4 *
5 * This file is included in "regexp.c".
6 *
7 * NOTICE:
8 *
9 * This is NOT the original regular expression code as written by Henry
10 * Spencer. This code has been modified specifically for use with the VIM
11 * editor, and should not be used separately from Vim. If you want a good
12 * regular expression library, get the original code. The copyright notice
13 * that follows is from the original.
14 *
15 * END NOTICE
16 *
17 * Copyright (c) 1986 by University of Toronto.
18 * Written by Henry Spencer. Not derived from licensed software.
19 *
20 * Permission is granted to anyone to use this software for any
21 * purpose on any computer system, and to redistribute it freely,
22 * subject to the following restrictions:
23 *
24 * 1. The author is not responsible for the consequences of use of
25 * this software, no matter how awful, even if they arise
26 * from defects in it.
27 *
28 * 2. The origin of this software must not be misrepresented, either
29 * by explicit claim or by omission.
30 *
31 * 3. Altered versions must be plainly marked as such, and must not
32 * be misrepresented as being the original software.
33 *
34 * Beware that some of this code is subtly aware of the way operator
35 * precedence is structured in regular expressions. Serious changes in
36 * regular-expression syntax might require a total rethink.
37 *
38 * Changes have been made by Tony Andrews, Olaf 'Rhialto' Seibert, Robert
39 * Webb, Ciaran McCreesh and Bram Moolenaar.
40 * Named character class support added by Walter Briscoe (1998 Jul 01)
41 */
42
43/*
44 * The "internal use only" fields in regexp.h are present to pass info from
45 * compile to execute that permits the execute phase to run lots faster on
46 * simple cases. They are:
47 *
48 * regstart char that must begin a match; NUL if none obvious; Can be a
49 * multi-byte character.
50 * reganch is the match anchored (at beginning-of-line only)?
51 * regmust string (pointer into program) that match must include, or NULL
52 * regmlen length of regmust string
53 * regflags RF_ values or'ed together
54 *
55 * Regstart and reganch permit very fast decisions on suitable starting points
56 * for a match, cutting down the work a lot. Regmust permits fast rejection
57 * of lines that cannot possibly match. The regmust tests are costly enough
58 * that vim_regcomp() supplies a regmust only if the r.e. contains something
59 * potentially expensive (at present, the only such thing detected is * or +
60 * at the start of the r.e., which can involve a lot of backup). Regmlen is
61 * supplied because the test in vim_regexec() needs it and vim_regcomp() is
62 * computing it anyway.
63 */
64
65/*
66 * Structure for regexp "program". This is essentially a linear encoding
67 * of a nondeterministic finite-state machine (aka syntax charts or
68 * "railroad normal form" in parsing technology). Each node is an opcode
69 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
70 * all nodes except BRANCH and BRACES_COMPLEX implement concatenation; a "next"
71 * pointer with a BRANCH on both ends of it is connecting two alternatives.
72 * (Here we have one of the subtle syntax dependencies: an individual BRANCH
73 * (as opposed to a collection of them) is never concatenated with anything
74 * because of operator precedence). The "next" pointer of a BRACES_COMPLEX
75 * node points to the node after the stuff to be repeated.
76 * The operand of some types of node is a literal string; for others, it is a
77 * node leading into a sub-FSM. In particular, the operand of a BRANCH node
78 * is the first node of the branch.
79 * (NB this is *not* a tree structure: the tail of the branch connects to the
80 * thing following the set of BRANCHes.)
81 *
82 * pattern is coded like:
83 *
84 * +-----------------+
85 * | V
86 * <aa>\|<bb> BRANCH <aa> BRANCH <bb> --> END
87 * | ^ | ^
88 * +------+ +----------+
89 *
90 *
91 * +------------------+
92 * V |
93 * <aa>* BRANCH BRANCH <aa> --> BACK BRANCH --> NOTHING --> END
94 * | | ^ ^
95 * | +---------------+ |
96 * +---------------------------------------------+
97 *
98 *
99 * +----------------------+
100 * V |
101 * <aa>\+ BRANCH <aa> --> BRANCH --> BACK BRANCH --> NOTHING --> END
102 * | | ^ ^
103 * | +-----------+ |
104 * +--------------------------------------------------+
105 *
106 *
107 * +-------------------------+
108 * V |
109 * <aa>\{} BRANCH BRACE_LIMITS --> BRACE_COMPLEX <aa> --> BACK END
110 * | | ^
111 * | +----------------+
112 * +-----------------------------------------------+
113 *
114 *
115 * <aa>\@!<bb> BRANCH NOMATCH <aa> --> END <bb> --> END
116 * | | ^ ^
117 * | +----------------+ |
118 * +--------------------------------+
119 *
120 * +---------+
121 * | V
122 * \z[abc] BRANCH BRANCH a BRANCH b BRANCH c BRANCH NOTHING --> END
123 * | | | | ^ ^
124 * | | | +-----+ |
125 * | | +----------------+ |
126 * | +---------------------------+ |
127 * +------------------------------------------------------+
128 *
129 * They all start with a BRANCH for "\|" alternatives, even when there is only
130 * one alternative.
131 */
132
133/*
134 * The opcodes are:
135 */
136
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200137// definition number opnd? meaning
138#define END 0 // End of program or NOMATCH operand.
139#define BOL 1 // Match "" at beginning of line.
140#define EOL 2 // Match "" at end of line.
141#define BRANCH 3 // node Match this alternative, or the
142 // next...
143#define BACK 4 // Match "", "next" ptr points backward.
144#define EXACTLY 5 // str Match this string.
145#define NOTHING 6 // Match empty string.
146#define STAR 7 // node Match this (simple) thing 0 or more
147 // times.
148#define PLUS 8 // node Match this (simple) thing 1 or more
149 // times.
150#define MATCH 9 // node match the operand zero-width
151#define NOMATCH 10 // node check for no match with operand
152#define BEHIND 11 // node look behind for a match with operand
153#define NOBEHIND 12 // node look behind for no match with operand
154#define SUBPAT 13 // node match the operand here
155#define BRACE_SIMPLE 14 // node Match this (simple) thing between m and
156 // n times (\{m,n\}).
157#define BOW 15 // Match "" after [^a-zA-Z0-9_]
158#define EOW 16 // Match "" at [^a-zA-Z0-9_]
159#define BRACE_LIMITS 17 // nr nr define the min & max for BRACE_SIMPLE
160 // and BRACE_COMPLEX.
161#define NEWL 18 // Match line-break
162#define BHPOS 19 // End position for BEHIND or NOBEHIND
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200163
164
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200165// character classes: 20-48 normal, 50-78 include a line-break
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200166#define ADD_NL 30
167#define FIRST_NL ANY + ADD_NL
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200168#define ANY 20 // Match any one character.
169#define ANYOF 21 // str Match any character in this string.
170#define ANYBUT 22 // str Match any character not in this
171 // string.
172#define IDENT 23 // Match identifier char
173#define SIDENT 24 // Match identifier char but no digit
174#define KWORD 25 // Match keyword char
175#define SKWORD 26 // Match word char but no digit
176#define FNAME 27 // Match file name char
177#define SFNAME 28 // Match file name char but no digit
178#define PRINT 29 // Match printable char
179#define SPRINT 30 // Match printable char but no digit
180#define WHITE 31 // Match whitespace char
181#define NWHITE 32 // Match non-whitespace char
182#define DIGIT 33 // Match digit char
183#define NDIGIT 34 // Match non-digit char
184#define HEX 35 // Match hex char
185#define NHEX 36 // Match non-hex char
186#define OCTAL 37 // Match octal char
187#define NOCTAL 38 // Match non-octal char
188#define WORD 39 // Match word char
189#define NWORD 40 // Match non-word char
190#define HEAD 41 // Match head char
191#define NHEAD 42 // Match non-head char
192#define ALPHA 43 // Match alpha char
193#define NALPHA 44 // Match non-alpha char
194#define LOWER 45 // Match lowercase char
195#define NLOWER 46 // Match non-lowercase char
196#define UPPER 47 // Match uppercase char
197#define NUPPER 48 // Match non-uppercase char
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200198#define LAST_NL NUPPER + ADD_NL
199#define WITH_NL(op) ((op) >= FIRST_NL && (op) <= LAST_NL)
200
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200201#define MOPEN 80 // -89 Mark this point in input as start of
202 // \( subexpr. MOPEN + 0 marks start of
203 // match.
204#define MCLOSE 90 // -99 Analogous to MOPEN. MCLOSE + 0 marks
205 // end of match.
206#define BACKREF 100 // -109 node Match same string again \1-\9
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200207
208#ifdef FEAT_SYN_HL
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200209# define ZOPEN 110 // -119 Mark this point in input as start of
210 // \z( subexpr.
211# define ZCLOSE 120 // -129 Analogous to ZOPEN.
212# define ZREF 130 // -139 node Match external submatch \z1-\z9
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200213#endif
214
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200215#define BRACE_COMPLEX 140 // -149 node Match nodes between m & n times
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200216
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200217#define NOPEN 150 // Mark this point in input as start of
218 // \%( subexpr.
219#define NCLOSE 151 // Analogous to NOPEN.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200220
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200221#define MULTIBYTECODE 200 // mbc Match one multi-byte character
222#define RE_BOF 201 // Match "" at beginning of file.
223#define RE_EOF 202 // Match "" at end of file.
224#define CURSOR 203 // Match location of cursor.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200225
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200226#define RE_LNUM 204 // nr cmp Match line number
227#define RE_COL 205 // nr cmp Match column number
228#define RE_VCOL 206 // nr cmp Match virtual column number
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200229
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200230#define RE_MARK 207 // mark cmp Match mark position
231#define RE_VISUAL 208 // Match Visual area
232#define RE_COMPOSING 209 // any composing characters
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200233
234/*
235 * Flags to be passed up and down.
236 */
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200237#define HASWIDTH 0x1 // Known never to match null string.
238#define SIMPLE 0x2 // Simple enough to be STAR/PLUS operand.
239#define SPSTART 0x4 // Starts with * or +.
240#define HASNL 0x8 // Contains some \n.
241#define HASLOOKBH 0x10 // Contains "\@<=" or "\@<!".
242#define WORST 0 // Worst case.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200243
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200244static int num_complex_braces; // Complex \{...} count
245static char_u *regcode; // Code-emit pointer, or JUST_CALC_SIZE
246static long regsize; // Code size.
247static int reg_toolong; // TRUE when offset out of range
248static char_u had_endbrace[NSUBEXP]; // flags, TRUE if end of () found
249static long brace_min[10]; // Minimums for complex brace repeats
250static long brace_max[10]; // Maximums for complex brace repeats
251static int brace_count[10]; // Current counts for complex brace repeats
252static int one_exactly = FALSE; // only do one char for EXACTLY
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200253
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200254// When making changes to classchars also change nfa_classcodes.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200255static char_u *classchars = (char_u *)".iIkKfFpPsSdDxXoOwWhHaAlLuU";
256static int classcodes[] = {
257 ANY, IDENT, SIDENT, KWORD, SKWORD,
258 FNAME, SFNAME, PRINT, SPRINT,
259 WHITE, NWHITE, DIGIT, NDIGIT,
260 HEX, NHEX, OCTAL, NOCTAL,
261 WORD, NWORD, HEAD, NHEAD,
262 ALPHA, NALPHA, LOWER, NLOWER,
263 UPPER, NUPPER
264};
265
266/*
267 * When regcode is set to this value, code is not emitted and size is computed
268 * instead.
269 */
270#define JUST_CALC_SIZE ((char_u *) -1)
271
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200272// Values for rs_state in regitem_T.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200273typedef enum regstate_E
274{
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200275 RS_NOPEN = 0 // NOPEN and NCLOSE
276 , RS_MOPEN // MOPEN + [0-9]
277 , RS_MCLOSE // MCLOSE + [0-9]
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200278#ifdef FEAT_SYN_HL
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200279 , RS_ZOPEN // ZOPEN + [0-9]
280 , RS_ZCLOSE // ZCLOSE + [0-9]
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200281#endif
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200282 , RS_BRANCH // BRANCH
283 , RS_BRCPLX_MORE // BRACE_COMPLEX and trying one more match
284 , RS_BRCPLX_LONG // BRACE_COMPLEX and trying longest match
285 , RS_BRCPLX_SHORT // BRACE_COMPLEX and trying shortest match
286 , RS_NOMATCH // NOMATCH
287 , RS_BEHIND1 // BEHIND / NOBEHIND matching rest
288 , RS_BEHIND2 // BEHIND / NOBEHIND matching behind part
289 , RS_STAR_LONG // STAR/PLUS/BRACE_SIMPLE longest match
290 , RS_STAR_SHORT // STAR/PLUS/BRACE_SIMPLE shortest match
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200291} regstate_T;
292
293/*
294 * Structure used to save the current input state, when it needs to be
295 * restored after trying a match. Used by reg_save() and reg_restore().
296 * Also stores the length of "backpos".
297 */
298typedef struct
299{
300 union
301 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200302 char_u *ptr; // rex.input pointer, for single-line regexp
303 lpos_T pos; // rex.input pos, for multi-line regexp
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200304 } rs_u;
305 int rs_len;
306} regsave_T;
307
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200308// struct to save start/end pointer/position in for \(\)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200309typedef struct
310{
311 union
312 {
313 char_u *ptr;
314 lpos_T pos;
315 } se_u;
316} save_se_T;
317
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200318// used for BEHIND and NOBEHIND matching
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200319typedef struct regbehind_S
320{
321 regsave_T save_after;
322 regsave_T save_behind;
323 int save_need_clear_subexpr;
324 save_se_T save_start[NSUBEXP];
325 save_se_T save_end[NSUBEXP];
326} regbehind_T;
327
328/*
329 * When there are alternatives a regstate_T is put on the regstack to remember
330 * what we are doing.
331 * Before it may be another type of item, depending on rs_state, to remember
332 * more things.
333 */
334typedef struct regitem_S
335{
336 regstate_T rs_state; // what we are doing, one of RS_ above
337 short rs_no; // submatch nr or BEHIND/NOBEHIND
338 char_u *rs_scan; // current node in program
339 union
340 {
341 save_se_T sesave;
342 regsave_T regsave;
343 } rs_un; // room for saving rex.input
344} regitem_T;
345
346
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200347// used for STAR, PLUS and BRACE_SIMPLE matching
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200348typedef struct regstar_S
349{
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200350 int nextb; // next byte
351 int nextb_ic; // next byte reverse case
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200352 long count;
353 long minval;
354 long maxval;
355} regstar_T;
356
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200357// used to store input position when a BACK was encountered, so that we now if
358// we made any progress since the last time.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200359typedef struct backpos_S
360{
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200361 char_u *bp_scan; // "scan" where BACK was encountered
362 regsave_T bp_pos; // last input position
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200363} backpos_T;
364
365/*
366 * "regstack" and "backpos" are used by regmatch(). They are kept over calls
367 * to avoid invoking malloc() and free() often.
368 * "regstack" is a stack with regitem_T items, sometimes preceded by regstar_T
369 * or regbehind_T.
370 * "backpos_T" is a table with backpos_T for BACK
371 */
372static garray_T regstack = {0, 0, 0, 0, NULL};
373static garray_T backpos = {0, 0, 0, 0, NULL};
374
375static regsave_T behind_pos;
376
377/*
378 * Both for regstack and backpos tables we use the following strategy of
379 * allocation (to reduce malloc/free calls):
380 * - Initial size is fairly small.
381 * - When needed, the tables are grown bigger (8 times at first, double after
382 * that).
383 * - After executing the match we free the memory only if the array has grown.
384 * Thus the memory is kept allocated when it's at the initial size.
385 * This makes it fast while not keeping a lot of memory allocated.
386 * A three times speed increase was observed when using many simple patterns.
387 */
388#define REGSTACK_INITIAL 2048
389#define BACKPOS_INITIAL 64
390
391/*
392 * Opcode notes:
393 *
394 * BRANCH The set of branches constituting a single choice are hooked
395 * together with their "next" pointers, since precedence prevents
396 * anything being concatenated to any individual branch. The
397 * "next" pointer of the last BRANCH in a choice points to the
398 * thing following the whole choice. This is also where the
399 * final "next" pointer of each individual branch points; each
400 * branch starts with the operand node of a BRANCH node.
401 *
402 * BACK Normal "next" pointers all implicitly point forward; BACK
403 * exists to make loop structures possible.
404 *
405 * STAR,PLUS '=', and complex '*' and '+', are implemented as circular
406 * BRANCH structures using BACK. Simple cases (one character
407 * per match) are implemented with STAR and PLUS for speed
408 * and to minimize recursive plunges.
409 *
410 * BRACE_LIMITS This is always followed by a BRACE_SIMPLE or BRACE_COMPLEX
411 * node, and defines the min and max limits to be used for that
412 * node.
413 *
414 * MOPEN,MCLOSE ...are numbered at compile time.
415 * ZOPEN,ZCLOSE ...ditto
416 */
417
418/*
419 * A node is one char of opcode followed by two chars of "next" pointer.
420 * "Next" pointers are stored as two 8-bit bytes, high order first. The
421 * value is a positive offset from the opcode of the node containing it.
422 * An operand, if any, simply follows the node. (Note that much of the
423 * code generation knows about this implicit relationship.)
424 *
425 * Using two bytes for the "next" pointer is vast overkill for most things,
426 * but allows patterns to get big without disasters.
427 */
428#define OP(p) ((int)*(p))
429#define NEXT(p) (((*((p) + 1) & 0377) << 8) + (*((p) + 2) & 0377))
430#define OPERAND(p) ((p) + 3)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200431// Obtain an operand that was stored as four bytes, MSB first.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200432#define OPERAND_MIN(p) (((long)(p)[3] << 24) + ((long)(p)[4] << 16) \
433 + ((long)(p)[5] << 8) + (long)(p)[6])
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200434// Obtain a second operand stored as four bytes.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200435#define OPERAND_MAX(p) OPERAND_MIN((p) + 4)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200436// Obtain a second single-byte operand stored after a four bytes operand.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200437#define OPERAND_CMP(p) (p)[7]
438
439static char_u *reg(int paren, int *flagp);
440
441#ifdef BT_REGEXP_DUMP
442static void regdump(char_u *, bt_regprog_T *);
443#endif
444
445static int re_num_cmp(long_u val, char_u *scan);
446
447#ifdef DEBUG
448static char_u *regprop(char_u *);
449
450static int regnarrate = 0;
451#endif
452
453
454/*
455 * Setup to parse the regexp. Used once to get the length and once to do it.
456 */
457 static void
458regcomp_start(
459 char_u *expr,
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200460 int re_flags) // see vim_regcomp()
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200461{
462 initchr(expr);
463 if (re_flags & RE_MAGIC)
464 reg_magic = MAGIC_ON;
465 else
466 reg_magic = MAGIC_OFF;
467 reg_string = (re_flags & RE_STRING);
468 reg_strict = (re_flags & RE_STRICT);
469 get_cpo_flags();
470
471 num_complex_braces = 0;
472 regnpar = 1;
Bram Moolenaara80faa82020-04-12 19:37:17 +0200473 CLEAR_FIELD(had_endbrace);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200474#ifdef FEAT_SYN_HL
475 regnzpar = 1;
476 re_has_z = 0;
477#endif
478 regsize = 0L;
479 reg_toolong = FALSE;
480 regflags = 0;
481#if defined(FEAT_SYN_HL) || defined(PROTO)
482 had_eol = FALSE;
483#endif
484}
485
486/*
487 * Return TRUE if MULTIBYTECODE should be used instead of EXACTLY for
488 * character "c".
489 */
490 static int
491use_multibytecode(int c)
492{
493 return has_mbyte && (*mb_char2len)(c) > 1
494 && (re_multi_type(peekchr()) != NOT_MULTI
495 || (enc_utf8 && utf_iscomposing(c)));
496}
497
498/*
499 * Emit (if appropriate) a byte of code
500 */
501 static void
502regc(int b)
503{
504 if (regcode == JUST_CALC_SIZE)
505 regsize++;
506 else
507 *regcode++ = b;
508}
509
510/*
511 * Emit (if appropriate) a multi-byte character of code
512 */
513 static void
514regmbc(int c)
515{
516 if (!has_mbyte && c > 0xff)
517 return;
518 if (regcode == JUST_CALC_SIZE)
519 regsize += (*mb_char2len)(c);
520 else
521 regcode += (*mb_char2bytes)(c, regcode);
522}
523
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200524
525/*
526 * Produce the bytes for equivalence class "c".
527 * Currently only handles latin1, latin9 and utf-8.
528 * NOTE: When changing this function, also change nfa_emit_equi_class()
529 */
530 static void
531reg_equi_class(int c)
532{
533 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0
534 || STRCMP(p_enc, "iso-8859-15") == 0)
535 {
536#ifdef EBCDIC
537 int i;
538
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200539 // This might be slower than switch/case below.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200540 for (i = 0; i < 16; i++)
541 {
542 if (vim_strchr(EQUIVAL_CLASS_C[i], c) != NULL)
543 {
544 char *p = EQUIVAL_CLASS_C[i];
545
546 while (*p != 0)
547 regmbc(*p++);
548 return;
549 }
550 }
551#else
552 switch (c)
553 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200554 // Do not use '\300' style, it results in a negative number.
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200555 case 'A': case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc4:
556 case 0xc5: case 0x100: case 0x102: case 0x104: case 0x1cd:
557 case 0x1de: case 0x1e0: case 0x1fa: case 0x202: case 0x226:
558 case 0x23a: case 0x1e00: case 0x1ea0: case 0x1ea2: case 0x1ea4:
559 case 0x1ea6: case 0x1ea8: case 0x1eaa: case 0x1eac: case 0x1eae:
560 case 0x1eb0: case 0x1eb2: case 0x1eb4: case 0x1eb6:
561 regmbc('A'); regmbc(0xc0); regmbc(0xc1); regmbc(0xc2);
562 regmbc(0xc3); regmbc(0xc4); regmbc(0xc5);
563 regmbc(0x100); regmbc(0x102); regmbc(0x104);
564 regmbc(0x1cd); regmbc(0x1de); regmbc(0x1e0);
565 regmbc(0x1fa); regmbc(0x202); regmbc(0x226);
566 regmbc(0x23a); regmbc(0x1e00); regmbc(0x1ea0);
567 regmbc(0x1ea2); regmbc(0x1ea4); regmbc(0x1ea6);
568 regmbc(0x1ea8); regmbc(0x1eaa); regmbc(0x1eac);
569 regmbc(0x1eae); regmbc(0x1eb0); regmbc(0x1eb2);
570 regmbc(0x1eb4); regmbc(0x1eb6);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200571 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200572 case 'B': case 0x181: case 0x243: case 0x1e02:
573 case 0x1e04: case 0x1e06:
574 regmbc('B');
575 regmbc(0x181); regmbc(0x243); regmbc(0x1e02);
576 regmbc(0x1e04); regmbc(0x1e06);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200577 return;
578 case 'C': case 0xc7:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200579 case 0x106: case 0x108: case 0x10a: case 0x10c: case 0x187:
580 case 0x23b: case 0x1e08: case 0xa792:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200581 regmbc('C'); regmbc(0xc7);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200582 regmbc(0x106); regmbc(0x108); regmbc(0x10a);
583 regmbc(0x10c); regmbc(0x187); regmbc(0x23b);
584 regmbc(0x1e08); regmbc(0xa792);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200585 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200586 case 'D': case 0x10e: case 0x110: case 0x18a:
587 case 0x1e0a: case 0x1e0c: case 0x1e0e: case 0x1e10:
588 case 0x1e12:
589 regmbc('D'); regmbc(0x10e); regmbc(0x110);
590 regmbc(0x18a); regmbc(0x1e0a); regmbc(0x1e0c);
591 regmbc(0x1e0e); regmbc(0x1e10); regmbc(0x1e12);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200592 return;
593 case 'E': case 0xc8: case 0xc9: case 0xca: case 0xcb:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200594 case 0x112: case 0x114: case 0x116: case 0x118: case 0x11a:
595 case 0x204: case 0x206: case 0x228: case 0x246: case 0x1e14:
596 case 0x1e16: case 0x1e18: case 0x1e1a: case 0x1e1c:
597 case 0x1eb8: case 0x1eba: case 0x1ebc: case 0x1ebe:
598 case 0x1ec0: case 0x1ec2: case 0x1ec4: case 0x1ec6:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200599 regmbc('E'); regmbc(0xc8); regmbc(0xc9);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200600 regmbc(0xca); regmbc(0xcb); regmbc(0x112);
601 regmbc(0x114); regmbc(0x116); regmbc(0x118);
602 regmbc(0x11a); regmbc(0x204); regmbc(0x206);
603 regmbc(0x228); regmbc(0x246); regmbc(0x1e14);
604 regmbc(0x1e16); regmbc(0x1e18); regmbc(0x1e1a);
605 regmbc(0x1e1c); regmbc(0x1eb8); regmbc(0x1eba);
606 regmbc(0x1ebc); regmbc(0x1ebe); regmbc(0x1ec0);
607 regmbc(0x1ec2); regmbc(0x1ec4); regmbc(0x1ec6);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200608 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200609 case 'F': case 0x191: case 0x1e1e: case 0xa798:
610 regmbc('F'); regmbc(0x191); regmbc(0x1e1e);
611 regmbc(0xa798);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200612 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200613 case 'G': case 0x11c: case 0x11e: case 0x120:
614 case 0x122: case 0x193: case 0x1e4: case 0x1e6:
615 case 0x1f4: case 0x1e20: case 0xa7a0:
616 regmbc('G'); regmbc(0x11c); regmbc(0x11e);
617 regmbc(0x120); regmbc(0x122); regmbc(0x193);
618 regmbc(0x1e4); regmbc(0x1e6); regmbc(0x1f4);
619 regmbc(0x1e20); regmbc(0xa7a0);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200620 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200621 case 'H': case 0x124: case 0x126: case 0x21e:
622 case 0x1e22: case 0x1e24: case 0x1e26:
623 case 0x1e28: case 0x1e2a: case 0x2c67:
624 regmbc('H'); regmbc(0x124); regmbc(0x126);
625 regmbc(0x21e); regmbc(0x1e22); regmbc(0x1e24);
626 regmbc(0x1e26); regmbc(0x1e28); regmbc(0x1e2a);
627 regmbc(0x2c67);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200628 return;
629 case 'I': case 0xcc: case 0xcd: case 0xce: case 0xcf:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200630 case 0x128: case 0x12a: case 0x12c: case 0x12e:
631 case 0x130: case 0x197: case 0x1cf: case 0x208:
632 case 0x20a: case 0x1e2c: case 0x1e2e: case 0x1ec8:
633 case 0x1eca:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200634 regmbc('I'); regmbc(0xcc); regmbc(0xcd);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200635 regmbc(0xce); regmbc(0xcf); regmbc(0x128);
636 regmbc(0x12a); regmbc(0x12c); regmbc(0x12e);
637 regmbc(0x130); regmbc(0x197); regmbc(0x1cf);
638 regmbc(0x208); regmbc(0x20a); regmbc(0x1e2c);
639 regmbc(0x1e2e); regmbc(0x1ec8); regmbc(0x1eca);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200640 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200641 case 'J': case 0x134: case 0x248:
642 regmbc('J'); regmbc(0x134); regmbc(0x248);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200643 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200644 case 'K': case 0x136: case 0x198: case 0x1e8: case 0x1e30:
645 case 0x1e32: case 0x1e34: case 0x2c69: case 0xa740:
646 regmbc('K'); regmbc(0x136); regmbc(0x198);
647 regmbc(0x1e8); regmbc(0x1e30); regmbc(0x1e32);
648 regmbc(0x1e34); regmbc(0x2c69); regmbc(0xa740);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200649 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200650 case 'L': case 0x139: case 0x13b: case 0x13d: case 0x13f:
651 case 0x141: case 0x23d: case 0x1e36: case 0x1e38:
652 case 0x1e3a: case 0x1e3c: case 0x2c60:
653 regmbc('L'); regmbc(0x139); regmbc(0x13b);
654 regmbc(0x13d); regmbc(0x13f); regmbc(0x141);
655 regmbc(0x23d); regmbc(0x1e36); regmbc(0x1e38);
656 regmbc(0x1e3a); regmbc(0x1e3c); regmbc(0x2c60);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200657 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200658 case 'M': case 0x1e3e: case 0x1e40: case 0x1e42:
659 regmbc('M'); regmbc(0x1e3e); regmbc(0x1e40);
660 regmbc(0x1e42);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200661 return;
662 case 'N': case 0xd1:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200663 case 0x143: case 0x145: case 0x147: case 0x1f8:
664 case 0x1e44: case 0x1e46: case 0x1e48: case 0x1e4a:
665 case 0xa7a4:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200666 regmbc('N'); regmbc(0xd1);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200667 regmbc(0x143); regmbc(0x145); regmbc(0x147);
668 regmbc(0x1f8); regmbc(0x1e44); regmbc(0x1e46);
669 regmbc(0x1e48); regmbc(0x1e4a); regmbc(0xa7a4);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200670 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200671 case 'O': case 0xd2: case 0xd3: case 0xd4: case 0xd5: case 0xd6:
672 case 0xd8: case 0x14c: case 0x14e: case 0x150: case 0x19f:
673 case 0x1a0: case 0x1d1: case 0x1ea: case 0x1ec: case 0x1fe:
674 case 0x20c: case 0x20e: case 0x22a: case 0x22c: case 0x22e:
675 case 0x230: case 0x1e4c: case 0x1e4e: case 0x1e50: case 0x1e52:
676 case 0x1ecc: case 0x1ece: case 0x1ed0: case 0x1ed2: case 0x1ed4:
677 case 0x1ed6: case 0x1ed8: case 0x1eda: case 0x1edc: case 0x1ede:
678 case 0x1ee0: case 0x1ee2:
679 regmbc('O'); regmbc(0xd2); regmbc(0xd3); regmbc(0xd4);
680 regmbc(0xd5); regmbc(0xd6); regmbc(0xd8);
681 regmbc(0x14c); regmbc(0x14e); regmbc(0x150);
682 regmbc(0x19f); regmbc(0x1a0); regmbc(0x1d1);
683 regmbc(0x1ea); regmbc(0x1ec); regmbc(0x1fe);
684 regmbc(0x20c); regmbc(0x20e); regmbc(0x22a);
685 regmbc(0x22c); regmbc(0x22e); regmbc(0x230);
686 regmbc(0x1e4c); regmbc(0x1e4e); regmbc(0x1e50);
687 regmbc(0x1e52); regmbc(0x1ecc); regmbc(0x1ece);
688 regmbc(0x1ed0); regmbc(0x1ed2); regmbc(0x1ed4);
689 regmbc(0x1ed6); regmbc(0x1ed8); regmbc(0x1eda);
690 regmbc(0x1edc); regmbc(0x1ede); regmbc(0x1ee0);
691 regmbc(0x1ee2);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200692 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200693 case 'P': case 0x1a4: case 0x1e54: case 0x1e56: case 0x2c63:
694 regmbc('P'); regmbc(0x1a4); regmbc(0x1e54);
695 regmbc(0x1e56); regmbc(0x2c63);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200696 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200697 case 'Q': case 0x24a:
698 regmbc('Q'); regmbc(0x24a);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200699 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200700 case 'R': case 0x154: case 0x156: case 0x158: case 0x210:
701 case 0x212: case 0x24c: case 0x1e58: case 0x1e5a:
702 case 0x1e5c: case 0x1e5e: case 0x2c64: case 0xa7a6:
703 regmbc('R'); regmbc(0x154); regmbc(0x156);
704 regmbc(0x210); regmbc(0x212); regmbc(0x158);
705 regmbc(0x24c); regmbc(0x1e58); regmbc(0x1e5a);
706 regmbc(0x1e5c); regmbc(0x1e5e); regmbc(0x2c64);
707 regmbc(0xa7a6);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200708 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200709 case 'S': case 0x15a: case 0x15c: case 0x15e: case 0x160:
710 case 0x218: case 0x1e60: case 0x1e62: case 0x1e64:
711 case 0x1e66: case 0x1e68: case 0x2c7e: case 0xa7a8:
712 regmbc('S'); regmbc(0x15a); regmbc(0x15c);
713 regmbc(0x15e); regmbc(0x160); regmbc(0x218);
714 regmbc(0x1e60); regmbc(0x1e62); regmbc(0x1e64);
715 regmbc(0x1e66); regmbc(0x1e68); regmbc(0x2c7e);
716 regmbc(0xa7a8);
717 return;
718 case 'T': case 0x162: case 0x164: case 0x166: case 0x1ac:
719 case 0x1ae: case 0x21a: case 0x23e: case 0x1e6a: case 0x1e6c:
720 case 0x1e6e: case 0x1e70:
721 regmbc('T'); regmbc(0x162); regmbc(0x164);
722 regmbc(0x166); regmbc(0x1ac); regmbc(0x23e);
723 regmbc(0x1ae); regmbc(0x21a); regmbc(0x1e6a);
724 regmbc(0x1e6c); regmbc(0x1e6e); regmbc(0x1e70);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200725 return;
726 case 'U': case 0xd9: case 0xda: case 0xdb: case 0xdc:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200727 case 0x168: case 0x16a: case 0x16c: case 0x16e:
728 case 0x170: case 0x172: case 0x1af: case 0x1d3:
729 case 0x1d5: case 0x1d7: case 0x1d9: case 0x1db:
730 case 0x214: case 0x216: case 0x244: case 0x1e72:
731 case 0x1e74: case 0x1e76: case 0x1e78: case 0x1e7a:
732 case 0x1ee4: case 0x1ee6: case 0x1ee8: case 0x1eea:
733 case 0x1eec: case 0x1eee: case 0x1ef0:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200734 regmbc('U'); regmbc(0xd9); regmbc(0xda);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200735 regmbc(0xdb); regmbc(0xdc); regmbc(0x168);
736 regmbc(0x16a); regmbc(0x16c); regmbc(0x16e);
737 regmbc(0x170); regmbc(0x172); regmbc(0x1af);
738 regmbc(0x1d3); regmbc(0x1d5); regmbc(0x1d7);
739 regmbc(0x1d9); regmbc(0x1db); regmbc(0x214);
740 regmbc(0x216); regmbc(0x244); regmbc(0x1e72);
741 regmbc(0x1e74); regmbc(0x1e76); regmbc(0x1e78);
742 regmbc(0x1e7a); regmbc(0x1ee4); regmbc(0x1ee6);
743 regmbc(0x1ee8); regmbc(0x1eea); regmbc(0x1eec);
744 regmbc(0x1eee); regmbc(0x1ef0);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200745 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200746 case 'V': case 0x1b2: case 0x1e7c: case 0x1e7e:
747 regmbc('V'); regmbc(0x1b2); regmbc(0x1e7c);
748 regmbc(0x1e7e);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200749 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200750 case 'W': case 0x174: case 0x1e80: case 0x1e82:
751 case 0x1e84: case 0x1e86: case 0x1e88:
752 regmbc('W'); regmbc(0x174); regmbc(0x1e80);
753 regmbc(0x1e82); regmbc(0x1e84); regmbc(0x1e86);
754 regmbc(0x1e88);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200755 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200756 case 'X': case 0x1e8a: case 0x1e8c:
757 regmbc('X'); regmbc(0x1e8a); regmbc(0x1e8c);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200758 return;
759 case 'Y': case 0xdd:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200760 case 0x176: case 0x178: case 0x1b3: case 0x232: case 0x24e:
761 case 0x1e8e: case 0x1ef2: case 0x1ef6: case 0x1ef4: case 0x1ef8:
762 regmbc('Y'); regmbc(0xdd); regmbc(0x176);
763 regmbc(0x178); regmbc(0x1b3); regmbc(0x232);
764 regmbc(0x24e); regmbc(0x1e8e); regmbc(0x1ef2);
765 regmbc(0x1ef4); regmbc(0x1ef6); regmbc(0x1ef8);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200766 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200767 case 'Z': case 0x179: case 0x17b: case 0x17d: case 0x1b5:
768 case 0x1e90: case 0x1e92: case 0x1e94: case 0x2c6b:
769 regmbc('Z'); regmbc(0x179); regmbc(0x17b);
770 regmbc(0x17d); regmbc(0x1b5); regmbc(0x1e90);
771 regmbc(0x1e92); regmbc(0x1e94); regmbc(0x2c6b);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200772 return;
773 case 'a': case 0xe0: case 0xe1: case 0xe2:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200774 case 0xe3: case 0xe4: case 0xe5: case 0x101: case 0x103:
775 case 0x105: case 0x1ce: case 0x1df: case 0x1e1: case 0x1fb:
776 case 0x201: case 0x203: case 0x227: case 0x1d8f: case 0x1e01:
777 case 0x1e9a: case 0x1ea1: case 0x1ea3: case 0x1ea5:
778 case 0x1ea7: case 0x1ea9: case 0x1eab: case 0x1ead:
779 case 0x1eaf: case 0x1eb1: case 0x1eb3: case 0x1eb5:
780 case 0x1eb7: case 0x2c65:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200781 regmbc('a'); regmbc(0xe0); regmbc(0xe1);
782 regmbc(0xe2); regmbc(0xe3); regmbc(0xe4);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200783 regmbc(0xe5); regmbc(0x101); regmbc(0x103);
784 regmbc(0x105); regmbc(0x1ce); regmbc(0x1df);
785 regmbc(0x1e1); regmbc(0x1fb); regmbc(0x201);
786 regmbc(0x203); regmbc(0x227); regmbc(0x1d8f);
787 regmbc(0x1e01); regmbc(0x1e9a); regmbc(0x1ea1);
788 regmbc(0x1ea3); regmbc(0x1ea5); regmbc(0x1ea7);
789 regmbc(0x1ea9); regmbc(0x1eab); regmbc(0x1ead);
790 regmbc(0x1eaf); regmbc(0x1eb1); regmbc(0x1eb3);
791 regmbc(0x1eb5); regmbc(0x1eb7); regmbc(0x2c65);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200792 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200793 case 'b': case 0x180: case 0x253: case 0x1d6c: case 0x1d80:
794 case 0x1e03: case 0x1e05: case 0x1e07:
795 regmbc('b');
796 regmbc(0x180); regmbc(0x253); regmbc(0x1d6c);
797 regmbc(0x1d80); regmbc(0x1e03); regmbc(0x1e05);
798 regmbc(0x1e07);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200799 return;
800 case 'c': case 0xe7:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200801 case 0x107: case 0x109: case 0x10b: case 0x10d: case 0x188:
802 case 0x23c: case 0x1e09: case 0xa793: case 0xa794:
803 regmbc('c'); regmbc(0xe7); regmbc(0x107);
804 regmbc(0x109); regmbc(0x10b); regmbc(0x10d);
805 regmbc(0x188); regmbc(0x23c); regmbc(0x1e09);
806 regmbc(0xa793); regmbc(0xa794);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200807 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200808 case 'd': case 0x10f: case 0x111: case 0x257: case 0x1d6d:
809 case 0x1d81: case 0x1d91: case 0x1e0b: case 0x1e0d:
810 case 0x1e0f: case 0x1e11: case 0x1e13:
811 regmbc('d'); regmbc(0x10f); regmbc(0x111);
812 regmbc(0x257); regmbc(0x1d6d); regmbc(0x1d81);
813 regmbc(0x1d91); regmbc(0x1e0b); regmbc(0x1e0d);
814 regmbc(0x1e0f); regmbc(0x1e11); regmbc(0x1e13);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200815 return;
816 case 'e': case 0xe8: case 0xe9: case 0xea: case 0xeb:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200817 case 0x113: case 0x115: case 0x117: case 0x119:
818 case 0x11b: case 0x205: case 0x207: case 0x229:
819 case 0x247: case 0x1d92: case 0x1e15: case 0x1e17:
820 case 0x1e19: case 0x1e1b: case 0x1eb9: case 0x1ebb:
821 case 0x1e1d: case 0x1ebd: case 0x1ebf: case 0x1ec1:
822 case 0x1ec3: case 0x1ec5: case 0x1ec7:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200823 regmbc('e'); regmbc(0xe8); regmbc(0xe9);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200824 regmbc(0xea); regmbc(0xeb); regmbc(0x113);
825 regmbc(0x115); regmbc(0x117); regmbc(0x119);
826 regmbc(0x11b); regmbc(0x205); regmbc(0x207);
827 regmbc(0x229); regmbc(0x247); regmbc(0x1d92);
828 regmbc(0x1e15); regmbc(0x1e17); regmbc(0x1e19);
829 regmbc(0x1e1b); regmbc(0x1e1d); regmbc(0x1eb9);
830 regmbc(0x1ebb); regmbc(0x1ebd); regmbc(0x1ebf);
831 regmbc(0x1ec1); regmbc(0x1ec3); regmbc(0x1ec5);
832 regmbc(0x1ec7);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200833 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200834 case 'f': case 0x192: case 0x1d6e: case 0x1d82:
835 case 0x1e1f: case 0xa799:
836 regmbc('f'); regmbc(0x192); regmbc(0x1d6e);
837 regmbc(0x1d82); regmbc(0x1e1f); regmbc(0xa799);
838 return;
839 case 'g': case 0x11d: case 0x11f: case 0x121: case 0x123:
840 case 0x1e5: case 0x1e7: case 0x260: case 0x1f5: case 0x1d83:
841 case 0x1e21: case 0xa7a1:
842 regmbc('g'); regmbc(0x11d); regmbc(0x11f);
843 regmbc(0x121); regmbc(0x123); regmbc(0x1e5);
844 regmbc(0x1e7); regmbc(0x1f5); regmbc(0x260);
845 regmbc(0x1d83); regmbc(0x1e21); regmbc(0xa7a1);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200846 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200847 case 'h': case 0x125: case 0x127: case 0x21f: case 0x1e23:
848 case 0x1e25: case 0x1e27: case 0x1e29: case 0x1e2b:
849 case 0x1e96: case 0x2c68: case 0xa795:
850 regmbc('h'); regmbc(0x125); regmbc(0x127);
851 regmbc(0x21f); regmbc(0x1e23); regmbc(0x1e25);
852 regmbc(0x1e27); regmbc(0x1e29); regmbc(0x1e2b);
853 regmbc(0x1e96); regmbc(0x2c68); regmbc(0xa795);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200854 return;
855 case 'i': case 0xec: case 0xed: case 0xee: case 0xef:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200856 case 0x129: case 0x12b: case 0x12d: case 0x12f:
857 case 0x1d0: case 0x209: case 0x20b: case 0x268:
858 case 0x1d96: case 0x1e2d: case 0x1e2f: case 0x1ec9:
859 case 0x1ecb:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200860 regmbc('i'); regmbc(0xec); regmbc(0xed);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200861 regmbc(0xee); regmbc(0xef); regmbc(0x129);
862 regmbc(0x12b); regmbc(0x12d); regmbc(0x12f);
863 regmbc(0x1d0); regmbc(0x209); regmbc(0x20b);
864 regmbc(0x268); regmbc(0x1d96); regmbc(0x1e2d);
865 regmbc(0x1e2f); regmbc(0x1ec9); regmbc(0x1ecb);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200866 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200867 case 'j': case 0x135: case 0x1f0: case 0x249:
868 regmbc('j'); regmbc(0x135); regmbc(0x1f0);
869 regmbc(0x249);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200870 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200871 case 'k': case 0x137: case 0x199: case 0x1e9:
872 case 0x1d84: case 0x1e31: case 0x1e33: case 0x1e35:
873 case 0x2c6a: case 0xa741:
874 regmbc('k'); regmbc(0x137); regmbc(0x199);
875 regmbc(0x1e9); regmbc(0x1d84); regmbc(0x1e31);
876 regmbc(0x1e33); regmbc(0x1e35); regmbc(0x2c6a);
877 regmbc(0xa741);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200878 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200879 case 'l': case 0x13a: case 0x13c: case 0x13e:
880 case 0x140: case 0x142: case 0x19a: case 0x1e37:
881 case 0x1e39: case 0x1e3b: case 0x1e3d: case 0x2c61:
882 regmbc('l'); regmbc(0x13a); regmbc(0x13c);
883 regmbc(0x13e); regmbc(0x140); regmbc(0x142);
884 regmbc(0x19a); regmbc(0x1e37); regmbc(0x1e39);
885 regmbc(0x1e3b); regmbc(0x1e3d); regmbc(0x2c61);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200886 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200887 case 'm': case 0x1d6f: case 0x1e3f: case 0x1e41: case 0x1e43:
888 regmbc('m'); regmbc(0x1d6f); regmbc(0x1e3f);
889 regmbc(0x1e41); regmbc(0x1e43);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200890 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200891 case 'n': case 0xf1: case 0x144: case 0x146: case 0x148:
892 case 0x149: case 0x1f9: case 0x1d70: case 0x1d87:
893 case 0x1e45: case 0x1e47: case 0x1e49: case 0x1e4b:
894 case 0xa7a5:
895 regmbc('n'); regmbc(0xf1); regmbc(0x144);
896 regmbc(0x146); regmbc(0x148); regmbc(0x149);
897 regmbc(0x1f9); regmbc(0x1d70); regmbc(0x1d87);
898 regmbc(0x1e45); regmbc(0x1e47); regmbc(0x1e49);
899 regmbc(0x1e4b); regmbc(0xa7a5);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200900 return;
901 case 'o': case 0xf2: case 0xf3: case 0xf4: case 0xf5:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200902 case 0xf6: case 0xf8: case 0x14d: case 0x14f: case 0x151:
903 case 0x1a1: case 0x1d2: case 0x1eb: case 0x1ed: case 0x1ff:
904 case 0x20d: case 0x20f: case 0x22b: case 0x22d: case 0x22f:
905 case 0x231: case 0x275: case 0x1e4d: case 0x1e4f:
906 case 0x1e51: case 0x1e53: case 0x1ecd: case 0x1ecf:
907 case 0x1ed1: case 0x1ed3: case 0x1ed5: case 0x1ed7:
908 case 0x1ed9: case 0x1edb: case 0x1edd: case 0x1edf:
909 case 0x1ee1: case 0x1ee3:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200910 regmbc('o'); regmbc(0xf2); regmbc(0xf3);
911 regmbc(0xf4); regmbc(0xf5); regmbc(0xf6);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200912 regmbc(0xf8); regmbc(0x14d); regmbc(0x14f);
913 regmbc(0x151); regmbc(0x1a1); regmbc(0x1d2);
914 regmbc(0x1eb); regmbc(0x1ed); regmbc(0x1ff);
915 regmbc(0x20d); regmbc(0x20f); regmbc(0x22b);
916 regmbc(0x22d); regmbc(0x22f); regmbc(0x231);
917 regmbc(0x275); regmbc(0x1e4d); regmbc(0x1e4f);
918 regmbc(0x1e51); regmbc(0x1e53); regmbc(0x1ecd);
919 regmbc(0x1ecf); regmbc(0x1ed1); regmbc(0x1ed3);
920 regmbc(0x1ed5); regmbc(0x1ed7); regmbc(0x1ed9);
921 regmbc(0x1edb); regmbc(0x1edd); regmbc(0x1edf);
922 regmbc(0x1ee1); regmbc(0x1ee3);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200923 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200924 case 'p': case 0x1a5: case 0x1d71: case 0x1d88: case 0x1d7d:
925 case 0x1e55: case 0x1e57:
926 regmbc('p'); regmbc(0x1a5); regmbc(0x1d71);
927 regmbc(0x1d7d); regmbc(0x1d88); regmbc(0x1e55);
928 regmbc(0x1e57);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200929 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200930 case 'q': case 0x24b: case 0x2a0:
931 regmbc('q'); regmbc(0x24b); regmbc(0x2a0);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200932 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200933 case 'r': case 0x155: case 0x157: case 0x159: case 0x211:
934 case 0x213: case 0x24d: case 0x27d: case 0x1d72: case 0x1d73:
935 case 0x1d89: case 0x1e59: case 0x1e5b: case 0x1e5d: case 0x1e5f:
936 case 0xa7a7:
937 regmbc('r'); regmbc(0x155); regmbc(0x157);
938 regmbc(0x159); regmbc(0x211); regmbc(0x213);
939 regmbc(0x24d); regmbc(0x1d72); regmbc(0x1d73);
940 regmbc(0x1d89); regmbc(0x1e59); regmbc(0x27d);
941 regmbc(0x1e5b); regmbc(0x1e5d); regmbc(0x1e5f);
942 regmbc(0xa7a7);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200943 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200944 case 's': case 0x15b: case 0x15d: case 0x15f: case 0x161:
945 case 0x1e61: case 0x219: case 0x23f: case 0x1d74: case 0x1d8a:
946 case 0x1e63: case 0x1e65: case 0x1e67: case 0x1e69: case 0xa7a9:
947 regmbc('s'); regmbc(0x15b); regmbc(0x15d);
948 regmbc(0x15f); regmbc(0x161); regmbc(0x23f);
949 regmbc(0x219); regmbc(0x1d74); regmbc(0x1d8a);
950 regmbc(0x1e61); regmbc(0x1e63); regmbc(0x1e65);
951 regmbc(0x1e67); regmbc(0x1e69); regmbc(0xa7a9);
952 return;
953 case 't': case 0x163: case 0x165: case 0x167: case 0x1ab:
954 case 0x1ad: case 0x21b: case 0x288: case 0x1d75: case 0x1e6b:
955 case 0x1e6d: case 0x1e6f: case 0x1e71: case 0x1e97: case 0x2c66:
956 regmbc('t'); regmbc(0x163); regmbc(0x165);
957 regmbc(0x167); regmbc(0x1ab); regmbc(0x21b);
958 regmbc(0x1ad); regmbc(0x288); regmbc(0x1d75);
959 regmbc(0x1e6b); regmbc(0x1e6d); regmbc(0x1e6f);
960 regmbc(0x1e71); regmbc(0x1e97); regmbc(0x2c66);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200961 return;
962 case 'u': case 0xf9: case 0xfa: case 0xfb: case 0xfc:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200963 case 0x169: case 0x16b: case 0x16d: case 0x16f:
964 case 0x171: case 0x173: case 0x1b0: case 0x1d4:
965 case 0x1d6: case 0x1d8: case 0x1da: case 0x1dc:
966 case 0x215: case 0x217: case 0x289: case 0x1e73:
967 case 0x1d7e: case 0x1d99: case 0x1e75: case 0x1e77:
968 case 0x1e79: case 0x1e7b: case 0x1ee5: case 0x1ee7:
969 case 0x1ee9: case 0x1eeb: case 0x1eed: case 0x1eef:
970 case 0x1ef1:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200971 regmbc('u'); regmbc(0xf9); regmbc(0xfa);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200972 regmbc(0xfb); regmbc(0xfc); regmbc(0x169);
973 regmbc(0x16b); regmbc(0x16d); regmbc(0x16f);
974 regmbc(0x171); regmbc(0x173); regmbc(0x1d6);
975 regmbc(0x1d8); regmbc(0x1da); regmbc(0x1dc);
976 regmbc(0x215); regmbc(0x217); regmbc(0x1b0);
977 regmbc(0x1d4); regmbc(0x289); regmbc(0x1d7e);
978 regmbc(0x1d99); regmbc(0x1e73); regmbc(0x1e75);
979 regmbc(0x1e77); regmbc(0x1e79); regmbc(0x1e7b);
980 regmbc(0x1ee5); regmbc(0x1ee7); regmbc(0x1ee9);
981 regmbc(0x1eeb); regmbc(0x1eed); regmbc(0x1eef);
982 regmbc(0x1ef1);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200983 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200984 case 'v': case 0x28b: case 0x1d8c: case 0x1e7d: case 0x1e7f:
985 regmbc('v'); regmbc(0x28b); regmbc(0x1d8c);
986 regmbc(0x1e7d); regmbc(0x1e7f);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200987 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200988 case 'w': case 0x175: case 0x1e81: case 0x1e83:
989 case 0x1e85: case 0x1e87: case 0x1e89: case 0x1e98:
990 regmbc('w'); regmbc(0x175); regmbc(0x1e81);
991 regmbc(0x1e83); regmbc(0x1e85); regmbc(0x1e87);
992 regmbc(0x1e89); regmbc(0x1e98);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200993 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200994 case 'x': case 0x1e8b: case 0x1e8d:
995 regmbc('x'); regmbc(0x1e8b); regmbc(0x1e8d);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200996 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200997 case 'y': case 0xfd: case 0xff: case 0x177: case 0x1b4:
998 case 0x233: case 0x24f: case 0x1e8f: case 0x1e99: case 0x1ef3:
999 case 0x1ef5: case 0x1ef7: case 0x1ef9:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001000 regmbc('y'); regmbc(0xfd); regmbc(0xff);
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001001 regmbc(0x177); regmbc(0x1b4); regmbc(0x233);
1002 regmbc(0x24f); regmbc(0x1e8f); regmbc(0x1e99);
1003 regmbc(0x1ef3); regmbc(0x1ef5); regmbc(0x1ef7);
1004 regmbc(0x1ef9);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001005 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +02001006 case 'z': case 0x17a: case 0x17c: case 0x17e: case 0x1b6:
1007 case 0x1d76: case 0x1d8e: case 0x1e91: case 0x1e93:
1008 case 0x1e95: case 0x2c6c:
1009 regmbc('z'); regmbc(0x17a); regmbc(0x17c);
1010 regmbc(0x17e); regmbc(0x1b6); regmbc(0x1d76);
1011 regmbc(0x1d8e); regmbc(0x1e91); regmbc(0x1e93);
1012 regmbc(0x1e95); regmbc(0x2c6c);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001013 return;
1014 }
1015#endif
1016 }
1017 regmbc(c);
1018}
1019
1020/*
1021 * Emit a node.
1022 * Return pointer to generated code.
1023 */
1024 static char_u *
1025regnode(int op)
1026{
1027 char_u *ret;
1028
1029 ret = regcode;
1030 if (ret == JUST_CALC_SIZE)
1031 regsize += 3;
1032 else
1033 {
1034 *regcode++ = op;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001035 *regcode++ = NUL; // Null "next" pointer.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001036 *regcode++ = NUL;
1037 }
1038 return ret;
1039}
1040
1041/*
1042 * Write a long as four bytes at "p" and return pointer to the next char.
1043 */
1044 static char_u *
1045re_put_long(char_u *p, long_u val)
1046{
1047 *p++ = (char_u) ((val >> 24) & 0377);
1048 *p++ = (char_u) ((val >> 16) & 0377);
1049 *p++ = (char_u) ((val >> 8) & 0377);
1050 *p++ = (char_u) (val & 0377);
1051 return p;
1052}
1053
1054/*
1055 * regnext - dig the "next" pointer out of a node
1056 * Returns NULL when calculating size, when there is no next item and when
1057 * there is an error.
1058 */
1059 static char_u *
1060regnext(char_u *p)
1061{
1062 int offset;
1063
1064 if (p == JUST_CALC_SIZE || reg_toolong)
1065 return NULL;
1066
1067 offset = NEXT(p);
1068 if (offset == 0)
1069 return NULL;
1070
1071 if (OP(p) == BACK)
1072 return p - offset;
1073 else
1074 return p + offset;
1075}
1076
1077/*
1078 * Set the next-pointer at the end of a node chain.
1079 */
1080 static void
1081regtail(char_u *p, char_u *val)
1082{
1083 char_u *scan;
1084 char_u *temp;
1085 int offset;
1086
1087 if (p == JUST_CALC_SIZE)
1088 return;
1089
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001090 // Find last node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001091 scan = p;
1092 for (;;)
1093 {
1094 temp = regnext(scan);
1095 if (temp == NULL)
1096 break;
1097 scan = temp;
1098 }
1099
1100 if (OP(scan) == BACK)
1101 offset = (int)(scan - val);
1102 else
1103 offset = (int)(val - scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001104 // When the offset uses more than 16 bits it can no longer fit in the two
1105 // bytes available. Use a global flag to avoid having to check return
1106 // values in too many places.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001107 if (offset > 0xffff)
1108 reg_toolong = TRUE;
1109 else
1110 {
1111 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
1112 *(scan + 2) = (char_u) (offset & 0377);
1113 }
1114}
1115
1116/*
1117 * Like regtail, on item after a BRANCH; nop if none.
1118 */
1119 static void
1120regoptail(char_u *p, char_u *val)
1121{
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001122 // When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless"
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001123 if (p == NULL || p == JUST_CALC_SIZE
1124 || (OP(p) != BRANCH
1125 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
1126 return;
1127 regtail(OPERAND(p), val);
1128}
1129
1130/*
1131 * Insert an operator in front of already-emitted operand
1132 *
1133 * Means relocating the operand.
1134 */
1135 static void
1136reginsert(int op, char_u *opnd)
1137{
1138 char_u *src;
1139 char_u *dst;
1140 char_u *place;
1141
1142 if (regcode == JUST_CALC_SIZE)
1143 {
1144 regsize += 3;
1145 return;
1146 }
1147 src = regcode;
1148 regcode += 3;
1149 dst = regcode;
1150 while (src > opnd)
1151 *--dst = *--src;
1152
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001153 place = opnd; // Op node, where operand used to be.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001154 *place++ = op;
1155 *place++ = NUL;
1156 *place = NUL;
1157}
1158
1159/*
1160 * Insert an operator in front of already-emitted operand.
1161 * Add a number to the operator.
1162 */
1163 static void
1164reginsert_nr(int op, long val, char_u *opnd)
1165{
1166 char_u *src;
1167 char_u *dst;
1168 char_u *place;
1169
1170 if (regcode == JUST_CALC_SIZE)
1171 {
1172 regsize += 7;
1173 return;
1174 }
1175 src = regcode;
1176 regcode += 7;
1177 dst = regcode;
1178 while (src > opnd)
1179 *--dst = *--src;
1180
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001181 place = opnd; // Op node, where operand used to be.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001182 *place++ = op;
1183 *place++ = NUL;
1184 *place++ = NUL;
1185 re_put_long(place, (long_u)val);
1186}
1187
1188/*
1189 * Insert an operator in front of already-emitted operand.
1190 * The operator has the given limit values as operands. Also set next pointer.
1191 *
1192 * Means relocating the operand.
1193 */
1194 static void
1195reginsert_limits(
1196 int op,
1197 long minval,
1198 long maxval,
1199 char_u *opnd)
1200{
1201 char_u *src;
1202 char_u *dst;
1203 char_u *place;
1204
1205 if (regcode == JUST_CALC_SIZE)
1206 {
1207 regsize += 11;
1208 return;
1209 }
1210 src = regcode;
1211 regcode += 11;
1212 dst = regcode;
1213 while (src > opnd)
1214 *--dst = *--src;
1215
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001216 place = opnd; // Op node, where operand used to be.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001217 *place++ = op;
1218 *place++ = NUL;
1219 *place++ = NUL;
1220 place = re_put_long(place, (long_u)minval);
1221 place = re_put_long(place, (long_u)maxval);
1222 regtail(opnd, place);
1223}
1224
1225/*
1226 * Return TRUE if the back reference is legal. We must have seen the close
1227 * brace.
1228 * TODO: Should also check that we don't refer to something that is repeated
1229 * (+*=): what instance of the repetition should we match?
1230 */
1231 static int
1232seen_endbrace(int refnum)
1233{
1234 if (!had_endbrace[refnum])
1235 {
1236 char_u *p;
1237
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001238 // Trick: check if "@<=" or "@<!" follows, in which case
1239 // the \1 can appear before the referenced match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001240 for (p = regparse; *p != NUL; ++p)
1241 if (p[0] == '@' && p[1] == '<' && (p[2] == '!' || p[2] == '='))
1242 break;
1243 if (*p == NUL)
1244 {
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001245 emsg(_(e_illegal_back_reference));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001246 rc_did_emsg = TRUE;
1247 return FALSE;
1248 }
1249 }
1250 return TRUE;
1251}
1252
1253/*
1254 * Parse the lowest level.
1255 *
1256 * Optimization: gobbles an entire sequence of ordinary characters so that
1257 * it can turn them into a single node, which is smaller to store and
1258 * faster to run. Don't do this when one_exactly is set.
1259 */
1260 static char_u *
1261regatom(int *flagp)
1262{
1263 char_u *ret;
1264 int flags;
1265 int c;
1266 char_u *p;
1267 int extra = 0;
1268 int save_prev_at_start = prev_at_start;
1269
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001270 *flagp = WORST; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001271
1272 c = getchr();
1273 switch (c)
1274 {
1275 case Magic('^'):
1276 ret = regnode(BOL);
1277 break;
1278
1279 case Magic('$'):
1280 ret = regnode(EOL);
1281#if defined(FEAT_SYN_HL) || defined(PROTO)
1282 had_eol = TRUE;
1283#endif
1284 break;
1285
1286 case Magic('<'):
1287 ret = regnode(BOW);
1288 break;
1289
1290 case Magic('>'):
1291 ret = regnode(EOW);
1292 break;
1293
1294 case Magic('_'):
1295 c = no_Magic(getchr());
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001296 if (c == '^') // "\_^" is start-of-line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001297 {
1298 ret = regnode(BOL);
1299 break;
1300 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001301 if (c == '$') // "\_$" is end-of-line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001302 {
1303 ret = regnode(EOL);
1304#if defined(FEAT_SYN_HL) || defined(PROTO)
1305 had_eol = TRUE;
1306#endif
1307 break;
1308 }
1309
1310 extra = ADD_NL;
1311 *flagp |= HASNL;
1312
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001313 // "\_[" is character range plus newline
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001314 if (c == '[')
1315 goto collection;
1316
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001317 // "\_x" is character class plus newline
1318 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001319
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001320 // Character classes.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001321 case Magic('.'):
1322 case Magic('i'):
1323 case Magic('I'):
1324 case Magic('k'):
1325 case Magic('K'):
1326 case Magic('f'):
1327 case Magic('F'):
1328 case Magic('p'):
1329 case Magic('P'):
1330 case Magic('s'):
1331 case Magic('S'):
1332 case Magic('d'):
1333 case Magic('D'):
1334 case Magic('x'):
1335 case Magic('X'):
1336 case Magic('o'):
1337 case Magic('O'):
1338 case Magic('w'):
1339 case Magic('W'):
1340 case Magic('h'):
1341 case Magic('H'):
1342 case Magic('a'):
1343 case Magic('A'):
1344 case Magic('l'):
1345 case Magic('L'):
1346 case Magic('u'):
1347 case Magic('U'):
1348 p = vim_strchr(classchars, no_Magic(c));
1349 if (p == NULL)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001350 EMSG_RET_NULL(_(e_invalid_use_of_underscore));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001351
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001352 // When '.' is followed by a composing char ignore the dot, so that
1353 // the composing char is matched here.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001354 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1355 {
1356 c = getchr();
1357 goto do_multibyte;
1358 }
1359 ret = regnode(classcodes[p - classchars] + extra);
1360 *flagp |= HASWIDTH | SIMPLE;
1361 break;
1362
1363 case Magic('n'):
1364 if (reg_string)
1365 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001366 // In a string "\n" matches a newline character.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001367 ret = regnode(EXACTLY);
1368 regc(NL);
1369 regc(NUL);
1370 *flagp |= HASWIDTH | SIMPLE;
1371 }
1372 else
1373 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001374 // In buffer text "\n" matches the end of a line.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001375 ret = regnode(NEWL);
1376 *flagp |= HASWIDTH | HASNL;
1377 }
1378 break;
1379
1380 case Magic('('):
1381 if (one_exactly)
1382 EMSG_ONE_RET_NULL;
1383 ret = reg(REG_PAREN, &flags);
1384 if (ret == NULL)
1385 return NULL;
1386 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1387 break;
1388
1389 case NUL:
1390 case Magic('|'):
1391 case Magic('&'):
1392 case Magic(')'):
1393 if (one_exactly)
1394 EMSG_ONE_RET_NULL;
Bram Moolenaard0819d12021-12-31 23:15:53 +00001395 // Supposed to be caught earlier.
1396 IEMSG_RET_NULL(_(e_internal_error_in_regexp));
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001397 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001398
1399 case Magic('='):
1400 case Magic('?'):
1401 case Magic('+'):
1402 case Magic('@'):
1403 case Magic('{'):
1404 case Magic('*'):
1405 c = no_Magic(c);
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001406 EMSG3_RET_NULL(_(e_str_chr_follows_nothing),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001407 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL), c);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001408 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001409
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001410 case Magic('~'): // previous substitute pattern
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001411 if (reg_prev_sub != NULL)
1412 {
1413 char_u *lp;
1414
1415 ret = regnode(EXACTLY);
1416 lp = reg_prev_sub;
1417 while (*lp != NUL)
1418 regc(*lp++);
1419 regc(NUL);
1420 if (*reg_prev_sub != NUL)
1421 {
1422 *flagp |= HASWIDTH;
1423 if ((lp - reg_prev_sub) == 1)
1424 *flagp |= SIMPLE;
1425 }
1426 }
1427 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001428 EMSG_RET_NULL(_(e_no_previous_substitute_regular_expression));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001429 break;
1430
1431 case Magic('1'):
1432 case Magic('2'):
1433 case Magic('3'):
1434 case Magic('4'):
1435 case Magic('5'):
1436 case Magic('6'):
1437 case Magic('7'):
1438 case Magic('8'):
1439 case Magic('9'):
1440 {
1441 int refnum;
1442
1443 refnum = c - Magic('0');
1444 if (!seen_endbrace(refnum))
1445 return NULL;
1446 ret = regnode(BACKREF + refnum);
1447 }
1448 break;
1449
1450 case Magic('z'):
1451 {
1452 c = no_Magic(getchr());
1453 switch (c)
1454 {
1455#ifdef FEAT_SYN_HL
1456 case '(': if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001457 EMSG_RET_NULL(_(e_z_not_allowed_here));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001458 if (one_exactly)
1459 EMSG_ONE_RET_NULL;
1460 ret = reg(REG_ZPAREN, &flags);
1461 if (ret == NULL)
1462 return NULL;
1463 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
1464 re_has_z = REX_SET;
1465 break;
1466
1467 case '1':
1468 case '2':
1469 case '3':
1470 case '4':
1471 case '5':
1472 case '6':
1473 case '7':
1474 case '8':
1475 case '9': if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001476 EMSG_RET_NULL(_(e_z1_z9_not_allowed_here));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001477 ret = regnode(ZREF + c - '0');
1478 re_has_z = REX_USE;
1479 break;
1480#endif
1481
1482 case 's': ret = regnode(MOPEN + 0);
1483 if (re_mult_next("\\zs") == FAIL)
1484 return NULL;
1485 break;
1486
1487 case 'e': ret = regnode(MCLOSE + 0);
1488 if (re_mult_next("\\ze") == FAIL)
1489 return NULL;
1490 break;
1491
1492 default: EMSG_RET_NULL(_("E68: Invalid character after \\z"));
1493 }
1494 }
1495 break;
1496
1497 case Magic('%'):
1498 {
1499 c = no_Magic(getchr());
1500 switch (c)
1501 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001502 // () without a back reference
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001503 case '(':
1504 if (one_exactly)
1505 EMSG_ONE_RET_NULL;
1506 ret = reg(REG_NPAREN, &flags);
1507 if (ret == NULL)
1508 return NULL;
1509 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1510 break;
1511
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001512 // Catch \%^ and \%$ regardless of where they appear in the
1513 // pattern -- regardless of whether or not it makes sense.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001514 case '^':
1515 ret = regnode(RE_BOF);
1516 break;
1517
1518 case '$':
1519 ret = regnode(RE_EOF);
1520 break;
1521
1522 case '#':
1523 ret = regnode(CURSOR);
1524 break;
1525
1526 case 'V':
1527 ret = regnode(RE_VISUAL);
1528 break;
1529
1530 case 'C':
1531 ret = regnode(RE_COMPOSING);
1532 break;
1533
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001534 // \%[abc]: Emit as a list of branches, all ending at the last
1535 // branch which matches nothing.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001536 case '[':
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001537 if (one_exactly) // doesn't nest
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001538 EMSG_ONE_RET_NULL;
1539 {
1540 char_u *lastbranch;
1541 char_u *lastnode = NULL;
1542 char_u *br;
1543
1544 ret = NULL;
1545 while ((c = getchr()) != ']')
1546 {
1547 if (c == NUL)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001548 EMSG2_RET_NULL(_(e_missing_sb_after_str),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001549 reg_magic == MAGIC_ALL);
1550 br = regnode(BRANCH);
1551 if (ret == NULL)
1552 ret = br;
1553 else
1554 {
1555 regtail(lastnode, br);
1556 if (reg_toolong)
1557 return NULL;
1558 }
1559
1560 ungetchr();
1561 one_exactly = TRUE;
1562 lastnode = regatom(flagp);
1563 one_exactly = FALSE;
1564 if (lastnode == NULL)
1565 return NULL;
1566 }
1567 if (ret == NULL)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001568 EMSG2_RET_NULL(_(e_empty_str_brackets),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001569 reg_magic == MAGIC_ALL);
1570 lastbranch = regnode(BRANCH);
1571 br = regnode(NOTHING);
1572 if (ret != JUST_CALC_SIZE)
1573 {
1574 regtail(lastnode, br);
1575 regtail(lastbranch, br);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001576 // connect all branches to the NOTHING
1577 // branch at the end
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001578 for (br = ret; br != lastnode; )
1579 {
1580 if (OP(br) == BRANCH)
1581 {
1582 regtail(br, lastbranch);
1583 if (reg_toolong)
1584 return NULL;
1585 br = OPERAND(br);
1586 }
1587 else
1588 br = regnext(br);
1589 }
1590 }
1591 *flagp &= ~(HASWIDTH | SIMPLE);
1592 break;
1593 }
1594
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001595 case 'd': // %d123 decimal
1596 case 'o': // %o123 octal
1597 case 'x': // %xab hex 2
1598 case 'u': // %uabcd hex 4
1599 case 'U': // %U1234abcd hex 8
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001600 {
1601 long i;
1602
1603 switch (c)
1604 {
1605 case 'd': i = getdecchrs(); break;
1606 case 'o': i = getoctchrs(); break;
1607 case 'x': i = gethexchrs(2); break;
1608 case 'u': i = gethexchrs(4); break;
1609 case 'U': i = gethexchrs(8); break;
1610 default: i = -1; break;
1611 }
1612
1613 if (i < 0 || i > INT_MAX)
1614 EMSG2_RET_NULL(
1615 _("E678: Invalid character after %s%%[dxouU]"),
1616 reg_magic == MAGIC_ALL);
1617 if (use_multibytecode(i))
1618 ret = regnode(MULTIBYTECODE);
1619 else
1620 ret = regnode(EXACTLY);
1621 if (i == 0)
1622 regc(0x0a);
1623 else
1624 regmbc(i);
1625 regc(NUL);
1626 *flagp |= HASWIDTH;
1627 break;
1628 }
1629
1630 default:
1631 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001632 || c == '\'' || c == '.')
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001633 {
1634 long_u n = 0;
1635 int cmp;
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001636 int cur = FALSE;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001637
1638 cmp = c;
1639 if (cmp == '<' || cmp == '>')
1640 c = getchr();
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001641 if (no_Magic(c) == '.')
1642 {
1643 cur = TRUE;
1644 c = getchr();
1645 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001646 while (VIM_ISDIGIT(c))
1647 {
1648 n = n * 10 + (c - '0');
1649 c = getchr();
1650 }
1651 if (c == '\'' && n == 0)
1652 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001653 // "\%'m", "\%<'m" and "\%>'m": Mark
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001654 c = getchr();
1655 ret = regnode(RE_MARK);
1656 if (ret == JUST_CALC_SIZE)
1657 regsize += 2;
1658 else
1659 {
1660 *regcode++ = c;
1661 *regcode++ = cmp;
1662 }
1663 break;
1664 }
1665 else if (c == 'l' || c == 'c' || c == 'v')
1666 {
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001667 if (cur && n)
1668 {
1669 semsg(_(e_regexp_number_after_dot_pos_search), no_Magic(c));
1670 rc_did_emsg = TRUE;
1671 return NULL;
1672 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001673 if (c == 'l')
1674 {
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001675 if (cur)
1676 n = curwin->w_cursor.lnum;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001677 ret = regnode(RE_LNUM);
1678 if (save_prev_at_start)
1679 at_start = TRUE;
1680 }
1681 else if (c == 'c')
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001682 {
1683 if (cur)
1684 {
1685 n = curwin->w_cursor.col;
1686 n++;
1687 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001688 ret = regnode(RE_COL);
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001689 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001690 else
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001691 {
1692 if (cur)
1693 {
1694 colnr_T vcol = 0;
1695
1696 getvvcol(curwin, &curwin->w_cursor,
1697 NULL, NULL, &vcol);
1698 ++vcol;
1699 n = vcol;
1700 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001701 ret = regnode(RE_VCOL);
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001702 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001703 if (ret == JUST_CALC_SIZE)
1704 regsize += 5;
1705 else
1706 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001707 // put the number and the optional
1708 // comparator after the opcode
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001709 regcode = re_put_long(regcode, n);
1710 *regcode++ = cmp;
1711 }
1712 break;
1713 }
1714 }
1715
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001716 EMSG2_RET_NULL(_(e_invalid_character_after_str),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001717 reg_magic == MAGIC_ALL);
1718 }
1719 }
1720 break;
1721
1722 case Magic('['):
1723collection:
1724 {
1725 char_u *lp;
1726
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001727 // If there is no matching ']', we assume the '[' is a normal
1728 // character. This makes 'incsearch' and ":help [" work.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001729 lp = skip_anyof(regparse);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001730 if (*lp == ']') // there is a matching ']'
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001731 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001732 int startc = -1; // > 0 when next '-' is a range
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001733 int endc;
1734
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001735 // In a character class, different parsing rules apply.
1736 // Not even \ is special anymore, nothing is.
1737 if (*regparse == '^') // Complement of range.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001738 {
1739 ret = regnode(ANYBUT + extra);
1740 regparse++;
1741 }
1742 else
1743 ret = regnode(ANYOF + extra);
1744
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001745 // At the start ']' and '-' mean the literal character.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001746 if (*regparse == ']' || *regparse == '-')
1747 {
1748 startc = *regparse;
1749 regc(*regparse++);
1750 }
1751
1752 while (*regparse != NUL && *regparse != ']')
1753 {
1754 if (*regparse == '-')
1755 {
1756 ++regparse;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001757 // The '-' is not used for a range at the end and
1758 // after or before a '\n'.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001759 if (*regparse == ']' || *regparse == NUL
1760 || startc == -1
1761 || (regparse[0] == '\\' && regparse[1] == 'n'))
1762 {
1763 regc('-');
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001764 startc = '-'; // [--x] is a range
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001765 }
1766 else
1767 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001768 // Also accept "a-[.z.]"
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001769 endc = 0;
1770 if (*regparse == '[')
1771 endc = get_coll_element(&regparse);
1772 if (endc == 0)
1773 {
1774 if (has_mbyte)
1775 endc = mb_ptr2char_adv(&regparse);
1776 else
1777 endc = *regparse++;
1778 }
1779
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001780 // Handle \o40, \x20 and \u20AC style sequences
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001781 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl)
1782 endc = coll_get_char();
1783
1784 if (startc > endc)
1785 EMSG_RET_NULL(_(e_reverse_range));
1786 if (has_mbyte && ((*mb_char2len)(startc) > 1
1787 || (*mb_char2len)(endc) > 1))
1788 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001789 // Limit to a range of 256 chars.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001790 if (endc > startc + 256)
1791 EMSG_RET_NULL(_(e_large_class));
1792 while (++startc <= endc)
1793 regmbc(startc);
1794 }
1795 else
1796 {
1797#ifdef EBCDIC
1798 int alpha_only = FALSE;
1799
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001800 // for alphabetical range skip the gaps
1801 // 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001802 if (isalpha(startc) && isalpha(endc))
1803 alpha_only = TRUE;
1804#endif
1805 while (++startc <= endc)
1806#ifdef EBCDIC
1807 if (!alpha_only || isalpha(startc))
1808#endif
1809 regc(startc);
1810 }
1811 startc = -1;
1812 }
1813 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001814 // Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1815 // accepts "\t", "\e", etc., but only when the 'l' flag in
1816 // 'cpoptions' is not included.
1817 // Posix doesn't recognize backslash at all.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001818 else if (*regparse == '\\'
1819 && !reg_cpo_bsl
1820 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1821 || (!reg_cpo_lit
1822 && vim_strchr(REGEXP_ABBR,
1823 regparse[1]) != NULL)))
1824 {
1825 regparse++;
1826 if (*regparse == 'n')
1827 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001828 // '\n' in range: also match NL
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001829 if (ret != JUST_CALC_SIZE)
1830 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001831 // Using \n inside [^] does not change what
1832 // matches. "[^\n]" is the same as ".".
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001833 if (*ret == ANYOF)
1834 {
1835 *ret = ANYOF + ADD_NL;
1836 *flagp |= HASNL;
1837 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001838 // else: must have had a \n already
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001839 }
1840 regparse++;
1841 startc = -1;
1842 }
1843 else if (*regparse == 'd'
1844 || *regparse == 'o'
1845 || *regparse == 'x'
1846 || *regparse == 'u'
1847 || *regparse == 'U')
1848 {
1849 startc = coll_get_char();
1850 if (startc == 0)
1851 regc(0x0a);
1852 else
1853 regmbc(startc);
1854 }
1855 else
1856 {
1857 startc = backslash_trans(*regparse++);
1858 regc(startc);
1859 }
1860 }
1861 else if (*regparse == '[')
1862 {
1863 int c_class;
1864 int cu;
1865
1866 c_class = get_char_class(&regparse);
1867 startc = -1;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001868 // Characters assumed to be 8 bits!
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001869 switch (c_class)
1870 {
1871 case CLASS_NONE:
1872 c_class = get_equi_class(&regparse);
1873 if (c_class != 0)
1874 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001875 // produce equivalence class
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001876 reg_equi_class(c_class);
1877 }
1878 else if ((c_class =
1879 get_coll_element(&regparse)) != 0)
1880 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001881 // produce a collating element
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001882 regmbc(c_class);
1883 }
1884 else
1885 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001886 // literal '[', allow [[-x] as a range
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001887 startc = *regparse++;
1888 regc(startc);
1889 }
1890 break;
1891 case CLASS_ALNUM:
1892 for (cu = 1; cu < 128; cu++)
1893 if (isalnum(cu))
1894 regmbc(cu);
1895 break;
1896 case CLASS_ALPHA:
1897 for (cu = 1; cu < 128; cu++)
1898 if (isalpha(cu))
1899 regmbc(cu);
1900 break;
1901 case CLASS_BLANK:
1902 regc(' ');
1903 regc('\t');
1904 break;
1905 case CLASS_CNTRL:
1906 for (cu = 1; cu <= 127; cu++)
1907 if (iscntrl(cu))
1908 regmbc(cu);
1909 break;
1910 case CLASS_DIGIT:
1911 for (cu = 1; cu <= 127; cu++)
1912 if (VIM_ISDIGIT(cu))
1913 regmbc(cu);
1914 break;
1915 case CLASS_GRAPH:
1916 for (cu = 1; cu <= 127; cu++)
1917 if (isgraph(cu))
1918 regmbc(cu);
1919 break;
1920 case CLASS_LOWER:
1921 for (cu = 1; cu <= 255; cu++)
1922 if (MB_ISLOWER(cu) && cu != 170
1923 && cu != 186)
1924 regmbc(cu);
1925 break;
1926 case CLASS_PRINT:
1927 for (cu = 1; cu <= 255; cu++)
1928 if (vim_isprintc(cu))
1929 regmbc(cu);
1930 break;
1931 case CLASS_PUNCT:
1932 for (cu = 1; cu < 128; cu++)
1933 if (ispunct(cu))
1934 regmbc(cu);
1935 break;
1936 case CLASS_SPACE:
1937 for (cu = 9; cu <= 13; cu++)
1938 regc(cu);
1939 regc(' ');
1940 break;
1941 case CLASS_UPPER:
1942 for (cu = 1; cu <= 255; cu++)
1943 if (MB_ISUPPER(cu))
1944 regmbc(cu);
1945 break;
1946 case CLASS_XDIGIT:
1947 for (cu = 1; cu <= 255; cu++)
1948 if (vim_isxdigit(cu))
1949 regmbc(cu);
1950 break;
1951 case CLASS_TAB:
1952 regc('\t');
1953 break;
1954 case CLASS_RETURN:
1955 regc('\r');
1956 break;
1957 case CLASS_BACKSPACE:
1958 regc('\b');
1959 break;
1960 case CLASS_ESCAPE:
1961 regc('\033');
1962 break;
1963 case CLASS_IDENT:
1964 for (cu = 1; cu <= 255; cu++)
1965 if (vim_isIDc(cu))
1966 regmbc(cu);
1967 break;
1968 case CLASS_KEYWORD:
1969 for (cu = 1; cu <= 255; cu++)
1970 if (reg_iswordc(cu))
1971 regmbc(cu);
1972 break;
1973 case CLASS_FNAME:
1974 for (cu = 1; cu <= 255; cu++)
1975 if (vim_isfilec(cu))
1976 regmbc(cu);
1977 break;
1978 }
1979 }
1980 else
1981 {
1982 if (has_mbyte)
1983 {
1984 int len;
1985
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001986 // produce a multibyte character, including any
1987 // following composing characters
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001988 startc = mb_ptr2char(regparse);
1989 len = (*mb_ptr2len)(regparse);
1990 if (enc_utf8 && utf_char2len(startc) != len)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001991 startc = -1; // composing chars
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001992 while (--len >= 0)
1993 regc(*regparse++);
1994 }
1995 else
1996 {
1997 startc = *regparse++;
1998 regc(startc);
1999 }
2000 }
2001 }
2002 regc(NUL);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002003 prevchr_len = 1; // last char was the ']'
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002004 if (*regparse != ']')
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00002005 EMSG_RET_NULL(_(e_too_many_brackets)); // Cannot happen?
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002006 skipchr(); // let's be friends with the lexer again
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002007 *flagp |= HASWIDTH | SIMPLE;
2008 break;
2009 }
2010 else if (reg_strict)
2011 EMSG2_RET_NULL(_(e_missingbracket), reg_magic > MAGIC_OFF);
2012 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002013 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002014
2015 default:
2016 {
2017 int len;
2018
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002019 // A multi-byte character is handled as a separate atom if it's
2020 // before a multi and when it's a composing char.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002021 if (use_multibytecode(c))
2022 {
2023do_multibyte:
2024 ret = regnode(MULTIBYTECODE);
2025 regmbc(c);
2026 *flagp |= HASWIDTH | SIMPLE;
2027 break;
2028 }
2029
2030 ret = regnode(EXACTLY);
2031
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002032 // Append characters as long as:
2033 // - there is no following multi, we then need the character in
2034 // front of it as a single character operand
2035 // - not running into a Magic character
2036 // - "one_exactly" is not set
2037 // But always emit at least one character. Might be a Multi,
2038 // e.g., a "[" without matching "]".
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002039 for (len = 0; c != NUL && (len == 0
2040 || (re_multi_type(peekchr()) == NOT_MULTI
2041 && !one_exactly
2042 && !is_Magic(c))); ++len)
2043 {
2044 c = no_Magic(c);
2045 if (has_mbyte)
2046 {
2047 regmbc(c);
2048 if (enc_utf8)
2049 {
2050 int l;
2051
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002052 // Need to get composing character too.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002053 for (;;)
2054 {
2055 l = utf_ptr2len(regparse);
2056 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
2057 break;
2058 regmbc(utf_ptr2char(regparse));
2059 skipchr();
2060 }
2061 }
2062 }
2063 else
2064 regc(c);
2065 c = getchr();
2066 }
2067 ungetchr();
2068
2069 regc(NUL);
2070 *flagp |= HASWIDTH;
2071 if (len == 1)
2072 *flagp |= SIMPLE;
2073 }
2074 break;
2075 }
2076
2077 return ret;
2078}
2079
2080/*
2081 * Parse something followed by possible [*+=].
2082 *
2083 * Note that the branching code sequences used for = and the general cases
2084 * of * and + are somewhat optimized: they use the same NOTHING node as
2085 * both the endmarker for their branch list and the body of the last branch.
2086 * It might seem that this node could be dispensed with entirely, but the
2087 * endmarker role is not redundant.
2088 */
2089 static char_u *
2090regpiece(int *flagp)
2091{
2092 char_u *ret;
2093 int op;
2094 char_u *next;
2095 int flags;
2096 long minval;
2097 long maxval;
2098
2099 ret = regatom(&flags);
2100 if (ret == NULL)
2101 return NULL;
2102
2103 op = peekchr();
2104 if (re_multi_type(op) == NOT_MULTI)
2105 {
2106 *flagp = flags;
2107 return ret;
2108 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002109 // default flags
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002110 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
2111
2112 skipchr();
2113 switch (op)
2114 {
2115 case Magic('*'):
2116 if (flags & SIMPLE)
2117 reginsert(STAR, ret);
2118 else
2119 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002120 // Emit x* as (x&|), where & means "self".
2121 reginsert(BRANCH, ret); // Either x
2122 regoptail(ret, regnode(BACK)); // and loop
2123 regoptail(ret, ret); // back
2124 regtail(ret, regnode(BRANCH)); // or
2125 regtail(ret, regnode(NOTHING)); // null.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002126 }
2127 break;
2128
2129 case Magic('+'):
2130 if (flags & SIMPLE)
2131 reginsert(PLUS, ret);
2132 else
2133 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002134 // Emit x+ as x(&|), where & means "self".
2135 next = regnode(BRANCH); // Either
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002136 regtail(ret, next);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002137 regtail(regnode(BACK), ret); // loop back
2138 regtail(next, regnode(BRANCH)); // or
2139 regtail(ret, regnode(NOTHING)); // null.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002140 }
2141 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
2142 break;
2143
2144 case Magic('@'):
2145 {
2146 int lop = END;
2147 long nr;
2148
2149 nr = getdecchrs();
2150 switch (no_Magic(getchr()))
2151 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002152 case '=': lop = MATCH; break; // \@=
2153 case '!': lop = NOMATCH; break; // \@!
2154 case '>': lop = SUBPAT; break; // \@>
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002155 case '<': switch (no_Magic(getchr()))
2156 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002157 case '=': lop = BEHIND; break; // \@<=
2158 case '!': lop = NOBEHIND; break; // \@<!
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002159 }
2160 }
2161 if (lop == END)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002162 EMSG2_RET_NULL(_(e_invalid_character_after_str_at),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002163 reg_magic == MAGIC_ALL);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002164 // Look behind must match with behind_pos.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002165 if (lop == BEHIND || lop == NOBEHIND)
2166 {
2167 regtail(ret, regnode(BHPOS));
2168 *flagp |= HASLOOKBH;
2169 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002170 regtail(ret, regnode(END)); // operand ends
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002171 if (lop == BEHIND || lop == NOBEHIND)
2172 {
2173 if (nr < 0)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002174 nr = 0; // no limit is same as zero limit
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002175 reginsert_nr(lop, nr, ret);
2176 }
2177 else
2178 reginsert(lop, ret);
2179 break;
2180 }
2181
2182 case Magic('?'):
2183 case Magic('='):
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002184 // Emit x= as (x|)
2185 reginsert(BRANCH, ret); // Either x
2186 regtail(ret, regnode(BRANCH)); // or
2187 next = regnode(NOTHING); // null.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002188 regtail(ret, next);
2189 regoptail(ret, next);
2190 break;
2191
2192 case Magic('{'):
2193 if (!read_limits(&minval, &maxval))
2194 return NULL;
2195 if (flags & SIMPLE)
2196 {
2197 reginsert(BRACE_SIMPLE, ret);
2198 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
2199 }
2200 else
2201 {
2202 if (num_complex_braces >= 10)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002203 EMSG2_RET_NULL(_(e_too_many_complex_str_curly),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002204 reg_magic == MAGIC_ALL);
2205 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
2206 regoptail(ret, regnode(BACK));
2207 regoptail(ret, ret);
2208 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
2209 ++num_complex_braces;
2210 }
2211 if (minval > 0 && maxval > 0)
2212 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
2213 break;
2214 }
2215 if (re_multi_type(peekchr()) != NOT_MULTI)
2216 {
2217 // Can't have a multi follow a multi.
2218 if (peekchr() == Magic('*'))
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00002219 EMSG2_RET_NULL(_(e_nested_str), reg_magic >= MAGIC_ON);
2220 EMSG3_RET_NULL(_(e_nested_str_chr), reg_magic == MAGIC_ALL,
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002221 no_Magic(peekchr()));
2222 }
2223
2224 return ret;
2225}
2226
2227/*
2228 * Parse one alternative of an | or & operator.
2229 * Implements the concatenation operator.
2230 */
2231 static char_u *
2232regconcat(int *flagp)
2233{
2234 char_u *first = NULL;
2235 char_u *chain = NULL;
2236 char_u *latest;
2237 int flags;
2238 int cont = TRUE;
2239
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002240 *flagp = WORST; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002241
2242 while (cont)
2243 {
2244 switch (peekchr())
2245 {
2246 case NUL:
2247 case Magic('|'):
2248 case Magic('&'):
2249 case Magic(')'):
2250 cont = FALSE;
2251 break;
2252 case Magic('Z'):
2253 regflags |= RF_ICOMBINE;
2254 skipchr_keepstart();
2255 break;
2256 case Magic('c'):
2257 regflags |= RF_ICASE;
2258 skipchr_keepstart();
2259 break;
2260 case Magic('C'):
2261 regflags |= RF_NOICASE;
2262 skipchr_keepstart();
2263 break;
2264 case Magic('v'):
2265 reg_magic = MAGIC_ALL;
2266 skipchr_keepstart();
2267 curchr = -1;
2268 break;
2269 case Magic('m'):
2270 reg_magic = MAGIC_ON;
2271 skipchr_keepstart();
2272 curchr = -1;
2273 break;
2274 case Magic('M'):
2275 reg_magic = MAGIC_OFF;
2276 skipchr_keepstart();
2277 curchr = -1;
2278 break;
2279 case Magic('V'):
2280 reg_magic = MAGIC_NONE;
2281 skipchr_keepstart();
2282 curchr = -1;
2283 break;
2284 default:
2285 latest = regpiece(&flags);
2286 if (latest == NULL || reg_toolong)
2287 return NULL;
2288 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002289 if (chain == NULL) // First piece.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002290 *flagp |= flags & SPSTART;
2291 else
2292 regtail(chain, latest);
2293 chain = latest;
2294 if (first == NULL)
2295 first = latest;
2296 break;
2297 }
2298 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002299 if (first == NULL) // Loop ran zero times.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002300 first = regnode(NOTHING);
2301 return first;
2302}
2303
2304/*
2305 * Parse one alternative of an | operator.
2306 * Implements the & operator.
2307 */
2308 static char_u *
2309regbranch(int *flagp)
2310{
2311 char_u *ret;
2312 char_u *chain = NULL;
2313 char_u *latest;
2314 int flags;
2315
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002316 *flagp = WORST | HASNL; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002317
2318 ret = regnode(BRANCH);
2319 for (;;)
2320 {
2321 latest = regconcat(&flags);
2322 if (latest == NULL)
2323 return NULL;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002324 // If one of the branches has width, the whole thing has. If one of
2325 // the branches anchors at start-of-line, the whole thing does.
2326 // If one of the branches uses look-behind, the whole thing does.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002327 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002328 // If one of the branches doesn't match a line-break, the whole thing
2329 // doesn't.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002330 *flagp &= ~HASNL | (flags & HASNL);
2331 if (chain != NULL)
2332 regtail(chain, latest);
2333 if (peekchr() != Magic('&'))
2334 break;
2335 skipchr();
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002336 regtail(latest, regnode(END)); // operand ends
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002337 if (reg_toolong)
2338 break;
2339 reginsert(MATCH, latest);
2340 chain = latest;
2341 }
2342
2343 return ret;
2344}
2345
2346/*
2347 * Parse regular expression, i.e. main body or parenthesized thing.
2348 *
2349 * Caller must absorb opening parenthesis.
2350 *
2351 * Combining parenthesis handling with the base level of regular expression
2352 * is a trifle forced, but the need to tie the tails of the branches to what
2353 * follows makes it hard to avoid.
2354 */
2355 static char_u *
2356reg(
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002357 int paren, // REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002358 int *flagp)
2359{
2360 char_u *ret;
2361 char_u *br;
2362 char_u *ender;
2363 int parno = 0;
2364 int flags;
2365
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002366 *flagp = HASWIDTH; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002367
2368#ifdef FEAT_SYN_HL
2369 if (paren == REG_ZPAREN)
2370 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002371 // Make a ZOPEN node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002372 if (regnzpar >= NSUBEXP)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002373 EMSG_RET_NULL(_(e_too_many_z));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002374 parno = regnzpar;
2375 regnzpar++;
2376 ret = regnode(ZOPEN + parno);
2377 }
2378 else
2379#endif
2380 if (paren == REG_PAREN)
2381 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002382 // Make a MOPEN node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002383 if (regnpar >= NSUBEXP)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002384 EMSG2_RET_NULL(_(e_too_many_str_open), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002385 parno = regnpar;
2386 ++regnpar;
2387 ret = regnode(MOPEN + parno);
2388 }
2389 else if (paren == REG_NPAREN)
2390 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002391 // Make a NOPEN node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002392 ret = regnode(NOPEN);
2393 }
2394 else
2395 ret = NULL;
2396
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002397 // Pick up the branches, linking them together.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002398 br = regbranch(&flags);
2399 if (br == NULL)
2400 return NULL;
2401 if (ret != NULL)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002402 regtail(ret, br); // [MZ]OPEN -> first.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002403 else
2404 ret = br;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002405 // If one of the branches can be zero-width, the whole thing can.
2406 // If one of the branches has * at start or matches a line-break, the
2407 // whole thing can.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002408 if (!(flags & HASWIDTH))
2409 *flagp &= ~HASWIDTH;
2410 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
2411 while (peekchr() == Magic('|'))
2412 {
2413 skipchr();
2414 br = regbranch(&flags);
2415 if (br == NULL || reg_toolong)
2416 return NULL;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002417 regtail(ret, br); // BRANCH -> BRANCH.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002418 if (!(flags & HASWIDTH))
2419 *flagp &= ~HASWIDTH;
2420 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
2421 }
2422
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002423 // Make a closing node, and hook it on the end.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002424 ender = regnode(
2425#ifdef FEAT_SYN_HL
2426 paren == REG_ZPAREN ? ZCLOSE + parno :
2427#endif
2428 paren == REG_PAREN ? MCLOSE + parno :
2429 paren == REG_NPAREN ? NCLOSE : END);
2430 regtail(ret, ender);
2431
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002432 // Hook the tails of the branches to the closing node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002433 for (br = ret; br != NULL; br = regnext(br))
2434 regoptail(br, ender);
2435
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002436 // Check for proper termination.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002437 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2438 {
2439#ifdef FEAT_SYN_HL
2440 if (paren == REG_ZPAREN)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002441 EMSG_RET_NULL(_(e_unmatched_z));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002442 else
2443#endif
2444 if (paren == REG_NPAREN)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002445 EMSG2_RET_NULL(_(e_unmatched_str_percent_open), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002446 else
Bram Moolenaard8e44472021-07-21 22:20:33 +02002447 EMSG2_RET_NULL(_(e_unmatched_str_open), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002448 }
2449 else if (paren == REG_NOPAREN && peekchr() != NUL)
2450 {
2451 if (curchr == Magic(')'))
Bram Moolenaard8e44472021-07-21 22:20:33 +02002452 EMSG2_RET_NULL(_(e_unmatched_str_close), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002453 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00002454 EMSG_RET_NULL(_(e_trailing_characters)); // "Can't happen".
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002455 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002456 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002457 // Here we set the flag allowing back references to this set of
2458 // parentheses.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002459 if (paren == REG_PAREN)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002460 had_endbrace[parno] = TRUE; // have seen the close paren
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002461 return ret;
2462}
2463
2464/*
2465 * bt_regcomp() - compile a regular expression into internal code for the
2466 * traditional back track matcher.
2467 * Returns the program in allocated space. Returns NULL for an error.
2468 *
2469 * We can't allocate space until we know how big the compiled form will be,
2470 * but we can't compile it (and thus know how big it is) until we've got a
2471 * place to put the code. So we cheat: we compile it twice, once with code
2472 * generation turned off and size counting turned on, and once "for real".
2473 * This also means that we don't allocate space until we are sure that the
2474 * thing really will compile successfully, and we never have to move the
2475 * code and thus invalidate pointers into it. (Note that it has to be in
2476 * one piece because vim_free() must be able to free it all.)
2477 *
2478 * Whether upper/lower case is to be ignored is decided when executing the
2479 * program, it does not matter here.
2480 *
2481 * Beware that the optimization-preparation code in here knows about some
2482 * of the structure of the compiled regexp.
2483 * "re_flags": RE_MAGIC and/or RE_STRING.
2484 */
2485 static regprog_T *
2486bt_regcomp(char_u *expr, int re_flags)
2487{
2488 bt_regprog_T *r;
2489 char_u *scan;
2490 char_u *longest;
2491 int len;
2492 int flags;
2493
2494 if (expr == NULL)
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002495 IEMSG_RET_NULL(_(e_null_argument));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002496
2497 init_class_tab();
2498
2499 // First pass: determine size, legality.
2500 regcomp_start(expr, re_flags);
2501 regcode = JUST_CALC_SIZE;
2502 regc(REGMAGIC);
2503 if (reg(REG_NOPAREN, &flags) == NULL)
2504 return NULL;
2505
2506 // Allocate space.
2507 r = alloc(offsetof(bt_regprog_T, program) + regsize);
2508 if (r == NULL)
2509 return NULL;
2510 r->re_in_use = FALSE;
2511
2512 // Second pass: emit code.
2513 regcomp_start(expr, re_flags);
2514 regcode = r->program;
2515 regc(REGMAGIC);
2516 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
2517 {
2518 vim_free(r);
2519 if (reg_toolong)
Bram Moolenaareaaac012022-01-02 17:00:40 +00002520 EMSG_RET_NULL(_(e_pattern_too_long));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002521 return NULL;
2522 }
2523
2524 // Dig out information for optimizations.
2525 r->regstart = NUL; // Worst-case defaults.
2526 r->reganch = 0;
2527 r->regmust = NULL;
2528 r->regmlen = 0;
2529 r->regflags = regflags;
2530 if (flags & HASNL)
2531 r->regflags |= RF_HASNL;
2532 if (flags & HASLOOKBH)
2533 r->regflags |= RF_LOOKBH;
2534#ifdef FEAT_SYN_HL
2535 // Remember whether this pattern has any \z specials in it.
2536 r->reghasz = re_has_z;
2537#endif
2538 scan = r->program + 1; // First BRANCH.
2539 if (OP(regnext(scan)) == END) // Only one top-level choice.
2540 {
2541 scan = OPERAND(scan);
2542
2543 // Starting-point info.
2544 if (OP(scan) == BOL || OP(scan) == RE_BOF)
2545 {
2546 r->reganch++;
2547 scan = regnext(scan);
2548 }
2549
2550 if (OP(scan) == EXACTLY)
2551 {
2552 if (has_mbyte)
2553 r->regstart = (*mb_ptr2char)(OPERAND(scan));
2554 else
2555 r->regstart = *OPERAND(scan);
2556 }
2557 else if ((OP(scan) == BOW
2558 || OP(scan) == EOW
2559 || OP(scan) == NOTHING
2560 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
2561 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
2562 && OP(regnext(scan)) == EXACTLY)
2563 {
2564 if (has_mbyte)
2565 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
2566 else
2567 r->regstart = *OPERAND(regnext(scan));
2568 }
2569
2570 // If there's something expensive in the r.e., find the longest
2571 // literal string that must appear and make it the regmust. Resolve
2572 // ties in favor of later strings, since the regstart check works
2573 // with the beginning of the r.e. and avoiding duplication
2574 // strengthens checking. Not a strong reason, but sufficient in the
2575 // absence of others.
2576
2577 // When the r.e. starts with BOW, it is faster to look for a regmust
2578 // first. Used a lot for "#" and "*" commands. (Added by mool).
2579 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
2580 && !(flags & HASNL))
2581 {
2582 longest = NULL;
2583 len = 0;
2584 for (; scan != NULL; scan = regnext(scan))
2585 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
2586 {
2587 longest = OPERAND(scan);
2588 len = (int)STRLEN(OPERAND(scan));
2589 }
2590 r->regmust = longest;
2591 r->regmlen = len;
2592 }
2593 }
2594#ifdef BT_REGEXP_DUMP
2595 regdump(expr, r);
2596#endif
2597 r->engine = &bt_regengine;
2598 return (regprog_T *)r;
2599}
2600
2601#if defined(FEAT_SYN_HL) || defined(PROTO)
2602/*
2603 * Check if during the previous call to vim_regcomp the EOL item "$" has been
2604 * found. This is messy, but it works fine.
2605 */
2606 int
2607vim_regcomp_had_eol(void)
2608{
2609 return had_eol;
2610}
2611#endif
2612
2613/*
2614 * Get a number after a backslash that is inside [].
2615 * When nothing is recognized return a backslash.
2616 */
2617 static int
2618coll_get_char(void)
2619{
2620 long nr = -1;
2621
2622 switch (*regparse++)
2623 {
2624 case 'd': nr = getdecchrs(); break;
2625 case 'o': nr = getoctchrs(); break;
2626 case 'x': nr = gethexchrs(2); break;
2627 case 'u': nr = gethexchrs(4); break;
2628 case 'U': nr = gethexchrs(8); break;
2629 }
2630 if (nr < 0 || nr > INT_MAX)
2631 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002632 // If getting the number fails be backwards compatible: the character
2633 // is a backslash.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002634 --regparse;
2635 nr = '\\';
2636 }
2637 return nr;
2638}
2639
2640/*
2641 * Free a compiled regexp program, returned by bt_regcomp().
2642 */
2643 static void
2644bt_regfree(regprog_T *prog)
2645{
2646 vim_free(prog);
2647}
2648
2649#define ADVANCE_REGINPUT() MB_PTR_ADV(rex.input)
2650
2651/*
2652 * The arguments from BRACE_LIMITS are stored here. They are actually local
2653 * to regmatch(), but they are here to reduce the amount of stack space used
2654 * (it can be called recursively many times).
2655 */
2656static long bl_minval;
2657static long bl_maxval;
2658
2659/*
2660 * Save the input line and position in a regsave_T.
2661 */
2662 static void
2663reg_save(regsave_T *save, garray_T *gap)
2664{
2665 if (REG_MULTI)
2666 {
2667 save->rs_u.pos.col = (colnr_T)(rex.input - rex.line);
2668 save->rs_u.pos.lnum = rex.lnum;
2669 }
2670 else
2671 save->rs_u.ptr = rex.input;
2672 save->rs_len = gap->ga_len;
2673}
2674
2675/*
2676 * Restore the input line and position from a regsave_T.
2677 */
2678 static void
2679reg_restore(regsave_T *save, garray_T *gap)
2680{
2681 if (REG_MULTI)
2682 {
2683 if (rex.lnum != save->rs_u.pos.lnum)
2684 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002685 // only call reg_getline() when the line number changed to save
2686 // a bit of time
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002687 rex.lnum = save->rs_u.pos.lnum;
2688 rex.line = reg_getline(rex.lnum);
2689 }
2690 rex.input = rex.line + save->rs_u.pos.col;
2691 }
2692 else
2693 rex.input = save->rs_u.ptr;
2694 gap->ga_len = save->rs_len;
2695}
2696
2697/*
2698 * Return TRUE if current position is equal to saved position.
2699 */
2700 static int
2701reg_save_equal(regsave_T *save)
2702{
2703 if (REG_MULTI)
2704 return rex.lnum == save->rs_u.pos.lnum
2705 && rex.input == rex.line + save->rs_u.pos.col;
2706 return rex.input == save->rs_u.ptr;
2707}
2708
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002709// Save the sub-expressions before attempting a match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002710#define save_se(savep, posp, pp) \
2711 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
2712
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002713// After a failed match restore the sub-expressions.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002714#define restore_se(savep, posp, pp) { \
2715 if (REG_MULTI) \
2716 *(posp) = (savep)->se_u.pos; \
2717 else \
2718 *(pp) = (savep)->se_u.ptr; }
2719
2720/*
2721 * Tentatively set the sub-expression start to the current position (after
2722 * calling regmatch() they will have changed). Need to save the existing
2723 * values for when there is no match.
2724 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
2725 * depending on REG_MULTI.
2726 */
2727 static void
2728save_se_multi(save_se_T *savep, lpos_T *posp)
2729{
2730 savep->se_u.pos = *posp;
2731 posp->lnum = rex.lnum;
2732 posp->col = (colnr_T)(rex.input - rex.line);
2733}
2734
2735 static void
2736save_se_one(save_se_T *savep, char_u **pp)
2737{
2738 savep->se_u.ptr = *pp;
2739 *pp = rex.input;
2740}
2741
2742/*
2743 * regrepeat - repeatedly match something simple, return how many.
2744 * Advances rex.input (and rex.lnum) to just after the matched chars.
2745 */
2746 static int
2747regrepeat(
2748 char_u *p,
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002749 long maxcount) // maximum number of matches allowed
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002750{
2751 long count = 0;
2752 char_u *scan;
2753 char_u *opnd;
2754 int mask;
2755 int testval = 0;
2756
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002757 scan = rex.input; // Make local copy of rex.input for speed.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002758 opnd = OPERAND(p);
2759 switch (OP(p))
2760 {
2761 case ANY:
2762 case ANY + ADD_NL:
2763 while (count < maxcount)
2764 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002765 // Matching anything means we continue until end-of-line (or
2766 // end-of-file for ANY + ADD_NL), only limited by maxcount.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002767 while (*scan != NUL && count < maxcount)
2768 {
2769 ++count;
2770 MB_PTR_ADV(scan);
2771 }
2772 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2773 || rex.reg_line_lbr || count == maxcount)
2774 break;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002775 ++count; // count the line-break
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002776 reg_nextline();
2777 scan = rex.input;
2778 if (got_int)
2779 break;
2780 }
2781 break;
2782
2783 case IDENT:
2784 case IDENT + ADD_NL:
2785 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002786 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002787 case SIDENT:
2788 case SIDENT + ADD_NL:
2789 while (count < maxcount)
2790 {
2791 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
2792 {
2793 MB_PTR_ADV(scan);
2794 }
2795 else if (*scan == NUL)
2796 {
2797 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2798 || rex.reg_line_lbr)
2799 break;
2800 reg_nextline();
2801 scan = rex.input;
2802 if (got_int)
2803 break;
2804 }
2805 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2806 ++scan;
2807 else
2808 break;
2809 ++count;
2810 }
2811 break;
2812
2813 case KWORD:
2814 case KWORD + ADD_NL:
2815 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002816 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002817 case SKWORD:
2818 case SKWORD + ADD_NL:
2819 while (count < maxcount)
2820 {
2821 if (vim_iswordp_buf(scan, rex.reg_buf)
2822 && (testval || !VIM_ISDIGIT(*scan)))
2823 {
2824 MB_PTR_ADV(scan);
2825 }
2826 else if (*scan == NUL)
2827 {
2828 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2829 || rex.reg_line_lbr)
2830 break;
2831 reg_nextline();
2832 scan = rex.input;
2833 if (got_int)
2834 break;
2835 }
2836 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2837 ++scan;
2838 else
2839 break;
2840 ++count;
2841 }
2842 break;
2843
2844 case FNAME:
2845 case FNAME + ADD_NL:
2846 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002847 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002848 case SFNAME:
2849 case SFNAME + ADD_NL:
2850 while (count < maxcount)
2851 {
2852 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
2853 {
2854 MB_PTR_ADV(scan);
2855 }
2856 else if (*scan == NUL)
2857 {
2858 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2859 || rex.reg_line_lbr)
2860 break;
2861 reg_nextline();
2862 scan = rex.input;
2863 if (got_int)
2864 break;
2865 }
2866 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2867 ++scan;
2868 else
2869 break;
2870 ++count;
2871 }
2872 break;
2873
2874 case PRINT:
2875 case PRINT + ADD_NL:
2876 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002877 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002878 case SPRINT:
2879 case SPRINT + ADD_NL:
2880 while (count < maxcount)
2881 {
2882 if (*scan == NUL)
2883 {
2884 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2885 || rex.reg_line_lbr)
2886 break;
2887 reg_nextline();
2888 scan = rex.input;
2889 if (got_int)
2890 break;
2891 }
2892 else if (vim_isprintc(PTR2CHAR(scan)) == 1
2893 && (testval || !VIM_ISDIGIT(*scan)))
2894 {
2895 MB_PTR_ADV(scan);
2896 }
2897 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2898 ++scan;
2899 else
2900 break;
2901 ++count;
2902 }
2903 break;
2904
2905 case WHITE:
2906 case WHITE + ADD_NL:
2907 testval = mask = RI_WHITE;
2908do_class:
2909 while (count < maxcount)
2910 {
2911 int l;
2912
2913 if (*scan == NUL)
2914 {
2915 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2916 || rex.reg_line_lbr)
2917 break;
2918 reg_nextline();
2919 scan = rex.input;
2920 if (got_int)
2921 break;
2922 }
2923 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
2924 {
2925 if (testval != 0)
2926 break;
2927 scan += l;
2928 }
2929 else if ((class_tab[*scan] & mask) == testval)
2930 ++scan;
2931 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2932 ++scan;
2933 else
2934 break;
2935 ++count;
2936 }
2937 break;
2938
2939 case NWHITE:
2940 case NWHITE + ADD_NL:
2941 mask = RI_WHITE;
2942 goto do_class;
2943 case DIGIT:
2944 case DIGIT + ADD_NL:
2945 testval = mask = RI_DIGIT;
2946 goto do_class;
2947 case NDIGIT:
2948 case NDIGIT + ADD_NL:
2949 mask = RI_DIGIT;
2950 goto do_class;
2951 case HEX:
2952 case HEX + ADD_NL:
2953 testval = mask = RI_HEX;
2954 goto do_class;
2955 case NHEX:
2956 case NHEX + ADD_NL:
2957 mask = RI_HEX;
2958 goto do_class;
2959 case OCTAL:
2960 case OCTAL + ADD_NL:
2961 testval = mask = RI_OCTAL;
2962 goto do_class;
2963 case NOCTAL:
2964 case NOCTAL + ADD_NL:
2965 mask = RI_OCTAL;
2966 goto do_class;
2967 case WORD:
2968 case WORD + ADD_NL:
2969 testval = mask = RI_WORD;
2970 goto do_class;
2971 case NWORD:
2972 case NWORD + ADD_NL:
2973 mask = RI_WORD;
2974 goto do_class;
2975 case HEAD:
2976 case HEAD + ADD_NL:
2977 testval = mask = RI_HEAD;
2978 goto do_class;
2979 case NHEAD:
2980 case NHEAD + ADD_NL:
2981 mask = RI_HEAD;
2982 goto do_class;
2983 case ALPHA:
2984 case ALPHA + ADD_NL:
2985 testval = mask = RI_ALPHA;
2986 goto do_class;
2987 case NALPHA:
2988 case NALPHA + ADD_NL:
2989 mask = RI_ALPHA;
2990 goto do_class;
2991 case LOWER:
2992 case LOWER + ADD_NL:
2993 testval = mask = RI_LOWER;
2994 goto do_class;
2995 case NLOWER:
2996 case NLOWER + ADD_NL:
2997 mask = RI_LOWER;
2998 goto do_class;
2999 case UPPER:
3000 case UPPER + ADD_NL:
3001 testval = mask = RI_UPPER;
3002 goto do_class;
3003 case NUPPER:
3004 case NUPPER + ADD_NL:
3005 mask = RI_UPPER;
3006 goto do_class;
3007
3008 case EXACTLY:
3009 {
3010 int cu, cl;
3011
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003012 // This doesn't do a multi-byte character, because a MULTIBYTECODE
3013 // would have been used for it. It does handle single-byte
3014 // characters, such as latin1.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003015 if (rex.reg_ic)
3016 {
3017 cu = MB_TOUPPER(*opnd);
3018 cl = MB_TOLOWER(*opnd);
3019 while (count < maxcount && (*scan == cu || *scan == cl))
3020 {
3021 count++;
3022 scan++;
3023 }
3024 }
3025 else
3026 {
3027 cu = *opnd;
3028 while (count < maxcount && *scan == cu)
3029 {
3030 count++;
3031 scan++;
3032 }
3033 }
3034 break;
3035 }
3036
3037 case MULTIBYTECODE:
3038 {
3039 int i, len, cf = 0;
3040
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003041 // Safety check (just in case 'encoding' was changed since
3042 // compiling the program).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003043 if ((len = (*mb_ptr2len)(opnd)) > 1)
3044 {
3045 if (rex.reg_ic && enc_utf8)
3046 cf = utf_fold(utf_ptr2char(opnd));
3047 while (count < maxcount && (*mb_ptr2len)(scan) >= len)
3048 {
3049 for (i = 0; i < len; ++i)
3050 if (opnd[i] != scan[i])
3051 break;
3052 if (i < len && (!rex.reg_ic || !enc_utf8
3053 || utf_fold(utf_ptr2char(scan)) != cf))
3054 break;
3055 scan += len;
3056 ++count;
3057 }
3058 }
3059 }
3060 break;
3061
3062 case ANYOF:
3063 case ANYOF + ADD_NL:
3064 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003065 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003066
3067 case ANYBUT:
3068 case ANYBUT + ADD_NL:
3069 while (count < maxcount)
3070 {
3071 int len;
3072
3073 if (*scan == NUL)
3074 {
3075 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
3076 || rex.reg_line_lbr)
3077 break;
3078 reg_nextline();
3079 scan = rex.input;
3080 if (got_int)
3081 break;
3082 }
3083 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
3084 ++scan;
3085 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
3086 {
3087 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
3088 break;
3089 scan += len;
3090 }
3091 else
3092 {
3093 if ((cstrchr(opnd, *scan) == NULL) == testval)
3094 break;
3095 ++scan;
3096 }
3097 ++count;
3098 }
3099 break;
3100
3101 case NEWL:
3102 while (count < maxcount
3103 && ((*scan == NUL && rex.lnum <= rex.reg_maxline
3104 && !rex.reg_line_lbr && REG_MULTI)
3105 || (*scan == '\n' && rex.reg_line_lbr)))
3106 {
3107 count++;
3108 if (rex.reg_line_lbr)
3109 ADVANCE_REGINPUT();
3110 else
3111 reg_nextline();
3112 scan = rex.input;
3113 if (got_int)
3114 break;
3115 }
3116 break;
3117
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003118 default: // Oh dear. Called inappropriately.
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003119 iemsg(_(e_corrupted_regexp_program));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003120#ifdef DEBUG
3121 printf("Called regrepeat with op code %d\n", OP(p));
3122#endif
3123 break;
3124 }
3125
3126 rex.input = scan;
3127
3128 return (int)count;
3129}
3130
3131/*
3132 * Push an item onto the regstack.
3133 * Returns pointer to new item. Returns NULL when out of memory.
3134 */
3135 static regitem_T *
3136regstack_push(regstate_T state, char_u *scan)
3137{
3138 regitem_T *rp;
3139
3140 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
3141 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003142 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003143 return NULL;
3144 }
3145 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
3146 return NULL;
3147
3148 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
3149 rp->rs_state = state;
3150 rp->rs_scan = scan;
3151
3152 regstack.ga_len += sizeof(regitem_T);
3153 return rp;
3154}
3155
3156/*
3157 * Pop an item from the regstack.
3158 */
3159 static void
3160regstack_pop(char_u **scan)
3161{
3162 regitem_T *rp;
3163
3164 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
3165 *scan = rp->rs_scan;
3166
3167 regstack.ga_len -= sizeof(regitem_T);
3168}
3169
3170/*
3171 * Save the current subexpr to "bp", so that they can be restored
3172 * later by restore_subexpr().
3173 */
3174 static void
3175save_subexpr(regbehind_T *bp)
3176{
3177 int i;
3178
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003179 // When "rex.need_clear_subexpr" is set we don't need to save the values,
3180 // only remember that this flag needs to be set again when restoring.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003181 bp->save_need_clear_subexpr = rex.need_clear_subexpr;
3182 if (!rex.need_clear_subexpr)
3183 {
3184 for (i = 0; i < NSUBEXP; ++i)
3185 {
3186 if (REG_MULTI)
3187 {
3188 bp->save_start[i].se_u.pos = rex.reg_startpos[i];
3189 bp->save_end[i].se_u.pos = rex.reg_endpos[i];
3190 }
3191 else
3192 {
3193 bp->save_start[i].se_u.ptr = rex.reg_startp[i];
3194 bp->save_end[i].se_u.ptr = rex.reg_endp[i];
3195 }
3196 }
3197 }
3198}
3199
3200/*
3201 * Restore the subexpr from "bp".
3202 */
3203 static void
3204restore_subexpr(regbehind_T *bp)
3205{
3206 int i;
3207
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003208 // Only need to restore saved values when they are not to be cleared.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003209 rex.need_clear_subexpr = bp->save_need_clear_subexpr;
3210 if (!rex.need_clear_subexpr)
3211 {
3212 for (i = 0; i < NSUBEXP; ++i)
3213 {
3214 if (REG_MULTI)
3215 {
3216 rex.reg_startpos[i] = bp->save_start[i].se_u.pos;
3217 rex.reg_endpos[i] = bp->save_end[i].se_u.pos;
3218 }
3219 else
3220 {
3221 rex.reg_startp[i] = bp->save_start[i].se_u.ptr;
3222 rex.reg_endp[i] = bp->save_end[i].se_u.ptr;
3223 }
3224 }
3225 }
3226}
3227
3228/*
3229 * regmatch - main matching routine
3230 *
3231 * Conceptually the strategy is simple: Check to see whether the current node
3232 * matches, push an item onto the regstack and loop to see whether the rest
3233 * matches, and then act accordingly. In practice we make some effort to
3234 * avoid using the regstack, in particular by going through "ordinary" nodes
3235 * (that don't need to know whether the rest of the match failed) by a nested
3236 * loop.
3237 *
3238 * Returns TRUE when there is a match. Leaves rex.input and rex.lnum just after
3239 * the last matched character.
3240 * Returns FALSE when there is no match. Leaves rex.input and rex.lnum in an
3241 * undefined state!
3242 */
3243 static int
3244regmatch(
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003245 char_u *scan, // Current node.
3246 proftime_T *tm UNUSED, // timeout limit or NULL
3247 int *timed_out UNUSED) // flag set on timeout or NULL
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003248{
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003249 char_u *next; // Next node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003250 int op;
3251 int c;
3252 regitem_T *rp;
3253 int no;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003254 int status; // one of the RA_ values:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003255#ifdef FEAT_RELTIME
3256 int tm_count = 0;
3257#endif
3258
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003259 // Make "regstack" and "backpos" empty. They are allocated and freed in
3260 // bt_regexec_both() to reduce malloc()/free() calls.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003261 regstack.ga_len = 0;
3262 backpos.ga_len = 0;
3263
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003264 // Repeat until "regstack" is empty.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003265 for (;;)
3266 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003267 // Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
3268 // Allow interrupting them with CTRL-C.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003269 fast_breakcheck();
3270
3271#ifdef DEBUG
3272 if (scan != NULL && regnarrate)
3273 {
3274 mch_errmsg((char *)regprop(scan));
3275 mch_errmsg("(\n");
3276 }
3277#endif
3278
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003279 // Repeat for items that can be matched sequentially, without using the
3280 // regstack.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003281 for (;;)
3282 {
3283 if (got_int || scan == NULL)
3284 {
3285 status = RA_FAIL;
3286 break;
3287 }
3288#ifdef FEAT_RELTIME
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003289 // Check for timeout once in a 100 times to avoid overhead.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003290 if (tm != NULL && ++tm_count == 100)
3291 {
3292 tm_count = 0;
3293 if (profile_passed_limit(tm))
3294 {
3295 if (timed_out != NULL)
3296 *timed_out = TRUE;
3297 status = RA_FAIL;
3298 break;
3299 }
3300 }
3301#endif
3302 status = RA_CONT;
3303
3304#ifdef DEBUG
3305 if (regnarrate)
3306 {
3307 mch_errmsg((char *)regprop(scan));
3308 mch_errmsg("...\n");
3309# ifdef FEAT_SYN_HL
3310 if (re_extmatch_in != NULL)
3311 {
3312 int i;
3313
3314 mch_errmsg(_("External submatches:\n"));
3315 for (i = 0; i < NSUBEXP; i++)
3316 {
3317 mch_errmsg(" \"");
3318 if (re_extmatch_in->matches[i] != NULL)
3319 mch_errmsg((char *)re_extmatch_in->matches[i]);
3320 mch_errmsg("\"\n");
3321 }
3322 }
3323# endif
3324 }
3325#endif
3326 next = regnext(scan);
3327
3328 op = OP(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003329 // Check for character class with NL added.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003330 if (!rex.reg_line_lbr && WITH_NL(op) && REG_MULTI
3331 && *rex.input == NUL && rex.lnum <= rex.reg_maxline)
3332 {
3333 reg_nextline();
3334 }
3335 else if (rex.reg_line_lbr && WITH_NL(op) && *rex.input == '\n')
3336 {
3337 ADVANCE_REGINPUT();
3338 }
3339 else
3340 {
3341 if (WITH_NL(op))
3342 op -= ADD_NL;
3343 if (has_mbyte)
3344 c = (*mb_ptr2char)(rex.input);
3345 else
3346 c = *rex.input;
3347 switch (op)
3348 {
3349 case BOL:
3350 if (rex.input != rex.line)
3351 status = RA_NOMATCH;
3352 break;
3353
3354 case EOL:
3355 if (c != NUL)
3356 status = RA_NOMATCH;
3357 break;
3358
3359 case RE_BOF:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003360 // We're not at the beginning of the file when below the first
3361 // line where we started, not at the start of the line or we
3362 // didn't start at the first line of the buffer.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003363 if (rex.lnum != 0 || rex.input != rex.line
3364 || (REG_MULTI && rex.reg_firstlnum > 1))
3365 status = RA_NOMATCH;
3366 break;
3367
3368 case RE_EOF:
3369 if (rex.lnum != rex.reg_maxline || c != NUL)
3370 status = RA_NOMATCH;
3371 break;
3372
3373 case CURSOR:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003374 // Check if the buffer is in a window and compare the
3375 // rex.reg_win->w_cursor position to the match position.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003376 if (rex.reg_win == NULL
3377 || (rex.lnum + rex.reg_firstlnum
3378 != rex.reg_win->w_cursor.lnum)
3379 || ((colnr_T)(rex.input - rex.line)
3380 != rex.reg_win->w_cursor.col))
3381 status = RA_NOMATCH;
3382 break;
3383
3384 case RE_MARK:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003385 // Compare the mark position to the match position.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003386 {
3387 int mark = OPERAND(scan)[0];
3388 int cmp = OPERAND(scan)[1];
3389 pos_T *pos;
3390
3391 pos = getmark_buf(rex.reg_buf, mark, FALSE);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003392 if (pos == NULL // mark doesn't exist
Bram Moolenaar872bee52021-05-24 22:56:15 +02003393 || pos->lnum <= 0) // mark isn't set in reg_buf
3394 {
3395 status = RA_NOMATCH;
3396 }
3397 else
3398 {
3399 colnr_T pos_col = pos->lnum == rex.lnum + rex.reg_firstlnum
3400 && pos->col == MAXCOL
3401 ? (colnr_T)STRLEN(reg_getline(
3402 pos->lnum - rex.reg_firstlnum))
3403 : pos->col;
3404
3405 if ((pos->lnum == rex.lnum + rex.reg_firstlnum
3406 ? (pos_col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003407 ? (cmp == '<' || cmp == '>')
Bram Moolenaar872bee52021-05-24 22:56:15 +02003408 : (pos_col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003409 ? cmp != '>'
3410 : cmp != '<'))
3411 : (pos->lnum < rex.lnum + rex.reg_firstlnum
3412 ? cmp != '>'
3413 : cmp != '<')))
3414 status = RA_NOMATCH;
Bram Moolenaar872bee52021-05-24 22:56:15 +02003415 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003416 }
3417 break;
3418
3419 case RE_VISUAL:
3420 if (!reg_match_visual())
3421 status = RA_NOMATCH;
3422 break;
3423
3424 case RE_LNUM:
3425 if (!REG_MULTI || !re_num_cmp((long_u)(rex.lnum + rex.reg_firstlnum),
3426 scan))
3427 status = RA_NOMATCH;
3428 break;
3429
3430 case RE_COL:
3431 if (!re_num_cmp((long_u)(rex.input - rex.line) + 1, scan))
3432 status = RA_NOMATCH;
3433 break;
3434
3435 case RE_VCOL:
3436 if (!re_num_cmp((long_u)win_linetabsize(
3437 rex.reg_win == NULL ? curwin : rex.reg_win,
3438 rex.line, (colnr_T)(rex.input - rex.line)) + 1, scan))
3439 status = RA_NOMATCH;
3440 break;
3441
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003442 case BOW: // \<word; rex.input points to w
3443 if (c == NUL) // Can't match at end of line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003444 status = RA_NOMATCH;
3445 else if (has_mbyte)
3446 {
3447 int this_class;
3448
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003449 // Get class of current and previous char (if it exists).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003450 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
3451 if (this_class <= 1)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003452 status = RA_NOMATCH; // not on a word at all
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003453 else if (reg_prev_class() == this_class)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003454 status = RA_NOMATCH; // previous char is in same word
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003455 }
3456 else
3457 {
3458 if (!vim_iswordc_buf(c, rex.reg_buf) || (rex.input > rex.line
3459 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
3460 status = RA_NOMATCH;
3461 }
3462 break;
3463
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003464 case EOW: // word\>; rex.input points after d
3465 if (rex.input == rex.line) // Can't match at start of line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003466 status = RA_NOMATCH;
3467 else if (has_mbyte)
3468 {
3469 int this_class, prev_class;
3470
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003471 // Get class of current and previous char (if it exists).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003472 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
3473 prev_class = reg_prev_class();
3474 if (this_class == prev_class
3475 || prev_class == 0 || prev_class == 1)
3476 status = RA_NOMATCH;
3477 }
3478 else
3479 {
3480 if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
3481 || (rex.input[0] != NUL
3482 && vim_iswordc_buf(c, rex.reg_buf)))
3483 status = RA_NOMATCH;
3484 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003485 break; // Matched with EOW
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003486
3487 case ANY:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003488 // ANY does not match new lines.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003489 if (c == NUL)
3490 status = RA_NOMATCH;
3491 else
3492 ADVANCE_REGINPUT();
3493 break;
3494
3495 case IDENT:
3496 if (!vim_isIDc(c))
3497 status = RA_NOMATCH;
3498 else
3499 ADVANCE_REGINPUT();
3500 break;
3501
3502 case SIDENT:
3503 if (VIM_ISDIGIT(*rex.input) || !vim_isIDc(c))
3504 status = RA_NOMATCH;
3505 else
3506 ADVANCE_REGINPUT();
3507 break;
3508
3509 case KWORD:
3510 if (!vim_iswordp_buf(rex.input, rex.reg_buf))
3511 status = RA_NOMATCH;
3512 else
3513 ADVANCE_REGINPUT();
3514 break;
3515
3516 case SKWORD:
3517 if (VIM_ISDIGIT(*rex.input)
3518 || !vim_iswordp_buf(rex.input, rex.reg_buf))
3519 status = RA_NOMATCH;
3520 else
3521 ADVANCE_REGINPUT();
3522 break;
3523
3524 case FNAME:
3525 if (!vim_isfilec(c))
3526 status = RA_NOMATCH;
3527 else
3528 ADVANCE_REGINPUT();
3529 break;
3530
3531 case SFNAME:
3532 if (VIM_ISDIGIT(*rex.input) || !vim_isfilec(c))
3533 status = RA_NOMATCH;
3534 else
3535 ADVANCE_REGINPUT();
3536 break;
3537
3538 case PRINT:
3539 if (!vim_isprintc(PTR2CHAR(rex.input)))
3540 status = RA_NOMATCH;
3541 else
3542 ADVANCE_REGINPUT();
3543 break;
3544
3545 case SPRINT:
3546 if (VIM_ISDIGIT(*rex.input) || !vim_isprintc(PTR2CHAR(rex.input)))
3547 status = RA_NOMATCH;
3548 else
3549 ADVANCE_REGINPUT();
3550 break;
3551
3552 case WHITE:
3553 if (!VIM_ISWHITE(c))
3554 status = RA_NOMATCH;
3555 else
3556 ADVANCE_REGINPUT();
3557 break;
3558
3559 case NWHITE:
3560 if (c == NUL || VIM_ISWHITE(c))
3561 status = RA_NOMATCH;
3562 else
3563 ADVANCE_REGINPUT();
3564 break;
3565
3566 case DIGIT:
3567 if (!ri_digit(c))
3568 status = RA_NOMATCH;
3569 else
3570 ADVANCE_REGINPUT();
3571 break;
3572
3573 case NDIGIT:
3574 if (c == NUL || ri_digit(c))
3575 status = RA_NOMATCH;
3576 else
3577 ADVANCE_REGINPUT();
3578 break;
3579
3580 case HEX:
3581 if (!ri_hex(c))
3582 status = RA_NOMATCH;
3583 else
3584 ADVANCE_REGINPUT();
3585 break;
3586
3587 case NHEX:
3588 if (c == NUL || ri_hex(c))
3589 status = RA_NOMATCH;
3590 else
3591 ADVANCE_REGINPUT();
3592 break;
3593
3594 case OCTAL:
3595 if (!ri_octal(c))
3596 status = RA_NOMATCH;
3597 else
3598 ADVANCE_REGINPUT();
3599 break;
3600
3601 case NOCTAL:
3602 if (c == NUL || ri_octal(c))
3603 status = RA_NOMATCH;
3604 else
3605 ADVANCE_REGINPUT();
3606 break;
3607
3608 case WORD:
3609 if (!ri_word(c))
3610 status = RA_NOMATCH;
3611 else
3612 ADVANCE_REGINPUT();
3613 break;
3614
3615 case NWORD:
3616 if (c == NUL || ri_word(c))
3617 status = RA_NOMATCH;
3618 else
3619 ADVANCE_REGINPUT();
3620 break;
3621
3622 case HEAD:
3623 if (!ri_head(c))
3624 status = RA_NOMATCH;
3625 else
3626 ADVANCE_REGINPUT();
3627 break;
3628
3629 case NHEAD:
3630 if (c == NUL || ri_head(c))
3631 status = RA_NOMATCH;
3632 else
3633 ADVANCE_REGINPUT();
3634 break;
3635
3636 case ALPHA:
3637 if (!ri_alpha(c))
3638 status = RA_NOMATCH;
3639 else
3640 ADVANCE_REGINPUT();
3641 break;
3642
3643 case NALPHA:
3644 if (c == NUL || ri_alpha(c))
3645 status = RA_NOMATCH;
3646 else
3647 ADVANCE_REGINPUT();
3648 break;
3649
3650 case LOWER:
3651 if (!ri_lower(c))
3652 status = RA_NOMATCH;
3653 else
3654 ADVANCE_REGINPUT();
3655 break;
3656
3657 case NLOWER:
3658 if (c == NUL || ri_lower(c))
3659 status = RA_NOMATCH;
3660 else
3661 ADVANCE_REGINPUT();
3662 break;
3663
3664 case UPPER:
3665 if (!ri_upper(c))
3666 status = RA_NOMATCH;
3667 else
3668 ADVANCE_REGINPUT();
3669 break;
3670
3671 case NUPPER:
3672 if (c == NUL || ri_upper(c))
3673 status = RA_NOMATCH;
3674 else
3675 ADVANCE_REGINPUT();
3676 break;
3677
3678 case EXACTLY:
3679 {
3680 int len;
3681 char_u *opnd;
3682
3683 opnd = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003684 // Inline the first byte, for speed.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003685 if (*opnd != *rex.input
3686 && (!rex.reg_ic
3687 || (!enc_utf8
3688 && MB_TOLOWER(*opnd) != MB_TOLOWER(*rex.input))))
3689 status = RA_NOMATCH;
3690 else if (*opnd == NUL)
3691 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003692 // match empty string always works; happens when "~" is
3693 // empty.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003694 }
3695 else
3696 {
3697 if (opnd[1] == NUL && !(enc_utf8 && rex.reg_ic))
3698 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003699 len = 1; // matched a single byte above
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003700 }
3701 else
3702 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003703 // Need to match first byte again for multi-byte.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003704 len = (int)STRLEN(opnd);
3705 if (cstrncmp(opnd, rex.input, &len) != 0)
3706 status = RA_NOMATCH;
3707 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003708 // Check for following composing character, unless %C
3709 // follows (skips over all composing chars).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003710 if (status != RA_NOMATCH
3711 && enc_utf8
3712 && UTF_COMPOSINGLIKE(rex.input, rex.input + len)
3713 && !rex.reg_icombine
3714 && OP(next) != RE_COMPOSING)
3715 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003716 // raaron: This code makes a composing character get
3717 // ignored, which is the correct behavior (sometimes)
3718 // for voweled Hebrew texts.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003719 status = RA_NOMATCH;
3720 }
3721 if (status != RA_NOMATCH)
3722 rex.input += len;
3723 }
3724 }
3725 break;
3726
3727 case ANYOF:
3728 case ANYBUT:
3729 if (c == NUL)
3730 status = RA_NOMATCH;
3731 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
3732 status = RA_NOMATCH;
3733 else
3734 ADVANCE_REGINPUT();
3735 break;
3736
3737 case MULTIBYTECODE:
3738 if (has_mbyte)
3739 {
3740 int i, len;
3741 char_u *opnd;
3742 int opndc = 0, inpc;
3743
3744 opnd = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003745 // Safety check (just in case 'encoding' was changed since
3746 // compiling the program).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003747 if ((len = (*mb_ptr2len)(opnd)) < 2)
3748 {
3749 status = RA_NOMATCH;
3750 break;
3751 }
3752 if (enc_utf8)
3753 opndc = utf_ptr2char(opnd);
3754 if (enc_utf8 && utf_iscomposing(opndc))
3755 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003756 // When only a composing char is given match at any
3757 // position where that composing char appears.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003758 status = RA_NOMATCH;
3759 for (i = 0; rex.input[i] != NUL;
3760 i += utf_ptr2len(rex.input + i))
3761 {
3762 inpc = utf_ptr2char(rex.input + i);
3763 if (!utf_iscomposing(inpc))
3764 {
3765 if (i > 0)
3766 break;
3767 }
3768 else if (opndc == inpc)
3769 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003770 // Include all following composing chars.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003771 len = i + utfc_ptr2len(rex.input + i);
3772 status = RA_MATCH;
3773 break;
3774 }
3775 }
3776 }
3777 else
3778 for (i = 0; i < len; ++i)
3779 if (opnd[i] != rex.input[i])
3780 {
3781 status = RA_NOMATCH;
3782 break;
3783 }
3784 rex.input += len;
3785 }
3786 else
3787 status = RA_NOMATCH;
3788 break;
3789 case RE_COMPOSING:
3790 if (enc_utf8)
3791 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003792 // Skip composing characters.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003793 while (utf_iscomposing(utf_ptr2char(rex.input)))
3794 MB_CPTR_ADV(rex.input);
3795 }
3796 break;
3797
3798 case NOTHING:
3799 break;
3800
3801 case BACK:
3802 {
3803 int i;
3804 backpos_T *bp;
3805
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003806 // When we run into BACK we need to check if we don't keep
3807 // looping without matching any input. The second and later
3808 // times a BACK is encountered it fails if the input is still
3809 // at the same position as the previous time.
3810 // The positions are stored in "backpos" and found by the
3811 // current value of "scan", the position in the RE program.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003812 bp = (backpos_T *)backpos.ga_data;
3813 for (i = 0; i < backpos.ga_len; ++i)
3814 if (bp[i].bp_scan == scan)
3815 break;
3816 if (i == backpos.ga_len)
3817 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003818 // First time at this BACK, make room to store the pos.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003819 if (ga_grow(&backpos, 1) == FAIL)
3820 status = RA_FAIL;
3821 else
3822 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003823 // get "ga_data" again, it may have changed
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003824 bp = (backpos_T *)backpos.ga_data;
3825 bp[i].bp_scan = scan;
3826 ++backpos.ga_len;
3827 }
3828 }
3829 else if (reg_save_equal(&bp[i].bp_pos))
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003830 // Still at same position as last time, fail.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003831 status = RA_NOMATCH;
3832
3833 if (status != RA_FAIL && status != RA_NOMATCH)
3834 reg_save(&bp[i].bp_pos, &backpos);
3835 }
3836 break;
3837
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003838 case MOPEN + 0: // Match start: \zs
3839 case MOPEN + 1: // \(
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003840 case MOPEN + 2:
3841 case MOPEN + 3:
3842 case MOPEN + 4:
3843 case MOPEN + 5:
3844 case MOPEN + 6:
3845 case MOPEN + 7:
3846 case MOPEN + 8:
3847 case MOPEN + 9:
3848 {
3849 no = op - MOPEN;
3850 cleanup_subexpr();
3851 rp = regstack_push(RS_MOPEN, scan);
3852 if (rp == NULL)
3853 status = RA_FAIL;
3854 else
3855 {
3856 rp->rs_no = no;
3857 save_se(&rp->rs_un.sesave, &rex.reg_startpos[no],
3858 &rex.reg_startp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003859 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003860 }
3861 }
3862 break;
3863
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003864 case NOPEN: // \%(
3865 case NCLOSE: // \) after \%(
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003866 if (regstack_push(RS_NOPEN, scan) == NULL)
3867 status = RA_FAIL;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003868 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003869 break;
3870
3871#ifdef FEAT_SYN_HL
3872 case ZOPEN + 1:
3873 case ZOPEN + 2:
3874 case ZOPEN + 3:
3875 case ZOPEN + 4:
3876 case ZOPEN + 5:
3877 case ZOPEN + 6:
3878 case ZOPEN + 7:
3879 case ZOPEN + 8:
3880 case ZOPEN + 9:
3881 {
3882 no = op - ZOPEN;
3883 cleanup_zsubexpr();
3884 rp = regstack_push(RS_ZOPEN, scan);
3885 if (rp == NULL)
3886 status = RA_FAIL;
3887 else
3888 {
3889 rp->rs_no = no;
3890 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
3891 &reg_startzp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003892 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003893 }
3894 }
3895 break;
3896#endif
3897
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003898 case MCLOSE + 0: // Match end: \ze
3899 case MCLOSE + 1: // \)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003900 case MCLOSE + 2:
3901 case MCLOSE + 3:
3902 case MCLOSE + 4:
3903 case MCLOSE + 5:
3904 case MCLOSE + 6:
3905 case MCLOSE + 7:
3906 case MCLOSE + 8:
3907 case MCLOSE + 9:
3908 {
3909 no = op - MCLOSE;
3910 cleanup_subexpr();
3911 rp = regstack_push(RS_MCLOSE, scan);
3912 if (rp == NULL)
3913 status = RA_FAIL;
3914 else
3915 {
3916 rp->rs_no = no;
3917 save_se(&rp->rs_un.sesave, &rex.reg_endpos[no],
3918 &rex.reg_endp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003919 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003920 }
3921 }
3922 break;
3923
3924#ifdef FEAT_SYN_HL
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003925 case ZCLOSE + 1: // \) after \z(
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003926 case ZCLOSE + 2:
3927 case ZCLOSE + 3:
3928 case ZCLOSE + 4:
3929 case ZCLOSE + 5:
3930 case ZCLOSE + 6:
3931 case ZCLOSE + 7:
3932 case ZCLOSE + 8:
3933 case ZCLOSE + 9:
3934 {
3935 no = op - ZCLOSE;
3936 cleanup_zsubexpr();
3937 rp = regstack_push(RS_ZCLOSE, scan);
3938 if (rp == NULL)
3939 status = RA_FAIL;
3940 else
3941 {
3942 rp->rs_no = no;
3943 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
3944 &reg_endzp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003945 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003946 }
3947 }
3948 break;
3949#endif
3950
3951 case BACKREF + 1:
3952 case BACKREF + 2:
3953 case BACKREF + 3:
3954 case BACKREF + 4:
3955 case BACKREF + 5:
3956 case BACKREF + 6:
3957 case BACKREF + 7:
3958 case BACKREF + 8:
3959 case BACKREF + 9:
3960 {
3961 int len;
3962
3963 no = op - BACKREF;
3964 cleanup_subexpr();
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003965 if (!REG_MULTI) // Single-line regexp
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003966 {
3967 if (rex.reg_startp[no] == NULL || rex.reg_endp[no] == NULL)
3968 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003969 // Backref was not set: Match an empty string.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003970 len = 0;
3971 }
3972 else
3973 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003974 // Compare current input with back-ref in the same
3975 // line.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003976 len = (int)(rex.reg_endp[no] - rex.reg_startp[no]);
3977 if (cstrncmp(rex.reg_startp[no], rex.input, &len) != 0)
3978 status = RA_NOMATCH;
3979 }
3980 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003981 else // Multi-line regexp
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003982 {
3983 if (rex.reg_startpos[no].lnum < 0
3984 || rex.reg_endpos[no].lnum < 0)
3985 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003986 // Backref was not set: Match an empty string.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003987 len = 0;
3988 }
3989 else
3990 {
3991 if (rex.reg_startpos[no].lnum == rex.lnum
3992 && rex.reg_endpos[no].lnum == rex.lnum)
3993 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003994 // Compare back-ref within the current line.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003995 len = rex.reg_endpos[no].col
3996 - rex.reg_startpos[no].col;
3997 if (cstrncmp(rex.line + rex.reg_startpos[no].col,
3998 rex.input, &len) != 0)
3999 status = RA_NOMATCH;
4000 }
4001 else
4002 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004003 // Messy situation: Need to compare between two
4004 // lines.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004005 int r = match_with_backref(
4006 rex.reg_startpos[no].lnum,
4007 rex.reg_startpos[no].col,
4008 rex.reg_endpos[no].lnum,
4009 rex.reg_endpos[no].col,
4010 &len);
4011
4012 if (r != RA_MATCH)
4013 status = r;
4014 }
4015 }
4016 }
4017
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004018 // Matched the backref, skip over it.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004019 rex.input += len;
4020 }
4021 break;
4022
4023#ifdef FEAT_SYN_HL
4024 case ZREF + 1:
4025 case ZREF + 2:
4026 case ZREF + 3:
4027 case ZREF + 4:
4028 case ZREF + 5:
4029 case ZREF + 6:
4030 case ZREF + 7:
4031 case ZREF + 8:
4032 case ZREF + 9:
4033 {
4034 int len;
4035
4036 cleanup_zsubexpr();
4037 no = op - ZREF;
4038 if (re_extmatch_in != NULL
4039 && re_extmatch_in->matches[no] != NULL)
4040 {
4041 len = (int)STRLEN(re_extmatch_in->matches[no]);
4042 if (cstrncmp(re_extmatch_in->matches[no],
4043 rex.input, &len) != 0)
4044 status = RA_NOMATCH;
4045 else
4046 rex.input += len;
4047 }
4048 else
4049 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004050 // Backref was not set: Match an empty string.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004051 }
4052 }
4053 break;
4054#endif
4055
4056 case BRANCH:
4057 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004058 if (OP(next) != BRANCH) // No choice.
4059 next = OPERAND(scan); // Avoid recursion.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004060 else
4061 {
4062 rp = regstack_push(RS_BRANCH, scan);
4063 if (rp == NULL)
4064 status = RA_FAIL;
4065 else
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004066 status = RA_BREAK; // rest is below
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004067 }
4068 }
4069 break;
4070
4071 case BRACE_LIMITS:
4072 {
4073 if (OP(next) == BRACE_SIMPLE)
4074 {
4075 bl_minval = OPERAND_MIN(scan);
4076 bl_maxval = OPERAND_MAX(scan);
4077 }
4078 else if (OP(next) >= BRACE_COMPLEX
4079 && OP(next) < BRACE_COMPLEX + 10)
4080 {
4081 no = OP(next) - BRACE_COMPLEX;
4082 brace_min[no] = OPERAND_MIN(scan);
4083 brace_max[no] = OPERAND_MAX(scan);
4084 brace_count[no] = 0;
4085 }
4086 else
4087 {
4088 internal_error("BRACE_LIMITS");
4089 status = RA_FAIL;
4090 }
4091 }
4092 break;
4093
4094 case BRACE_COMPLEX + 0:
4095 case BRACE_COMPLEX + 1:
4096 case BRACE_COMPLEX + 2:
4097 case BRACE_COMPLEX + 3:
4098 case BRACE_COMPLEX + 4:
4099 case BRACE_COMPLEX + 5:
4100 case BRACE_COMPLEX + 6:
4101 case BRACE_COMPLEX + 7:
4102 case BRACE_COMPLEX + 8:
4103 case BRACE_COMPLEX + 9:
4104 {
4105 no = op - BRACE_COMPLEX;
4106 ++brace_count[no];
4107
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004108 // If not matched enough times yet, try one more
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004109 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
4110 ? brace_min[no] : brace_max[no]))
4111 {
4112 rp = regstack_push(RS_BRCPLX_MORE, scan);
4113 if (rp == NULL)
4114 status = RA_FAIL;
4115 else
4116 {
4117 rp->rs_no = no;
4118 reg_save(&rp->rs_un.regsave, &backpos);
4119 next = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004120 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004121 }
4122 break;
4123 }
4124
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004125 // If matched enough times, may try matching some more
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004126 if (brace_min[no] <= brace_max[no])
4127 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004128 // Range is the normal way around, use longest match
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004129 if (brace_count[no] <= brace_max[no])
4130 {
4131 rp = regstack_push(RS_BRCPLX_LONG, scan);
4132 if (rp == NULL)
4133 status = RA_FAIL;
4134 else
4135 {
4136 rp->rs_no = no;
4137 reg_save(&rp->rs_un.regsave, &backpos);
4138 next = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004139 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004140 }
4141 }
4142 }
4143 else
4144 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004145 // Range is backwards, use shortest match first
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004146 if (brace_count[no] <= brace_min[no])
4147 {
4148 rp = regstack_push(RS_BRCPLX_SHORT, scan);
4149 if (rp == NULL)
4150 status = RA_FAIL;
4151 else
4152 {
4153 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004154 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004155 }
4156 }
4157 }
4158 }
4159 break;
4160
4161 case BRACE_SIMPLE:
4162 case STAR:
4163 case PLUS:
4164 {
4165 regstar_T rst;
4166
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004167 // Lookahead to avoid useless match attempts when we know
4168 // what character comes next.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004169 if (OP(next) == EXACTLY)
4170 {
4171 rst.nextb = *OPERAND(next);
4172 if (rex.reg_ic)
4173 {
4174 if (MB_ISUPPER(rst.nextb))
4175 rst.nextb_ic = MB_TOLOWER(rst.nextb);
4176 else
4177 rst.nextb_ic = MB_TOUPPER(rst.nextb);
4178 }
4179 else
4180 rst.nextb_ic = rst.nextb;
4181 }
4182 else
4183 {
4184 rst.nextb = NUL;
4185 rst.nextb_ic = NUL;
4186 }
4187 if (op != BRACE_SIMPLE)
4188 {
4189 rst.minval = (op == STAR) ? 0 : 1;
4190 rst.maxval = MAX_LIMIT;
4191 }
4192 else
4193 {
4194 rst.minval = bl_minval;
4195 rst.maxval = bl_maxval;
4196 }
4197
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004198 // When maxval > minval, try matching as much as possible, up
4199 // to maxval. When maxval < minval, try matching at least the
4200 // minimal number (since the range is backwards, that's also
4201 // maxval!).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004202 rst.count = regrepeat(OPERAND(scan), rst.maxval);
4203 if (got_int)
4204 {
4205 status = RA_FAIL;
4206 break;
4207 }
4208 if (rst.minval <= rst.maxval
4209 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4210 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004211 // It could match. Prepare for trying to match what
4212 // follows. The code is below. Parameters are stored in
4213 // a regstar_T on the regstack.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004214 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
4215 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004216 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004217 status = RA_FAIL;
4218 }
4219 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
4220 status = RA_FAIL;
4221 else
4222 {
4223 regstack.ga_len += sizeof(regstar_T);
4224 rp = regstack_push(rst.minval <= rst.maxval
4225 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
4226 if (rp == NULL)
4227 status = RA_FAIL;
4228 else
4229 {
4230 *(((regstar_T *)rp) - 1) = rst;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004231 status = RA_BREAK; // skip the restore bits
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004232 }
4233 }
4234 }
4235 else
4236 status = RA_NOMATCH;
4237
4238 }
4239 break;
4240
4241 case NOMATCH:
4242 case MATCH:
4243 case SUBPAT:
4244 rp = regstack_push(RS_NOMATCH, scan);
4245 if (rp == NULL)
4246 status = RA_FAIL;
4247 else
4248 {
4249 rp->rs_no = op;
4250 reg_save(&rp->rs_un.regsave, &backpos);
4251 next = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004252 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004253 }
4254 break;
4255
4256 case BEHIND:
4257 case NOBEHIND:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004258 // Need a bit of room to store extra positions.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004259 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
4260 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004261 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004262 status = RA_FAIL;
4263 }
4264 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
4265 status = RA_FAIL;
4266 else
4267 {
4268 regstack.ga_len += sizeof(regbehind_T);
4269 rp = regstack_push(RS_BEHIND1, scan);
4270 if (rp == NULL)
4271 status = RA_FAIL;
4272 else
4273 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004274 // Need to save the subexpr to be able to restore them
4275 // when there is a match but we don't use it.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004276 save_subexpr(((regbehind_T *)rp) - 1);
4277
4278 rp->rs_no = op;
4279 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004280 // First try if what follows matches. If it does then we
4281 // check the behind match by looping.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004282 }
4283 }
4284 break;
4285
4286 case BHPOS:
4287 if (REG_MULTI)
4288 {
4289 if (behind_pos.rs_u.pos.col != (colnr_T)(rex.input - rex.line)
4290 || behind_pos.rs_u.pos.lnum != rex.lnum)
4291 status = RA_NOMATCH;
4292 }
4293 else if (behind_pos.rs_u.ptr != rex.input)
4294 status = RA_NOMATCH;
4295 break;
4296
4297 case NEWL:
4298 if ((c != NUL || !REG_MULTI || rex.lnum > rex.reg_maxline
4299 || rex.reg_line_lbr)
4300 && (c != '\n' || !rex.reg_line_lbr))
4301 status = RA_NOMATCH;
4302 else if (rex.reg_line_lbr)
4303 ADVANCE_REGINPUT();
4304 else
4305 reg_nextline();
4306 break;
4307
4308 case END:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004309 status = RA_MATCH; // Success!
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004310 break;
4311
4312 default:
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004313 iemsg(_(e_corrupted_regexp_program));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004314#ifdef DEBUG
4315 printf("Illegal op code %d\n", op);
4316#endif
4317 status = RA_FAIL;
4318 break;
4319 }
4320 }
4321
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004322 // If we can't continue sequentially, break the inner loop.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004323 if (status != RA_CONT)
4324 break;
4325
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004326 // Continue in inner loop, advance to next item.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004327 scan = next;
4328
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004329 } // end of inner loop
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004330
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004331 // If there is something on the regstack execute the code for the state.
4332 // If the state is popped then loop and use the older state.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004333 while (regstack.ga_len > 0 && status != RA_FAIL)
4334 {
4335 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4336 switch (rp->rs_state)
4337 {
4338 case RS_NOPEN:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004339 // Result is passed on as-is, simply pop the state.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004340 regstack_pop(&scan);
4341 break;
4342
4343 case RS_MOPEN:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004344 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004345 if (status == RA_NOMATCH)
4346 restore_se(&rp->rs_un.sesave, &rex.reg_startpos[rp->rs_no],
4347 &rex.reg_startp[rp->rs_no]);
4348 regstack_pop(&scan);
4349 break;
4350
4351#ifdef FEAT_SYN_HL
4352 case RS_ZOPEN:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004353 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004354 if (status == RA_NOMATCH)
4355 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4356 &reg_startzp[rp->rs_no]);
4357 regstack_pop(&scan);
4358 break;
4359#endif
4360
4361 case RS_MCLOSE:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004362 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004363 if (status == RA_NOMATCH)
4364 restore_se(&rp->rs_un.sesave, &rex.reg_endpos[rp->rs_no],
4365 &rex.reg_endp[rp->rs_no]);
4366 regstack_pop(&scan);
4367 break;
4368
4369#ifdef FEAT_SYN_HL
4370 case RS_ZCLOSE:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004371 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004372 if (status == RA_NOMATCH)
4373 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4374 &reg_endzp[rp->rs_no]);
4375 regstack_pop(&scan);
4376 break;
4377#endif
4378
4379 case RS_BRANCH:
4380 if (status == RA_MATCH)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004381 // this branch matched, use it
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004382 regstack_pop(&scan);
4383 else
4384 {
4385 if (status != RA_BREAK)
4386 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004387 // After a non-matching branch: try next one.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004388 reg_restore(&rp->rs_un.regsave, &backpos);
4389 scan = rp->rs_scan;
4390 }
4391 if (scan == NULL || OP(scan) != BRANCH)
4392 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004393 // no more branches, didn't find a match
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004394 status = RA_NOMATCH;
4395 regstack_pop(&scan);
4396 }
4397 else
4398 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004399 // Prepare to try a branch.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004400 rp->rs_scan = regnext(scan);
4401 reg_save(&rp->rs_un.regsave, &backpos);
4402 scan = OPERAND(scan);
4403 }
4404 }
4405 break;
4406
4407 case RS_BRCPLX_MORE:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004408 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004409 if (status == RA_NOMATCH)
4410 {
4411 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004412 --brace_count[rp->rs_no]; // decrement match count
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004413 }
4414 regstack_pop(&scan);
4415 break;
4416
4417 case RS_BRCPLX_LONG:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004418 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004419 if (status == RA_NOMATCH)
4420 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004421 // There was no match, but we did find enough matches.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004422 reg_restore(&rp->rs_un.regsave, &backpos);
4423 --brace_count[rp->rs_no];
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004424 // continue with the items after "\{}"
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004425 status = RA_CONT;
4426 }
4427 regstack_pop(&scan);
4428 if (status == RA_CONT)
4429 scan = regnext(scan);
4430 break;
4431
4432 case RS_BRCPLX_SHORT:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004433 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004434 if (status == RA_NOMATCH)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004435 // There was no match, try to match one more item.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004436 reg_restore(&rp->rs_un.regsave, &backpos);
4437 regstack_pop(&scan);
4438 if (status == RA_NOMATCH)
4439 {
4440 scan = OPERAND(scan);
4441 status = RA_CONT;
4442 }
4443 break;
4444
4445 case RS_NOMATCH:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004446 // Pop the state. If the operand matches for NOMATCH or
4447 // doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4448 // except for SUBPAT, and continue with the next item.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004449 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4450 status = RA_NOMATCH;
4451 else
4452 {
4453 status = RA_CONT;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004454 if (rp->rs_no != SUBPAT) // zero-width
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004455 reg_restore(&rp->rs_un.regsave, &backpos);
4456 }
4457 regstack_pop(&scan);
4458 if (status == RA_CONT)
4459 scan = regnext(scan);
4460 break;
4461
4462 case RS_BEHIND1:
4463 if (status == RA_NOMATCH)
4464 {
4465 regstack_pop(&scan);
4466 regstack.ga_len -= sizeof(regbehind_T);
4467 }
4468 else
4469 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004470 // The stuff after BEHIND/NOBEHIND matches. Now try if
4471 // the behind part does (not) match before the current
4472 // position in the input. This must be done at every
4473 // position in the input and checking if the match ends at
4474 // the current position.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004475
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004476 // save the position after the found match for next
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004477 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
4478
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004479 // Start looking for a match with operand at the current
4480 // position. Go back one character until we find the
4481 // result, hitting the start of the line or the previous
4482 // line (for multi-line matching).
4483 // Set behind_pos to where the match should end, BHPOS
4484 // will match it. Save the current value.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004485 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4486 behind_pos = rp->rs_un.regsave;
4487
4488 rp->rs_state = RS_BEHIND2;
4489
4490 reg_restore(&rp->rs_un.regsave, &backpos);
4491 scan = OPERAND(rp->rs_scan) + 4;
4492 }
4493 break;
4494
4495 case RS_BEHIND2:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004496 // Looping for BEHIND / NOBEHIND match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004497 if (status == RA_MATCH && reg_save_equal(&behind_pos))
4498 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004499 // found a match that ends where "next" started
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004500 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4501 if (rp->rs_no == BEHIND)
4502 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4503 &backpos);
4504 else
4505 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004506 // But we didn't want a match. Need to restore the
4507 // subexpr, because what follows matched, so they have
4508 // been set.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004509 status = RA_NOMATCH;
4510 restore_subexpr(((regbehind_T *)rp) - 1);
4511 }
4512 regstack_pop(&scan);
4513 regstack.ga_len -= sizeof(regbehind_T);
4514 }
4515 else
4516 {
4517 long limit;
4518
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004519 // No match or a match that doesn't end where we want it: Go
4520 // back one character. May go to previous line once.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004521 no = OK;
4522 limit = OPERAND_MIN(rp->rs_scan);
4523 if (REG_MULTI)
4524 {
4525 if (limit > 0
4526 && ((rp->rs_un.regsave.rs_u.pos.lnum
4527 < behind_pos.rs_u.pos.lnum
4528 ? (colnr_T)STRLEN(rex.line)
4529 : behind_pos.rs_u.pos.col)
4530 - rp->rs_un.regsave.rs_u.pos.col >= limit))
4531 no = FAIL;
4532 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
4533 {
4534 if (rp->rs_un.regsave.rs_u.pos.lnum
4535 < behind_pos.rs_u.pos.lnum
4536 || reg_getline(
4537 --rp->rs_un.regsave.rs_u.pos.lnum)
4538 == NULL)
4539 no = FAIL;
4540 else
4541 {
4542 reg_restore(&rp->rs_un.regsave, &backpos);
4543 rp->rs_un.regsave.rs_u.pos.col =
4544 (colnr_T)STRLEN(rex.line);
4545 }
4546 }
4547 else
4548 {
4549 if (has_mbyte)
4550 {
4551 char_u *line =
4552 reg_getline(rp->rs_un.regsave.rs_u.pos.lnum);
4553
4554 rp->rs_un.regsave.rs_u.pos.col -=
4555 (*mb_head_off)(line, line
4556 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
4557 }
4558 else
4559 --rp->rs_un.regsave.rs_u.pos.col;
4560 }
4561 }
4562 else
4563 {
4564 if (rp->rs_un.regsave.rs_u.ptr == rex.line)
4565 no = FAIL;
4566 else
4567 {
4568 MB_PTR_BACK(rex.line, rp->rs_un.regsave.rs_u.ptr);
4569 if (limit > 0 && (long)(behind_pos.rs_u.ptr
4570 - rp->rs_un.regsave.rs_u.ptr) > limit)
4571 no = FAIL;
4572 }
4573 }
4574 if (no == OK)
4575 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004576 // Advanced, prepare for finding match again.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004577 reg_restore(&rp->rs_un.regsave, &backpos);
4578 scan = OPERAND(rp->rs_scan) + 4;
4579 if (status == RA_MATCH)
4580 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004581 // We did match, so subexpr may have been changed,
4582 // need to restore them for the next try.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004583 status = RA_NOMATCH;
4584 restore_subexpr(((regbehind_T *)rp) - 1);
4585 }
4586 }
4587 else
4588 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004589 // Can't advance. For NOBEHIND that's a match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004590 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4591 if (rp->rs_no == NOBEHIND)
4592 {
4593 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4594 &backpos);
4595 status = RA_MATCH;
4596 }
4597 else
4598 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004599 // We do want a proper match. Need to restore the
4600 // subexpr if we had a match, because they may have
4601 // been set.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004602 if (status == RA_MATCH)
4603 {
4604 status = RA_NOMATCH;
4605 restore_subexpr(((regbehind_T *)rp) - 1);
4606 }
4607 }
4608 regstack_pop(&scan);
4609 regstack.ga_len -= sizeof(regbehind_T);
4610 }
4611 }
4612 break;
4613
4614 case RS_STAR_LONG:
4615 case RS_STAR_SHORT:
4616 {
4617 regstar_T *rst = ((regstar_T *)rp) - 1;
4618
4619 if (status == RA_MATCH)
4620 {
4621 regstack_pop(&scan);
4622 regstack.ga_len -= sizeof(regstar_T);
4623 break;
4624 }
4625
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004626 // Tried once already, restore input pointers.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004627 if (status != RA_BREAK)
4628 reg_restore(&rp->rs_un.regsave, &backpos);
4629
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004630 // Repeat until we found a position where it could match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004631 for (;;)
4632 {
4633 if (status != RA_BREAK)
4634 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004635 // Tried first position already, advance.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004636 if (rp->rs_state == RS_STAR_LONG)
4637 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004638 // Trying for longest match, but couldn't or
4639 // didn't match -- back up one char.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004640 if (--rst->count < rst->minval)
4641 break;
4642 if (rex.input == rex.line)
4643 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004644 // backup to last char of previous line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004645 --rex.lnum;
4646 rex.line = reg_getline(rex.lnum);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004647 // Just in case regrepeat() didn't count
4648 // right.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004649 if (rex.line == NULL)
4650 break;
4651 rex.input = rex.line + STRLEN(rex.line);
4652 fast_breakcheck();
4653 }
4654 else
4655 MB_PTR_BACK(rex.line, rex.input);
4656 }
4657 else
4658 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004659 // Range is backwards, use shortest match first.
4660 // Careful: maxval and minval are exchanged!
4661 // Couldn't or didn't match: try advancing one
4662 // char.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004663 if (rst->count == rst->minval
4664 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
4665 break;
4666 ++rst->count;
4667 }
4668 if (got_int)
4669 break;
4670 }
4671 else
4672 status = RA_NOMATCH;
4673
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004674 // If it could match, try it.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004675 if (rst->nextb == NUL || *rex.input == rst->nextb
4676 || *rex.input == rst->nextb_ic)
4677 {
4678 reg_save(&rp->rs_un.regsave, &backpos);
4679 scan = regnext(rp->rs_scan);
4680 status = RA_CONT;
4681 break;
4682 }
4683 }
4684 if (status != RA_CONT)
4685 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004686 // Failed.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004687 regstack_pop(&scan);
4688 regstack.ga_len -= sizeof(regstar_T);
4689 status = RA_NOMATCH;
4690 }
4691 }
4692 break;
4693 }
4694
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004695 // If we want to continue the inner loop or didn't pop a state
4696 // continue matching loop
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004697 if (status == RA_CONT || rp == (regitem_T *)
4698 ((char *)regstack.ga_data + regstack.ga_len) - 1)
4699 break;
4700 }
4701
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004702 // May need to continue with the inner loop, starting at "scan".
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004703 if (status == RA_CONT)
4704 continue;
4705
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004706 // If the regstack is empty or something failed we are done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004707 if (regstack.ga_len == 0 || status == RA_FAIL)
4708 {
4709 if (scan == NULL)
4710 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004711 // We get here only if there's trouble -- normally "case END" is
4712 // the terminating point.
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004713 iemsg(_(e_corrupted_regexp_program));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004714#ifdef DEBUG
4715 printf("Premature EOL\n");
4716#endif
4717 }
4718 return (status == RA_MATCH);
4719 }
4720
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004721 } // End of loop until the regstack is empty.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004722
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004723 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004724}
4725
4726/*
4727 * regtry - try match of "prog" with at rex.line["col"].
4728 * Returns 0 for failure, number of lines contained in the match otherwise.
4729 */
4730 static long
4731regtry(
4732 bt_regprog_T *prog,
4733 colnr_T col,
4734 proftime_T *tm, // timeout limit or NULL
4735 int *timed_out) // flag set on timeout or NULL
4736{
4737 rex.input = rex.line + col;
4738 rex.need_clear_subexpr = TRUE;
4739#ifdef FEAT_SYN_HL
4740 // Clear the external match subpointers if necessary.
4741 rex.need_clear_zsubexpr = (prog->reghasz == REX_SET);
4742#endif
4743
4744 if (regmatch(prog->program + 1, tm, timed_out) == 0)
4745 return 0;
4746
4747 cleanup_subexpr();
4748 if (REG_MULTI)
4749 {
4750 if (rex.reg_startpos[0].lnum < 0)
4751 {
4752 rex.reg_startpos[0].lnum = 0;
4753 rex.reg_startpos[0].col = col;
4754 }
4755 if (rex.reg_endpos[0].lnum < 0)
4756 {
4757 rex.reg_endpos[0].lnum = rex.lnum;
4758 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
4759 }
4760 else
4761 // Use line number of "\ze".
4762 rex.lnum = rex.reg_endpos[0].lnum;
4763 }
4764 else
4765 {
4766 if (rex.reg_startp[0] == NULL)
4767 rex.reg_startp[0] = rex.line + col;
4768 if (rex.reg_endp[0] == NULL)
4769 rex.reg_endp[0] = rex.input;
4770 }
4771#ifdef FEAT_SYN_HL
4772 // Package any found \z(...\) matches for export. Default is none.
4773 unref_extmatch(re_extmatch_out);
4774 re_extmatch_out = NULL;
4775
4776 if (prog->reghasz == REX_SET)
4777 {
4778 int i;
4779
4780 cleanup_zsubexpr();
4781 re_extmatch_out = make_extmatch();
Bram Moolenaar7c77b342019-12-22 19:40:40 +01004782 if (re_extmatch_out == NULL)
4783 return 0;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004784 for (i = 0; i < NSUBEXP; i++)
4785 {
4786 if (REG_MULTI)
4787 {
4788 // Only accept single line matches.
4789 if (reg_startzpos[i].lnum >= 0
4790 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4791 && reg_endzpos[i].col >= reg_startzpos[i].col)
4792 re_extmatch_out->matches[i] =
4793 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
4794 + reg_startzpos[i].col,
4795 reg_endzpos[i].col - reg_startzpos[i].col);
4796 }
4797 else
4798 {
4799 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4800 re_extmatch_out->matches[i] =
4801 vim_strnsave(reg_startzp[i],
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004802 reg_endzp[i] - reg_startzp[i]);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004803 }
4804 }
4805 }
4806#endif
4807 return 1 + rex.lnum;
4808}
4809
4810/*
4811 * Match a regexp against a string ("line" points to the string) or multiple
Bram Moolenaardf365142021-05-03 20:01:45 +02004812 * lines (if "line" is NULL, use reg_getline()).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004813 * Returns 0 for failure, number of lines contained in the match otherwise.
4814 */
4815 static long
4816bt_regexec_both(
4817 char_u *line,
4818 colnr_T col, // column to start looking for match
4819 proftime_T *tm, // timeout limit or NULL
4820 int *timed_out) // flag set on timeout or NULL
4821{
4822 bt_regprog_T *prog;
4823 char_u *s;
4824 long retval = 0L;
4825
4826 // Create "regstack" and "backpos" if they are not allocated yet.
4827 // We allocate *_INITIAL amount of bytes first and then set the grow size
4828 // to much bigger value to avoid many malloc calls in case of deep regular
4829 // expressions.
4830 if (regstack.ga_data == NULL)
4831 {
4832 // Use an item size of 1 byte, since we push different things
4833 // onto the regstack.
4834 ga_init2(&regstack, 1, REGSTACK_INITIAL);
4835 (void)ga_grow(&regstack, REGSTACK_INITIAL);
4836 regstack.ga_growsize = REGSTACK_INITIAL * 8;
4837 }
4838
4839 if (backpos.ga_data == NULL)
4840 {
4841 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
4842 (void)ga_grow(&backpos, BACKPOS_INITIAL);
4843 backpos.ga_growsize = BACKPOS_INITIAL * 8;
4844 }
4845
4846 if (REG_MULTI)
4847 {
4848 prog = (bt_regprog_T *)rex.reg_mmatch->regprog;
4849 line = reg_getline((linenr_T)0);
4850 rex.reg_startpos = rex.reg_mmatch->startpos;
4851 rex.reg_endpos = rex.reg_mmatch->endpos;
4852 }
4853 else
4854 {
4855 prog = (bt_regprog_T *)rex.reg_match->regprog;
4856 rex.reg_startp = rex.reg_match->startp;
4857 rex.reg_endp = rex.reg_match->endp;
4858 }
4859
4860 // Be paranoid...
4861 if (prog == NULL || line == NULL)
4862 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004863 iemsg(_(e_null_argument));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004864 goto theend;
4865 }
4866
4867 // Check validity of program.
4868 if (prog_magic_wrong())
4869 goto theend;
4870
4871 // If the start column is past the maximum column: no need to try.
4872 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
4873 goto theend;
4874
4875 // If pattern contains "\c" or "\C": overrule value of rex.reg_ic
4876 if (prog->regflags & RF_ICASE)
4877 rex.reg_ic = TRUE;
4878 else if (prog->regflags & RF_NOICASE)
4879 rex.reg_ic = FALSE;
4880
4881 // If pattern contains "\Z" overrule value of rex.reg_icombine
4882 if (prog->regflags & RF_ICOMBINE)
4883 rex.reg_icombine = TRUE;
4884
4885 // If there is a "must appear" string, look for it.
4886 if (prog->regmust != NULL)
4887 {
4888 int c;
4889
4890 if (has_mbyte)
4891 c = (*mb_ptr2char)(prog->regmust);
4892 else
4893 c = *prog->regmust;
4894 s = line + col;
4895
4896 // This is used very often, esp. for ":global". Use three versions of
4897 // the loop to avoid overhead of conditions.
4898 if (!rex.reg_ic && !has_mbyte)
4899 while ((s = vim_strbyte(s, c)) != NULL)
4900 {
4901 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
4902 break; // Found it.
4903 ++s;
4904 }
4905 else if (!rex.reg_ic || (!enc_utf8 && mb_char2len(c) > 1))
4906 while ((s = vim_strchr(s, c)) != NULL)
4907 {
4908 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
4909 break; // Found it.
4910 MB_PTR_ADV(s);
4911 }
4912 else
4913 while ((s = cstrchr(s, c)) != NULL)
4914 {
4915 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
4916 break; // Found it.
4917 MB_PTR_ADV(s);
4918 }
4919 if (s == NULL) // Not present.
4920 goto theend;
4921 }
4922
4923 rex.line = line;
4924 rex.lnum = 0;
4925 reg_toolong = FALSE;
4926
4927 // Simplest case: Anchored match need be tried only once.
4928 if (prog->reganch)
4929 {
4930 int c;
4931
4932 if (has_mbyte)
4933 c = (*mb_ptr2char)(rex.line + col);
4934 else
4935 c = rex.line[col];
4936 if (prog->regstart == NUL
4937 || prog->regstart == c
4938 || (rex.reg_ic
4939 && (((enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
4940 || (c < 255 && prog->regstart < 255 &&
4941 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
4942 retval = regtry(prog, col, tm, timed_out);
4943 else
4944 retval = 0;
4945 }
4946 else
4947 {
4948#ifdef FEAT_RELTIME
4949 int tm_count = 0;
4950#endif
4951 // Messy cases: unanchored match.
4952 while (!got_int)
4953 {
4954 if (prog->regstart != NUL)
4955 {
4956 // Skip until the char we know it must start with.
4957 // Used often, do some work to avoid call overhead.
4958 if (!rex.reg_ic && !has_mbyte)
4959 s = vim_strbyte(rex.line + col, prog->regstart);
4960 else
4961 s = cstrchr(rex.line + col, prog->regstart);
4962 if (s == NULL)
4963 {
4964 retval = 0;
4965 break;
4966 }
4967 col = (int)(s - rex.line);
4968 }
4969
4970 // Check for maximum column to try.
4971 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
4972 {
4973 retval = 0;
4974 break;
4975 }
4976
4977 retval = regtry(prog, col, tm, timed_out);
4978 if (retval > 0)
4979 break;
4980
4981 // if not currently on the first line, get it again
4982 if (rex.lnum != 0)
4983 {
4984 rex.lnum = 0;
4985 rex.line = reg_getline((linenr_T)0);
4986 }
4987 if (rex.line[col] == NUL)
4988 break;
4989 if (has_mbyte)
4990 col += (*mb_ptr2len)(rex.line + col);
4991 else
4992 ++col;
4993#ifdef FEAT_RELTIME
4994 // Check for timeout once in a twenty times to avoid overhead.
4995 if (tm != NULL && ++tm_count == 20)
4996 {
4997 tm_count = 0;
4998 if (profile_passed_limit(tm))
4999 {
5000 if (timed_out != NULL)
5001 *timed_out = TRUE;
5002 break;
5003 }
5004 }
5005#endif
5006 }
5007 }
5008
5009theend:
5010 // Free "reg_tofree" when it's a bit big.
5011 // Free regstack and backpos if they are bigger than their initial size.
5012 if (reg_tofreelen > 400)
5013 VIM_CLEAR(reg_tofree);
5014 if (regstack.ga_maxlen > REGSTACK_INITIAL)
5015 ga_clear(&regstack);
5016 if (backpos.ga_maxlen > BACKPOS_INITIAL)
5017 ga_clear(&backpos);
5018
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005019 if (retval > 0)
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005020 {
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005021 // Make sure the end is never before the start. Can happen when \zs
5022 // and \ze are used.
5023 if (REG_MULTI)
5024 {
5025 lpos_T *start = &rex.reg_mmatch->startpos[0];
5026 lpos_T *end = &rex.reg_mmatch->endpos[0];
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005027
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005028 if (end->lnum < start->lnum
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005029 || (end->lnum == start->lnum && end->col < start->col))
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005030 rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
5031 }
5032 else
5033 {
5034 if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
5035 rex.reg_match->endp[0] = rex.reg_match->startp[0];
5036 }
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005037 }
5038
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02005039 return retval;
5040}
5041
5042/*
5043 * Match a regexp against a string.
5044 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5045 * Uses curbuf for line count and 'iskeyword'.
5046 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
5047 *
5048 * Returns 0 for failure, number of lines contained in the match otherwise.
5049 */
5050 static int
5051bt_regexec_nl(
5052 regmatch_T *rmp,
5053 char_u *line, // string to match against
5054 colnr_T col, // column to start looking for match
5055 int line_lbr)
5056{
5057 rex.reg_match = rmp;
5058 rex.reg_mmatch = NULL;
5059 rex.reg_maxline = 0;
5060 rex.reg_line_lbr = line_lbr;
5061 rex.reg_buf = curbuf;
5062 rex.reg_win = NULL;
5063 rex.reg_ic = rmp->rm_ic;
5064 rex.reg_icombine = FALSE;
5065 rex.reg_maxcol = 0;
5066
5067 return bt_regexec_both(line, col, NULL, NULL);
5068}
5069
5070/*
5071 * Match a regexp against multiple lines.
5072 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5073 * Uses curbuf for line count and 'iskeyword'.
5074 *
5075 * Return zero if there is no match. Return number of lines contained in the
5076 * match otherwise.
5077 */
5078 static long
5079bt_regexec_multi(
5080 regmmatch_T *rmp,
5081 win_T *win, // window in which to search or NULL
5082 buf_T *buf, // buffer in which to search
5083 linenr_T lnum, // nr of line to start looking for match
5084 colnr_T col, // column to start looking for match
5085 proftime_T *tm, // timeout limit or NULL
5086 int *timed_out) // flag set on timeout or NULL
5087{
Bram Moolenaarf4140482020-02-15 23:06:45 +01005088 init_regexec_multi(rmp, win, buf, lnum);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02005089 return bt_regexec_both(NULL, col, tm, timed_out);
5090}
5091
5092/*
5093 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5094 */
5095 static int
5096re_num_cmp(long_u val, char_u *scan)
5097{
5098 long_u n = OPERAND_MIN(scan);
5099
5100 if (OPERAND_CMP(scan) == '>')
5101 return val > n;
5102 if (OPERAND_CMP(scan) == '<')
5103 return val < n;
5104 return val == n;
5105}
5106
5107#ifdef BT_REGEXP_DUMP
5108
5109/*
5110 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5111 */
5112 static void
5113regdump(char_u *pattern, bt_regprog_T *r)
5114{
5115 char_u *s;
5116 int op = EXACTLY; // Arbitrary non-END op.
5117 char_u *next;
5118 char_u *end = NULL;
5119 FILE *f;
5120
5121#ifdef BT_REGEXP_LOG
5122 f = fopen("bt_regexp_log.log", "a");
5123#else
5124 f = stdout;
5125#endif
5126 if (f == NULL)
5127 return;
5128 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
5129
5130 s = r->program + 1;
5131 // Loop until we find the END that isn't before a referred next (an END
5132 // can also appear in a NOMATCH operand).
5133 while (op != END || s <= end)
5134 {
5135 op = OP(s);
5136 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); // Where, what.
5137 next = regnext(s);
5138 if (next == NULL) // Next ptr.
5139 fprintf(f, "(0)");
5140 else
5141 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
5142 if (end < next)
5143 end = next;
5144 if (op == BRACE_LIMITS)
5145 {
5146 // Two ints
5147 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5148 s += 8;
5149 }
5150 else if (op == BEHIND || op == NOBEHIND)
5151 {
5152 // one int
5153 fprintf(f, " count %ld", OPERAND_MIN(s));
5154 s += 4;
5155 }
5156 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
5157 {
5158 // one int plus comparator
5159 fprintf(f, " count %ld", OPERAND_MIN(s));
5160 s += 5;
5161 }
5162 s += 3;
5163 if (op == ANYOF || op == ANYOF + ADD_NL
5164 || op == ANYBUT || op == ANYBUT + ADD_NL
5165 || op == EXACTLY)
5166 {
5167 // Literal string, where present.
5168 fprintf(f, "\nxxxxxxxxx\n");
5169 while (*s != NUL)
5170 fprintf(f, "%c", *s++);
5171 fprintf(f, "\nxxxxxxxxx\n");
5172 s++;
5173 }
5174 fprintf(f, "\r\n");
5175 }
5176
5177 // Header fields of interest.
5178 if (r->regstart != NUL)
5179 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
5180 ? (char *)transchar(r->regstart)
5181 : "multibyte", r->regstart);
5182 if (r->reganch)
5183 fprintf(f, "anchored; ");
5184 if (r->regmust != NULL)
5185 fprintf(f, "must have \"%s\"", r->regmust);
5186 fprintf(f, "\r\n");
5187
5188#ifdef BT_REGEXP_LOG
5189 fclose(f);
5190#endif
5191}
5192#endif // BT_REGEXP_DUMP
5193
5194#ifdef DEBUG
5195/*
5196 * regprop - printable representation of opcode
5197 */
5198 static char_u *
5199regprop(char_u *op)
5200{
5201 char *p;
5202 static char buf[50];
5203
5204 STRCPY(buf, ":");
5205
5206 switch ((int) OP(op))
5207 {
5208 case BOL:
5209 p = "BOL";
5210 break;
5211 case EOL:
5212 p = "EOL";
5213 break;
5214 case RE_BOF:
5215 p = "BOF";
5216 break;
5217 case RE_EOF:
5218 p = "EOF";
5219 break;
5220 case CURSOR:
5221 p = "CURSOR";
5222 break;
5223 case RE_VISUAL:
5224 p = "RE_VISUAL";
5225 break;
5226 case RE_LNUM:
5227 p = "RE_LNUM";
5228 break;
5229 case RE_MARK:
5230 p = "RE_MARK";
5231 break;
5232 case RE_COL:
5233 p = "RE_COL";
5234 break;
5235 case RE_VCOL:
5236 p = "RE_VCOL";
5237 break;
5238 case BOW:
5239 p = "BOW";
5240 break;
5241 case EOW:
5242 p = "EOW";
5243 break;
5244 case ANY:
5245 p = "ANY";
5246 break;
5247 case ANY + ADD_NL:
5248 p = "ANY+NL";
5249 break;
5250 case ANYOF:
5251 p = "ANYOF";
5252 break;
5253 case ANYOF + ADD_NL:
5254 p = "ANYOF+NL";
5255 break;
5256 case ANYBUT:
5257 p = "ANYBUT";
5258 break;
5259 case ANYBUT + ADD_NL:
5260 p = "ANYBUT+NL";
5261 break;
5262 case IDENT:
5263 p = "IDENT";
5264 break;
5265 case IDENT + ADD_NL:
5266 p = "IDENT+NL";
5267 break;
5268 case SIDENT:
5269 p = "SIDENT";
5270 break;
5271 case SIDENT + ADD_NL:
5272 p = "SIDENT+NL";
5273 break;
5274 case KWORD:
5275 p = "KWORD";
5276 break;
5277 case KWORD + ADD_NL:
5278 p = "KWORD+NL";
5279 break;
5280 case SKWORD:
5281 p = "SKWORD";
5282 break;
5283 case SKWORD + ADD_NL:
5284 p = "SKWORD+NL";
5285 break;
5286 case FNAME:
5287 p = "FNAME";
5288 break;
5289 case FNAME + ADD_NL:
5290 p = "FNAME+NL";
5291 break;
5292 case SFNAME:
5293 p = "SFNAME";
5294 break;
5295 case SFNAME + ADD_NL:
5296 p = "SFNAME+NL";
5297 break;
5298 case PRINT:
5299 p = "PRINT";
5300 break;
5301 case PRINT + ADD_NL:
5302 p = "PRINT+NL";
5303 break;
5304 case SPRINT:
5305 p = "SPRINT";
5306 break;
5307 case SPRINT + ADD_NL:
5308 p = "SPRINT+NL";
5309 break;
5310 case WHITE:
5311 p = "WHITE";
5312 break;
5313 case WHITE + ADD_NL:
5314 p = "WHITE+NL";
5315 break;
5316 case NWHITE:
5317 p = "NWHITE";
5318 break;
5319 case NWHITE + ADD_NL:
5320 p = "NWHITE+NL";
5321 break;
5322 case DIGIT:
5323 p = "DIGIT";
5324 break;
5325 case DIGIT + ADD_NL:
5326 p = "DIGIT+NL";
5327 break;
5328 case NDIGIT:
5329 p = "NDIGIT";
5330 break;
5331 case NDIGIT + ADD_NL:
5332 p = "NDIGIT+NL";
5333 break;
5334 case HEX:
5335 p = "HEX";
5336 break;
5337 case HEX + ADD_NL:
5338 p = "HEX+NL";
5339 break;
5340 case NHEX:
5341 p = "NHEX";
5342 break;
5343 case NHEX + ADD_NL:
5344 p = "NHEX+NL";
5345 break;
5346 case OCTAL:
5347 p = "OCTAL";
5348 break;
5349 case OCTAL + ADD_NL:
5350 p = "OCTAL+NL";
5351 break;
5352 case NOCTAL:
5353 p = "NOCTAL";
5354 break;
5355 case NOCTAL + ADD_NL:
5356 p = "NOCTAL+NL";
5357 break;
5358 case WORD:
5359 p = "WORD";
5360 break;
5361 case WORD + ADD_NL:
5362 p = "WORD+NL";
5363 break;
5364 case NWORD:
5365 p = "NWORD";
5366 break;
5367 case NWORD + ADD_NL:
5368 p = "NWORD+NL";
5369 break;
5370 case HEAD:
5371 p = "HEAD";
5372 break;
5373 case HEAD + ADD_NL:
5374 p = "HEAD+NL";
5375 break;
5376 case NHEAD:
5377 p = "NHEAD";
5378 break;
5379 case NHEAD + ADD_NL:
5380 p = "NHEAD+NL";
5381 break;
5382 case ALPHA:
5383 p = "ALPHA";
5384 break;
5385 case ALPHA + ADD_NL:
5386 p = "ALPHA+NL";
5387 break;
5388 case NALPHA:
5389 p = "NALPHA";
5390 break;
5391 case NALPHA + ADD_NL:
5392 p = "NALPHA+NL";
5393 break;
5394 case LOWER:
5395 p = "LOWER";
5396 break;
5397 case LOWER + ADD_NL:
5398 p = "LOWER+NL";
5399 break;
5400 case NLOWER:
5401 p = "NLOWER";
5402 break;
5403 case NLOWER + ADD_NL:
5404 p = "NLOWER+NL";
5405 break;
5406 case UPPER:
5407 p = "UPPER";
5408 break;
5409 case UPPER + ADD_NL:
5410 p = "UPPER+NL";
5411 break;
5412 case NUPPER:
5413 p = "NUPPER";
5414 break;
5415 case NUPPER + ADD_NL:
5416 p = "NUPPER+NL";
5417 break;
5418 case BRANCH:
5419 p = "BRANCH";
5420 break;
5421 case EXACTLY:
5422 p = "EXACTLY";
5423 break;
5424 case NOTHING:
5425 p = "NOTHING";
5426 break;
5427 case BACK:
5428 p = "BACK";
5429 break;
5430 case END:
5431 p = "END";
5432 break;
5433 case MOPEN + 0:
5434 p = "MATCH START";
5435 break;
5436 case MOPEN + 1:
5437 case MOPEN + 2:
5438 case MOPEN + 3:
5439 case MOPEN + 4:
5440 case MOPEN + 5:
5441 case MOPEN + 6:
5442 case MOPEN + 7:
5443 case MOPEN + 8:
5444 case MOPEN + 9:
5445 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
5446 p = NULL;
5447 break;
5448 case MCLOSE + 0:
5449 p = "MATCH END";
5450 break;
5451 case MCLOSE + 1:
5452 case MCLOSE + 2:
5453 case MCLOSE + 3:
5454 case MCLOSE + 4:
5455 case MCLOSE + 5:
5456 case MCLOSE + 6:
5457 case MCLOSE + 7:
5458 case MCLOSE + 8:
5459 case MCLOSE + 9:
5460 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
5461 p = NULL;
5462 break;
5463 case BACKREF + 1:
5464 case BACKREF + 2:
5465 case BACKREF + 3:
5466 case BACKREF + 4:
5467 case BACKREF + 5:
5468 case BACKREF + 6:
5469 case BACKREF + 7:
5470 case BACKREF + 8:
5471 case BACKREF + 9:
5472 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
5473 p = NULL;
5474 break;
5475 case NOPEN:
5476 p = "NOPEN";
5477 break;
5478 case NCLOSE:
5479 p = "NCLOSE";
5480 break;
5481#ifdef FEAT_SYN_HL
5482 case ZOPEN + 1:
5483 case ZOPEN + 2:
5484 case ZOPEN + 3:
5485 case ZOPEN + 4:
5486 case ZOPEN + 5:
5487 case ZOPEN + 6:
5488 case ZOPEN + 7:
5489 case ZOPEN + 8:
5490 case ZOPEN + 9:
5491 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
5492 p = NULL;
5493 break;
5494 case ZCLOSE + 1:
5495 case ZCLOSE + 2:
5496 case ZCLOSE + 3:
5497 case ZCLOSE + 4:
5498 case ZCLOSE + 5:
5499 case ZCLOSE + 6:
5500 case ZCLOSE + 7:
5501 case ZCLOSE + 8:
5502 case ZCLOSE + 9:
5503 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
5504 p = NULL;
5505 break;
5506 case ZREF + 1:
5507 case ZREF + 2:
5508 case ZREF + 3:
5509 case ZREF + 4:
5510 case ZREF + 5:
5511 case ZREF + 6:
5512 case ZREF + 7:
5513 case ZREF + 8:
5514 case ZREF + 9:
5515 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
5516 p = NULL;
5517 break;
5518#endif
5519 case STAR:
5520 p = "STAR";
5521 break;
5522 case PLUS:
5523 p = "PLUS";
5524 break;
5525 case NOMATCH:
5526 p = "NOMATCH";
5527 break;
5528 case MATCH:
5529 p = "MATCH";
5530 break;
5531 case BEHIND:
5532 p = "BEHIND";
5533 break;
5534 case NOBEHIND:
5535 p = "NOBEHIND";
5536 break;
5537 case SUBPAT:
5538 p = "SUBPAT";
5539 break;
5540 case BRACE_LIMITS:
5541 p = "BRACE_LIMITS";
5542 break;
5543 case BRACE_SIMPLE:
5544 p = "BRACE_SIMPLE";
5545 break;
5546 case BRACE_COMPLEX + 0:
5547 case BRACE_COMPLEX + 1:
5548 case BRACE_COMPLEX + 2:
5549 case BRACE_COMPLEX + 3:
5550 case BRACE_COMPLEX + 4:
5551 case BRACE_COMPLEX + 5:
5552 case BRACE_COMPLEX + 6:
5553 case BRACE_COMPLEX + 7:
5554 case BRACE_COMPLEX + 8:
5555 case BRACE_COMPLEX + 9:
5556 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
5557 p = NULL;
5558 break;
5559 case MULTIBYTECODE:
5560 p = "MULTIBYTECODE";
5561 break;
5562 case NEWL:
5563 p = "NEWL";
5564 break;
5565 default:
5566 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
5567 p = NULL;
5568 break;
5569 }
5570 if (p != NULL)
5571 STRCAT(buf, p);
5572 return (char_u *)buf;
5573}
5574#endif // DEBUG