blob: cf484e0bab35f2e863ffbbf7e16b79d7590db4b8 [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 {
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200536 switch (c)
537 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +0200538 // Do not use '\300' style, it results in a negative number.
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200539 case 'A': case 0xc0: case 0xc1: case 0xc2: case 0xc3: case 0xc4:
540 case 0xc5: case 0x100: case 0x102: case 0x104: case 0x1cd:
541 case 0x1de: case 0x1e0: case 0x1fa: case 0x202: case 0x226:
542 case 0x23a: case 0x1e00: case 0x1ea0: case 0x1ea2: case 0x1ea4:
543 case 0x1ea6: case 0x1ea8: case 0x1eaa: case 0x1eac: case 0x1eae:
544 case 0x1eb0: case 0x1eb2: case 0x1eb4: case 0x1eb6:
545 regmbc('A'); regmbc(0xc0); regmbc(0xc1); regmbc(0xc2);
546 regmbc(0xc3); regmbc(0xc4); regmbc(0xc5);
547 regmbc(0x100); regmbc(0x102); regmbc(0x104);
548 regmbc(0x1cd); regmbc(0x1de); regmbc(0x1e0);
549 regmbc(0x1fa); regmbc(0x202); regmbc(0x226);
550 regmbc(0x23a); regmbc(0x1e00); regmbc(0x1ea0);
551 regmbc(0x1ea2); regmbc(0x1ea4); regmbc(0x1ea6);
552 regmbc(0x1ea8); regmbc(0x1eaa); regmbc(0x1eac);
553 regmbc(0x1eae); regmbc(0x1eb0); regmbc(0x1eb2);
554 regmbc(0x1eb4); regmbc(0x1eb6);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200555 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200556 case 'B': case 0x181: case 0x243: case 0x1e02:
557 case 0x1e04: case 0x1e06:
558 regmbc('B');
559 regmbc(0x181); regmbc(0x243); regmbc(0x1e02);
560 regmbc(0x1e04); regmbc(0x1e06);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200561 return;
562 case 'C': case 0xc7:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200563 case 0x106: case 0x108: case 0x10a: case 0x10c: case 0x187:
564 case 0x23b: case 0x1e08: case 0xa792:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200565 regmbc('C'); regmbc(0xc7);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200566 regmbc(0x106); regmbc(0x108); regmbc(0x10a);
567 regmbc(0x10c); regmbc(0x187); regmbc(0x23b);
568 regmbc(0x1e08); regmbc(0xa792);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200569 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200570 case 'D': case 0x10e: case 0x110: case 0x18a:
571 case 0x1e0a: case 0x1e0c: case 0x1e0e: case 0x1e10:
572 case 0x1e12:
573 regmbc('D'); regmbc(0x10e); regmbc(0x110);
574 regmbc(0x18a); regmbc(0x1e0a); regmbc(0x1e0c);
575 regmbc(0x1e0e); regmbc(0x1e10); regmbc(0x1e12);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200576 return;
577 case 'E': case 0xc8: case 0xc9: case 0xca: case 0xcb:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200578 case 0x112: case 0x114: case 0x116: case 0x118: case 0x11a:
579 case 0x204: case 0x206: case 0x228: case 0x246: case 0x1e14:
580 case 0x1e16: case 0x1e18: case 0x1e1a: case 0x1e1c:
581 case 0x1eb8: case 0x1eba: case 0x1ebc: case 0x1ebe:
582 case 0x1ec0: case 0x1ec2: case 0x1ec4: case 0x1ec6:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200583 regmbc('E'); regmbc(0xc8); regmbc(0xc9);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200584 regmbc(0xca); regmbc(0xcb); regmbc(0x112);
585 regmbc(0x114); regmbc(0x116); regmbc(0x118);
586 regmbc(0x11a); regmbc(0x204); regmbc(0x206);
587 regmbc(0x228); regmbc(0x246); regmbc(0x1e14);
588 regmbc(0x1e16); regmbc(0x1e18); regmbc(0x1e1a);
589 regmbc(0x1e1c); regmbc(0x1eb8); regmbc(0x1eba);
590 regmbc(0x1ebc); regmbc(0x1ebe); regmbc(0x1ec0);
591 regmbc(0x1ec2); regmbc(0x1ec4); regmbc(0x1ec6);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200592 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200593 case 'F': case 0x191: case 0x1e1e: case 0xa798:
594 regmbc('F'); regmbc(0x191); regmbc(0x1e1e);
595 regmbc(0xa798);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200596 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200597 case 'G': case 0x11c: case 0x11e: case 0x120:
598 case 0x122: case 0x193: case 0x1e4: case 0x1e6:
599 case 0x1f4: case 0x1e20: case 0xa7a0:
600 regmbc('G'); regmbc(0x11c); regmbc(0x11e);
601 regmbc(0x120); regmbc(0x122); regmbc(0x193);
602 regmbc(0x1e4); regmbc(0x1e6); regmbc(0x1f4);
603 regmbc(0x1e20); regmbc(0xa7a0);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200604 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200605 case 'H': case 0x124: case 0x126: case 0x21e:
606 case 0x1e22: case 0x1e24: case 0x1e26:
607 case 0x1e28: case 0x1e2a: case 0x2c67:
608 regmbc('H'); regmbc(0x124); regmbc(0x126);
609 regmbc(0x21e); regmbc(0x1e22); regmbc(0x1e24);
610 regmbc(0x1e26); regmbc(0x1e28); regmbc(0x1e2a);
611 regmbc(0x2c67);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200612 return;
613 case 'I': case 0xcc: case 0xcd: case 0xce: case 0xcf:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200614 case 0x128: case 0x12a: case 0x12c: case 0x12e:
615 case 0x130: case 0x197: case 0x1cf: case 0x208:
616 case 0x20a: case 0x1e2c: case 0x1e2e: case 0x1ec8:
617 case 0x1eca:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200618 regmbc('I'); regmbc(0xcc); regmbc(0xcd);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200619 regmbc(0xce); regmbc(0xcf); regmbc(0x128);
620 regmbc(0x12a); regmbc(0x12c); regmbc(0x12e);
621 regmbc(0x130); regmbc(0x197); regmbc(0x1cf);
622 regmbc(0x208); regmbc(0x20a); regmbc(0x1e2c);
623 regmbc(0x1e2e); regmbc(0x1ec8); regmbc(0x1eca);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200624 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200625 case 'J': case 0x134: case 0x248:
626 regmbc('J'); regmbc(0x134); regmbc(0x248);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200627 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200628 case 'K': case 0x136: case 0x198: case 0x1e8: case 0x1e30:
629 case 0x1e32: case 0x1e34: case 0x2c69: case 0xa740:
630 regmbc('K'); regmbc(0x136); regmbc(0x198);
631 regmbc(0x1e8); regmbc(0x1e30); regmbc(0x1e32);
632 regmbc(0x1e34); regmbc(0x2c69); regmbc(0xa740);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200633 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200634 case 'L': case 0x139: case 0x13b: case 0x13d: case 0x13f:
635 case 0x141: case 0x23d: case 0x1e36: case 0x1e38:
636 case 0x1e3a: case 0x1e3c: case 0x2c60:
637 regmbc('L'); regmbc(0x139); regmbc(0x13b);
638 regmbc(0x13d); regmbc(0x13f); regmbc(0x141);
639 regmbc(0x23d); regmbc(0x1e36); regmbc(0x1e38);
640 regmbc(0x1e3a); regmbc(0x1e3c); regmbc(0x2c60);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200641 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200642 case 'M': case 0x1e3e: case 0x1e40: case 0x1e42:
643 regmbc('M'); regmbc(0x1e3e); regmbc(0x1e40);
644 regmbc(0x1e42);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200645 return;
646 case 'N': case 0xd1:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200647 case 0x143: case 0x145: case 0x147: case 0x1f8:
648 case 0x1e44: case 0x1e46: case 0x1e48: case 0x1e4a:
649 case 0xa7a4:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200650 regmbc('N'); regmbc(0xd1);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200651 regmbc(0x143); regmbc(0x145); regmbc(0x147);
652 regmbc(0x1f8); regmbc(0x1e44); regmbc(0x1e46);
653 regmbc(0x1e48); regmbc(0x1e4a); regmbc(0xa7a4);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200654 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200655 case 'O': case 0xd2: case 0xd3: case 0xd4: case 0xd5: case 0xd6:
656 case 0xd8: case 0x14c: case 0x14e: case 0x150: case 0x19f:
657 case 0x1a0: case 0x1d1: case 0x1ea: case 0x1ec: case 0x1fe:
658 case 0x20c: case 0x20e: case 0x22a: case 0x22c: case 0x22e:
659 case 0x230: case 0x1e4c: case 0x1e4e: case 0x1e50: case 0x1e52:
660 case 0x1ecc: case 0x1ece: case 0x1ed0: case 0x1ed2: case 0x1ed4:
661 case 0x1ed6: case 0x1ed8: case 0x1eda: case 0x1edc: case 0x1ede:
662 case 0x1ee0: case 0x1ee2:
663 regmbc('O'); regmbc(0xd2); regmbc(0xd3); regmbc(0xd4);
664 regmbc(0xd5); regmbc(0xd6); regmbc(0xd8);
665 regmbc(0x14c); regmbc(0x14e); regmbc(0x150);
666 regmbc(0x19f); regmbc(0x1a0); regmbc(0x1d1);
667 regmbc(0x1ea); regmbc(0x1ec); regmbc(0x1fe);
668 regmbc(0x20c); regmbc(0x20e); regmbc(0x22a);
669 regmbc(0x22c); regmbc(0x22e); regmbc(0x230);
670 regmbc(0x1e4c); regmbc(0x1e4e); regmbc(0x1e50);
671 regmbc(0x1e52); regmbc(0x1ecc); regmbc(0x1ece);
672 regmbc(0x1ed0); regmbc(0x1ed2); regmbc(0x1ed4);
673 regmbc(0x1ed6); regmbc(0x1ed8); regmbc(0x1eda);
674 regmbc(0x1edc); regmbc(0x1ede); regmbc(0x1ee0);
675 regmbc(0x1ee2);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200676 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200677 case 'P': case 0x1a4: case 0x1e54: case 0x1e56: case 0x2c63:
678 regmbc('P'); regmbc(0x1a4); regmbc(0x1e54);
679 regmbc(0x1e56); regmbc(0x2c63);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200680 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200681 case 'Q': case 0x24a:
682 regmbc('Q'); regmbc(0x24a);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200683 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200684 case 'R': case 0x154: case 0x156: case 0x158: case 0x210:
685 case 0x212: case 0x24c: case 0x1e58: case 0x1e5a:
686 case 0x1e5c: case 0x1e5e: case 0x2c64: case 0xa7a6:
687 regmbc('R'); regmbc(0x154); regmbc(0x156);
688 regmbc(0x210); regmbc(0x212); regmbc(0x158);
689 regmbc(0x24c); regmbc(0x1e58); regmbc(0x1e5a);
690 regmbc(0x1e5c); regmbc(0x1e5e); regmbc(0x2c64);
691 regmbc(0xa7a6);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200692 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200693 case 'S': case 0x15a: case 0x15c: case 0x15e: case 0x160:
694 case 0x218: case 0x1e60: case 0x1e62: case 0x1e64:
695 case 0x1e66: case 0x1e68: case 0x2c7e: case 0xa7a8:
696 regmbc('S'); regmbc(0x15a); regmbc(0x15c);
697 regmbc(0x15e); regmbc(0x160); regmbc(0x218);
698 regmbc(0x1e60); regmbc(0x1e62); regmbc(0x1e64);
699 regmbc(0x1e66); regmbc(0x1e68); regmbc(0x2c7e);
700 regmbc(0xa7a8);
701 return;
702 case 'T': case 0x162: case 0x164: case 0x166: case 0x1ac:
703 case 0x1ae: case 0x21a: case 0x23e: case 0x1e6a: case 0x1e6c:
704 case 0x1e6e: case 0x1e70:
705 regmbc('T'); regmbc(0x162); regmbc(0x164);
706 regmbc(0x166); regmbc(0x1ac); regmbc(0x23e);
707 regmbc(0x1ae); regmbc(0x21a); regmbc(0x1e6a);
708 regmbc(0x1e6c); regmbc(0x1e6e); regmbc(0x1e70);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200709 return;
710 case 'U': case 0xd9: case 0xda: case 0xdb: case 0xdc:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200711 case 0x168: case 0x16a: case 0x16c: case 0x16e:
712 case 0x170: case 0x172: case 0x1af: case 0x1d3:
713 case 0x1d5: case 0x1d7: case 0x1d9: case 0x1db:
714 case 0x214: case 0x216: case 0x244: case 0x1e72:
715 case 0x1e74: case 0x1e76: case 0x1e78: case 0x1e7a:
716 case 0x1ee4: case 0x1ee6: case 0x1ee8: case 0x1eea:
717 case 0x1eec: case 0x1eee: case 0x1ef0:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200718 regmbc('U'); regmbc(0xd9); regmbc(0xda);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200719 regmbc(0xdb); regmbc(0xdc); regmbc(0x168);
720 regmbc(0x16a); regmbc(0x16c); regmbc(0x16e);
721 regmbc(0x170); regmbc(0x172); regmbc(0x1af);
722 regmbc(0x1d3); regmbc(0x1d5); regmbc(0x1d7);
723 regmbc(0x1d9); regmbc(0x1db); regmbc(0x214);
724 regmbc(0x216); regmbc(0x244); regmbc(0x1e72);
725 regmbc(0x1e74); regmbc(0x1e76); regmbc(0x1e78);
726 regmbc(0x1e7a); regmbc(0x1ee4); regmbc(0x1ee6);
727 regmbc(0x1ee8); regmbc(0x1eea); regmbc(0x1eec);
728 regmbc(0x1eee); regmbc(0x1ef0);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200729 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200730 case 'V': case 0x1b2: case 0x1e7c: case 0x1e7e:
731 regmbc('V'); regmbc(0x1b2); regmbc(0x1e7c);
732 regmbc(0x1e7e);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200733 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200734 case 'W': case 0x174: case 0x1e80: case 0x1e82:
735 case 0x1e84: case 0x1e86: case 0x1e88:
736 regmbc('W'); regmbc(0x174); regmbc(0x1e80);
737 regmbc(0x1e82); regmbc(0x1e84); regmbc(0x1e86);
738 regmbc(0x1e88);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200739 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200740 case 'X': case 0x1e8a: case 0x1e8c:
741 regmbc('X'); regmbc(0x1e8a); regmbc(0x1e8c);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200742 return;
743 case 'Y': case 0xdd:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200744 case 0x176: case 0x178: case 0x1b3: case 0x232: case 0x24e:
745 case 0x1e8e: case 0x1ef2: case 0x1ef6: case 0x1ef4: case 0x1ef8:
746 regmbc('Y'); regmbc(0xdd); regmbc(0x176);
747 regmbc(0x178); regmbc(0x1b3); regmbc(0x232);
748 regmbc(0x24e); regmbc(0x1e8e); regmbc(0x1ef2);
749 regmbc(0x1ef4); regmbc(0x1ef6); regmbc(0x1ef8);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200750 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200751 case 'Z': case 0x179: case 0x17b: case 0x17d: case 0x1b5:
752 case 0x1e90: case 0x1e92: case 0x1e94: case 0x2c6b:
753 regmbc('Z'); regmbc(0x179); regmbc(0x17b);
754 regmbc(0x17d); regmbc(0x1b5); regmbc(0x1e90);
755 regmbc(0x1e92); regmbc(0x1e94); regmbc(0x2c6b);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200756 return;
757 case 'a': case 0xe0: case 0xe1: case 0xe2:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200758 case 0xe3: case 0xe4: case 0xe5: case 0x101: case 0x103:
759 case 0x105: case 0x1ce: case 0x1df: case 0x1e1: case 0x1fb:
760 case 0x201: case 0x203: case 0x227: case 0x1d8f: case 0x1e01:
761 case 0x1e9a: case 0x1ea1: case 0x1ea3: case 0x1ea5:
762 case 0x1ea7: case 0x1ea9: case 0x1eab: case 0x1ead:
763 case 0x1eaf: case 0x1eb1: case 0x1eb3: case 0x1eb5:
764 case 0x1eb7: case 0x2c65:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200765 regmbc('a'); regmbc(0xe0); regmbc(0xe1);
766 regmbc(0xe2); regmbc(0xe3); regmbc(0xe4);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200767 regmbc(0xe5); regmbc(0x101); regmbc(0x103);
768 regmbc(0x105); regmbc(0x1ce); regmbc(0x1df);
769 regmbc(0x1e1); regmbc(0x1fb); regmbc(0x201);
770 regmbc(0x203); regmbc(0x227); regmbc(0x1d8f);
771 regmbc(0x1e01); regmbc(0x1e9a); regmbc(0x1ea1);
772 regmbc(0x1ea3); regmbc(0x1ea5); regmbc(0x1ea7);
773 regmbc(0x1ea9); regmbc(0x1eab); regmbc(0x1ead);
774 regmbc(0x1eaf); regmbc(0x1eb1); regmbc(0x1eb3);
775 regmbc(0x1eb5); regmbc(0x1eb7); regmbc(0x2c65);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200776 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200777 case 'b': case 0x180: case 0x253: case 0x1d6c: case 0x1d80:
778 case 0x1e03: case 0x1e05: case 0x1e07:
779 regmbc('b');
780 regmbc(0x180); regmbc(0x253); regmbc(0x1d6c);
781 regmbc(0x1d80); regmbc(0x1e03); regmbc(0x1e05);
782 regmbc(0x1e07);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200783 return;
784 case 'c': case 0xe7:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200785 case 0x107: case 0x109: case 0x10b: case 0x10d: case 0x188:
786 case 0x23c: case 0x1e09: case 0xa793: case 0xa794:
787 regmbc('c'); regmbc(0xe7); regmbc(0x107);
788 regmbc(0x109); regmbc(0x10b); regmbc(0x10d);
789 regmbc(0x188); regmbc(0x23c); regmbc(0x1e09);
790 regmbc(0xa793); regmbc(0xa794);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200791 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200792 case 'd': case 0x10f: case 0x111: case 0x257: case 0x1d6d:
793 case 0x1d81: case 0x1d91: case 0x1e0b: case 0x1e0d:
794 case 0x1e0f: case 0x1e11: case 0x1e13:
795 regmbc('d'); regmbc(0x10f); regmbc(0x111);
796 regmbc(0x257); regmbc(0x1d6d); regmbc(0x1d81);
797 regmbc(0x1d91); regmbc(0x1e0b); regmbc(0x1e0d);
798 regmbc(0x1e0f); regmbc(0x1e11); regmbc(0x1e13);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200799 return;
800 case 'e': case 0xe8: case 0xe9: case 0xea: case 0xeb:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200801 case 0x113: case 0x115: case 0x117: case 0x119:
802 case 0x11b: case 0x205: case 0x207: case 0x229:
803 case 0x247: case 0x1d92: case 0x1e15: case 0x1e17:
804 case 0x1e19: case 0x1e1b: case 0x1eb9: case 0x1ebb:
805 case 0x1e1d: case 0x1ebd: case 0x1ebf: case 0x1ec1:
806 case 0x1ec3: case 0x1ec5: case 0x1ec7:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200807 regmbc('e'); regmbc(0xe8); regmbc(0xe9);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200808 regmbc(0xea); regmbc(0xeb); regmbc(0x113);
809 regmbc(0x115); regmbc(0x117); regmbc(0x119);
810 regmbc(0x11b); regmbc(0x205); regmbc(0x207);
811 regmbc(0x229); regmbc(0x247); regmbc(0x1d92);
812 regmbc(0x1e15); regmbc(0x1e17); regmbc(0x1e19);
813 regmbc(0x1e1b); regmbc(0x1e1d); regmbc(0x1eb9);
814 regmbc(0x1ebb); regmbc(0x1ebd); regmbc(0x1ebf);
815 regmbc(0x1ec1); regmbc(0x1ec3); regmbc(0x1ec5);
816 regmbc(0x1ec7);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200817 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200818 case 'f': case 0x192: case 0x1d6e: case 0x1d82:
819 case 0x1e1f: case 0xa799:
820 regmbc('f'); regmbc(0x192); regmbc(0x1d6e);
821 regmbc(0x1d82); regmbc(0x1e1f); regmbc(0xa799);
822 return;
823 case 'g': case 0x11d: case 0x11f: case 0x121: case 0x123:
824 case 0x1e5: case 0x1e7: case 0x260: case 0x1f5: case 0x1d83:
825 case 0x1e21: case 0xa7a1:
826 regmbc('g'); regmbc(0x11d); regmbc(0x11f);
827 regmbc(0x121); regmbc(0x123); regmbc(0x1e5);
828 regmbc(0x1e7); regmbc(0x1f5); regmbc(0x260);
829 regmbc(0x1d83); regmbc(0x1e21); regmbc(0xa7a1);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200830 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200831 case 'h': case 0x125: case 0x127: case 0x21f: case 0x1e23:
832 case 0x1e25: case 0x1e27: case 0x1e29: case 0x1e2b:
833 case 0x1e96: case 0x2c68: case 0xa795:
834 regmbc('h'); regmbc(0x125); regmbc(0x127);
835 regmbc(0x21f); regmbc(0x1e23); regmbc(0x1e25);
836 regmbc(0x1e27); regmbc(0x1e29); regmbc(0x1e2b);
837 regmbc(0x1e96); regmbc(0x2c68); regmbc(0xa795);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200838 return;
839 case 'i': case 0xec: case 0xed: case 0xee: case 0xef:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200840 case 0x129: case 0x12b: case 0x12d: case 0x12f:
841 case 0x1d0: case 0x209: case 0x20b: case 0x268:
842 case 0x1d96: case 0x1e2d: case 0x1e2f: case 0x1ec9:
843 case 0x1ecb:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200844 regmbc('i'); regmbc(0xec); regmbc(0xed);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200845 regmbc(0xee); regmbc(0xef); regmbc(0x129);
846 regmbc(0x12b); regmbc(0x12d); regmbc(0x12f);
847 regmbc(0x1d0); regmbc(0x209); regmbc(0x20b);
848 regmbc(0x268); regmbc(0x1d96); regmbc(0x1e2d);
849 regmbc(0x1e2f); regmbc(0x1ec9); regmbc(0x1ecb);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200850 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200851 case 'j': case 0x135: case 0x1f0: case 0x249:
852 regmbc('j'); regmbc(0x135); regmbc(0x1f0);
853 regmbc(0x249);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200854 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200855 case 'k': case 0x137: case 0x199: case 0x1e9:
856 case 0x1d84: case 0x1e31: case 0x1e33: case 0x1e35:
857 case 0x2c6a: case 0xa741:
858 regmbc('k'); regmbc(0x137); regmbc(0x199);
859 regmbc(0x1e9); regmbc(0x1d84); regmbc(0x1e31);
860 regmbc(0x1e33); regmbc(0x1e35); regmbc(0x2c6a);
861 regmbc(0xa741);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200862 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200863 case 'l': case 0x13a: case 0x13c: case 0x13e:
864 case 0x140: case 0x142: case 0x19a: case 0x1e37:
865 case 0x1e39: case 0x1e3b: case 0x1e3d: case 0x2c61:
866 regmbc('l'); regmbc(0x13a); regmbc(0x13c);
867 regmbc(0x13e); regmbc(0x140); regmbc(0x142);
868 regmbc(0x19a); regmbc(0x1e37); regmbc(0x1e39);
869 regmbc(0x1e3b); regmbc(0x1e3d); regmbc(0x2c61);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200870 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200871 case 'm': case 0x1d6f: case 0x1e3f: case 0x1e41: case 0x1e43:
872 regmbc('m'); regmbc(0x1d6f); regmbc(0x1e3f);
873 regmbc(0x1e41); regmbc(0x1e43);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200874 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200875 case 'n': case 0xf1: case 0x144: case 0x146: case 0x148:
876 case 0x149: case 0x1f9: case 0x1d70: case 0x1d87:
877 case 0x1e45: case 0x1e47: case 0x1e49: case 0x1e4b:
878 case 0xa7a5:
879 regmbc('n'); regmbc(0xf1); regmbc(0x144);
880 regmbc(0x146); regmbc(0x148); regmbc(0x149);
881 regmbc(0x1f9); regmbc(0x1d70); regmbc(0x1d87);
882 regmbc(0x1e45); regmbc(0x1e47); regmbc(0x1e49);
883 regmbc(0x1e4b); regmbc(0xa7a5);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200884 return;
885 case 'o': case 0xf2: case 0xf3: case 0xf4: case 0xf5:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200886 case 0xf6: case 0xf8: case 0x14d: case 0x14f: case 0x151:
887 case 0x1a1: case 0x1d2: case 0x1eb: case 0x1ed: case 0x1ff:
888 case 0x20d: case 0x20f: case 0x22b: case 0x22d: case 0x22f:
889 case 0x231: case 0x275: case 0x1e4d: case 0x1e4f:
890 case 0x1e51: case 0x1e53: case 0x1ecd: case 0x1ecf:
891 case 0x1ed1: case 0x1ed3: case 0x1ed5: case 0x1ed7:
892 case 0x1ed9: case 0x1edb: case 0x1edd: case 0x1edf:
893 case 0x1ee1: case 0x1ee3:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200894 regmbc('o'); regmbc(0xf2); regmbc(0xf3);
895 regmbc(0xf4); regmbc(0xf5); regmbc(0xf6);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200896 regmbc(0xf8); regmbc(0x14d); regmbc(0x14f);
897 regmbc(0x151); regmbc(0x1a1); regmbc(0x1d2);
898 regmbc(0x1eb); regmbc(0x1ed); regmbc(0x1ff);
899 regmbc(0x20d); regmbc(0x20f); regmbc(0x22b);
900 regmbc(0x22d); regmbc(0x22f); regmbc(0x231);
901 regmbc(0x275); regmbc(0x1e4d); regmbc(0x1e4f);
902 regmbc(0x1e51); regmbc(0x1e53); regmbc(0x1ecd);
903 regmbc(0x1ecf); regmbc(0x1ed1); regmbc(0x1ed3);
904 regmbc(0x1ed5); regmbc(0x1ed7); regmbc(0x1ed9);
905 regmbc(0x1edb); regmbc(0x1edd); regmbc(0x1edf);
906 regmbc(0x1ee1); regmbc(0x1ee3);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200907 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200908 case 'p': case 0x1a5: case 0x1d71: case 0x1d88: case 0x1d7d:
909 case 0x1e55: case 0x1e57:
910 regmbc('p'); regmbc(0x1a5); regmbc(0x1d71);
911 regmbc(0x1d7d); regmbc(0x1d88); regmbc(0x1e55);
912 regmbc(0x1e57);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200913 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200914 case 'q': case 0x24b: case 0x2a0:
915 regmbc('q'); regmbc(0x24b); regmbc(0x2a0);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200916 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200917 case 'r': case 0x155: case 0x157: case 0x159: case 0x211:
918 case 0x213: case 0x24d: case 0x27d: case 0x1d72: case 0x1d73:
919 case 0x1d89: case 0x1e59: case 0x1e5b: case 0x1e5d: case 0x1e5f:
920 case 0xa7a7:
921 regmbc('r'); regmbc(0x155); regmbc(0x157);
922 regmbc(0x159); regmbc(0x211); regmbc(0x213);
923 regmbc(0x24d); regmbc(0x1d72); regmbc(0x1d73);
924 regmbc(0x1d89); regmbc(0x1e59); regmbc(0x27d);
925 regmbc(0x1e5b); regmbc(0x1e5d); regmbc(0x1e5f);
926 regmbc(0xa7a7);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200927 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200928 case 's': case 0x15b: case 0x15d: case 0x15f: case 0x161:
929 case 0x1e61: case 0x219: case 0x23f: case 0x1d74: case 0x1d8a:
930 case 0x1e63: case 0x1e65: case 0x1e67: case 0x1e69: case 0xa7a9:
931 regmbc('s'); regmbc(0x15b); regmbc(0x15d);
932 regmbc(0x15f); regmbc(0x161); regmbc(0x23f);
933 regmbc(0x219); regmbc(0x1d74); regmbc(0x1d8a);
934 regmbc(0x1e61); regmbc(0x1e63); regmbc(0x1e65);
935 regmbc(0x1e67); regmbc(0x1e69); regmbc(0xa7a9);
936 return;
937 case 't': case 0x163: case 0x165: case 0x167: case 0x1ab:
938 case 0x1ad: case 0x21b: case 0x288: case 0x1d75: case 0x1e6b:
939 case 0x1e6d: case 0x1e6f: case 0x1e71: case 0x1e97: case 0x2c66:
940 regmbc('t'); regmbc(0x163); regmbc(0x165);
941 regmbc(0x167); regmbc(0x1ab); regmbc(0x21b);
942 regmbc(0x1ad); regmbc(0x288); regmbc(0x1d75);
943 regmbc(0x1e6b); regmbc(0x1e6d); regmbc(0x1e6f);
944 regmbc(0x1e71); regmbc(0x1e97); regmbc(0x2c66);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200945 return;
946 case 'u': case 0xf9: case 0xfa: case 0xfb: case 0xfc:
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200947 case 0x169: case 0x16b: case 0x16d: case 0x16f:
948 case 0x171: case 0x173: case 0x1b0: case 0x1d4:
949 case 0x1d6: case 0x1d8: case 0x1da: case 0x1dc:
950 case 0x215: case 0x217: case 0x289: case 0x1e73:
951 case 0x1d7e: case 0x1d99: case 0x1e75: case 0x1e77:
952 case 0x1e79: case 0x1e7b: case 0x1ee5: case 0x1ee7:
953 case 0x1ee9: case 0x1eeb: case 0x1eed: case 0x1eef:
954 case 0x1ef1:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200955 regmbc('u'); regmbc(0xf9); regmbc(0xfa);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200956 regmbc(0xfb); regmbc(0xfc); regmbc(0x169);
957 regmbc(0x16b); regmbc(0x16d); regmbc(0x16f);
958 regmbc(0x171); regmbc(0x173); regmbc(0x1d6);
959 regmbc(0x1d8); regmbc(0x1da); regmbc(0x1dc);
960 regmbc(0x215); regmbc(0x217); regmbc(0x1b0);
961 regmbc(0x1d4); regmbc(0x289); regmbc(0x1d7e);
962 regmbc(0x1d99); regmbc(0x1e73); regmbc(0x1e75);
963 regmbc(0x1e77); regmbc(0x1e79); regmbc(0x1e7b);
964 regmbc(0x1ee5); regmbc(0x1ee7); regmbc(0x1ee9);
965 regmbc(0x1eeb); regmbc(0x1eed); regmbc(0x1eef);
966 regmbc(0x1ef1);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200967 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200968 case 'v': case 0x28b: case 0x1d8c: case 0x1e7d: case 0x1e7f:
969 regmbc('v'); regmbc(0x28b); regmbc(0x1d8c);
970 regmbc(0x1e7d); regmbc(0x1e7f);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200971 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200972 case 'w': case 0x175: case 0x1e81: case 0x1e83:
973 case 0x1e85: case 0x1e87: case 0x1e89: case 0x1e98:
974 regmbc('w'); regmbc(0x175); regmbc(0x1e81);
975 regmbc(0x1e83); regmbc(0x1e85); regmbc(0x1e87);
976 regmbc(0x1e89); regmbc(0x1e98);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200977 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200978 case 'x': case 0x1e8b: case 0x1e8d:
979 regmbc('x'); regmbc(0x1e8b); regmbc(0x1e8d);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200980 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200981 case 'y': case 0xfd: case 0xff: case 0x177: case 0x1b4:
982 case 0x233: case 0x24f: case 0x1e8f: case 0x1e99: case 0x1ef3:
983 case 0x1ef5: case 0x1ef7: case 0x1ef9:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200984 regmbc('y'); regmbc(0xfd); regmbc(0xff);
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200985 regmbc(0x177); regmbc(0x1b4); regmbc(0x233);
986 regmbc(0x24f); regmbc(0x1e8f); regmbc(0x1e99);
987 regmbc(0x1ef3); regmbc(0x1ef5); regmbc(0x1ef7);
988 regmbc(0x1ef9);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200989 return;
Bram Moolenaar0b94e292021-04-05 13:59:53 +0200990 case 'z': case 0x17a: case 0x17c: case 0x17e: case 0x1b6:
991 case 0x1d76: case 0x1d8e: case 0x1e91: case 0x1e93:
992 case 0x1e95: case 0x2c6c:
993 regmbc('z'); regmbc(0x17a); regmbc(0x17c);
994 regmbc(0x17e); regmbc(0x1b6); regmbc(0x1d76);
995 regmbc(0x1d8e); regmbc(0x1e91); regmbc(0x1e93);
996 regmbc(0x1e95); regmbc(0x2c6c);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200997 return;
998 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +0200999 }
1000 regmbc(c);
1001}
1002
1003/*
1004 * Emit a node.
1005 * Return pointer to generated code.
1006 */
1007 static char_u *
1008regnode(int op)
1009{
1010 char_u *ret;
1011
1012 ret = regcode;
1013 if (ret == JUST_CALC_SIZE)
1014 regsize += 3;
1015 else
1016 {
1017 *regcode++ = op;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001018 *regcode++ = NUL; // Null "next" pointer.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001019 *regcode++ = NUL;
1020 }
1021 return ret;
1022}
1023
1024/*
1025 * Write a long as four bytes at "p" and return pointer to the next char.
1026 */
1027 static char_u *
1028re_put_long(char_u *p, long_u val)
1029{
1030 *p++ = (char_u) ((val >> 24) & 0377);
1031 *p++ = (char_u) ((val >> 16) & 0377);
1032 *p++ = (char_u) ((val >> 8) & 0377);
1033 *p++ = (char_u) (val & 0377);
1034 return p;
1035}
1036
1037/*
1038 * regnext - dig the "next" pointer out of a node
1039 * Returns NULL when calculating size, when there is no next item and when
1040 * there is an error.
1041 */
1042 static char_u *
1043regnext(char_u *p)
1044{
1045 int offset;
1046
1047 if (p == JUST_CALC_SIZE || reg_toolong)
1048 return NULL;
1049
1050 offset = NEXT(p);
1051 if (offset == 0)
1052 return NULL;
1053
1054 if (OP(p) == BACK)
1055 return p - offset;
1056 else
1057 return p + offset;
1058}
1059
1060/*
1061 * Set the next-pointer at the end of a node chain.
1062 */
1063 static void
1064regtail(char_u *p, char_u *val)
1065{
1066 char_u *scan;
1067 char_u *temp;
1068 int offset;
1069
1070 if (p == JUST_CALC_SIZE)
1071 return;
1072
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001073 // Find last node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001074 scan = p;
1075 for (;;)
1076 {
1077 temp = regnext(scan);
1078 if (temp == NULL)
1079 break;
1080 scan = temp;
1081 }
1082
1083 if (OP(scan) == BACK)
1084 offset = (int)(scan - val);
1085 else
1086 offset = (int)(val - scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001087 // When the offset uses more than 16 bits it can no longer fit in the two
1088 // bytes available. Use a global flag to avoid having to check return
1089 // values in too many places.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001090 if (offset > 0xffff)
1091 reg_toolong = TRUE;
1092 else
1093 {
1094 *(scan + 1) = (char_u) (((unsigned)offset >> 8) & 0377);
1095 *(scan + 2) = (char_u) (offset & 0377);
1096 }
1097}
1098
1099/*
1100 * Like regtail, on item after a BRANCH; nop if none.
1101 */
1102 static void
1103regoptail(char_u *p, char_u *val)
1104{
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001105 // When op is neither BRANCH nor BRACE_COMPLEX0-9, it is "operandless"
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001106 if (p == NULL || p == JUST_CALC_SIZE
1107 || (OP(p) != BRANCH
1108 && (OP(p) < BRACE_COMPLEX || OP(p) > BRACE_COMPLEX + 9)))
1109 return;
1110 regtail(OPERAND(p), val);
1111}
1112
1113/*
1114 * Insert an operator in front of already-emitted operand
1115 *
1116 * Means relocating the operand.
1117 */
1118 static void
1119reginsert(int op, char_u *opnd)
1120{
1121 char_u *src;
1122 char_u *dst;
1123 char_u *place;
1124
1125 if (regcode == JUST_CALC_SIZE)
1126 {
1127 regsize += 3;
1128 return;
1129 }
1130 src = regcode;
1131 regcode += 3;
1132 dst = regcode;
1133 while (src > opnd)
1134 *--dst = *--src;
1135
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001136 place = opnd; // Op node, where operand used to be.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001137 *place++ = op;
1138 *place++ = NUL;
1139 *place = NUL;
1140}
1141
1142/*
1143 * Insert an operator in front of already-emitted operand.
1144 * Add a number to the operator.
1145 */
1146 static void
1147reginsert_nr(int op, long val, char_u *opnd)
1148{
1149 char_u *src;
1150 char_u *dst;
1151 char_u *place;
1152
1153 if (regcode == JUST_CALC_SIZE)
1154 {
1155 regsize += 7;
1156 return;
1157 }
1158 src = regcode;
1159 regcode += 7;
1160 dst = regcode;
1161 while (src > opnd)
1162 *--dst = *--src;
1163
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001164 place = opnd; // Op node, where operand used to be.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001165 *place++ = op;
1166 *place++ = NUL;
1167 *place++ = NUL;
1168 re_put_long(place, (long_u)val);
1169}
1170
1171/*
1172 * Insert an operator in front of already-emitted operand.
1173 * The operator has the given limit values as operands. Also set next pointer.
1174 *
1175 * Means relocating the operand.
1176 */
1177 static void
1178reginsert_limits(
1179 int op,
1180 long minval,
1181 long maxval,
1182 char_u *opnd)
1183{
1184 char_u *src;
1185 char_u *dst;
1186 char_u *place;
1187
1188 if (regcode == JUST_CALC_SIZE)
1189 {
1190 regsize += 11;
1191 return;
1192 }
1193 src = regcode;
1194 regcode += 11;
1195 dst = regcode;
1196 while (src > opnd)
1197 *--dst = *--src;
1198
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001199 place = opnd; // Op node, where operand used to be.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001200 *place++ = op;
1201 *place++ = NUL;
1202 *place++ = NUL;
1203 place = re_put_long(place, (long_u)minval);
1204 place = re_put_long(place, (long_u)maxval);
1205 regtail(opnd, place);
1206}
1207
1208/*
1209 * Return TRUE if the back reference is legal. We must have seen the close
1210 * brace.
1211 * TODO: Should also check that we don't refer to something that is repeated
1212 * (+*=): what instance of the repetition should we match?
1213 */
1214 static int
1215seen_endbrace(int refnum)
1216{
1217 if (!had_endbrace[refnum])
1218 {
1219 char_u *p;
1220
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001221 // Trick: check if "@<=" or "@<!" follows, in which case
1222 // the \1 can appear before the referenced match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001223 for (p = regparse; *p != NUL; ++p)
1224 if (p[0] == '@' && p[1] == '<' && (p[2] == '!' || p[2] == '='))
1225 break;
1226 if (*p == NUL)
1227 {
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001228 emsg(_(e_illegal_back_reference));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001229 rc_did_emsg = TRUE;
1230 return FALSE;
1231 }
1232 }
1233 return TRUE;
1234}
1235
1236/*
1237 * Parse the lowest level.
1238 *
1239 * Optimization: gobbles an entire sequence of ordinary characters so that
1240 * it can turn them into a single node, which is smaller to store and
1241 * faster to run. Don't do this when one_exactly is set.
1242 */
1243 static char_u *
1244regatom(int *flagp)
1245{
1246 char_u *ret;
1247 int flags;
1248 int c;
1249 char_u *p;
1250 int extra = 0;
1251 int save_prev_at_start = prev_at_start;
1252
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001253 *flagp = WORST; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001254
1255 c = getchr();
1256 switch (c)
1257 {
1258 case Magic('^'):
1259 ret = regnode(BOL);
1260 break;
1261
1262 case Magic('$'):
1263 ret = regnode(EOL);
1264#if defined(FEAT_SYN_HL) || defined(PROTO)
1265 had_eol = TRUE;
1266#endif
1267 break;
1268
1269 case Magic('<'):
1270 ret = regnode(BOW);
1271 break;
1272
1273 case Magic('>'):
1274 ret = regnode(EOW);
1275 break;
1276
1277 case Magic('_'):
1278 c = no_Magic(getchr());
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001279 if (c == '^') // "\_^" is start-of-line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001280 {
1281 ret = regnode(BOL);
1282 break;
1283 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001284 if (c == '$') // "\_$" is end-of-line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001285 {
1286 ret = regnode(EOL);
1287#if defined(FEAT_SYN_HL) || defined(PROTO)
1288 had_eol = TRUE;
1289#endif
1290 break;
1291 }
1292
1293 extra = ADD_NL;
1294 *flagp |= HASNL;
1295
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001296 // "\_[" is character range plus newline
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001297 if (c == '[')
1298 goto collection;
1299
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001300 // "\_x" is character class plus newline
1301 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001302
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001303 // Character classes.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001304 case Magic('.'):
1305 case Magic('i'):
1306 case Magic('I'):
1307 case Magic('k'):
1308 case Magic('K'):
1309 case Magic('f'):
1310 case Magic('F'):
1311 case Magic('p'):
1312 case Magic('P'):
1313 case Magic('s'):
1314 case Magic('S'):
1315 case Magic('d'):
1316 case Magic('D'):
1317 case Magic('x'):
1318 case Magic('X'):
1319 case Magic('o'):
1320 case Magic('O'):
1321 case Magic('w'):
1322 case Magic('W'):
1323 case Magic('h'):
1324 case Magic('H'):
1325 case Magic('a'):
1326 case Magic('A'):
1327 case Magic('l'):
1328 case Magic('L'):
1329 case Magic('u'):
1330 case Magic('U'):
1331 p = vim_strchr(classchars, no_Magic(c));
1332 if (p == NULL)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001333 EMSG_RET_NULL(_(e_invalid_use_of_underscore));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001334
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001335 // When '.' is followed by a composing char ignore the dot, so that
1336 // the composing char is matched here.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001337 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr()))
1338 {
1339 c = getchr();
1340 goto do_multibyte;
1341 }
1342 ret = regnode(classcodes[p - classchars] + extra);
1343 *flagp |= HASWIDTH | SIMPLE;
1344 break;
1345
1346 case Magic('n'):
1347 if (reg_string)
1348 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001349 // In a string "\n" matches a newline character.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001350 ret = regnode(EXACTLY);
1351 regc(NL);
1352 regc(NUL);
1353 *flagp |= HASWIDTH | SIMPLE;
1354 }
1355 else
1356 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001357 // In buffer text "\n" matches the end of a line.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001358 ret = regnode(NEWL);
1359 *flagp |= HASWIDTH | HASNL;
1360 }
1361 break;
1362
1363 case Magic('('):
1364 if (one_exactly)
1365 EMSG_ONE_RET_NULL;
1366 ret = reg(REG_PAREN, &flags);
1367 if (ret == NULL)
1368 return NULL;
1369 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1370 break;
1371
1372 case NUL:
1373 case Magic('|'):
1374 case Magic('&'):
1375 case Magic(')'):
1376 if (one_exactly)
1377 EMSG_ONE_RET_NULL;
Bram Moolenaard0819d12021-12-31 23:15:53 +00001378 // Supposed to be caught earlier.
1379 IEMSG_RET_NULL(_(e_internal_error_in_regexp));
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001380 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001381
1382 case Magic('='):
1383 case Magic('?'):
1384 case Magic('+'):
1385 case Magic('@'):
1386 case Magic('{'):
1387 case Magic('*'):
1388 c = no_Magic(c);
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001389 EMSG3_RET_NULL(_(e_str_chr_follows_nothing),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001390 (c == '*' ? reg_magic >= MAGIC_ON : reg_magic == MAGIC_ALL), c);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001391 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001392
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001393 case Magic('~'): // previous substitute pattern
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001394 if (reg_prev_sub != NULL)
1395 {
1396 char_u *lp;
1397
1398 ret = regnode(EXACTLY);
1399 lp = reg_prev_sub;
1400 while (*lp != NUL)
1401 regc(*lp++);
1402 regc(NUL);
1403 if (*reg_prev_sub != NUL)
1404 {
1405 *flagp |= HASWIDTH;
1406 if ((lp - reg_prev_sub) == 1)
1407 *flagp |= SIMPLE;
1408 }
1409 }
1410 else
Bram Moolenaare29a27f2021-07-20 21:07:36 +02001411 EMSG_RET_NULL(_(e_no_previous_substitute_regular_expression));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001412 break;
1413
1414 case Magic('1'):
1415 case Magic('2'):
1416 case Magic('3'):
1417 case Magic('4'):
1418 case Magic('5'):
1419 case Magic('6'):
1420 case Magic('7'):
1421 case Magic('8'):
1422 case Magic('9'):
1423 {
1424 int refnum;
1425
1426 refnum = c - Magic('0');
1427 if (!seen_endbrace(refnum))
1428 return NULL;
1429 ret = regnode(BACKREF + refnum);
1430 }
1431 break;
1432
1433 case Magic('z'):
1434 {
1435 c = no_Magic(getchr());
1436 switch (c)
1437 {
1438#ifdef FEAT_SYN_HL
1439 case '(': if ((reg_do_extmatch & REX_SET) == 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001440 EMSG_RET_NULL(_(e_z_not_allowed_here));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001441 if (one_exactly)
1442 EMSG_ONE_RET_NULL;
1443 ret = reg(REG_ZPAREN, &flags);
1444 if (ret == NULL)
1445 return NULL;
1446 *flagp |= flags & (HASWIDTH|SPSTART|HASNL|HASLOOKBH);
1447 re_has_z = REX_SET;
1448 break;
1449
1450 case '1':
1451 case '2':
1452 case '3':
1453 case '4':
1454 case '5':
1455 case '6':
1456 case '7':
1457 case '8':
1458 case '9': if ((reg_do_extmatch & REX_USE) == 0)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001459 EMSG_RET_NULL(_(e_z1_z9_not_allowed_here));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001460 ret = regnode(ZREF + c - '0');
1461 re_has_z = REX_USE;
1462 break;
1463#endif
1464
1465 case 's': ret = regnode(MOPEN + 0);
1466 if (re_mult_next("\\zs") == FAIL)
1467 return NULL;
1468 break;
1469
1470 case 'e': ret = regnode(MCLOSE + 0);
1471 if (re_mult_next("\\ze") == FAIL)
1472 return NULL;
1473 break;
1474
Bram Moolenaarb2810f12022-01-08 21:38:52 +00001475 default: EMSG_RET_NULL(_(e_invalid_character_after_bsl_z));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001476 }
1477 }
1478 break;
1479
1480 case Magic('%'):
1481 {
1482 c = no_Magic(getchr());
1483 switch (c)
1484 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001485 // () without a back reference
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001486 case '(':
1487 if (one_exactly)
1488 EMSG_ONE_RET_NULL;
1489 ret = reg(REG_NPAREN, &flags);
1490 if (ret == NULL)
1491 return NULL;
1492 *flagp |= flags & (HASWIDTH | SPSTART | HASNL | HASLOOKBH);
1493 break;
1494
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001495 // Catch \%^ and \%$ regardless of where they appear in the
1496 // pattern -- regardless of whether or not it makes sense.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001497 case '^':
1498 ret = regnode(RE_BOF);
1499 break;
1500
1501 case '$':
1502 ret = regnode(RE_EOF);
1503 break;
1504
1505 case '#':
Christian Brabandt360da402022-05-18 15:04:02 +01001506 if (regparse[0] == '=' && regparse[1] >= 48
1507 && regparse[1] <= 50)
1508 {
1509 // misplaced \%#=1
1510 semsg(_(e_atom_engine_must_be_at_start_of_pattern),
1511 regparse[1]);
1512 return FAIL;
1513 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001514 ret = regnode(CURSOR);
1515 break;
1516
1517 case 'V':
1518 ret = regnode(RE_VISUAL);
1519 break;
1520
1521 case 'C':
1522 ret = regnode(RE_COMPOSING);
1523 break;
1524
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001525 // \%[abc]: Emit as a list of branches, all ending at the last
1526 // branch which matches nothing.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001527 case '[':
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001528 if (one_exactly) // doesn't nest
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001529 EMSG_ONE_RET_NULL;
1530 {
1531 char_u *lastbranch;
1532 char_u *lastnode = NULL;
1533 char_u *br;
1534
1535 ret = NULL;
1536 while ((c = getchr()) != ']')
1537 {
1538 if (c == NUL)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001539 EMSG2_RET_NULL(_(e_missing_sb_after_str),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001540 reg_magic == MAGIC_ALL);
1541 br = regnode(BRANCH);
1542 if (ret == NULL)
1543 ret = br;
1544 else
1545 {
1546 regtail(lastnode, br);
1547 if (reg_toolong)
1548 return NULL;
1549 }
1550
1551 ungetchr();
1552 one_exactly = TRUE;
1553 lastnode = regatom(flagp);
1554 one_exactly = FALSE;
1555 if (lastnode == NULL)
1556 return NULL;
1557 }
1558 if (ret == NULL)
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001559 EMSG2_RET_NULL(_(e_empty_str_brackets),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001560 reg_magic == MAGIC_ALL);
1561 lastbranch = regnode(BRANCH);
1562 br = regnode(NOTHING);
1563 if (ret != JUST_CALC_SIZE)
1564 {
1565 regtail(lastnode, br);
1566 regtail(lastbranch, br);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001567 // connect all branches to the NOTHING
1568 // branch at the end
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001569 for (br = ret; br != lastnode; )
1570 {
1571 if (OP(br) == BRANCH)
1572 {
1573 regtail(br, lastbranch);
1574 if (reg_toolong)
1575 return NULL;
1576 br = OPERAND(br);
1577 }
1578 else
1579 br = regnext(br);
1580 }
1581 }
1582 *flagp &= ~(HASWIDTH | SIMPLE);
1583 break;
1584 }
1585
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001586 case 'd': // %d123 decimal
1587 case 'o': // %o123 octal
1588 case 'x': // %xab hex 2
1589 case 'u': // %uabcd hex 4
1590 case 'U': // %U1234abcd hex 8
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001591 {
1592 long i;
1593
1594 switch (c)
1595 {
1596 case 'd': i = getdecchrs(); break;
1597 case 'o': i = getoctchrs(); break;
1598 case 'x': i = gethexchrs(2); break;
1599 case 'u': i = gethexchrs(4); break;
1600 case 'U': i = gethexchrs(8); break;
1601 default: i = -1; break;
1602 }
1603
1604 if (i < 0 || i > INT_MAX)
1605 EMSG2_RET_NULL(
Bram Moolenaara6f79292022-01-04 21:30:47 +00001606 _(e_invalid_character_after_str_2),
1607 reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001608 if (use_multibytecode(i))
1609 ret = regnode(MULTIBYTECODE);
1610 else
1611 ret = regnode(EXACTLY);
1612 if (i == 0)
1613 regc(0x0a);
1614 else
1615 regmbc(i);
1616 regc(NUL);
1617 *flagp |= HASWIDTH;
1618 break;
1619 }
1620
1621 default:
1622 if (VIM_ISDIGIT(c) || c == '<' || c == '>'
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001623 || c == '\'' || c == '.')
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001624 {
1625 long_u n = 0;
1626 int cmp;
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001627 int cur = FALSE;
Bram Moolenaar72bb10d2022-04-05 14:00:32 +01001628 int got_digit = FALSE;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001629
1630 cmp = c;
1631 if (cmp == '<' || cmp == '>')
1632 c = getchr();
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001633 if (no_Magic(c) == '.')
1634 {
1635 cur = TRUE;
1636 c = getchr();
1637 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001638 while (VIM_ISDIGIT(c))
1639 {
Bram Moolenaar72bb10d2022-04-05 14:00:32 +01001640 got_digit = TRUE;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001641 n = n * 10 + (c - '0');
1642 c = getchr();
1643 }
1644 if (c == '\'' && n == 0)
1645 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001646 // "\%'m", "\%<'m" and "\%>'m": Mark
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001647 c = getchr();
1648 ret = regnode(RE_MARK);
1649 if (ret == JUST_CALC_SIZE)
1650 regsize += 2;
1651 else
1652 {
1653 *regcode++ = c;
1654 *regcode++ = cmp;
1655 }
1656 break;
1657 }
Bram Moolenaar72bb10d2022-04-05 14:00:32 +01001658 else if ((c == 'l' || c == 'c' || c == 'v')
1659 && (cur || got_digit))
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001660 {
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001661 if (cur && n)
1662 {
Bram Moolenaar91ff3d42022-04-04 18:32:32 +01001663 semsg(_(e_regexp_number_after_dot_pos_search_chr),
1664 no_Magic(c));
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001665 rc_did_emsg = TRUE;
1666 return NULL;
1667 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001668 if (c == 'l')
1669 {
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001670 if (cur)
1671 n = curwin->w_cursor.lnum;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001672 ret = regnode(RE_LNUM);
1673 if (save_prev_at_start)
1674 at_start = TRUE;
1675 }
1676 else if (c == 'c')
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001677 {
1678 if (cur)
1679 {
1680 n = curwin->w_cursor.col;
1681 n++;
1682 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001683 ret = regnode(RE_COL);
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001684 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001685 else
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001686 {
1687 if (cur)
1688 {
1689 colnr_T vcol = 0;
1690
1691 getvvcol(curwin, &curwin->w_cursor,
1692 NULL, NULL, &vcol);
1693 ++vcol;
1694 n = vcol;
1695 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001696 ret = regnode(RE_VCOL);
Bram Moolenaar04db26b2021-07-05 20:15:23 +02001697 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001698 if (ret == JUST_CALC_SIZE)
1699 regsize += 5;
1700 else
1701 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001702 // put the number and the optional
1703 // comparator after the opcode
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001704 regcode = re_put_long(regcode, n);
1705 *regcode++ = cmp;
1706 }
1707 break;
1708 }
1709 }
1710
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001711 EMSG2_RET_NULL(_(e_invalid_character_after_str),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001712 reg_magic == MAGIC_ALL);
1713 }
1714 }
1715 break;
1716
1717 case Magic('['):
1718collection:
1719 {
1720 char_u *lp;
1721
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001722 // If there is no matching ']', we assume the '[' is a normal
1723 // character. This makes 'incsearch' and ":help [" work.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001724 lp = skip_anyof(regparse);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001725 if (*lp == ']') // there is a matching ']'
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001726 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001727 int startc = -1; // > 0 when next '-' is a range
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001728 int endc;
1729
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001730 // In a character class, different parsing rules apply.
1731 // Not even \ is special anymore, nothing is.
1732 if (*regparse == '^') // Complement of range.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001733 {
1734 ret = regnode(ANYBUT + extra);
1735 regparse++;
1736 }
1737 else
1738 ret = regnode(ANYOF + extra);
1739
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001740 // At the start ']' and '-' mean the literal character.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001741 if (*regparse == ']' || *regparse == '-')
1742 {
1743 startc = *regparse;
1744 regc(*regparse++);
1745 }
1746
1747 while (*regparse != NUL && *regparse != ']')
1748 {
1749 if (*regparse == '-')
1750 {
1751 ++regparse;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001752 // The '-' is not used for a range at the end and
1753 // after or before a '\n'.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001754 if (*regparse == ']' || *regparse == NUL
1755 || startc == -1
1756 || (regparse[0] == '\\' && regparse[1] == 'n'))
1757 {
1758 regc('-');
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001759 startc = '-'; // [--x] is a range
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001760 }
1761 else
1762 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001763 // Also accept "a-[.z.]"
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001764 endc = 0;
1765 if (*regparse == '[')
1766 endc = get_coll_element(&regparse);
1767 if (endc == 0)
1768 {
1769 if (has_mbyte)
1770 endc = mb_ptr2char_adv(&regparse);
1771 else
1772 endc = *regparse++;
1773 }
1774
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001775 // Handle \o40, \x20 and \u20AC style sequences
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001776 if (endc == '\\' && !reg_cpo_lit && !reg_cpo_bsl)
1777 endc = coll_get_char();
1778
1779 if (startc > endc)
Bram Moolenaar677658a2022-01-05 16:09:06 +00001780 EMSG_RET_NULL(_(e_reverse_range_in_character_class));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001781 if (has_mbyte && ((*mb_char2len)(startc) > 1
1782 || (*mb_char2len)(endc) > 1))
1783 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001784 // Limit to a range of 256 chars.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001785 if (endc > startc + 256)
Bram Moolenaar677658a2022-01-05 16:09:06 +00001786 EMSG_RET_NULL(_(e_range_too_large_in_character_class));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001787 while (++startc <= endc)
1788 regmbc(startc);
1789 }
1790 else
1791 {
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001792 while (++startc <= endc)
Bram Moolenaar424bcae2022-01-31 14:59:41 +00001793 regc(startc);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001794 }
1795 startc = -1;
1796 }
1797 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001798 // Only "\]", "\^", "\]" and "\\" are special in Vi. Vim
1799 // accepts "\t", "\e", etc., but only when the 'l' flag in
1800 // 'cpoptions' is not included.
1801 // Posix doesn't recognize backslash at all.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001802 else if (*regparse == '\\'
1803 && !reg_cpo_bsl
1804 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL
1805 || (!reg_cpo_lit
1806 && vim_strchr(REGEXP_ABBR,
1807 regparse[1]) != NULL)))
1808 {
1809 regparse++;
1810 if (*regparse == 'n')
1811 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001812 // '\n' in range: also match NL
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001813 if (ret != JUST_CALC_SIZE)
1814 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001815 // Using \n inside [^] does not change what
1816 // matches. "[^\n]" is the same as ".".
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001817 if (*ret == ANYOF)
1818 {
1819 *ret = ANYOF + ADD_NL;
1820 *flagp |= HASNL;
1821 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001822 // else: must have had a \n already
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001823 }
1824 regparse++;
1825 startc = -1;
1826 }
1827 else if (*regparse == 'd'
1828 || *regparse == 'o'
1829 || *regparse == 'x'
1830 || *regparse == 'u'
1831 || *regparse == 'U')
1832 {
1833 startc = coll_get_char();
1834 if (startc == 0)
1835 regc(0x0a);
1836 else
1837 regmbc(startc);
1838 }
1839 else
1840 {
1841 startc = backslash_trans(*regparse++);
1842 regc(startc);
1843 }
1844 }
1845 else if (*regparse == '[')
1846 {
1847 int c_class;
1848 int cu;
1849
1850 c_class = get_char_class(&regparse);
1851 startc = -1;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001852 // Characters assumed to be 8 bits!
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001853 switch (c_class)
1854 {
1855 case CLASS_NONE:
1856 c_class = get_equi_class(&regparse);
1857 if (c_class != 0)
1858 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001859 // produce equivalence class
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001860 reg_equi_class(c_class);
1861 }
1862 else if ((c_class =
1863 get_coll_element(&regparse)) != 0)
1864 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001865 // produce a collating element
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001866 regmbc(c_class);
1867 }
1868 else
1869 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001870 // literal '[', allow [[-x] as a range
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001871 startc = *regparse++;
1872 regc(startc);
1873 }
1874 break;
1875 case CLASS_ALNUM:
1876 for (cu = 1; cu < 128; cu++)
1877 if (isalnum(cu))
1878 regmbc(cu);
1879 break;
1880 case CLASS_ALPHA:
1881 for (cu = 1; cu < 128; cu++)
1882 if (isalpha(cu))
1883 regmbc(cu);
1884 break;
1885 case CLASS_BLANK:
1886 regc(' ');
1887 regc('\t');
1888 break;
1889 case CLASS_CNTRL:
1890 for (cu = 1; cu <= 127; cu++)
1891 if (iscntrl(cu))
1892 regmbc(cu);
1893 break;
1894 case CLASS_DIGIT:
1895 for (cu = 1; cu <= 127; cu++)
1896 if (VIM_ISDIGIT(cu))
1897 regmbc(cu);
1898 break;
1899 case CLASS_GRAPH:
1900 for (cu = 1; cu <= 127; cu++)
1901 if (isgraph(cu))
1902 regmbc(cu);
1903 break;
1904 case CLASS_LOWER:
1905 for (cu = 1; cu <= 255; cu++)
1906 if (MB_ISLOWER(cu) && cu != 170
1907 && cu != 186)
1908 regmbc(cu);
1909 break;
1910 case CLASS_PRINT:
1911 for (cu = 1; cu <= 255; cu++)
1912 if (vim_isprintc(cu))
1913 regmbc(cu);
1914 break;
1915 case CLASS_PUNCT:
1916 for (cu = 1; cu < 128; cu++)
1917 if (ispunct(cu))
1918 regmbc(cu);
1919 break;
1920 case CLASS_SPACE:
1921 for (cu = 9; cu <= 13; cu++)
1922 regc(cu);
1923 regc(' ');
1924 break;
1925 case CLASS_UPPER:
1926 for (cu = 1; cu <= 255; cu++)
1927 if (MB_ISUPPER(cu))
1928 regmbc(cu);
1929 break;
1930 case CLASS_XDIGIT:
1931 for (cu = 1; cu <= 255; cu++)
1932 if (vim_isxdigit(cu))
1933 regmbc(cu);
1934 break;
1935 case CLASS_TAB:
1936 regc('\t');
1937 break;
1938 case CLASS_RETURN:
1939 regc('\r');
1940 break;
1941 case CLASS_BACKSPACE:
1942 regc('\b');
1943 break;
1944 case CLASS_ESCAPE:
1945 regc('\033');
1946 break;
1947 case CLASS_IDENT:
1948 for (cu = 1; cu <= 255; cu++)
1949 if (vim_isIDc(cu))
1950 regmbc(cu);
1951 break;
1952 case CLASS_KEYWORD:
1953 for (cu = 1; cu <= 255; cu++)
1954 if (reg_iswordc(cu))
1955 regmbc(cu);
1956 break;
1957 case CLASS_FNAME:
1958 for (cu = 1; cu <= 255; cu++)
1959 if (vim_isfilec(cu))
1960 regmbc(cu);
1961 break;
1962 }
1963 }
1964 else
1965 {
1966 if (has_mbyte)
1967 {
1968 int len;
1969
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001970 // produce a multibyte character, including any
1971 // following composing characters
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001972 startc = mb_ptr2char(regparse);
1973 len = (*mb_ptr2len)(regparse);
1974 if (enc_utf8 && utf_char2len(startc) != len)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001975 startc = -1; // composing chars
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001976 while (--len >= 0)
1977 regc(*regparse++);
1978 }
1979 else
1980 {
1981 startc = *regparse++;
1982 regc(startc);
1983 }
1984 }
1985 }
1986 regc(NUL);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001987 prevchr_len = 1; // last char was the ']'
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001988 if (*regparse != ']')
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00001989 EMSG_RET_NULL(_(e_too_many_brackets)); // Cannot happen?
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001990 skipchr(); // let's be friends with the lexer again
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001991 *flagp |= HASWIDTH | SIMPLE;
1992 break;
1993 }
1994 else if (reg_strict)
Bram Moolenaar677658a2022-01-05 16:09:06 +00001995 EMSG2_RET_NULL(_(e_missing_rsb_after_str_lsb),
1996 reg_magic > MAGIC_OFF);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001997 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02001998 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02001999
2000 default:
2001 {
2002 int len;
2003
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002004 // A multi-byte character is handled as a separate atom if it's
2005 // before a multi and when it's a composing char.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002006 if (use_multibytecode(c))
2007 {
2008do_multibyte:
2009 ret = regnode(MULTIBYTECODE);
2010 regmbc(c);
2011 *flagp |= HASWIDTH | SIMPLE;
2012 break;
2013 }
2014
2015 ret = regnode(EXACTLY);
2016
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002017 // Append characters as long as:
2018 // - there is no following multi, we then need the character in
2019 // front of it as a single character operand
2020 // - not running into a Magic character
2021 // - "one_exactly" is not set
2022 // But always emit at least one character. Might be a Multi,
2023 // e.g., a "[" without matching "]".
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002024 for (len = 0; c != NUL && (len == 0
2025 || (re_multi_type(peekchr()) == NOT_MULTI
2026 && !one_exactly
2027 && !is_Magic(c))); ++len)
2028 {
2029 c = no_Magic(c);
2030 if (has_mbyte)
2031 {
2032 regmbc(c);
2033 if (enc_utf8)
2034 {
2035 int l;
2036
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002037 // Need to get composing character too.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002038 for (;;)
2039 {
2040 l = utf_ptr2len(regparse);
2041 if (!UTF_COMPOSINGLIKE(regparse, regparse + l))
2042 break;
2043 regmbc(utf_ptr2char(regparse));
2044 skipchr();
2045 }
2046 }
2047 }
2048 else
2049 regc(c);
2050 c = getchr();
2051 }
2052 ungetchr();
2053
2054 regc(NUL);
2055 *flagp |= HASWIDTH;
2056 if (len == 1)
2057 *flagp |= SIMPLE;
2058 }
2059 break;
2060 }
2061
2062 return ret;
2063}
2064
2065/*
2066 * Parse something followed by possible [*+=].
2067 *
2068 * Note that the branching code sequences used for = and the general cases
2069 * of * and + are somewhat optimized: they use the same NOTHING node as
2070 * both the endmarker for their branch list and the body of the last branch.
2071 * It might seem that this node could be dispensed with entirely, but the
2072 * endmarker role is not redundant.
2073 */
2074 static char_u *
2075regpiece(int *flagp)
2076{
2077 char_u *ret;
2078 int op;
2079 char_u *next;
2080 int flags;
2081 long minval;
2082 long maxval;
2083
2084 ret = regatom(&flags);
2085 if (ret == NULL)
2086 return NULL;
2087
2088 op = peekchr();
2089 if (re_multi_type(op) == NOT_MULTI)
2090 {
2091 *flagp = flags;
2092 return ret;
2093 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002094 // default flags
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002095 *flagp = (WORST | SPSTART | (flags & (HASNL | HASLOOKBH)));
2096
2097 skipchr();
2098 switch (op)
2099 {
2100 case Magic('*'):
2101 if (flags & SIMPLE)
2102 reginsert(STAR, ret);
2103 else
2104 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002105 // Emit x* as (x&|), where & means "self".
2106 reginsert(BRANCH, ret); // Either x
2107 regoptail(ret, regnode(BACK)); // and loop
2108 regoptail(ret, ret); // back
2109 regtail(ret, regnode(BRANCH)); // or
2110 regtail(ret, regnode(NOTHING)); // null.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002111 }
2112 break;
2113
2114 case Magic('+'):
2115 if (flags & SIMPLE)
2116 reginsert(PLUS, ret);
2117 else
2118 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002119 // Emit x+ as x(&|), where & means "self".
2120 next = regnode(BRANCH); // Either
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002121 regtail(ret, next);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002122 regtail(regnode(BACK), ret); // loop back
2123 regtail(next, regnode(BRANCH)); // or
2124 regtail(ret, regnode(NOTHING)); // null.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002125 }
2126 *flagp = (WORST | HASWIDTH | (flags & (HASNL | HASLOOKBH)));
2127 break;
2128
2129 case Magic('@'):
2130 {
2131 int lop = END;
2132 long nr;
2133
2134 nr = getdecchrs();
2135 switch (no_Magic(getchr()))
2136 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002137 case '=': lop = MATCH; break; // \@=
2138 case '!': lop = NOMATCH; break; // \@!
2139 case '>': lop = SUBPAT; break; // \@>
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002140 case '<': switch (no_Magic(getchr()))
2141 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002142 case '=': lop = BEHIND; break; // \@<=
2143 case '!': lop = NOBEHIND; break; // \@<!
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002144 }
2145 }
2146 if (lop == END)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002147 EMSG2_RET_NULL(_(e_invalid_character_after_str_at),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002148 reg_magic == MAGIC_ALL);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002149 // Look behind must match with behind_pos.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002150 if (lop == BEHIND || lop == NOBEHIND)
2151 {
2152 regtail(ret, regnode(BHPOS));
2153 *flagp |= HASLOOKBH;
2154 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002155 regtail(ret, regnode(END)); // operand ends
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002156 if (lop == BEHIND || lop == NOBEHIND)
2157 {
2158 if (nr < 0)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002159 nr = 0; // no limit is same as zero limit
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002160 reginsert_nr(lop, nr, ret);
2161 }
2162 else
2163 reginsert(lop, ret);
2164 break;
2165 }
2166
2167 case Magic('?'):
2168 case Magic('='):
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002169 // Emit x= as (x|)
2170 reginsert(BRANCH, ret); // Either x
2171 regtail(ret, regnode(BRANCH)); // or
2172 next = regnode(NOTHING); // null.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002173 regtail(ret, next);
2174 regoptail(ret, next);
2175 break;
2176
2177 case Magic('{'):
2178 if (!read_limits(&minval, &maxval))
2179 return NULL;
2180 if (flags & SIMPLE)
2181 {
2182 reginsert(BRACE_SIMPLE, ret);
2183 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
2184 }
2185 else
2186 {
2187 if (num_complex_braces >= 10)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002188 EMSG2_RET_NULL(_(e_too_many_complex_str_curly),
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002189 reg_magic == MAGIC_ALL);
2190 reginsert(BRACE_COMPLEX + num_complex_braces, ret);
2191 regoptail(ret, regnode(BACK));
2192 regoptail(ret, ret);
2193 reginsert_limits(BRACE_LIMITS, minval, maxval, ret);
2194 ++num_complex_braces;
2195 }
2196 if (minval > 0 && maxval > 0)
2197 *flagp = (HASWIDTH | (flags & (HASNL | HASLOOKBH)));
2198 break;
2199 }
2200 if (re_multi_type(peekchr()) != NOT_MULTI)
2201 {
2202 // Can't have a multi follow a multi.
2203 if (peekchr() == Magic('*'))
Bram Moolenaar12f3c1b2021-12-05 21:46:34 +00002204 EMSG2_RET_NULL(_(e_nested_str), reg_magic >= MAGIC_ON);
2205 EMSG3_RET_NULL(_(e_nested_str_chr), reg_magic == MAGIC_ALL,
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002206 no_Magic(peekchr()));
2207 }
2208
2209 return ret;
2210}
2211
2212/*
2213 * Parse one alternative of an | or & operator.
2214 * Implements the concatenation operator.
2215 */
2216 static char_u *
2217regconcat(int *flagp)
2218{
2219 char_u *first = NULL;
2220 char_u *chain = NULL;
2221 char_u *latest;
2222 int flags;
2223 int cont = TRUE;
2224
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002225 *flagp = WORST; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002226
2227 while (cont)
2228 {
2229 switch (peekchr())
2230 {
2231 case NUL:
2232 case Magic('|'):
2233 case Magic('&'):
2234 case Magic(')'):
2235 cont = FALSE;
2236 break;
2237 case Magic('Z'):
2238 regflags |= RF_ICOMBINE;
2239 skipchr_keepstart();
2240 break;
2241 case Magic('c'):
2242 regflags |= RF_ICASE;
2243 skipchr_keepstart();
2244 break;
2245 case Magic('C'):
2246 regflags |= RF_NOICASE;
2247 skipchr_keepstart();
2248 break;
2249 case Magic('v'):
2250 reg_magic = MAGIC_ALL;
2251 skipchr_keepstart();
2252 curchr = -1;
2253 break;
2254 case Magic('m'):
2255 reg_magic = MAGIC_ON;
2256 skipchr_keepstart();
2257 curchr = -1;
2258 break;
2259 case Magic('M'):
2260 reg_magic = MAGIC_OFF;
2261 skipchr_keepstart();
2262 curchr = -1;
2263 break;
2264 case Magic('V'):
2265 reg_magic = MAGIC_NONE;
2266 skipchr_keepstart();
2267 curchr = -1;
2268 break;
2269 default:
2270 latest = regpiece(&flags);
2271 if (latest == NULL || reg_toolong)
2272 return NULL;
2273 *flagp |= flags & (HASWIDTH | HASNL | HASLOOKBH);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002274 if (chain == NULL) // First piece.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002275 *flagp |= flags & SPSTART;
2276 else
2277 regtail(chain, latest);
2278 chain = latest;
2279 if (first == NULL)
2280 first = latest;
2281 break;
2282 }
2283 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002284 if (first == NULL) // Loop ran zero times.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002285 first = regnode(NOTHING);
2286 return first;
2287}
2288
2289/*
2290 * Parse one alternative of an | operator.
2291 * Implements the & operator.
2292 */
2293 static char_u *
2294regbranch(int *flagp)
2295{
2296 char_u *ret;
2297 char_u *chain = NULL;
2298 char_u *latest;
2299 int flags;
2300
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002301 *flagp = WORST | HASNL; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002302
2303 ret = regnode(BRANCH);
2304 for (;;)
2305 {
2306 latest = regconcat(&flags);
2307 if (latest == NULL)
2308 return NULL;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002309 // If one of the branches has width, the whole thing has. If one of
2310 // the branches anchors at start-of-line, the whole thing does.
2311 // If one of the branches uses look-behind, the whole thing does.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002312 *flagp |= flags & (HASWIDTH | SPSTART | HASLOOKBH);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002313 // If one of the branches doesn't match a line-break, the whole thing
2314 // doesn't.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002315 *flagp &= ~HASNL | (flags & HASNL);
2316 if (chain != NULL)
2317 regtail(chain, latest);
2318 if (peekchr() != Magic('&'))
2319 break;
2320 skipchr();
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002321 regtail(latest, regnode(END)); // operand ends
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002322 if (reg_toolong)
2323 break;
2324 reginsert(MATCH, latest);
2325 chain = latest;
2326 }
2327
2328 return ret;
2329}
2330
2331/*
2332 * Parse regular expression, i.e. main body or parenthesized thing.
2333 *
2334 * Caller must absorb opening parenthesis.
2335 *
2336 * Combining parenthesis handling with the base level of regular expression
2337 * is a trifle forced, but the need to tie the tails of the branches to what
2338 * follows makes it hard to avoid.
2339 */
2340 static char_u *
2341reg(
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002342 int paren, // REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002343 int *flagp)
2344{
2345 char_u *ret;
2346 char_u *br;
2347 char_u *ender;
2348 int parno = 0;
2349 int flags;
2350
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002351 *flagp = HASWIDTH; // Tentatively.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002352
2353#ifdef FEAT_SYN_HL
2354 if (paren == REG_ZPAREN)
2355 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002356 // Make a ZOPEN node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002357 if (regnzpar >= NSUBEXP)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002358 EMSG_RET_NULL(_(e_too_many_z));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002359 parno = regnzpar;
2360 regnzpar++;
2361 ret = regnode(ZOPEN + parno);
2362 }
2363 else
2364#endif
2365 if (paren == REG_PAREN)
2366 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002367 // Make a MOPEN node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002368 if (regnpar >= NSUBEXP)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002369 EMSG2_RET_NULL(_(e_too_many_str_open), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002370 parno = regnpar;
2371 ++regnpar;
2372 ret = regnode(MOPEN + parno);
2373 }
2374 else if (paren == REG_NPAREN)
2375 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002376 // Make a NOPEN node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002377 ret = regnode(NOPEN);
2378 }
2379 else
2380 ret = NULL;
2381
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002382 // Pick up the branches, linking them together.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002383 br = regbranch(&flags);
2384 if (br == NULL)
2385 return NULL;
2386 if (ret != NULL)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002387 regtail(ret, br); // [MZ]OPEN -> first.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002388 else
2389 ret = br;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002390 // If one of the branches can be zero-width, the whole thing can.
2391 // If one of the branches has * at start or matches a line-break, the
2392 // whole thing can.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002393 if (!(flags & HASWIDTH))
2394 *flagp &= ~HASWIDTH;
2395 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
2396 while (peekchr() == Magic('|'))
2397 {
2398 skipchr();
2399 br = regbranch(&flags);
2400 if (br == NULL || reg_toolong)
2401 return NULL;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002402 regtail(ret, br); // BRANCH -> BRANCH.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002403 if (!(flags & HASWIDTH))
2404 *flagp &= ~HASWIDTH;
2405 *flagp |= flags & (SPSTART | HASNL | HASLOOKBH);
2406 }
2407
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002408 // Make a closing node, and hook it on the end.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002409 ender = regnode(
2410#ifdef FEAT_SYN_HL
2411 paren == REG_ZPAREN ? ZCLOSE + parno :
2412#endif
2413 paren == REG_PAREN ? MCLOSE + parno :
2414 paren == REG_NPAREN ? NCLOSE : END);
2415 regtail(ret, ender);
2416
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002417 // Hook the tails of the branches to the closing node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002418 for (br = ret; br != NULL; br = regnext(br))
2419 regoptail(br, ender);
2420
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002421 // Check for proper termination.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002422 if (paren != REG_NOPAREN && getchr() != Magic(')'))
2423 {
2424#ifdef FEAT_SYN_HL
2425 if (paren == REG_ZPAREN)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002426 EMSG_RET_NULL(_(e_unmatched_z));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002427 else
2428#endif
2429 if (paren == REG_NPAREN)
Bram Moolenaard8e44472021-07-21 22:20:33 +02002430 EMSG2_RET_NULL(_(e_unmatched_str_percent_open), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002431 else
Bram Moolenaard8e44472021-07-21 22:20:33 +02002432 EMSG2_RET_NULL(_(e_unmatched_str_open), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002433 }
2434 else if (paren == REG_NOPAREN && peekchr() != NUL)
2435 {
2436 if (curchr == Magic(')'))
Bram Moolenaard8e44472021-07-21 22:20:33 +02002437 EMSG2_RET_NULL(_(e_unmatched_str_close), reg_magic == MAGIC_ALL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002438 else
Bram Moolenaar74409f62022-01-01 15:58:22 +00002439 EMSG_RET_NULL(_(e_trailing_characters)); // "Can't happen".
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002440 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002441 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002442 // Here we set the flag allowing back references to this set of
2443 // parentheses.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002444 if (paren == REG_PAREN)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002445 had_endbrace[parno] = TRUE; // have seen the close paren
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002446 return ret;
2447}
2448
2449/*
2450 * bt_regcomp() - compile a regular expression into internal code for the
2451 * traditional back track matcher.
2452 * Returns the program in allocated space. Returns NULL for an error.
2453 *
2454 * We can't allocate space until we know how big the compiled form will be,
2455 * but we can't compile it (and thus know how big it is) until we've got a
2456 * place to put the code. So we cheat: we compile it twice, once with code
2457 * generation turned off and size counting turned on, and once "for real".
2458 * This also means that we don't allocate space until we are sure that the
2459 * thing really will compile successfully, and we never have to move the
2460 * code and thus invalidate pointers into it. (Note that it has to be in
2461 * one piece because vim_free() must be able to free it all.)
2462 *
2463 * Whether upper/lower case is to be ignored is decided when executing the
2464 * program, it does not matter here.
2465 *
2466 * Beware that the optimization-preparation code in here knows about some
2467 * of the structure of the compiled regexp.
2468 * "re_flags": RE_MAGIC and/or RE_STRING.
2469 */
2470 static regprog_T *
2471bt_regcomp(char_u *expr, int re_flags)
2472{
2473 bt_regprog_T *r;
2474 char_u *scan;
2475 char_u *longest;
2476 int len;
2477 int flags;
2478
2479 if (expr == NULL)
Bram Moolenaare29a27f2021-07-20 21:07:36 +02002480 IEMSG_RET_NULL(_(e_null_argument));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002481
2482 init_class_tab();
2483
2484 // First pass: determine size, legality.
2485 regcomp_start(expr, re_flags);
2486 regcode = JUST_CALC_SIZE;
2487 regc(REGMAGIC);
2488 if (reg(REG_NOPAREN, &flags) == NULL)
2489 return NULL;
2490
2491 // Allocate space.
2492 r = alloc(offsetof(bt_regprog_T, program) + regsize);
2493 if (r == NULL)
2494 return NULL;
2495 r->re_in_use = FALSE;
2496
2497 // Second pass: emit code.
2498 regcomp_start(expr, re_flags);
2499 regcode = r->program;
2500 regc(REGMAGIC);
2501 if (reg(REG_NOPAREN, &flags) == NULL || reg_toolong)
2502 {
2503 vim_free(r);
2504 if (reg_toolong)
Bram Moolenaareaaac012022-01-02 17:00:40 +00002505 EMSG_RET_NULL(_(e_pattern_too_long));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002506 return NULL;
2507 }
2508
2509 // Dig out information for optimizations.
2510 r->regstart = NUL; // Worst-case defaults.
2511 r->reganch = 0;
2512 r->regmust = NULL;
2513 r->regmlen = 0;
2514 r->regflags = regflags;
2515 if (flags & HASNL)
2516 r->regflags |= RF_HASNL;
2517 if (flags & HASLOOKBH)
2518 r->regflags |= RF_LOOKBH;
2519#ifdef FEAT_SYN_HL
2520 // Remember whether this pattern has any \z specials in it.
2521 r->reghasz = re_has_z;
2522#endif
2523 scan = r->program + 1; // First BRANCH.
2524 if (OP(regnext(scan)) == END) // Only one top-level choice.
2525 {
2526 scan = OPERAND(scan);
2527
2528 // Starting-point info.
2529 if (OP(scan) == BOL || OP(scan) == RE_BOF)
2530 {
2531 r->reganch++;
2532 scan = regnext(scan);
2533 }
2534
2535 if (OP(scan) == EXACTLY)
2536 {
2537 if (has_mbyte)
2538 r->regstart = (*mb_ptr2char)(OPERAND(scan));
2539 else
2540 r->regstart = *OPERAND(scan);
2541 }
2542 else if ((OP(scan) == BOW
2543 || OP(scan) == EOW
2544 || OP(scan) == NOTHING
2545 || OP(scan) == MOPEN + 0 || OP(scan) == NOPEN
2546 || OP(scan) == MCLOSE + 0 || OP(scan) == NCLOSE)
2547 && OP(regnext(scan)) == EXACTLY)
2548 {
2549 if (has_mbyte)
2550 r->regstart = (*mb_ptr2char)(OPERAND(regnext(scan)));
2551 else
2552 r->regstart = *OPERAND(regnext(scan));
2553 }
2554
2555 // If there's something expensive in the r.e., find the longest
2556 // literal string that must appear and make it the regmust. Resolve
2557 // ties in favor of later strings, since the regstart check works
2558 // with the beginning of the r.e. and avoiding duplication
2559 // strengthens checking. Not a strong reason, but sufficient in the
2560 // absence of others.
2561
2562 // When the r.e. starts with BOW, it is faster to look for a regmust
2563 // first. Used a lot for "#" and "*" commands. (Added by mool).
2564 if ((flags & SPSTART || OP(scan) == BOW || OP(scan) == EOW)
2565 && !(flags & HASNL))
2566 {
2567 longest = NULL;
2568 len = 0;
2569 for (; scan != NULL; scan = regnext(scan))
2570 if (OP(scan) == EXACTLY && STRLEN(OPERAND(scan)) >= (size_t)len)
2571 {
2572 longest = OPERAND(scan);
2573 len = (int)STRLEN(OPERAND(scan));
2574 }
2575 r->regmust = longest;
2576 r->regmlen = len;
2577 }
2578 }
2579#ifdef BT_REGEXP_DUMP
2580 regdump(expr, r);
2581#endif
2582 r->engine = &bt_regengine;
2583 return (regprog_T *)r;
2584}
2585
2586#if defined(FEAT_SYN_HL) || defined(PROTO)
2587/*
2588 * Check if during the previous call to vim_regcomp the EOL item "$" has been
2589 * found. This is messy, but it works fine.
2590 */
2591 int
2592vim_regcomp_had_eol(void)
2593{
2594 return had_eol;
2595}
2596#endif
2597
2598/*
2599 * Get a number after a backslash that is inside [].
2600 * When nothing is recognized return a backslash.
2601 */
2602 static int
2603coll_get_char(void)
2604{
2605 long nr = -1;
2606
2607 switch (*regparse++)
2608 {
2609 case 'd': nr = getdecchrs(); break;
2610 case 'o': nr = getoctchrs(); break;
2611 case 'x': nr = gethexchrs(2); break;
2612 case 'u': nr = gethexchrs(4); break;
2613 case 'U': nr = gethexchrs(8); break;
2614 }
2615 if (nr < 0 || nr > INT_MAX)
2616 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002617 // If getting the number fails be backwards compatible: the character
2618 // is a backslash.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002619 --regparse;
2620 nr = '\\';
2621 }
2622 return nr;
2623}
2624
2625/*
2626 * Free a compiled regexp program, returned by bt_regcomp().
2627 */
2628 static void
2629bt_regfree(regprog_T *prog)
2630{
2631 vim_free(prog);
2632}
2633
2634#define ADVANCE_REGINPUT() MB_PTR_ADV(rex.input)
2635
2636/*
2637 * The arguments from BRACE_LIMITS are stored here. They are actually local
2638 * to regmatch(), but they are here to reduce the amount of stack space used
2639 * (it can be called recursively many times).
2640 */
2641static long bl_minval;
2642static long bl_maxval;
2643
2644/*
2645 * Save the input line and position in a regsave_T.
2646 */
2647 static void
2648reg_save(regsave_T *save, garray_T *gap)
2649{
2650 if (REG_MULTI)
2651 {
2652 save->rs_u.pos.col = (colnr_T)(rex.input - rex.line);
2653 save->rs_u.pos.lnum = rex.lnum;
2654 }
2655 else
2656 save->rs_u.ptr = rex.input;
2657 save->rs_len = gap->ga_len;
2658}
2659
2660/*
2661 * Restore the input line and position from a regsave_T.
2662 */
2663 static void
2664reg_restore(regsave_T *save, garray_T *gap)
2665{
2666 if (REG_MULTI)
2667 {
2668 if (rex.lnum != save->rs_u.pos.lnum)
2669 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002670 // only call reg_getline() when the line number changed to save
2671 // a bit of time
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002672 rex.lnum = save->rs_u.pos.lnum;
2673 rex.line = reg_getline(rex.lnum);
2674 }
2675 rex.input = rex.line + save->rs_u.pos.col;
2676 }
2677 else
2678 rex.input = save->rs_u.ptr;
2679 gap->ga_len = save->rs_len;
2680}
2681
2682/*
2683 * Return TRUE if current position is equal to saved position.
2684 */
2685 static int
2686reg_save_equal(regsave_T *save)
2687{
2688 if (REG_MULTI)
2689 return rex.lnum == save->rs_u.pos.lnum
2690 && rex.input == rex.line + save->rs_u.pos.col;
2691 return rex.input == save->rs_u.ptr;
2692}
2693
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002694// Save the sub-expressions before attempting a match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002695#define save_se(savep, posp, pp) \
2696 REG_MULTI ? save_se_multi((savep), (posp)) : save_se_one((savep), (pp))
2697
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002698// After a failed match restore the sub-expressions.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002699#define restore_se(savep, posp, pp) { \
2700 if (REG_MULTI) \
2701 *(posp) = (savep)->se_u.pos; \
2702 else \
2703 *(pp) = (savep)->se_u.ptr; }
2704
2705/*
2706 * Tentatively set the sub-expression start to the current position (after
2707 * calling regmatch() they will have changed). Need to save the existing
2708 * values for when there is no match.
2709 * Use se_save() to use pointer (save_se_multi()) or position (save_se_one()),
2710 * depending on REG_MULTI.
2711 */
2712 static void
2713save_se_multi(save_se_T *savep, lpos_T *posp)
2714{
2715 savep->se_u.pos = *posp;
2716 posp->lnum = rex.lnum;
2717 posp->col = (colnr_T)(rex.input - rex.line);
2718}
2719
2720 static void
2721save_se_one(save_se_T *savep, char_u **pp)
2722{
2723 savep->se_u.ptr = *pp;
2724 *pp = rex.input;
2725}
2726
2727/*
2728 * regrepeat - repeatedly match something simple, return how many.
2729 * Advances rex.input (and rex.lnum) to just after the matched chars.
2730 */
2731 static int
2732regrepeat(
2733 char_u *p,
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002734 long maxcount) // maximum number of matches allowed
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002735{
2736 long count = 0;
2737 char_u *scan;
2738 char_u *opnd;
2739 int mask;
2740 int testval = 0;
2741
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002742 scan = rex.input; // Make local copy of rex.input for speed.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002743 opnd = OPERAND(p);
2744 switch (OP(p))
2745 {
2746 case ANY:
2747 case ANY + ADD_NL:
2748 while (count < maxcount)
2749 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002750 // Matching anything means we continue until end-of-line (or
2751 // end-of-file for ANY + ADD_NL), only limited by maxcount.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002752 while (*scan != NUL && count < maxcount)
2753 {
2754 ++count;
2755 MB_PTR_ADV(scan);
2756 }
2757 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2758 || rex.reg_line_lbr || count == maxcount)
2759 break;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002760 ++count; // count the line-break
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002761 reg_nextline();
2762 scan = rex.input;
2763 if (got_int)
2764 break;
2765 }
2766 break;
2767
2768 case IDENT:
2769 case IDENT + ADD_NL:
2770 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002771 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002772 case SIDENT:
2773 case SIDENT + ADD_NL:
2774 while (count < maxcount)
2775 {
2776 if (vim_isIDc(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
2777 {
2778 MB_PTR_ADV(scan);
2779 }
2780 else if (*scan == NUL)
2781 {
2782 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2783 || rex.reg_line_lbr)
2784 break;
2785 reg_nextline();
2786 scan = rex.input;
2787 if (got_int)
2788 break;
2789 }
2790 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2791 ++scan;
2792 else
2793 break;
2794 ++count;
2795 }
2796 break;
2797
2798 case KWORD:
2799 case KWORD + ADD_NL:
2800 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002801 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002802 case SKWORD:
2803 case SKWORD + ADD_NL:
2804 while (count < maxcount)
2805 {
2806 if (vim_iswordp_buf(scan, rex.reg_buf)
2807 && (testval || !VIM_ISDIGIT(*scan)))
2808 {
2809 MB_PTR_ADV(scan);
2810 }
2811 else if (*scan == NUL)
2812 {
2813 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2814 || rex.reg_line_lbr)
2815 break;
2816 reg_nextline();
2817 scan = rex.input;
2818 if (got_int)
2819 break;
2820 }
2821 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2822 ++scan;
2823 else
2824 break;
2825 ++count;
2826 }
2827 break;
2828
2829 case FNAME:
2830 case FNAME + ADD_NL:
2831 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002832 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002833 case SFNAME:
2834 case SFNAME + ADD_NL:
2835 while (count < maxcount)
2836 {
2837 if (vim_isfilec(PTR2CHAR(scan)) && (testval || !VIM_ISDIGIT(*scan)))
2838 {
2839 MB_PTR_ADV(scan);
2840 }
2841 else if (*scan == NUL)
2842 {
2843 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2844 || rex.reg_line_lbr)
2845 break;
2846 reg_nextline();
2847 scan = rex.input;
2848 if (got_int)
2849 break;
2850 }
2851 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2852 ++scan;
2853 else
2854 break;
2855 ++count;
2856 }
2857 break;
2858
2859 case PRINT:
2860 case PRINT + ADD_NL:
2861 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002862 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02002863 case SPRINT:
2864 case SPRINT + ADD_NL:
2865 while (count < maxcount)
2866 {
2867 if (*scan == NUL)
2868 {
2869 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2870 || rex.reg_line_lbr)
2871 break;
2872 reg_nextline();
2873 scan = rex.input;
2874 if (got_int)
2875 break;
2876 }
2877 else if (vim_isprintc(PTR2CHAR(scan)) == 1
2878 && (testval || !VIM_ISDIGIT(*scan)))
2879 {
2880 MB_PTR_ADV(scan);
2881 }
2882 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2883 ++scan;
2884 else
2885 break;
2886 ++count;
2887 }
2888 break;
2889
2890 case WHITE:
2891 case WHITE + ADD_NL:
2892 testval = mask = RI_WHITE;
2893do_class:
2894 while (count < maxcount)
2895 {
2896 int l;
2897
2898 if (*scan == NUL)
2899 {
2900 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
2901 || rex.reg_line_lbr)
2902 break;
2903 reg_nextline();
2904 scan = rex.input;
2905 if (got_int)
2906 break;
2907 }
2908 else if (has_mbyte && (l = (*mb_ptr2len)(scan)) > 1)
2909 {
2910 if (testval != 0)
2911 break;
2912 scan += l;
2913 }
2914 else if ((class_tab[*scan] & mask) == testval)
2915 ++scan;
2916 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
2917 ++scan;
2918 else
2919 break;
2920 ++count;
2921 }
2922 break;
2923
2924 case NWHITE:
2925 case NWHITE + ADD_NL:
2926 mask = RI_WHITE;
2927 goto do_class;
2928 case DIGIT:
2929 case DIGIT + ADD_NL:
2930 testval = mask = RI_DIGIT;
2931 goto do_class;
2932 case NDIGIT:
2933 case NDIGIT + ADD_NL:
2934 mask = RI_DIGIT;
2935 goto do_class;
2936 case HEX:
2937 case HEX + ADD_NL:
2938 testval = mask = RI_HEX;
2939 goto do_class;
2940 case NHEX:
2941 case NHEX + ADD_NL:
2942 mask = RI_HEX;
2943 goto do_class;
2944 case OCTAL:
2945 case OCTAL + ADD_NL:
2946 testval = mask = RI_OCTAL;
2947 goto do_class;
2948 case NOCTAL:
2949 case NOCTAL + ADD_NL:
2950 mask = RI_OCTAL;
2951 goto do_class;
2952 case WORD:
2953 case WORD + ADD_NL:
2954 testval = mask = RI_WORD;
2955 goto do_class;
2956 case NWORD:
2957 case NWORD + ADD_NL:
2958 mask = RI_WORD;
2959 goto do_class;
2960 case HEAD:
2961 case HEAD + ADD_NL:
2962 testval = mask = RI_HEAD;
2963 goto do_class;
2964 case NHEAD:
2965 case NHEAD + ADD_NL:
2966 mask = RI_HEAD;
2967 goto do_class;
2968 case ALPHA:
2969 case ALPHA + ADD_NL:
2970 testval = mask = RI_ALPHA;
2971 goto do_class;
2972 case NALPHA:
2973 case NALPHA + ADD_NL:
2974 mask = RI_ALPHA;
2975 goto do_class;
2976 case LOWER:
2977 case LOWER + ADD_NL:
2978 testval = mask = RI_LOWER;
2979 goto do_class;
2980 case NLOWER:
2981 case NLOWER + ADD_NL:
2982 mask = RI_LOWER;
2983 goto do_class;
2984 case UPPER:
2985 case UPPER + ADD_NL:
2986 testval = mask = RI_UPPER;
2987 goto do_class;
2988 case NUPPER:
2989 case NUPPER + ADD_NL:
2990 mask = RI_UPPER;
2991 goto do_class;
2992
2993 case EXACTLY:
2994 {
2995 int cu, cl;
2996
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02002997 // This doesn't do a multi-byte character, because a MULTIBYTECODE
2998 // would have been used for it. It does handle single-byte
2999 // characters, such as latin1.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003000 if (rex.reg_ic)
3001 {
3002 cu = MB_TOUPPER(*opnd);
3003 cl = MB_TOLOWER(*opnd);
3004 while (count < maxcount && (*scan == cu || *scan == cl))
3005 {
3006 count++;
3007 scan++;
3008 }
3009 }
3010 else
3011 {
3012 cu = *opnd;
3013 while (count < maxcount && *scan == cu)
3014 {
3015 count++;
3016 scan++;
3017 }
3018 }
3019 break;
3020 }
3021
3022 case MULTIBYTECODE:
3023 {
3024 int i, len, cf = 0;
3025
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003026 // Safety check (just in case 'encoding' was changed since
3027 // compiling the program).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003028 if ((len = (*mb_ptr2len)(opnd)) > 1)
3029 {
3030 if (rex.reg_ic && enc_utf8)
3031 cf = utf_fold(utf_ptr2char(opnd));
3032 while (count < maxcount && (*mb_ptr2len)(scan) >= len)
3033 {
3034 for (i = 0; i < len; ++i)
3035 if (opnd[i] != scan[i])
3036 break;
3037 if (i < len && (!rex.reg_ic || !enc_utf8
3038 || utf_fold(utf_ptr2char(scan)) != cf))
3039 break;
3040 scan += len;
3041 ++count;
3042 }
3043 }
3044 }
3045 break;
3046
3047 case ANYOF:
3048 case ANYOF + ADD_NL:
3049 testval = TRUE;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003050 // FALLTHROUGH
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003051
3052 case ANYBUT:
3053 case ANYBUT + ADD_NL:
3054 while (count < maxcount)
3055 {
3056 int len;
3057
3058 if (*scan == NUL)
3059 {
3060 if (!REG_MULTI || !WITH_NL(OP(p)) || rex.lnum > rex.reg_maxline
3061 || rex.reg_line_lbr)
3062 break;
3063 reg_nextline();
3064 scan = rex.input;
3065 if (got_int)
3066 break;
3067 }
3068 else if (rex.reg_line_lbr && *scan == '\n' && WITH_NL(OP(p)))
3069 ++scan;
3070 else if (has_mbyte && (len = (*mb_ptr2len)(scan)) > 1)
3071 {
3072 if ((cstrchr(opnd, (*mb_ptr2char)(scan)) == NULL) == testval)
3073 break;
3074 scan += len;
3075 }
3076 else
3077 {
3078 if ((cstrchr(opnd, *scan) == NULL) == testval)
3079 break;
3080 ++scan;
3081 }
3082 ++count;
3083 }
3084 break;
3085
3086 case NEWL:
3087 while (count < maxcount
3088 && ((*scan == NUL && rex.lnum <= rex.reg_maxline
3089 && !rex.reg_line_lbr && REG_MULTI)
3090 || (*scan == '\n' && rex.reg_line_lbr)))
3091 {
3092 count++;
3093 if (rex.reg_line_lbr)
3094 ADVANCE_REGINPUT();
3095 else
3096 reg_nextline();
3097 scan = rex.input;
3098 if (got_int)
3099 break;
3100 }
3101 break;
3102
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003103 default: // Oh dear. Called inappropriately.
Bram Moolenaare29a27f2021-07-20 21:07:36 +02003104 iemsg(_(e_corrupted_regexp_program));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003105#ifdef DEBUG
3106 printf("Called regrepeat with op code %d\n", OP(p));
3107#endif
3108 break;
3109 }
3110
3111 rex.input = scan;
3112
3113 return (int)count;
3114}
3115
3116/*
3117 * Push an item onto the regstack.
3118 * Returns pointer to new item. Returns NULL when out of memory.
3119 */
3120 static regitem_T *
3121regstack_push(regstate_T state, char_u *scan)
3122{
3123 regitem_T *rp;
3124
3125 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
3126 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00003127 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003128 return NULL;
3129 }
3130 if (ga_grow(&regstack, sizeof(regitem_T)) == FAIL)
3131 return NULL;
3132
3133 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len);
3134 rp->rs_state = state;
3135 rp->rs_scan = scan;
3136
3137 regstack.ga_len += sizeof(regitem_T);
3138 return rp;
3139}
3140
3141/*
3142 * Pop an item from the regstack.
3143 */
3144 static void
3145regstack_pop(char_u **scan)
3146{
3147 regitem_T *rp;
3148
3149 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
3150 *scan = rp->rs_scan;
3151
3152 regstack.ga_len -= sizeof(regitem_T);
3153}
3154
Bram Moolenaar616592e2022-06-17 15:17:10 +01003155#ifdef FEAT_RELTIME
3156/*
3157 * Check if the timer expired, return TRUE if so.
3158 */
3159 static int
3160bt_did_time_out(int *timed_out)
3161{
3162 if (*timeout_flag)
3163 {
3164 if (timed_out != NULL)
3165 {
Bram Moolenaar509ce032022-06-20 11:23:01 +01003166# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar616592e2022-06-17 15:17:10 +01003167 if (!*timed_out)
3168 ch_log(NULL, "BT regexp timed out");
Bram Moolenaar509ce032022-06-20 11:23:01 +01003169# endif
Bram Moolenaar616592e2022-06-17 15:17:10 +01003170 *timed_out = TRUE;
3171 }
3172 return TRUE;
3173 }
3174 return FALSE;
3175}
3176#endif
3177
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003178/*
3179 * Save the current subexpr to "bp", so that they can be restored
3180 * later by restore_subexpr().
3181 */
3182 static void
3183save_subexpr(regbehind_T *bp)
3184{
3185 int i;
3186
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003187 // When "rex.need_clear_subexpr" is set we don't need to save the values,
3188 // only remember that this flag needs to be set again when restoring.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003189 bp->save_need_clear_subexpr = rex.need_clear_subexpr;
3190 if (!rex.need_clear_subexpr)
3191 {
3192 for (i = 0; i < NSUBEXP; ++i)
3193 {
3194 if (REG_MULTI)
3195 {
3196 bp->save_start[i].se_u.pos = rex.reg_startpos[i];
3197 bp->save_end[i].se_u.pos = rex.reg_endpos[i];
3198 }
3199 else
3200 {
3201 bp->save_start[i].se_u.ptr = rex.reg_startp[i];
3202 bp->save_end[i].se_u.ptr = rex.reg_endp[i];
3203 }
3204 }
3205 }
3206}
3207
3208/*
3209 * Restore the subexpr from "bp".
3210 */
3211 static void
3212restore_subexpr(regbehind_T *bp)
3213{
3214 int i;
3215
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003216 // Only need to restore saved values when they are not to be cleared.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003217 rex.need_clear_subexpr = bp->save_need_clear_subexpr;
3218 if (!rex.need_clear_subexpr)
3219 {
3220 for (i = 0; i < NSUBEXP; ++i)
3221 {
3222 if (REG_MULTI)
3223 {
3224 rex.reg_startpos[i] = bp->save_start[i].se_u.pos;
3225 rex.reg_endpos[i] = bp->save_end[i].se_u.pos;
3226 }
3227 else
3228 {
3229 rex.reg_startp[i] = bp->save_start[i].se_u.ptr;
3230 rex.reg_endp[i] = bp->save_end[i].se_u.ptr;
3231 }
3232 }
3233 }
3234}
3235
3236/*
3237 * regmatch - main matching routine
3238 *
3239 * Conceptually the strategy is simple: Check to see whether the current node
3240 * matches, push an item onto the regstack and loop to see whether the rest
3241 * matches, and then act accordingly. In practice we make some effort to
3242 * avoid using the regstack, in particular by going through "ordinary" nodes
3243 * (that don't need to know whether the rest of the match failed) by a nested
3244 * loop.
3245 *
3246 * Returns TRUE when there is a match. Leaves rex.input and rex.lnum just after
3247 * the last matched character.
3248 * Returns FALSE when there is no match. Leaves rex.input and rex.lnum in an
3249 * undefined state!
3250 */
3251 static int
3252regmatch(
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003253 char_u *scan, // Current node.
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003254 int *timed_out UNUSED) // flag set on timeout or NULL
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003255{
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003256 char_u *next; // Next node.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003257 int op;
3258 int c;
3259 regitem_T *rp;
3260 int no;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003261 int status; // one of the RA_ values:
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003262
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003263 // Make "regstack" and "backpos" empty. They are allocated and freed in
3264 // bt_regexec_both() to reduce malloc()/free() calls.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003265 regstack.ga_len = 0;
3266 backpos.ga_len = 0;
3267
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003268 // Repeat until "regstack" is empty.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003269 for (;;)
3270 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003271 // Some patterns may take a long time to match, e.g., "\([a-z]\+\)\+Q".
3272 // Allow interrupting them with CTRL-C.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003273 fast_breakcheck();
3274
3275#ifdef DEBUG
3276 if (scan != NULL && regnarrate)
3277 {
3278 mch_errmsg((char *)regprop(scan));
3279 mch_errmsg("(\n");
3280 }
3281#endif
3282
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003283 // Repeat for items that can be matched sequentially, without using the
3284 // regstack.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003285 for (;;)
3286 {
3287 if (got_int || scan == NULL)
3288 {
3289 status = RA_FAIL;
3290 break;
3291 }
3292#ifdef FEAT_RELTIME
Bram Moolenaar616592e2022-06-17 15:17:10 +01003293 if (bt_did_time_out(timed_out))
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003294 {
Paul Ollis65745772022-06-05 16:55:54 +01003295 status = RA_FAIL;
3296 break;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003297 }
3298#endif
3299 status = RA_CONT;
3300
3301#ifdef DEBUG
3302 if (regnarrate)
3303 {
3304 mch_errmsg((char *)regprop(scan));
3305 mch_errmsg("...\n");
3306# ifdef FEAT_SYN_HL
3307 if (re_extmatch_in != NULL)
3308 {
3309 int i;
3310
3311 mch_errmsg(_("External submatches:\n"));
3312 for (i = 0; i < NSUBEXP; i++)
3313 {
3314 mch_errmsg(" \"");
3315 if (re_extmatch_in->matches[i] != NULL)
3316 mch_errmsg((char *)re_extmatch_in->matches[i]);
3317 mch_errmsg("\"\n");
3318 }
3319 }
3320# endif
3321 }
3322#endif
3323 next = regnext(scan);
3324
3325 op = OP(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003326 // Check for character class with NL added.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003327 if (!rex.reg_line_lbr && WITH_NL(op) && REG_MULTI
Paul Ollis65745772022-06-05 16:55:54 +01003328 && *rex.input == NUL && rex.lnum <= rex.reg_maxline)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003329 {
3330 reg_nextline();
3331 }
3332 else if (rex.reg_line_lbr && WITH_NL(op) && *rex.input == '\n')
3333 {
3334 ADVANCE_REGINPUT();
3335 }
3336 else
3337 {
3338 if (WITH_NL(op))
3339 op -= ADD_NL;
3340 if (has_mbyte)
3341 c = (*mb_ptr2char)(rex.input);
3342 else
3343 c = *rex.input;
3344 switch (op)
3345 {
3346 case BOL:
3347 if (rex.input != rex.line)
3348 status = RA_NOMATCH;
3349 break;
3350
3351 case EOL:
3352 if (c != NUL)
3353 status = RA_NOMATCH;
3354 break;
3355
3356 case RE_BOF:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003357 // We're not at the beginning of the file when below the first
3358 // line where we started, not at the start of the line or we
3359 // didn't start at the first line of the buffer.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003360 if (rex.lnum != 0 || rex.input != rex.line
3361 || (REG_MULTI && rex.reg_firstlnum > 1))
3362 status = RA_NOMATCH;
3363 break;
3364
3365 case RE_EOF:
3366 if (rex.lnum != rex.reg_maxline || c != NUL)
3367 status = RA_NOMATCH;
3368 break;
3369
3370 case CURSOR:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003371 // Check if the buffer is in a window and compare the
3372 // rex.reg_win->w_cursor position to the match position.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003373 if (rex.reg_win == NULL
3374 || (rex.lnum + rex.reg_firstlnum
3375 != rex.reg_win->w_cursor.lnum)
3376 || ((colnr_T)(rex.input - rex.line)
3377 != rex.reg_win->w_cursor.col))
3378 status = RA_NOMATCH;
3379 break;
3380
3381 case RE_MARK:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003382 // Compare the mark position to the match position.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003383 {
3384 int mark = OPERAND(scan)[0];
3385 int cmp = OPERAND(scan)[1];
3386 pos_T *pos;
Bram Moolenaarb55986c2022-03-29 13:24:58 +01003387 size_t col = REG_MULTI ? rex.input - rex.line : 0;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003388
3389 pos = getmark_buf(rex.reg_buf, mark, FALSE);
Bram Moolenaarb55986c2022-03-29 13:24:58 +01003390
3391 // Line may have been freed, get it again.
3392 if (REG_MULTI)
3393 {
3394 rex.line = reg_getline(rex.lnum);
3395 rex.input = rex.line + col;
3396 }
3397
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003398 if (pos == NULL // mark doesn't exist
Bram Moolenaar872bee52021-05-24 22:56:15 +02003399 || pos->lnum <= 0) // mark isn't set in reg_buf
3400 {
3401 status = RA_NOMATCH;
3402 }
3403 else
3404 {
3405 colnr_T pos_col = pos->lnum == rex.lnum + rex.reg_firstlnum
3406 && pos->col == MAXCOL
3407 ? (colnr_T)STRLEN(reg_getline(
3408 pos->lnum - rex.reg_firstlnum))
3409 : pos->col;
3410
3411 if ((pos->lnum == rex.lnum + rex.reg_firstlnum
3412 ? (pos_col == (colnr_T)(rex.input - rex.line)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003413 ? (cmp == '<' || cmp == '>')
Bram Moolenaar872bee52021-05-24 22:56:15 +02003414 : (pos_col < (colnr_T)(rex.input - rex.line)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003415 ? cmp != '>'
3416 : cmp != '<'))
3417 : (pos->lnum < rex.lnum + rex.reg_firstlnum
3418 ? cmp != '>'
3419 : cmp != '<')))
3420 status = RA_NOMATCH;
Bram Moolenaar872bee52021-05-24 22:56:15 +02003421 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003422 }
3423 break;
3424
3425 case RE_VISUAL:
3426 if (!reg_match_visual())
3427 status = RA_NOMATCH;
3428 break;
3429
3430 case RE_LNUM:
3431 if (!REG_MULTI || !re_num_cmp((long_u)(rex.lnum + rex.reg_firstlnum),
3432 scan))
3433 status = RA_NOMATCH;
3434 break;
3435
3436 case RE_COL:
3437 if (!re_num_cmp((long_u)(rex.input - rex.line) + 1, scan))
3438 status = RA_NOMATCH;
3439 break;
3440
3441 case RE_VCOL:
Bram Moolenaar13ed4942022-08-19 13:59:25 +01003442 {
3443 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win;
3444 linenr_T lnum = rex.reg_firstlnum + rex.lnum;
3445 long_u vcol = 0;
3446
3447 if (lnum > 0 && lnum <= wp->w_buffer->b_ml.ml_line_count)
3448 vcol = (long_u)win_linetabsize(wp, lnum, rex.line,
3449 (colnr_T)(rex.input - rex.line));
3450 if (!re_num_cmp(vcol + 1, scan))
3451 status = RA_NOMATCH;
3452 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003453 break;
3454
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003455 case BOW: // \<word; rex.input points to w
3456 if (c == NUL) // Can't match at end of line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003457 status = RA_NOMATCH;
3458 else if (has_mbyte)
3459 {
3460 int this_class;
3461
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003462 // Get class of current and previous char (if it exists).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003463 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
3464 if (this_class <= 1)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003465 status = RA_NOMATCH; // not on a word at all
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003466 else if (reg_prev_class() == this_class)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003467 status = RA_NOMATCH; // previous char is in same word
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003468 }
3469 else
3470 {
3471 if (!vim_iswordc_buf(c, rex.reg_buf) || (rex.input > rex.line
3472 && vim_iswordc_buf(rex.input[-1], rex.reg_buf)))
3473 status = RA_NOMATCH;
3474 }
3475 break;
3476
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003477 case EOW: // word\>; rex.input points after d
3478 if (rex.input == rex.line) // Can't match at start of line
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003479 status = RA_NOMATCH;
3480 else if (has_mbyte)
3481 {
3482 int this_class, prev_class;
3483
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003484 // Get class of current and previous char (if it exists).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003485 this_class = mb_get_class_buf(rex.input, rex.reg_buf);
3486 prev_class = reg_prev_class();
3487 if (this_class == prev_class
3488 || prev_class == 0 || prev_class == 1)
3489 status = RA_NOMATCH;
3490 }
3491 else
3492 {
3493 if (!vim_iswordc_buf(rex.input[-1], rex.reg_buf)
3494 || (rex.input[0] != NUL
3495 && vim_iswordc_buf(c, rex.reg_buf)))
3496 status = RA_NOMATCH;
3497 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003498 break; // Matched with EOW
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003499
3500 case ANY:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003501 // ANY does not match new lines.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003502 if (c == NUL)
3503 status = RA_NOMATCH;
3504 else
3505 ADVANCE_REGINPUT();
3506 break;
3507
3508 case IDENT:
3509 if (!vim_isIDc(c))
3510 status = RA_NOMATCH;
3511 else
3512 ADVANCE_REGINPUT();
3513 break;
3514
3515 case SIDENT:
3516 if (VIM_ISDIGIT(*rex.input) || !vim_isIDc(c))
3517 status = RA_NOMATCH;
3518 else
3519 ADVANCE_REGINPUT();
3520 break;
3521
3522 case KWORD:
3523 if (!vim_iswordp_buf(rex.input, rex.reg_buf))
3524 status = RA_NOMATCH;
3525 else
3526 ADVANCE_REGINPUT();
3527 break;
3528
3529 case SKWORD:
3530 if (VIM_ISDIGIT(*rex.input)
3531 || !vim_iswordp_buf(rex.input, rex.reg_buf))
3532 status = RA_NOMATCH;
3533 else
3534 ADVANCE_REGINPUT();
3535 break;
3536
3537 case FNAME:
3538 if (!vim_isfilec(c))
3539 status = RA_NOMATCH;
3540 else
3541 ADVANCE_REGINPUT();
3542 break;
3543
3544 case SFNAME:
3545 if (VIM_ISDIGIT(*rex.input) || !vim_isfilec(c))
3546 status = RA_NOMATCH;
3547 else
3548 ADVANCE_REGINPUT();
3549 break;
3550
3551 case PRINT:
3552 if (!vim_isprintc(PTR2CHAR(rex.input)))
3553 status = RA_NOMATCH;
3554 else
3555 ADVANCE_REGINPUT();
3556 break;
3557
3558 case SPRINT:
3559 if (VIM_ISDIGIT(*rex.input) || !vim_isprintc(PTR2CHAR(rex.input)))
3560 status = RA_NOMATCH;
3561 else
3562 ADVANCE_REGINPUT();
3563 break;
3564
3565 case WHITE:
3566 if (!VIM_ISWHITE(c))
3567 status = RA_NOMATCH;
3568 else
3569 ADVANCE_REGINPUT();
3570 break;
3571
3572 case NWHITE:
3573 if (c == NUL || VIM_ISWHITE(c))
3574 status = RA_NOMATCH;
3575 else
3576 ADVANCE_REGINPUT();
3577 break;
3578
3579 case DIGIT:
3580 if (!ri_digit(c))
3581 status = RA_NOMATCH;
3582 else
3583 ADVANCE_REGINPUT();
3584 break;
3585
3586 case NDIGIT:
3587 if (c == NUL || ri_digit(c))
3588 status = RA_NOMATCH;
3589 else
3590 ADVANCE_REGINPUT();
3591 break;
3592
3593 case HEX:
3594 if (!ri_hex(c))
3595 status = RA_NOMATCH;
3596 else
3597 ADVANCE_REGINPUT();
3598 break;
3599
3600 case NHEX:
3601 if (c == NUL || ri_hex(c))
3602 status = RA_NOMATCH;
3603 else
3604 ADVANCE_REGINPUT();
3605 break;
3606
3607 case OCTAL:
3608 if (!ri_octal(c))
3609 status = RA_NOMATCH;
3610 else
3611 ADVANCE_REGINPUT();
3612 break;
3613
3614 case NOCTAL:
3615 if (c == NUL || ri_octal(c))
3616 status = RA_NOMATCH;
3617 else
3618 ADVANCE_REGINPUT();
3619 break;
3620
3621 case WORD:
3622 if (!ri_word(c))
3623 status = RA_NOMATCH;
3624 else
3625 ADVANCE_REGINPUT();
3626 break;
3627
3628 case NWORD:
3629 if (c == NUL || ri_word(c))
3630 status = RA_NOMATCH;
3631 else
3632 ADVANCE_REGINPUT();
3633 break;
3634
3635 case HEAD:
3636 if (!ri_head(c))
3637 status = RA_NOMATCH;
3638 else
3639 ADVANCE_REGINPUT();
3640 break;
3641
3642 case NHEAD:
3643 if (c == NUL || ri_head(c))
3644 status = RA_NOMATCH;
3645 else
3646 ADVANCE_REGINPUT();
3647 break;
3648
3649 case ALPHA:
3650 if (!ri_alpha(c))
3651 status = RA_NOMATCH;
3652 else
3653 ADVANCE_REGINPUT();
3654 break;
3655
3656 case NALPHA:
3657 if (c == NUL || ri_alpha(c))
3658 status = RA_NOMATCH;
3659 else
3660 ADVANCE_REGINPUT();
3661 break;
3662
3663 case LOWER:
3664 if (!ri_lower(c))
3665 status = RA_NOMATCH;
3666 else
3667 ADVANCE_REGINPUT();
3668 break;
3669
3670 case NLOWER:
3671 if (c == NUL || ri_lower(c))
3672 status = RA_NOMATCH;
3673 else
3674 ADVANCE_REGINPUT();
3675 break;
3676
3677 case UPPER:
3678 if (!ri_upper(c))
3679 status = RA_NOMATCH;
3680 else
3681 ADVANCE_REGINPUT();
3682 break;
3683
3684 case NUPPER:
3685 if (c == NUL || ri_upper(c))
3686 status = RA_NOMATCH;
3687 else
3688 ADVANCE_REGINPUT();
3689 break;
3690
3691 case EXACTLY:
3692 {
3693 int len;
3694 char_u *opnd;
3695
3696 opnd = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003697 // Inline the first byte, for speed.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003698 if (*opnd != *rex.input
3699 && (!rex.reg_ic
3700 || (!enc_utf8
3701 && MB_TOLOWER(*opnd) != MB_TOLOWER(*rex.input))))
3702 status = RA_NOMATCH;
3703 else if (*opnd == NUL)
3704 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003705 // match empty string always works; happens when "~" is
3706 // empty.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003707 }
3708 else
3709 {
3710 if (opnd[1] == NUL && !(enc_utf8 && rex.reg_ic))
3711 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003712 len = 1; // matched a single byte above
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003713 }
3714 else
3715 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003716 // Need to match first byte again for multi-byte.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003717 len = (int)STRLEN(opnd);
3718 if (cstrncmp(opnd, rex.input, &len) != 0)
3719 status = RA_NOMATCH;
3720 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003721 // Check for following composing character, unless %C
3722 // follows (skips over all composing chars).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003723 if (status != RA_NOMATCH
3724 && enc_utf8
3725 && UTF_COMPOSINGLIKE(rex.input, rex.input + len)
3726 && !rex.reg_icombine
3727 && OP(next) != RE_COMPOSING)
3728 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003729 // raaron: This code makes a composing character get
3730 // ignored, which is the correct behavior (sometimes)
3731 // for voweled Hebrew texts.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003732 status = RA_NOMATCH;
3733 }
3734 if (status != RA_NOMATCH)
3735 rex.input += len;
3736 }
3737 }
3738 break;
3739
3740 case ANYOF:
3741 case ANYBUT:
3742 if (c == NUL)
3743 status = RA_NOMATCH;
3744 else if ((cstrchr(OPERAND(scan), c) == NULL) == (op == ANYOF))
3745 status = RA_NOMATCH;
3746 else
3747 ADVANCE_REGINPUT();
3748 break;
3749
3750 case MULTIBYTECODE:
3751 if (has_mbyte)
3752 {
3753 int i, len;
3754 char_u *opnd;
3755 int opndc = 0, inpc;
3756
3757 opnd = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003758 // Safety check (just in case 'encoding' was changed since
3759 // compiling the program).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003760 if ((len = (*mb_ptr2len)(opnd)) < 2)
3761 {
3762 status = RA_NOMATCH;
3763 break;
3764 }
3765 if (enc_utf8)
3766 opndc = utf_ptr2char(opnd);
3767 if (enc_utf8 && utf_iscomposing(opndc))
3768 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003769 // When only a composing char is given match at any
3770 // position where that composing char appears.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003771 status = RA_NOMATCH;
3772 for (i = 0; rex.input[i] != NUL;
3773 i += utf_ptr2len(rex.input + i))
3774 {
3775 inpc = utf_ptr2char(rex.input + i);
3776 if (!utf_iscomposing(inpc))
3777 {
3778 if (i > 0)
3779 break;
3780 }
3781 else if (opndc == inpc)
3782 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003783 // Include all following composing chars.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003784 len = i + utfc_ptr2len(rex.input + i);
3785 status = RA_MATCH;
3786 break;
3787 }
3788 }
3789 }
3790 else
3791 for (i = 0; i < len; ++i)
3792 if (opnd[i] != rex.input[i])
3793 {
3794 status = RA_NOMATCH;
3795 break;
3796 }
3797 rex.input += len;
3798 }
3799 else
3800 status = RA_NOMATCH;
3801 break;
3802 case RE_COMPOSING:
3803 if (enc_utf8)
3804 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003805 // Skip composing characters.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003806 while (utf_iscomposing(utf_ptr2char(rex.input)))
3807 MB_CPTR_ADV(rex.input);
3808 }
3809 break;
3810
3811 case NOTHING:
3812 break;
3813
3814 case BACK:
3815 {
3816 int i;
3817 backpos_T *bp;
3818
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003819 // When we run into BACK we need to check if we don't keep
3820 // looping without matching any input. The second and later
3821 // times a BACK is encountered it fails if the input is still
3822 // at the same position as the previous time.
3823 // The positions are stored in "backpos" and found by the
3824 // current value of "scan", the position in the RE program.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003825 bp = (backpos_T *)backpos.ga_data;
3826 for (i = 0; i < backpos.ga_len; ++i)
3827 if (bp[i].bp_scan == scan)
3828 break;
3829 if (i == backpos.ga_len)
3830 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003831 // First time at this BACK, make room to store the pos.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003832 if (ga_grow(&backpos, 1) == FAIL)
3833 status = RA_FAIL;
3834 else
3835 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003836 // get "ga_data" again, it may have changed
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003837 bp = (backpos_T *)backpos.ga_data;
3838 bp[i].bp_scan = scan;
3839 ++backpos.ga_len;
3840 }
3841 }
3842 else if (reg_save_equal(&bp[i].bp_pos))
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003843 // Still at same position as last time, fail.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003844 status = RA_NOMATCH;
3845
3846 if (status != RA_FAIL && status != RA_NOMATCH)
3847 reg_save(&bp[i].bp_pos, &backpos);
3848 }
3849 break;
3850
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003851 case MOPEN + 0: // Match start: \zs
3852 case MOPEN + 1: // \(
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003853 case MOPEN + 2:
3854 case MOPEN + 3:
3855 case MOPEN + 4:
3856 case MOPEN + 5:
3857 case MOPEN + 6:
3858 case MOPEN + 7:
3859 case MOPEN + 8:
3860 case MOPEN + 9:
3861 {
3862 no = op - MOPEN;
3863 cleanup_subexpr();
3864 rp = regstack_push(RS_MOPEN, scan);
3865 if (rp == NULL)
3866 status = RA_FAIL;
3867 else
3868 {
3869 rp->rs_no = no;
3870 save_se(&rp->rs_un.sesave, &rex.reg_startpos[no],
3871 &rex.reg_startp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003872 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003873 }
3874 }
3875 break;
3876
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003877 case NOPEN: // \%(
3878 case NCLOSE: // \) after \%(
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003879 if (regstack_push(RS_NOPEN, scan) == NULL)
3880 status = RA_FAIL;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003881 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003882 break;
3883
3884#ifdef FEAT_SYN_HL
3885 case ZOPEN + 1:
3886 case ZOPEN + 2:
3887 case ZOPEN + 3:
3888 case ZOPEN + 4:
3889 case ZOPEN + 5:
3890 case ZOPEN + 6:
3891 case ZOPEN + 7:
3892 case ZOPEN + 8:
3893 case ZOPEN + 9:
3894 {
3895 no = op - ZOPEN;
3896 cleanup_zsubexpr();
3897 rp = regstack_push(RS_ZOPEN, scan);
3898 if (rp == NULL)
3899 status = RA_FAIL;
3900 else
3901 {
3902 rp->rs_no = no;
3903 save_se(&rp->rs_un.sesave, &reg_startzpos[no],
3904 &reg_startzp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003905 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003906 }
3907 }
3908 break;
3909#endif
3910
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003911 case MCLOSE + 0: // Match end: \ze
3912 case MCLOSE + 1: // \)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003913 case MCLOSE + 2:
3914 case MCLOSE + 3:
3915 case MCLOSE + 4:
3916 case MCLOSE + 5:
3917 case MCLOSE + 6:
3918 case MCLOSE + 7:
3919 case MCLOSE + 8:
3920 case MCLOSE + 9:
3921 {
3922 no = op - MCLOSE;
3923 cleanup_subexpr();
3924 rp = regstack_push(RS_MCLOSE, scan);
3925 if (rp == NULL)
3926 status = RA_FAIL;
3927 else
3928 {
3929 rp->rs_no = no;
3930 save_se(&rp->rs_un.sesave, &rex.reg_endpos[no],
3931 &rex.reg_endp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003932 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003933 }
3934 }
3935 break;
3936
3937#ifdef FEAT_SYN_HL
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003938 case ZCLOSE + 1: // \) after \z(
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003939 case ZCLOSE + 2:
3940 case ZCLOSE + 3:
3941 case ZCLOSE + 4:
3942 case ZCLOSE + 5:
3943 case ZCLOSE + 6:
3944 case ZCLOSE + 7:
3945 case ZCLOSE + 8:
3946 case ZCLOSE + 9:
3947 {
3948 no = op - ZCLOSE;
3949 cleanup_zsubexpr();
3950 rp = regstack_push(RS_ZCLOSE, scan);
3951 if (rp == NULL)
3952 status = RA_FAIL;
3953 else
3954 {
3955 rp->rs_no = no;
3956 save_se(&rp->rs_un.sesave, &reg_endzpos[no],
3957 &reg_endzp[no]);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003958 // We simply continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003959 }
3960 }
3961 break;
3962#endif
3963
3964 case BACKREF + 1:
3965 case BACKREF + 2:
3966 case BACKREF + 3:
3967 case BACKREF + 4:
3968 case BACKREF + 5:
3969 case BACKREF + 6:
3970 case BACKREF + 7:
3971 case BACKREF + 8:
3972 case BACKREF + 9:
3973 {
3974 int len;
3975
3976 no = op - BACKREF;
3977 cleanup_subexpr();
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003978 if (!REG_MULTI) // Single-line regexp
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003979 {
3980 if (rex.reg_startp[no] == NULL || rex.reg_endp[no] == NULL)
3981 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003982 // Backref was not set: Match an empty string.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003983 len = 0;
3984 }
3985 else
3986 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003987 // Compare current input with back-ref in the same
3988 // line.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003989 len = (int)(rex.reg_endp[no] - rex.reg_startp[no]);
3990 if (cstrncmp(rex.reg_startp[no], rex.input, &len) != 0)
3991 status = RA_NOMATCH;
3992 }
3993 }
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003994 else // Multi-line regexp
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02003995 {
3996 if (rex.reg_startpos[no].lnum < 0
3997 || rex.reg_endpos[no].lnum < 0)
3998 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02003999 // Backref was not set: Match an empty string.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004000 len = 0;
4001 }
4002 else
4003 {
4004 if (rex.reg_startpos[no].lnum == rex.lnum
4005 && rex.reg_endpos[no].lnum == rex.lnum)
4006 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004007 // Compare back-ref within the current line.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004008 len = rex.reg_endpos[no].col
4009 - rex.reg_startpos[no].col;
4010 if (cstrncmp(rex.line + rex.reg_startpos[no].col,
4011 rex.input, &len) != 0)
4012 status = RA_NOMATCH;
4013 }
4014 else
4015 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004016 // Messy situation: Need to compare between two
4017 // lines.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004018 int r = match_with_backref(
4019 rex.reg_startpos[no].lnum,
4020 rex.reg_startpos[no].col,
4021 rex.reg_endpos[no].lnum,
4022 rex.reg_endpos[no].col,
4023 &len);
4024
4025 if (r != RA_MATCH)
4026 status = r;
4027 }
4028 }
4029 }
4030
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004031 // Matched the backref, skip over it.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004032 rex.input += len;
4033 }
4034 break;
4035
4036#ifdef FEAT_SYN_HL
4037 case ZREF + 1:
4038 case ZREF + 2:
4039 case ZREF + 3:
4040 case ZREF + 4:
4041 case ZREF + 5:
4042 case ZREF + 6:
4043 case ZREF + 7:
4044 case ZREF + 8:
4045 case ZREF + 9:
4046 {
4047 int len;
4048
4049 cleanup_zsubexpr();
4050 no = op - ZREF;
4051 if (re_extmatch_in != NULL
4052 && re_extmatch_in->matches[no] != NULL)
4053 {
4054 len = (int)STRLEN(re_extmatch_in->matches[no]);
4055 if (cstrncmp(re_extmatch_in->matches[no],
4056 rex.input, &len) != 0)
4057 status = RA_NOMATCH;
4058 else
4059 rex.input += len;
4060 }
4061 else
4062 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004063 // Backref was not set: Match an empty string.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004064 }
4065 }
4066 break;
4067#endif
4068
4069 case BRANCH:
4070 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004071 if (OP(next) != BRANCH) // No choice.
4072 next = OPERAND(scan); // Avoid recursion.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004073 else
4074 {
4075 rp = regstack_push(RS_BRANCH, scan);
4076 if (rp == NULL)
4077 status = RA_FAIL;
4078 else
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004079 status = RA_BREAK; // rest is below
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004080 }
4081 }
4082 break;
4083
4084 case BRACE_LIMITS:
4085 {
4086 if (OP(next) == BRACE_SIMPLE)
4087 {
4088 bl_minval = OPERAND_MIN(scan);
4089 bl_maxval = OPERAND_MAX(scan);
4090 }
4091 else if (OP(next) >= BRACE_COMPLEX
4092 && OP(next) < BRACE_COMPLEX + 10)
4093 {
4094 no = OP(next) - BRACE_COMPLEX;
4095 brace_min[no] = OPERAND_MIN(scan);
4096 brace_max[no] = OPERAND_MAX(scan);
4097 brace_count[no] = 0;
4098 }
4099 else
4100 {
4101 internal_error("BRACE_LIMITS");
4102 status = RA_FAIL;
4103 }
4104 }
4105 break;
4106
4107 case BRACE_COMPLEX + 0:
4108 case BRACE_COMPLEX + 1:
4109 case BRACE_COMPLEX + 2:
4110 case BRACE_COMPLEX + 3:
4111 case BRACE_COMPLEX + 4:
4112 case BRACE_COMPLEX + 5:
4113 case BRACE_COMPLEX + 6:
4114 case BRACE_COMPLEX + 7:
4115 case BRACE_COMPLEX + 8:
4116 case BRACE_COMPLEX + 9:
4117 {
4118 no = op - BRACE_COMPLEX;
4119 ++brace_count[no];
4120
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004121 // If not matched enough times yet, try one more
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004122 if (brace_count[no] <= (brace_min[no] <= brace_max[no]
4123 ? brace_min[no] : brace_max[no]))
4124 {
4125 rp = regstack_push(RS_BRCPLX_MORE, scan);
4126 if (rp == NULL)
4127 status = RA_FAIL;
4128 else
4129 {
4130 rp->rs_no = no;
4131 reg_save(&rp->rs_un.regsave, &backpos);
4132 next = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004133 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004134 }
4135 break;
4136 }
4137
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004138 // If matched enough times, may try matching some more
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004139 if (brace_min[no] <= brace_max[no])
4140 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004141 // Range is the normal way around, use longest match
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004142 if (brace_count[no] <= brace_max[no])
4143 {
4144 rp = regstack_push(RS_BRCPLX_LONG, scan);
4145 if (rp == NULL)
4146 status = RA_FAIL;
4147 else
4148 {
4149 rp->rs_no = no;
4150 reg_save(&rp->rs_un.regsave, &backpos);
4151 next = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004152 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004153 }
4154 }
4155 }
4156 else
4157 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004158 // Range is backwards, use shortest match first
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004159 if (brace_count[no] <= brace_min[no])
4160 {
4161 rp = regstack_push(RS_BRCPLX_SHORT, scan);
4162 if (rp == NULL)
4163 status = RA_FAIL;
4164 else
4165 {
4166 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004167 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004168 }
4169 }
4170 }
4171 }
4172 break;
4173
4174 case BRACE_SIMPLE:
4175 case STAR:
4176 case PLUS:
4177 {
4178 regstar_T rst;
4179
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004180 // Lookahead to avoid useless match attempts when we know
4181 // what character comes next.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004182 if (OP(next) == EXACTLY)
4183 {
4184 rst.nextb = *OPERAND(next);
4185 if (rex.reg_ic)
4186 {
4187 if (MB_ISUPPER(rst.nextb))
4188 rst.nextb_ic = MB_TOLOWER(rst.nextb);
4189 else
4190 rst.nextb_ic = MB_TOUPPER(rst.nextb);
4191 }
4192 else
4193 rst.nextb_ic = rst.nextb;
4194 }
4195 else
4196 {
4197 rst.nextb = NUL;
4198 rst.nextb_ic = NUL;
4199 }
4200 if (op != BRACE_SIMPLE)
4201 {
4202 rst.minval = (op == STAR) ? 0 : 1;
4203 rst.maxval = MAX_LIMIT;
4204 }
4205 else
4206 {
4207 rst.minval = bl_minval;
4208 rst.maxval = bl_maxval;
4209 }
4210
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004211 // When maxval > minval, try matching as much as possible, up
4212 // to maxval. When maxval < minval, try matching at least the
4213 // minimal number (since the range is backwards, that's also
4214 // maxval!).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004215 rst.count = regrepeat(OPERAND(scan), rst.maxval);
4216 if (got_int)
4217 {
4218 status = RA_FAIL;
4219 break;
4220 }
4221 if (rst.minval <= rst.maxval
4222 ? rst.count >= rst.minval : rst.count >= rst.maxval)
4223 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004224 // It could match. Prepare for trying to match what
4225 // follows. The code is below. Parameters are stored in
4226 // a regstar_T on the regstack.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004227 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
4228 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004229 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004230 status = RA_FAIL;
4231 }
4232 else if (ga_grow(&regstack, sizeof(regstar_T)) == FAIL)
4233 status = RA_FAIL;
4234 else
4235 {
4236 regstack.ga_len += sizeof(regstar_T);
4237 rp = regstack_push(rst.minval <= rst.maxval
4238 ? RS_STAR_LONG : RS_STAR_SHORT, scan);
4239 if (rp == NULL)
4240 status = RA_FAIL;
4241 else
4242 {
4243 *(((regstar_T *)rp) - 1) = rst;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004244 status = RA_BREAK; // skip the restore bits
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004245 }
4246 }
4247 }
4248 else
4249 status = RA_NOMATCH;
4250
4251 }
4252 break;
4253
4254 case NOMATCH:
4255 case MATCH:
4256 case SUBPAT:
4257 rp = regstack_push(RS_NOMATCH, scan);
4258 if (rp == NULL)
4259 status = RA_FAIL;
4260 else
4261 {
4262 rp->rs_no = op;
4263 reg_save(&rp->rs_un.regsave, &backpos);
4264 next = OPERAND(scan);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004265 // We continue and handle the result when done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004266 }
4267 break;
4268
4269 case BEHIND:
4270 case NOBEHIND:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004271 // Need a bit of room to store extra positions.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004272 if ((long)((unsigned)regstack.ga_len >> 10) >= p_mmp)
4273 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00004274 emsg(_(e_pattern_uses_more_memory_than_maxmempattern));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004275 status = RA_FAIL;
4276 }
4277 else if (ga_grow(&regstack, sizeof(regbehind_T)) == FAIL)
4278 status = RA_FAIL;
4279 else
4280 {
4281 regstack.ga_len += sizeof(regbehind_T);
4282 rp = regstack_push(RS_BEHIND1, scan);
4283 if (rp == NULL)
4284 status = RA_FAIL;
4285 else
4286 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004287 // Need to save the subexpr to be able to restore them
4288 // when there is a match but we don't use it.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004289 save_subexpr(((regbehind_T *)rp) - 1);
4290
4291 rp->rs_no = op;
4292 reg_save(&rp->rs_un.regsave, &backpos);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004293 // First try if what follows matches. If it does then we
4294 // check the behind match by looping.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004295 }
4296 }
4297 break;
4298
4299 case BHPOS:
4300 if (REG_MULTI)
4301 {
4302 if (behind_pos.rs_u.pos.col != (colnr_T)(rex.input - rex.line)
4303 || behind_pos.rs_u.pos.lnum != rex.lnum)
4304 status = RA_NOMATCH;
4305 }
4306 else if (behind_pos.rs_u.ptr != rex.input)
4307 status = RA_NOMATCH;
4308 break;
4309
4310 case NEWL:
4311 if ((c != NUL || !REG_MULTI || rex.lnum > rex.reg_maxline
4312 || rex.reg_line_lbr)
4313 && (c != '\n' || !rex.reg_line_lbr))
4314 status = RA_NOMATCH;
4315 else if (rex.reg_line_lbr)
4316 ADVANCE_REGINPUT();
4317 else
4318 reg_nextline();
4319 break;
4320
4321 case END:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004322 status = RA_MATCH; // Success!
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004323 break;
4324
4325 default:
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004326 iemsg(_(e_corrupted_regexp_program));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004327#ifdef DEBUG
4328 printf("Illegal op code %d\n", op);
4329#endif
4330 status = RA_FAIL;
4331 break;
4332 }
4333 }
4334
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004335 // If we can't continue sequentially, break the inner loop.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004336 if (status != RA_CONT)
4337 break;
4338
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004339 // Continue in inner loop, advance to next item.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004340 scan = next;
4341
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004342 } // end of inner loop
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004343
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004344 // If there is something on the regstack execute the code for the state.
4345 // If the state is popped then loop and use the older state.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004346 while (regstack.ga_len > 0 && status != RA_FAIL)
4347 {
4348 rp = (regitem_T *)((char *)regstack.ga_data + regstack.ga_len) - 1;
4349 switch (rp->rs_state)
4350 {
4351 case RS_NOPEN:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004352 // Result is passed on as-is, simply pop the state.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004353 regstack_pop(&scan);
4354 break;
4355
4356 case RS_MOPEN:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004357 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004358 if (status == RA_NOMATCH)
4359 restore_se(&rp->rs_un.sesave, &rex.reg_startpos[rp->rs_no],
4360 &rex.reg_startp[rp->rs_no]);
4361 regstack_pop(&scan);
4362 break;
4363
4364#ifdef FEAT_SYN_HL
4365 case RS_ZOPEN:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004366 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004367 if (status == RA_NOMATCH)
4368 restore_se(&rp->rs_un.sesave, &reg_startzpos[rp->rs_no],
4369 &reg_startzp[rp->rs_no]);
4370 regstack_pop(&scan);
4371 break;
4372#endif
4373
4374 case RS_MCLOSE:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004375 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004376 if (status == RA_NOMATCH)
4377 restore_se(&rp->rs_un.sesave, &rex.reg_endpos[rp->rs_no],
4378 &rex.reg_endp[rp->rs_no]);
4379 regstack_pop(&scan);
4380 break;
4381
4382#ifdef FEAT_SYN_HL
4383 case RS_ZCLOSE:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004384 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004385 if (status == RA_NOMATCH)
4386 restore_se(&rp->rs_un.sesave, &reg_endzpos[rp->rs_no],
4387 &reg_endzp[rp->rs_no]);
4388 regstack_pop(&scan);
4389 break;
4390#endif
4391
4392 case RS_BRANCH:
4393 if (status == RA_MATCH)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004394 // this branch matched, use it
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004395 regstack_pop(&scan);
4396 else
4397 {
4398 if (status != RA_BREAK)
4399 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004400 // After a non-matching branch: try next one.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004401 reg_restore(&rp->rs_un.regsave, &backpos);
4402 scan = rp->rs_scan;
4403 }
4404 if (scan == NULL || OP(scan) != BRANCH)
4405 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004406 // no more branches, didn't find a match
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004407 status = RA_NOMATCH;
4408 regstack_pop(&scan);
4409 }
4410 else
4411 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004412 // Prepare to try a branch.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004413 rp->rs_scan = regnext(scan);
4414 reg_save(&rp->rs_un.regsave, &backpos);
4415 scan = OPERAND(scan);
4416 }
4417 }
4418 break;
4419
4420 case RS_BRCPLX_MORE:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004421 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004422 if (status == RA_NOMATCH)
4423 {
4424 reg_restore(&rp->rs_un.regsave, &backpos);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004425 --brace_count[rp->rs_no]; // decrement match count
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004426 }
4427 regstack_pop(&scan);
4428 break;
4429
4430 case RS_BRCPLX_LONG:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004431 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004432 if (status == RA_NOMATCH)
4433 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004434 // There was no match, but we did find enough matches.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004435 reg_restore(&rp->rs_un.regsave, &backpos);
4436 --brace_count[rp->rs_no];
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004437 // continue with the items after "\{}"
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004438 status = RA_CONT;
4439 }
4440 regstack_pop(&scan);
4441 if (status == RA_CONT)
4442 scan = regnext(scan);
4443 break;
4444
4445 case RS_BRCPLX_SHORT:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004446 // Pop the state. Restore pointers when there is no match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004447 if (status == RA_NOMATCH)
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004448 // There was no match, try to match one more item.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004449 reg_restore(&rp->rs_un.regsave, &backpos);
4450 regstack_pop(&scan);
4451 if (status == RA_NOMATCH)
4452 {
4453 scan = OPERAND(scan);
4454 status = RA_CONT;
4455 }
4456 break;
4457
4458 case RS_NOMATCH:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004459 // Pop the state. If the operand matches for NOMATCH or
4460 // doesn't match for MATCH/SUBPAT, we fail. Otherwise backup,
4461 // except for SUBPAT, and continue with the next item.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004462 if (status == (rp->rs_no == NOMATCH ? RA_MATCH : RA_NOMATCH))
4463 status = RA_NOMATCH;
4464 else
4465 {
4466 status = RA_CONT;
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004467 if (rp->rs_no != SUBPAT) // zero-width
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004468 reg_restore(&rp->rs_un.regsave, &backpos);
4469 }
4470 regstack_pop(&scan);
4471 if (status == RA_CONT)
4472 scan = regnext(scan);
4473 break;
4474
4475 case RS_BEHIND1:
4476 if (status == RA_NOMATCH)
4477 {
4478 regstack_pop(&scan);
4479 regstack.ga_len -= sizeof(regbehind_T);
4480 }
4481 else
4482 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004483 // The stuff after BEHIND/NOBEHIND matches. Now try if
4484 // the behind part does (not) match before the current
4485 // position in the input. This must be done at every
4486 // position in the input and checking if the match ends at
4487 // the current position.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004488
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004489 // save the position after the found match for next
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004490 reg_save(&(((regbehind_T *)rp) - 1)->save_after, &backpos);
4491
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004492 // Start looking for a match with operand at the current
4493 // position. Go back one character until we find the
4494 // result, hitting the start of the line or the previous
4495 // line (for multi-line matching).
4496 // Set behind_pos to where the match should end, BHPOS
4497 // will match it. Save the current value.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004498 (((regbehind_T *)rp) - 1)->save_behind = behind_pos;
4499 behind_pos = rp->rs_un.regsave;
4500
4501 rp->rs_state = RS_BEHIND2;
4502
4503 reg_restore(&rp->rs_un.regsave, &backpos);
4504 scan = OPERAND(rp->rs_scan) + 4;
4505 }
4506 break;
4507
4508 case RS_BEHIND2:
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004509 // Looping for BEHIND / NOBEHIND match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004510 if (status == RA_MATCH && reg_save_equal(&behind_pos))
4511 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004512 // found a match that ends where "next" started
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004513 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4514 if (rp->rs_no == BEHIND)
4515 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4516 &backpos);
4517 else
4518 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004519 // But we didn't want a match. Need to restore the
4520 // subexpr, because what follows matched, so they have
4521 // been set.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004522 status = RA_NOMATCH;
4523 restore_subexpr(((regbehind_T *)rp) - 1);
4524 }
4525 regstack_pop(&scan);
4526 regstack.ga_len -= sizeof(regbehind_T);
4527 }
4528 else
4529 {
4530 long limit;
4531
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004532 // No match or a match that doesn't end where we want it: Go
4533 // back one character. May go to previous line once.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004534 no = OK;
4535 limit = OPERAND_MIN(rp->rs_scan);
4536 if (REG_MULTI)
4537 {
4538 if (limit > 0
4539 && ((rp->rs_un.regsave.rs_u.pos.lnum
4540 < behind_pos.rs_u.pos.lnum
4541 ? (colnr_T)STRLEN(rex.line)
4542 : behind_pos.rs_u.pos.col)
4543 - rp->rs_un.regsave.rs_u.pos.col >= limit))
4544 no = FAIL;
4545 else if (rp->rs_un.regsave.rs_u.pos.col == 0)
4546 {
4547 if (rp->rs_un.regsave.rs_u.pos.lnum
4548 < behind_pos.rs_u.pos.lnum
4549 || reg_getline(
4550 --rp->rs_un.regsave.rs_u.pos.lnum)
4551 == NULL)
4552 no = FAIL;
4553 else
4554 {
4555 reg_restore(&rp->rs_un.regsave, &backpos);
4556 rp->rs_un.regsave.rs_u.pos.col =
4557 (colnr_T)STRLEN(rex.line);
4558 }
4559 }
4560 else
4561 {
4562 if (has_mbyte)
4563 {
4564 char_u *line =
4565 reg_getline(rp->rs_un.regsave.rs_u.pos.lnum);
4566
4567 rp->rs_un.regsave.rs_u.pos.col -=
4568 (*mb_head_off)(line, line
4569 + rp->rs_un.regsave.rs_u.pos.col - 1) + 1;
4570 }
4571 else
4572 --rp->rs_un.regsave.rs_u.pos.col;
4573 }
4574 }
4575 else
4576 {
4577 if (rp->rs_un.regsave.rs_u.ptr == rex.line)
4578 no = FAIL;
4579 else
4580 {
4581 MB_PTR_BACK(rex.line, rp->rs_un.regsave.rs_u.ptr);
4582 if (limit > 0 && (long)(behind_pos.rs_u.ptr
4583 - rp->rs_un.regsave.rs_u.ptr) > limit)
4584 no = FAIL;
4585 }
4586 }
4587 if (no == OK)
4588 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004589 // Advanced, prepare for finding match again.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004590 reg_restore(&rp->rs_un.regsave, &backpos);
4591 scan = OPERAND(rp->rs_scan) + 4;
4592 if (status == RA_MATCH)
4593 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004594 // We did match, so subexpr may have been changed,
4595 // need to restore them for the next try.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004596 status = RA_NOMATCH;
4597 restore_subexpr(((regbehind_T *)rp) - 1);
4598 }
4599 }
4600 else
4601 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004602 // Can't advance. For NOBEHIND that's a match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004603 behind_pos = (((regbehind_T *)rp) - 1)->save_behind;
4604 if (rp->rs_no == NOBEHIND)
4605 {
4606 reg_restore(&(((regbehind_T *)rp) - 1)->save_after,
4607 &backpos);
4608 status = RA_MATCH;
4609 }
4610 else
4611 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004612 // We do want a proper match. Need to restore the
4613 // subexpr if we had a match, because they may have
4614 // been set.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004615 if (status == RA_MATCH)
4616 {
4617 status = RA_NOMATCH;
4618 restore_subexpr(((regbehind_T *)rp) - 1);
4619 }
4620 }
4621 regstack_pop(&scan);
4622 regstack.ga_len -= sizeof(regbehind_T);
4623 }
4624 }
4625 break;
4626
4627 case RS_STAR_LONG:
4628 case RS_STAR_SHORT:
4629 {
4630 regstar_T *rst = ((regstar_T *)rp) - 1;
4631
4632 if (status == RA_MATCH)
4633 {
4634 regstack_pop(&scan);
4635 regstack.ga_len -= sizeof(regstar_T);
4636 break;
4637 }
4638
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004639 // Tried once already, restore input pointers.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004640 if (status != RA_BREAK)
4641 reg_restore(&rp->rs_un.regsave, &backpos);
4642
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004643 // Repeat until we found a position where it could match.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004644 for (;;)
4645 {
4646 if (status != RA_BREAK)
4647 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004648 // Tried first position already, advance.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004649 if (rp->rs_state == RS_STAR_LONG)
4650 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004651 // Trying for longest match, but couldn't or
4652 // didn't match -- back up one char.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004653 if (--rst->count < rst->minval)
4654 break;
4655 if (rex.input == rex.line)
4656 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004657 // backup to last char of previous line
Bram Moolenaar6456fae2022-02-22 13:37:31 +00004658 if (rex.lnum == 0)
4659 {
4660 status = RA_NOMATCH;
4661 break;
4662 }
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004663 --rex.lnum;
4664 rex.line = reg_getline(rex.lnum);
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004665 // Just in case regrepeat() didn't count
4666 // right.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004667 if (rex.line == NULL)
4668 break;
4669 rex.input = rex.line + STRLEN(rex.line);
4670 fast_breakcheck();
4671 }
4672 else
4673 MB_PTR_BACK(rex.line, rex.input);
4674 }
4675 else
4676 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004677 // Range is backwards, use shortest match first.
4678 // Careful: maxval and minval are exchanged!
4679 // Couldn't or didn't match: try advancing one
4680 // char.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004681 if (rst->count == rst->minval
4682 || regrepeat(OPERAND(rp->rs_scan), 1L) == 0)
4683 break;
4684 ++rst->count;
4685 }
4686 if (got_int)
4687 break;
4688 }
4689 else
4690 status = RA_NOMATCH;
4691
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004692 // If it could match, try it.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004693 if (rst->nextb == NUL || *rex.input == rst->nextb
4694 || *rex.input == rst->nextb_ic)
4695 {
4696 reg_save(&rp->rs_un.regsave, &backpos);
4697 scan = regnext(rp->rs_scan);
4698 status = RA_CONT;
4699 break;
4700 }
4701 }
4702 if (status != RA_CONT)
4703 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004704 // Failed.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004705 regstack_pop(&scan);
4706 regstack.ga_len -= sizeof(regstar_T);
4707 status = RA_NOMATCH;
4708 }
4709 }
4710 break;
4711 }
4712
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004713 // If we want to continue the inner loop or didn't pop a state
4714 // continue matching loop
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004715 if (status == RA_CONT || rp == (regitem_T *)
4716 ((char *)regstack.ga_data + regstack.ga_len) - 1)
4717 break;
Bram Moolenaar616592e2022-06-17 15:17:10 +01004718
4719#ifdef FEAT_RELTIME
4720 if (bt_did_time_out(timed_out))
4721 {
4722 status = RA_FAIL;
4723 break;
4724 }
4725#endif
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004726 }
4727
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004728 // May need to continue with the inner loop, starting at "scan".
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004729 if (status == RA_CONT)
4730 continue;
4731
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004732 // If the regstack is empty or something failed we are done.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004733 if (regstack.ga_len == 0 || status == RA_FAIL)
4734 {
4735 if (scan == NULL)
4736 {
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004737 // We get here only if there's trouble -- normally "case END" is
4738 // the terminating point.
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004739 iemsg(_(e_corrupted_regexp_program));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004740#ifdef DEBUG
4741 printf("Premature EOL\n");
4742#endif
4743 }
4744 return (status == RA_MATCH);
4745 }
4746
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004747 } // End of loop until the regstack is empty.
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004748
Bram Moolenaar9490b9a2019-09-08 17:20:12 +02004749 // NOTREACHED
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004750}
4751
4752/*
4753 * regtry - try match of "prog" with at rex.line["col"].
4754 * Returns 0 for failure, number of lines contained in the match otherwise.
4755 */
4756 static long
4757regtry(
4758 bt_regprog_T *prog,
4759 colnr_T col,
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004760 int *timed_out) // flag set on timeout or NULL
4761{
4762 rex.input = rex.line + col;
4763 rex.need_clear_subexpr = TRUE;
4764#ifdef FEAT_SYN_HL
4765 // Clear the external match subpointers if necessary.
4766 rex.need_clear_zsubexpr = (prog->reghasz == REX_SET);
4767#endif
4768
Paul Ollis65745772022-06-05 16:55:54 +01004769 if (regmatch(prog->program + 1, timed_out) == 0)
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004770 return 0;
4771
4772 cleanup_subexpr();
4773 if (REG_MULTI)
4774 {
4775 if (rex.reg_startpos[0].lnum < 0)
4776 {
4777 rex.reg_startpos[0].lnum = 0;
4778 rex.reg_startpos[0].col = col;
4779 }
4780 if (rex.reg_endpos[0].lnum < 0)
4781 {
4782 rex.reg_endpos[0].lnum = rex.lnum;
4783 rex.reg_endpos[0].col = (int)(rex.input - rex.line);
4784 }
4785 else
4786 // Use line number of "\ze".
4787 rex.lnum = rex.reg_endpos[0].lnum;
4788 }
4789 else
4790 {
4791 if (rex.reg_startp[0] == NULL)
4792 rex.reg_startp[0] = rex.line + col;
4793 if (rex.reg_endp[0] == NULL)
4794 rex.reg_endp[0] = rex.input;
4795 }
4796#ifdef FEAT_SYN_HL
4797 // Package any found \z(...\) matches for export. Default is none.
4798 unref_extmatch(re_extmatch_out);
4799 re_extmatch_out = NULL;
4800
4801 if (prog->reghasz == REX_SET)
4802 {
4803 int i;
4804
4805 cleanup_zsubexpr();
4806 re_extmatch_out = make_extmatch();
Bram Moolenaar7c77b342019-12-22 19:40:40 +01004807 if (re_extmatch_out == NULL)
4808 return 0;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004809 for (i = 0; i < NSUBEXP; i++)
4810 {
4811 if (REG_MULTI)
4812 {
4813 // Only accept single line matches.
4814 if (reg_startzpos[i].lnum >= 0
4815 && reg_endzpos[i].lnum == reg_startzpos[i].lnum
4816 && reg_endzpos[i].col >= reg_startzpos[i].col)
4817 re_extmatch_out->matches[i] =
4818 vim_strnsave(reg_getline(reg_startzpos[i].lnum)
4819 + reg_startzpos[i].col,
4820 reg_endzpos[i].col - reg_startzpos[i].col);
4821 }
4822 else
4823 {
4824 if (reg_startzp[i] != NULL && reg_endzp[i] != NULL)
4825 re_extmatch_out->matches[i] =
4826 vim_strnsave(reg_startzp[i],
Bram Moolenaar71ccd032020-06-12 22:59:11 +02004827 reg_endzp[i] - reg_startzp[i]);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004828 }
4829 }
4830 }
4831#endif
4832 return 1 + rex.lnum;
4833}
4834
4835/*
4836 * Match a regexp against a string ("line" points to the string) or multiple
Bram Moolenaardf365142021-05-03 20:01:45 +02004837 * lines (if "line" is NULL, use reg_getline()).
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004838 * Returns 0 for failure, number of lines contained in the match otherwise.
4839 */
4840 static long
4841bt_regexec_both(
4842 char_u *line,
4843 colnr_T col, // column to start looking for match
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004844 int *timed_out) // flag set on timeout or NULL
4845{
4846 bt_regprog_T *prog;
4847 char_u *s;
4848 long retval = 0L;
4849
4850 // Create "regstack" and "backpos" if they are not allocated yet.
4851 // We allocate *_INITIAL amount of bytes first and then set the grow size
4852 // to much bigger value to avoid many malloc calls in case of deep regular
4853 // expressions.
4854 if (regstack.ga_data == NULL)
4855 {
4856 // Use an item size of 1 byte, since we push different things
4857 // onto the regstack.
4858 ga_init2(&regstack, 1, REGSTACK_INITIAL);
4859 (void)ga_grow(&regstack, REGSTACK_INITIAL);
4860 regstack.ga_growsize = REGSTACK_INITIAL * 8;
4861 }
4862
4863 if (backpos.ga_data == NULL)
4864 {
4865 ga_init2(&backpos, sizeof(backpos_T), BACKPOS_INITIAL);
4866 (void)ga_grow(&backpos, BACKPOS_INITIAL);
4867 backpos.ga_growsize = BACKPOS_INITIAL * 8;
4868 }
4869
4870 if (REG_MULTI)
4871 {
4872 prog = (bt_regprog_T *)rex.reg_mmatch->regprog;
4873 line = reg_getline((linenr_T)0);
4874 rex.reg_startpos = rex.reg_mmatch->startpos;
4875 rex.reg_endpos = rex.reg_mmatch->endpos;
4876 }
4877 else
4878 {
4879 prog = (bt_regprog_T *)rex.reg_match->regprog;
4880 rex.reg_startp = rex.reg_match->startp;
4881 rex.reg_endp = rex.reg_match->endp;
4882 }
4883
4884 // Be paranoid...
4885 if (prog == NULL || line == NULL)
4886 {
Bram Moolenaare29a27f2021-07-20 21:07:36 +02004887 iemsg(_(e_null_argument));
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004888 goto theend;
4889 }
4890
4891 // Check validity of program.
4892 if (prog_magic_wrong())
4893 goto theend;
4894
4895 // If the start column is past the maximum column: no need to try.
4896 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
4897 goto theend;
4898
4899 // If pattern contains "\c" or "\C": overrule value of rex.reg_ic
4900 if (prog->regflags & RF_ICASE)
4901 rex.reg_ic = TRUE;
4902 else if (prog->regflags & RF_NOICASE)
4903 rex.reg_ic = FALSE;
4904
4905 // If pattern contains "\Z" overrule value of rex.reg_icombine
4906 if (prog->regflags & RF_ICOMBINE)
4907 rex.reg_icombine = TRUE;
4908
4909 // If there is a "must appear" string, look for it.
4910 if (prog->regmust != NULL)
4911 {
4912 int c;
4913
4914 if (has_mbyte)
4915 c = (*mb_ptr2char)(prog->regmust);
4916 else
4917 c = *prog->regmust;
4918 s = line + col;
4919
4920 // This is used very often, esp. for ":global". Use three versions of
4921 // the loop to avoid overhead of conditions.
4922 if (!rex.reg_ic && !has_mbyte)
4923 while ((s = vim_strbyte(s, c)) != NULL)
4924 {
4925 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
4926 break; // Found it.
4927 ++s;
4928 }
4929 else if (!rex.reg_ic || (!enc_utf8 && mb_char2len(c) > 1))
4930 while ((s = vim_strchr(s, c)) != NULL)
4931 {
4932 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
4933 break; // Found it.
4934 MB_PTR_ADV(s);
4935 }
4936 else
4937 while ((s = cstrchr(s, c)) != NULL)
4938 {
4939 if (cstrncmp(s, prog->regmust, &prog->regmlen) == 0)
4940 break; // Found it.
4941 MB_PTR_ADV(s);
4942 }
4943 if (s == NULL) // Not present.
4944 goto theend;
4945 }
4946
4947 rex.line = line;
4948 rex.lnum = 0;
4949 reg_toolong = FALSE;
4950
4951 // Simplest case: Anchored match need be tried only once.
4952 if (prog->reganch)
4953 {
4954 int c;
4955
4956 if (has_mbyte)
4957 c = (*mb_ptr2char)(rex.line + col);
4958 else
4959 c = rex.line[col];
4960 if (prog->regstart == NUL
4961 || prog->regstart == c
4962 || (rex.reg_ic
4963 && (((enc_utf8 && utf_fold(prog->regstart) == utf_fold(c)))
4964 || (c < 255 && prog->regstart < 255 &&
4965 MB_TOLOWER(prog->regstart) == MB_TOLOWER(c)))))
Paul Ollis65745772022-06-05 16:55:54 +01004966 retval = regtry(prog, col, timed_out);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004967 else
4968 retval = 0;
4969 }
4970 else
4971 {
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004972 // Messy cases: unanchored match.
4973 while (!got_int)
4974 {
4975 if (prog->regstart != NUL)
4976 {
4977 // Skip until the char we know it must start with.
4978 // Used often, do some work to avoid call overhead.
4979 if (!rex.reg_ic && !has_mbyte)
4980 s = vim_strbyte(rex.line + col, prog->regstart);
4981 else
4982 s = cstrchr(rex.line + col, prog->regstart);
4983 if (s == NULL)
4984 {
4985 retval = 0;
4986 break;
4987 }
4988 col = (int)(s - rex.line);
4989 }
4990
4991 // Check for maximum column to try.
4992 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol)
4993 {
4994 retval = 0;
4995 break;
4996 }
4997
Paul Ollis65745772022-06-05 16:55:54 +01004998 retval = regtry(prog, col, timed_out);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02004999 if (retval > 0)
5000 break;
5001
5002 // if not currently on the first line, get it again
5003 if (rex.lnum != 0)
5004 {
5005 rex.lnum = 0;
5006 rex.line = reg_getline((linenr_T)0);
5007 }
5008 if (rex.line[col] == NUL)
5009 break;
5010 if (has_mbyte)
5011 col += (*mb_ptr2len)(rex.line + col);
5012 else
5013 ++col;
5014#ifdef FEAT_RELTIME
Bram Moolenaar616592e2022-06-17 15:17:10 +01005015 if (bt_did_time_out(timed_out))
Paul Ollis65745772022-06-05 16:55:54 +01005016 break;
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02005017#endif
5018 }
5019 }
5020
5021theend:
5022 // Free "reg_tofree" when it's a bit big.
5023 // Free regstack and backpos if they are bigger than their initial size.
5024 if (reg_tofreelen > 400)
5025 VIM_CLEAR(reg_tofree);
5026 if (regstack.ga_maxlen > REGSTACK_INITIAL)
5027 ga_clear(&regstack);
5028 if (backpos.ga_maxlen > BACKPOS_INITIAL)
5029 ga_clear(&backpos);
5030
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005031 if (retval > 0)
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005032 {
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005033 // Make sure the end is never before the start. Can happen when \zs
5034 // and \ze are used.
5035 if (REG_MULTI)
5036 {
5037 lpos_T *start = &rex.reg_mmatch->startpos[0];
5038 lpos_T *end = &rex.reg_mmatch->endpos[0];
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005039
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005040 if (end->lnum < start->lnum
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005041 || (end->lnum == start->lnum && end->col < start->col))
Bram Moolenaara3d10a52020-12-21 18:24:00 +01005042 rex.reg_mmatch->endpos[0] = rex.reg_mmatch->startpos[0];
5043 }
5044 else
5045 {
5046 if (rex.reg_match->endp[0] < rex.reg_match->startp[0])
5047 rex.reg_match->endp[0] = rex.reg_match->startp[0];
5048 }
Bram Moolenaara7a691c2020-12-09 16:36:04 +01005049 }
5050
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02005051 return retval;
5052}
5053
5054/*
5055 * Match a regexp against a string.
5056 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5057 * Uses curbuf for line count and 'iskeyword'.
5058 * if "line_lbr" is TRUE consider a "\n" in "line" to be a line break.
5059 *
5060 * Returns 0 for failure, number of lines contained in the match otherwise.
5061 */
5062 static int
5063bt_regexec_nl(
5064 regmatch_T *rmp,
5065 char_u *line, // string to match against
5066 colnr_T col, // column to start looking for match
5067 int line_lbr)
5068{
5069 rex.reg_match = rmp;
5070 rex.reg_mmatch = NULL;
5071 rex.reg_maxline = 0;
5072 rex.reg_line_lbr = line_lbr;
5073 rex.reg_buf = curbuf;
5074 rex.reg_win = NULL;
5075 rex.reg_ic = rmp->rm_ic;
5076 rex.reg_icombine = FALSE;
5077 rex.reg_maxcol = 0;
5078
Paul Ollis65745772022-06-05 16:55:54 +01005079 return bt_regexec_both(line, col, NULL);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02005080}
5081
5082/*
5083 * Match a regexp against multiple lines.
5084 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp().
5085 * Uses curbuf for line count and 'iskeyword'.
5086 *
5087 * Return zero if there is no match. Return number of lines contained in the
5088 * match otherwise.
5089 */
5090 static long
5091bt_regexec_multi(
5092 regmmatch_T *rmp,
5093 win_T *win, // window in which to search or NULL
5094 buf_T *buf, // buffer in which to search
5095 linenr_T lnum, // nr of line to start looking for match
5096 colnr_T col, // column to start looking for match
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02005097 int *timed_out) // flag set on timeout or NULL
5098{
Bram Moolenaarf4140482020-02-15 23:06:45 +01005099 init_regexec_multi(rmp, win, buf, lnum);
Paul Ollis65745772022-06-05 16:55:54 +01005100 return bt_regexec_both(NULL, col, timed_out);
Bram Moolenaar6d7d7cf2019-09-07 23:16:33 +02005101}
5102
5103/*
5104 * Compare a number with the operand of RE_LNUM, RE_COL or RE_VCOL.
5105 */
5106 static int
5107re_num_cmp(long_u val, char_u *scan)
5108{
5109 long_u n = OPERAND_MIN(scan);
5110
5111 if (OPERAND_CMP(scan) == '>')
5112 return val > n;
5113 if (OPERAND_CMP(scan) == '<')
5114 return val < n;
5115 return val == n;
5116}
5117
5118#ifdef BT_REGEXP_DUMP
5119
5120/*
5121 * regdump - dump a regexp onto stdout in vaguely comprehensible form
5122 */
5123 static void
5124regdump(char_u *pattern, bt_regprog_T *r)
5125{
5126 char_u *s;
5127 int op = EXACTLY; // Arbitrary non-END op.
5128 char_u *next;
5129 char_u *end = NULL;
5130 FILE *f;
5131
5132#ifdef BT_REGEXP_LOG
5133 f = fopen("bt_regexp_log.log", "a");
5134#else
5135 f = stdout;
5136#endif
5137 if (f == NULL)
5138 return;
5139 fprintf(f, "-------------------------------------\n\r\nregcomp(%s):\r\n", pattern);
5140
5141 s = r->program + 1;
5142 // Loop until we find the END that isn't before a referred next (an END
5143 // can also appear in a NOMATCH operand).
5144 while (op != END || s <= end)
5145 {
5146 op = OP(s);
5147 fprintf(f, "%2d%s", (int)(s - r->program), regprop(s)); // Where, what.
5148 next = regnext(s);
5149 if (next == NULL) // Next ptr.
5150 fprintf(f, "(0)");
5151 else
5152 fprintf(f, "(%d)", (int)((s - r->program) + (next - s)));
5153 if (end < next)
5154 end = next;
5155 if (op == BRACE_LIMITS)
5156 {
5157 // Two ints
5158 fprintf(f, " minval %ld, maxval %ld", OPERAND_MIN(s), OPERAND_MAX(s));
5159 s += 8;
5160 }
5161 else if (op == BEHIND || op == NOBEHIND)
5162 {
5163 // one int
5164 fprintf(f, " count %ld", OPERAND_MIN(s));
5165 s += 4;
5166 }
5167 else if (op == RE_LNUM || op == RE_COL || op == RE_VCOL)
5168 {
5169 // one int plus comparator
5170 fprintf(f, " count %ld", OPERAND_MIN(s));
5171 s += 5;
5172 }
5173 s += 3;
5174 if (op == ANYOF || op == ANYOF + ADD_NL
5175 || op == ANYBUT || op == ANYBUT + ADD_NL
5176 || op == EXACTLY)
5177 {
5178 // Literal string, where present.
5179 fprintf(f, "\nxxxxxxxxx\n");
5180 while (*s != NUL)
5181 fprintf(f, "%c", *s++);
5182 fprintf(f, "\nxxxxxxxxx\n");
5183 s++;
5184 }
5185 fprintf(f, "\r\n");
5186 }
5187
5188 // Header fields of interest.
5189 if (r->regstart != NUL)
5190 fprintf(f, "start `%s' 0x%x; ", r->regstart < 256
5191 ? (char *)transchar(r->regstart)
5192 : "multibyte", r->regstart);
5193 if (r->reganch)
5194 fprintf(f, "anchored; ");
5195 if (r->regmust != NULL)
5196 fprintf(f, "must have \"%s\"", r->regmust);
5197 fprintf(f, "\r\n");
5198
5199#ifdef BT_REGEXP_LOG
5200 fclose(f);
5201#endif
5202}
5203#endif // BT_REGEXP_DUMP
5204
5205#ifdef DEBUG
5206/*
5207 * regprop - printable representation of opcode
5208 */
5209 static char_u *
5210regprop(char_u *op)
5211{
5212 char *p;
5213 static char buf[50];
5214
5215 STRCPY(buf, ":");
5216
5217 switch ((int) OP(op))
5218 {
5219 case BOL:
5220 p = "BOL";
5221 break;
5222 case EOL:
5223 p = "EOL";
5224 break;
5225 case RE_BOF:
5226 p = "BOF";
5227 break;
5228 case RE_EOF:
5229 p = "EOF";
5230 break;
5231 case CURSOR:
5232 p = "CURSOR";
5233 break;
5234 case RE_VISUAL:
5235 p = "RE_VISUAL";
5236 break;
5237 case RE_LNUM:
5238 p = "RE_LNUM";
5239 break;
5240 case RE_MARK:
5241 p = "RE_MARK";
5242 break;
5243 case RE_COL:
5244 p = "RE_COL";
5245 break;
5246 case RE_VCOL:
5247 p = "RE_VCOL";
5248 break;
5249 case BOW:
5250 p = "BOW";
5251 break;
5252 case EOW:
5253 p = "EOW";
5254 break;
5255 case ANY:
5256 p = "ANY";
5257 break;
5258 case ANY + ADD_NL:
5259 p = "ANY+NL";
5260 break;
5261 case ANYOF:
5262 p = "ANYOF";
5263 break;
5264 case ANYOF + ADD_NL:
5265 p = "ANYOF+NL";
5266 break;
5267 case ANYBUT:
5268 p = "ANYBUT";
5269 break;
5270 case ANYBUT + ADD_NL:
5271 p = "ANYBUT+NL";
5272 break;
5273 case IDENT:
5274 p = "IDENT";
5275 break;
5276 case IDENT + ADD_NL:
5277 p = "IDENT+NL";
5278 break;
5279 case SIDENT:
5280 p = "SIDENT";
5281 break;
5282 case SIDENT + ADD_NL:
5283 p = "SIDENT+NL";
5284 break;
5285 case KWORD:
5286 p = "KWORD";
5287 break;
5288 case KWORD + ADD_NL:
5289 p = "KWORD+NL";
5290 break;
5291 case SKWORD:
5292 p = "SKWORD";
5293 break;
5294 case SKWORD + ADD_NL:
5295 p = "SKWORD+NL";
5296 break;
5297 case FNAME:
5298 p = "FNAME";
5299 break;
5300 case FNAME + ADD_NL:
5301 p = "FNAME+NL";
5302 break;
5303 case SFNAME:
5304 p = "SFNAME";
5305 break;
5306 case SFNAME + ADD_NL:
5307 p = "SFNAME+NL";
5308 break;
5309 case PRINT:
5310 p = "PRINT";
5311 break;
5312 case PRINT + ADD_NL:
5313 p = "PRINT+NL";
5314 break;
5315 case SPRINT:
5316 p = "SPRINT";
5317 break;
5318 case SPRINT + ADD_NL:
5319 p = "SPRINT+NL";
5320 break;
5321 case WHITE:
5322 p = "WHITE";
5323 break;
5324 case WHITE + ADD_NL:
5325 p = "WHITE+NL";
5326 break;
5327 case NWHITE:
5328 p = "NWHITE";
5329 break;
5330 case NWHITE + ADD_NL:
5331 p = "NWHITE+NL";
5332 break;
5333 case DIGIT:
5334 p = "DIGIT";
5335 break;
5336 case DIGIT + ADD_NL:
5337 p = "DIGIT+NL";
5338 break;
5339 case NDIGIT:
5340 p = "NDIGIT";
5341 break;
5342 case NDIGIT + ADD_NL:
5343 p = "NDIGIT+NL";
5344 break;
5345 case HEX:
5346 p = "HEX";
5347 break;
5348 case HEX + ADD_NL:
5349 p = "HEX+NL";
5350 break;
5351 case NHEX:
5352 p = "NHEX";
5353 break;
5354 case NHEX + ADD_NL:
5355 p = "NHEX+NL";
5356 break;
5357 case OCTAL:
5358 p = "OCTAL";
5359 break;
5360 case OCTAL + ADD_NL:
5361 p = "OCTAL+NL";
5362 break;
5363 case NOCTAL:
5364 p = "NOCTAL";
5365 break;
5366 case NOCTAL + ADD_NL:
5367 p = "NOCTAL+NL";
5368 break;
5369 case WORD:
5370 p = "WORD";
5371 break;
5372 case WORD + ADD_NL:
5373 p = "WORD+NL";
5374 break;
5375 case NWORD:
5376 p = "NWORD";
5377 break;
5378 case NWORD + ADD_NL:
5379 p = "NWORD+NL";
5380 break;
5381 case HEAD:
5382 p = "HEAD";
5383 break;
5384 case HEAD + ADD_NL:
5385 p = "HEAD+NL";
5386 break;
5387 case NHEAD:
5388 p = "NHEAD";
5389 break;
5390 case NHEAD + ADD_NL:
5391 p = "NHEAD+NL";
5392 break;
5393 case ALPHA:
5394 p = "ALPHA";
5395 break;
5396 case ALPHA + ADD_NL:
5397 p = "ALPHA+NL";
5398 break;
5399 case NALPHA:
5400 p = "NALPHA";
5401 break;
5402 case NALPHA + ADD_NL:
5403 p = "NALPHA+NL";
5404 break;
5405 case LOWER:
5406 p = "LOWER";
5407 break;
5408 case LOWER + ADD_NL:
5409 p = "LOWER+NL";
5410 break;
5411 case NLOWER:
5412 p = "NLOWER";
5413 break;
5414 case NLOWER + ADD_NL:
5415 p = "NLOWER+NL";
5416 break;
5417 case UPPER:
5418 p = "UPPER";
5419 break;
5420 case UPPER + ADD_NL:
5421 p = "UPPER+NL";
5422 break;
5423 case NUPPER:
5424 p = "NUPPER";
5425 break;
5426 case NUPPER + ADD_NL:
5427 p = "NUPPER+NL";
5428 break;
5429 case BRANCH:
5430 p = "BRANCH";
5431 break;
5432 case EXACTLY:
5433 p = "EXACTLY";
5434 break;
5435 case NOTHING:
5436 p = "NOTHING";
5437 break;
5438 case BACK:
5439 p = "BACK";
5440 break;
5441 case END:
5442 p = "END";
5443 break;
5444 case MOPEN + 0:
5445 p = "MATCH START";
5446 break;
5447 case MOPEN + 1:
5448 case MOPEN + 2:
5449 case MOPEN + 3:
5450 case MOPEN + 4:
5451 case MOPEN + 5:
5452 case MOPEN + 6:
5453 case MOPEN + 7:
5454 case MOPEN + 8:
5455 case MOPEN + 9:
5456 sprintf(buf + STRLEN(buf), "MOPEN%d", OP(op) - MOPEN);
5457 p = NULL;
5458 break;
5459 case MCLOSE + 0:
5460 p = "MATCH END";
5461 break;
5462 case MCLOSE + 1:
5463 case MCLOSE + 2:
5464 case MCLOSE + 3:
5465 case MCLOSE + 4:
5466 case MCLOSE + 5:
5467 case MCLOSE + 6:
5468 case MCLOSE + 7:
5469 case MCLOSE + 8:
5470 case MCLOSE + 9:
5471 sprintf(buf + STRLEN(buf), "MCLOSE%d", OP(op) - MCLOSE);
5472 p = NULL;
5473 break;
5474 case BACKREF + 1:
5475 case BACKREF + 2:
5476 case BACKREF + 3:
5477 case BACKREF + 4:
5478 case BACKREF + 5:
5479 case BACKREF + 6:
5480 case BACKREF + 7:
5481 case BACKREF + 8:
5482 case BACKREF + 9:
5483 sprintf(buf + STRLEN(buf), "BACKREF%d", OP(op) - BACKREF);
5484 p = NULL;
5485 break;
5486 case NOPEN:
5487 p = "NOPEN";
5488 break;
5489 case NCLOSE:
5490 p = "NCLOSE";
5491 break;
5492#ifdef FEAT_SYN_HL
5493 case ZOPEN + 1:
5494 case ZOPEN + 2:
5495 case ZOPEN + 3:
5496 case ZOPEN + 4:
5497 case ZOPEN + 5:
5498 case ZOPEN + 6:
5499 case ZOPEN + 7:
5500 case ZOPEN + 8:
5501 case ZOPEN + 9:
5502 sprintf(buf + STRLEN(buf), "ZOPEN%d", OP(op) - ZOPEN);
5503 p = NULL;
5504 break;
5505 case ZCLOSE + 1:
5506 case ZCLOSE + 2:
5507 case ZCLOSE + 3:
5508 case ZCLOSE + 4:
5509 case ZCLOSE + 5:
5510 case ZCLOSE + 6:
5511 case ZCLOSE + 7:
5512 case ZCLOSE + 8:
5513 case ZCLOSE + 9:
5514 sprintf(buf + STRLEN(buf), "ZCLOSE%d", OP(op) - ZCLOSE);
5515 p = NULL;
5516 break;
5517 case ZREF + 1:
5518 case ZREF + 2:
5519 case ZREF + 3:
5520 case ZREF + 4:
5521 case ZREF + 5:
5522 case ZREF + 6:
5523 case ZREF + 7:
5524 case ZREF + 8:
5525 case ZREF + 9:
5526 sprintf(buf + STRLEN(buf), "ZREF%d", OP(op) - ZREF);
5527 p = NULL;
5528 break;
5529#endif
5530 case STAR:
5531 p = "STAR";
5532 break;
5533 case PLUS:
5534 p = "PLUS";
5535 break;
5536 case NOMATCH:
5537 p = "NOMATCH";
5538 break;
5539 case MATCH:
5540 p = "MATCH";
5541 break;
5542 case BEHIND:
5543 p = "BEHIND";
5544 break;
5545 case NOBEHIND:
5546 p = "NOBEHIND";
5547 break;
5548 case SUBPAT:
5549 p = "SUBPAT";
5550 break;
5551 case BRACE_LIMITS:
5552 p = "BRACE_LIMITS";
5553 break;
5554 case BRACE_SIMPLE:
5555 p = "BRACE_SIMPLE";
5556 break;
5557 case BRACE_COMPLEX + 0:
5558 case BRACE_COMPLEX + 1:
5559 case BRACE_COMPLEX + 2:
5560 case BRACE_COMPLEX + 3:
5561 case BRACE_COMPLEX + 4:
5562 case BRACE_COMPLEX + 5:
5563 case BRACE_COMPLEX + 6:
5564 case BRACE_COMPLEX + 7:
5565 case BRACE_COMPLEX + 8:
5566 case BRACE_COMPLEX + 9:
5567 sprintf(buf + STRLEN(buf), "BRACE_COMPLEX%d", OP(op) - BRACE_COMPLEX);
5568 p = NULL;
5569 break;
5570 case MULTIBYTECODE:
5571 p = "MULTIBYTECODE";
5572 break;
5573 case NEWL:
5574 p = "NEWL";
5575 break;
5576 default:
5577 sprintf(buf + STRLEN(buf), "corrupt %d", OP(op));
5578 p = NULL;
5579 break;
5580 }
5581 if (p != NULL)
5582 STRCAT(buf, p);
5583 return (char_u *)buf;
5584}
5585#endif // DEBUG