Mercurial > vim
comparison src/regexp_nfa.c @ 18812:d34ec6fe207d v8.1.2394
patch 8.1.2394: using old C style comments
Commit: https://github.com/vim/vim/commit/63d9e730f726341bf41ee4f4b829253cb9879110
Author: Bram Moolenaar <Bram@vim.org>
Date: Thu Dec 5 21:10:38 2019 +0100
patch 8.1.2394: using old C style comments
Problem: Using old C style comments.
Solution: Use // comments where appropriate.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Thu, 05 Dec 2019 21:15:04 +0100 |
parents | 9e6d5a4abb1c |
children | c62d63d2b9f0 |
comparison
equal
deleted
inserted
replaced
18811:65b77e62ec07 | 18812:d34ec6fe207d |
---|---|
27 # define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log" | 27 # define NFA_REGEXP_DUMP_LOG "nfa_regexp_dump.log" |
28 # define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log" | 28 # define NFA_REGEXP_RUN_LOG "nfa_regexp_run.log" |
29 # define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log" | 29 # define NFA_REGEXP_DEBUG_LOG "nfa_regexp_debug.log" |
30 #endif | 30 #endif |
31 | 31 |
32 /* Added to NFA_ANY - NFA_NUPPER_IC to include a NL. */ | 32 // Added to NFA_ANY - NFA_NUPPER_IC to include a NL. |
33 #define NFA_ADD_NL 31 | 33 #define NFA_ADD_NL 31 |
34 | 34 |
35 enum | 35 enum |
36 { | 36 { |
37 NFA_SPLIT = -1024, | 37 NFA_SPLIT = -1024, |
38 NFA_MATCH, | 38 NFA_MATCH, |
39 NFA_EMPTY, /* matches 0-length */ | 39 NFA_EMPTY, // matches 0-length |
40 | 40 |
41 NFA_START_COLL, /* [abc] start */ | 41 NFA_START_COLL, // [abc] start |
42 NFA_END_COLL, /* [abc] end */ | 42 NFA_END_COLL, // [abc] end |
43 NFA_START_NEG_COLL, /* [^abc] start */ | 43 NFA_START_NEG_COLL, // [^abc] start |
44 NFA_END_NEG_COLL, /* [^abc] end (postfix only) */ | 44 NFA_END_NEG_COLL, // [^abc] end (postfix only) |
45 NFA_RANGE, /* range of the two previous items | 45 NFA_RANGE, // range of the two previous items |
46 * (postfix only) */ | 46 // (postfix only) |
47 NFA_RANGE_MIN, /* low end of a range */ | 47 NFA_RANGE_MIN, // low end of a range |
48 NFA_RANGE_MAX, /* high end of a range */ | 48 NFA_RANGE_MAX, // high end of a range |
49 | 49 |
50 NFA_CONCAT, /* concatenate two previous items (postfix | 50 NFA_CONCAT, // concatenate two previous items (postfix |
51 * only) */ | 51 // only) |
52 NFA_OR, /* \| (postfix only) */ | 52 NFA_OR, // \| (postfix only) |
53 NFA_STAR, /* greedy * (postfix only) */ | 53 NFA_STAR, // greedy * (postfix only) |
54 NFA_STAR_NONGREEDY, /* non-greedy * (postfix only) */ | 54 NFA_STAR_NONGREEDY, // non-greedy * (postfix only) |
55 NFA_QUEST, /* greedy \? (postfix only) */ | 55 NFA_QUEST, // greedy \? (postfix only) |
56 NFA_QUEST_NONGREEDY, /* non-greedy \? (postfix only) */ | 56 NFA_QUEST_NONGREEDY, // non-greedy \? (postfix only) |
57 | 57 |
58 NFA_BOL, /* ^ Begin line */ | 58 NFA_BOL, // ^ Begin line |
59 NFA_EOL, /* $ End line */ | 59 NFA_EOL, // $ End line |
60 NFA_BOW, /* \< Begin word */ | 60 NFA_BOW, // \< Begin word |
61 NFA_EOW, /* \> End word */ | 61 NFA_EOW, // \> End word |
62 NFA_BOF, /* \%^ Begin file */ | 62 NFA_BOF, // \%^ Begin file |
63 NFA_EOF, /* \%$ End file */ | 63 NFA_EOF, // \%$ End file |
64 NFA_NEWL, | 64 NFA_NEWL, |
65 NFA_ZSTART, /* Used for \zs */ | 65 NFA_ZSTART, // Used for \zs |
66 NFA_ZEND, /* Used for \ze */ | 66 NFA_ZEND, // Used for \ze |
67 NFA_NOPEN, /* Start of subexpression marked with \%( */ | 67 NFA_NOPEN, // Start of subexpression marked with \%( |
68 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ | 68 NFA_NCLOSE, // End of subexpr. marked with \%( ... \) |
69 NFA_START_INVISIBLE, | 69 NFA_START_INVISIBLE, |
70 NFA_START_INVISIBLE_FIRST, | 70 NFA_START_INVISIBLE_FIRST, |
71 NFA_START_INVISIBLE_NEG, | 71 NFA_START_INVISIBLE_NEG, |
72 NFA_START_INVISIBLE_NEG_FIRST, | 72 NFA_START_INVISIBLE_NEG_FIRST, |
73 NFA_START_INVISIBLE_BEFORE, | 73 NFA_START_INVISIBLE_BEFORE, |
76 NFA_START_INVISIBLE_BEFORE_NEG_FIRST, | 76 NFA_START_INVISIBLE_BEFORE_NEG_FIRST, |
77 NFA_START_PATTERN, | 77 NFA_START_PATTERN, |
78 NFA_END_INVISIBLE, | 78 NFA_END_INVISIBLE, |
79 NFA_END_INVISIBLE_NEG, | 79 NFA_END_INVISIBLE_NEG, |
80 NFA_END_PATTERN, | 80 NFA_END_PATTERN, |
81 NFA_COMPOSING, /* Next nodes in NFA are part of the | 81 NFA_COMPOSING, // Next nodes in NFA are part of the |
82 composing multibyte char */ | 82 // composing multibyte char |
83 NFA_END_COMPOSING, /* End of a composing char in the NFA */ | 83 NFA_END_COMPOSING, // End of a composing char in the NFA |
84 NFA_ANY_COMPOSING, /* \%C: Any composing characters. */ | 84 NFA_ANY_COMPOSING, // \%C: Any composing characters. |
85 NFA_OPT_CHARS, /* \%[abc] */ | 85 NFA_OPT_CHARS, // \%[abc] |
86 | 86 |
87 /* The following are used only in the postfix form, not in the NFA */ | 87 // The following are used only in the postfix form, not in the NFA |
88 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ | 88 NFA_PREV_ATOM_NO_WIDTH, // Used for \@= |
89 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ | 89 NFA_PREV_ATOM_NO_WIDTH_NEG, // Used for \@! |
90 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ | 90 NFA_PREV_ATOM_JUST_BEFORE, // Used for \@<= |
91 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ | 91 NFA_PREV_ATOM_JUST_BEFORE_NEG, // Used for \@<! |
92 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ | 92 NFA_PREV_ATOM_LIKE_PATTERN, // Used for \@> |
93 | 93 |
94 NFA_BACKREF1, /* \1 */ | 94 NFA_BACKREF1, // \1 |
95 NFA_BACKREF2, /* \2 */ | 95 NFA_BACKREF2, // \2 |
96 NFA_BACKREF3, /* \3 */ | 96 NFA_BACKREF3, // \3 |
97 NFA_BACKREF4, /* \4 */ | 97 NFA_BACKREF4, // \4 |
98 NFA_BACKREF5, /* \5 */ | 98 NFA_BACKREF5, // \5 |
99 NFA_BACKREF6, /* \6 */ | 99 NFA_BACKREF6, // \6 |
100 NFA_BACKREF7, /* \7 */ | 100 NFA_BACKREF7, // \7 |
101 NFA_BACKREF8, /* \8 */ | 101 NFA_BACKREF8, // \8 |
102 NFA_BACKREF9, /* \9 */ | 102 NFA_BACKREF9, // \9 |
103 #ifdef FEAT_SYN_HL | 103 #ifdef FEAT_SYN_HL |
104 NFA_ZREF1, /* \z1 */ | 104 NFA_ZREF1, // \z1 |
105 NFA_ZREF2, /* \z2 */ | 105 NFA_ZREF2, // \z2 |
106 NFA_ZREF3, /* \z3 */ | 106 NFA_ZREF3, // \z3 |
107 NFA_ZREF4, /* \z4 */ | 107 NFA_ZREF4, // \z4 |
108 NFA_ZREF5, /* \z5 */ | 108 NFA_ZREF5, // \z5 |
109 NFA_ZREF6, /* \z6 */ | 109 NFA_ZREF6, // \z6 |
110 NFA_ZREF7, /* \z7 */ | 110 NFA_ZREF7, // \z7 |
111 NFA_ZREF8, /* \z8 */ | 111 NFA_ZREF8, // \z8 |
112 NFA_ZREF9, /* \z9 */ | 112 NFA_ZREF9, // \z9 |
113 #endif | 113 #endif |
114 NFA_SKIP, /* Skip characters */ | 114 NFA_SKIP, // Skip characters |
115 | 115 |
116 NFA_MOPEN, | 116 NFA_MOPEN, |
117 NFA_MOPEN1, | 117 NFA_MOPEN1, |
118 NFA_MOPEN2, | 118 NFA_MOPEN2, |
119 NFA_MOPEN3, | 119 NFA_MOPEN3, |
157 NFA_ZCLOSE7, | 157 NFA_ZCLOSE7, |
158 NFA_ZCLOSE8, | 158 NFA_ZCLOSE8, |
159 NFA_ZCLOSE9, | 159 NFA_ZCLOSE9, |
160 #endif | 160 #endif |
161 | 161 |
162 /* NFA_FIRST_NL */ | 162 // NFA_FIRST_NL |
163 NFA_ANY, /* Match any one character. */ | 163 NFA_ANY, // Match any one character. |
164 NFA_IDENT, /* Match identifier char */ | 164 NFA_IDENT, // Match identifier char |
165 NFA_SIDENT, /* Match identifier char but no digit */ | 165 NFA_SIDENT, // Match identifier char but no digit |
166 NFA_KWORD, /* Match keyword char */ | 166 NFA_KWORD, // Match keyword char |
167 NFA_SKWORD, /* Match word char but no digit */ | 167 NFA_SKWORD, // Match word char but no digit |
168 NFA_FNAME, /* Match file name char */ | 168 NFA_FNAME, // Match file name char |
169 NFA_SFNAME, /* Match file name char but no digit */ | 169 NFA_SFNAME, // Match file name char but no digit |
170 NFA_PRINT, /* Match printable char */ | 170 NFA_PRINT, // Match printable char |
171 NFA_SPRINT, /* Match printable char but no digit */ | 171 NFA_SPRINT, // Match printable char but no digit |
172 NFA_WHITE, /* Match whitespace char */ | 172 NFA_WHITE, // Match whitespace char |
173 NFA_NWHITE, /* Match non-whitespace char */ | 173 NFA_NWHITE, // Match non-whitespace char |
174 NFA_DIGIT, /* Match digit char */ | 174 NFA_DIGIT, // Match digit char |
175 NFA_NDIGIT, /* Match non-digit char */ | 175 NFA_NDIGIT, // Match non-digit char |
176 NFA_HEX, /* Match hex char */ | 176 NFA_HEX, // Match hex char |
177 NFA_NHEX, /* Match non-hex char */ | 177 NFA_NHEX, // Match non-hex char |
178 NFA_OCTAL, /* Match octal char */ | 178 NFA_OCTAL, // Match octal char |
179 NFA_NOCTAL, /* Match non-octal char */ | 179 NFA_NOCTAL, // Match non-octal char |
180 NFA_WORD, /* Match word char */ | 180 NFA_WORD, // Match word char |
181 NFA_NWORD, /* Match non-word char */ | 181 NFA_NWORD, // Match non-word char |
182 NFA_HEAD, /* Match head char */ | 182 NFA_HEAD, // Match head char |
183 NFA_NHEAD, /* Match non-head char */ | 183 NFA_NHEAD, // Match non-head char |
184 NFA_ALPHA, /* Match alpha char */ | 184 NFA_ALPHA, // Match alpha char |
185 NFA_NALPHA, /* Match non-alpha char */ | 185 NFA_NALPHA, // Match non-alpha char |
186 NFA_LOWER, /* Match lowercase char */ | 186 NFA_LOWER, // Match lowercase char |
187 NFA_NLOWER, /* Match non-lowercase char */ | 187 NFA_NLOWER, // Match non-lowercase char |
188 NFA_UPPER, /* Match uppercase char */ | 188 NFA_UPPER, // Match uppercase char |
189 NFA_NUPPER, /* Match non-uppercase char */ | 189 NFA_NUPPER, // Match non-uppercase char |
190 NFA_LOWER_IC, /* Match [a-z] */ | 190 NFA_LOWER_IC, // Match [a-z] |
191 NFA_NLOWER_IC, /* Match [^a-z] */ | 191 NFA_NLOWER_IC, // Match [^a-z] |
192 NFA_UPPER_IC, /* Match [A-Z] */ | 192 NFA_UPPER_IC, // Match [A-Z] |
193 NFA_NUPPER_IC, /* Match [^A-Z] */ | 193 NFA_NUPPER_IC, // Match [^A-Z] |
194 | 194 |
195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL, | 195 NFA_FIRST_NL = NFA_ANY + NFA_ADD_NL, |
196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL, | 196 NFA_LAST_NL = NFA_NUPPER_IC + NFA_ADD_NL, |
197 | 197 |
198 NFA_CURSOR, /* Match cursor pos */ | 198 NFA_CURSOR, // Match cursor pos |
199 NFA_LNUM, /* Match line number */ | 199 NFA_LNUM, // Match line number |
200 NFA_LNUM_GT, /* Match > line number */ | 200 NFA_LNUM_GT, // Match > line number |
201 NFA_LNUM_LT, /* Match < line number */ | 201 NFA_LNUM_LT, // Match < line number |
202 NFA_COL, /* Match cursor column */ | 202 NFA_COL, // Match cursor column |
203 NFA_COL_GT, /* Match > cursor column */ | 203 NFA_COL_GT, // Match > cursor column |
204 NFA_COL_LT, /* Match < cursor column */ | 204 NFA_COL_LT, // Match < cursor column |
205 NFA_VCOL, /* Match cursor virtual column */ | 205 NFA_VCOL, // Match cursor virtual column |
206 NFA_VCOL_GT, /* Match > cursor virtual column */ | 206 NFA_VCOL_GT, // Match > cursor virtual column |
207 NFA_VCOL_LT, /* Match < cursor virtual column */ | 207 NFA_VCOL_LT, // Match < cursor virtual column |
208 NFA_MARK, /* Match mark */ | 208 NFA_MARK, // Match mark |
209 NFA_MARK_GT, /* Match > mark */ | 209 NFA_MARK_GT, // Match > mark |
210 NFA_MARK_LT, /* Match < mark */ | 210 NFA_MARK_LT, // Match < mark |
211 NFA_VISUAL, /* Match Visual area */ | 211 NFA_VISUAL, // Match Visual area |
212 | 212 |
213 /* Character classes [:alnum:] etc */ | 213 // Character classes [:alnum:] etc |
214 NFA_CLASS_ALNUM, | 214 NFA_CLASS_ALNUM, |
215 NFA_CLASS_ALPHA, | 215 NFA_CLASS_ALPHA, |
216 NFA_CLASS_BLANK, | 216 NFA_CLASS_BLANK, |
217 NFA_CLASS_CNTRL, | 217 NFA_CLASS_CNTRL, |
218 NFA_CLASS_DIGIT, | 218 NFA_CLASS_DIGIT, |
230 NFA_CLASS_IDENT, | 230 NFA_CLASS_IDENT, |
231 NFA_CLASS_KEYWORD, | 231 NFA_CLASS_KEYWORD, |
232 NFA_CLASS_FNAME | 232 NFA_CLASS_FNAME |
233 }; | 233 }; |
234 | 234 |
235 /* Keep in sync with classchars. */ | 235 // Keep in sync with classchars. |
236 static int nfa_classcodes[] = { | 236 static int nfa_classcodes[] = { |
237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, | 237 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, |
238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, | 238 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, |
239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, | 239 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, |
240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, | 240 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, |
253 static int *post_end; | 253 static int *post_end; |
254 static int *post_ptr; | 254 static int *post_ptr; |
255 static int nstate; // Number of states in the NFA. | 255 static int nstate; // Number of states in the NFA. |
256 static int istate; // Index in the state vector, used in alloc_state() | 256 static int istate; // Index in the state vector, used in alloc_state() |
257 | 257 |
258 /* If not NULL match must end at this position */ | 258 // If not NULL match must end at this position |
259 static save_se_T *nfa_endp = NULL; | 259 static save_se_T *nfa_endp = NULL; |
260 | 260 |
261 /* 0 for first call to nfa_regmatch(), 1 for recursive call. */ | 261 // 0 for first call to nfa_regmatch(), 1 for recursive call. |
262 static int nfa_ll_index = 0; | 262 static int nfa_ll_index = 0; |
263 | 263 |
264 static int realloc_post_list(void); | 264 static int realloc_post_list(void); |
265 static int nfa_reg(int paren); | 265 static int nfa_reg(int paren); |
266 #ifdef DEBUG | 266 #ifdef DEBUG |
267 static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent); | 267 static void nfa_print_state2(FILE *debugf, nfa_state_T *state, garray_T *indent); |
268 #endif | 268 #endif |
269 static int match_follows(nfa_state_T *startstate, int depth); | 269 static int match_follows(nfa_state_T *startstate, int depth); |
270 static int failure_chance(nfa_state_T *state, int depth); | 270 static int failure_chance(nfa_state_T *state, int depth); |
271 | 271 |
272 /* helper functions used when doing re2post() ... regatom() parsing */ | 272 // helper functions used when doing re2post() ... regatom() parsing |
273 #define EMIT(c) do { \ | 273 #define EMIT(c) do { \ |
274 if (post_ptr >= post_end && realloc_post_list() == FAIL) \ | 274 if (post_ptr >= post_end && realloc_post_list() == FAIL) \ |
275 return FAIL; \ | 275 return FAIL; \ |
276 *post_ptr++ = c; \ | 276 *post_ptr++ = c; \ |
277 } while (0) | 277 } while (0) |
281 * Return OK on success, FAIL otherwise. | 281 * Return OK on success, FAIL otherwise. |
282 */ | 282 */ |
283 static int | 283 static int |
284 nfa_regcomp_start( | 284 nfa_regcomp_start( |
285 char_u *expr, | 285 char_u *expr, |
286 int re_flags) /* see vim_regcomp() */ | 286 int re_flags) // see vim_regcomp() |
287 { | 287 { |
288 size_t postfix_size; | 288 size_t postfix_size; |
289 int nstate_max; | 289 int nstate_max; |
290 | 290 |
291 nstate = 0; | 291 nstate = 0; |
292 istate = 0; | 292 istate = 0; |
293 /* A reasonable estimation for maximum size */ | 293 // A reasonable estimation for maximum size |
294 nstate_max = (int)(STRLEN(expr) + 1) * 25; | 294 nstate_max = (int)(STRLEN(expr) + 1) * 25; |
295 | 295 |
296 /* Some items blow up in size, such as [A-z]. Add more space for that. | 296 // Some items blow up in size, such as [A-z]. Add more space for that. |
297 * When it is still not enough realloc_post_list() will be used. */ | 297 // When it is still not enough realloc_post_list() will be used. |
298 nstate_max += 1000; | 298 nstate_max += 1000; |
299 | 299 |
300 /* Size for postfix representation of expr. */ | 300 // Size for postfix representation of expr. |
301 postfix_size = sizeof(int) * nstate_max; | 301 postfix_size = sizeof(int) * nstate_max; |
302 | 302 |
303 post_start = alloc(postfix_size); | 303 post_start = alloc(postfix_size); |
304 if (post_start == NULL) | 304 if (post_start == NULL) |
305 return FAIL; | 305 return FAIL; |
306 post_ptr = post_start; | 306 post_ptr = post_start; |
307 post_end = post_start + nstate_max; | 307 post_end = post_start + nstate_max; |
308 rex.nfa_has_zend = FALSE; | 308 rex.nfa_has_zend = FALSE; |
309 rex.nfa_has_backref = FALSE; | 309 rex.nfa_has_backref = FALSE; |
310 | 310 |
311 /* shared with BT engine */ | 311 // shared with BT engine |
312 regcomp_start(expr, re_flags); | 312 regcomp_start(expr, re_flags); |
313 | 313 |
314 return OK; | 314 return OK; |
315 } | 315 } |
316 | 316 |
330 { | 330 { |
331 switch (p->c) | 331 switch (p->c) |
332 { | 332 { |
333 case NFA_BOL: | 333 case NFA_BOL: |
334 case NFA_BOF: | 334 case NFA_BOF: |
335 return 1; /* yes! */ | 335 return 1; // yes! |
336 | 336 |
337 case NFA_ZSTART: | 337 case NFA_ZSTART: |
338 case NFA_ZEND: | 338 case NFA_ZEND: |
339 case NFA_CURSOR: | 339 case NFA_CURSOR: |
340 case NFA_VISUAL: | 340 case NFA_VISUAL: |
368 case NFA_SPLIT: | 368 case NFA_SPLIT: |
369 return nfa_get_reganch(p->out, depth + 1) | 369 return nfa_get_reganch(p->out, depth + 1) |
370 && nfa_get_reganch(p->out1, depth + 1); | 370 && nfa_get_reganch(p->out1, depth + 1); |
371 | 371 |
372 default: | 372 default: |
373 return 0; /* noooo */ | 373 return 0; // noooo |
374 } | 374 } |
375 } | 375 } |
376 return 0; | 376 return 0; |
377 } | 377 } |
378 | 378 |
390 | 390 |
391 while (p != NULL) | 391 while (p != NULL) |
392 { | 392 { |
393 switch (p->c) | 393 switch (p->c) |
394 { | 394 { |
395 /* all kinds of zero-width matches */ | 395 // all kinds of zero-width matches |
396 case NFA_BOL: | 396 case NFA_BOL: |
397 case NFA_BOF: | 397 case NFA_BOF: |
398 case NFA_BOW: | 398 case NFA_BOW: |
399 case NFA_EOW: | 399 case NFA_EOW: |
400 case NFA_ZSTART: | 400 case NFA_ZSTART: |
444 { | 444 { |
445 int c1 = nfa_get_regstart(p->out, depth + 1); | 445 int c1 = nfa_get_regstart(p->out, depth + 1); |
446 int c2 = nfa_get_regstart(p->out1, depth + 1); | 446 int c2 = nfa_get_regstart(p->out1, depth + 1); |
447 | 447 |
448 if (c1 == c2) | 448 if (c1 == c2) |
449 return c1; /* yes! */ | 449 return c1; // yes! |
450 return 0; | 450 return 0; |
451 } | 451 } |
452 | 452 |
453 default: | 453 default: |
454 if (p->c > 0) | 454 if (p->c > 0) |
455 return p->c; /* yes! */ | 455 return p->c; // yes! |
456 return 0; | 456 return 0; |
457 } | 457 } |
458 } | 458 } |
459 return 0; | 459 return 0; |
460 } | 460 } |
471 int len = 0; | 471 int len = 0; |
472 char_u *ret; | 472 char_u *ret; |
473 char_u *s; | 473 char_u *s; |
474 | 474 |
475 if (p->c != NFA_MOPEN) | 475 if (p->c != NFA_MOPEN) |
476 return NULL; /* just in case */ | 476 return NULL; // just in case |
477 p = p->out; | 477 p = p->out; |
478 while (p->c > 0) | 478 while (p->c > 0) |
479 { | 479 { |
480 len += MB_CHAR2LEN(p->c); | 480 len += MB_CHAR2LEN(p->c); |
481 p = p->out; | 481 p = p->out; |
484 return NULL; | 484 return NULL; |
485 | 485 |
486 ret = alloc(len); | 486 ret = alloc(len); |
487 if (ret != NULL) | 487 if (ret != NULL) |
488 { | 488 { |
489 p = start->out->out; /* skip first char, it goes into regstart */ | 489 p = start->out->out; // skip first char, it goes into regstart |
490 s = ret; | 490 s = ret; |
491 while (p->c > 0) | 491 while (p->c > 0) |
492 { | 492 { |
493 if (has_mbyte) | 493 if (has_mbyte) |
494 s += (*mb_char2bytes)(p->c, s); | 494 s += (*mb_char2bytes)(p->c, s); |
631 newl = TRUE; | 631 newl = TRUE; |
632 p ++; | 632 p ++; |
633 } | 633 } |
634 else | 634 else |
635 return FAIL; | 635 return FAIL; |
636 } /* while (p < end) */ | 636 } // while (p < end) |
637 | 637 |
638 if (p != end) | 638 if (p != end) |
639 return FAIL; | 639 return FAIL; |
640 | 640 |
641 if (newl == TRUE) | 641 if (newl == TRUE) |
1159 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) | 1159 CASEMBC(0x1b6) CASEMBC(0x1e91) CASEMBC(0x1e95) |
1160 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e) | 1160 EMIT2('z'); EMITMBC(0x17a) EMITMBC(0x17c) EMITMBC(0x17e) |
1161 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95) | 1161 EMITMBC(0x1b6) EMITMBC(0x1e91) EMITMBC(0x1e95) |
1162 return OK; | 1162 return OK; |
1163 | 1163 |
1164 /* default: character itself */ | 1164 // default: character itself |
1165 } | 1165 } |
1166 } | 1166 } |
1167 | 1167 |
1168 EMIT2(c); | 1168 EMIT2(c); |
1169 return OK; | 1169 return OK; |
1239 case Magic('_'): | 1239 case Magic('_'): |
1240 c = no_Magic(getchr()); | 1240 c = no_Magic(getchr()); |
1241 if (c == NUL) | 1241 if (c == NUL) |
1242 EMSG_RET_FAIL(_(e_nul_found)); | 1242 EMSG_RET_FAIL(_(e_nul_found)); |
1243 | 1243 |
1244 if (c == '^') /* "\_^" is start-of-line */ | 1244 if (c == '^') // "\_^" is start-of-line |
1245 { | 1245 { |
1246 EMIT(NFA_BOL); | 1246 EMIT(NFA_BOL); |
1247 break; | 1247 break; |
1248 } | 1248 } |
1249 if (c == '$') /* "\_$" is end-of-line */ | 1249 if (c == '$') // "\_$" is end-of-line |
1250 { | 1250 { |
1251 EMIT(NFA_EOL); | 1251 EMIT(NFA_EOL); |
1252 #if defined(FEAT_SYN_HL) || defined(PROTO) | 1252 #if defined(FEAT_SYN_HL) || defined(PROTO) |
1253 had_eol = TRUE; | 1253 had_eol = TRUE; |
1254 #endif | 1254 #endif |
1255 break; | 1255 break; |
1256 } | 1256 } |
1257 | 1257 |
1258 extra = NFA_ADD_NL; | 1258 extra = NFA_ADD_NL; |
1259 | 1259 |
1260 /* "\_[" is collection plus newline */ | 1260 // "\_[" is collection plus newline |
1261 if (c == '[') | 1261 if (c == '[') |
1262 goto collection; | 1262 goto collection; |
1263 | 1263 |
1264 /* "\_x" is character class plus newline */ | 1264 // "\_x" is character class plus newline |
1265 /* FALLTHROUGH */ | 1265 // FALLTHROUGH |
1266 | 1266 |
1267 /* | 1267 /* |
1268 * Character classes. | 1268 * Character classes. |
1269 */ | 1269 */ |
1270 case Magic('.'): | 1270 case Magic('.'): |
1305 } | 1305 } |
1306 siemsg("INTERNAL: Unknown character class char: %d", c); | 1306 siemsg("INTERNAL: Unknown character class char: %d", c); |
1307 return FAIL; | 1307 return FAIL; |
1308 } | 1308 } |
1309 | 1309 |
1310 /* When '.' is followed by a composing char ignore the dot, so that | 1310 // When '.' is followed by a composing char ignore the dot, so that |
1311 * the composing char is matched here. */ | 1311 // the composing char is matched here. |
1312 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) | 1312 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) |
1313 { | 1313 { |
1314 old_regparse = regparse; | 1314 old_regparse = regparse; |
1315 c = getchr(); | 1315 c = getchr(); |
1316 goto nfa_do_multibyte; | 1316 goto nfa_do_multibyte; |
1324 } | 1324 } |
1325 break; | 1325 break; |
1326 | 1326 |
1327 case Magic('n'): | 1327 case Magic('n'): |
1328 if (reg_string) | 1328 if (reg_string) |
1329 /* In a string "\n" matches a newline character. */ | 1329 // In a string "\n" matches a newline character. |
1330 EMIT(NL); | 1330 EMIT(NL); |
1331 else | 1331 else |
1332 { | 1332 { |
1333 /* In buffer text "\n" matches the end of a line. */ | 1333 // In buffer text "\n" matches the end of a line. |
1334 EMIT(NFA_NEWL); | 1334 EMIT(NFA_NEWL); |
1335 regflags |= RF_HASNL; | 1335 regflags |= RF_HASNL; |
1336 } | 1336 } |
1337 break; | 1337 break; |
1338 | 1338 |
1339 case Magic('('): | 1339 case Magic('('): |
1340 if (nfa_reg(REG_PAREN) == FAIL) | 1340 if (nfa_reg(REG_PAREN) == FAIL) |
1341 return FAIL; /* cascaded error */ | 1341 return FAIL; // cascaded error |
1342 break; | 1342 break; |
1343 | 1343 |
1344 case Magic('|'): | 1344 case Magic('|'): |
1345 case Magic('&'): | 1345 case Magic('&'): |
1346 case Magic(')'): | 1346 case Magic(')'): |
1351 case Magic('?'): | 1351 case Magic('?'): |
1352 case Magic('+'): | 1352 case Magic('+'): |
1353 case Magic('@'): | 1353 case Magic('@'): |
1354 case Magic('*'): | 1354 case Magic('*'): |
1355 case Magic('{'): | 1355 case Magic('{'): |
1356 /* these should follow an atom, not form an atom */ | 1356 // these should follow an atom, not form an atom |
1357 semsg(_(e_misplaced), no_Magic(c)); | 1357 semsg(_(e_misplaced), no_Magic(c)); |
1358 return FAIL; | 1358 return FAIL; |
1359 | 1359 |
1360 case Magic('~'): | 1360 case Magic('~'): |
1361 { | 1361 { |
1362 char_u *lp; | 1362 char_u *lp; |
1363 | 1363 |
1364 /* Previous substitute pattern. | 1364 // Previous substitute pattern. |
1365 * Generated as "\%(pattern\)". */ | 1365 // Generated as "\%(pattern\)". |
1366 if (reg_prev_sub == NULL) | 1366 if (reg_prev_sub == NULL) |
1367 { | 1367 { |
1368 emsg(_(e_nopresub)); | 1368 emsg(_(e_nopresub)); |
1369 return FAIL; | 1369 return FAIL; |
1370 } | 1370 } |
1420 case '5': | 1420 case '5': |
1421 case '6': | 1421 case '6': |
1422 case '7': | 1422 case '7': |
1423 case '8': | 1423 case '8': |
1424 case '9': | 1424 case '9': |
1425 /* \z1...\z9 */ | 1425 // \z1...\z9 |
1426 if ((reg_do_extmatch & REX_USE) == 0) | 1426 if ((reg_do_extmatch & REX_USE) == 0) |
1427 EMSG_RET_FAIL(_(e_z1_not_allowed)); | 1427 EMSG_RET_FAIL(_(e_z1_not_allowed)); |
1428 EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); | 1428 EMIT(NFA_ZREF1 + (no_Magic(c) - '1')); |
1429 /* No need to set rex.nfa_has_backref, the sub-matches don't | 1429 // No need to set rex.nfa_has_backref, the sub-matches don't |
1430 * change when \z1 .. \z9 matches or not. */ | 1430 // change when \z1 .. \z9 matches or not. |
1431 re_has_z = REX_USE; | 1431 re_has_z = REX_USE; |
1432 break; | 1432 break; |
1433 case '(': | 1433 case '(': |
1434 /* \z( */ | 1434 // \z( |
1435 if ((reg_do_extmatch & REX_SET) == 0) | 1435 if ((reg_do_extmatch & REX_SET) == 0) |
1436 EMSG_RET_FAIL(_(e_z_not_allowed)); | 1436 EMSG_RET_FAIL(_(e_z_not_allowed)); |
1437 if (nfa_reg(REG_ZPAREN) == FAIL) | 1437 if (nfa_reg(REG_ZPAREN) == FAIL) |
1438 return FAIL; /* cascaded error */ | 1438 return FAIL; // cascaded error |
1439 re_has_z = REX_SET; | 1439 re_has_z = REX_SET; |
1440 break; | 1440 break; |
1441 #endif | 1441 #endif |
1442 default: | 1442 default: |
1443 semsg(_("E867: (NFA) Unknown operator '\\z%c'"), | 1443 semsg(_("E867: (NFA) Unknown operator '\\z%c'"), |
1448 | 1448 |
1449 case Magic('%'): | 1449 case Magic('%'): |
1450 c = no_Magic(getchr()); | 1450 c = no_Magic(getchr()); |
1451 switch (c) | 1451 switch (c) |
1452 { | 1452 { |
1453 /* () without a back reference */ | 1453 // () without a back reference |
1454 case '(': | 1454 case '(': |
1455 if (nfa_reg(REG_NPAREN) == FAIL) | 1455 if (nfa_reg(REG_NPAREN) == FAIL) |
1456 return FAIL; | 1456 return FAIL; |
1457 EMIT(NFA_NOPEN); | 1457 EMIT(NFA_NOPEN); |
1458 break; | 1458 break; |
1459 | 1459 |
1460 case 'd': /* %d123 decimal */ | 1460 case 'd': // %d123 decimal |
1461 case 'o': /* %o123 octal */ | 1461 case 'o': // %o123 octal |
1462 case 'x': /* %xab hex 2 */ | 1462 case 'x': // %xab hex 2 |
1463 case 'u': /* %uabcd hex 4 */ | 1463 case 'u': // %uabcd hex 4 |
1464 case 'U': /* %U1234abcd hex 8 */ | 1464 case 'U': // %U1234abcd hex 8 |
1465 { | 1465 { |
1466 long nr; | 1466 long nr; |
1467 | 1467 |
1468 switch (c) | 1468 switch (c) |
1469 { | 1469 { |
1477 | 1477 |
1478 if (nr < 0 || nr > INT_MAX) | 1478 if (nr < 0 || nr > INT_MAX) |
1479 EMSG2_RET_FAIL( | 1479 EMSG2_RET_FAIL( |
1480 _("E678: Invalid character after %s%%[dxouU]"), | 1480 _("E678: Invalid character after %s%%[dxouU]"), |
1481 reg_magic == MAGIC_ALL); | 1481 reg_magic == MAGIC_ALL); |
1482 /* A NUL is stored in the text as NL */ | 1482 // A NUL is stored in the text as NL |
1483 /* TODO: what if a composing character follows? */ | 1483 // TODO: what if a composing character follows? |
1484 EMIT(nr == 0 ? 0x0a : nr); | 1484 EMIT(nr == 0 ? 0x0a : nr); |
1485 } | 1485 } |
1486 break; | 1486 break; |
1487 | 1487 |
1488 /* Catch \%^ and \%$ regardless of where they appear in the | 1488 // Catch \%^ and \%$ regardless of where they appear in the |
1489 * pattern -- regardless of whether or not it makes sense. */ | 1489 // pattern -- regardless of whether or not it makes sense. |
1490 case '^': | 1490 case '^': |
1491 EMIT(NFA_BOF); | 1491 EMIT(NFA_BOF); |
1492 break; | 1492 break; |
1493 | 1493 |
1494 case '$': | 1494 case '$': |
1509 | 1509 |
1510 case '[': | 1510 case '[': |
1511 { | 1511 { |
1512 int n; | 1512 int n; |
1513 | 1513 |
1514 /* \%[abc] */ | 1514 // \%[abc] |
1515 for (n = 0; (c = peekchr()) != ']'; ++n) | 1515 for (n = 0; (c = peekchr()) != ']'; ++n) |
1516 { | 1516 { |
1517 if (c == NUL) | 1517 if (c == NUL) |
1518 EMSG2_RET_FAIL(_(e_missing_sb), | 1518 EMSG2_RET_FAIL(_(e_missing_sb), |
1519 reg_magic == MAGIC_ALL); | 1519 reg_magic == MAGIC_ALL); |
1520 /* recursive call! */ | 1520 // recursive call! |
1521 if (nfa_regatom() == FAIL) | 1521 if (nfa_regatom() == FAIL) |
1522 return FAIL; | 1522 return FAIL; |
1523 } | 1523 } |
1524 getchr(); /* get the ] */ | 1524 getchr(); // get the ] |
1525 if (n == 0) | 1525 if (n == 0) |
1526 EMSG2_RET_FAIL(_(e_empty_sb), | 1526 EMSG2_RET_FAIL(_(e_empty_sb), |
1527 reg_magic == MAGIC_ALL); | 1527 reg_magic == MAGIC_ALL); |
1528 EMIT(NFA_OPT_CHARS); | 1528 EMIT(NFA_OPT_CHARS); |
1529 EMIT(n); | 1529 EMIT(n); |
1530 | 1530 |
1531 /* Emit as "\%(\%[abc]\)" to be able to handle | 1531 // Emit as "\%(\%[abc]\)" to be able to handle |
1532 * "\%[abc]*" which would cause the empty string to be | 1532 // "\%[abc]*" which would cause the empty string to be |
1533 * matched an unlimited number of times. NFA_NOPEN is | 1533 // matched an unlimited number of times. NFA_NOPEN is |
1534 * added only once at a position, while NFA_SPLIT is | 1534 // added only once at a position, while NFA_SPLIT is |
1535 * added multiple times. This is more efficient than | 1535 // added multiple times. This is more efficient than |
1536 * not allowing NFA_SPLIT multiple times, it is used | 1536 // not allowing NFA_SPLIT multiple times, it is used |
1537 * a lot. */ | 1537 // a lot. |
1538 EMIT(NFA_NOPEN); | 1538 EMIT(NFA_NOPEN); |
1539 break; | 1539 break; |
1540 } | 1540 } |
1541 | 1541 |
1542 default: | 1542 default: |
1555 { | 1555 { |
1556 int limit = INT_MAX; | 1556 int limit = INT_MAX; |
1557 | 1557 |
1558 if (c == 'l') | 1558 if (c == 'l') |
1559 { | 1559 { |
1560 /* \%{n}l \%{n}<l \%{n}>l */ | 1560 // \%{n}l \%{n}<l \%{n}>l |
1561 EMIT(cmp == '<' ? NFA_LNUM_LT : | 1561 EMIT(cmp == '<' ? NFA_LNUM_LT : |
1562 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); | 1562 cmp == '>' ? NFA_LNUM_GT : NFA_LNUM); |
1563 if (save_prev_at_start) | 1563 if (save_prev_at_start) |
1564 at_start = TRUE; | 1564 at_start = TRUE; |
1565 } | 1565 } |
1566 else if (c == 'c') | 1566 else if (c == 'c') |
1567 /* \%{n}c \%{n}<c \%{n}>c */ | 1567 // \%{n}c \%{n}<c \%{n}>c |
1568 EMIT(cmp == '<' ? NFA_COL_LT : | 1568 EMIT(cmp == '<' ? NFA_COL_LT : |
1569 cmp == '>' ? NFA_COL_GT : NFA_COL); | 1569 cmp == '>' ? NFA_COL_GT : NFA_COL); |
1570 else | 1570 else |
1571 { | 1571 { |
1572 /* \%{n}v \%{n}<v \%{n}>v */ | 1572 // \%{n}v \%{n}<v \%{n}>v |
1573 EMIT(cmp == '<' ? NFA_VCOL_LT : | 1573 EMIT(cmp == '<' ? NFA_VCOL_LT : |
1574 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); | 1574 cmp == '>' ? NFA_VCOL_GT : NFA_VCOL); |
1575 limit = INT_MAX / MB_MAXBYTES; | 1575 limit = INT_MAX / MB_MAXBYTES; |
1576 } | 1576 } |
1577 if (n >= limit) | 1577 if (n >= limit) |
1582 EMIT((int)n); | 1582 EMIT((int)n); |
1583 break; | 1583 break; |
1584 } | 1584 } |
1585 else if (c == '\'' && n == 0) | 1585 else if (c == '\'' && n == 0) |
1586 { | 1586 { |
1587 /* \%'m \%<'m \%>'m */ | 1587 // \%'m \%<'m \%>'m |
1588 EMIT(cmp == '<' ? NFA_MARK_LT : | 1588 EMIT(cmp == '<' ? NFA_MARK_LT : |
1589 cmp == '>' ? NFA_MARK_GT : NFA_MARK); | 1589 cmp == '>' ? NFA_MARK_GT : NFA_MARK); |
1590 EMIT(getchr()); | 1590 EMIT(getchr()); |
1591 break; | 1591 break; |
1592 } | 1592 } |
1638 * Failed to recognize a character class. Use the simple | 1638 * Failed to recognize a character class. Use the simple |
1639 * version that turns [abc] into 'a' OR 'b' OR 'c' | 1639 * version that turns [abc] into 'a' OR 'b' OR 'c' |
1640 */ | 1640 */ |
1641 startc = endc = oldstartc = -1; | 1641 startc = endc = oldstartc = -1; |
1642 negated = FALSE; | 1642 negated = FALSE; |
1643 if (*regparse == '^') /* negated range */ | 1643 if (*regparse == '^') // negated range |
1644 { | 1644 { |
1645 negated = TRUE; | 1645 negated = TRUE; |
1646 MB_PTR_ADV(regparse); | 1646 MB_PTR_ADV(regparse); |
1647 EMIT(NFA_START_NEG_COLL); | 1647 EMIT(NFA_START_NEG_COLL); |
1648 } | 1648 } |
1653 startc = '-'; | 1653 startc = '-'; |
1654 EMIT(startc); | 1654 EMIT(startc); |
1655 EMIT(NFA_CONCAT); | 1655 EMIT(NFA_CONCAT); |
1656 MB_PTR_ADV(regparse); | 1656 MB_PTR_ADV(regparse); |
1657 } | 1657 } |
1658 /* Emit the OR branches for each character in the [] */ | 1658 // Emit the OR branches for each character in the [] |
1659 emit_range = FALSE; | 1659 emit_range = FALSE; |
1660 while (regparse < endp) | 1660 while (regparse < endp) |
1661 { | 1661 { |
1662 oldstartc = startc; | 1662 oldstartc = startc; |
1663 startc = -1; | 1663 startc = -1; |
1664 got_coll_char = FALSE; | 1664 got_coll_char = FALSE; |
1665 if (*regparse == '[') | 1665 if (*regparse == '[') |
1666 { | 1666 { |
1667 /* Check for [: :], [= =], [. .] */ | 1667 // Check for [: :], [= =], [. .] |
1668 equiclass = collclass = 0; | 1668 equiclass = collclass = 0; |
1669 charclass = get_char_class(®parse); | 1669 charclass = get_char_class(®parse); |
1670 if (charclass == CLASS_NONE) | 1670 if (charclass == CLASS_NONE) |
1671 { | 1671 { |
1672 equiclass = get_equi_class(®parse); | 1672 equiclass = get_equi_class(®parse); |
1673 if (equiclass == 0) | 1673 if (equiclass == 0) |
1674 collclass = get_coll_element(®parse); | 1674 collclass = get_coll_element(®parse); |
1675 } | 1675 } |
1676 | 1676 |
1677 /* Character class like [:alpha:] */ | 1677 // Character class like [:alpha:] |
1678 if (charclass != CLASS_NONE) | 1678 if (charclass != CLASS_NONE) |
1679 { | 1679 { |
1680 switch (charclass) | 1680 switch (charclass) |
1681 { | 1681 { |
1682 case CLASS_ALNUM: | 1682 case CLASS_ALNUM: |
1738 break; | 1738 break; |
1739 } | 1739 } |
1740 EMIT(NFA_CONCAT); | 1740 EMIT(NFA_CONCAT); |
1741 continue; | 1741 continue; |
1742 } | 1742 } |
1743 /* Try equivalence class [=a=] and the like */ | 1743 // Try equivalence class [=a=] and the like |
1744 if (equiclass != 0) | 1744 if (equiclass != 0) |
1745 { | 1745 { |
1746 result = nfa_emit_equi_class(equiclass); | 1746 result = nfa_emit_equi_class(equiclass); |
1747 if (result == FAIL) | 1747 if (result == FAIL) |
1748 { | 1748 { |
1749 /* should never happen */ | 1749 // should never happen |
1750 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); | 1750 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); |
1751 } | 1751 } |
1752 continue; | 1752 continue; |
1753 } | 1753 } |
1754 /* Try collating class like [. .] */ | 1754 // Try collating class like [. .] |
1755 if (collclass != 0) | 1755 if (collclass != 0) |
1756 { | 1756 { |
1757 startc = collclass; /* allow [.a.]-x as a range */ | 1757 startc = collclass; // allow [.a.]-x as a range |
1758 /* Will emit the proper atom at the end of the | 1758 // Will emit the proper atom at the end of the |
1759 * while loop. */ | 1759 // while loop. |
1760 } | 1760 } |
1761 } | 1761 } |
1762 /* Try a range like 'a-x' or '\t-z'. Also allows '-' as a | 1762 // Try a range like 'a-x' or '\t-z'. Also allows '-' as a |
1763 * start character. */ | 1763 // start character. |
1764 if (*regparse == '-' && oldstartc != -1) | 1764 if (*regparse == '-' && oldstartc != -1) |
1765 { | 1765 { |
1766 emit_range = TRUE; | 1766 emit_range = TRUE; |
1767 startc = oldstartc; | 1767 startc = oldstartc; |
1768 MB_PTR_ADV(regparse); | 1768 MB_PTR_ADV(regparse); |
1769 continue; /* reading the end of the range */ | 1769 continue; // reading the end of the range |
1770 } | 1770 } |
1771 | 1771 |
1772 /* Now handle simple and escaped characters. | 1772 // Now handle simple and escaped characters. |
1773 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim | 1773 // Only "\]", "\^", "\]" and "\\" are special in Vi. Vim |
1774 * accepts "\t", "\e", etc., but only when the 'l' flag in | 1774 // accepts "\t", "\e", etc., but only when the 'l' flag in |
1775 * 'cpoptions' is not included. | 1775 // 'cpoptions' is not included. |
1776 * Posix doesn't recognize backslash at all. | 1776 // Posix doesn't recognize backslash at all. |
1777 */ | |
1778 if (*regparse == '\\' | 1777 if (*regparse == '\\' |
1779 && !reg_cpo_bsl | 1778 && !reg_cpo_bsl |
1780 && regparse + 1 <= endp | 1779 && regparse + 1 <= endp |
1781 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL | 1780 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL |
1782 || (!reg_cpo_lit | 1781 || (!reg_cpo_lit |
1795 || *regparse == 'x' | 1794 || *regparse == 'x' |
1796 || *regparse == 'u' | 1795 || *regparse == 'u' |
1797 || *regparse == 'U' | 1796 || *regparse == 'U' |
1798 ) | 1797 ) |
1799 { | 1798 { |
1800 /* TODO(RE) This needs more testing */ | 1799 // TODO(RE) This needs more testing |
1801 startc = coll_get_char(); | 1800 startc = coll_get_char(); |
1802 got_coll_char = TRUE; | 1801 got_coll_char = TRUE; |
1803 MB_PTR_BACK(old_regparse, regparse); | 1802 MB_PTR_BACK(old_regparse, regparse); |
1804 } | 1803 } |
1805 else | 1804 else |
1806 { | 1805 { |
1807 /* \r,\t,\e,\b */ | 1806 // \r,\t,\e,\b |
1808 startc = backslash_trans(*regparse); | 1807 startc = backslash_trans(*regparse); |
1809 } | 1808 } |
1810 } | 1809 } |
1811 | 1810 |
1812 /* Normal printable char */ | 1811 // Normal printable char |
1813 if (startc == -1) | 1812 if (startc == -1) |
1814 startc = PTR2CHAR(regparse); | 1813 startc = PTR2CHAR(regparse); |
1815 | 1814 |
1816 /* Previous char was '-', so this char is end of range. */ | 1815 // Previous char was '-', so this char is end of range. |
1817 if (emit_range) | 1816 if (emit_range) |
1818 { | 1817 { |
1819 endc = startc; | 1818 endc = startc; |
1820 startc = oldstartc; | 1819 startc = oldstartc; |
1821 if (startc > endc) | 1820 if (startc > endc) |
1822 EMSG_RET_FAIL(_(e_reverse_range)); | 1821 EMSG_RET_FAIL(_(e_reverse_range)); |
1823 | 1822 |
1824 if (endc > startc + 2) | 1823 if (endc > startc + 2) |
1825 { | 1824 { |
1826 /* Emit a range instead of the sequence of | 1825 // Emit a range instead of the sequence of |
1827 * individual characters. */ | 1826 // individual characters. |
1828 if (startc == 0) | 1827 if (startc == 0) |
1829 /* \x00 is translated to \x0a, start at \x01. */ | 1828 // \x00 is translated to \x0a, start at \x01. |
1830 EMIT(1); | 1829 EMIT(1); |
1831 else | 1830 else |
1832 --post_ptr; /* remove NFA_CONCAT */ | 1831 --post_ptr; // remove NFA_CONCAT |
1833 EMIT(endc); | 1832 EMIT(endc); |
1834 EMIT(NFA_RANGE); | 1833 EMIT(NFA_RANGE); |
1835 EMIT(NFA_CONCAT); | 1834 EMIT(NFA_CONCAT); |
1836 } | 1835 } |
1837 else if (has_mbyte && ((*mb_char2len)(startc) > 1 | 1836 else if (has_mbyte && ((*mb_char2len)(startc) > 1 |
1838 || (*mb_char2len)(endc) > 1)) | 1837 || (*mb_char2len)(endc) > 1)) |
1839 { | 1838 { |
1840 /* Emit the characters in the range. | 1839 // Emit the characters in the range. |
1841 * "startc" was already emitted, so skip it. | 1840 // "startc" was already emitted, so skip it. |
1842 * */ | 1841 // |
1843 for (c = startc + 1; c <= endc; c++) | 1842 for (c = startc + 1; c <= endc; c++) |
1844 { | 1843 { |
1845 EMIT(c); | 1844 EMIT(c); |
1846 EMIT(NFA_CONCAT); | 1845 EMIT(NFA_CONCAT); |
1847 } | 1846 } |
1849 else | 1848 else |
1850 { | 1849 { |
1851 #ifdef EBCDIC | 1850 #ifdef EBCDIC |
1852 int alpha_only = FALSE; | 1851 int alpha_only = FALSE; |
1853 | 1852 |
1854 /* for alphabetical range skip the gaps | 1853 // for alphabetical range skip the gaps |
1855 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ | 1854 // 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. |
1856 if (isalpha(startc) && isalpha(endc)) | 1855 if (isalpha(startc) && isalpha(endc)) |
1857 alpha_only = TRUE; | 1856 alpha_only = TRUE; |
1858 #endif | 1857 #endif |
1859 /* Emit the range. "startc" was already emitted, so | 1858 // Emit the range. "startc" was already emitted, so |
1860 * skip it. */ | 1859 // skip it. |
1861 for (c = startc + 1; c <= endc; c++) | 1860 for (c = startc + 1; c <= endc; c++) |
1862 #ifdef EBCDIC | 1861 #ifdef EBCDIC |
1863 if (!alpha_only || isalpha(startc)) | 1862 if (!alpha_only || isalpha(startc)) |
1864 #endif | 1863 #endif |
1865 { | 1864 { |
1870 emit_range = FALSE; | 1869 emit_range = FALSE; |
1871 startc = -1; | 1870 startc = -1; |
1872 } | 1871 } |
1873 else | 1872 else |
1874 { | 1873 { |
1875 /* This char (startc) is not part of a range. Just | 1874 // This char (startc) is not part of a range. Just |
1876 * emit it. | 1875 // emit it. |
1877 * Normally, simply emit startc. But if we get char | 1876 // Normally, simply emit startc. But if we get char |
1878 * code=0 from a collating char, then replace it with | 1877 // code=0 from a collating char, then replace it with |
1879 * 0x0a. | 1878 // 0x0a. |
1880 * This is needed to completely mimic the behaviour of | 1879 // This is needed to completely mimic the behaviour of |
1881 * the backtracking engine. */ | 1880 // the backtracking engine. |
1882 if (startc == NFA_NEWL) | 1881 if (startc == NFA_NEWL) |
1883 { | 1882 { |
1884 /* Line break can't be matched as part of the | 1883 // Line break can't be matched as part of the |
1885 * collection, add an OR below. But not for negated | 1884 // collection, add an OR below. But not for negated |
1886 * range. */ | 1885 // range. |
1887 if (!negated) | 1886 if (!negated) |
1888 extra = NFA_ADD_NL; | 1887 extra = NFA_ADD_NL; |
1889 } | 1888 } |
1890 else | 1889 else |
1891 { | 1890 { |
1896 EMIT(NFA_CONCAT); | 1895 EMIT(NFA_CONCAT); |
1897 } | 1896 } |
1898 } | 1897 } |
1899 | 1898 |
1900 MB_PTR_ADV(regparse); | 1899 MB_PTR_ADV(regparse); |
1901 } /* while (p < endp) */ | 1900 } // while (p < endp) |
1902 | 1901 |
1903 MB_PTR_BACK(old_regparse, regparse); | 1902 MB_PTR_BACK(old_regparse, regparse); |
1904 if (*regparse == '-') /* if last, '-' is just a char */ | 1903 if (*regparse == '-') // if last, '-' is just a char |
1905 { | 1904 { |
1906 EMIT('-'); | 1905 EMIT('-'); |
1907 EMIT(NFA_CONCAT); | 1906 EMIT(NFA_CONCAT); |
1908 } | 1907 } |
1909 | 1908 |
1910 /* skip the trailing ] */ | 1909 // skip the trailing ] |
1911 regparse = endp; | 1910 regparse = endp; |
1912 MB_PTR_ADV(regparse); | 1911 MB_PTR_ADV(regparse); |
1913 | 1912 |
1914 /* Mark end of the collection. */ | 1913 // Mark end of the collection. |
1915 if (negated == TRUE) | 1914 if (negated == TRUE) |
1916 EMIT(NFA_END_NEG_COLL); | 1915 EMIT(NFA_END_NEG_COLL); |
1917 else | 1916 else |
1918 EMIT(NFA_END_COLL); | 1917 EMIT(NFA_END_COLL); |
1919 | 1918 |
1920 /* \_[] also matches \n but it's not negated */ | 1919 // \_[] also matches \n but it's not negated |
1921 if (extra == NFA_ADD_NL) | 1920 if (extra == NFA_ADD_NL) |
1922 { | 1921 { |
1923 EMIT(reg_string ? NL : NFA_NEWL); | 1922 EMIT(reg_string ? NL : NFA_NEWL); |
1924 EMIT(NFA_OR); | 1923 EMIT(NFA_OR); |
1925 } | 1924 } |
1926 | 1925 |
1927 return OK; | 1926 return OK; |
1928 } /* if exists closing ] */ | 1927 } // if exists closing ] |
1929 | 1928 |
1930 if (reg_strict) | 1929 if (reg_strict) |
1931 EMSG_RET_FAIL(_(e_missingbracket)); | 1930 EMSG_RET_FAIL(_(e_missingbracket)); |
1932 /* FALLTHROUGH */ | 1931 // FALLTHROUGH |
1933 | 1932 |
1934 default: | 1933 default: |
1935 { | 1934 { |
1936 int plen; | 1935 int plen; |
1937 | 1936 |
1938 nfa_do_multibyte: | 1937 nfa_do_multibyte: |
1939 /* plen is length of current char with composing chars */ | 1938 // plen is length of current char with composing chars |
1940 if (enc_utf8 && ((*mb_char2len)(c) | 1939 if (enc_utf8 && ((*mb_char2len)(c) |
1941 != (plen = utfc_ptr2len(old_regparse)) | 1940 != (plen = utfc_ptr2len(old_regparse)) |
1942 || utf_iscomposing(c))) | 1941 || utf_iscomposing(c))) |
1943 { | 1942 { |
1944 int i = 0; | 1943 int i = 0; |
1945 | 1944 |
1946 /* A base character plus composing characters, or just one | 1945 // A base character plus composing characters, or just one |
1947 * or more composing characters. | 1946 // or more composing characters. |
1948 * This requires creating a separate atom as if enclosing | 1947 // This requires creating a separate atom as if enclosing |
1949 * the characters in (), where NFA_COMPOSING is the ( and | 1948 // the characters in (), where NFA_COMPOSING is the ( and |
1950 * NFA_END_COMPOSING is the ). Note that right now we are | 1949 // NFA_END_COMPOSING is the ). Note that right now we are |
1951 * building the postfix form, not the NFA itself; | 1950 // building the postfix form, not the NFA itself; |
1952 * a composing char could be: a, b, c, NFA_COMPOSING | 1951 // a composing char could be: a, b, c, NFA_COMPOSING |
1953 * where 'b' and 'c' are chars with codes > 256. */ | 1952 // where 'b' and 'c' are chars with codes > 256. |
1954 for (;;) | 1953 for (;;) |
1955 { | 1954 { |
1956 EMIT(c); | 1955 EMIT(c); |
1957 if (i > 0) | 1956 if (i > 0) |
1958 EMIT(NFA_CONCAT); | 1957 EMIT(NFA_CONCAT); |
1990 { | 1989 { |
1991 int i; | 1990 int i; |
1992 int op; | 1991 int op; |
1993 int ret; | 1992 int ret; |
1994 long minval, maxval; | 1993 long minval, maxval; |
1995 int greedy = TRUE; /* Braces are prefixed with '-' ? */ | 1994 int greedy = TRUE; // Braces are prefixed with '-' ? |
1996 parse_state_T old_state; | 1995 parse_state_T old_state; |
1997 parse_state_T new_state; | 1996 parse_state_T new_state; |
1998 long c2; | 1997 long c2; |
1999 int old_post_pos; | 1998 int old_post_pos; |
2000 int my_post_start; | 1999 int my_post_start; |
2001 int quest; | 2000 int quest; |
2002 | 2001 |
2003 /* Save the current parse state, so that we can use it if <atom>{m,n} is | 2002 // Save the current parse state, so that we can use it if <atom>{m,n} is |
2004 * next. */ | 2003 // next. |
2005 save_parse_state(&old_state); | 2004 save_parse_state(&old_state); |
2006 | 2005 |
2007 /* store current pos in the postfix form, for \{m,n} involving 0s */ | 2006 // store current pos in the postfix form, for \{m,n} involving 0s |
2008 my_post_start = (int)(post_ptr - post_start); | 2007 my_post_start = (int)(post_ptr - post_start); |
2009 | 2008 |
2010 ret = nfa_regatom(); | 2009 ret = nfa_regatom(); |
2011 if (ret == FAIL) | 2010 if (ret == FAIL) |
2012 return FAIL; /* cascaded error */ | 2011 return FAIL; // cascaded error |
2013 | 2012 |
2014 op = peekchr(); | 2013 op = peekchr(); |
2015 if (re_multi_type(op) == NOT_MULTI) | 2014 if (re_multi_type(op) == NOT_MULTI) |
2016 return OK; | 2015 return OK; |
2017 | 2016 |
2038 curchr = -1; | 2037 curchr = -1; |
2039 if (nfa_regatom() == FAIL) | 2038 if (nfa_regatom() == FAIL) |
2040 return FAIL; | 2039 return FAIL; |
2041 EMIT(NFA_STAR); | 2040 EMIT(NFA_STAR); |
2042 EMIT(NFA_CONCAT); | 2041 EMIT(NFA_CONCAT); |
2043 skipchr(); /* skip the \+ */ | 2042 skipchr(); // skip the \+ |
2044 break; | 2043 break; |
2045 | 2044 |
2046 case Magic('@'): | 2045 case Magic('@'): |
2047 c2 = getdecchrs(); | 2046 c2 = getdecchrs(); |
2048 op = no_Magic(getchr()); | 2047 op = no_Magic(getchr()); |
2049 i = 0; | 2048 i = 0; |
2050 switch(op) | 2049 switch(op) |
2051 { | 2050 { |
2052 case '=': | 2051 case '=': |
2053 /* \@= */ | 2052 // \@= |
2054 i = NFA_PREV_ATOM_NO_WIDTH; | 2053 i = NFA_PREV_ATOM_NO_WIDTH; |
2055 break; | 2054 break; |
2056 case '!': | 2055 case '!': |
2057 /* \@! */ | 2056 // \@! |
2058 i = NFA_PREV_ATOM_NO_WIDTH_NEG; | 2057 i = NFA_PREV_ATOM_NO_WIDTH_NEG; |
2059 break; | 2058 break; |
2060 case '<': | 2059 case '<': |
2061 op = no_Magic(getchr()); | 2060 op = no_Magic(getchr()); |
2062 if (op == '=') | 2061 if (op == '=') |
2063 /* \@<= */ | 2062 // \@<= |
2064 i = NFA_PREV_ATOM_JUST_BEFORE; | 2063 i = NFA_PREV_ATOM_JUST_BEFORE; |
2065 else if (op == '!') | 2064 else if (op == '!') |
2066 /* \@<! */ | 2065 // \@<! |
2067 i = NFA_PREV_ATOM_JUST_BEFORE_NEG; | 2066 i = NFA_PREV_ATOM_JUST_BEFORE_NEG; |
2068 break; | 2067 break; |
2069 case '>': | 2068 case '>': |
2070 /* \@> */ | 2069 // \@> |
2071 i = NFA_PREV_ATOM_LIKE_PATTERN; | 2070 i = NFA_PREV_ATOM_LIKE_PATTERN; |
2072 break; | 2071 break; |
2073 } | 2072 } |
2074 if (i == 0) | 2073 if (i == 0) |
2075 { | 2074 { |
2086 case Magic('='): | 2085 case Magic('='): |
2087 EMIT(NFA_QUEST); | 2086 EMIT(NFA_QUEST); |
2088 break; | 2087 break; |
2089 | 2088 |
2090 case Magic('{'): | 2089 case Magic('{'): |
2091 /* a{2,5} will expand to 'aaa?a?a?' | 2090 // a{2,5} will expand to 'aaa?a?a?' |
2092 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy | 2091 // a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy |
2093 * version of '?' | 2092 // version of '?' |
2094 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the | 2093 // \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the |
2095 * parenthesis have the same id | 2094 // parenthesis have the same id |
2096 */ | |
2097 | 2095 |
2098 greedy = TRUE; | 2096 greedy = TRUE; |
2099 c2 = peekchr(); | 2097 c2 = peekchr(); |
2100 if (c2 == '-' || c2 == Magic('-')) | 2098 if (c2 == '-' || c2 == Magic('-')) |
2101 { | 2099 { |
2103 greedy = FALSE; | 2101 greedy = FALSE; |
2104 } | 2102 } |
2105 if (!read_limits(&minval, &maxval)) | 2103 if (!read_limits(&minval, &maxval)) |
2106 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); | 2104 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); |
2107 | 2105 |
2108 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to | 2106 // <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to |
2109 * <atom>* */ | 2107 // <atom>* |
2110 if (minval == 0 && maxval == MAX_LIMIT) | 2108 if (minval == 0 && maxval == MAX_LIMIT) |
2111 { | 2109 { |
2112 if (greedy) /* { { (match the braces) */ | 2110 if (greedy) // { { (match the braces) |
2113 /* \{}, \{0,} */ | 2111 // \{}, \{0,} |
2114 EMIT(NFA_STAR); | 2112 EMIT(NFA_STAR); |
2115 else /* { { (match the braces) */ | 2113 else // { { (match the braces) |
2116 /* \{-}, \{-0,} */ | 2114 // \{-}, \{-0,} |
2117 EMIT(NFA_STAR_NONGREEDY); | 2115 EMIT(NFA_STAR_NONGREEDY); |
2118 break; | 2116 break; |
2119 } | 2117 } |
2120 | 2118 |
2121 /* Special case: x{0} or x{-0} */ | 2119 // Special case: x{0} or x{-0} |
2122 if (maxval == 0) | 2120 if (maxval == 0) |
2123 { | 2121 { |
2124 /* Ignore result of previous call to nfa_regatom() */ | 2122 // Ignore result of previous call to nfa_regatom() |
2125 post_ptr = post_start + my_post_start; | 2123 post_ptr = post_start + my_post_start; |
2126 /* NFA_EMPTY is 0-length and works everywhere */ | 2124 // NFA_EMPTY is 0-length and works everywhere |
2127 EMIT(NFA_EMPTY); | 2125 EMIT(NFA_EMPTY); |
2128 return OK; | 2126 return OK; |
2129 } | 2127 } |
2130 | 2128 |
2131 /* The engine is very inefficient (uses too many states) when the | 2129 // The engine is very inefficient (uses too many states) when the |
2132 * maximum is much larger than the minimum and when the maximum is | 2130 // maximum is much larger than the minimum and when the maximum is |
2133 * large. Bail out if we can use the other engine. */ | 2131 // large. Bail out if we can use the other engine. |
2134 if ((nfa_re_flags & RE_AUTO) | 2132 if ((nfa_re_flags & RE_AUTO) |
2135 && (maxval > 500 || maxval > minval + 200)) | 2133 && (maxval > 500 || maxval > minval + 200)) |
2136 return FAIL; | 2134 return FAIL; |
2137 | 2135 |
2138 /* Ignore previous call to nfa_regatom() */ | 2136 // Ignore previous call to nfa_regatom() |
2139 post_ptr = post_start + my_post_start; | 2137 post_ptr = post_start + my_post_start; |
2140 /* Save parse state after the repeated atom and the \{} */ | 2138 // Save parse state after the repeated atom and the \{} |
2141 save_parse_state(&new_state); | 2139 save_parse_state(&new_state); |
2142 | 2140 |
2143 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); | 2141 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); |
2144 for (i = 0; i < maxval; i++) | 2142 for (i = 0; i < maxval; i++) |
2145 { | 2143 { |
2146 /* Goto beginning of the repeated atom */ | 2144 // Goto beginning of the repeated atom |
2147 restore_parse_state(&old_state); | 2145 restore_parse_state(&old_state); |
2148 old_post_pos = (int)(post_ptr - post_start); | 2146 old_post_pos = (int)(post_ptr - post_start); |
2149 if (nfa_regatom() == FAIL) | 2147 if (nfa_regatom() == FAIL) |
2150 return FAIL; | 2148 return FAIL; |
2151 /* after "minval" times, atoms are optional */ | 2149 // after "minval" times, atoms are optional |
2152 if (i + 1 > minval) | 2150 if (i + 1 > minval) |
2153 { | 2151 { |
2154 if (maxval == MAX_LIMIT) | 2152 if (maxval == MAX_LIMIT) |
2155 { | 2153 { |
2156 if (greedy) | 2154 if (greedy) |
2165 EMIT(NFA_CONCAT); | 2163 EMIT(NFA_CONCAT); |
2166 if (i + 1 > minval && maxval == MAX_LIMIT) | 2164 if (i + 1 > minval && maxval == MAX_LIMIT) |
2167 break; | 2165 break; |
2168 } | 2166 } |
2169 | 2167 |
2170 /* Go to just after the repeated atom and the \{} */ | 2168 // Go to just after the repeated atom and the \{} |
2171 restore_parse_state(&new_state); | 2169 restore_parse_state(&new_state); |
2172 curchr = -1; | 2170 curchr = -1; |
2173 | 2171 |
2174 break; | 2172 break; |
2175 | 2173 |
2176 | 2174 |
2177 default: | 2175 default: |
2178 break; | 2176 break; |
2179 } /* end switch */ | 2177 } // end switch |
2180 | 2178 |
2181 if (re_multi_type(peekchr()) != NOT_MULTI) | 2179 if (re_multi_type(peekchr()) != NOT_MULTI) |
2182 /* Can't have a multi follow a multi. */ | 2180 // Can't have a multi follow a multi. |
2183 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi")); | 2181 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi")); |
2184 | 2182 |
2185 return OK; | 2183 return OK; |
2186 } | 2184 } |
2187 | 2185 |
2276 { | 2274 { |
2277 int old_post_pos; | 2275 int old_post_pos; |
2278 | 2276 |
2279 old_post_pos = (int)(post_ptr - post_start); | 2277 old_post_pos = (int)(post_ptr - post_start); |
2280 | 2278 |
2281 /* First branch, possibly the only one */ | 2279 // First branch, possibly the only one |
2282 if (nfa_regconcat() == FAIL) | 2280 if (nfa_regconcat() == FAIL) |
2283 return FAIL; | 2281 return FAIL; |
2284 | 2282 |
2285 /* Try next concats */ | 2283 // Try next concats |
2286 while (peekchr() == Magic('&')) | 2284 while (peekchr() == Magic('&')) |
2287 { | 2285 { |
2288 skipchr(); | 2286 skipchr(); |
2289 /* if concat is empty do emit a node */ | 2287 // if concat is empty do emit a node |
2290 if (old_post_pos == (int)(post_ptr - post_start)) | 2288 if (old_post_pos == (int)(post_ptr - post_start)) |
2291 EMIT(NFA_EMPTY); | 2289 EMIT(NFA_EMPTY); |
2292 EMIT(NFA_NOPEN); | 2290 EMIT(NFA_NOPEN); |
2293 EMIT(NFA_PREV_ATOM_NO_WIDTH); | 2291 EMIT(NFA_PREV_ATOM_NO_WIDTH); |
2294 old_post_pos = (int)(post_ptr - post_start); | 2292 old_post_pos = (int)(post_ptr - post_start); |
2295 if (nfa_regconcat() == FAIL) | 2293 if (nfa_regconcat() == FAIL) |
2296 return FAIL; | 2294 return FAIL; |
2297 /* if concat is empty do emit a node */ | 2295 // if concat is empty do emit a node |
2298 if (old_post_pos == (int)(post_ptr - post_start)) | 2296 if (old_post_pos == (int)(post_ptr - post_start)) |
2299 EMIT(NFA_EMPTY); | 2297 EMIT(NFA_EMPTY); |
2300 EMIT(NFA_CONCAT); | 2298 EMIT(NFA_CONCAT); |
2301 } | 2299 } |
2302 | 2300 |
2303 /* if a branch is empty, emit one node for it */ | 2301 // if a branch is empty, emit one node for it |
2304 if (old_post_pos == (int)(post_ptr - post_start)) | 2302 if (old_post_pos == (int)(post_ptr - post_start)) |
2305 EMIT(NFA_EMPTY); | 2303 EMIT(NFA_EMPTY); |
2306 | 2304 |
2307 return OK; | 2305 return OK; |
2308 } | 2306 } |
2318 * or branch \| branch \| branch | 2316 * or branch \| branch \| branch |
2319 * etc. | 2317 * etc. |
2320 */ | 2318 */ |
2321 static int | 2319 static int |
2322 nfa_reg( | 2320 nfa_reg( |
2323 int paren) /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ | 2321 int paren) // REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN |
2324 { | 2322 { |
2325 int parno = 0; | 2323 int parno = 0; |
2326 | 2324 |
2327 if (paren == REG_PAREN) | 2325 if (paren == REG_PAREN) |
2328 { | 2326 { |
2329 if (regnpar >= NSUBEXP) /* Too many `(' */ | 2327 if (regnpar >= NSUBEXP) // Too many `(' |
2330 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); | 2328 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); |
2331 parno = regnpar++; | 2329 parno = regnpar++; |
2332 } | 2330 } |
2333 #ifdef FEAT_SYN_HL | 2331 #ifdef FEAT_SYN_HL |
2334 else if (paren == REG_ZPAREN) | 2332 else if (paren == REG_ZPAREN) |
2335 { | 2333 { |
2336 /* Make a ZOPEN node. */ | 2334 // Make a ZOPEN node. |
2337 if (regnzpar >= NSUBEXP) | 2335 if (regnzpar >= NSUBEXP) |
2338 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); | 2336 EMSG_RET_FAIL(_("E879: (NFA regexp) Too many \\z(")); |
2339 parno = regnzpar++; | 2337 parno = regnzpar++; |
2340 } | 2338 } |
2341 #endif | 2339 #endif |
2342 | 2340 |
2343 if (nfa_regbranch() == FAIL) | 2341 if (nfa_regbranch() == FAIL) |
2344 return FAIL; /* cascaded error */ | 2342 return FAIL; // cascaded error |
2345 | 2343 |
2346 while (peekchr() == Magic('|')) | 2344 while (peekchr() == Magic('|')) |
2347 { | 2345 { |
2348 skipchr(); | 2346 skipchr(); |
2349 if (nfa_regbranch() == FAIL) | 2347 if (nfa_regbranch() == FAIL) |
2350 return FAIL; /* cascaded error */ | 2348 return FAIL; // cascaded error |
2351 EMIT(NFA_OR); | 2349 EMIT(NFA_OR); |
2352 } | 2350 } |
2353 | 2351 |
2354 /* Check for proper termination. */ | 2352 // Check for proper termination. |
2355 if (paren != REG_NOPAREN && getchr() != Magic(')')) | 2353 if (paren != REG_NOPAREN && getchr() != Magic(')')) |
2356 { | 2354 { |
2357 if (paren == REG_NPAREN) | 2355 if (paren == REG_NPAREN) |
2358 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); | 2356 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); |
2359 else | 2357 else |
2370 * Here we set the flag allowing back references to this set of | 2368 * Here we set the flag allowing back references to this set of |
2371 * parentheses. | 2369 * parentheses. |
2372 */ | 2370 */ |
2373 if (paren == REG_PAREN) | 2371 if (paren == REG_PAREN) |
2374 { | 2372 { |
2375 had_endbrace[parno] = TRUE; /* have seen the close paren */ | 2373 had_endbrace[parno] = TRUE; // have seen the close paren |
2376 EMIT(NFA_MOPEN + parno); | 2374 EMIT(NFA_MOPEN + parno); |
2377 } | 2375 } |
2378 #ifdef FEAT_SYN_HL | 2376 #ifdef FEAT_SYN_HL |
2379 else if (paren == REG_ZPAREN) | 2377 else if (paren == REG_ZPAREN) |
2380 EMIT(NFA_ZOPEN + parno); | 2378 EMIT(NFA_ZOPEN + parno); |
2676 if (state == NULL) | 2674 if (state == NULL) |
2677 return; | 2675 return; |
2678 | 2676 |
2679 fprintf(debugf, "(%2d)", abs(state->id)); | 2677 fprintf(debugf, "(%2d)", abs(state->id)); |
2680 | 2678 |
2681 /* Output indent */ | 2679 // Output indent |
2682 p = (char_u *)indent->ga_data; | 2680 p = (char_u *)indent->ga_data; |
2683 if (indent->ga_len >= 3) | 2681 if (indent->ga_len >= 3) |
2684 { | 2682 { |
2685 int last = indent->ga_len - 3; | 2683 int last = indent->ga_len - 3; |
2686 char_u save[2]; | 2684 char_u save[2]; |
2702 if (state->id < 0) | 2700 if (state->id < 0) |
2703 return; | 2701 return; |
2704 | 2702 |
2705 state->id = abs(state->id) * -1; | 2703 state->id = abs(state->id) * -1; |
2706 | 2704 |
2707 /* grow indent for state->out */ | 2705 // grow indent for state->out |
2708 indent->ga_len -= 1; | 2706 indent->ga_len -= 1; |
2709 if (state->out1) | 2707 if (state->out1) |
2710 ga_concat(indent, (char_u *)"| "); | 2708 ga_concat(indent, (char_u *)"| "); |
2711 else | 2709 else |
2712 ga_concat(indent, (char_u *)" "); | 2710 ga_concat(indent, (char_u *)" "); |
2713 ga_append(indent, '\0'); | 2711 ga_append(indent, '\0'); |
2714 | 2712 |
2715 nfa_print_state2(debugf, state->out, indent); | 2713 nfa_print_state2(debugf, state->out, indent); |
2716 | 2714 |
2717 /* replace last part of indent for state->out1 */ | 2715 // replace last part of indent for state->out1 |
2718 indent->ga_len -= 3; | 2716 indent->ga_len -= 3; |
2719 ga_concat(indent, (char_u *)" "); | 2717 ga_concat(indent, (char_u *)" "); |
2720 ga_append(indent, '\0'); | 2718 ga_append(indent, '\0'); |
2721 | 2719 |
2722 nfa_print_state2(debugf, state->out1, indent); | 2720 nfa_print_state2(debugf, state->out1, indent); |
2723 | 2721 |
2724 /* shrink indent */ | 2722 // shrink indent |
2725 indent->ga_len -= 3; | 2723 indent->ga_len -= 3; |
2726 ga_append(indent, '\0'); | 2724 ga_append(indent, '\0'); |
2727 } | 2725 } |
2728 | 2726 |
2729 /* | 2727 /* |
2747 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); | 2745 fprintf(debugf, "match_text: \"%s\"\n", prog->match_text); |
2748 | 2746 |
2749 fclose(debugf); | 2747 fclose(debugf); |
2750 } | 2748 } |
2751 } | 2749 } |
2752 #endif /* ENABLE_LOG */ | 2750 #endif // ENABLE_LOG |
2753 #endif /* DEBUG */ | 2751 #endif // DEBUG |
2754 | 2752 |
2755 /* | 2753 /* |
2756 * Parse r.e. @expr and convert it into postfix form. | 2754 * Parse r.e. @expr and convert it into postfix form. |
2757 * Return the postfix string on success, NULL otherwise. | 2755 * Return the postfix string on success, NULL otherwise. |
2758 */ | 2756 */ |
2763 return NULL; | 2761 return NULL; |
2764 EMIT(NFA_MOPEN); | 2762 EMIT(NFA_MOPEN); |
2765 return post_start; | 2763 return post_start; |
2766 } | 2764 } |
2767 | 2765 |
2768 /* NB. Some of the code below is inspired by Russ's. */ | 2766 // NB. Some of the code below is inspired by Russ's. |
2769 | 2767 |
2770 /* | 2768 /* |
2771 * Represents an NFA state plus zero or one or two arrows exiting. | 2769 * Represents an NFA state plus zero or one or two arrows exiting. |
2772 * if c == MATCH, no arrows out; matching state. | 2770 * if c == MATCH, no arrows out; matching state. |
2773 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). | 2771 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). |
2774 * If c < 256, labeled arrow with character c to out. | 2772 * If c < 256, labeled arrow with character c to out. |
2775 */ | 2773 */ |
2776 | 2774 |
2777 static nfa_state_T *state_ptr; /* points to nfa_prog->state */ | 2775 static nfa_state_T *state_ptr; // points to nfa_prog->state |
2778 | 2776 |
2779 /* | 2777 /* |
2780 * Allocate and initialize nfa_state_T. | 2778 * Allocate and initialize nfa_state_T. |
2781 */ | 2779 */ |
2782 static nfa_state_T * | 2780 static nfa_state_T * |
2806 * Frag_T.start points at the start state. | 2804 * Frag_T.start points at the start state. |
2807 * Frag_T.out is a list of places that need to be set to the | 2805 * Frag_T.out is a list of places that need to be set to the |
2808 * next state for this fragment. | 2806 * next state for this fragment. |
2809 */ | 2807 */ |
2810 | 2808 |
2811 /* Since the out pointers in the list are always | 2809 // Since the out pointers in the list are always |
2812 * uninitialized, we use the pointers themselves | 2810 // uninitialized, we use the pointers themselves |
2813 * as storage for the Ptrlists. */ | 2811 // as storage for the Ptrlists. |
2814 typedef union Ptrlist Ptrlist; | 2812 typedef union Ptrlist Ptrlist; |
2815 union Ptrlist | 2813 union Ptrlist |
2816 { | 2814 { |
2817 Ptrlist *next; | 2815 Ptrlist *next; |
2818 nfa_state_T *s; | 2816 nfa_state_T *s; |
2968 { | 2966 { |
2969 int l, r; | 2967 int l, r; |
2970 nfa_state_T *state = startstate; | 2968 nfa_state_T *state = startstate; |
2971 int len = 0; | 2969 int len = 0; |
2972 | 2970 |
2973 /* detect looping in a NFA_SPLIT */ | 2971 // detect looping in a NFA_SPLIT |
2974 if (depth > 4) | 2972 if (depth > 4) |
2975 return -1; | 2973 return -1; |
2976 | 2974 |
2977 while (state != NULL) | 2975 while (state != NULL) |
2978 { | 2976 { |
2979 switch (state->c) | 2977 switch (state->c) |
2980 { | 2978 { |
2981 case NFA_END_INVISIBLE: | 2979 case NFA_END_INVISIBLE: |
2982 case NFA_END_INVISIBLE_NEG: | 2980 case NFA_END_INVISIBLE_NEG: |
2983 /* the end, return what we have */ | 2981 // the end, return what we have |
2984 return len; | 2982 return len; |
2985 | 2983 |
2986 case NFA_SPLIT: | 2984 case NFA_SPLIT: |
2987 /* two alternatives, use the maximum */ | 2985 // two alternatives, use the maximum |
2988 l = nfa_max_width(state->out, depth + 1); | 2986 l = nfa_max_width(state->out, depth + 1); |
2989 r = nfa_max_width(state->out1, depth + 1); | 2987 r = nfa_max_width(state->out1, depth + 1); |
2990 if (l < 0 || r < 0) | 2988 if (l < 0 || r < 0) |
2991 return -1; | 2989 return -1; |
2992 return len + (l > r ? l : r); | 2990 return len + (l > r ? l : r); |
2993 | 2991 |
2994 case NFA_ANY: | 2992 case NFA_ANY: |
2995 case NFA_START_COLL: | 2993 case NFA_START_COLL: |
2996 case NFA_START_NEG_COLL: | 2994 case NFA_START_NEG_COLL: |
2997 /* matches some character, including composing chars */ | 2995 // matches some character, including composing chars |
2998 if (enc_utf8) | 2996 if (enc_utf8) |
2999 len += MB_MAXBYTES; | 2997 len += MB_MAXBYTES; |
3000 else if (has_mbyte) | 2998 else if (has_mbyte) |
3001 len += 2; | 2999 len += 2; |
3002 else | 3000 else |
3003 ++len; | 3001 ++len; |
3004 if (state->c != NFA_ANY) | 3002 if (state->c != NFA_ANY) |
3005 { | 3003 { |
3006 /* skip over the characters */ | 3004 // skip over the characters |
3007 state = state->out1->out; | 3005 state = state->out1->out; |
3008 continue; | 3006 continue; |
3009 } | 3007 } |
3010 break; | 3008 break; |
3011 | 3009 |
3012 case NFA_DIGIT: | 3010 case NFA_DIGIT: |
3013 case NFA_WHITE: | 3011 case NFA_WHITE: |
3014 case NFA_HEX: | 3012 case NFA_HEX: |
3015 case NFA_OCTAL: | 3013 case NFA_OCTAL: |
3016 /* ascii */ | 3014 // ascii |
3017 ++len; | 3015 ++len; |
3018 break; | 3016 break; |
3019 | 3017 |
3020 case NFA_IDENT: | 3018 case NFA_IDENT: |
3021 case NFA_SIDENT: | 3019 case NFA_SIDENT: |
3042 case NFA_LOWER_IC: | 3040 case NFA_LOWER_IC: |
3043 case NFA_NLOWER_IC: | 3041 case NFA_NLOWER_IC: |
3044 case NFA_UPPER_IC: | 3042 case NFA_UPPER_IC: |
3045 case NFA_NUPPER_IC: | 3043 case NFA_NUPPER_IC: |
3046 case NFA_ANY_COMPOSING: | 3044 case NFA_ANY_COMPOSING: |
3047 /* possibly non-ascii */ | 3045 // possibly non-ascii |
3048 if (has_mbyte) | 3046 if (has_mbyte) |
3049 len += 3; | 3047 len += 3; |
3050 else | 3048 else |
3051 ++len; | 3049 ++len; |
3052 break; | 3050 break; |
3053 | 3051 |
3054 case NFA_START_INVISIBLE: | 3052 case NFA_START_INVISIBLE: |
3055 case NFA_START_INVISIBLE_NEG: | 3053 case NFA_START_INVISIBLE_NEG: |
3056 case NFA_START_INVISIBLE_BEFORE: | 3054 case NFA_START_INVISIBLE_BEFORE: |
3057 case NFA_START_INVISIBLE_BEFORE_NEG: | 3055 case NFA_START_INVISIBLE_BEFORE_NEG: |
3058 /* zero-width, out1 points to the END state */ | 3056 // zero-width, out1 points to the END state |
3059 state = state->out1->out; | 3057 state = state->out1->out; |
3060 continue; | 3058 continue; |
3061 | 3059 |
3062 case NFA_BACKREF1: | 3060 case NFA_BACKREF1: |
3063 case NFA_BACKREF2: | 3061 case NFA_BACKREF2: |
3079 case NFA_ZREF8: | 3077 case NFA_ZREF8: |
3080 case NFA_ZREF9: | 3078 case NFA_ZREF9: |
3081 #endif | 3079 #endif |
3082 case NFA_NEWL: | 3080 case NFA_NEWL: |
3083 case NFA_SKIP: | 3081 case NFA_SKIP: |
3084 /* unknown width */ | 3082 // unknown width |
3085 return -1; | 3083 return -1; |
3086 | 3084 |
3087 case NFA_BOL: | 3085 case NFA_BOL: |
3088 case NFA_EOL: | 3086 case NFA_EOL: |
3089 case NFA_BOF: | 3087 case NFA_BOF: |
3156 case NFA_EMPTY: | 3154 case NFA_EMPTY: |
3157 case NFA_START_PATTERN: | 3155 case NFA_START_PATTERN: |
3158 case NFA_END_PATTERN: | 3156 case NFA_END_PATTERN: |
3159 case NFA_COMPOSING: | 3157 case NFA_COMPOSING: |
3160 case NFA_END_COMPOSING: | 3158 case NFA_END_COMPOSING: |
3161 /* zero-width */ | 3159 // zero-width |
3162 break; | 3160 break; |
3163 | 3161 |
3164 default: | 3162 default: |
3165 if (state->c < 0) | 3163 if (state->c < 0) |
3166 /* don't know what this is */ | 3164 // don't know what this is |
3167 return -1; | 3165 return -1; |
3168 /* normal character */ | 3166 // normal character |
3169 len += MB_CHAR2LEN(state->c); | 3167 len += MB_CHAR2LEN(state->c); |
3170 break; | 3168 break; |
3171 } | 3169 } |
3172 | 3170 |
3173 /* normal way to continue */ | 3171 // normal way to continue |
3174 state = state->out; | 3172 state = state->out; |
3175 } | 3173 } |
3176 | 3174 |
3177 /* unrecognized, "cannot happen" */ | 3175 // unrecognized, "cannot happen" |
3178 return -1; | 3176 return -1; |
3179 } | 3177 } |
3180 | 3178 |
3181 /* | 3179 /* |
3182 * Convert a postfix form into its equivalent NFA. | 3180 * Convert a postfix form into its equivalent NFA. |
3224 for (p = postfix; p < end; ++p) | 3222 for (p = postfix; p < end; ++p) |
3225 { | 3223 { |
3226 switch (*p) | 3224 switch (*p) |
3227 { | 3225 { |
3228 case NFA_CONCAT: | 3226 case NFA_CONCAT: |
3229 /* Concatenation. | 3227 // Concatenation. |
3230 * Pay attention: this operator does not exist in the r.e. itself | 3228 // Pay attention: this operator does not exist in the r.e. itself |
3231 * (it is implicit, really). It is added when r.e. is translated | 3229 // (it is implicit, really). It is added when r.e. is translated |
3232 * to postfix form in re2post(). */ | 3230 // to postfix form in re2post(). |
3233 if (nfa_calc_size == TRUE) | 3231 if (nfa_calc_size == TRUE) |
3234 { | 3232 { |
3235 /* nstate += 0; */ | 3233 // nstate += 0; |
3236 break; | 3234 break; |
3237 } | 3235 } |
3238 e2 = POP(); | 3236 e2 = POP(); |
3239 e1 = POP(); | 3237 e1 = POP(); |
3240 patch(e1.out, e2.start); | 3238 patch(e1.out, e2.start); |
3241 PUSH(frag(e1.start, e2.out)); | 3239 PUSH(frag(e1.start, e2.out)); |
3242 break; | 3240 break; |
3243 | 3241 |
3244 case NFA_OR: | 3242 case NFA_OR: |
3245 /* Alternation */ | 3243 // Alternation |
3246 if (nfa_calc_size == TRUE) | 3244 if (nfa_calc_size == TRUE) |
3247 { | 3245 { |
3248 nstate++; | 3246 nstate++; |
3249 break; | 3247 break; |
3250 } | 3248 } |
3255 goto theend; | 3253 goto theend; |
3256 PUSH(frag(s, append(e1.out, e2.out))); | 3254 PUSH(frag(s, append(e1.out, e2.out))); |
3257 break; | 3255 break; |
3258 | 3256 |
3259 case NFA_STAR: | 3257 case NFA_STAR: |
3260 /* Zero or more, prefer more */ | 3258 // Zero or more, prefer more |
3261 if (nfa_calc_size == TRUE) | 3259 if (nfa_calc_size == TRUE) |
3262 { | 3260 { |
3263 nstate++; | 3261 nstate++; |
3264 break; | 3262 break; |
3265 } | 3263 } |
3270 patch(e.out, s); | 3268 patch(e.out, s); |
3271 PUSH(frag(s, list1(&s->out1))); | 3269 PUSH(frag(s, list1(&s->out1))); |
3272 break; | 3270 break; |
3273 | 3271 |
3274 case NFA_STAR_NONGREEDY: | 3272 case NFA_STAR_NONGREEDY: |
3275 /* Zero or more, prefer zero */ | 3273 // Zero or more, prefer zero |
3276 if (nfa_calc_size == TRUE) | 3274 if (nfa_calc_size == TRUE) |
3277 { | 3275 { |
3278 nstate++; | 3276 nstate++; |
3279 break; | 3277 break; |
3280 } | 3278 } |
3285 patch(e.out, s); | 3283 patch(e.out, s); |
3286 PUSH(frag(s, list1(&s->out))); | 3284 PUSH(frag(s, list1(&s->out))); |
3287 break; | 3285 break; |
3288 | 3286 |
3289 case NFA_QUEST: | 3287 case NFA_QUEST: |
3290 /* one or zero atoms=> greedy match */ | 3288 // one or zero atoms=> greedy match |
3291 if (nfa_calc_size == TRUE) | 3289 if (nfa_calc_size == TRUE) |
3292 { | 3290 { |
3293 nstate++; | 3291 nstate++; |
3294 break; | 3292 break; |
3295 } | 3293 } |
3299 goto theend; | 3297 goto theend; |
3300 PUSH(frag(s, append(e.out, list1(&s->out1)))); | 3298 PUSH(frag(s, append(e.out, list1(&s->out1)))); |
3301 break; | 3299 break; |
3302 | 3300 |
3303 case NFA_QUEST_NONGREEDY: | 3301 case NFA_QUEST_NONGREEDY: |
3304 /* zero or one atoms => non-greedy match */ | 3302 // zero or one atoms => non-greedy match |
3305 if (nfa_calc_size == TRUE) | 3303 if (nfa_calc_size == TRUE) |
3306 { | 3304 { |
3307 nstate++; | 3305 nstate++; |
3308 break; | 3306 break; |
3309 } | 3307 } |
3314 PUSH(frag(s, append(e.out, list1(&s->out)))); | 3312 PUSH(frag(s, append(e.out, list1(&s->out)))); |
3315 break; | 3313 break; |
3316 | 3314 |
3317 case NFA_END_COLL: | 3315 case NFA_END_COLL: |
3318 case NFA_END_NEG_COLL: | 3316 case NFA_END_NEG_COLL: |
3319 /* On the stack is the sequence starting with NFA_START_COLL or | 3317 // On the stack is the sequence starting with NFA_START_COLL or |
3320 * NFA_START_NEG_COLL and all possible characters. Patch it to | 3318 // NFA_START_NEG_COLL and all possible characters. Patch it to |
3321 * add the output to the start. */ | 3319 // add the output to the start. |
3322 if (nfa_calc_size == TRUE) | 3320 if (nfa_calc_size == TRUE) |
3323 { | 3321 { |
3324 nstate++; | 3322 nstate++; |
3325 break; | 3323 break; |
3326 } | 3324 } |
3332 e.start->out1 = s; | 3330 e.start->out1 = s; |
3333 PUSH(frag(e.start, list1(&s->out))); | 3331 PUSH(frag(e.start, list1(&s->out))); |
3334 break; | 3332 break; |
3335 | 3333 |
3336 case NFA_RANGE: | 3334 case NFA_RANGE: |
3337 /* Before this are two characters, the low and high end of a | 3335 // Before this are two characters, the low and high end of a |
3338 * range. Turn them into two states with MIN and MAX. */ | 3336 // range. Turn them into two states with MIN and MAX. |
3339 if (nfa_calc_size == TRUE) | 3337 if (nfa_calc_size == TRUE) |
3340 { | 3338 { |
3341 /* nstate += 0; */ | 3339 // nstate += 0; |
3342 break; | 3340 break; |
3343 } | 3341 } |
3344 e2 = POP(); | 3342 e2 = POP(); |
3345 e1 = POP(); | 3343 e1 = POP(); |
3346 e2.start->val = e2.start->c; | 3344 e2.start->val = e2.start->c; |
3350 patch(e1.out, e2.start); | 3348 patch(e1.out, e2.start); |
3351 PUSH(frag(e1.start, e2.out)); | 3349 PUSH(frag(e1.start, e2.out)); |
3352 break; | 3350 break; |
3353 | 3351 |
3354 case NFA_EMPTY: | 3352 case NFA_EMPTY: |
3355 /* 0-length, used in a repetition with max/min count of 0 */ | 3353 // 0-length, used in a repetition with max/min count of 0 |
3356 if (nfa_calc_size == TRUE) | 3354 if (nfa_calc_size == TRUE) |
3357 { | 3355 { |
3358 nstate++; | 3356 nstate++; |
3359 break; | 3357 break; |
3360 } | 3358 } |
3366 | 3364 |
3367 case NFA_OPT_CHARS: | 3365 case NFA_OPT_CHARS: |
3368 { | 3366 { |
3369 int n; | 3367 int n; |
3370 | 3368 |
3371 /* \%[abc] implemented as: | 3369 // \%[abc] implemented as: |
3372 * NFA_SPLIT | 3370 // NFA_SPLIT |
3373 * +-CHAR(a) | 3371 // +-CHAR(a) |
3374 * | +-NFA_SPLIT | 3372 // | +-NFA_SPLIT |
3375 * | +-CHAR(b) | 3373 // | +-CHAR(b) |
3376 * | | +-NFA_SPLIT | 3374 // | | +-NFA_SPLIT |
3377 * | | +-CHAR(c) | 3375 // | | +-CHAR(c) |
3378 * | | | +-next | 3376 // | | | +-next |
3379 * | | +- next | 3377 // | | +- next |
3380 * | +- next | 3378 // | +- next |
3381 * +- next | 3379 // +- next |
3382 */ | 3380 n = *++p; // get number of characters |
3383 n = *++p; /* get number of characters */ | |
3384 if (nfa_calc_size == TRUE) | 3381 if (nfa_calc_size == TRUE) |
3385 { | 3382 { |
3386 nstate += n; | 3383 nstate += n; |
3387 break; | 3384 break; |
3388 } | 3385 } |
3389 s = NULL; /* avoid compiler warning */ | 3386 s = NULL; // avoid compiler warning |
3390 e1.out = NULL; /* stores list with out1's */ | 3387 e1.out = NULL; // stores list with out1's |
3391 s1 = NULL; /* previous NFA_SPLIT to connect to */ | 3388 s1 = NULL; // previous NFA_SPLIT to connect to |
3392 while (n-- > 0) | 3389 while (n-- > 0) |
3393 { | 3390 { |
3394 e = POP(); /* get character */ | 3391 e = POP(); // get character |
3395 s = alloc_state(NFA_SPLIT, e.start, NULL); | 3392 s = alloc_state(NFA_SPLIT, e.start, NULL); |
3396 if (s == NULL) | 3393 if (s == NULL) |
3397 goto theend; | 3394 goto theend; |
3398 if (e1.out == NULL) | 3395 if (e1.out == NULL) |
3399 e1 = e; | 3396 e1 = e; |
3436 break; | 3433 break; |
3437 case NFA_PREV_ATOM_JUST_BEFORE_NEG: | 3434 case NFA_PREV_ATOM_JUST_BEFORE_NEG: |
3438 start_state = NFA_START_INVISIBLE_BEFORE_NEG; | 3435 start_state = NFA_START_INVISIBLE_BEFORE_NEG; |
3439 end_state = NFA_END_INVISIBLE_NEG; | 3436 end_state = NFA_END_INVISIBLE_NEG; |
3440 break; | 3437 break; |
3441 default: /* NFA_PREV_ATOM_LIKE_PATTERN: */ | 3438 default: // NFA_PREV_ATOM_LIKE_PATTERN: |
3442 start_state = NFA_START_PATTERN; | 3439 start_state = NFA_START_PATTERN; |
3443 end_state = NFA_END_PATTERN; | 3440 end_state = NFA_END_PATTERN; |
3444 break; | 3441 break; |
3445 } | 3442 } |
3446 | 3443 |
3447 if (before) | 3444 if (before) |
3448 n = *++p; /* get the count */ | 3445 n = *++p; // get the count |
3449 | 3446 |
3450 /* The \@= operator: match the preceding atom with zero width. | 3447 // The \@= operator: match the preceding atom with zero width. |
3451 * The \@! operator: no match for the preceding atom. | 3448 // The \@! operator: no match for the preceding atom. |
3452 * The \@<= operator: match for the preceding atom. | 3449 // The \@<= operator: match for the preceding atom. |
3453 * The \@<! operator: no match for the preceding atom. | 3450 // The \@<! operator: no match for the preceding atom. |
3454 * Surrounds the preceding atom with START_INVISIBLE and | 3451 // Surrounds the preceding atom with START_INVISIBLE and |
3455 * END_INVISIBLE, similarly to MOPEN. */ | 3452 // END_INVISIBLE, similarly to MOPEN. |
3456 | 3453 |
3457 if (nfa_calc_size == TRUE) | 3454 if (nfa_calc_size == TRUE) |
3458 { | 3455 { |
3459 nstate += pattern ? 4 : 2; | 3456 nstate += pattern ? 4 : 2; |
3460 break; | 3457 break; |
3467 s = alloc_state(start_state, e.start, s1); | 3464 s = alloc_state(start_state, e.start, s1); |
3468 if (s == NULL) | 3465 if (s == NULL) |
3469 goto theend; | 3466 goto theend; |
3470 if (pattern) | 3467 if (pattern) |
3471 { | 3468 { |
3472 /* NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. */ | 3469 // NFA_ZEND -> NFA_END_PATTERN -> NFA_SKIP -> what follows. |
3473 skip = alloc_state(NFA_SKIP, NULL, NULL); | 3470 skip = alloc_state(NFA_SKIP, NULL, NULL); |
3474 if (skip == NULL) | 3471 if (skip == NULL) |
3475 goto theend; | 3472 goto theend; |
3476 zend = alloc_state(NFA_ZEND, s1, NULL); | 3473 zend = alloc_state(NFA_ZEND, s1, NULL); |
3477 if (zend == NULL) | 3474 if (zend == NULL) |
3485 patch(e.out, s1); | 3482 patch(e.out, s1); |
3486 PUSH(frag(s, list1(&s1->out))); | 3483 PUSH(frag(s, list1(&s1->out))); |
3487 if (before) | 3484 if (before) |
3488 { | 3485 { |
3489 if (n <= 0) | 3486 if (n <= 0) |
3490 /* See if we can guess the maximum width, it avoids a | 3487 // See if we can guess the maximum width, it avoids a |
3491 * lot of pointless tries. */ | 3488 // lot of pointless tries. |
3492 n = nfa_max_width(e.start, 0); | 3489 n = nfa_max_width(e.start, 0); |
3493 s->val = n; /* store the count */ | 3490 s->val = n; // store the count |
3494 } | 3491 } |
3495 } | 3492 } |
3496 break; | 3493 break; |
3497 } | 3494 } |
3498 | 3495 |
3499 case NFA_COMPOSING: /* char with composing char */ | 3496 case NFA_COMPOSING: // char with composing char |
3500 #if 0 | 3497 #if 0 |
3501 /* TODO */ | 3498 // TODO |
3502 if (regflags & RF_ICOMBINE) | 3499 if (regflags & RF_ICOMBINE) |
3503 { | 3500 { |
3504 /* use the base character only */ | 3501 // use the base character only |
3505 } | 3502 } |
3506 #endif | 3503 #endif |
3507 /* FALLTHROUGH */ | 3504 // FALLTHROUGH |
3508 | 3505 |
3509 case NFA_MOPEN: /* \( \) Submatch */ | 3506 case NFA_MOPEN: // \( \) Submatch |
3510 case NFA_MOPEN1: | 3507 case NFA_MOPEN1: |
3511 case NFA_MOPEN2: | 3508 case NFA_MOPEN2: |
3512 case NFA_MOPEN3: | 3509 case NFA_MOPEN3: |
3513 case NFA_MOPEN4: | 3510 case NFA_MOPEN4: |
3514 case NFA_MOPEN5: | 3511 case NFA_MOPEN5: |
3515 case NFA_MOPEN6: | 3512 case NFA_MOPEN6: |
3516 case NFA_MOPEN7: | 3513 case NFA_MOPEN7: |
3517 case NFA_MOPEN8: | 3514 case NFA_MOPEN8: |
3518 case NFA_MOPEN9: | 3515 case NFA_MOPEN9: |
3519 #ifdef FEAT_SYN_HL | 3516 #ifdef FEAT_SYN_HL |
3520 case NFA_ZOPEN: /* \z( \) Submatch */ | 3517 case NFA_ZOPEN: // \z( \) Submatch |
3521 case NFA_ZOPEN1: | 3518 case NFA_ZOPEN1: |
3522 case NFA_ZOPEN2: | 3519 case NFA_ZOPEN2: |
3523 case NFA_ZOPEN3: | 3520 case NFA_ZOPEN3: |
3524 case NFA_ZOPEN4: | 3521 case NFA_ZOPEN4: |
3525 case NFA_ZOPEN5: | 3522 case NFA_ZOPEN5: |
3526 case NFA_ZOPEN6: | 3523 case NFA_ZOPEN6: |
3527 case NFA_ZOPEN7: | 3524 case NFA_ZOPEN7: |
3528 case NFA_ZOPEN8: | 3525 case NFA_ZOPEN8: |
3529 case NFA_ZOPEN9: | 3526 case NFA_ZOPEN9: |
3530 #endif | 3527 #endif |
3531 case NFA_NOPEN: /* \%( \) "Invisible Submatch" */ | 3528 case NFA_NOPEN: // \%( \) "Invisible Submatch" |
3532 if (nfa_calc_size == TRUE) | 3529 if (nfa_calc_size == TRUE) |
3533 { | 3530 { |
3534 nstate += 2; | 3531 nstate += 2; |
3535 break; | 3532 break; |
3536 } | 3533 } |
3551 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; | 3548 case NFA_ZOPEN8: mclose = NFA_ZCLOSE8; break; |
3552 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; | 3549 case NFA_ZOPEN9: mclose = NFA_ZCLOSE9; break; |
3553 #endif | 3550 #endif |
3554 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; | 3551 case NFA_COMPOSING: mclose = NFA_END_COMPOSING; break; |
3555 default: | 3552 default: |
3556 /* NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 */ | 3553 // NFA_MOPEN, NFA_MOPEN1 .. NFA_MOPEN9 |
3557 mclose = *p + NSUBEXP; | 3554 mclose = *p + NSUBEXP; |
3558 break; | 3555 break; |
3559 } | 3556 } |
3560 | 3557 |
3561 /* Allow "NFA_MOPEN" as a valid postfix representation for | 3558 // Allow "NFA_MOPEN" as a valid postfix representation for |
3562 * the empty regexp "". In this case, the NFA will be | 3559 // the empty regexp "". In this case, the NFA will be |
3563 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows | 3560 // NFA_MOPEN -> NFA_MCLOSE. Note that this also allows |
3564 * empty groups of parenthesis, and empty mbyte chars */ | 3561 // empty groups of parenthesis, and empty mbyte chars |
3565 if (stackp == stack) | 3562 if (stackp == stack) |
3566 { | 3563 { |
3567 s = alloc_state(mopen, NULL, NULL); | 3564 s = alloc_state(mopen, NULL, NULL); |
3568 if (s == NULL) | 3565 if (s == NULL) |
3569 goto theend; | 3566 goto theend; |
3573 patch(list1(&s->out), s1); | 3570 patch(list1(&s->out), s1); |
3574 PUSH(frag(s, list1(&s1->out))); | 3571 PUSH(frag(s, list1(&s1->out))); |
3575 break; | 3572 break; |
3576 } | 3573 } |
3577 | 3574 |
3578 /* At least one node was emitted before NFA_MOPEN, so | 3575 // At least one node was emitted before NFA_MOPEN, so |
3579 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ | 3576 // at least one node will be between NFA_MOPEN and NFA_MCLOSE |
3580 e = POP(); | 3577 e = POP(); |
3581 s = alloc_state(mopen, e.start, NULL); /* `(' */ | 3578 s = alloc_state(mopen, e.start, NULL); // `(' |
3582 if (s == NULL) | 3579 if (s == NULL) |
3583 goto theend; | 3580 goto theend; |
3584 | 3581 |
3585 s1 = alloc_state(mclose, NULL, NULL); /* `)' */ | 3582 s1 = alloc_state(mclose, NULL, NULL); // `)' |
3586 if (s1 == NULL) | 3583 if (s1 == NULL) |
3587 goto theend; | 3584 goto theend; |
3588 patch(e.out, s1); | 3585 patch(e.out, s1); |
3589 | 3586 |
3590 if (mopen == NFA_COMPOSING) | 3587 if (mopen == NFA_COMPOSING) |
3591 /* COMPOSING->out1 = END_COMPOSING */ | 3588 // COMPOSING->out1 = END_COMPOSING |
3592 patch(list1(&s->out1), s1); | 3589 patch(list1(&s->out1), s1); |
3593 | 3590 |
3594 PUSH(frag(s, list1(&s1->out))); | 3591 PUSH(frag(s, list1(&s1->out))); |
3595 break; | 3592 break; |
3596 | 3593 |
3640 case NFA_COL_LT: | 3637 case NFA_COL_LT: |
3641 case NFA_MARK: | 3638 case NFA_MARK: |
3642 case NFA_MARK_GT: | 3639 case NFA_MARK_GT: |
3643 case NFA_MARK_LT: | 3640 case NFA_MARK_LT: |
3644 { | 3641 { |
3645 int n = *++p; /* lnum, col or mark name */ | 3642 int n = *++p; // lnum, col or mark name |
3646 | 3643 |
3647 if (nfa_calc_size == TRUE) | 3644 if (nfa_calc_size == TRUE) |
3648 { | 3645 { |
3649 nstate += 1; | 3646 nstate += 1; |
3650 break; | 3647 break; |
3658 } | 3655 } |
3659 | 3656 |
3660 case NFA_ZSTART: | 3657 case NFA_ZSTART: |
3661 case NFA_ZEND: | 3658 case NFA_ZEND: |
3662 default: | 3659 default: |
3663 /* Operands */ | 3660 // Operands |
3664 if (nfa_calc_size == TRUE) | 3661 if (nfa_calc_size == TRUE) |
3665 { | 3662 { |
3666 nstate++; | 3663 nstate++; |
3667 break; | 3664 break; |
3668 } | 3665 } |
3670 if (s == NULL) | 3667 if (s == NULL) |
3671 goto theend; | 3668 goto theend; |
3672 PUSH(frag(s, list1(&s->out))); | 3669 PUSH(frag(s, list1(&s->out))); |
3673 break; | 3670 break; |
3674 | 3671 |
3675 } /* switch(*p) */ | 3672 } // switch(*p) |
3676 | 3673 |
3677 } /* for(p = postfix; *p; ++p) */ | 3674 } // for(p = postfix; *p; ++p) |
3678 | 3675 |
3679 if (nfa_calc_size == TRUE) | 3676 if (nfa_calc_size == TRUE) |
3680 { | 3677 { |
3681 nstate++; | 3678 nstate++; |
3682 goto theend; /* Return value when counting size is ignored anyway */ | 3679 goto theend; // Return value when counting size is ignored anyway |
3683 } | 3680 } |
3684 | 3681 |
3685 e = POP(); | 3682 e = POP(); |
3686 if (stackp != stack) | 3683 if (stackp != stack) |
3687 { | 3684 { |
3693 { | 3690 { |
3694 vim_free(stack); | 3691 vim_free(stack); |
3695 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); | 3692 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); |
3696 } | 3693 } |
3697 | 3694 |
3698 matchstate = &state_ptr[istate++]; /* the match state */ | 3695 matchstate = &state_ptr[istate++]; // the match state |
3699 matchstate->c = NFA_MATCH; | 3696 matchstate->c = NFA_MATCH; |
3700 matchstate->out = matchstate->out1 = NULL; | 3697 matchstate->out = matchstate->out1 = NULL; |
3701 matchstate->id = 0; | 3698 matchstate->id = 0; |
3702 | 3699 |
3703 patch(e.out, matchstate); | 3700 patch(e.out, matchstate); |
3732 || c == NFA_START_INVISIBLE_BEFORE | 3729 || c == NFA_START_INVISIBLE_BEFORE |
3733 || c == NFA_START_INVISIBLE_BEFORE_NEG) | 3730 || c == NFA_START_INVISIBLE_BEFORE_NEG) |
3734 { | 3731 { |
3735 int directly; | 3732 int directly; |
3736 | 3733 |
3737 /* Do it directly when what follows is possibly the end of the | 3734 // Do it directly when what follows is possibly the end of the |
3738 * match. */ | 3735 // match. |
3739 if (match_follows(prog->state[i].out1->out, 0)) | 3736 if (match_follows(prog->state[i].out1->out, 0)) |
3740 directly = TRUE; | 3737 directly = TRUE; |
3741 else | 3738 else |
3742 { | 3739 { |
3743 int ch_invisible = failure_chance(prog->state[i].out, 0); | 3740 int ch_invisible = failure_chance(prog->state[i].out, 0); |
3744 int ch_follows = failure_chance(prog->state[i].out1->out, 0); | 3741 int ch_follows = failure_chance(prog->state[i].out1->out, 0); |
3745 | 3742 |
3746 /* Postpone when the invisible match is expensive or has a | 3743 // Postpone when the invisible match is expensive or has a |
3747 * lower chance of failing. */ | 3744 // lower chance of failing. |
3748 if (c == NFA_START_INVISIBLE_BEFORE | 3745 if (c == NFA_START_INVISIBLE_BEFORE |
3749 || c == NFA_START_INVISIBLE_BEFORE_NEG) | 3746 || c == NFA_START_INVISIBLE_BEFORE_NEG) |
3750 { | 3747 { |
3751 /* "before" matches are very expensive when | 3748 // "before" matches are very expensive when |
3752 * unbounded, always prefer what follows then, | 3749 // unbounded, always prefer what follows then, |
3753 * unless what follows will always match. | 3750 // unless what follows will always match. |
3754 * Otherwise strongly prefer what follows. */ | 3751 // Otherwise strongly prefer what follows. |
3755 if (prog->state[i].val <= 0 && ch_follows > 0) | 3752 if (prog->state[i].val <= 0 && ch_follows > 0) |
3756 directly = FALSE; | 3753 directly = FALSE; |
3757 else | 3754 else |
3758 directly = ch_follows * 10 < ch_invisible; | 3755 directly = ch_follows * 10 < ch_invisible; |
3759 } | 3756 } |
3760 else | 3757 else |
3761 { | 3758 { |
3762 /* normal invisible, first do the one with the | 3759 // normal invisible, first do the one with the |
3763 * highest failure chance */ | 3760 // highest failure chance |
3764 directly = ch_follows < ch_invisible; | 3761 directly = ch_follows < ch_invisible; |
3765 } | 3762 } |
3766 } | 3763 } |
3767 if (directly) | 3764 if (directly) |
3768 /* switch to the _FIRST state */ | 3765 // switch to the _FIRST state |
3769 ++prog->state[i].c; | 3766 ++prog->state[i].c; |
3770 } | 3767 } |
3771 } | 3768 } |
3772 } | 3769 } |
3773 | 3770 |
3774 /**************************************************************** | 3771 ///////////////////////////////////////////////////////////////// |
3775 * NFA execution code. | 3772 // NFA execution code. |
3776 ****************************************************************/ | 3773 ///////////////////////////////////////////////////////////////// |
3777 | 3774 |
3778 typedef struct | 3775 typedef struct |
3779 { | 3776 { |
3780 int in_use; /* number of subexpr with useful info */ | 3777 int in_use; // number of subexpr with useful info |
3781 | 3778 |
3782 /* When REG_MULTI is TRUE list.multi is used, otherwise list.line. */ | 3779 // When REG_MULTI is TRUE list.multi is used, otherwise list.line. |
3783 union | 3780 union |
3784 { | 3781 { |
3785 struct multipos | 3782 struct multipos |
3786 { | 3783 { |
3787 linenr_T start_lnum; | 3784 linenr_T start_lnum; |
3797 } list; | 3794 } list; |
3798 } regsub_T; | 3795 } regsub_T; |
3799 | 3796 |
3800 typedef struct | 3797 typedef struct |
3801 { | 3798 { |
3802 regsub_T norm; /* \( .. \) matches */ | 3799 regsub_T norm; // \( .. \) matches |
3803 #ifdef FEAT_SYN_HL | 3800 #ifdef FEAT_SYN_HL |
3804 regsub_T synt; /* \z( .. \) matches */ | 3801 regsub_T synt; // \z( .. \) matches |
3805 #endif | 3802 #endif |
3806 } regsubs_T; | 3803 } regsubs_T; |
3807 | 3804 |
3808 /* nfa_pim_T stores a Postponed Invisible Match. */ | 3805 // nfa_pim_T stores a Postponed Invisible Match. |
3809 typedef struct nfa_pim_S nfa_pim_T; | 3806 typedef struct nfa_pim_S nfa_pim_T; |
3810 struct nfa_pim_S | 3807 struct nfa_pim_S |
3811 { | 3808 { |
3812 int result; /* NFA_PIM_*, see below */ | 3809 int result; // NFA_PIM_*, see below |
3813 nfa_state_T *state; /* the invisible match start state */ | 3810 nfa_state_T *state; // the invisible match start state |
3814 regsubs_T subs; /* submatch info, only party used */ | 3811 regsubs_T subs; // submatch info, only party used |
3815 union | 3812 union |
3816 { | 3813 { |
3817 lpos_T pos; | 3814 lpos_T pos; |
3818 char_u *ptr; | 3815 char_u *ptr; |
3819 } end; /* where the match must end */ | 3816 } end; // where the match must end |
3820 }; | 3817 }; |
3821 | 3818 |
3822 /* Values for done in nfa_pim_T. */ | 3819 // Values for done in nfa_pim_T. |
3823 #define NFA_PIM_UNUSED 0 /* pim not used */ | 3820 #define NFA_PIM_UNUSED 0 // pim not used |
3824 #define NFA_PIM_TODO 1 /* pim not done yet */ | 3821 #define NFA_PIM_TODO 1 // pim not done yet |
3825 #define NFA_PIM_MATCH 2 /* pim executed, matches */ | 3822 #define NFA_PIM_MATCH 2 // pim executed, matches |
3826 #define NFA_PIM_NOMATCH 3 /* pim executed, no match */ | 3823 #define NFA_PIM_NOMATCH 3 // pim executed, no match |
3827 | 3824 |
3828 | 3825 |
3829 /* nfa_thread_T contains execution information of a NFA state */ | 3826 // nfa_thread_T contains execution information of a NFA state |
3830 typedef struct | 3827 typedef struct |
3831 { | 3828 { |
3832 nfa_state_T *state; | 3829 nfa_state_T *state; |
3833 int count; | 3830 int count; |
3834 nfa_pim_T pim; /* if pim.result != NFA_PIM_UNUSED: postponed | 3831 nfa_pim_T pim; // if pim.result != NFA_PIM_UNUSED: postponed |
3835 * invisible match */ | 3832 // invisible match |
3836 regsubs_T subs; /* submatch info, only party used */ | 3833 regsubs_T subs; // submatch info, only party used |
3837 } nfa_thread_T; | 3834 } nfa_thread_T; |
3838 | 3835 |
3839 /* nfa_list_T contains the alternative NFA execution states. */ | 3836 // nfa_list_T contains the alternative NFA execution states. |
3840 typedef struct | 3837 typedef struct |
3841 { | 3838 { |
3842 nfa_thread_T *t; /* allocated array of states */ | 3839 nfa_thread_T *t; // allocated array of states |
3843 int n; /* nr of states currently in "t" */ | 3840 int n; // nr of states currently in "t" |
3844 int len; /* max nr of states in "t" */ | 3841 int len; // max nr of states in "t" |
3845 int id; /* ID of the list */ | 3842 int id; // ID of the list |
3846 int has_pim; /* TRUE when any state has a PIM */ | 3843 int has_pim; // TRUE when any state has a PIM |
3847 } nfa_list_T; | 3844 } nfa_list_T; |
3848 | 3845 |
3849 #ifdef ENABLE_LOG | 3846 #ifdef ENABLE_LOG |
3850 static void log_subexpr(regsub_T *sub); | 3847 static void log_subexpr(regsub_T *sub); |
3851 | 3848 |
3899 return buf; | 3896 return buf; |
3900 } | 3897 } |
3901 | 3898 |
3902 #endif | 3899 #endif |
3903 | 3900 |
3904 /* Used during execution: whether a match has been found. */ | 3901 // Used during execution: whether a match has been found. |
3905 static int nfa_match; | 3902 static int nfa_match; |
3906 #ifdef FEAT_RELTIME | 3903 #ifdef FEAT_RELTIME |
3907 static proftime_T *nfa_time_limit; | 3904 static proftime_T *nfa_time_limit; |
3908 static int *nfa_timed_out; | 3905 static int *nfa_timed_out; |
3909 static int nfa_time_count; | 3906 static int nfa_time_count; |
3930 | 3927 |
3931 static void | 3928 static void |
3932 clear_sub(regsub_T *sub) | 3929 clear_sub(regsub_T *sub) |
3933 { | 3930 { |
3934 if (REG_MULTI) | 3931 if (REG_MULTI) |
3935 /* Use 0xff to set lnum to -1 */ | 3932 // Use 0xff to set lnum to -1 |
3936 vim_memset(sub->list.multi, 0xff, | 3933 vim_memset(sub->list.multi, 0xff, |
3937 sizeof(struct multipos) * rex.nfa_nsubexpr); | 3934 sizeof(struct multipos) * rex.nfa_nsubexpr); |
3938 else | 3935 else |
3939 vim_memset(sub->list.line, 0, | 3936 vim_memset(sub->list.line, 0, |
3940 sizeof(struct linepos) * rex.nfa_nsubexpr); | 3937 sizeof(struct linepos) * rex.nfa_nsubexpr); |
3948 copy_sub(regsub_T *to, regsub_T *from) | 3945 copy_sub(regsub_T *to, regsub_T *from) |
3949 { | 3946 { |
3950 to->in_use = from->in_use; | 3947 to->in_use = from->in_use; |
3951 if (from->in_use > 0) | 3948 if (from->in_use > 0) |
3952 { | 3949 { |
3953 /* Copy the match start and end positions. */ | 3950 // Copy the match start and end positions. |
3954 if (REG_MULTI) | 3951 if (REG_MULTI) |
3955 mch_memmove(&to->list.multi[0], | 3952 mch_memmove(&to->list.multi[0], |
3956 &from->list.multi[0], | 3953 &from->list.multi[0], |
3957 sizeof(struct multipos) * from->in_use); | 3954 sizeof(struct multipos) * from->in_use); |
3958 else | 3955 else |
3970 { | 3967 { |
3971 if (to->in_use < from->in_use) | 3968 if (to->in_use < from->in_use) |
3972 to->in_use = from->in_use; | 3969 to->in_use = from->in_use; |
3973 if (from->in_use > 1) | 3970 if (from->in_use > 1) |
3974 { | 3971 { |
3975 /* Copy the match start and end positions. */ | 3972 // Copy the match start and end positions. |
3976 if (REG_MULTI) | 3973 if (REG_MULTI) |
3977 mch_memmove(&to->list.multi[1], | 3974 mch_memmove(&to->list.multi[1], |
3978 &from->list.multi[1], | 3975 &from->list.multi[1], |
3979 sizeof(struct multipos) * (from->in_use - 1)); | 3976 sizeof(struct multipos) * (from->in_use - 1)); |
3980 else | 3977 else |
4119 * Return TRUE if the same state is already in list "l" with the same | 4116 * Return TRUE if the same state is already in list "l" with the same |
4120 * positions as "subs". | 4117 * positions as "subs". |
4121 */ | 4118 */ |
4122 static int | 4119 static int |
4123 has_state_with_pos( | 4120 has_state_with_pos( |
4124 nfa_list_T *l, /* runtime state list */ | 4121 nfa_list_T *l, // runtime state list |
4125 nfa_state_T *state, /* state to update */ | 4122 nfa_state_T *state, // state to update |
4126 regsubs_T *subs, /* pointers to subexpressions */ | 4123 regsubs_T *subs, // pointers to subexpressions |
4127 nfa_pim_T *pim) /* postponed match or NULL */ | 4124 nfa_pim_T *pim) // postponed match or NULL |
4128 { | 4125 { |
4129 nfa_thread_T *thread; | 4126 nfa_thread_T *thread; |
4130 int i; | 4127 int i; |
4131 | 4128 |
4132 for (i = 0; i < l->n; ++i) | 4129 for (i = 0; i < l->n; ++i) |
4153 { | 4150 { |
4154 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED); | 4151 int one_unused = (one == NULL || one->result == NFA_PIM_UNUSED); |
4155 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED); | 4152 int two_unused = (two == NULL || two->result == NFA_PIM_UNUSED); |
4156 | 4153 |
4157 if (one_unused) | 4154 if (one_unused) |
4158 /* one is unused: equal when two is also unused */ | 4155 // one is unused: equal when two is also unused |
4159 return two_unused; | 4156 return two_unused; |
4160 if (two_unused) | 4157 if (two_unused) |
4161 /* one is used and two is not: not equal */ | 4158 // one is used and two is not: not equal |
4162 return FALSE; | 4159 return FALSE; |
4163 /* compare the state id */ | 4160 // compare the state id |
4164 if (one->state->id != two->state->id) | 4161 if (one->state->id != two->state->id) |
4165 return FALSE; | 4162 return FALSE; |
4166 /* compare the position */ | 4163 // compare the position |
4167 if (REG_MULTI) | 4164 if (REG_MULTI) |
4168 return one->end.pos.lnum == two->end.pos.lnum | 4165 return one->end.pos.lnum == two->end.pos.lnum |
4169 && one->end.pos.col == two->end.pos.col; | 4166 && one->end.pos.col == two->end.pos.col; |
4170 return one->end.ptr == two->end.ptr; | 4167 return one->end.ptr == two->end.ptr; |
4171 } | 4168 } |
4176 static int | 4173 static int |
4177 match_follows(nfa_state_T *startstate, int depth) | 4174 match_follows(nfa_state_T *startstate, int depth) |
4178 { | 4175 { |
4179 nfa_state_T *state = startstate; | 4176 nfa_state_T *state = startstate; |
4180 | 4177 |
4181 /* avoid too much recursion */ | 4178 // avoid too much recursion |
4182 if (depth > 10) | 4179 if (depth > 10) |
4183 return FALSE; | 4180 return FALSE; |
4184 | 4181 |
4185 while (state != NULL) | 4182 while (state != NULL) |
4186 { | 4183 { |
4204 case NFA_START_INVISIBLE_NEG: | 4201 case NFA_START_INVISIBLE_NEG: |
4205 case NFA_START_INVISIBLE_NEG_FIRST: | 4202 case NFA_START_INVISIBLE_NEG_FIRST: |
4206 case NFA_START_INVISIBLE_BEFORE_NEG: | 4203 case NFA_START_INVISIBLE_BEFORE_NEG: |
4207 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: | 4204 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
4208 case NFA_COMPOSING: | 4205 case NFA_COMPOSING: |
4209 /* skip ahead to next state */ | 4206 // skip ahead to next state |
4210 state = state->out1->out; | 4207 state = state->out1->out; |
4211 continue; | 4208 continue; |
4212 | 4209 |
4213 case NFA_ANY: | 4210 case NFA_ANY: |
4214 case NFA_ANY_COMPOSING: | 4211 case NFA_ANY_COMPOSING: |
4243 case NFA_UPPER_IC: | 4240 case NFA_UPPER_IC: |
4244 case NFA_NUPPER_IC: | 4241 case NFA_NUPPER_IC: |
4245 case NFA_START_COLL: | 4242 case NFA_START_COLL: |
4246 case NFA_START_NEG_COLL: | 4243 case NFA_START_NEG_COLL: |
4247 case NFA_NEWL: | 4244 case NFA_NEWL: |
4248 /* state will advance input */ | 4245 // state will advance input |
4249 return FALSE; | 4246 return FALSE; |
4250 | 4247 |
4251 default: | 4248 default: |
4252 if (state->c > 0) | 4249 if (state->c > 0) |
4253 /* state will advance input */ | 4250 // state will advance input |
4254 return FALSE; | 4251 return FALSE; |
4255 | 4252 |
4256 /* Others: zero-width or possibly zero-width, might still find | 4253 // Others: zero-width or possibly zero-width, might still find |
4257 * a match at the same position, keep looking. */ | 4254 // a match at the same position, keep looking. |
4258 break; | 4255 break; |
4259 } | 4256 } |
4260 state = state->out; | 4257 state = state->out; |
4261 } | 4258 } |
4262 return FALSE; | 4259 return FALSE; |
4266 /* | 4263 /* |
4267 * Return TRUE if "state" is already in list "l". | 4264 * Return TRUE if "state" is already in list "l". |
4268 */ | 4265 */ |
4269 static int | 4266 static int |
4270 state_in_list( | 4267 state_in_list( |
4271 nfa_list_T *l, /* runtime state list */ | 4268 nfa_list_T *l, // runtime state list |
4272 nfa_state_T *state, /* state to update */ | 4269 nfa_state_T *state, // state to update |
4273 regsubs_T *subs) /* pointers to subexpressions */ | 4270 regsubs_T *subs) // pointers to subexpressions |
4274 { | 4271 { |
4275 if (state->lastlist[nfa_ll_index] == l->id) | 4272 if (state->lastlist[nfa_ll_index] == l->id) |
4276 { | 4273 { |
4277 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL)) | 4274 if (!rex.nfa_has_backref || has_state_with_pos(l, state, subs, NULL)) |
4278 return TRUE; | 4275 return TRUE; |
4279 } | 4276 } |
4280 return FALSE; | 4277 return FALSE; |
4281 } | 4278 } |
4282 | 4279 |
4283 /* Offset used for "off" by addstate_here(). */ | 4280 // Offset used for "off" by addstate_here(). |
4284 #define ADDSTATE_HERE_OFFSET 10 | 4281 #define ADDSTATE_HERE_OFFSET 10 |
4285 | 4282 |
4286 /* | 4283 /* |
4287 * Add "state" and possibly what follows to state list ".". | 4284 * Add "state" and possibly what follows to state list ".". |
4288 * Returns "subs_arg", possibly copied into temp_subs. | 4285 * Returns "subs_arg", possibly copied into temp_subs. |
4289 * Returns NULL when recursiveness is too deep. | 4286 * Returns NULL when recursiveness is too deep. |
4290 */ | 4287 */ |
4291 static regsubs_T * | 4288 static regsubs_T * |
4292 addstate( | 4289 addstate( |
4293 nfa_list_T *l, /* runtime state list */ | 4290 nfa_list_T *l, // runtime state list |
4294 nfa_state_T *state, /* state to update */ | 4291 nfa_state_T *state, // state to update |
4295 regsubs_T *subs_arg, /* pointers to subexpressions */ | 4292 regsubs_T *subs_arg, // pointers to subexpressions |
4296 nfa_pim_T *pim, /* postponed look-behind match */ | 4293 nfa_pim_T *pim, // postponed look-behind match |
4297 int off_arg) /* byte offset, when -1 go to next line */ | 4294 int off_arg) // byte offset, when -1 go to next line |
4298 { | 4295 { |
4299 int subidx; | 4296 int subidx; |
4300 int off = off_arg; | 4297 int off = off_arg; |
4301 int add_here = FALSE; | 4298 int add_here = FALSE; |
4302 int listindex = 0; | 4299 int listindex = 0; |
4357 #endif | 4354 #endif |
4358 case NFA_MOPEN: | 4355 case NFA_MOPEN: |
4359 case NFA_ZEND: | 4356 case NFA_ZEND: |
4360 case NFA_SPLIT: | 4357 case NFA_SPLIT: |
4361 case NFA_EMPTY: | 4358 case NFA_EMPTY: |
4362 /* These nodes are not added themselves but their "out" and/or | 4359 // These nodes are not added themselves but their "out" and/or |
4363 * "out1" may be added below. */ | 4360 // "out1" may be added below. |
4364 break; | 4361 break; |
4365 | 4362 |
4366 case NFA_BOL: | 4363 case NFA_BOL: |
4367 case NFA_BOF: | 4364 case NFA_BOF: |
4368 /* "^" won't match past end-of-line, don't bother trying. | 4365 // "^" won't match past end-of-line, don't bother trying. |
4369 * Except when at the end of the line, or when we are going to the | 4366 // Except when at the end of the line, or when we are going to the |
4370 * next line for a look-behind match. */ | 4367 // next line for a look-behind match. |
4371 if (rex.input > rex.line | 4368 if (rex.input > rex.line |
4372 && *rex.input != NUL | 4369 && *rex.input != NUL |
4373 && (nfa_endp == NULL | 4370 && (nfa_endp == NULL |
4374 || !REG_MULTI | 4371 || !REG_MULTI |
4375 || rex.lnum == nfa_endp->se_u.pos.lnum)) | 4372 || rex.lnum == nfa_endp->se_u.pos.lnum)) |
4376 goto skip_add; | 4373 goto skip_add; |
4377 /* FALLTHROUGH */ | 4374 // FALLTHROUGH |
4378 | 4375 |
4379 case NFA_MOPEN1: | 4376 case NFA_MOPEN1: |
4380 case NFA_MOPEN2: | 4377 case NFA_MOPEN2: |
4381 case NFA_MOPEN3: | 4378 case NFA_MOPEN3: |
4382 case NFA_MOPEN4: | 4379 case NFA_MOPEN4: |
4397 case NFA_ZOPEN8: | 4394 case NFA_ZOPEN8: |
4398 case NFA_ZOPEN9: | 4395 case NFA_ZOPEN9: |
4399 #endif | 4396 #endif |
4400 case NFA_NOPEN: | 4397 case NFA_NOPEN: |
4401 case NFA_ZSTART: | 4398 case NFA_ZSTART: |
4402 /* These nodes need to be added so that we can bail out when it | 4399 // These nodes need to be added so that we can bail out when it |
4403 * was added to this list before at the same position to avoid an | 4400 // was added to this list before at the same position to avoid an |
4404 * endless loop for "\(\)*" */ | 4401 // endless loop for "\(\)*" |
4405 | 4402 |
4406 default: | 4403 default: |
4407 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP) | 4404 if (state->lastlist[nfa_ll_index] == l->id && state->c != NFA_SKIP) |
4408 { | 4405 { |
4409 /* This state is already in the list, don't add it again, | 4406 // This state is already in the list, don't add it again, |
4410 * unless it is an MOPEN that is used for a backreference or | 4407 // unless it is an MOPEN that is used for a backreference or |
4411 * when there is a PIM. For NFA_MATCH check the position, | 4408 // when there is a PIM. For NFA_MATCH check the position, |
4412 * lower position is preferred. */ | 4409 // lower position is preferred. |
4413 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim | 4410 if (!rex.nfa_has_backref && pim == NULL && !l->has_pim |
4414 && state->c != NFA_MATCH) | 4411 && state->c != NFA_MATCH) |
4415 { | 4412 { |
4416 /* When called from addstate_here() do insert before | 4413 // When called from addstate_here() do insert before |
4417 * existing states. */ | 4414 // existing states. |
4418 if (add_here) | 4415 if (add_here) |
4419 { | 4416 { |
4420 for (k = 0; k < l->n && k < listindex; ++k) | 4417 for (k = 0; k < l->n && k < listindex; ++k) |
4421 if (l->t[k].state->id == state->id) | 4418 if (l->t[k].state->id == state->id) |
4422 { | 4419 { |
4436 --depth; | 4433 --depth; |
4437 return subs; | 4434 return subs; |
4438 } | 4435 } |
4439 } | 4436 } |
4440 | 4437 |
4441 /* Do not add the state again when it exists with the same | 4438 // Do not add the state again when it exists with the same |
4442 * positions. */ | 4439 // positions. |
4443 if (has_state_with_pos(l, state, subs, pim)) | 4440 if (has_state_with_pos(l, state, subs, pim)) |
4444 goto skip_add; | 4441 goto skip_add; |
4445 } | 4442 } |
4446 | 4443 |
4447 // When there are backreferences or PIMs the number of states may | 4444 // When there are backreferences or PIMs the number of states may |
4458 --depth; | 4455 --depth; |
4459 return NULL; | 4456 return NULL; |
4460 } | 4457 } |
4461 if (subs != &temp_subs) | 4458 if (subs != &temp_subs) |
4462 { | 4459 { |
4463 /* "subs" may point into the current array, need to make a | 4460 // "subs" may point into the current array, need to make a |
4464 * copy before it becomes invalid. */ | 4461 // copy before it becomes invalid. |
4465 copy_sub(&temp_subs.norm, &subs->norm); | 4462 copy_sub(&temp_subs.norm, &subs->norm); |
4466 #ifdef FEAT_SYN_HL | 4463 #ifdef FEAT_SYN_HL |
4467 if (rex.nfa_has_zsubexpr) | 4464 if (rex.nfa_has_zsubexpr) |
4468 copy_sub(&temp_subs.synt, &subs->synt); | 4465 copy_sub(&temp_subs.synt, &subs->synt); |
4469 #endif | 4466 #endif |
4479 } | 4476 } |
4480 l->t = newt; | 4477 l->t = newt; |
4481 l->len = newlen; | 4478 l->len = newlen; |
4482 } | 4479 } |
4483 | 4480 |
4484 /* add the state to the list */ | 4481 // add the state to the list |
4485 state->lastlist[nfa_ll_index] = l->id; | 4482 state->lastlist[nfa_ll_index] = l->id; |
4486 thread = &l->t[l->n++]; | 4483 thread = &l->t[l->n++]; |
4487 thread->state = state; | 4484 thread->state = state; |
4488 if (pim == NULL) | 4485 if (pim == NULL) |
4489 thread->pim.result = NFA_PIM_UNUSED; | 4486 thread->pim.result = NFA_PIM_UNUSED; |
4511 { | 4508 { |
4512 case NFA_MATCH: | 4509 case NFA_MATCH: |
4513 break; | 4510 break; |
4514 | 4511 |
4515 case NFA_SPLIT: | 4512 case NFA_SPLIT: |
4516 /* order matters here */ | 4513 // order matters here |
4517 subs = addstate(l, state->out, subs, pim, off_arg); | 4514 subs = addstate(l, state->out, subs, pim, off_arg); |
4518 subs = addstate(l, state->out1, subs, pim, off_arg); | 4515 subs = addstate(l, state->out1, subs, pim, off_arg); |
4519 break; | 4516 break; |
4520 | 4517 |
4521 case NFA_EMPTY: | 4518 case NFA_EMPTY: |
4563 { | 4560 { |
4564 subidx = state->c - NFA_MOPEN; | 4561 subidx = state->c - NFA_MOPEN; |
4565 sub = &subs->norm; | 4562 sub = &subs->norm; |
4566 } | 4563 } |
4567 | 4564 |
4568 /* avoid compiler warnings */ | 4565 // avoid compiler warnings |
4569 save_ptr = NULL; | 4566 save_ptr = NULL; |
4570 vim_memset(&save_multipos, 0, sizeof(save_multipos)); | 4567 vim_memset(&save_multipos, 0, sizeof(save_multipos)); |
4571 | 4568 |
4572 /* Set the position (with "off" added) in the subexpression. Save | 4569 // Set the position (with "off" added) in the subexpression. Save |
4573 * and restore it when it was in use. Otherwise fill any gap. */ | 4570 // and restore it when it was in use. Otherwise fill any gap. |
4574 if (REG_MULTI) | 4571 if (REG_MULTI) |
4575 { | 4572 { |
4576 if (subidx < sub->in_use) | 4573 if (subidx < sub->in_use) |
4577 { | 4574 { |
4578 save_multipos = sub->list.multi[subidx]; | 4575 save_multipos = sub->list.multi[subidx]; |
4650 { | 4647 { |
4651 // Do not overwrite the position set by \ze. | 4648 // Do not overwrite the position set by \ze. |
4652 subs = addstate(l, state->out, subs, pim, off_arg); | 4649 subs = addstate(l, state->out, subs, pim, off_arg); |
4653 break; | 4650 break; |
4654 } | 4651 } |
4655 /* FALLTHROUGH */ | 4652 // FALLTHROUGH |
4656 case NFA_MCLOSE1: | 4653 case NFA_MCLOSE1: |
4657 case NFA_MCLOSE2: | 4654 case NFA_MCLOSE2: |
4658 case NFA_MCLOSE3: | 4655 case NFA_MCLOSE3: |
4659 case NFA_MCLOSE4: | 4656 case NFA_MCLOSE4: |
4660 case NFA_MCLOSE5: | 4657 case NFA_MCLOSE5: |
4691 { | 4688 { |
4692 subidx = state->c - NFA_MCLOSE; | 4689 subidx = state->c - NFA_MCLOSE; |
4693 sub = &subs->norm; | 4690 sub = &subs->norm; |
4694 } | 4691 } |
4695 | 4692 |
4696 /* We don't fill in gaps here, there must have been an MOPEN that | 4693 // We don't fill in gaps here, there must have been an MOPEN that |
4697 * has done that. */ | 4694 // has done that. |
4698 save_in_use = sub->in_use; | 4695 save_in_use = sub->in_use; |
4699 if (sub->in_use <= subidx) | 4696 if (sub->in_use <= subidx) |
4700 sub->in_use = subidx + 1; | 4697 sub->in_use = subidx + 1; |
4701 if (REG_MULTI) | 4698 if (REG_MULTI) |
4702 { | 4699 { |
4710 { | 4707 { |
4711 sub->list.multi[subidx].end_lnum = rex.lnum; | 4708 sub->list.multi[subidx].end_lnum = rex.lnum; |
4712 sub->list.multi[subidx].end_col = | 4709 sub->list.multi[subidx].end_col = |
4713 (colnr_T)(rex.input - rex.line + off); | 4710 (colnr_T)(rex.input - rex.line + off); |
4714 } | 4711 } |
4715 /* avoid compiler warnings */ | 4712 // avoid compiler warnings |
4716 save_ptr = NULL; | 4713 save_ptr = NULL; |
4717 } | 4714 } |
4718 else | 4715 else |
4719 { | 4716 { |
4720 save_ptr = sub->list.line[subidx].end; | 4717 save_ptr = sub->list.line[subidx].end; |
4721 sub->list.line[subidx].end = rex.input + off; | 4718 sub->list.line[subidx].end = rex.input + off; |
4722 /* avoid compiler warnings */ | 4719 // avoid compiler warnings |
4723 vim_memset(&save_multipos, 0, sizeof(save_multipos)); | 4720 vim_memset(&save_multipos, 0, sizeof(save_multipos)); |
4724 } | 4721 } |
4725 | 4722 |
4726 subs = addstate(l, state->out, subs, pim, off_arg); | 4723 subs = addstate(l, state->out, subs, pim, off_arg); |
4727 if (subs == NULL) | 4724 if (subs == NULL) |
4728 break; | 4725 break; |
4729 /* "subs" may have changed, need to set "sub" again */ | 4726 // "subs" may have changed, need to set "sub" again |
4730 #ifdef FEAT_SYN_HL | 4727 #ifdef FEAT_SYN_HL |
4731 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) | 4728 if (state->c >= NFA_ZCLOSE && state->c <= NFA_ZCLOSE9) |
4732 sub = &subs->synt; | 4729 sub = &subs->synt; |
4733 else | 4730 else |
4734 #endif | 4731 #endif |
4751 * This makes sure the order of states to be tried does not change, which | 4748 * This makes sure the order of states to be tried does not change, which |
4752 * matters for alternatives. | 4749 * matters for alternatives. |
4753 */ | 4750 */ |
4754 static regsubs_T * | 4751 static regsubs_T * |
4755 addstate_here( | 4752 addstate_here( |
4756 nfa_list_T *l, /* runtime state list */ | 4753 nfa_list_T *l, // runtime state list |
4757 nfa_state_T *state, /* state to update */ | 4754 nfa_state_T *state, // state to update |
4758 regsubs_T *subs, /* pointers to subexpressions */ | 4755 regsubs_T *subs, // pointers to subexpressions |
4759 nfa_pim_T *pim, /* postponed look-behind match */ | 4756 nfa_pim_T *pim, // postponed look-behind match |
4760 int *ip) | 4757 int *ip) |
4761 { | 4758 { |
4762 int tlen = l->n; | 4759 int tlen = l->n; |
4763 int count; | 4760 int count; |
4764 int listidx = *ip; | 4761 int listidx = *ip; |
4765 regsubs_T *r; | 4762 regsubs_T *r; |
4766 | 4763 |
4767 /* First add the state(s) at the end, so that we know how many there are. | 4764 // First add the state(s) at the end, so that we know how many there are. |
4768 * Pass the listidx as offset (avoids adding another argument to | 4765 // Pass the listidx as offset (avoids adding another argument to |
4769 * addstate(). */ | 4766 // addstate(). |
4770 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET); | 4767 r = addstate(l, state, subs, pim, -listidx - ADDSTATE_HERE_OFFSET); |
4771 if (r == NULL) | 4768 if (r == NULL) |
4772 return NULL; | 4769 return NULL; |
4773 | 4770 |
4774 // when "*ip" was at the end of the list, nothing to do | 4771 // when "*ip" was at the end of the list, nothing to do |
4786 } | 4783 } |
4787 else if (count > 1) | 4784 else if (count > 1) |
4788 { | 4785 { |
4789 if (l->n + count - 1 >= l->len) | 4786 if (l->n + count - 1 >= l->len) |
4790 { | 4787 { |
4791 /* not enough space to move the new states, reallocate the list | 4788 // not enough space to move the new states, reallocate the list |
4792 * and move the states to the right position */ | 4789 // and move the states to the right position |
4793 int newlen = l->len * 3 / 2 + 50; | 4790 int newlen = l->len * 3 / 2 + 50; |
4794 size_t newsize = newlen * sizeof(nfa_thread_T); | 4791 size_t newsize = newlen * sizeof(nfa_thread_T); |
4795 nfa_thread_T *newl; | 4792 nfa_thread_T *newl; |
4796 | 4793 |
4797 if ((long)(newsize >> 10) >= p_mmp) | 4794 if ((long)(newsize >> 10) >= p_mmp) |
4815 vim_free(l->t); | 4812 vim_free(l->t); |
4816 l->t = newl; | 4813 l->t = newl; |
4817 } | 4814 } |
4818 else | 4815 else |
4819 { | 4816 { |
4820 /* make space for new states, then move them from the | 4817 // make space for new states, then move them from the |
4821 * end to the current position */ | 4818 // end to the current position |
4822 mch_memmove(&(l->t[listidx + count]), | 4819 mch_memmove(&(l->t[listidx + count]), |
4823 &(l->t[listidx + 1]), | 4820 &(l->t[listidx + 1]), |
4824 sizeof(nfa_thread_T) * (l->n - listidx - 1)); | 4821 sizeof(nfa_thread_T) * (l->n - listidx - 1)); |
4825 mch_memmove(&(l->t[listidx]), | 4822 mch_memmove(&(l->t[listidx]), |
4826 &(l->t[l->n - 1]), | 4823 &(l->t[l->n - 1]), |
4917 if (vim_isfilec(c)) | 4914 if (vim_isfilec(c)) |
4918 return OK; | 4915 return OK; |
4919 break; | 4916 break; |
4920 | 4917 |
4921 default: | 4918 default: |
4922 /* should not be here :P */ | 4919 // should not be here :P |
4923 siemsg(_(e_ill_char_class), class); | 4920 siemsg(_(e_ill_char_class), class); |
4924 return FAIL; | 4921 return FAIL; |
4925 } | 4922 } |
4926 return FAIL; | 4923 return FAIL; |
4927 } | 4924 } |
4930 * Check for a match with subexpression "subidx". | 4927 * Check for a match with subexpression "subidx". |
4931 * Return TRUE if it matches. | 4928 * Return TRUE if it matches. |
4932 */ | 4929 */ |
4933 static int | 4930 static int |
4934 match_backref( | 4931 match_backref( |
4935 regsub_T *sub, /* pointers to subexpressions */ | 4932 regsub_T *sub, // pointers to subexpressions |
4936 int subidx, | 4933 int subidx, |
4937 int *bytelen) /* out: length of match in bytes */ | 4934 int *bytelen) // out: length of match in bytes |
4938 { | 4935 { |
4939 int len; | 4936 int len; |
4940 | 4937 |
4941 if (sub->in_use <= subidx) | 4938 if (sub->in_use <= subidx) |
4942 { | 4939 { |
4943 retempty: | 4940 retempty: |
4944 /* backref was not set, match an empty string */ | 4941 // backref was not set, match an empty string |
4945 *bytelen = 0; | 4942 *bytelen = 0; |
4946 return TRUE; | 4943 return TRUE; |
4947 } | 4944 } |
4948 | 4945 |
4949 if (REG_MULTI) | 4946 if (REG_MULTI) |
4996 * Return TRUE if it matches. | 4993 * Return TRUE if it matches. |
4997 */ | 4994 */ |
4998 static int | 4995 static int |
4999 match_zref( | 4996 match_zref( |
5000 int subidx, | 4997 int subidx, |
5001 int *bytelen) /* out: length of match in bytes */ | 4998 int *bytelen) // out: length of match in bytes |
5002 { | 4999 { |
5003 int len; | 5000 int len; |
5004 | 5001 |
5005 cleanup_zsubexpr(); | 5002 cleanup_zsubexpr(); |
5006 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) | 5003 if (re_extmatch_in == NULL || re_extmatch_in->matches[subidx] == NULL) |
5007 { | 5004 { |
5008 /* backref was not set, match an empty string */ | 5005 // backref was not set, match an empty string |
5009 *bytelen = 0; | 5006 *bytelen = 0; |
5010 return TRUE; | 5007 return TRUE; |
5011 } | 5008 } |
5012 | 5009 |
5013 len = (int)STRLEN(re_extmatch_in->matches[subidx]); | 5010 len = (int)STRLEN(re_extmatch_in->matches[subidx]); |
5029 nfa_save_listids(nfa_regprog_T *prog, int *list) | 5026 nfa_save_listids(nfa_regprog_T *prog, int *list) |
5030 { | 5027 { |
5031 int i; | 5028 int i; |
5032 nfa_state_T *p; | 5029 nfa_state_T *p; |
5033 | 5030 |
5034 /* Order in the list is reverse, it's a bit faster that way. */ | 5031 // Order in the list is reverse, it's a bit faster that way. |
5035 p = &prog->state[0]; | 5032 p = &prog->state[0]; |
5036 for (i = prog->nstate; --i >= 0; ) | 5033 for (i = prog->nstate; --i >= 0; ) |
5037 { | 5034 { |
5038 list[i] = p->lastlist[1]; | 5035 list[i] = p->lastlist[1]; |
5039 p->lastlist[1] = 0; | 5036 p->lastlist[1] = 0; |
5093 int result; | 5090 int result; |
5094 int need_restore = FALSE; | 5091 int need_restore = FALSE; |
5095 | 5092 |
5096 if (pim != NULL) | 5093 if (pim != NULL) |
5097 { | 5094 { |
5098 /* start at the position where the postponed match was */ | 5095 // start at the position where the postponed match was |
5099 if (REG_MULTI) | 5096 if (REG_MULTI) |
5100 rex.input = rex.line + pim->end.pos.col; | 5097 rex.input = rex.line + pim->end.pos.col; |
5101 else | 5098 else |
5102 rex.input = pim->end.ptr; | 5099 rex.input = pim->end.ptr; |
5103 } | 5100 } |
5105 if (state->c == NFA_START_INVISIBLE_BEFORE | 5102 if (state->c == NFA_START_INVISIBLE_BEFORE |
5106 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST | 5103 || state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
5107 || state->c == NFA_START_INVISIBLE_BEFORE_NEG | 5104 || state->c == NFA_START_INVISIBLE_BEFORE_NEG |
5108 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) | 5105 || state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
5109 { | 5106 { |
5110 /* The recursive match must end at the current position. When "pim" is | 5107 // The recursive match must end at the current position. When "pim" is |
5111 * not NULL it specifies the current position. */ | 5108 // not NULL it specifies the current position. |
5112 endposp = &endpos; | 5109 endposp = &endpos; |
5113 if (REG_MULTI) | 5110 if (REG_MULTI) |
5114 { | 5111 { |
5115 if (pim == NULL) | 5112 if (pim == NULL) |
5116 { | 5113 { |
5126 endpos.se_u.ptr = rex.input; | 5123 endpos.se_u.ptr = rex.input; |
5127 else | 5124 else |
5128 endpos.se_u.ptr = pim->end.ptr; | 5125 endpos.se_u.ptr = pim->end.ptr; |
5129 } | 5126 } |
5130 | 5127 |
5131 /* Go back the specified number of bytes, or as far as the | 5128 // Go back the specified number of bytes, or as far as the |
5132 * start of the previous line, to try matching "\@<=" or | 5129 // start of the previous line, to try matching "\@<=" or |
5133 * not matching "\@<!". This is very inefficient, limit the number of | 5130 // not matching "\@<!". This is very inefficient, limit the number of |
5134 * bytes if possible. */ | 5131 // bytes if possible. |
5135 if (state->val <= 0) | 5132 if (state->val <= 0) |
5136 { | 5133 { |
5137 if (REG_MULTI) | 5134 if (REG_MULTI) |
5138 { | 5135 { |
5139 rex.line = reg_getline(--rex.lnum); | 5136 rex.line = reg_getline(--rex.lnum); |
5140 if (rex.line == NULL) | 5137 if (rex.line == NULL) |
5141 /* can't go before the first line */ | 5138 // can't go before the first line |
5142 rex.line = reg_getline(++rex.lnum); | 5139 rex.line = reg_getline(++rex.lnum); |
5143 } | 5140 } |
5144 rex.input = rex.line; | 5141 rex.input = rex.line; |
5145 } | 5142 } |
5146 else | 5143 else |
5147 { | 5144 { |
5148 if (REG_MULTI && (int)(rex.input - rex.line) < state->val) | 5145 if (REG_MULTI && (int)(rex.input - rex.line) < state->val) |
5149 { | 5146 { |
5150 /* Not enough bytes in this line, go to end of | 5147 // Not enough bytes in this line, go to end of |
5151 * previous line. */ | 5148 // previous line. |
5152 rex.line = reg_getline(--rex.lnum); | 5149 rex.line = reg_getline(--rex.lnum); |
5153 if (rex.line == NULL) | 5150 if (rex.line == NULL) |
5154 { | 5151 { |
5155 /* can't go before the first line */ | 5152 // can't go before the first line |
5156 rex.line = reg_getline(++rex.lnum); | 5153 rex.line = reg_getline(++rex.lnum); |
5157 rex.input = rex.line; | 5154 rex.input = rex.line; |
5158 } | 5155 } |
5159 else | 5156 else |
5160 rex.input = rex.line + STRLEN(rex.line); | 5157 rex.input = rex.line + STRLEN(rex.line); |
5173 #ifdef ENABLE_LOG | 5170 #ifdef ENABLE_LOG |
5174 if (log_fd != stderr) | 5171 if (log_fd != stderr) |
5175 fclose(log_fd); | 5172 fclose(log_fd); |
5176 log_fd = NULL; | 5173 log_fd = NULL; |
5177 #endif | 5174 #endif |
5178 /* Have to clear the lastlist field of the NFA nodes, so that | 5175 // Have to clear the lastlist field of the NFA nodes, so that |
5179 * nfa_regmatch() and addstate() can run properly after recursion. */ | 5176 // nfa_regmatch() and addstate() can run properly after recursion. |
5180 if (nfa_ll_index == 1) | 5177 if (nfa_ll_index == 1) |
5181 { | 5178 { |
5182 /* Already calling nfa_regmatch() recursively. Save the lastlist[1] | 5179 // Already calling nfa_regmatch() recursively. Save the lastlist[1] |
5183 * values and clear them. */ | 5180 // values and clear them. |
5184 if (*listids == NULL || *listids_len < prog->nstate) | 5181 if (*listids == NULL || *listids_len < prog->nstate) |
5185 { | 5182 { |
5186 vim_free(*listids); | 5183 vim_free(*listids); |
5187 *listids = ALLOC_MULT(int, prog->nstate); | 5184 *listids = ALLOC_MULT(int, prog->nstate); |
5188 if (*listids == NULL) | 5185 if (*listids == NULL) |
5192 } | 5189 } |
5193 *listids_len = prog->nstate; | 5190 *listids_len = prog->nstate; |
5194 } | 5191 } |
5195 nfa_save_listids(prog, *listids); | 5192 nfa_save_listids(prog, *listids); |
5196 need_restore = TRUE; | 5193 need_restore = TRUE; |
5197 /* any value of rex.nfa_listid will do */ | 5194 // any value of rex.nfa_listid will do |
5198 } | 5195 } |
5199 else | 5196 else |
5200 { | 5197 { |
5201 /* First recursive nfa_regmatch() call, switch to the second lastlist | 5198 // First recursive nfa_regmatch() call, switch to the second lastlist |
5202 * entry. Make sure rex.nfa_listid is different from a previous | 5199 // entry. Make sure rex.nfa_listid is different from a previous |
5203 * recursive call, because some states may still have this ID. */ | 5200 // recursive call, because some states may still have this ID. |
5204 ++nfa_ll_index; | 5201 ++nfa_ll_index; |
5205 if (rex.nfa_listid <= rex.nfa_alt_listid) | 5202 if (rex.nfa_listid <= rex.nfa_alt_listid) |
5206 rex.nfa_listid = rex.nfa_alt_listid; | 5203 rex.nfa_listid = rex.nfa_alt_listid; |
5207 } | 5204 } |
5208 | 5205 |
5209 /* Call nfa_regmatch() to check if the current concat matches at this | 5206 // Call nfa_regmatch() to check if the current concat matches at this |
5210 * position. The concat ends with the node NFA_END_INVISIBLE */ | 5207 // position. The concat ends with the node NFA_END_INVISIBLE |
5211 nfa_endp = endposp; | 5208 nfa_endp = endposp; |
5212 result = nfa_regmatch(prog, state->out, submatch, m); | 5209 result = nfa_regmatch(prog, state->out, submatch, m); |
5213 | 5210 |
5214 if (need_restore) | 5211 if (need_restore) |
5215 nfa_restore_listids(prog, *listids); | 5212 nfa_restore_listids(prog, *listids); |
5217 { | 5214 { |
5218 --nfa_ll_index; | 5215 --nfa_ll_index; |
5219 rex.nfa_alt_listid = rex.nfa_listid; | 5216 rex.nfa_alt_listid = rex.nfa_listid; |
5220 } | 5217 } |
5221 | 5218 |
5222 /* restore position in input text */ | 5219 // restore position in input text |
5223 rex.lnum = save_reglnum; | 5220 rex.lnum = save_reglnum; |
5224 if (REG_MULTI) | 5221 if (REG_MULTI) |
5225 rex.line = reg_getline(rex.lnum); | 5222 rex.line = reg_getline(rex.lnum); |
5226 rex.input = rex.line + save_reginput_col; | 5223 rex.input = rex.line + save_reginput_col; |
5227 if (result != NFA_TOO_EXPENSIVE) | 5224 if (result != NFA_TOO_EXPENSIVE) |
5260 failure_chance(nfa_state_T *state, int depth) | 5257 failure_chance(nfa_state_T *state, int depth) |
5261 { | 5258 { |
5262 int c = state->c; | 5259 int c = state->c; |
5263 int l, r; | 5260 int l, r; |
5264 | 5261 |
5265 /* detect looping */ | 5262 // detect looping |
5266 if (depth > 4) | 5263 if (depth > 4) |
5267 return 1; | 5264 return 1; |
5268 | 5265 |
5269 switch (c) | 5266 switch (c) |
5270 { | 5267 { |
5271 case NFA_SPLIT: | 5268 case NFA_SPLIT: |
5272 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) | 5269 if (state->out->c == NFA_SPLIT || state->out1->c == NFA_SPLIT) |
5273 /* avoid recursive stuff */ | 5270 // avoid recursive stuff |
5274 return 1; | 5271 return 1; |
5275 /* two alternatives, use the lowest failure chance */ | 5272 // two alternatives, use the lowest failure chance |
5276 l = failure_chance(state->out, depth + 1); | 5273 l = failure_chance(state->out, depth + 1); |
5277 r = failure_chance(state->out1, depth + 1); | 5274 r = failure_chance(state->out1, depth + 1); |
5278 return l < r ? l : r; | 5275 return l < r ? l : r; |
5279 | 5276 |
5280 case NFA_ANY: | 5277 case NFA_ANY: |
5281 /* matches anything, unlikely to fail */ | 5278 // matches anything, unlikely to fail |
5282 return 1; | 5279 return 1; |
5283 | 5280 |
5284 case NFA_MATCH: | 5281 case NFA_MATCH: |
5285 case NFA_MCLOSE: | 5282 case NFA_MCLOSE: |
5286 case NFA_ANY_COMPOSING: | 5283 case NFA_ANY_COMPOSING: |
5287 /* empty match works always */ | 5284 // empty match works always |
5288 return 0; | 5285 return 0; |
5289 | 5286 |
5290 case NFA_START_INVISIBLE: | 5287 case NFA_START_INVISIBLE: |
5291 case NFA_START_INVISIBLE_FIRST: | 5288 case NFA_START_INVISIBLE_FIRST: |
5292 case NFA_START_INVISIBLE_NEG: | 5289 case NFA_START_INVISIBLE_NEG: |
5294 case NFA_START_INVISIBLE_BEFORE: | 5291 case NFA_START_INVISIBLE_BEFORE: |
5295 case NFA_START_INVISIBLE_BEFORE_FIRST: | 5292 case NFA_START_INVISIBLE_BEFORE_FIRST: |
5296 case NFA_START_INVISIBLE_BEFORE_NEG: | 5293 case NFA_START_INVISIBLE_BEFORE_NEG: |
5297 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: | 5294 case NFA_START_INVISIBLE_BEFORE_NEG_FIRST: |
5298 case NFA_START_PATTERN: | 5295 case NFA_START_PATTERN: |
5299 /* recursive regmatch is expensive, use low failure chance */ | 5296 // recursive regmatch is expensive, use low failure chance |
5300 return 5; | 5297 return 5; |
5301 | 5298 |
5302 case NFA_BOL: | 5299 case NFA_BOL: |
5303 case NFA_EOL: | 5300 case NFA_EOL: |
5304 case NFA_BOF: | 5301 case NFA_BOF: |
5373 case NFA_ZREF6: | 5370 case NFA_ZREF6: |
5374 case NFA_ZREF7: | 5371 case NFA_ZREF7: |
5375 case NFA_ZREF8: | 5372 case NFA_ZREF8: |
5376 case NFA_ZREF9: | 5373 case NFA_ZREF9: |
5377 #endif | 5374 #endif |
5378 /* backreferences don't match in many places */ | 5375 // backreferences don't match in many places |
5379 return 94; | 5376 return 94; |
5380 | 5377 |
5381 case NFA_LNUM_GT: | 5378 case NFA_LNUM_GT: |
5382 case NFA_LNUM_LT: | 5379 case NFA_LNUM_LT: |
5383 case NFA_COL_GT: | 5380 case NFA_COL_GT: |
5385 case NFA_VCOL_GT: | 5382 case NFA_VCOL_GT: |
5386 case NFA_VCOL_LT: | 5383 case NFA_VCOL_LT: |
5387 case NFA_MARK_GT: | 5384 case NFA_MARK_GT: |
5388 case NFA_MARK_LT: | 5385 case NFA_MARK_LT: |
5389 case NFA_VISUAL: | 5386 case NFA_VISUAL: |
5390 /* before/after positions don't match very often */ | 5387 // before/after positions don't match very often |
5391 return 85; | 5388 return 85; |
5392 | 5389 |
5393 case NFA_LNUM: | 5390 case NFA_LNUM: |
5394 return 90; | 5391 return 90; |
5395 | 5392 |
5396 case NFA_CURSOR: | 5393 case NFA_CURSOR: |
5397 case NFA_COL: | 5394 case NFA_COL: |
5398 case NFA_VCOL: | 5395 case NFA_VCOL: |
5399 case NFA_MARK: | 5396 case NFA_MARK: |
5400 /* specific positions rarely match */ | 5397 // specific positions rarely match |
5401 return 98; | 5398 return 98; |
5402 | 5399 |
5403 case NFA_COMPOSING: | 5400 case NFA_COMPOSING: |
5404 return 95; | 5401 return 95; |
5405 | 5402 |
5406 default: | 5403 default: |
5407 if (c > 0) | 5404 if (c > 0) |
5408 /* character match fails often */ | 5405 // character match fails often |
5409 return 95; | 5406 return 95; |
5410 } | 5407 } |
5411 | 5408 |
5412 /* something else, includes character classes */ | 5409 // something else, includes character classes |
5413 return 50; | 5410 return 50; |
5414 } | 5411 } |
5415 | 5412 |
5416 /* | 5413 /* |
5417 * Skip until the char "c" we know a match must start with. | 5414 * Skip until the char "c" we know a match must start with. |
5419 static int | 5416 static int |
5420 skip_to_start(int c, colnr_T *colp) | 5417 skip_to_start(int c, colnr_T *colp) |
5421 { | 5418 { |
5422 char_u *s; | 5419 char_u *s; |
5423 | 5420 |
5424 /* Used often, do some work to avoid call overhead. */ | 5421 // Used often, do some work to avoid call overhead. |
5425 if (!rex.reg_ic && !has_mbyte) | 5422 if (!rex.reg_ic && !has_mbyte) |
5426 s = vim_strbyte(rex.line + *colp, c); | 5423 s = vim_strbyte(rex.line + *colp, c); |
5427 else | 5424 else |
5428 s = cstrchr(rex.line + *colp, c); | 5425 s = cstrchr(rex.line + *colp, c); |
5429 if (s == NULL) | 5426 if (s == NULL) |
5446 int match; | 5443 int match; |
5447 | 5444 |
5448 for (;;) | 5445 for (;;) |
5449 { | 5446 { |
5450 match = TRUE; | 5447 match = TRUE; |
5451 len2 = MB_CHAR2LEN(regstart); /* skip regstart */ | 5448 len2 = MB_CHAR2LEN(regstart); // skip regstart |
5452 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) | 5449 for (len1 = 0; match_text[len1] != NUL; len1 += MB_CHAR2LEN(c1)) |
5453 { | 5450 { |
5454 c1 = PTR2CHAR(match_text + len1); | 5451 c1 = PTR2CHAR(match_text + len1); |
5455 c2 = PTR2CHAR(rex.line + col + len2); | 5452 c2 = PTR2CHAR(rex.line + col + len2); |
5456 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) | 5453 if (c1 != c2 && (!rex.reg_ic || MB_TOLOWER(c1) != MB_TOLOWER(c2))) |
5459 break; | 5456 break; |
5460 } | 5457 } |
5461 len2 += MB_CHAR2LEN(c2); | 5458 len2 += MB_CHAR2LEN(c2); |
5462 } | 5459 } |
5463 if (match | 5460 if (match |
5464 /* check that no composing char follows */ | 5461 // check that no composing char follows |
5465 && !(enc_utf8 | 5462 && !(enc_utf8 |
5466 && utf_iscomposing(PTR2CHAR(rex.line + col + len2)))) | 5463 && utf_iscomposing(PTR2CHAR(rex.line + col + len2)))) |
5467 { | 5464 { |
5468 cleanup_subexpr(); | 5465 cleanup_subexpr(); |
5469 if (REG_MULTI) | 5466 if (REG_MULTI) |
5479 rex.reg_endp[0] = rex.line + col + len2; | 5476 rex.reg_endp[0] = rex.line + col + len2; |
5480 } | 5477 } |
5481 return 1L; | 5478 return 1L; |
5482 } | 5479 } |
5483 | 5480 |
5484 /* Try finding regstart after the current match. */ | 5481 // Try finding regstart after the current match. |
5485 col += MB_CHAR2LEN(regstart); /* skip regstart */ | 5482 col += MB_CHAR2LEN(regstart); // skip regstart |
5486 if (skip_to_start(regstart, &col) == FAIL) | 5483 if (skip_to_start(regstart, &col) == FAIL) |
5487 break; | 5484 break; |
5488 } | 5485 } |
5489 return 0L; | 5486 return 0L; |
5490 } | 5487 } |
5542 regsubs_T *r; | 5539 regsubs_T *r; |
5543 #ifdef NFA_REGEXP_DEBUG_LOG | 5540 #ifdef NFA_REGEXP_DEBUG_LOG |
5544 FILE *debug; | 5541 FILE *debug; |
5545 #endif | 5542 #endif |
5546 | 5543 |
5547 /* Some patterns may take a long time to match, especially when using | 5544 // Some patterns may take a long time to match, especially when using |
5548 * recursive_regmatch(). Allow interrupting them with CTRL-C. */ | 5545 // recursive_regmatch(). Allow interrupting them with CTRL-C. |
5549 fast_breakcheck(); | 5546 fast_breakcheck(); |
5550 if (got_int) | 5547 if (got_int) |
5551 return FALSE; | 5548 return FALSE; |
5552 #ifdef FEAT_RELTIME | 5549 #ifdef FEAT_RELTIME |
5553 if (nfa_did_time_out()) | 5550 if (nfa_did_time_out()) |
5562 return FALSE; | 5559 return FALSE; |
5563 } | 5560 } |
5564 #endif | 5561 #endif |
5565 nfa_match = FALSE; | 5562 nfa_match = FALSE; |
5566 | 5563 |
5567 /* Allocate memory for the lists of nodes. */ | 5564 // Allocate memory for the lists of nodes. |
5568 size = (prog->nstate + 1) * sizeof(nfa_thread_T); | 5565 size = (prog->nstate + 1) * sizeof(nfa_thread_T); |
5569 | 5566 |
5570 list[0].t = alloc(size); | 5567 list[0].t = alloc(size); |
5571 list[0].len = prog->nstate + 1; | 5568 list[0].len = prog->nstate + 1; |
5572 list[1].t = alloc(size); | 5569 list[1].t = alloc(size); |
5600 #ifdef ENABLE_LOG | 5597 #ifdef ENABLE_LOG |
5601 fprintf(log_fd, "(---) STARTSTATE first\n"); | 5598 fprintf(log_fd, "(---) STARTSTATE first\n"); |
5602 #endif | 5599 #endif |
5603 thislist->id = rex.nfa_listid + 1; | 5600 thislist->id = rex.nfa_listid + 1; |
5604 | 5601 |
5605 /* Inline optimized code for addstate(thislist, start, m, 0) if we know | 5602 // Inline optimized code for addstate(thislist, start, m, 0) if we know |
5606 * it's the first MOPEN. */ | 5603 // it's the first MOPEN. |
5607 if (toplevel) | 5604 if (toplevel) |
5608 { | 5605 { |
5609 if (REG_MULTI) | 5606 if (REG_MULTI) |
5610 { | 5607 { |
5611 m->norm.list.multi[0].start_lnum = rex.lnum; | 5608 m->norm.list.multi[0].start_lnum = rex.lnum; |
5652 { | 5649 { |
5653 clen = 0; | 5650 clen = 0; |
5654 go_to_nextline = FALSE; | 5651 go_to_nextline = FALSE; |
5655 } | 5652 } |
5656 | 5653 |
5657 /* swap lists */ | 5654 // swap lists |
5658 thislist = &list[flag]; | 5655 thislist = &list[flag]; |
5659 nextlist = &list[flag ^= 1]; | 5656 nextlist = &list[flag ^= 1]; |
5660 nextlist->n = 0; /* clear nextlist */ | 5657 nextlist->n = 0; // clear nextlist |
5661 nextlist->has_pim = FALSE; | 5658 nextlist->has_pim = FALSE; |
5662 ++rex.nfa_listid; | 5659 ++rex.nfa_listid; |
5663 if (prog->re_engine == AUTOMATIC_ENGINE | 5660 if (prog->re_engine == AUTOMATIC_ENGINE |
5664 && (rex.nfa_listid >= NFA_MAX_STATES | 5661 && (rex.nfa_listid >= NFA_MAX_STATES |
5665 # ifdef FEAT_EVAL | 5662 # ifdef FEAT_EVAL |
5666 || nfa_fail_for_testing | 5663 || nfa_fail_for_testing |
5667 # endif | 5664 # endif |
5668 )) | 5665 )) |
5669 { | 5666 { |
5670 /* too many states, retry with old engine */ | 5667 // too many states, retry with old engine |
5671 nfa_match = NFA_TOO_EXPENSIVE; | 5668 nfa_match = NFA_TOO_EXPENSIVE; |
5672 goto theend; | 5669 goto theend; |
5673 } | 5670 } |
5674 | 5671 |
5675 thislist->id = rex.nfa_listid; | 5672 thislist->id = rex.nfa_listid; |
5696 * If the state lists are empty we can stop. | 5693 * If the state lists are empty we can stop. |
5697 */ | 5694 */ |
5698 if (thislist->n == 0) | 5695 if (thislist->n == 0) |
5699 break; | 5696 break; |
5700 | 5697 |
5701 /* compute nextlist */ | 5698 // compute nextlist |
5702 for (listidx = 0; listidx < thislist->n; ++listidx) | 5699 for (listidx = 0; listidx < thislist->n; ++listidx) |
5703 { | 5700 { |
5704 /* If the list gets very long there probably is something wrong. | 5701 // If the list gets very long there probably is something wrong. |
5705 * At least allow interrupting with CTRL-C. */ | 5702 // At least allow interrupting with CTRL-C. |
5706 fast_breakcheck(); | 5703 fast_breakcheck(); |
5707 if (got_int) | 5704 if (got_int) |
5708 break; | 5705 break; |
5709 #ifdef FEAT_RELTIME | 5706 #ifdef FEAT_RELTIME |
5710 if (nfa_time_limit != NULL && ++nfa_time_count == 20) | 5707 if (nfa_time_limit != NULL && ++nfa_time_count == 20) |
5746 add_count = 0; | 5743 add_count = 0; |
5747 switch (t->state->c) | 5744 switch (t->state->c) |
5748 { | 5745 { |
5749 case NFA_MATCH: | 5746 case NFA_MATCH: |
5750 { | 5747 { |
5751 /* If the match ends before a composing characters and | 5748 // If the match ends before a composing characters and |
5752 * rex.reg_icombine is not set, that is not really a match. */ | 5749 // rex.reg_icombine is not set, that is not really a match. |
5753 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc)) | 5750 if (enc_utf8 && !rex.reg_icombine && utf_iscomposing(curc)) |
5754 break; | 5751 break; |
5755 | 5752 |
5756 nfa_match = TRUE; | 5753 nfa_match = TRUE; |
5757 copy_sub(&submatch->norm, &t->subs.norm); | 5754 copy_sub(&submatch->norm, &t->subs.norm); |
5760 copy_sub(&submatch->synt, &t->subs.synt); | 5757 copy_sub(&submatch->synt, &t->subs.synt); |
5761 #endif | 5758 #endif |
5762 #ifdef ENABLE_LOG | 5759 #ifdef ENABLE_LOG |
5763 log_subsexpr(&t->subs); | 5760 log_subsexpr(&t->subs); |
5764 #endif | 5761 #endif |
5765 /* Found the left-most longest match, do not look at any other | 5762 // Found the left-most longest match, do not look at any other |
5766 * states at this position. When the list of states is going | 5763 // states at this position. When the list of states is going |
5767 * to be empty quit without advancing, so that "rex.input" is | 5764 // to be empty quit without advancing, so that "rex.input" is |
5768 * correct. */ | 5765 // correct. |
5769 if (nextlist->n == 0) | 5766 if (nextlist->n == 0) |
5770 clen = 0; | 5767 clen = 0; |
5771 goto nextchar; | 5768 goto nextchar; |
5772 } | 5769 } |
5773 | 5770 |
5798 fprintf(log_fd, "Current col: %d, endp col: %d\n", | 5795 fprintf(log_fd, "Current col: %d, endp col: %d\n", |
5799 (int)(rex.input - rex.line), | 5796 (int)(rex.input - rex.line), |
5800 (int)(nfa_endp->se_u.ptr - rex.input)); | 5797 (int)(nfa_endp->se_u.ptr - rex.input)); |
5801 } | 5798 } |
5802 #endif | 5799 #endif |
5803 /* If "nfa_endp" is set it's only a match if it ends at | 5800 // If "nfa_endp" is set it's only a match if it ends at |
5804 * "nfa_endp" */ | 5801 // "nfa_endp" |
5805 if (nfa_endp != NULL && (REG_MULTI | 5802 if (nfa_endp != NULL && (REG_MULTI |
5806 ? (rex.lnum != nfa_endp->se_u.pos.lnum | 5803 ? (rex.lnum != nfa_endp->se_u.pos.lnum |
5807 || (int)(rex.input - rex.line) | 5804 || (int)(rex.input - rex.line) |
5808 != nfa_endp->se_u.pos.col) | 5805 != nfa_endp->se_u.pos.col) |
5809 : rex.input != nfa_endp->se_u.ptr)) | 5806 : rex.input != nfa_endp->se_u.ptr)) |
5810 break; | 5807 break; |
5811 | 5808 |
5812 /* do not set submatches for \@! */ | 5809 // do not set submatches for \@! |
5813 if (t->state->c != NFA_END_INVISIBLE_NEG) | 5810 if (t->state->c != NFA_END_INVISIBLE_NEG) |
5814 { | 5811 { |
5815 copy_sub(&m->norm, &t->subs.norm); | 5812 copy_sub(&m->norm, &t->subs.norm); |
5816 #ifdef FEAT_SYN_HL | 5813 #ifdef FEAT_SYN_HL |
5817 if (rex.nfa_has_zsubexpr) | 5814 if (rex.nfa_has_zsubexpr) |
5821 #ifdef ENABLE_LOG | 5818 #ifdef ENABLE_LOG |
5822 fprintf(log_fd, "Match found:\n"); | 5819 fprintf(log_fd, "Match found:\n"); |
5823 log_subsexpr(m); | 5820 log_subsexpr(m); |
5824 #endif | 5821 #endif |
5825 nfa_match = TRUE; | 5822 nfa_match = TRUE; |
5826 /* See comment above at "goto nextchar". */ | 5823 // See comment above at "goto nextchar". |
5827 if (nextlist->n == 0) | 5824 if (nextlist->n == 0) |
5828 clen = 0; | 5825 clen = 0; |
5829 goto nextchar; | 5826 goto nextchar; |
5830 | 5827 |
5831 case NFA_START_INVISIBLE: | 5828 case NFA_START_INVISIBLE: |
5840 #ifdef ENABLE_LOG | 5837 #ifdef ENABLE_LOG |
5841 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", | 5838 fprintf(log_fd, "Failure chance invisible: %d, what follows: %d\n", |
5842 failure_chance(t->state->out, 0), | 5839 failure_chance(t->state->out, 0), |
5843 failure_chance(t->state->out1->out, 0)); | 5840 failure_chance(t->state->out1->out, 0)); |
5844 #endif | 5841 #endif |
5845 /* Do it directly if there already is a PIM or when | 5842 // Do it directly if there already is a PIM or when |
5846 * nfa_postprocess() detected it will work better. */ | 5843 // nfa_postprocess() detected it will work better. |
5847 if (t->pim.result != NFA_PIM_UNUSED | 5844 if (t->pim.result != NFA_PIM_UNUSED |
5848 || t->state->c == NFA_START_INVISIBLE_FIRST | 5845 || t->state->c == NFA_START_INVISIBLE_FIRST |
5849 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST | 5846 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
5850 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST | 5847 || t->state->c == NFA_START_INVISIBLE_BEFORE_FIRST |
5851 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) | 5848 || t->state->c == NFA_START_INVISIBLE_BEFORE_NEG_FIRST) |
5852 { | 5849 { |
5853 int in_use = m->norm.in_use; | 5850 int in_use = m->norm.in_use; |
5854 | 5851 |
5855 /* Copy submatch info for the recursive call, opposite | 5852 // Copy submatch info for the recursive call, opposite |
5856 * of what happens on success below. */ | 5853 // of what happens on success below. |
5857 copy_sub_off(&m->norm, &t->subs.norm); | 5854 copy_sub_off(&m->norm, &t->subs.norm); |
5858 #ifdef FEAT_SYN_HL | 5855 #ifdef FEAT_SYN_HL |
5859 if (rex.nfa_has_zsubexpr) | 5856 if (rex.nfa_has_zsubexpr) |
5860 copy_sub_off(&m->synt, &t->subs.synt); | 5857 copy_sub_off(&m->synt, &t->subs.synt); |
5861 #endif | 5858 #endif |
5870 { | 5867 { |
5871 nfa_match = result; | 5868 nfa_match = result; |
5872 goto theend; | 5869 goto theend; |
5873 } | 5870 } |
5874 | 5871 |
5875 /* for \@! and \@<! it is a match when the result is | 5872 // for \@! and \@<! it is a match when the result is |
5876 * FALSE */ | 5873 // FALSE |
5877 if (result != (t->state->c == NFA_START_INVISIBLE_NEG | 5874 if (result != (t->state->c == NFA_START_INVISIBLE_NEG |
5878 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST | 5875 || t->state->c == NFA_START_INVISIBLE_NEG_FIRST |
5879 || t->state->c | 5876 || t->state->c |
5880 == NFA_START_INVISIBLE_BEFORE_NEG | 5877 == NFA_START_INVISIBLE_BEFORE_NEG |
5881 || t->state->c | 5878 || t->state->c |
5882 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) | 5879 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
5883 { | 5880 { |
5884 /* Copy submatch info from the recursive call */ | 5881 // Copy submatch info from the recursive call |
5885 copy_sub_off(&t->subs.norm, &m->norm); | 5882 copy_sub_off(&t->subs.norm, &m->norm); |
5886 #ifdef FEAT_SYN_HL | 5883 #ifdef FEAT_SYN_HL |
5887 if (rex.nfa_has_zsubexpr) | 5884 if (rex.nfa_has_zsubexpr) |
5888 copy_sub_off(&t->subs.synt, &m->synt); | 5885 copy_sub_off(&t->subs.synt, &m->synt); |
5889 #endif | 5886 #endif |
5890 /* If the pattern has \ze and it matched in the | 5887 // If the pattern has \ze and it matched in the |
5891 * sub pattern, use it. */ | 5888 // sub pattern, use it. |
5892 copy_ze_off(&t->subs.norm, &m->norm); | 5889 copy_ze_off(&t->subs.norm, &m->norm); |
5893 | 5890 |
5894 /* t->state->out1 is the corresponding | 5891 // t->state->out1 is the corresponding |
5895 * END_INVISIBLE node; Add its out to the current | 5892 // END_INVISIBLE node; Add its out to the current |
5896 * list (zero-width match). */ | 5893 // list (zero-width match). |
5897 add_here = TRUE; | 5894 add_here = TRUE; |
5898 add_state = t->state->out1->out; | 5895 add_state = t->state->out1->out; |
5899 } | 5896 } |
5900 m->norm.in_use = in_use; | 5897 m->norm.in_use = in_use; |
5901 } | 5898 } |
5921 pim.end.pos.lnum = rex.lnum; | 5918 pim.end.pos.lnum = rex.lnum; |
5922 } | 5919 } |
5923 else | 5920 else |
5924 pim.end.ptr = rex.input; | 5921 pim.end.ptr = rex.input; |
5925 | 5922 |
5926 /* t->state->out1 is the corresponding END_INVISIBLE | 5923 // t->state->out1 is the corresponding END_INVISIBLE |
5927 * node; Add its out to the current list (zero-width | 5924 // node; Add its out to the current list (zero-width |
5928 * match). */ | 5925 // match). |
5929 if (addstate_here(thislist, t->state->out1->out, | 5926 if (addstate_here(thislist, t->state->out1->out, |
5930 &t->subs, &pim, &listidx) == NULL) | 5927 &t->subs, &pim, &listidx) == NULL) |
5931 { | 5928 { |
5932 nfa_match = NFA_TOO_EXPENSIVE; | 5929 nfa_match = NFA_TOO_EXPENSIVE; |
5933 goto theend; | 5930 goto theend; |
5941 nfa_state_T *skip = NULL; | 5938 nfa_state_T *skip = NULL; |
5942 #ifdef ENABLE_LOG | 5939 #ifdef ENABLE_LOG |
5943 int skip_lid = 0; | 5940 int skip_lid = 0; |
5944 #endif | 5941 #endif |
5945 | 5942 |
5946 /* There is no point in trying to match the pattern if the | 5943 // There is no point in trying to match the pattern if the |
5947 * output state is not going to be added to the list. */ | 5944 // output state is not going to be added to the list. |
5948 if (state_in_list(nextlist, t->state->out1->out, &t->subs)) | 5945 if (state_in_list(nextlist, t->state->out1->out, &t->subs)) |
5949 { | 5946 { |
5950 skip = t->state->out1->out; | 5947 skip = t->state->out1->out; |
5951 #ifdef ENABLE_LOG | 5948 #ifdef ENABLE_LOG |
5952 skip_lid = nextlist->id; | 5949 skip_lid = nextlist->id; |
5975 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", | 5972 fprintf(log_fd, "> Not trying to match pattern, output state %d is already in list %d. char %d: %s\n", |
5976 abs(skip->id), skip_lid, skip->c, code); | 5973 abs(skip->id), skip_lid, skip->c, code); |
5977 #endif | 5974 #endif |
5978 break; | 5975 break; |
5979 } | 5976 } |
5980 /* Copy submatch info to the recursive call, opposite of what | 5977 // Copy submatch info to the recursive call, opposite of what |
5981 * happens afterwards. */ | 5978 // happens afterwards. |
5982 copy_sub_off(&m->norm, &t->subs.norm); | 5979 copy_sub_off(&m->norm, &t->subs.norm); |
5983 #ifdef FEAT_SYN_HL | 5980 #ifdef FEAT_SYN_HL |
5984 if (rex.nfa_has_zsubexpr) | 5981 if (rex.nfa_has_zsubexpr) |
5985 copy_sub_off(&m->synt, &t->subs.synt); | 5982 copy_sub_off(&m->synt, &t->subs.synt); |
5986 #endif | 5983 #endif |
5987 | 5984 |
5988 /* First try matching the pattern. */ | 5985 // First try matching the pattern. |
5989 result = recursive_regmatch(t->state, NULL, prog, | 5986 result = recursive_regmatch(t->state, NULL, prog, |
5990 submatch, m, &listids, &listids_len); | 5987 submatch, m, &listids, &listids_len); |
5991 if (result == NFA_TOO_EXPENSIVE) | 5988 if (result == NFA_TOO_EXPENSIVE) |
5992 { | 5989 { |
5993 nfa_match = result; | 5990 nfa_match = result; |
5999 | 5996 |
6000 #ifdef ENABLE_LOG | 5997 #ifdef ENABLE_LOG |
6001 fprintf(log_fd, "NFA_START_PATTERN matches:\n"); | 5998 fprintf(log_fd, "NFA_START_PATTERN matches:\n"); |
6002 log_subsexpr(m); | 5999 log_subsexpr(m); |
6003 #endif | 6000 #endif |
6004 /* Copy submatch info from the recursive call */ | 6001 // Copy submatch info from the recursive call |
6005 copy_sub_off(&t->subs.norm, &m->norm); | 6002 copy_sub_off(&t->subs.norm, &m->norm); |
6006 #ifdef FEAT_SYN_HL | 6003 #ifdef FEAT_SYN_HL |
6007 if (rex.nfa_has_zsubexpr) | 6004 if (rex.nfa_has_zsubexpr) |
6008 copy_sub_off(&t->subs.synt, &m->synt); | 6005 copy_sub_off(&t->subs.synt, &m->synt); |
6009 #endif | 6006 #endif |
6010 /* Now we need to skip over the matched text and then | 6007 // Now we need to skip over the matched text and then |
6011 * continue with what follows. */ | 6008 // continue with what follows. |
6012 if (REG_MULTI) | 6009 if (REG_MULTI) |
6013 /* TODO: multi-line match */ | 6010 // TODO: multi-line match |
6014 bytelen = m->norm.list.multi[0].end_col | 6011 bytelen = m->norm.list.multi[0].end_col |
6015 - (int)(rex.input - rex.line); | 6012 - (int)(rex.input - rex.line); |
6016 else | 6013 else |
6017 bytelen = (int)(m->norm.list.line[0].end - rex.input); | 6014 bytelen = (int)(m->norm.list.line[0].end - rex.input); |
6018 | 6015 |
6019 #ifdef ENABLE_LOG | 6016 #ifdef ENABLE_LOG |
6020 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); | 6017 fprintf(log_fd, "NFA_START_PATTERN length: %d\n", bytelen); |
6021 #endif | 6018 #endif |
6022 if (bytelen == 0) | 6019 if (bytelen == 0) |
6023 { | 6020 { |
6024 /* empty match, output of corresponding | 6021 // empty match, output of corresponding |
6025 * NFA_END_PATTERN/NFA_SKIP to be used at current | 6022 // NFA_END_PATTERN/NFA_SKIP to be used at current |
6026 * position */ | 6023 // position |
6027 add_here = TRUE; | 6024 add_here = TRUE; |
6028 add_state = t->state->out1->out->out; | 6025 add_state = t->state->out1->out->out; |
6029 } | 6026 } |
6030 else if (bytelen <= clen) | 6027 else if (bytelen <= clen) |
6031 { | 6028 { |
6032 /* match current character, output of corresponding | 6029 // match current character, output of corresponding |
6033 * NFA_END_PATTERN to be used at next position. */ | 6030 // NFA_END_PATTERN to be used at next position. |
6034 add_state = t->state->out1->out->out; | 6031 add_state = t->state->out1->out->out; |
6035 add_off = clen; | 6032 add_off = clen; |
6036 } | 6033 } |
6037 else | 6034 else |
6038 { | 6035 { |
6039 /* skip over the matched characters, set character | 6036 // skip over the matched characters, set character |
6040 * count in NFA_SKIP */ | 6037 // count in NFA_SKIP |
6041 add_state = t->state->out1->out; | 6038 add_state = t->state->out1->out; |
6042 add_off = bytelen; | 6039 add_off = bytelen; |
6043 add_count = bytelen - clen; | 6040 add_count = bytelen - clen; |
6044 } | 6041 } |
6045 } | 6042 } |
6069 result = FALSE; | 6066 result = FALSE; |
6070 else if (has_mbyte) | 6067 else if (has_mbyte) |
6071 { | 6068 { |
6072 int this_class; | 6069 int this_class; |
6073 | 6070 |
6074 /* Get class of current and previous char (if it exists). */ | 6071 // Get class of current and previous char (if it exists). |
6075 this_class = mb_get_class_buf(rex.input, rex.reg_buf); | 6072 this_class = mb_get_class_buf(rex.input, rex.reg_buf); |
6076 if (this_class <= 1) | 6073 if (this_class <= 1) |
6077 result = FALSE; | 6074 result = FALSE; |
6078 else if (reg_prev_class() == this_class) | 6075 else if (reg_prev_class() == this_class) |
6079 result = FALSE; | 6076 result = FALSE; |
6095 result = FALSE; | 6092 result = FALSE; |
6096 else if (has_mbyte) | 6093 else if (has_mbyte) |
6097 { | 6094 { |
6098 int this_class, prev_class; | 6095 int this_class, prev_class; |
6099 | 6096 |
6100 /* Get class of current and previous char (if it exists). */ | 6097 // Get class of current and previous char (if it exists). |
6101 this_class = mb_get_class_buf(rex.input, rex.reg_buf); | 6098 this_class = mb_get_class_buf(rex.input, rex.reg_buf); |
6102 prev_class = reg_prev_class(); | 6099 prev_class = reg_prev_class(); |
6103 if (this_class == prev_class | 6100 if (this_class == prev_class |
6104 || prev_class == 0 || prev_class == 1) | 6101 || prev_class == 0 || prev_class == 1) |
6105 result = FALSE; | 6102 result = FALSE; |
6144 | 6141 |
6145 sta = t->state->out; | 6142 sta = t->state->out; |
6146 len = 0; | 6143 len = 0; |
6147 if (utf_iscomposing(sta->c)) | 6144 if (utf_iscomposing(sta->c)) |
6148 { | 6145 { |
6149 /* Only match composing character(s), ignore base | 6146 // Only match composing character(s), ignore base |
6150 * character. Used for ".{composing}" and "{composing}" | 6147 // character. Used for ".{composing}" and "{composing}" |
6151 * (no preceding character). */ | 6148 // (no preceding character). |
6152 len += mb_char2len(mc); | 6149 len += mb_char2len(mc); |
6153 } | 6150 } |
6154 if (rex.reg_icombine && len == 0) | 6151 if (rex.reg_icombine && len == 0) |
6155 { | 6152 { |
6156 /* If \Z was present, then ignore composing characters. | 6153 // If \Z was present, then ignore composing characters. |
6157 * When ignoring the base character this always matches. */ | 6154 // When ignoring the base character this always matches. |
6158 if (sta->c != curc) | 6155 if (sta->c != curc) |
6159 result = FAIL; | 6156 result = FAIL; |
6160 else | 6157 else |
6161 result = OK; | 6158 result = OK; |
6162 while (sta->c != NFA_END_COMPOSING) | 6159 while (sta->c != NFA_END_COMPOSING) |
6163 sta = sta->out; | 6160 sta = sta->out; |
6164 } | 6161 } |
6165 | 6162 |
6166 /* Check base character matches first, unless ignored. */ | 6163 // Check base character matches first, unless ignored. |
6167 else if (len > 0 || mc == sta->c) | 6164 else if (len > 0 || mc == sta->c) |
6168 { | 6165 { |
6169 if (len == 0) | 6166 if (len == 0) |
6170 { | 6167 { |
6171 len += mb_char2len(mc); | 6168 len += mb_char2len(mc); |
6172 sta = sta->out; | 6169 sta = sta->out; |
6173 } | 6170 } |
6174 | 6171 |
6175 /* We don't care about the order of composing characters. | 6172 // We don't care about the order of composing characters. |
6176 * Get them into cchars[] first. */ | 6173 // Get them into cchars[] first. |
6177 while (len < clen) | 6174 while (len < clen) |
6178 { | 6175 { |
6179 mc = mb_ptr2char(rex.input + len); | 6176 mc = mb_ptr2char(rex.input + len); |
6180 cchars[ccount++] = mc; | 6177 cchars[ccount++] = mc; |
6181 len += mb_char2len(mc); | 6178 len += mb_char2len(mc); |
6182 if (ccount == MAX_MCO) | 6179 if (ccount == MAX_MCO) |
6183 break; | 6180 break; |
6184 } | 6181 } |
6185 | 6182 |
6186 /* Check that each composing char in the pattern matches a | 6183 // Check that each composing char in the pattern matches a |
6187 * composing char in the text. We do not check if all | 6184 // composing char in the text. We do not check if all |
6188 * composing chars are matched. */ | 6185 // composing chars are matched. |
6189 result = OK; | 6186 result = OK; |
6190 while (sta->c != NFA_END_COMPOSING) | 6187 while (sta->c != NFA_END_COMPOSING) |
6191 { | 6188 { |
6192 for (j = 0; j < ccount; ++j) | 6189 for (j = 0; j < ccount; ++j) |
6193 if (cchars[j] == sta->c) | 6190 if (cchars[j] == sta->c) |
6201 } | 6198 } |
6202 } | 6199 } |
6203 else | 6200 else |
6204 result = FAIL; | 6201 result = FAIL; |
6205 | 6202 |
6206 end = t->state->out1; /* NFA_END_COMPOSING */ | 6203 end = t->state->out1; // NFA_END_COMPOSING |
6207 ADD_STATE_IF_MATCH(end); | 6204 ADD_STATE_IF_MATCH(end); |
6208 break; | 6205 break; |
6209 } | 6206 } |
6210 | 6207 |
6211 case NFA_NEWL: | 6208 case NFA_NEWL: |
6212 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI | 6209 if (curc == NUL && !rex.reg_line_lbr && REG_MULTI |
6213 && rex.lnum <= rex.reg_maxline) | 6210 && rex.lnum <= rex.reg_maxline) |
6214 { | 6211 { |
6215 go_to_nextline = TRUE; | 6212 go_to_nextline = TRUE; |
6216 /* Pass -1 for the offset, which means taking the position | 6213 // Pass -1 for the offset, which means taking the position |
6217 * at the start of the next line. */ | 6214 // at the start of the next line. |
6218 add_state = t->state->out; | 6215 add_state = t->state->out; |
6219 add_off = -1; | 6216 add_off = -1; |
6220 } | 6217 } |
6221 else if (curc == '\n' && rex.reg_line_lbr) | 6218 else if (curc == '\n' && rex.reg_line_lbr) |
6222 { | 6219 { |
6223 /* match \n as if it is an ordinary character */ | 6220 // match \n as if it is an ordinary character |
6224 add_state = t->state->out; | 6221 add_state = t->state->out; |
6225 add_off = 1; | 6222 add_off = 1; |
6226 } | 6223 } |
6227 break; | 6224 break; |
6228 | 6225 |
6229 case NFA_START_COLL: | 6226 case NFA_START_COLL: |
6230 case NFA_START_NEG_COLL: | 6227 case NFA_START_NEG_COLL: |
6231 { | 6228 { |
6232 /* What follows is a list of characters, until NFA_END_COLL. | 6229 // What follows is a list of characters, until NFA_END_COLL. |
6233 * One of them must match or none of them must match. */ | 6230 // One of them must match or none of them must match. |
6234 nfa_state_T *state; | 6231 nfa_state_T *state; |
6235 int result_if_matched; | 6232 int result_if_matched; |
6236 int c1, c2; | 6233 int c1, c2; |
6237 | 6234 |
6238 /* Never match EOL. If it's part of the collection it is added | 6235 // Never match EOL. If it's part of the collection it is added |
6239 * as a separate state with an OR. */ | 6236 // as a separate state with an OR. |
6240 if (curc == NUL) | 6237 if (curc == NUL) |
6241 break; | 6238 break; |
6242 | 6239 |
6243 state = t->state->out; | 6240 state = t->state->out; |
6244 result_if_matched = (t->state->c == NFA_START_COLL); | 6241 result_if_matched = (t->state->c == NFA_START_COLL); |
6250 break; | 6247 break; |
6251 } | 6248 } |
6252 if (state->c == NFA_RANGE_MIN) | 6249 if (state->c == NFA_RANGE_MIN) |
6253 { | 6250 { |
6254 c1 = state->val; | 6251 c1 = state->val; |
6255 state = state->out; /* advance to NFA_RANGE_MAX */ | 6252 state = state->out; // advance to NFA_RANGE_MAX |
6256 c2 = state->val; | 6253 c2 = state->val; |
6257 #ifdef ENABLE_LOG | 6254 #ifdef ENABLE_LOG |
6258 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", | 6255 fprintf(log_fd, "NFA_RANGE_MIN curc=%d c1=%d c2=%d\n", |
6259 curc, c1, c2); | 6256 curc, c1, c2); |
6260 #endif | 6257 #endif |
6289 } | 6286 } |
6290 state = state->out; | 6287 state = state->out; |
6291 } | 6288 } |
6292 if (result) | 6289 if (result) |
6293 { | 6290 { |
6294 /* next state is in out of the NFA_END_COLL, out1 of | 6291 // next state is in out of the NFA_END_COLL, out1 of |
6295 * START points to the END state */ | 6292 // START points to the END state |
6296 add_state = t->state->out1->out; | 6293 add_state = t->state->out1->out; |
6297 add_off = clen; | 6294 add_off = clen; |
6298 } | 6295 } |
6299 break; | 6296 break; |
6300 } | 6297 } |
6301 | 6298 |
6302 case NFA_ANY: | 6299 case NFA_ANY: |
6303 /* Any char except '\0', (end of input) does not match. */ | 6300 // Any char except '\0', (end of input) does not match. |
6304 if (curc > 0) | 6301 if (curc > 0) |
6305 { | 6302 { |
6306 add_state = t->state->out; | 6303 add_state = t->state->out; |
6307 add_off = clen; | 6304 add_off = clen; |
6308 } | 6305 } |
6309 break; | 6306 break; |
6310 | 6307 |
6311 case NFA_ANY_COMPOSING: | 6308 case NFA_ANY_COMPOSING: |
6312 /* On a composing character skip over it. Otherwise do | 6309 // On a composing character skip over it. Otherwise do |
6313 * nothing. Always matches. */ | 6310 // nothing. Always matches. |
6314 if (enc_utf8 && utf_iscomposing(curc)) | 6311 if (enc_utf8 && utf_iscomposing(curc)) |
6315 { | 6312 { |
6316 add_off = clen; | 6313 add_off = clen; |
6317 } | 6314 } |
6318 else | 6315 else |
6324 break; | 6321 break; |
6325 | 6322 |
6326 /* | 6323 /* |
6327 * Character classes like \a for alpha, \d for digit etc. | 6324 * Character classes like \a for alpha, \d for digit etc. |
6328 */ | 6325 */ |
6329 case NFA_IDENT: /* \i */ | 6326 case NFA_IDENT: // \i |
6330 result = vim_isIDc(curc); | 6327 result = vim_isIDc(curc); |
6331 ADD_STATE_IF_MATCH(t->state); | 6328 ADD_STATE_IF_MATCH(t->state); |
6332 break; | 6329 break; |
6333 | 6330 |
6334 case NFA_SIDENT: /* \I */ | 6331 case NFA_SIDENT: // \I |
6335 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); | 6332 result = !VIM_ISDIGIT(curc) && vim_isIDc(curc); |
6336 ADD_STATE_IF_MATCH(t->state); | 6333 ADD_STATE_IF_MATCH(t->state); |
6337 break; | 6334 break; |
6338 | 6335 |
6339 case NFA_KWORD: /* \k */ | 6336 case NFA_KWORD: // \k |
6340 result = vim_iswordp_buf(rex.input, rex.reg_buf); | 6337 result = vim_iswordp_buf(rex.input, rex.reg_buf); |
6341 ADD_STATE_IF_MATCH(t->state); | 6338 ADD_STATE_IF_MATCH(t->state); |
6342 break; | 6339 break; |
6343 | 6340 |
6344 case NFA_SKWORD: /* \K */ | 6341 case NFA_SKWORD: // \K |
6345 result = !VIM_ISDIGIT(curc) | 6342 result = !VIM_ISDIGIT(curc) |
6346 && vim_iswordp_buf(rex.input, rex.reg_buf); | 6343 && vim_iswordp_buf(rex.input, rex.reg_buf); |
6347 ADD_STATE_IF_MATCH(t->state); | 6344 ADD_STATE_IF_MATCH(t->state); |
6348 break; | 6345 break; |
6349 | 6346 |
6350 case NFA_FNAME: /* \f */ | 6347 case NFA_FNAME: // \f |
6351 result = vim_isfilec(curc); | 6348 result = vim_isfilec(curc); |
6352 ADD_STATE_IF_MATCH(t->state); | 6349 ADD_STATE_IF_MATCH(t->state); |
6353 break; | 6350 break; |
6354 | 6351 |
6355 case NFA_SFNAME: /* \F */ | 6352 case NFA_SFNAME: // \F |
6356 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); | 6353 result = !VIM_ISDIGIT(curc) && vim_isfilec(curc); |
6357 ADD_STATE_IF_MATCH(t->state); | 6354 ADD_STATE_IF_MATCH(t->state); |
6358 break; | 6355 break; |
6359 | 6356 |
6360 case NFA_PRINT: /* \p */ | 6357 case NFA_PRINT: // \p |
6361 result = vim_isprintc(PTR2CHAR(rex.input)); | 6358 result = vim_isprintc(PTR2CHAR(rex.input)); |
6362 ADD_STATE_IF_MATCH(t->state); | 6359 ADD_STATE_IF_MATCH(t->state); |
6363 break; | 6360 break; |
6364 | 6361 |
6365 case NFA_SPRINT: /* \P */ | 6362 case NFA_SPRINT: // \P |
6366 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input)); | 6363 result = !VIM_ISDIGIT(curc) && vim_isprintc(PTR2CHAR(rex.input)); |
6367 ADD_STATE_IF_MATCH(t->state); | 6364 ADD_STATE_IF_MATCH(t->state); |
6368 break; | 6365 break; |
6369 | 6366 |
6370 case NFA_WHITE: /* \s */ | 6367 case NFA_WHITE: // \s |
6371 result = VIM_ISWHITE(curc); | 6368 result = VIM_ISWHITE(curc); |
6372 ADD_STATE_IF_MATCH(t->state); | 6369 ADD_STATE_IF_MATCH(t->state); |
6373 break; | 6370 break; |
6374 | 6371 |
6375 case NFA_NWHITE: /* \S */ | 6372 case NFA_NWHITE: // \S |
6376 result = curc != NUL && !VIM_ISWHITE(curc); | 6373 result = curc != NUL && !VIM_ISWHITE(curc); |
6377 ADD_STATE_IF_MATCH(t->state); | 6374 ADD_STATE_IF_MATCH(t->state); |
6378 break; | 6375 break; |
6379 | 6376 |
6380 case NFA_DIGIT: /* \d */ | 6377 case NFA_DIGIT: // \d |
6381 result = ri_digit(curc); | 6378 result = ri_digit(curc); |
6382 ADD_STATE_IF_MATCH(t->state); | 6379 ADD_STATE_IF_MATCH(t->state); |
6383 break; | 6380 break; |
6384 | 6381 |
6385 case NFA_NDIGIT: /* \D */ | 6382 case NFA_NDIGIT: // \D |
6386 result = curc != NUL && !ri_digit(curc); | 6383 result = curc != NUL && !ri_digit(curc); |
6387 ADD_STATE_IF_MATCH(t->state); | 6384 ADD_STATE_IF_MATCH(t->state); |
6388 break; | 6385 break; |
6389 | 6386 |
6390 case NFA_HEX: /* \x */ | 6387 case NFA_HEX: // \x |
6391 result = ri_hex(curc); | 6388 result = ri_hex(curc); |
6392 ADD_STATE_IF_MATCH(t->state); | 6389 ADD_STATE_IF_MATCH(t->state); |
6393 break; | 6390 break; |
6394 | 6391 |
6395 case NFA_NHEX: /* \X */ | 6392 case NFA_NHEX: // \X |
6396 result = curc != NUL && !ri_hex(curc); | 6393 result = curc != NUL && !ri_hex(curc); |
6397 ADD_STATE_IF_MATCH(t->state); | 6394 ADD_STATE_IF_MATCH(t->state); |
6398 break; | 6395 break; |
6399 | 6396 |
6400 case NFA_OCTAL: /* \o */ | 6397 case NFA_OCTAL: // \o |
6401 result = ri_octal(curc); | 6398 result = ri_octal(curc); |
6402 ADD_STATE_IF_MATCH(t->state); | 6399 ADD_STATE_IF_MATCH(t->state); |
6403 break; | 6400 break; |
6404 | 6401 |
6405 case NFA_NOCTAL: /* \O */ | 6402 case NFA_NOCTAL: // \O |
6406 result = curc != NUL && !ri_octal(curc); | 6403 result = curc != NUL && !ri_octal(curc); |
6407 ADD_STATE_IF_MATCH(t->state); | 6404 ADD_STATE_IF_MATCH(t->state); |
6408 break; | 6405 break; |
6409 | 6406 |
6410 case NFA_WORD: /* \w */ | 6407 case NFA_WORD: // \w |
6411 result = ri_word(curc); | 6408 result = ri_word(curc); |
6412 ADD_STATE_IF_MATCH(t->state); | 6409 ADD_STATE_IF_MATCH(t->state); |
6413 break; | 6410 break; |
6414 | 6411 |
6415 case NFA_NWORD: /* \W */ | 6412 case NFA_NWORD: // \W |
6416 result = curc != NUL && !ri_word(curc); | 6413 result = curc != NUL && !ri_word(curc); |
6417 ADD_STATE_IF_MATCH(t->state); | 6414 ADD_STATE_IF_MATCH(t->state); |
6418 break; | 6415 break; |
6419 | 6416 |
6420 case NFA_HEAD: /* \h */ | 6417 case NFA_HEAD: // \h |
6421 result = ri_head(curc); | 6418 result = ri_head(curc); |
6422 ADD_STATE_IF_MATCH(t->state); | 6419 ADD_STATE_IF_MATCH(t->state); |
6423 break; | 6420 break; |
6424 | 6421 |
6425 case NFA_NHEAD: /* \H */ | 6422 case NFA_NHEAD: // \H |
6426 result = curc != NUL && !ri_head(curc); | 6423 result = curc != NUL && !ri_head(curc); |
6427 ADD_STATE_IF_MATCH(t->state); | 6424 ADD_STATE_IF_MATCH(t->state); |
6428 break; | 6425 break; |
6429 | 6426 |
6430 case NFA_ALPHA: /* \a */ | 6427 case NFA_ALPHA: // \a |
6431 result = ri_alpha(curc); | 6428 result = ri_alpha(curc); |
6432 ADD_STATE_IF_MATCH(t->state); | 6429 ADD_STATE_IF_MATCH(t->state); |
6433 break; | 6430 break; |
6434 | 6431 |
6435 case NFA_NALPHA: /* \A */ | 6432 case NFA_NALPHA: // \A |
6436 result = curc != NUL && !ri_alpha(curc); | 6433 result = curc != NUL && !ri_alpha(curc); |
6437 ADD_STATE_IF_MATCH(t->state); | 6434 ADD_STATE_IF_MATCH(t->state); |
6438 break; | 6435 break; |
6439 | 6436 |
6440 case NFA_LOWER: /* \l */ | 6437 case NFA_LOWER: // \l |
6441 result = ri_lower(curc); | 6438 result = ri_lower(curc); |
6442 ADD_STATE_IF_MATCH(t->state); | 6439 ADD_STATE_IF_MATCH(t->state); |
6443 break; | 6440 break; |
6444 | 6441 |
6445 case NFA_NLOWER: /* \L */ | 6442 case NFA_NLOWER: // \L |
6446 result = curc != NUL && !ri_lower(curc); | 6443 result = curc != NUL && !ri_lower(curc); |
6447 ADD_STATE_IF_MATCH(t->state); | 6444 ADD_STATE_IF_MATCH(t->state); |
6448 break; | 6445 break; |
6449 | 6446 |
6450 case NFA_UPPER: /* \u */ | 6447 case NFA_UPPER: // \u |
6451 result = ri_upper(curc); | 6448 result = ri_upper(curc); |
6452 ADD_STATE_IF_MATCH(t->state); | 6449 ADD_STATE_IF_MATCH(t->state); |
6453 break; | 6450 break; |
6454 | 6451 |
6455 case NFA_NUPPER: /* \U */ | 6452 case NFA_NUPPER: // \U |
6456 result = curc != NUL && !ri_upper(curc); | 6453 result = curc != NUL && !ri_upper(curc); |
6457 ADD_STATE_IF_MATCH(t->state); | 6454 ADD_STATE_IF_MATCH(t->state); |
6458 break; | 6455 break; |
6459 | 6456 |
6460 case NFA_LOWER_IC: /* [a-z] */ | 6457 case NFA_LOWER_IC: // [a-z] |
6461 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc)); | 6458 result = ri_lower(curc) || (rex.reg_ic && ri_upper(curc)); |
6462 ADD_STATE_IF_MATCH(t->state); | 6459 ADD_STATE_IF_MATCH(t->state); |
6463 break; | 6460 break; |
6464 | 6461 |
6465 case NFA_NLOWER_IC: /* [^a-z] */ | 6462 case NFA_NLOWER_IC: // [^a-z] |
6466 result = curc != NUL | 6463 result = curc != NUL |
6467 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc))); | 6464 && !(ri_lower(curc) || (rex.reg_ic && ri_upper(curc))); |
6468 ADD_STATE_IF_MATCH(t->state); | 6465 ADD_STATE_IF_MATCH(t->state); |
6469 break; | 6466 break; |
6470 | 6467 |
6471 case NFA_UPPER_IC: /* [A-Z] */ | 6468 case NFA_UPPER_IC: // [A-Z] |
6472 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc)); | 6469 result = ri_upper(curc) || (rex.reg_ic && ri_lower(curc)); |
6473 ADD_STATE_IF_MATCH(t->state); | 6470 ADD_STATE_IF_MATCH(t->state); |
6474 break; | 6471 break; |
6475 | 6472 |
6476 case NFA_NUPPER_IC: /* ^[A-Z] */ | 6473 case NFA_NUPPER_IC: // ^[A-Z] |
6477 result = curc != NUL | 6474 result = curc != NUL |
6478 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc))); | 6475 && !(ri_upper(curc) || (rex.reg_ic && ri_lower(curc))); |
6479 ADD_STATE_IF_MATCH(t->state); | 6476 ADD_STATE_IF_MATCH(t->state); |
6480 break; | 6477 break; |
6481 | 6478 |
6497 case NFA_ZREF6: | 6494 case NFA_ZREF6: |
6498 case NFA_ZREF7: | 6495 case NFA_ZREF7: |
6499 case NFA_ZREF8: | 6496 case NFA_ZREF8: |
6500 case NFA_ZREF9: | 6497 case NFA_ZREF9: |
6501 #endif | 6498 #endif |
6502 /* \1 .. \9 \z1 .. \z9 */ | 6499 // \1 .. \9 \z1 .. \z9 |
6503 { | 6500 { |
6504 int subidx; | 6501 int subidx; |
6505 int bytelen; | 6502 int bytelen; |
6506 | 6503 |
6507 if (t->state->c <= NFA_BACKREF9) | 6504 if (t->state->c <= NFA_BACKREF9) |
6519 | 6516 |
6520 if (result) | 6517 if (result) |
6521 { | 6518 { |
6522 if (bytelen == 0) | 6519 if (bytelen == 0) |
6523 { | 6520 { |
6524 /* empty match always works, output of NFA_SKIP to be | 6521 // empty match always works, output of NFA_SKIP to be |
6525 * used next */ | 6522 // used next |
6526 add_here = TRUE; | 6523 add_here = TRUE; |
6527 add_state = t->state->out->out; | 6524 add_state = t->state->out->out; |
6528 } | 6525 } |
6529 else if (bytelen <= clen) | 6526 else if (bytelen <= clen) |
6530 { | 6527 { |
6531 /* match current character, jump ahead to out of | 6528 // match current character, jump ahead to out of |
6532 * NFA_SKIP */ | 6529 // NFA_SKIP |
6533 add_state = t->state->out->out; | 6530 add_state = t->state->out->out; |
6534 add_off = clen; | 6531 add_off = clen; |
6535 } | 6532 } |
6536 else | 6533 else |
6537 { | 6534 { |
6538 /* skip over the matched characters, set character | 6535 // skip over the matched characters, set character |
6539 * count in NFA_SKIP */ | 6536 // count in NFA_SKIP |
6540 add_state = t->state->out; | 6537 add_state = t->state->out; |
6541 add_off = bytelen; | 6538 add_off = bytelen; |
6542 add_count = bytelen - clen; | 6539 add_count = bytelen - clen; |
6543 } | 6540 } |
6544 } | 6541 } |
6545 break; | 6542 break; |
6546 } | 6543 } |
6547 case NFA_SKIP: | 6544 case NFA_SKIP: |
6548 /* character of previous matching \1 .. \9 or \@> */ | 6545 // character of previous matching \1 .. \9 or \@> |
6549 if (t->count - clen <= 0) | 6546 if (t->count - clen <= 0) |
6550 { | 6547 { |
6551 /* end of match, go to what follows */ | 6548 // end of match, go to what follows |
6552 add_state = t->state->out; | 6549 add_state = t->state->out; |
6553 add_off = clen; | 6550 add_off = clen; |
6554 } | 6551 } |
6555 else | 6552 else |
6556 { | 6553 { |
6557 /* add state again with decremented count */ | 6554 // add state again with decremented count |
6558 add_state = t->state; | 6555 add_state = t->state; |
6559 add_off = 0; | 6556 add_off = 0; |
6560 add_count = t->count - clen; | 6557 add_count = t->count - clen; |
6561 } | 6558 } |
6562 break; | 6559 break; |
6592 { | 6589 { |
6593 int op = t->state->c - NFA_VCOL; | 6590 int op = t->state->c - NFA_VCOL; |
6594 colnr_T col = (colnr_T)(rex.input - rex.line); | 6591 colnr_T col = (colnr_T)(rex.input - rex.line); |
6595 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win; | 6592 win_T *wp = rex.reg_win == NULL ? curwin : rex.reg_win; |
6596 | 6593 |
6597 /* Bail out quickly when there can't be a match, avoid the | 6594 // Bail out quickly when there can't be a match, avoid the |
6598 * overhead of win_linetabsize() on long lines. */ | 6595 // overhead of win_linetabsize() on long lines. |
6599 if (op != 1 && col > t->state->val | 6596 if (op != 1 && col > t->state->val |
6600 * (has_mbyte ? MB_MAXBYTES : 1)) | 6597 * (has_mbyte ? MB_MAXBYTES : 1)) |
6601 break; | 6598 break; |
6602 result = FALSE; | 6599 result = FALSE; |
6603 if (op == 1 && col - 1 > t->state->val && col > 100) | 6600 if (op == 1 && col - 1 > t->state->val && col > 100) |
6604 { | 6601 { |
6605 int ts = wp->w_buffer->b_p_ts; | 6602 int ts = wp->w_buffer->b_p_ts; |
6606 | 6603 |
6607 /* Guess that a character won't use more columns than | 6604 // Guess that a character won't use more columns than |
6608 * 'tabstop', with a minimum of 4. */ | 6605 // 'tabstop', with a minimum of 4. |
6609 if (ts < 4) | 6606 if (ts < 4) |
6610 ts = 4; | 6607 ts = 4; |
6611 result = col > t->state->val * ts; | 6608 result = col > t->state->val * ts; |
6612 } | 6609 } |
6613 if (!result) | 6610 if (!result) |
6625 case NFA_MARK_GT: | 6622 case NFA_MARK_GT: |
6626 case NFA_MARK_LT: | 6623 case NFA_MARK_LT: |
6627 { | 6624 { |
6628 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE); | 6625 pos_T *pos = getmark_buf(rex.reg_buf, t->state->val, FALSE); |
6629 | 6626 |
6630 /* Compare the mark position to the match position. */ | 6627 // Compare the mark position to the match position. |
6631 result = (pos != NULL /* mark doesn't exist */ | 6628 result = (pos != NULL // mark doesn't exist |
6632 && pos->lnum > 0 /* mark isn't set in reg_buf */ | 6629 && pos->lnum > 0 // mark isn't set in reg_buf |
6633 && (pos->lnum == rex.lnum + rex.reg_firstlnum | 6630 && (pos->lnum == rex.lnum + rex.reg_firstlnum |
6634 ? (pos->col == (colnr_T)(rex.input - rex.line) | 6631 ? (pos->col == (colnr_T)(rex.input - rex.line) |
6635 ? t->state->c == NFA_MARK | 6632 ? t->state->c == NFA_MARK |
6636 : (pos->col < (colnr_T)(rex.input - rex.line) | 6633 : (pos->col < (colnr_T)(rex.input - rex.line) |
6637 ? t->state->c == NFA_MARK_GT | 6634 ? t->state->c == NFA_MARK_GT |
6690 case NFA_ZOPEN8: | 6687 case NFA_ZOPEN8: |
6691 case NFA_ZOPEN9: | 6688 case NFA_ZOPEN9: |
6692 #endif | 6689 #endif |
6693 case NFA_NOPEN: | 6690 case NFA_NOPEN: |
6694 case NFA_ZSTART: | 6691 case NFA_ZSTART: |
6695 /* These states are only added to be able to bail out when | 6692 // These states are only added to be able to bail out when |
6696 * they are added again, nothing is to be done. */ | 6693 // they are added again, nothing is to be done. |
6697 break; | 6694 break; |
6698 | 6695 |
6699 default: /* regular character */ | 6696 default: // regular character |
6700 { | 6697 { |
6701 int c = t->state->c; | 6698 int c = t->state->c; |
6702 | 6699 |
6703 #ifdef DEBUG | 6700 #ifdef DEBUG |
6704 if (c < 0) | 6701 if (c < 0) |
6706 #endif | 6703 #endif |
6707 result = (c == curc); | 6704 result = (c == curc); |
6708 | 6705 |
6709 if (!result && rex.reg_ic) | 6706 if (!result && rex.reg_ic) |
6710 result = MB_TOLOWER(c) == MB_TOLOWER(curc); | 6707 result = MB_TOLOWER(c) == MB_TOLOWER(curc); |
6711 /* If rex.reg_icombine is not set only skip over the character | 6708 // If rex.reg_icombine is not set only skip over the character |
6712 * itself. When it is set skip over composing characters. */ | 6709 // itself. When it is set skip over composing characters. |
6713 if (result && enc_utf8 && !rex.reg_icombine) | 6710 if (result && enc_utf8 && !rex.reg_icombine) |
6714 clen = utf_ptr2len(rex.input); | 6711 clen = utf_ptr2len(rex.input); |
6715 ADD_STATE_IF_MATCH(t->state); | 6712 ADD_STATE_IF_MATCH(t->state); |
6716 break; | 6713 break; |
6717 } | 6714 } |
6718 | 6715 |
6719 } /* switch (t->state->c) */ | 6716 } // switch (t->state->c) |
6720 | 6717 |
6721 if (add_state != NULL) | 6718 if (add_state != NULL) |
6722 { | 6719 { |
6723 nfa_pim_T *pim; | 6720 nfa_pim_T *pim; |
6724 nfa_pim_T pim_copy; | 6721 nfa_pim_T pim_copy; |
6726 if (t->pim.result == NFA_PIM_UNUSED) | 6723 if (t->pim.result == NFA_PIM_UNUSED) |
6727 pim = NULL; | 6724 pim = NULL; |
6728 else | 6725 else |
6729 pim = &t->pim; | 6726 pim = &t->pim; |
6730 | 6727 |
6731 /* Handle the postponed invisible match if the match might end | 6728 // Handle the postponed invisible match if the match might end |
6732 * without advancing and before the end of the line. */ | 6729 // without advancing and before the end of the line. |
6733 if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) | 6730 if (pim != NULL && (clen == 0 || match_follows(add_state, 0))) |
6734 { | 6731 { |
6735 if (pim->result == NFA_PIM_TODO) | 6732 if (pim->result == NFA_PIM_TODO) |
6736 { | 6733 { |
6737 #ifdef ENABLE_LOG | 6734 #ifdef ENABLE_LOG |
6741 fprintf(log_fd, "\n"); | 6738 fprintf(log_fd, "\n"); |
6742 #endif | 6739 #endif |
6743 result = recursive_regmatch(pim->state, pim, | 6740 result = recursive_regmatch(pim->state, pim, |
6744 prog, submatch, m, &listids, &listids_len); | 6741 prog, submatch, m, &listids, &listids_len); |
6745 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; | 6742 pim->result = result ? NFA_PIM_MATCH : NFA_PIM_NOMATCH; |
6746 /* for \@! and \@<! it is a match when the result is | 6743 // for \@! and \@<! it is a match when the result is |
6747 * FALSE */ | 6744 // FALSE |
6748 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG | 6745 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
6749 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST | 6746 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
6750 || pim->state->c | 6747 || pim->state->c |
6751 == NFA_START_INVISIBLE_BEFORE_NEG | 6748 == NFA_START_INVISIBLE_BEFORE_NEG |
6752 || pim->state->c | 6749 || pim->state->c |
6753 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) | 6750 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
6754 { | 6751 { |
6755 /* Copy submatch info from the recursive call */ | 6752 // Copy submatch info from the recursive call |
6756 copy_sub_off(&pim->subs.norm, &m->norm); | 6753 copy_sub_off(&pim->subs.norm, &m->norm); |
6757 #ifdef FEAT_SYN_HL | 6754 #ifdef FEAT_SYN_HL |
6758 if (rex.nfa_has_zsubexpr) | 6755 if (rex.nfa_has_zsubexpr) |
6759 copy_sub_off(&pim->subs.synt, &m->synt); | 6756 copy_sub_off(&pim->subs.synt, &m->synt); |
6760 #endif | 6757 #endif |
6769 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); | 6766 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); |
6770 fprintf(log_fd, "\n"); | 6767 fprintf(log_fd, "\n"); |
6771 #endif | 6768 #endif |
6772 } | 6769 } |
6773 | 6770 |
6774 /* for \@! and \@<! it is a match when result is FALSE */ | 6771 // for \@! and \@<! it is a match when result is FALSE |
6775 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG | 6772 if (result != (pim->state->c == NFA_START_INVISIBLE_NEG |
6776 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST | 6773 || pim->state->c == NFA_START_INVISIBLE_NEG_FIRST |
6777 || pim->state->c | 6774 || pim->state->c |
6778 == NFA_START_INVISIBLE_BEFORE_NEG | 6775 == NFA_START_INVISIBLE_BEFORE_NEG |
6779 || pim->state->c | 6776 || pim->state->c |
6780 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) | 6777 == NFA_START_INVISIBLE_BEFORE_NEG_FIRST)) |
6781 { | 6778 { |
6782 /* Copy submatch info from the recursive call */ | 6779 // Copy submatch info from the recursive call |
6783 copy_sub_off(&t->subs.norm, &pim->subs.norm); | 6780 copy_sub_off(&t->subs.norm, &pim->subs.norm); |
6784 #ifdef FEAT_SYN_HL | 6781 #ifdef FEAT_SYN_HL |
6785 if (rex.nfa_has_zsubexpr) | 6782 if (rex.nfa_has_zsubexpr) |
6786 copy_sub_off(&t->subs.synt, &pim->subs.synt); | 6783 copy_sub_off(&t->subs.synt, &pim->subs.synt); |
6787 #endif | 6784 #endif |
6788 } | 6785 } |
6789 else | 6786 else |
6790 /* look-behind match failed, don't add the state */ | 6787 // look-behind match failed, don't add the state |
6791 continue; | 6788 continue; |
6792 | 6789 |
6793 /* Postponed invisible match was handled, don't add it to | 6790 // Postponed invisible match was handled, don't add it to |
6794 * following states. */ | 6791 // following states. |
6795 pim = NULL; | 6792 pim = NULL; |
6796 } | 6793 } |
6797 | 6794 |
6798 /* If "pim" points into l->t it will become invalid when | 6795 // If "pim" points into l->t it will become invalid when |
6799 * adding the state causes the list to be reallocated. Make a | 6796 // adding the state causes the list to be reallocated. Make a |
6800 * local copy to avoid that. */ | 6797 // local copy to avoid that. |
6801 if (pim == &t->pim) | 6798 if (pim == &t->pim) |
6802 { | 6799 { |
6803 copy_pim(&pim_copy, pim); | 6800 copy_pim(&pim_copy, pim); |
6804 pim = &pim_copy; | 6801 pim = &pim_copy; |
6805 } | 6802 } |
6818 nfa_match = NFA_TOO_EXPENSIVE; | 6815 nfa_match = NFA_TOO_EXPENSIVE; |
6819 goto theend; | 6816 goto theend; |
6820 } | 6817 } |
6821 } | 6818 } |
6822 | 6819 |
6823 } /* for (thislist = thislist; thislist->state; thislist++) */ | 6820 } // for (thislist = thislist; thislist->state; thislist++) |
6824 | 6821 |
6825 /* Look for the start of a match in the current position by adding the | 6822 // Look for the start of a match in the current position by adding the |
6826 * start state to the list of states. | 6823 // start state to the list of states. |
6827 * The first found match is the leftmost one, thus the order of states | 6824 // The first found match is the leftmost one, thus the order of states |
6828 * matters! | 6825 // matters! |
6829 * Do not add the start state in recursive calls of nfa_regmatch(), | 6826 // Do not add the start state in recursive calls of nfa_regmatch(), |
6830 * because recursive calls should only start in the first position. | 6827 // because recursive calls should only start in the first position. |
6831 * Unless "nfa_endp" is not NULL, then we match the end position. | 6828 // Unless "nfa_endp" is not NULL, then we match the end position. |
6832 * Also don't start a match past the first line. */ | 6829 // Also don't start a match past the first line. |
6833 if (nfa_match == FALSE | 6830 if (nfa_match == FALSE |
6834 && ((toplevel | 6831 && ((toplevel |
6835 && rex.lnum == 0 | 6832 && rex.lnum == 0 |
6836 && clen != 0 | 6833 && clen != 0 |
6837 && (rex.reg_maxcol == 0 | 6834 && (rex.reg_maxcol == 0 |
6845 : rex.input < nfa_endp->se_u.ptr)))) | 6842 : rex.input < nfa_endp->se_u.ptr)))) |
6846 { | 6843 { |
6847 #ifdef ENABLE_LOG | 6844 #ifdef ENABLE_LOG |
6848 fprintf(log_fd, "(---) STARTSTATE\n"); | 6845 fprintf(log_fd, "(---) STARTSTATE\n"); |
6849 #endif | 6846 #endif |
6850 /* Inline optimized code for addstate() if we know the state is | 6847 // Inline optimized code for addstate() if we know the state is |
6851 * the first MOPEN. */ | 6848 // the first MOPEN. |
6852 if (toplevel) | 6849 if (toplevel) |
6853 { | 6850 { |
6854 int add = TRUE; | 6851 int add = TRUE; |
6855 int c; | 6852 int c; |
6856 | 6853 |
6858 { | 6855 { |
6859 if (nextlist->n == 0) | 6856 if (nextlist->n == 0) |
6860 { | 6857 { |
6861 colnr_T col = (colnr_T)(rex.input - rex.line) + clen; | 6858 colnr_T col = (colnr_T)(rex.input - rex.line) + clen; |
6862 | 6859 |
6863 /* Nextlist is empty, we can skip ahead to the | 6860 // Nextlist is empty, we can skip ahead to the |
6864 * character that must appear at the start. */ | 6861 // character that must appear at the start. |
6865 if (skip_to_start(prog->regstart, &col) == FAIL) | 6862 if (skip_to_start(prog->regstart, &col) == FAIL) |
6866 break; | 6863 break; |
6867 #ifdef ENABLE_LOG | 6864 #ifdef ENABLE_LOG |
6868 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", | 6865 fprintf(log_fd, " Skipping ahead %d bytes to regstart\n", |
6869 col - ((colnr_T)(rex.input - rex.line) + clen)); | 6866 col - ((colnr_T)(rex.input - rex.line) + clen)); |
6870 #endif | 6867 #endif |
6871 rex.input = rex.line + col - clen; | 6868 rex.input = rex.line + col - clen; |
6872 } | 6869 } |
6873 else | 6870 else |
6874 { | 6871 { |
6875 /* Checking if the required start character matches is | 6872 // Checking if the required start character matches is |
6876 * cheaper than adding a state that won't match. */ | 6873 // cheaper than adding a state that won't match. |
6877 c = PTR2CHAR(rex.input + clen); | 6874 c = PTR2CHAR(rex.input + clen); |
6878 if (c != prog->regstart && (!rex.reg_ic | 6875 if (c != prog->regstart && (!rex.reg_ic |
6879 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart))) | 6876 || MB_TOLOWER(c) != MB_TOLOWER(prog->regstart))) |
6880 { | 6877 { |
6881 #ifdef ENABLE_LOG | 6878 #ifdef ENABLE_LOG |
6920 } | 6917 } |
6921 fprintf(log_fd, "\n"); | 6918 fprintf(log_fd, "\n"); |
6922 #endif | 6919 #endif |
6923 | 6920 |
6924 nextchar: | 6921 nextchar: |
6925 /* Advance to the next character, or advance to the next line, or | 6922 // Advance to the next character, or advance to the next line, or |
6926 * finish. */ | 6923 // finish. |
6927 if (clen != 0) | 6924 if (clen != 0) |
6928 rex.input += clen; | 6925 rex.input += clen; |
6929 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI | 6926 else if (go_to_nextline || (nfa_endp != NULL && REG_MULTI |
6930 && rex.lnum < nfa_endp->se_u.pos.lnum)) | 6927 && rex.lnum < nfa_endp->se_u.pos.lnum)) |
6931 reg_nextline(); | 6928 reg_nextline(); |
6932 else | 6929 else |
6933 break; | 6930 break; |
6934 | 6931 |
6935 /* Allow interrupting with CTRL-C. */ | 6932 // Allow interrupting with CTRL-C. |
6936 line_breakcheck(); | 6933 line_breakcheck(); |
6937 if (got_int) | 6934 if (got_int) |
6938 break; | 6935 break; |
6939 #ifdef FEAT_RELTIME | 6936 #ifdef FEAT_RELTIME |
6940 /* Check for timeout once in a twenty times to avoid overhead. */ | 6937 // Check for timeout once in a twenty times to avoid overhead. |
6941 if (nfa_time_limit != NULL && ++nfa_time_count == 20) | 6938 if (nfa_time_limit != NULL && ++nfa_time_count == 20) |
6942 { | 6939 { |
6943 nfa_time_count = 0; | 6940 nfa_time_count = 0; |
6944 if (nfa_did_time_out()) | 6941 if (nfa_did_time_out()) |
6945 break; | 6942 break; |
6952 fclose(log_fd); | 6949 fclose(log_fd); |
6953 log_fd = NULL; | 6950 log_fd = NULL; |
6954 #endif | 6951 #endif |
6955 | 6952 |
6956 theend: | 6953 theend: |
6957 /* Free memory */ | 6954 // Free memory |
6958 vim_free(list[0].t); | 6955 vim_free(list[0].t); |
6959 vim_free(list[1].t); | 6956 vim_free(list[1].t); |
6960 vim_free(listids); | 6957 vim_free(listids); |
6961 #undef ADD_STATE_IF_MATCH | 6958 #undef ADD_STATE_IF_MATCH |
6962 #ifdef NFA_REGEXP_DEBUG_LOG | 6959 #ifdef NFA_REGEXP_DEBUG_LOG |
6972 */ | 6969 */ |
6973 static long | 6970 static long |
6974 nfa_regtry( | 6971 nfa_regtry( |
6975 nfa_regprog_T *prog, | 6972 nfa_regprog_T *prog, |
6976 colnr_T col, | 6973 colnr_T col, |
6977 proftime_T *tm UNUSED, /* timeout limit or NULL */ | 6974 proftime_T *tm UNUSED, // timeout limit or NULL |
6978 int *timed_out UNUSED) /* flag set on timeout or NULL */ | 6975 int *timed_out UNUSED) // flag set on timeout or NULL |
6979 { | 6976 { |
6980 int i; | 6977 int i; |
6981 regsubs_T subs, m; | 6978 regsubs_T subs, m; |
6982 nfa_state_T *start = prog->start; | 6979 nfa_state_T *start = prog->start; |
6983 int result; | 6980 int result; |
7040 rex.reg_startpos[0].lnum = 0; | 7037 rex.reg_startpos[0].lnum = 0; |
7041 rex.reg_startpos[0].col = col; | 7038 rex.reg_startpos[0].col = col; |
7042 } | 7039 } |
7043 if (rex.reg_endpos[0].lnum < 0) | 7040 if (rex.reg_endpos[0].lnum < 0) |
7044 { | 7041 { |
7045 /* pattern has a \ze but it didn't match, use current end */ | 7042 // pattern has a \ze but it didn't match, use current end |
7046 rex.reg_endpos[0].lnum = rex.lnum; | 7043 rex.reg_endpos[0].lnum = rex.lnum; |
7047 rex.reg_endpos[0].col = (int)(rex.input - rex.line); | 7044 rex.reg_endpos[0].col = (int)(rex.input - rex.line); |
7048 } | 7045 } |
7049 else | 7046 else |
7050 /* Use line number of "\ze". */ | 7047 // Use line number of "\ze". |
7051 rex.lnum = rex.reg_endpos[0].lnum; | 7048 rex.lnum = rex.reg_endpos[0].lnum; |
7052 } | 7049 } |
7053 else | 7050 else |
7054 { | 7051 { |
7055 for (i = 0; i < subs.norm.in_use; i++) | 7052 for (i = 0; i < subs.norm.in_use; i++) |
7063 if (rex.reg_endp[0] == NULL) | 7060 if (rex.reg_endp[0] == NULL) |
7064 rex.reg_endp[0] = rex.input; | 7061 rex.reg_endp[0] = rex.input; |
7065 } | 7062 } |
7066 | 7063 |
7067 #ifdef FEAT_SYN_HL | 7064 #ifdef FEAT_SYN_HL |
7068 /* Package any found \z(...\) matches for export. Default is none. */ | 7065 // Package any found \z(...\) matches for export. Default is none. |
7069 unref_extmatch(re_extmatch_out); | 7066 unref_extmatch(re_extmatch_out); |
7070 re_extmatch_out = NULL; | 7067 re_extmatch_out = NULL; |
7071 | 7068 |
7072 if (prog->reghasz == REX_SET) | 7069 if (prog->reghasz == REX_SET) |
7073 { | 7070 { |
7074 cleanup_zsubexpr(); | 7071 cleanup_zsubexpr(); |
7075 re_extmatch_out = make_extmatch(); | 7072 re_extmatch_out = make_extmatch(); |
7076 /* Loop over \z1, \z2, etc. There is no \z0. */ | 7073 // Loop over \z1, \z2, etc. There is no \z0. |
7077 for (i = 1; i < subs.synt.in_use; i++) | 7074 for (i = 1; i < subs.synt.in_use; i++) |
7078 { | 7075 { |
7079 if (REG_MULTI) | 7076 if (REG_MULTI) |
7080 { | 7077 { |
7081 struct multipos *mpos = &subs.synt.list.multi[i]; | 7078 struct multipos *mpos = &subs.synt.list.multi[i]; |
7082 | 7079 |
7083 /* Only accept single line matches that are valid. */ | 7080 // Only accept single line matches that are valid. |
7084 if (mpos->start_lnum >= 0 | 7081 if (mpos->start_lnum >= 0 |
7085 && mpos->start_lnum == mpos->end_lnum | 7082 && mpos->start_lnum == mpos->end_lnum |
7086 && mpos->end_col >= mpos->start_col) | 7083 && mpos->end_col >= mpos->start_col) |
7087 re_extmatch_out->matches[i] = | 7084 re_extmatch_out->matches[i] = |
7088 vim_strnsave(reg_getline(mpos->start_lnum) | 7085 vim_strnsave(reg_getline(mpos->start_lnum) |
7112 * Returns <= 0 for failure, number of lines contained in the match otherwise. | 7109 * Returns <= 0 for failure, number of lines contained in the match otherwise. |
7113 */ | 7110 */ |
7114 static long | 7111 static long |
7115 nfa_regexec_both( | 7112 nfa_regexec_both( |
7116 char_u *line, | 7113 char_u *line, |
7117 colnr_T startcol, /* column to start looking for match */ | 7114 colnr_T startcol, // column to start looking for match |
7118 proftime_T *tm, /* timeout limit or NULL */ | 7115 proftime_T *tm, // timeout limit or NULL |
7119 int *timed_out) /* flag set on timeout or NULL */ | 7116 int *timed_out) // flag set on timeout or NULL |
7120 { | 7117 { |
7121 nfa_regprog_T *prog; | 7118 nfa_regprog_T *prog; |
7122 long retval = 0L; | 7119 long retval = 0L; |
7123 int i; | 7120 int i; |
7124 colnr_T col = startcol; | 7121 colnr_T col = startcol; |
7125 | 7122 |
7126 if (REG_MULTI) | 7123 if (REG_MULTI) |
7127 { | 7124 { |
7128 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog; | 7125 prog = (nfa_regprog_T *)rex.reg_mmatch->regprog; |
7129 line = reg_getline((linenr_T)0); /* relative to the cursor */ | 7126 line = reg_getline((linenr_T)0); // relative to the cursor |
7130 rex.reg_startpos = rex.reg_mmatch->startpos; | 7127 rex.reg_startpos = rex.reg_mmatch->startpos; |
7131 rex.reg_endpos = rex.reg_mmatch->endpos; | 7128 rex.reg_endpos = rex.reg_mmatch->endpos; |
7132 } | 7129 } |
7133 else | 7130 else |
7134 { | 7131 { |
7135 prog = (nfa_regprog_T *)rex.reg_match->regprog; | 7132 prog = (nfa_regprog_T *)rex.reg_match->regprog; |
7136 rex.reg_startp = rex.reg_match->startp; | 7133 rex.reg_startp = rex.reg_match->startp; |
7137 rex.reg_endp = rex.reg_match->endp; | 7134 rex.reg_endp = rex.reg_match->endp; |
7138 } | 7135 } |
7139 | 7136 |
7140 /* Be paranoid... */ | 7137 // Be paranoid... |
7141 if (prog == NULL || line == NULL) | 7138 if (prog == NULL || line == NULL) |
7142 { | 7139 { |
7143 emsg(_(e_null)); | 7140 emsg(_(e_null)); |
7144 goto theend; | 7141 goto theend; |
7145 } | 7142 } |
7146 | 7143 |
7147 /* If pattern contains "\c" or "\C": overrule value of rex.reg_ic */ | 7144 // If pattern contains "\c" or "\C": overrule value of rex.reg_ic |
7148 if (prog->regflags & RF_ICASE) | 7145 if (prog->regflags & RF_ICASE) |
7149 rex.reg_ic = TRUE; | 7146 rex.reg_ic = TRUE; |
7150 else if (prog->regflags & RF_NOICASE) | 7147 else if (prog->regflags & RF_NOICASE) |
7151 rex.reg_ic = FALSE; | 7148 rex.reg_ic = FALSE; |
7152 | 7149 |
7153 /* If pattern contains "\Z" overrule value of rex.reg_icombine */ | 7150 // If pattern contains "\Z" overrule value of rex.reg_icombine |
7154 if (prog->regflags & RF_ICOMBINE) | 7151 if (prog->regflags & RF_ICOMBINE) |
7155 rex.reg_icombine = TRUE; | 7152 rex.reg_icombine = TRUE; |
7156 | 7153 |
7157 rex.line = line; | 7154 rex.line = line; |
7158 rex.lnum = 0; /* relative to line */ | 7155 rex.lnum = 0; // relative to line |
7159 | 7156 |
7160 rex.nfa_has_zend = prog->has_zend; | 7157 rex.nfa_has_zend = prog->has_zend; |
7161 rex.nfa_has_backref = prog->has_backref; | 7158 rex.nfa_has_backref = prog->has_backref; |
7162 rex.nfa_nsubexpr = prog->nsubexp; | 7159 rex.nfa_nsubexpr = prog->nsubexp; |
7163 rex.nfa_listid = 1; | 7160 rex.nfa_listid = 1; |
7169 if (prog->reganch && col > 0) | 7166 if (prog->reganch && col > 0) |
7170 return 0L; | 7167 return 0L; |
7171 | 7168 |
7172 rex.need_clear_subexpr = TRUE; | 7169 rex.need_clear_subexpr = TRUE; |
7173 #ifdef FEAT_SYN_HL | 7170 #ifdef FEAT_SYN_HL |
7174 /* Clear the external match subpointers if necessary. */ | 7171 // Clear the external match subpointers if necessary. |
7175 if (prog->reghasz == REX_SET) | 7172 if (prog->reghasz == REX_SET) |
7176 { | 7173 { |
7177 rex.nfa_has_zsubexpr = TRUE; | 7174 rex.nfa_has_zsubexpr = TRUE; |
7178 rex.need_clear_zsubexpr = TRUE; | 7175 rex.need_clear_zsubexpr = TRUE; |
7179 } | 7176 } |
7184 } | 7181 } |
7185 #endif | 7182 #endif |
7186 | 7183 |
7187 if (prog->regstart != NUL) | 7184 if (prog->regstart != NUL) |
7188 { | 7185 { |
7189 /* Skip ahead until a character we know the match must start with. | 7186 // Skip ahead until a character we know the match must start with. |
7190 * When there is none there is no match. */ | 7187 // When there is none there is no match. |
7191 if (skip_to_start(prog->regstart, &col) == FAIL) | 7188 if (skip_to_start(prog->regstart, &col) == FAIL) |
7192 return 0L; | 7189 return 0L; |
7193 | 7190 |
7194 /* If match_text is set it contains the full text that must match. | 7191 // If match_text is set it contains the full text that must match. |
7195 * Nothing else to try. Doesn't handle combining chars well. */ | 7192 // Nothing else to try. Doesn't handle combining chars well. |
7196 if (prog->match_text != NULL && !rex.reg_icombine) | 7193 if (prog->match_text != NULL && !rex.reg_icombine) |
7197 return find_match_text(col, prog->regstart, prog->match_text); | 7194 return find_match_text(col, prog->regstart, prog->match_text); |
7198 } | 7195 } |
7199 | 7196 |
7200 /* If the start column is past the maximum column: no need to try. */ | 7197 // If the start column is past the maximum column: no need to try. |
7201 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol) | 7198 if (rex.reg_maxcol > 0 && col >= rex.reg_maxcol) |
7202 goto theend; | 7199 goto theend; |
7203 | 7200 |
7204 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when | 7201 // Set the "nstate" used by nfa_regcomp() to zero to trigger an error when |
7205 // it's accidentally used during execution. | 7202 // it's accidentally used during execution. |
7243 init_class_tab(); | 7240 init_class_tab(); |
7244 | 7241 |
7245 if (nfa_regcomp_start(expr, re_flags) == FAIL) | 7242 if (nfa_regcomp_start(expr, re_flags) == FAIL) |
7246 return NULL; | 7243 return NULL; |
7247 | 7244 |
7248 /* Build postfix form of the regexp. Needed to build the NFA | 7245 // Build postfix form of the regexp. Needed to build the NFA |
7249 * (and count its size). */ | 7246 // (and count its size). |
7250 postfix = re2post(); | 7247 postfix = re2post(); |
7251 if (postfix == NULL) | 7248 if (postfix == NULL) |
7252 goto fail; /* Cascaded (syntax?) error */ | 7249 goto fail; // Cascaded (syntax?) error |
7253 | 7250 |
7254 /* | 7251 /* |
7255 * In order to build the NFA, we parse the input regexp twice: | 7252 * In order to build the NFA, we parse the input regexp twice: |
7256 * 1. first pass to count size (so we can allocate space) | 7253 * 1. first pass to count size (so we can allocate space) |
7257 * 2. second to emit code | 7254 * 2. second to emit code |
7272 * PASS 1 | 7269 * PASS 1 |
7273 * Count number of NFA states in "nstate". Do not build the NFA. | 7270 * Count number of NFA states in "nstate". Do not build the NFA. |
7274 */ | 7271 */ |
7275 post2nfa(postfix, post_ptr, TRUE); | 7272 post2nfa(postfix, post_ptr, TRUE); |
7276 | 7273 |
7277 /* allocate the regprog with space for the compiled regexp */ | 7274 // allocate the regprog with space for the compiled regexp |
7278 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); | 7275 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * (nstate - 1); |
7279 prog = alloc(prog_size); | 7276 prog = alloc(prog_size); |
7280 if (prog == NULL) | 7277 if (prog == NULL) |
7281 goto fail; | 7278 goto fail; |
7282 state_ptr = prog->state; | 7279 state_ptr = prog->state; |
7306 #ifdef ENABLE_LOG | 7303 #ifdef ENABLE_LOG |
7307 nfa_postfix_dump(expr, OK); | 7304 nfa_postfix_dump(expr, OK); |
7308 nfa_dump(prog); | 7305 nfa_dump(prog); |
7309 #endif | 7306 #endif |
7310 #ifdef FEAT_SYN_HL | 7307 #ifdef FEAT_SYN_HL |
7311 /* Remember whether this pattern has any \z specials in it. */ | 7308 // Remember whether this pattern has any \z specials in it. |
7312 prog->reghasz = re_has_z; | 7309 prog->reghasz = re_has_z; |
7313 #endif | 7310 #endif |
7314 prog->pattern = vim_strsave(expr); | 7311 prog->pattern = vim_strsave(expr); |
7315 #ifdef DEBUG | 7312 #ifdef DEBUG |
7316 nfa_regengine.expr = NULL; | 7313 nfa_regengine.expr = NULL; |
7356 * Returns <= 0 for failure, number of lines contained in the match otherwise. | 7353 * Returns <= 0 for failure, number of lines contained in the match otherwise. |
7357 */ | 7354 */ |
7358 static int | 7355 static int |
7359 nfa_regexec_nl( | 7356 nfa_regexec_nl( |
7360 regmatch_T *rmp, | 7357 regmatch_T *rmp, |
7361 char_u *line, /* string to match against */ | 7358 char_u *line, // string to match against |
7362 colnr_T col, /* column to start looking for match */ | 7359 colnr_T col, // column to start looking for match |
7363 int line_lbr) | 7360 int line_lbr) |
7364 { | 7361 { |
7365 rex.reg_match = rmp; | 7362 rex.reg_match = rmp; |
7366 rex.reg_mmatch = NULL; | 7363 rex.reg_mmatch = NULL; |
7367 rex.reg_maxline = 0; | 7364 rex.reg_maxline = 0; |
7401 * FIXME if this behavior is not compatible. | 7398 * FIXME if this behavior is not compatible. |
7402 */ | 7399 */ |
7403 static long | 7400 static long |
7404 nfa_regexec_multi( | 7401 nfa_regexec_multi( |
7405 regmmatch_T *rmp, | 7402 regmmatch_T *rmp, |
7406 win_T *win, /* window in which to search or NULL */ | 7403 win_T *win, // window in which to search or NULL |
7407 buf_T *buf, /* buffer in which to search */ | 7404 buf_T *buf, // buffer in which to search |
7408 linenr_T lnum, /* nr of line to start looking for match */ | 7405 linenr_T lnum, // nr of line to start looking for match |
7409 colnr_T col, /* column to start looking for match */ | 7406 colnr_T col, // column to start looking for match |
7410 proftime_T *tm, /* timeout limit or NULL */ | 7407 proftime_T *tm, // timeout limit or NULL |
7411 int *timed_out) /* flag set on timeout or NULL */ | 7408 int *timed_out) // flag set on timeout or NULL |
7412 { | 7409 { |
7413 rex.reg_match = NULL; | 7410 rex.reg_match = NULL; |
7414 rex.reg_mmatch = rmp; | 7411 rex.reg_mmatch = rmp; |
7415 rex.reg_buf = buf; | 7412 rex.reg_buf = buf; |
7416 rex.reg_win = win; | 7413 rex.reg_win = win; |