Mercurial > vim
annotate src/regexp_nfa.c @ 4515:90e9917d4114 v7.3.1005
updated for version 7.3.1005
Problem: Get stuck on regexp "\n*" and on "%s/^\n\+/\r".
Solution: Fix handling of matching a line break. (idea by Hirohito Higashi)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 22 May 2013 23:00:40 +0200 |
parents | 9dbbddb2ed10 |
children | 9a2183bd8295 |
rev | line source |
---|---|
4444 | 1 /* vi:set ts=8 sts=4 sw=4: |
2 * | |
3 * NFA regular expression implementation. | |
4 * | |
5 * This file is included in "regexp.c". | |
6 */ | |
7 | |
8 #ifdef DEBUG | |
9 /* Comment this out to disable log files. They can get pretty big */ | |
10 # define ENABLE_LOG | |
11 # define LOG_NAME "log_nfarun.log" | |
4460 | 12 # define NFA_REGEXP_DEBUG_LOG |
13 # define NFA_REGEXP_DEBUG_LOG_NAME "nfa_regexp_debug.log" | |
4444 | 14 #endif |
15 | |
16 /* Upper limit allowed for {m,n} repetitions handled by NFA */ | |
17 #define NFA_BRACES_MAXLIMIT 10 | |
18 /* For allocating space for the postfix representation */ | |
19 #define NFA_POSTFIX_MULTIPLIER (NFA_BRACES_MAXLIMIT + 2)*2 | |
20 | |
21 enum | |
22 { | |
23 NFA_SPLIT = -1024, | |
24 NFA_MATCH, | |
25 NFA_SKIP_CHAR, /* matches a 0-length char */ | |
26 NFA_END_NEG_RANGE, /* Used when expanding [^ab] */ | |
27 | |
28 NFA_CONCAT, | |
29 NFA_OR, | |
30 NFA_STAR, | |
31 NFA_PLUS, | |
32 NFA_QUEST, | |
33 NFA_QUEST_NONGREEDY, /* Non-greedy version of \? */ | |
34 NFA_NOT, /* used for [^ab] negated char ranges */ | |
35 | |
36 NFA_BOL, /* ^ Begin line */ | |
37 NFA_EOL, /* $ End line */ | |
38 NFA_BOW, /* \< Begin word */ | |
39 NFA_EOW, /* \> End word */ | |
40 NFA_BOF, /* \%^ Begin file */ | |
41 NFA_EOF, /* \%$ End file */ | |
42 NFA_NEWL, | |
43 NFA_ZSTART, /* Used for \zs */ | |
44 NFA_ZEND, /* Used for \ze */ | |
45 NFA_NOPEN, /* Start of subexpression marked with \%( */ | |
46 NFA_NCLOSE, /* End of subexpr. marked with \%( ... \) */ | |
47 NFA_START_INVISIBLE, | |
48 NFA_END_INVISIBLE, | |
49 NFA_MULTIBYTE, /* Next nodes in NFA are part of the same | |
50 multibyte char */ | |
51 NFA_END_MULTIBYTE, /* End of multibyte char in the NFA */ | |
52 NFA_COMPOSING, /* Next nodes in NFA are part of the | |
53 composing multibyte char */ | |
54 NFA_END_COMPOSING, /* End of a composing char in the NFA */ | |
55 | |
56 /* The following are used only in the postfix form, not in the NFA */ | |
57 NFA_PREV_ATOM_NO_WIDTH, /* Used for \@= */ | |
58 NFA_PREV_ATOM_NO_WIDTH_NEG, /* Used for \@! */ | |
59 NFA_PREV_ATOM_JUST_BEFORE, /* Used for \@<= */ | |
60 NFA_PREV_ATOM_JUST_BEFORE_NEG, /* Used for \@<! */ | |
61 NFA_PREV_ATOM_LIKE_PATTERN, /* Used for \@> */ | |
62 | |
63 NFA_MOPEN, | |
64 NFA_MCLOSE = NFA_MOPEN + NSUBEXP, | |
65 | |
66 /* NFA_FIRST_NL */ | |
67 NFA_ANY = NFA_MCLOSE + NSUBEXP, /* Match any one character. */ | |
68 NFA_ANYOF, /* Match any character in this string. */ | |
69 NFA_ANYBUT, /* Match any character not in this string. */ | |
70 NFA_IDENT, /* Match identifier char */ | |
71 NFA_SIDENT, /* Match identifier char but no digit */ | |
72 NFA_KWORD, /* Match keyword char */ | |
73 NFA_SKWORD, /* Match word char but no digit */ | |
74 NFA_FNAME, /* Match file name char */ | |
75 NFA_SFNAME, /* Match file name char but no digit */ | |
76 NFA_PRINT, /* Match printable char */ | |
77 NFA_SPRINT, /* Match printable char but no digit */ | |
78 NFA_WHITE, /* Match whitespace char */ | |
79 NFA_NWHITE, /* Match non-whitespace char */ | |
80 NFA_DIGIT, /* Match digit char */ | |
81 NFA_NDIGIT, /* Match non-digit char */ | |
82 NFA_HEX, /* Match hex char */ | |
83 NFA_NHEX, /* Match non-hex char */ | |
84 NFA_OCTAL, /* Match octal char */ | |
85 NFA_NOCTAL, /* Match non-octal char */ | |
86 NFA_WORD, /* Match word char */ | |
87 NFA_NWORD, /* Match non-word char */ | |
88 NFA_HEAD, /* Match head char */ | |
89 NFA_NHEAD, /* Match non-head char */ | |
90 NFA_ALPHA, /* Match alpha char */ | |
91 NFA_NALPHA, /* Match non-alpha char */ | |
92 NFA_LOWER, /* Match lowercase char */ | |
93 NFA_NLOWER, /* Match non-lowercase char */ | |
94 NFA_UPPER, /* Match uppercase char */ | |
95 NFA_NUPPER, /* Match non-uppercase char */ | |
96 NFA_FIRST_NL = NFA_ANY + ADD_NL, | |
97 NFA_LAST_NL = NFA_NUPPER + ADD_NL, | |
98 | |
99 /* Character classes [:alnum:] etc */ | |
100 NFA_CLASS_ALNUM, | |
101 NFA_CLASS_ALPHA, | |
102 NFA_CLASS_BLANK, | |
103 NFA_CLASS_CNTRL, | |
104 NFA_CLASS_DIGIT, | |
105 NFA_CLASS_GRAPH, | |
106 NFA_CLASS_LOWER, | |
107 NFA_CLASS_PRINT, | |
108 NFA_CLASS_PUNCT, | |
109 NFA_CLASS_SPACE, | |
110 NFA_CLASS_UPPER, | |
111 NFA_CLASS_XDIGIT, | |
112 NFA_CLASS_TAB, | |
113 NFA_CLASS_RETURN, | |
114 NFA_CLASS_BACKSPACE, | |
115 NFA_CLASS_ESCAPE | |
116 }; | |
117 | |
118 /* Keep in sync with classchars. */ | |
119 static int nfa_classcodes[] = { | |
120 NFA_ANY, NFA_IDENT, NFA_SIDENT, NFA_KWORD,NFA_SKWORD, | |
121 NFA_FNAME, NFA_SFNAME, NFA_PRINT, NFA_SPRINT, | |
122 NFA_WHITE, NFA_NWHITE, NFA_DIGIT, NFA_NDIGIT, | |
123 NFA_HEX, NFA_NHEX, NFA_OCTAL, NFA_NOCTAL, | |
124 NFA_WORD, NFA_NWORD, NFA_HEAD, NFA_NHEAD, | |
125 NFA_ALPHA, NFA_NALPHA, NFA_LOWER, NFA_NLOWER, | |
126 NFA_UPPER, NFA_NUPPER | |
127 }; | |
128 | |
129 static char_u e_misplaced[] = N_("E866: (NFA regexp) Misplaced %c"); | |
130 | |
131 /* | |
132 * NFA errors can be of 3 types: | |
133 * *** NFA runtime errors, when something unknown goes wrong. The NFA fails | |
134 * silently and revert the to backtracking engine. | |
135 * syntax_error = FALSE; | |
136 * *** Regexp syntax errors, when the input regexp is not syntactically correct. | |
137 * The NFA engine displays an error message, and nothing else happens. | |
138 * syntax_error = TRUE | |
139 * *** Unsupported features, when the input regexp uses an operator that is not | |
140 * implemented in the NFA. The NFA engine fails silently, and reverts to the | |
141 * old backtracking engine. | |
142 * syntax_error = FALSE | |
143 * "The NFA fails" means that "compiling the regexp with the NFA fails": | |
144 * nfa_regcomp() returns FAIL. | |
145 */ | |
146 static int syntax_error = FALSE; | |
147 | |
148 /* NFA regexp \ze operator encountered. */ | |
149 static int nfa_has_zend = FALSE; | |
150 | |
151 static int *post_start; /* holds the postfix form of r.e. */ | |
152 static int *post_end; | |
153 static int *post_ptr; | |
154 | |
155 static int nstate; /* Number of states in the NFA. */ | |
156 static int istate; /* Index in the state vector, used in new_state() */ | |
157 static int nstate_max; /* Upper bound of estimated number of states. */ | |
158 | |
159 | |
160 static int nfa_regcomp_start __ARGS((char_u*expr, int re_flags)); | |
161 static int nfa_recognize_char_class __ARGS((char_u *start, char_u *end, int extra_newl)); | |
162 static int nfa_emit_equi_class __ARGS((int c, int neg)); | |
163 static void nfa_inc __ARGS((char_u **p)); | |
164 static void nfa_dec __ARGS((char_u **p)); | |
165 static int nfa_regatom __ARGS((void)); | |
166 static int nfa_regpiece __ARGS((void)); | |
167 static int nfa_regconcat __ARGS((void)); | |
168 static int nfa_regbranch __ARGS((void)); | |
169 static int nfa_reg __ARGS((int paren)); | |
170 #ifdef DEBUG | |
171 static void nfa_set_code __ARGS((int c)); | |
172 static void nfa_postfix_dump __ARGS((char_u *expr, int retval)); | |
173 static void nfa_print_state __ARGS((FILE *debugf, nfa_state_T *state, int ident)); | |
174 static void nfa_dump __ARGS((nfa_regprog_T *prog)); | |
175 #endif | |
176 static int *re2post __ARGS((void)); | |
177 static nfa_state_T *new_state __ARGS((int c, nfa_state_T *out, nfa_state_T *out1)); | |
178 static nfa_state_T *post2nfa __ARGS((int *postfix, int *end, int nfa_calc_size)); | |
179 static int check_char_class __ARGS((int class, int c)); | |
180 static void st_error __ARGS((int *postfix, int *end, int *p)); | |
181 static void nfa_save_listids __ARGS((nfa_state_T *start, int *list)); | |
182 static void nfa_restore_listids __ARGS((nfa_state_T *start, int *list)); | |
183 static void nfa_set_null_listids __ARGS((nfa_state_T *start)); | |
184 static void nfa_set_neg_listids __ARGS((nfa_state_T *start)); | |
185 static long nfa_regtry __ARGS((nfa_state_T *start, colnr_T col)); | |
186 static long nfa_regexec_both __ARGS((char_u *line, colnr_T col)); | |
187 static regprog_T *nfa_regcomp __ARGS((char_u *expr, int re_flags)); | |
188 static int nfa_regexec __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); | |
189 static long nfa_regexec_multi __ARGS((regmmatch_T *rmp, win_T *win, buf_T *buf, linenr_T lnum, colnr_T col, proftime_T *tm)); | |
190 | |
191 /* helper functions used when doing re2post() ... regatom() parsing */ | |
192 #define EMIT(c) do { \ | |
193 if (post_ptr >= post_end) \ | |
194 return FAIL; \ | |
195 *post_ptr++ = c; \ | |
196 } while (0) | |
197 | |
198 #define EMIT_MBYTE(c) \ | |
199 len = (*mb_char2bytes)(c, buf); \ | |
200 EMIT(buf[0]); \ | |
201 for (i = 1; i < len; i++) \ | |
202 { \ | |
203 EMIT(buf[i]); \ | |
204 EMIT(NFA_CONCAT); \ | |
205 } \ | |
206 EMIT(NFA_MULTIBYTE); | |
207 | |
208 #define EMIT_COMPOSING_UTF(input) \ | |
209 len = utfc_ptr2len(input); \ | |
210 EMIT(input[0]); \ | |
211 for (i = 1; i < len; i++) \ | |
212 { \ | |
213 EMIT(input[i]); \ | |
214 EMIT(NFA_CONCAT); \ | |
215 } \ | |
216 EMIT(NFA_COMPOSING); | |
217 | |
218 /* | |
219 * Initialize internal variables before NFA compilation. | |
220 * Return OK on success, FAIL otherwise. | |
221 */ | |
222 static int | |
223 nfa_regcomp_start(expr, re_flags) | |
224 char_u *expr; | |
225 int re_flags; /* see vim_regcomp() */ | |
226 { | |
4458 | 227 size_t postfix_size; |
4444 | 228 |
229 nstate = 0; | |
230 istate = 0; | |
231 /* A reasonable estimation for size */ | |
4458 | 232 nstate_max = (int)(STRLEN(expr) + 1) * NFA_POSTFIX_MULTIPLIER; |
4444 | 233 |
4454 | 234 /* Some items blow up in size, such as [A-z]. Add more space for that. |
235 * TODO: some patterns may still fail. */ | |
4458 | 236 nstate_max += 1000; |
4454 | 237 |
238 /* Size for postfix representation of expr. */ | |
4444 | 239 postfix_size = sizeof(*post_start) * nstate_max; |
4454 | 240 |
4444 | 241 post_start = (int *)lalloc(postfix_size, TRUE); |
242 if (post_start == NULL) | |
243 return FAIL; | |
244 vim_memset(post_start, 0, postfix_size); | |
245 post_ptr = post_start; | |
4454 | 246 post_end = post_start + nstate_max; |
4444 | 247 nfa_has_zend = FALSE; |
248 | |
249 regcomp_start(expr, re_flags); | |
250 | |
251 return OK; | |
252 } | |
253 | |
254 /* | |
255 * Search between "start" and "end" and try to recognize a | |
256 * character class in expanded form. For example [0-9]. | |
257 * On success, return the id the character class to be emitted. | |
258 * On failure, return 0 (=FAIL) | |
259 * Start points to the first char of the range, while end should point | |
260 * to the closing brace. | |
261 */ | |
262 static int | |
263 nfa_recognize_char_class(start, end, extra_newl) | |
264 char_u *start; | |
265 char_u *end; | |
266 int extra_newl; | |
267 { | |
268 int i; | |
269 /* Each of these variables takes up a char in "config[]", | |
270 * in the order they are here. */ | |
271 int not = FALSE, af = FALSE, AF = FALSE, az = FALSE, AZ = FALSE, | |
272 o7 = FALSE, o9 = FALSE, underscore = FALSE, newl = FALSE; | |
273 char_u *p; | |
274 #define NCONFIGS 16 | |
275 int classid[NCONFIGS] = { | |
276 NFA_DIGIT, NFA_NDIGIT, NFA_HEX, NFA_NHEX, | |
277 NFA_OCTAL, NFA_NOCTAL, NFA_WORD, NFA_NWORD, | |
278 NFA_HEAD, NFA_NHEAD, NFA_ALPHA, NFA_NALPHA, | |
279 NFA_LOWER, NFA_NLOWER, NFA_UPPER, NFA_NUPPER | |
280 }; | |
4450 | 281 char_u myconfig[10]; |
4444 | 282 char_u config[NCONFIGS][9] = { |
283 "000000100", /* digit */ | |
284 "100000100", /* non digit */ | |
285 "011000100", /* hex-digit */ | |
286 "111000100", /* non hex-digit */ | |
287 "000001000", /* octal-digit */ | |
288 "100001000", /* [^0-7] */ | |
289 "000110110", /* [0-9A-Za-z_] */ | |
290 "100110110", /* [^0-9A-Za-z_] */ | |
291 "000110010", /* head of word */ | |
292 "100110010", /* not head of word */ | |
293 "000110000", /* alphabetic char a-z */ | |
294 "100110000", /* non alphabetic char */ | |
295 "000100000", /* lowercase letter */ | |
296 "100100000", /* non lowercase */ | |
297 "000010000", /* uppercase */ | |
298 "100010000" /* non uppercase */ | |
299 }; | |
300 | |
301 if (extra_newl == TRUE) | |
302 newl = TRUE; | |
303 | |
304 if (*end != ']') | |
305 return FAIL; | |
306 p = start; | |
307 if (*p == '^') | |
308 { | |
309 not = TRUE; | |
310 p ++; | |
311 } | |
312 | |
313 while (p < end) | |
314 { | |
315 if (p + 2 < end && *(p + 1) == '-') | |
316 { | |
317 switch (*p) | |
318 { | |
319 case '0': | |
320 if (*(p + 2) == '9') | |
321 { | |
322 o9 = TRUE; | |
323 break; | |
324 } | |
325 else | |
326 if (*(p + 2) == '7') | |
327 { | |
328 o7 = TRUE; | |
329 break; | |
330 } | |
331 case 'a': | |
332 if (*(p + 2) == 'z') | |
333 { | |
334 az = TRUE; | |
335 break; | |
336 } | |
337 else | |
338 if (*(p + 2) == 'f') | |
339 { | |
340 af = TRUE; | |
341 break; | |
342 } | |
343 case 'A': | |
344 if (*(p + 2) == 'Z') | |
345 { | |
346 AZ = TRUE; | |
347 break; | |
348 } | |
349 else | |
350 if (*(p + 2) == 'F') | |
351 { | |
352 AF = TRUE; | |
353 break; | |
354 } | |
355 /* FALLTHROUGH */ | |
356 default: | |
357 return FAIL; | |
358 } | |
359 p += 3; | |
360 } | |
361 else if (p + 1 < end && *p == '\\' && *(p + 1) == 'n') | |
362 { | |
363 newl = TRUE; | |
364 p += 2; | |
365 } | |
366 else if (*p == '_') | |
367 { | |
368 underscore = TRUE; | |
369 p ++; | |
370 } | |
371 else if (*p == '\n') | |
372 { | |
373 newl = TRUE; | |
374 p ++; | |
375 } | |
376 else | |
377 return FAIL; | |
378 } /* while (p < end) */ | |
379 | |
380 if (p != end) | |
381 return FAIL; | |
382 | |
383 /* build the config that represents the ranges we gathered */ | |
384 STRCPY(myconfig, "000000000"); | |
385 if (not == TRUE) | |
386 myconfig[0] = '1'; | |
387 if (af == TRUE) | |
388 myconfig[1] = '1'; | |
389 if (AF == TRUE) | |
390 myconfig[2] = '1'; | |
391 if (az == TRUE) | |
392 myconfig[3] = '1'; | |
393 if (AZ == TRUE) | |
394 myconfig[4] = '1'; | |
395 if (o7 == TRUE) | |
396 myconfig[5] = '1'; | |
397 if (o9 == TRUE) | |
398 myconfig[6] = '1'; | |
399 if (underscore == TRUE) | |
400 myconfig[7] = '1'; | |
401 if (newl == TRUE) | |
402 { | |
403 myconfig[8] = '1'; | |
404 extra_newl = ADD_NL; | |
405 } | |
406 /* try to recognize character classes */ | |
407 for (i = 0; i < NCONFIGS; i++) | |
4450 | 408 if (STRNCMP(myconfig, config[i], 8) == 0) |
4444 | 409 return classid[i] + extra_newl; |
410 | |
411 /* fallthrough => no success so far */ | |
412 return FAIL; | |
413 | |
414 #undef NCONFIGS | |
415 } | |
416 | |
417 /* | |
418 * Produce the bytes for equivalence class "c". | |
419 * Currently only handles latin1, latin9 and utf-8. | |
420 * Emits bytes in postfix notation: 'a,b,NFA_OR,c,NFA_OR' is | |
421 * equivalent to 'a OR b OR c' | |
422 * | |
423 * NOTE! When changing this function, also update reg_equi_class() | |
424 */ | |
425 static int | |
426 nfa_emit_equi_class(c, neg) | |
427 int c; | |
428 int neg; | |
429 { | |
430 int first = TRUE; | |
431 int glue = neg == TRUE ? NFA_CONCAT : NFA_OR; | |
432 #define EMIT2(c) \ | |
433 EMIT(c); \ | |
434 if (neg == TRUE) { \ | |
435 EMIT(NFA_NOT); \ | |
436 } \ | |
437 if (first == FALSE) \ | |
438 EMIT(glue); \ | |
439 else \ | |
440 first = FALSE; \ | |
441 | |
442 #ifdef FEAT_MBYTE | |
443 if (enc_utf8 || STRCMP(p_enc, "latin1") == 0 | |
444 || STRCMP(p_enc, "iso-8859-15") == 0) | |
445 #endif | |
446 { | |
447 switch (c) | |
448 { | |
449 case 'A': case '\300': case '\301': case '\302': | |
450 case '\303': case '\304': case '\305': | |
451 EMIT2('A'); EMIT2('\300'); EMIT2('\301'); | |
452 EMIT2('\302'); EMIT2('\303'); EMIT2('\304'); | |
453 EMIT2('\305'); | |
454 return OK; | |
455 | |
456 case 'C': case '\307': | |
457 EMIT2('C'); EMIT2('\307'); | |
458 return OK; | |
459 | |
460 case 'E': case '\310': case '\311': case '\312': case '\313': | |
461 EMIT2('E'); EMIT2('\310'); EMIT2('\311'); | |
462 EMIT2('\312'); EMIT2('\313'); | |
463 return OK; | |
464 | |
465 case 'I': case '\314': case '\315': case '\316': case '\317': | |
466 EMIT2('I'); EMIT2('\314'); EMIT2('\315'); | |
467 EMIT2('\316'); EMIT2('\317'); | |
468 return OK; | |
469 | |
470 case 'N': case '\321': | |
471 EMIT2('N'); EMIT2('\321'); | |
472 return OK; | |
473 | |
474 case 'O': case '\322': case '\323': case '\324': case '\325': | |
475 case '\326': | |
476 EMIT2('O'); EMIT2('\322'); EMIT2('\323'); | |
477 EMIT2('\324'); EMIT2('\325'); EMIT2('\326'); | |
478 return OK; | |
479 | |
480 case 'U': case '\331': case '\332': case '\333': case '\334': | |
481 EMIT2('U'); EMIT2('\331'); EMIT2('\332'); | |
482 EMIT2('\333'); EMIT2('\334'); | |
483 return OK; | |
484 | |
485 case 'Y': case '\335': | |
486 EMIT2('Y'); EMIT2('\335'); | |
487 return OK; | |
488 | |
489 case 'a': case '\340': case '\341': case '\342': | |
490 case '\343': case '\344': case '\345': | |
491 EMIT2('a'); EMIT2('\340'); EMIT2('\341'); | |
492 EMIT2('\342'); EMIT2('\343'); EMIT2('\344'); | |
493 EMIT2('\345'); | |
494 return OK; | |
495 | |
496 case 'c': case '\347': | |
497 EMIT2('c'); EMIT2('\347'); | |
498 return OK; | |
499 | |
500 case 'e': case '\350': case '\351': case '\352': case '\353': | |
501 EMIT2('e'); EMIT2('\350'); EMIT2('\351'); | |
502 EMIT2('\352'); EMIT2('\353'); | |
503 return OK; | |
504 | |
505 case 'i': case '\354': case '\355': case '\356': case '\357': | |
506 EMIT2('i'); EMIT2('\354'); EMIT2('\355'); | |
507 EMIT2('\356'); EMIT2('\357'); | |
508 return OK; | |
509 | |
510 case 'n': case '\361': | |
511 EMIT2('n'); EMIT2('\361'); | |
512 return OK; | |
513 | |
514 case 'o': case '\362': case '\363': case '\364': case '\365': | |
515 case '\366': | |
516 EMIT2('o'); EMIT2('\362'); EMIT2('\363'); | |
517 EMIT2('\364'); EMIT2('\365'); EMIT2('\366'); | |
518 return OK; | |
519 | |
520 case 'u': case '\371': case '\372': case '\373': case '\374': | |
521 EMIT2('u'); EMIT2('\371'); EMIT2('\372'); | |
522 EMIT2('\373'); EMIT2('\374'); | |
523 return OK; | |
524 | |
525 case 'y': case '\375': case '\377': | |
526 EMIT2('y'); EMIT2('\375'); EMIT2('\377'); | |
527 return OK; | |
528 | |
529 default: | |
530 return FAIL; | |
531 } | |
532 } | |
533 | |
534 EMIT(c); | |
535 return OK; | |
536 #undef EMIT2 | |
537 } | |
538 | |
539 /* | |
540 * Code to parse regular expression. | |
541 * | |
542 * We try to reuse parsing functions in regexp.c to | |
543 * minimize surprise and keep the syntax consistent. | |
544 */ | |
545 | |
546 /* | |
547 * Increments the pointer "p" by one (multi-byte) character. | |
548 */ | |
549 static void | |
550 nfa_inc(p) | |
551 char_u **p; | |
552 { | |
553 #ifdef FEAT_MBYTE | |
554 if (has_mbyte) | |
555 mb_ptr2char_adv(p); | |
556 else | |
557 #endif | |
558 *p = *p + 1; | |
559 } | |
560 | |
561 /* | |
562 * Decrements the pointer "p" by one (multi-byte) character. | |
563 */ | |
564 static void | |
565 nfa_dec(p) | |
566 char_u **p; | |
567 { | |
568 #ifdef FEAT_MBYTE | |
569 char_u *p2, *oldp; | |
570 | |
571 if (has_mbyte) | |
572 { | |
573 oldp = *p; | |
574 /* Try to find the multibyte char that advances to the current | |
575 * position. */ | |
576 do | |
577 { | |
578 *p = *p - 1; | |
579 p2 = *p; | |
580 mb_ptr2char_adv(&p2); | |
581 } while (p2 != oldp); | |
582 } | |
583 #else | |
584 *p = *p - 1; | |
585 #endif | |
586 } | |
587 | |
588 /* | |
589 * Parse the lowest level. | |
590 * | |
591 * An atom can be one of a long list of items. Many atoms match one character | |
592 * in the text. It is often an ordinary character or a character class. | |
593 * Braces can be used to make a pattern into an atom. The "\z(\)" construct | |
594 * is only for syntax highlighting. | |
595 * | |
596 * atom ::= ordinary-atom | |
597 * or \( pattern \) | |
598 * or \%( pattern \) | |
599 * or \z( pattern \) | |
600 */ | |
601 static int | |
602 nfa_regatom() | |
603 { | |
604 int c; | |
605 int charclass; | |
606 int equiclass; | |
607 int collclass; | |
608 int got_coll_char; | |
609 char_u *p; | |
610 char_u *endp; | |
611 #ifdef FEAT_MBYTE | |
612 char_u *old_regparse = regparse; | |
613 int clen; | |
614 int len; | |
615 static char_u buf[30]; | |
616 int i; | |
617 #endif | |
618 int extra = 0; | |
619 int first; | |
620 int emit_range; | |
621 int negated; | |
622 int result; | |
623 int startc = -1; | |
624 int endc = -1; | |
625 int oldstartc = -1; | |
626 int cpo_lit; /* 'cpoptions' contains 'l' flag */ | |
627 int cpo_bsl; /* 'cpoptions' contains '\' flag */ | |
628 int glue; /* ID that will "glue" nodes together */ | |
629 | |
630 cpo_lit = vim_strchr(p_cpo, CPO_LITERAL) != NULL; | |
631 cpo_bsl = vim_strchr(p_cpo, CPO_BACKSL) != NULL; | |
632 | |
633 c = getchr(); | |
634 | |
635 #ifdef FEAT_MBYTE | |
636 /* clen has the length of the current char, without composing chars */ | |
637 clen = (*mb_char2len)(c); | |
638 if (has_mbyte && clen > 1) | |
639 goto nfa_do_multibyte; | |
640 #endif | |
641 switch (c) | |
642 { | |
643 case Magic('^'): | |
644 EMIT(NFA_BOL); | |
645 break; | |
646 | |
647 case Magic('$'): | |
648 EMIT(NFA_EOL); | |
649 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
650 had_eol = TRUE; | |
651 #endif | |
652 break; | |
653 | |
654 case Magic('<'): | |
655 EMIT(NFA_BOW); | |
656 break; | |
657 | |
658 case Magic('>'): | |
659 EMIT(NFA_EOW); | |
660 break; | |
661 | |
662 case Magic('_'): | |
663 c = no_Magic(getchr()); | |
664 if (c == '^') /* "\_^" is start-of-line */ | |
665 { | |
666 EMIT(NFA_BOL); | |
667 break; | |
668 } | |
669 if (c == '$') /* "\_$" is end-of-line */ | |
670 { | |
671 EMIT(NFA_EOL); | |
672 #if defined(FEAT_SYN_HL) || defined(PROTO) | |
673 had_eol = TRUE; | |
674 #endif | |
675 break; | |
676 } | |
677 | |
678 extra = ADD_NL; | |
679 | |
680 /* "\_[" is collection plus newline */ | |
681 if (c == '[') | |
682 /* TODO: make this work | |
683 * goto collection; */ | |
684 return FAIL; | |
685 | |
686 /* "\_x" is character class plus newline */ | |
687 /*FALLTHROUGH*/ | |
688 | |
689 /* | |
690 * Character classes. | |
691 */ | |
692 case Magic('.'): | |
693 case Magic('i'): | |
694 case Magic('I'): | |
695 case Magic('k'): | |
696 case Magic('K'): | |
697 case Magic('f'): | |
698 case Magic('F'): | |
699 case Magic('p'): | |
700 case Magic('P'): | |
701 case Magic('s'): | |
702 case Magic('S'): | |
703 case Magic('d'): | |
704 case Magic('D'): | |
705 case Magic('x'): | |
706 case Magic('X'): | |
707 case Magic('o'): | |
708 case Magic('O'): | |
709 case Magic('w'): | |
710 case Magic('W'): | |
711 case Magic('h'): | |
712 case Magic('H'): | |
713 case Magic('a'): | |
714 case Magic('A'): | |
715 case Magic('l'): | |
716 case Magic('L'): | |
717 case Magic('u'): | |
718 case Magic('U'): | |
719 p = vim_strchr(classchars, no_Magic(c)); | |
720 if (p == NULL) | |
721 { | |
722 return FAIL; /* runtime error */ | |
723 } | |
724 #ifdef FEAT_MBYTE | |
725 /* When '.' is followed by a composing char ignore the dot, so that | |
726 * the composing char is matched here. */ | |
727 if (enc_utf8 && c == Magic('.') && utf_iscomposing(peekchr())) | |
728 { | |
729 c = getchr(); | |
730 goto nfa_do_multibyte; | |
731 } | |
732 #endif | |
733 EMIT(nfa_classcodes[p - classchars]); | |
734 if (extra == ADD_NL) | |
735 { | |
736 EMIT(NFA_NEWL); | |
737 EMIT(NFA_OR); | |
738 regflags |= RF_HASNL; | |
739 } | |
740 break; | |
741 | |
742 case Magic('n'): | |
743 if (reg_string) | |
744 /* In a string "\n" matches a newline character. */ | |
745 EMIT(NL); | |
746 else | |
747 { | |
748 /* In buffer text "\n" matches the end of a line. */ | |
749 EMIT(NFA_NEWL); | |
750 regflags |= RF_HASNL; | |
751 } | |
752 break; | |
753 | |
754 case Magic('('): | |
755 if (nfa_reg(REG_PAREN) == FAIL) | |
756 return FAIL; /* cascaded error */ | |
757 break; | |
758 | |
759 case NUL: | |
760 syntax_error = TRUE; | |
761 EMSG_RET_FAIL(_("E865: (NFA) Regexp end encountered prematurely")); | |
762 | |
763 case Magic('|'): | |
764 case Magic('&'): | |
765 case Magic(')'): | |
766 syntax_error = TRUE; | |
4450 | 767 EMSGN(_(e_misplaced), no_Magic(c)); |
4444 | 768 return FAIL; |
769 | |
770 case Magic('='): | |
771 case Magic('?'): | |
772 case Magic('+'): | |
773 case Magic('@'): | |
774 case Magic('*'): | |
775 case Magic('{'): | |
776 /* these should follow an atom, not form an atom */ | |
777 syntax_error = TRUE; | |
4450 | 778 EMSGN(_(e_misplaced), no_Magic(c)); |
4444 | 779 return FAIL; |
780 | |
781 case Magic('~'): /* previous substitute pattern */ | |
782 /* Not supported yet */ | |
783 return FAIL; | |
784 | |
785 case Magic('1'): | |
786 case Magic('2'): | |
787 case Magic('3'): | |
788 case Magic('4'): | |
789 case Magic('5'): | |
790 case Magic('6'): | |
791 case Magic('7'): | |
792 case Magic('8'): | |
793 case Magic('9'): | |
794 /* not supported yet */ | |
795 return FAIL; | |
796 | |
797 case Magic('z'): | |
798 c = no_Magic(getchr()); | |
799 switch (c) | |
800 { | |
801 case 's': | |
802 EMIT(NFA_ZSTART); | |
803 break; | |
804 case 'e': | |
805 EMIT(NFA_ZEND); | |
806 nfa_has_zend = TRUE; | |
807 /* TODO: Currently \ze does not work properly. */ | |
808 return FAIL; | |
809 /* break; */ | |
810 case '1': | |
811 case '2': | |
812 case '3': | |
813 case '4': | |
814 case '5': | |
815 case '6': | |
816 case '7': | |
817 case '8': | |
818 case '9': | |
819 case '(': | |
820 /* \z1...\z9 and \z( not yet supported */ | |
821 return FAIL; | |
822 default: | |
823 syntax_error = TRUE; | |
4450 | 824 EMSGN(_("E867: (NFA) Unknown operator '\\z%c'"), |
4444 | 825 no_Magic(c)); |
826 return FAIL; | |
827 } | |
828 break; | |
829 | |
830 case Magic('%'): | |
831 c = no_Magic(getchr()); | |
832 switch (c) | |
833 { | |
834 /* () without a back reference */ | |
835 case '(': | |
836 if (nfa_reg(REG_NPAREN) == FAIL) | |
837 return FAIL; | |
838 EMIT(NFA_NOPEN); | |
839 break; | |
840 | |
841 case 'd': /* %d123 decimal */ | |
842 case 'o': /* %o123 octal */ | |
843 case 'x': /* %xab hex 2 */ | |
844 case 'u': /* %uabcd hex 4 */ | |
845 case 'U': /* %U1234abcd hex 8 */ | |
846 /* Not yet supported */ | |
847 return FAIL; | |
848 | |
849 c = coll_get_char(); | |
850 #ifdef FEAT_MBYTE | |
851 if ((*mb_char2len)(c) > 1) | |
852 { | |
853 EMIT_MBYTE(c); | |
854 } | |
855 else | |
856 #endif | |
857 EMIT(c); | |
858 break; | |
859 | |
860 /* Catch \%^ and \%$ regardless of where they appear in the | |
861 * pattern -- regardless of whether or not it makes sense. */ | |
862 case '^': | |
863 EMIT(NFA_BOF); | |
864 /* Not yet supported */ | |
865 return FAIL; | |
866 break; | |
867 | |
868 case '$': | |
869 EMIT(NFA_EOF); | |
870 /* Not yet supported */ | |
871 return FAIL; | |
872 break; | |
873 | |
874 case '#': | |
875 /* not supported yet */ | |
876 return FAIL; | |
877 break; | |
878 | |
879 case 'V': | |
880 /* not supported yet */ | |
881 return FAIL; | |
882 break; | |
883 | |
884 case '[': | |
885 /* \%[abc] not supported yet */ | |
886 return FAIL; | |
887 | |
888 default: | |
889 /* not supported yet */ | |
890 return FAIL; | |
891 } | |
892 break; | |
893 | |
894 /* collection: */ | |
895 case Magic('['): | |
896 /* | |
897 * Glue is emitted between several atoms from the []. | |
898 * It is either NFA_OR, or NFA_CONCAT. | |
899 * | |
900 * [abc] expands to 'a b NFA_OR c NFA_OR' (in postfix notation) | |
901 * [^abc] expands to 'a NFA_NOT b NFA_NOT NFA_CONCAT c NFA_NOT | |
902 * NFA_CONCAT NFA_END_NEG_RANGE NFA_CONCAT' (in postfix | |
903 * notation) | |
904 * | |
905 */ | |
906 | |
907 | |
908 /* Emit negation atoms, if needed. | |
909 * The CONCAT below merges the NOT with the previous node. */ | |
910 #define TRY_NEG() \ | |
911 if (negated == TRUE) \ | |
912 { \ | |
913 EMIT(NFA_NOT); \ | |
914 } | |
915 | |
916 /* Emit glue between important nodes : CONCAT or OR. */ | |
917 #define EMIT_GLUE() \ | |
918 if (first == FALSE) \ | |
919 EMIT(glue); \ | |
920 else \ | |
921 first = FALSE; | |
922 | |
923 p = regparse; | |
924 endp = skip_anyof(p); | |
925 if (*endp == ']') | |
926 { | |
927 /* | |
928 * Try to reverse engineer character classes. For example, | |
929 * recognize that [0-9] stands for \d and [A-Za-z_] with \h, | |
930 * and perform the necessary substitutions in the NFA. | |
931 */ | |
932 result = nfa_recognize_char_class(regparse, endp, | |
933 extra == ADD_NL); | |
934 if (result != FAIL) | |
935 { | |
936 if (result >= NFA_DIGIT && result <= NFA_NUPPER) | |
937 EMIT(result); | |
938 else /* must be char class + newline */ | |
939 { | |
940 EMIT(result - ADD_NL); | |
941 EMIT(NFA_NEWL); | |
942 EMIT(NFA_OR); | |
943 } | |
944 regparse = endp; | |
945 nfa_inc(®parse); | |
946 return OK; | |
947 } | |
948 /* | |
949 * Failed to recognize a character class. Use the simple | |
950 * version that turns [abc] into 'a' OR 'b' OR 'c' | |
951 */ | |
952 startc = endc = oldstartc = -1; | |
953 first = TRUE; /* Emitting first atom in this sequence? */ | |
954 negated = FALSE; | |
955 glue = NFA_OR; | |
956 if (*regparse == '^') /* negated range */ | |
957 { | |
958 negated = TRUE; | |
959 glue = NFA_CONCAT; | |
960 nfa_inc(®parse); | |
961 } | |
962 if (*regparse == '-') | |
963 { | |
964 startc = '-'; | |
965 EMIT(startc); | |
966 TRY_NEG(); | |
967 EMIT_GLUE(); | |
968 nfa_inc(®parse); | |
969 } | |
970 /* Emit the OR branches for each character in the [] */ | |
971 emit_range = FALSE; | |
972 while (regparse < endp) | |
973 { | |
974 oldstartc = startc; | |
975 startc = -1; | |
976 got_coll_char = FALSE; | |
977 if (*regparse == '[') | |
978 { | |
979 /* Check for [: :], [= =], [. .] */ | |
980 equiclass = collclass = 0; | |
981 charclass = get_char_class(®parse); | |
982 if (charclass == CLASS_NONE) | |
983 { | |
984 equiclass = get_equi_class(®parse); | |
985 if (equiclass == 0) | |
986 collclass = get_coll_element(®parse); | |
987 } | |
988 | |
989 /* Character class like [:alpha:] */ | |
990 if (charclass != CLASS_NONE) | |
991 { | |
992 switch (charclass) | |
993 { | |
994 case CLASS_ALNUM: | |
995 EMIT(NFA_CLASS_ALNUM); | |
996 break; | |
997 case CLASS_ALPHA: | |
998 EMIT(NFA_CLASS_ALPHA); | |
999 break; | |
1000 case CLASS_BLANK: | |
1001 EMIT(NFA_CLASS_BLANK); | |
1002 break; | |
1003 case CLASS_CNTRL: | |
1004 EMIT(NFA_CLASS_CNTRL); | |
1005 break; | |
1006 case CLASS_DIGIT: | |
1007 EMIT(NFA_CLASS_DIGIT); | |
1008 break; | |
1009 case CLASS_GRAPH: | |
1010 EMIT(NFA_CLASS_GRAPH); | |
1011 break; | |
1012 case CLASS_LOWER: | |
1013 EMIT(NFA_CLASS_LOWER); | |
1014 break; | |
1015 case CLASS_PRINT: | |
1016 EMIT(NFA_CLASS_PRINT); | |
1017 break; | |
1018 case CLASS_PUNCT: | |
1019 EMIT(NFA_CLASS_PUNCT); | |
1020 break; | |
1021 case CLASS_SPACE: | |
1022 EMIT(NFA_CLASS_SPACE); | |
1023 break; | |
1024 case CLASS_UPPER: | |
1025 EMIT(NFA_CLASS_UPPER); | |
1026 break; | |
1027 case CLASS_XDIGIT: | |
1028 EMIT(NFA_CLASS_XDIGIT); | |
1029 break; | |
1030 case CLASS_TAB: | |
1031 EMIT(NFA_CLASS_TAB); | |
1032 break; | |
1033 case CLASS_RETURN: | |
1034 EMIT(NFA_CLASS_RETURN); | |
1035 break; | |
1036 case CLASS_BACKSPACE: | |
1037 EMIT(NFA_CLASS_BACKSPACE); | |
1038 break; | |
1039 case CLASS_ESCAPE: | |
1040 EMIT(NFA_CLASS_ESCAPE); | |
1041 break; | |
1042 } | |
1043 TRY_NEG(); | |
1044 EMIT_GLUE(); | |
1045 continue; | |
1046 } | |
1047 /* Try equivalence class [=a=] and the like */ | |
1048 if (equiclass != 0) | |
1049 { | |
1050 result = nfa_emit_equi_class(equiclass, negated); | |
1051 if (result == FAIL) | |
1052 { | |
1053 /* should never happen */ | |
1054 EMSG_RET_FAIL(_("E868: Error building NFA with equivalence class!")); | |
1055 } | |
1056 EMIT_GLUE(); | |
1057 continue; | |
1058 } | |
1059 /* Try collating class like [. .] */ | |
1060 if (collclass != 0) | |
1061 { | |
1062 startc = collclass; /* allow [.a.]-x as a range */ | |
1063 /* Will emit the proper atom at the end of the | |
1064 * while loop. */ | |
1065 } | |
1066 } | |
1067 /* Try a range like 'a-x' or '\t-z' */ | |
1068 if (*regparse == '-') | |
1069 { | |
1070 emit_range = TRUE; | |
1071 startc = oldstartc; | |
1072 nfa_inc(®parse); | |
1073 continue; /* reading the end of the range */ | |
1074 } | |
1075 | |
1076 /* Now handle simple and escaped characters. | |
1077 * Only "\]", "\^", "\]" and "\\" are special in Vi. Vim | |
1078 * accepts "\t", "\e", etc., but only when the 'l' flag in | |
1079 * 'cpoptions' is not included. | |
1080 * Posix doesn't recognize backslash at all. | |
1081 */ | |
1082 if (*regparse == '\\' | |
1083 && !cpo_bsl | |
1084 && regparse + 1 <= endp | |
1085 && (vim_strchr(REGEXP_INRANGE, regparse[1]) != NULL | |
1086 || (!cpo_lit | |
1087 && vim_strchr(REGEXP_ABBR, regparse[1]) | |
1088 != NULL) | |
1089 ) | |
1090 ) | |
1091 { | |
1092 nfa_inc(®parse); | |
1093 | |
4507
9dbbddb2ed10
updated for version 7.3.1001
Bram Moolenaar <bram@vim.org>
parents:
4503
diff
changeset
|
1094 if (*regparse == 'n') |
4444 | 1095 startc = reg_string ? NL : NFA_NEWL; |
1096 else | |
1097 if (*regparse == 'd' | |
1098 || *regparse == 'o' | |
1099 || *regparse == 'x' | |
1100 || *regparse == 'u' | |
1101 || *regparse == 'U' | |
1102 ) | |
1103 { | |
1104 /* TODO(RE) This needs more testing */ | |
1105 startc = coll_get_char(); | |
1106 got_coll_char = TRUE; | |
1107 nfa_dec(®parse); | |
1108 } | |
1109 else | |
1110 { | |
1111 /* \r,\t,\e,\b */ | |
1112 startc = backslash_trans(*regparse); | |
1113 } | |
1114 } | |
1115 | |
1116 /* Normal printable char */ | |
1117 if (startc == -1) | |
1118 #ifdef FEAT_MBYTE | |
1119 startc = (*mb_ptr2char)(regparse); | |
1120 #else | |
1121 startc = *regparse; | |
1122 #endif | |
1123 | |
1124 /* Previous char was '-', so this char is end of range. */ | |
1125 if (emit_range) | |
1126 { | |
1127 endc = startc; startc = oldstartc; | |
1128 if (startc > endc) | |
1129 EMSG_RET_FAIL(_(e_invrange)); | |
1130 #ifdef FEAT_MBYTE | |
1131 if (has_mbyte && ((*mb_char2len)(startc) > 1 | |
1132 || (*mb_char2len)(endc) > 1)) | |
1133 { | |
1134 if (endc > startc + 256) | |
1135 EMSG_RET_FAIL(_(e_invrange)); | |
1136 /* Emit the range. "startc" was already emitted, so | |
1137 * skip it. */ | |
1138 for (c = startc + 1; c <= endc; c++) | |
1139 { | |
1140 if ((*mb_char2len)(c) > 1) | |
1141 { | |
1142 EMIT_MBYTE(c); | |
1143 } | |
1144 else | |
1145 EMIT(c); | |
1146 TRY_NEG(); | |
1147 EMIT_GLUE(); | |
1148 } | |
1149 emit_range = FALSE; | |
1150 } | |
1151 else | |
1152 #endif | |
1153 { | |
1154 #ifdef EBCDIC | |
1155 int alpha_only = FALSE; | |
1156 | |
1157 /* for alphabetical range skip the gaps | |
1158 * 'i'-'j', 'r'-'s', 'I'-'J' and 'R'-'S'. */ | |
1159 if (isalpha(startc) && isalpha(endc)) | |
1160 alpha_only = TRUE; | |
1161 #endif | |
1162 /* Emit the range. "startc" was already emitted, so | |
1163 * skip it. */ | |
1164 for (c = startc + 1; c <= endc; c++) | |
1165 #ifdef EBCDIC | |
1166 if (!alpha_only || isalpha(startc)) | |
1167 #endif | |
1168 { | |
1169 EMIT(c); | |
1170 TRY_NEG(); | |
1171 EMIT_GLUE(); | |
1172 } | |
1173 emit_range = FALSE; | |
1174 } | |
1175 } | |
1176 else | |
1177 { | |
1178 /* | |
1179 * This char (startc) is not part of a range. Just | |
1180 * emit it. | |
1181 * | |
1182 * Normally, simply emit startc. But if we get char | |
1183 * code=0 from a collating char, then replace it with | |
1184 * 0x0a. | |
1185 * | |
1186 * This is needed to completely mimic the behaviour of | |
1187 * the backtracking engine. | |
1188 */ | |
1189 if (got_coll_char == TRUE && startc == 0) | |
1190 EMIT(0x0a); | |
1191 else | |
1192 #ifdef FEAT_MBYTE | |
1193 if ((*mb_char2len)(startc) > 1) | |
1194 { | |
1195 EMIT_MBYTE(startc); | |
1196 } | |
1197 else | |
1198 #endif | |
1199 EMIT(startc); | |
1200 TRY_NEG(); | |
1201 EMIT_GLUE(); | |
1202 } | |
1203 | |
1204 nfa_inc(®parse); | |
1205 } /* while (p < endp) */ | |
1206 | |
1207 nfa_dec(®parse); | |
1208 if (*regparse == '-') /* if last, '-' is just a char */ | |
1209 { | |
1210 EMIT('-'); | |
1211 TRY_NEG(); | |
1212 EMIT_GLUE(); | |
1213 } | |
1214 nfa_inc(®parse); | |
1215 | |
1216 if (extra == ADD_NL) /* \_[] also matches \n */ | |
1217 { | |
1218 EMIT(reg_string ? NL : NFA_NEWL); | |
1219 TRY_NEG(); | |
1220 EMIT_GLUE(); | |
1221 } | |
1222 | |
1223 /* skip the trailing ] */ | |
1224 regparse = endp; | |
1225 nfa_inc(®parse); | |
1226 if (negated == TRUE) | |
1227 { | |
1228 /* Mark end of negated char range */ | |
1229 EMIT(NFA_END_NEG_RANGE); | |
1230 EMIT(NFA_CONCAT); | |
1231 } | |
1232 return OK; | |
1233 } /* if exists closing ] */ | |
1234 else if (reg_strict) | |
1235 { | |
1236 syntax_error = TRUE; | |
1237 EMSG_RET_FAIL(_(e_missingbracket)); | |
1238 } | |
1239 | |
1240 /* FALLTHROUGH */ | |
1241 default: | |
1242 { | |
1243 #ifdef FEAT_MBYTE | |
1244 int plen; | |
1245 | |
1246 nfa_do_multibyte: | |
1247 /* length of current char, with composing chars, | |
1248 * from pointer */ | |
1249 plen = (*mb_ptr2len)(old_regparse); | |
1250 if (enc_utf8 && clen != plen) | |
1251 { | |
1252 /* A composing character is always handled as a | |
1253 * separate atom, surrounded by NFA_COMPOSING and | |
1254 * NFA_END_COMPOSING. Note that right now we are | |
1255 * building the postfix form, not the NFA itself; | |
1256 * a composing char could be: a, b, c, NFA_COMPOSING | |
1257 * where 'a', 'b', 'c' are chars with codes > 256. | |
1258 */ | |
1259 EMIT_COMPOSING_UTF(old_regparse); | |
1260 regparse = old_regparse + plen; | |
1261 } | |
1262 else | |
1263 /* A multi-byte character is always handled as a | |
1264 * separate atom, surrounded by NFA_MULTIBYTE and | |
1265 * NFA_END_MULTIBYTE */ | |
1266 if (plen > 1) | |
1267 { | |
1268 EMIT_MBYTE(c); | |
1269 } | |
1270 else | |
1271 #endif | |
1272 { | |
1273 c = no_Magic(c); | |
1274 EMIT(c); | |
1275 } | |
1276 return OK; | |
1277 } | |
1278 } | |
1279 | |
1280 #undef TRY_NEG | |
1281 #undef EMIT_GLUE | |
1282 | |
1283 return OK; | |
1284 } | |
1285 | |
1286 /* | |
1287 * Parse something followed by possible [*+=]. | |
1288 * | |
1289 * A piece is an atom, possibly followed by a multi, an indication of how many | |
1290 * times the atom can be matched. Example: "a*" matches any sequence of "a" | |
1291 * characters: "", "a", "aa", etc. | |
1292 * | |
1293 * piece ::= atom | |
1294 * or atom multi | |
1295 */ | |
1296 static int | |
1297 nfa_regpiece() | |
1298 { | |
1299 int i; | |
1300 int op; | |
1301 int ret; | |
1302 long minval, maxval; | |
1303 int greedy = TRUE; /* Braces are prefixed with '-' ? */ | |
1304 char_u *old_regparse, *new_regparse; | |
1305 int c2; | |
1306 int *old_post_ptr, *my_post_start; | |
1307 int old_regnpar; | |
1308 int quest; | |
1309 | |
1310 /* Save the current position in the regexp, so that we can use it if | |
1311 * <atom>{m,n} is next. */ | |
1312 old_regparse = regparse; | |
1313 /* Save current number of open parenthesis, so we can use it if | |
1314 * <atom>{m,n} is next */ | |
1315 old_regnpar = regnpar; | |
1316 /* store current pos in the postfix form, for \{m,n} involving 0s */ | |
1317 my_post_start = post_ptr; | |
1318 | |
1319 ret = nfa_regatom(); | |
1320 if (ret == FAIL) | |
1321 return FAIL; /* cascaded error */ | |
1322 | |
1323 op = peekchr(); | |
1324 if (re_multi_type(op) == NOT_MULTI) | |
1325 return OK; | |
1326 | |
1327 skipchr(); | |
1328 switch (op) | |
1329 { | |
1330 case Magic('*'): | |
1331 EMIT(NFA_STAR); | |
1332 break; | |
1333 | |
1334 case Magic('+'): | |
1335 /* | |
1336 * Trick: Normally, (a*)\+ would match the whole input "aaa". The | |
1337 * first and only submatch would be "aaa". But the backtracking | |
1338 * engine interprets the plus as "try matching one more time", and | |
1339 * a* matches a second time at the end of the input, the empty | |
1340 * string. | |
1341 * The submatch will the empty string. | |
1342 * | |
1343 * In order to be consistent with the old engine, we disable | |
1344 * NFA_PLUS, and replace <atom>+ with <atom><atom>* | |
1345 */ | |
1346 /* EMIT(NFA_PLUS); */ | |
1347 regnpar = old_regnpar; | |
1348 regparse = old_regparse; | |
1349 curchr = -1; | |
1350 if (nfa_regatom() == FAIL) | |
1351 return FAIL; | |
1352 EMIT(NFA_STAR); | |
1353 EMIT(NFA_CONCAT); | |
1354 skipchr(); /* skip the \+ */ | |
1355 break; | |
1356 | |
1357 case Magic('@'): | |
1358 op = no_Magic(getchr()); | |
1359 switch(op) | |
1360 { | |
1361 case '=': | |
1362 EMIT(NFA_PREV_ATOM_NO_WIDTH); | |
1363 break; | |
1364 case '!': | |
1365 case '<': | |
1366 case '>': | |
1367 /* Not supported yet */ | |
1368 return FAIL; | |
1369 default: | |
1370 syntax_error = TRUE; | |
4450 | 1371 EMSGN(_("E869: (NFA) Unknown operator '\\@%c'"), op); |
4444 | 1372 return FAIL; |
1373 } | |
1374 break; | |
1375 | |
1376 case Magic('?'): | |
1377 case Magic('='): | |
1378 EMIT(NFA_QUEST); | |
1379 break; | |
1380 | |
1381 case Magic('{'): | |
1382 /* a{2,5} will expand to 'aaa?a?a?' | |
1383 * a{-1,3} will expand to 'aa??a??', where ?? is the nongreedy | |
1384 * version of '?' | |
1385 * \v(ab){2,3} will expand to '(ab)(ab)(ab)?', where all the | |
1386 * parenthesis have the same id | |
1387 */ | |
1388 | |
1389 greedy = TRUE; | |
1390 c2 = peekchr(); | |
1391 if (c2 == '-' || c2 == Magic('-')) | |
1392 { | |
1393 skipchr(); | |
1394 greedy = FALSE; | |
1395 } | |
1396 if (!read_limits(&minval, &maxval)) | |
1397 { | |
1398 syntax_error = TRUE; | |
1399 EMSG_RET_FAIL(_("E870: (NFA regexp) Error reading repetition limits")); | |
1400 } | |
1401 /* <atom>{0,inf}, <atom>{0,} and <atom>{} are equivalent to | |
1402 * <atom>* */ | |
1403 if (minval == 0 && maxval == MAX_LIMIT && greedy) | |
1404 { | |
1405 EMIT(NFA_STAR); | |
1406 break; | |
1407 } | |
1408 | |
1409 if (maxval > NFA_BRACES_MAXLIMIT) | |
1410 { | |
1411 /* This would yield a huge automaton and use too much memory. | |
1412 * Revert to old engine */ | |
1413 return FAIL; | |
1414 } | |
1415 | |
1416 /* Special case: x{0} or x{-0} */ | |
1417 if (maxval == 0) | |
1418 { | |
1419 /* Ignore result of previous call to nfa_regatom() */ | |
1420 post_ptr = my_post_start; | |
1421 /* NFA_SKIP_CHAR has 0-length and works everywhere */ | |
1422 EMIT(NFA_SKIP_CHAR); | |
1423 return OK; | |
1424 } | |
1425 | |
1426 /* Ignore previous call to nfa_regatom() */ | |
1427 post_ptr = my_post_start; | |
1428 /* Save pos after the repeated atom and the \{} */ | |
1429 new_regparse = regparse; | |
1430 | |
1431 new_regparse = regparse; | |
1432 quest = (greedy == TRUE? NFA_QUEST : NFA_QUEST_NONGREEDY); | |
1433 for (i = 0; i < maxval; i++) | |
1434 { | |
1435 /* Goto beginning of the repeated atom */ | |
1436 regparse = old_regparse; | |
1437 curchr = -1; | |
1438 /* Restore count of parenthesis */ | |
1439 regnpar = old_regnpar; | |
1440 old_post_ptr = post_ptr; | |
1441 if (nfa_regatom() == FAIL) | |
1442 return FAIL; | |
1443 /* after "minval" times, atoms are optional */ | |
1444 if (i + 1 > minval) | |
1445 EMIT(quest); | |
1446 if (old_post_ptr != my_post_start) | |
1447 EMIT(NFA_CONCAT); | |
1448 } | |
1449 | |
1450 /* Go to just after the repeated atom and the \{} */ | |
1451 regparse = new_regparse; | |
1452 curchr = -1; | |
1453 | |
1454 break; | |
1455 | |
1456 | |
1457 default: | |
1458 break; | |
1459 } /* end switch */ | |
1460 | |
1461 if (re_multi_type(peekchr()) != NOT_MULTI) | |
1462 { | |
1463 /* Can't have a multi follow a multi. */ | |
1464 syntax_error = TRUE; | |
1465 EMSG_RET_FAIL(_("E871: (NFA regexp) Can't have a multi follow a multi !")); | |
1466 } | |
1467 | |
1468 return OK; | |
1469 } | |
1470 | |
1471 /* | |
1472 * Parse one or more pieces, concatenated. It matches a match for the | |
1473 * first piece, followed by a match for the second piece, etc. Example: | |
1474 * "f[0-9]b", first matches "f", then a digit and then "b". | |
1475 * | |
1476 * concat ::= piece | |
1477 * or piece piece | |
1478 * or piece piece piece | |
1479 * etc. | |
1480 */ | |
1481 static int | |
1482 nfa_regconcat() | |
1483 { | |
1484 int cont = TRUE; | |
1485 int first = TRUE; | |
1486 | |
1487 while (cont) | |
1488 { | |
1489 switch (peekchr()) | |
1490 { | |
1491 case NUL: | |
1492 case Magic('|'): | |
1493 case Magic('&'): | |
1494 case Magic(')'): | |
1495 cont = FALSE; | |
1496 break; | |
1497 | |
1498 case Magic('Z'): | |
1499 #ifdef FEAT_MBYTE | |
1500 regflags |= RF_ICOMBINE; | |
1501 #endif | |
1502 skipchr_keepstart(); | |
1503 break; | |
1504 case Magic('c'): | |
1505 regflags |= RF_ICASE; | |
1506 skipchr_keepstart(); | |
1507 break; | |
1508 case Magic('C'): | |
1509 regflags |= RF_NOICASE; | |
1510 skipchr_keepstart(); | |
1511 break; | |
1512 case Magic('v'): | |
1513 reg_magic = MAGIC_ALL; | |
1514 skipchr_keepstart(); | |
1515 curchr = -1; | |
1516 break; | |
1517 case Magic('m'): | |
1518 reg_magic = MAGIC_ON; | |
1519 skipchr_keepstart(); | |
1520 curchr = -1; | |
1521 break; | |
1522 case Magic('M'): | |
1523 reg_magic = MAGIC_OFF; | |
1524 skipchr_keepstart(); | |
1525 curchr = -1; | |
1526 break; | |
1527 case Magic('V'): | |
1528 reg_magic = MAGIC_NONE; | |
1529 skipchr_keepstart(); | |
1530 curchr = -1; | |
1531 break; | |
1532 | |
1533 default: | |
1534 if (nfa_regpiece() == FAIL) | |
1535 return FAIL; | |
1536 if (first == FALSE) | |
1537 EMIT(NFA_CONCAT); | |
1538 else | |
1539 first = FALSE; | |
1540 break; | |
1541 } | |
1542 } | |
1543 | |
1544 return OK; | |
1545 } | |
1546 | |
1547 /* | |
1548 * Parse a branch, one or more concats, separated by "\&". It matches the | |
1549 * last concat, but only if all the preceding concats also match at the same | |
1550 * position. Examples: | |
1551 * "foobeep\&..." matches "foo" in "foobeep". | |
1552 * ".*Peter\&.*Bob" matches in a line containing both "Peter" and "Bob" | |
1553 * | |
1554 * branch ::= concat | |
1555 * or concat \& concat | |
1556 * or concat \& concat \& concat | |
1557 * etc. | |
1558 */ | |
1559 static int | |
1560 nfa_regbranch() | |
1561 { | |
1562 int ch; | |
1563 int *old_post_ptr; | |
1564 | |
1565 old_post_ptr = post_ptr; | |
1566 | |
1567 /* First branch, possibly the only one */ | |
1568 if (nfa_regconcat() == FAIL) | |
1569 return FAIL; | |
1570 | |
1571 ch = peekchr(); | |
1572 /* Try next concats */ | |
1573 while (ch == Magic('&')) | |
1574 { | |
1575 skipchr(); | |
1576 EMIT(NFA_NOPEN); | |
1577 EMIT(NFA_PREV_ATOM_NO_WIDTH); | |
1578 old_post_ptr = post_ptr; | |
1579 if (nfa_regconcat() == FAIL) | |
1580 return FAIL; | |
1581 /* if concat is empty, skip a input char. But do emit a node */ | |
1582 if (old_post_ptr == post_ptr) | |
1583 EMIT(NFA_SKIP_CHAR); | |
1584 EMIT(NFA_CONCAT); | |
1585 ch = peekchr(); | |
1586 } | |
1587 | |
1588 /* Even if a branch is empty, emit one node for it */ | |
1589 if (old_post_ptr == post_ptr) | |
1590 EMIT(NFA_SKIP_CHAR); | |
1591 | |
1592 return OK; | |
1593 } | |
1594 | |
1595 /* | |
1596 * Parse a pattern, one or more branches, separated by "\|". It matches | |
1597 * anything that matches one of the branches. Example: "foo\|beep" matches | |
1598 * "foo" and matches "beep". If more than one branch matches, the first one | |
1599 * is used. | |
1600 * | |
1601 * pattern ::= branch | |
1602 * or branch \| branch | |
1603 * or branch \| branch \| branch | |
1604 * etc. | |
1605 */ | |
1606 static int | |
1607 nfa_reg(paren) | |
1608 int paren; /* REG_NOPAREN, REG_PAREN, REG_NPAREN or REG_ZPAREN */ | |
1609 { | |
1610 int parno = 0; | |
1611 | |
1612 #ifdef FEAT_SYN_HL | |
1613 #endif | |
1614 if (paren == REG_PAREN) | |
1615 { | |
1616 if (regnpar >= NSUBEXP) /* Too many `(' */ | |
1617 { | |
1618 syntax_error = TRUE; | |
1619 EMSG_RET_FAIL(_("E872: (NFA regexp) Too many '('")); | |
1620 } | |
1621 parno = regnpar++; | |
1622 } | |
1623 | |
1624 if (nfa_regbranch() == FAIL) | |
1625 return FAIL; /* cascaded error */ | |
1626 | |
1627 while (peekchr() == Magic('|')) | |
1628 { | |
1629 skipchr(); | |
1630 if (nfa_regbranch() == FAIL) | |
1631 return FAIL; /* cascaded error */ | |
1632 EMIT(NFA_OR); | |
1633 } | |
1634 | |
1635 /* Check for proper termination. */ | |
1636 if (paren != REG_NOPAREN && getchr() != Magic(')')) | |
1637 { | |
1638 syntax_error = TRUE; | |
1639 if (paren == REG_NPAREN) | |
1640 EMSG2_RET_FAIL(_(e_unmatchedpp), reg_magic == MAGIC_ALL); | |
1641 else | |
1642 EMSG2_RET_FAIL(_(e_unmatchedp), reg_magic == MAGIC_ALL); | |
1643 } | |
1644 else if (paren == REG_NOPAREN && peekchr() != NUL) | |
1645 { | |
1646 syntax_error = TRUE; | |
1647 if (peekchr() == Magic(')')) | |
1648 EMSG2_RET_FAIL(_(e_unmatchedpar), reg_magic == MAGIC_ALL); | |
1649 else | |
1650 EMSG_RET_FAIL(_("E873: (NFA regexp) proper termination error")); | |
1651 } | |
1652 /* | |
1653 * Here we set the flag allowing back references to this set of | |
1654 * parentheses. | |
1655 */ | |
1656 if (paren == REG_PAREN) | |
1657 { | |
1658 had_endbrace[parno] = TRUE; /* have seen the close paren */ | |
1659 EMIT(NFA_MOPEN + parno); | |
1660 } | |
1661 | |
1662 return OK; | |
1663 } | |
1664 | |
1665 typedef struct | |
1666 { | |
1667 char_u *start[NSUBEXP]; | |
1668 char_u *end[NSUBEXP]; | |
1669 lpos_T startpos[NSUBEXP]; | |
1670 lpos_T endpos[NSUBEXP]; | |
1671 } regsub_T; | |
1672 | |
1673 static int nfa_regmatch __ARGS((nfa_state_T *start, regsub_T *submatch, regsub_T *m)); | |
1674 | |
1675 #ifdef DEBUG | |
1676 static char_u code[50]; | |
1677 | |
1678 static void | |
1679 nfa_set_code(c) | |
1680 int c; | |
1681 { | |
1682 int addnl = FALSE; | |
1683 | |
1684 if (c >= NFA_FIRST_NL && c <= NFA_LAST_NL) | |
1685 { | |
1686 addnl = TRUE; | |
1687 c -= ADD_NL; | |
1688 } | |
1689 | |
1690 STRCPY(code, ""); | |
1691 switch (c) | |
1692 { | |
1693 case NFA_MATCH: STRCPY(code, "NFA_MATCH "); break; | |
1694 case NFA_SPLIT: STRCPY(code, "NFA_SPLIT "); break; | |
1695 case NFA_CONCAT: STRCPY(code, "NFA_CONCAT "); break; | |
1696 case NFA_NEWL: STRCPY(code, "NFA_NEWL "); break; | |
1697 case NFA_ZSTART: STRCPY(code, "NFA_ZSTART"); break; | |
1698 case NFA_ZEND: STRCPY(code, "NFA_ZEND"); break; | |
1699 | |
1700 case NFA_PREV_ATOM_NO_WIDTH: | |
1701 STRCPY(code, "NFA_PREV_ATOM_NO_WIDTH"); break; | |
1702 case NFA_NOPEN: STRCPY(code, "NFA_MOPEN_INVISIBLE"); break; | |
1703 case NFA_NCLOSE: STRCPY(code, "NFA_MCLOSE_INVISIBLE"); break; | |
1704 case NFA_START_INVISIBLE: STRCPY(code, "NFA_START_INVISIBLE"); break; | |
1705 case NFA_END_INVISIBLE: STRCPY(code, "NFA_END_INVISIBLE"); break; | |
1706 | |
1707 case NFA_MULTIBYTE: STRCPY(code, "NFA_MULTIBYTE"); break; | |
1708 case NFA_END_MULTIBYTE: STRCPY(code, "NFA_END_MULTIBYTE"); break; | |
1709 | |
1710 case NFA_COMPOSING: STRCPY(code, "NFA_COMPOSING"); break; | |
1711 case NFA_END_COMPOSING: STRCPY(code, "NFA_END_COMPOSING"); break; | |
1712 | |
1713 case NFA_MOPEN + 0: | |
1714 case NFA_MOPEN + 1: | |
1715 case NFA_MOPEN + 2: | |
1716 case NFA_MOPEN + 3: | |
1717 case NFA_MOPEN + 4: | |
1718 case NFA_MOPEN + 5: | |
1719 case NFA_MOPEN + 6: | |
1720 case NFA_MOPEN + 7: | |
1721 case NFA_MOPEN + 8: | |
1722 case NFA_MOPEN + 9: | |
1723 STRCPY(code, "NFA_MOPEN(x)"); | |
1724 code[10] = c - NFA_MOPEN + '0'; | |
1725 break; | |
1726 case NFA_MCLOSE + 0: | |
1727 case NFA_MCLOSE + 1: | |
1728 case NFA_MCLOSE + 2: | |
1729 case NFA_MCLOSE + 3: | |
1730 case NFA_MCLOSE + 4: | |
1731 case NFA_MCLOSE + 5: | |
1732 case NFA_MCLOSE + 6: | |
1733 case NFA_MCLOSE + 7: | |
1734 case NFA_MCLOSE + 8: | |
1735 case NFA_MCLOSE + 9: | |
1736 STRCPY(code, "NFA_MCLOSE(x)"); | |
1737 code[11] = c - NFA_MCLOSE + '0'; | |
1738 break; | |
1739 case NFA_EOL: STRCPY(code, "NFA_EOL "); break; | |
1740 case NFA_BOL: STRCPY(code, "NFA_BOL "); break; | |
1741 case NFA_EOW: STRCPY(code, "NFA_EOW "); break; | |
1742 case NFA_BOW: STRCPY(code, "NFA_BOW "); break; | |
1743 case NFA_STAR: STRCPY(code, "NFA_STAR "); break; | |
1744 case NFA_PLUS: STRCPY(code, "NFA_PLUS "); break; | |
1745 case NFA_NOT: STRCPY(code, "NFA_NOT "); break; | |
1746 case NFA_SKIP_CHAR: STRCPY(code, "NFA_SKIP_CHAR"); break; | |
1747 case NFA_OR: STRCPY(code, "NFA_OR"); break; | |
1748 case NFA_QUEST: STRCPY(code, "NFA_QUEST"); break; | |
1749 case NFA_QUEST_NONGREEDY: STRCPY(code, "NFA_QUEST_NON_GREEDY"); break; | |
1750 case NFA_END_NEG_RANGE: STRCPY(code, "NFA_END_NEG_RANGE"); break; | |
1751 case NFA_CLASS_ALNUM: STRCPY(code, "NFA_CLASS_ALNUM"); break; | |
1752 case NFA_CLASS_ALPHA: STRCPY(code, "NFA_CLASS_ALPHA"); break; | |
1753 case NFA_CLASS_BLANK: STRCPY(code, "NFA_CLASS_BLANK"); break; | |
1754 case NFA_CLASS_CNTRL: STRCPY(code, "NFA_CLASS_CNTRL"); break; | |
1755 case NFA_CLASS_DIGIT: STRCPY(code, "NFA_CLASS_DIGIT"); break; | |
1756 case NFA_CLASS_GRAPH: STRCPY(code, "NFA_CLASS_GRAPH"); break; | |
1757 case NFA_CLASS_LOWER: STRCPY(code, "NFA_CLASS_LOWER"); break; | |
1758 case NFA_CLASS_PRINT: STRCPY(code, "NFA_CLASS_PRINT"); break; | |
1759 case NFA_CLASS_PUNCT: STRCPY(code, "NFA_CLASS_PUNCT"); break; | |
1760 case NFA_CLASS_SPACE: STRCPY(code, "NFA_CLASS_SPACE"); break; | |
1761 case NFA_CLASS_UPPER: STRCPY(code, "NFA_CLASS_UPPER"); break; | |
1762 case NFA_CLASS_XDIGIT: STRCPY(code, "NFA_CLASS_XDIGIT"); break; | |
1763 case NFA_CLASS_TAB: STRCPY(code, "NFA_CLASS_TAB"); break; | |
1764 case NFA_CLASS_RETURN: STRCPY(code, "NFA_CLASS_RETURN"); break; | |
1765 case NFA_CLASS_BACKSPACE: STRCPY(code, "NFA_CLASS_BACKSPACE"); break; | |
1766 case NFA_CLASS_ESCAPE: STRCPY(code, "NFA_CLASS_ESCAPE"); break; | |
1767 | |
1768 case NFA_ANY: STRCPY(code, "NFA_ANY"); break; | |
1769 case NFA_IDENT: STRCPY(code, "NFA_IDENT"); break; | |
1770 case NFA_SIDENT:STRCPY(code, "NFA_SIDENT"); break; | |
1771 case NFA_KWORD: STRCPY(code, "NFA_KWORD"); break; | |
1772 case NFA_SKWORD:STRCPY(code, "NFA_SKWORD"); break; | |
1773 case NFA_FNAME: STRCPY(code, "NFA_FNAME"); break; | |
1774 case NFA_SFNAME:STRCPY(code, "NFA_SFNAME"); break; | |
1775 case NFA_PRINT: STRCPY(code, "NFA_PRINT"); break; | |
1776 case NFA_SPRINT:STRCPY(code, "NFA_SPRINT"); break; | |
1777 case NFA_WHITE: STRCPY(code, "NFA_WHITE"); break; | |
1778 case NFA_NWHITE:STRCPY(code, "NFA_NWHITE"); break; | |
1779 case NFA_DIGIT: STRCPY(code, "NFA_DIGIT"); break; | |
1780 case NFA_NDIGIT:STRCPY(code, "NFA_NDIGIT"); break; | |
1781 case NFA_HEX: STRCPY(code, "NFA_HEX"); break; | |
1782 case NFA_NHEX: STRCPY(code, "NFA_NHEX"); break; | |
1783 case NFA_OCTAL: STRCPY(code, "NFA_OCTAL"); break; | |
1784 case NFA_NOCTAL:STRCPY(code, "NFA_NOCTAL"); break; | |
1785 case NFA_WORD: STRCPY(code, "NFA_WORD"); break; | |
1786 case NFA_NWORD: STRCPY(code, "NFA_NWORD"); break; | |
1787 case NFA_HEAD: STRCPY(code, "NFA_HEAD"); break; | |
1788 case NFA_NHEAD: STRCPY(code, "NFA_NHEAD"); break; | |
1789 case NFA_ALPHA: STRCPY(code, "NFA_ALPHA"); break; | |
1790 case NFA_NALPHA:STRCPY(code, "NFA_NALPHA"); break; | |
1791 case NFA_LOWER: STRCPY(code, "NFA_LOWER"); break; | |
1792 case NFA_NLOWER:STRCPY(code, "NFA_NLOWER"); break; | |
1793 case NFA_UPPER: STRCPY(code, "NFA_UPPER"); break; | |
1794 case NFA_NUPPER:STRCPY(code, "NFA_NUPPER"); break; | |
1795 | |
1796 default: | |
1797 STRCPY(code, "CHAR(x)"); | |
1798 code[5] = c; | |
1799 } | |
1800 | |
1801 if (addnl == TRUE) | |
1802 STRCAT(code, " + NEWLINE "); | |
1803 | |
1804 } | |
1805 | |
1806 #ifdef ENABLE_LOG | |
1807 static FILE *log_fd; | |
1808 | |
1809 /* | |
1810 * Print the postfix notation of the current regexp. | |
1811 */ | |
1812 static void | |
1813 nfa_postfix_dump(expr, retval) | |
1814 char_u *expr; | |
1815 int retval; | |
1816 { | |
1817 int *p; | |
1818 FILE *f; | |
1819 | |
1820 f = fopen("LOG.log", "a"); | |
1821 if (f != NULL) | |
1822 { | |
1823 fprintf(f, "\n-------------------------\n"); | |
1824 if (retval == FAIL) | |
1825 fprintf(f, ">>> NFA engine failed ... \n"); | |
1826 else if (retval == OK) | |
1827 fprintf(f, ">>> NFA engine succeeded !\n"); | |
1828 fprintf(f, "Regexp: \"%s\"\nPostfix notation (char): \"", expr); | |
4464 | 1829 for (p = post_start; *p && p < post_end; p++) |
4444 | 1830 { |
1831 nfa_set_code(*p); | |
1832 fprintf(f, "%s, ", code); | |
1833 } | |
1834 fprintf(f, "\"\nPostfix notation (int): "); | |
4464 | 1835 for (p = post_start; *p && p < post_end; p++) |
4444 | 1836 fprintf(f, "%d ", *p); |
1837 fprintf(f, "\n\n"); | |
1838 fclose(f); | |
1839 } | |
1840 } | |
1841 | |
1842 /* | |
1843 * Print the NFA starting with a root node "state". | |
1844 */ | |
1845 static void | |
1846 nfa_print_state(debugf, state, ident) | |
1847 FILE *debugf; | |
1848 nfa_state_T *state; | |
1849 int ident; | |
1850 { | |
1851 int i; | |
1852 | |
1853 if (state == NULL) | |
1854 return; | |
1855 | |
1856 fprintf(debugf, "(%2d)", abs(state->id)); | |
1857 for (i = 0; i < ident; i++) | |
1858 fprintf(debugf, "%c", ' '); | |
1859 | |
1860 nfa_set_code(state->c); | |
1861 fprintf(debugf, "%s %s (%d) (id=%d)\n", | |
1862 state->negated ? "NOT" : "", code, state->c, abs(state->id)); | |
1863 if (state->id < 0) | |
1864 return; | |
1865 | |
1866 state->id = abs(state->id) * -1; | |
1867 nfa_print_state(debugf, state->out, ident + 4); | |
1868 nfa_print_state(debugf, state->out1, ident + 4); | |
1869 } | |
1870 | |
1871 /* | |
1872 * Print the NFA state machine. | |
1873 */ | |
1874 static void | |
1875 nfa_dump(prog) | |
1876 nfa_regprog_T *prog; | |
1877 { | |
1878 FILE *debugf = fopen("LOG.log", "a"); | |
1879 | |
1880 if (debugf != NULL) | |
1881 { | |
1882 nfa_print_state(debugf, prog->start, 0); | |
1883 fclose(debugf); | |
1884 } | |
1885 } | |
1886 #endif /* ENABLE_LOG */ | |
1887 #endif /* DEBUG */ | |
1888 | |
1889 /* | |
1890 * Parse r.e. @expr and convert it into postfix form. | |
1891 * Return the postfix string on success, NULL otherwise. | |
1892 */ | |
1893 static int * | |
1894 re2post() | |
1895 { | |
1896 if (nfa_reg(REG_NOPAREN) == FAIL) | |
1897 return NULL; | |
1898 EMIT(NFA_MOPEN); | |
1899 return post_start; | |
1900 } | |
1901 | |
1902 /* NB. Some of the code below is inspired by Russ's. */ | |
1903 | |
1904 /* | |
1905 * Represents an NFA state plus zero or one or two arrows exiting. | |
1906 * if c == MATCH, no arrows out; matching state. | |
1907 * If c == SPLIT, unlabeled arrows to out and out1 (if != NULL). | |
1908 * If c < 256, labeled arrow with character c to out. | |
1909 */ | |
1910 | |
1911 static nfa_state_T *state_ptr; /* points to nfa_prog->state */ | |
1912 | |
1913 /* | |
1914 * Allocate and initialize nfa_state_T. | |
1915 */ | |
1916 static nfa_state_T * | |
1917 new_state(c, out, out1) | |
1918 int c; | |
1919 nfa_state_T *out; | |
1920 nfa_state_T *out1; | |
1921 { | |
1922 nfa_state_T *s; | |
1923 | |
1924 if (istate >= nstate) | |
1925 return NULL; | |
1926 | |
1927 s = &state_ptr[istate++]; | |
1928 | |
1929 s->c = c; | |
1930 s->out = out; | |
1931 s->out1 = out1; | |
1932 | |
1933 s->id = istate; | |
1934 s->lastlist = 0; | |
1935 s->lastthread = NULL; | |
1936 s->visits = 0; | |
1937 s->negated = FALSE; | |
1938 | |
1939 return s; | |
1940 } | |
1941 | |
1942 /* | |
1943 * A partially built NFA without the matching state filled in. | |
1944 * Frag_T.start points at the start state. | |
1945 * Frag_T.out is a list of places that need to be set to the | |
1946 * next state for this fragment. | |
1947 */ | |
1948 typedef union Ptrlist Ptrlist; | |
1949 struct Frag | |
1950 { | |
1951 nfa_state_T *start; | |
1952 Ptrlist *out; | |
1953 }; | |
1954 typedef struct Frag Frag_T; | |
1955 | |
1956 static Frag_T frag __ARGS((nfa_state_T *start, Ptrlist *out)); | |
1957 static Ptrlist *list1 __ARGS((nfa_state_T **outp)); | |
1958 static void patch __ARGS((Ptrlist *l, nfa_state_T *s)); | |
1959 static Ptrlist *append __ARGS((Ptrlist *l1, Ptrlist *l2)); | |
1960 static void st_push __ARGS((Frag_T s, Frag_T **p, Frag_T *stack_end)); | |
1961 static Frag_T st_pop __ARGS((Frag_T **p, Frag_T *stack)); | |
1962 | |
1963 /* | |
4456 | 1964 * Initialize a Frag_T struct and return it. |
4444 | 1965 */ |
1966 static Frag_T | |
1967 frag(start, out) | |
1968 nfa_state_T *start; | |
1969 Ptrlist *out; | |
1970 { | |
4456 | 1971 Frag_T n; |
1972 | |
1973 n.start = start; | |
1974 n.out = out; | |
4444 | 1975 return n; |
1976 } | |
1977 | |
1978 /* | |
1979 * Since the out pointers in the list are always | |
1980 * uninitialized, we use the pointers themselves | |
1981 * as storage for the Ptrlists. | |
1982 */ | |
1983 union Ptrlist | |
1984 { | |
1985 Ptrlist *next; | |
1986 nfa_state_T *s; | |
1987 }; | |
1988 | |
1989 /* | |
1990 * Create singleton list containing just outp. | |
1991 */ | |
1992 static Ptrlist * | |
1993 list1(outp) | |
1994 nfa_state_T **outp; | |
1995 { | |
1996 Ptrlist *l; | |
1997 | |
1998 l = (Ptrlist *)outp; | |
1999 l->next = NULL; | |
2000 return l; | |
2001 } | |
2002 | |
2003 /* | |
2004 * Patch the list of states at out to point to start. | |
2005 */ | |
2006 static void | |
2007 patch(l, s) | |
2008 Ptrlist *l; | |
2009 nfa_state_T *s; | |
2010 { | |
2011 Ptrlist *next; | |
2012 | |
2013 for (; l; l = next) | |
2014 { | |
2015 next = l->next; | |
2016 l->s = s; | |
2017 } | |
2018 } | |
2019 | |
2020 | |
2021 /* | |
2022 * Join the two lists l1 and l2, returning the combination. | |
2023 */ | |
2024 static Ptrlist * | |
2025 append(l1, l2) | |
2026 Ptrlist *l1; | |
2027 Ptrlist *l2; | |
2028 { | |
2029 Ptrlist *oldl1; | |
2030 | |
2031 oldl1 = l1; | |
2032 while (l1->next) | |
2033 l1 = l1->next; | |
2034 l1->next = l2; | |
2035 return oldl1; | |
2036 } | |
2037 | |
2038 /* | |
2039 * Stack used for transforming postfix form into NFA. | |
2040 */ | |
2041 static Frag_T empty; | |
2042 | |
2043 static void | |
2044 st_error(postfix, end, p) | |
2045 int *postfix; | |
2046 int *end; | |
2047 int *p; | |
2048 { | |
2049 FILE *df; | |
2050 int *p2; | |
2051 | |
2052 df = fopen("stack.err", "a"); | |
2053 if (df) | |
2054 { | |
2055 fprintf(df, "Error popping the stack!\n"); | |
2056 #ifdef DEBUG | |
2057 fprintf(df, "Current regexp is \"%s\"\n", nfa_regengine.expr); | |
2058 #endif | |
2059 fprintf(df, "Postfix form is: "); | |
2060 #ifdef DEBUG | |
2061 for (p2 = postfix; p2 < end; p2++) | |
2062 { | |
2063 nfa_set_code(*p2); | |
2064 fprintf(df, "%s, ", code); | |
2065 } | |
2066 nfa_set_code(*p); | |
2067 fprintf(df, "\nCurrent position is: "); | |
2068 for (p2 = postfix; p2 <= p; p2 ++) | |
2069 { | |
2070 nfa_set_code(*p2); | |
2071 fprintf(df, "%s, ", code); | |
2072 } | |
2073 #else | |
2074 for (p2 = postfix; p2 < end; p2++) | |
2075 { | |
2076 fprintf(df, "%d, ", *p2); | |
2077 } | |
2078 fprintf(df, "\nCurrent position is: "); | |
2079 for (p2 = postfix; p2 <= p; p2 ++) | |
2080 { | |
2081 fprintf(df, "%d, ", *p2); | |
2082 } | |
2083 #endif | |
2084 fprintf(df, "\n--------------------------\n"); | |
2085 fclose(df); | |
2086 } | |
2087 EMSG(_("E874: (NFA) Could not pop the stack !")); | |
2088 } | |
2089 | |
2090 /* | |
2091 * Push an item onto the stack. | |
2092 */ | |
2093 static void | |
2094 st_push(s, p, stack_end) | |
2095 Frag_T s; | |
2096 Frag_T **p; | |
2097 Frag_T *stack_end; | |
2098 { | |
2099 Frag_T *stackp = *p; | |
2100 | |
2101 if (stackp >= stack_end) | |
2102 return; | |
2103 *stackp = s; | |
2104 *p = *p + 1; | |
2105 } | |
2106 | |
2107 /* | |
2108 * Pop an item from the stack. | |
2109 */ | |
2110 static Frag_T | |
2111 st_pop(p, stack) | |
2112 Frag_T **p; | |
2113 Frag_T *stack; | |
2114 { | |
2115 Frag_T *stackp; | |
2116 | |
2117 *p = *p - 1; | |
2118 stackp = *p; | |
2119 if (stackp < stack) | |
2120 return empty; | |
2121 return **p; | |
2122 } | |
2123 | |
2124 /* | |
2125 * Convert a postfix form into its equivalent NFA. | |
2126 * Return the NFA start state on success, NULL otherwise. | |
2127 */ | |
2128 static nfa_state_T * | |
2129 post2nfa(postfix, end, nfa_calc_size) | |
2130 int *postfix; | |
2131 int *end; | |
2132 int nfa_calc_size; | |
2133 { | |
2134 int *p; | |
2135 int mopen; | |
2136 int mclose; | |
2137 Frag_T *stack = NULL; | |
2138 Frag_T *stackp = NULL; | |
2139 Frag_T *stack_end = NULL; | |
2140 Frag_T e1; | |
2141 Frag_T e2; | |
2142 Frag_T e; | |
2143 nfa_state_T *s; | |
2144 nfa_state_T *s1; | |
2145 nfa_state_T *matchstate; | |
4484 | 2146 nfa_state_T *ret = NULL; |
4444 | 2147 |
2148 if (postfix == NULL) | |
2149 return NULL; | |
2150 | |
4456 | 2151 #define PUSH(s) st_push((s), &stackp, stack_end) |
4444 | 2152 #define POP() st_pop(&stackp, stack); \ |
2153 if (stackp < stack) \ | |
2154 { \ | |
2155 st_error(postfix, end, p); \ | |
2156 return NULL; \ | |
2157 } | |
2158 | |
2159 if (nfa_calc_size == FALSE) | |
2160 { | |
2161 /* Allocate space for the stack. Max states on the stack : nstate */ | |
4462 | 2162 stack = (Frag_T *) lalloc((nstate + 1) * sizeof(Frag_T), TRUE); |
4444 | 2163 stackp = stack; |
4462 | 2164 stack_end = stack + (nstate + 1); |
4444 | 2165 } |
2166 | |
2167 for (p = postfix; p < end; ++p) | |
2168 { | |
2169 switch (*p) | |
2170 { | |
2171 case NFA_CONCAT: | |
2172 /* Catenation. | |
2173 * Pay attention: this operator does not exist | |
2174 * in the r.e. itself (it is implicit, really). | |
2175 * It is added when r.e. is translated to postfix | |
2176 * form in re2post(). | |
2177 * | |
2178 * No new state added here. */ | |
2179 if (nfa_calc_size == TRUE) | |
2180 { | |
4458 | 2181 /* nstate += 0; */ |
4444 | 2182 break; |
2183 } | |
2184 e2 = POP(); | |
2185 e1 = POP(); | |
2186 patch(e1.out, e2.start); | |
2187 PUSH(frag(e1.start, e2.out)); | |
2188 break; | |
2189 | |
2190 case NFA_NOT: | |
2191 /* Negation of a character */ | |
2192 if (nfa_calc_size == TRUE) | |
2193 { | |
4458 | 2194 /* nstate += 0; */ |
4444 | 2195 break; |
2196 } | |
2197 e1 = POP(); | |
2198 e1.start->negated = TRUE; | |
2199 if (e1.start->c == NFA_MULTIBYTE || e1.start->c == NFA_COMPOSING) | |
2200 e1.start->out1->negated = TRUE; | |
2201 PUSH(e1); | |
2202 break; | |
2203 | |
2204 case NFA_OR: | |
2205 /* Alternation */ | |
2206 if (nfa_calc_size == TRUE) | |
2207 { | |
4458 | 2208 nstate++; |
4444 | 2209 break; |
2210 } | |
2211 e2 = POP(); | |
2212 e1 = POP(); | |
2213 s = new_state(NFA_SPLIT, e1.start, e2.start); | |
2214 if (s == NULL) | |
4484 | 2215 goto theend; |
4444 | 2216 PUSH(frag(s, append(e1.out, e2.out))); |
2217 break; | |
2218 | |
2219 case NFA_STAR: | |
2220 /* Zero or more */ | |
2221 if (nfa_calc_size == TRUE) | |
2222 { | |
4458 | 2223 nstate++; |
4444 | 2224 break; |
2225 } | |
2226 e = POP(); | |
2227 s = new_state(NFA_SPLIT, e.start, NULL); | |
2228 if (s == NULL) | |
4484 | 2229 goto theend; |
4444 | 2230 patch(e.out, s); |
2231 PUSH(frag(s, list1(&s->out1))); | |
2232 break; | |
2233 | |
2234 case NFA_QUEST: | |
2235 /* one or zero atoms=> greedy match */ | |
2236 if (nfa_calc_size == TRUE) | |
2237 { | |
4458 | 2238 nstate++; |
4444 | 2239 break; |
2240 } | |
2241 e = POP(); | |
2242 s = new_state(NFA_SPLIT, e.start, NULL); | |
2243 if (s == NULL) | |
4484 | 2244 goto theend; |
4444 | 2245 PUSH(frag(s, append(e.out, list1(&s->out1)))); |
2246 break; | |
2247 | |
2248 case NFA_QUEST_NONGREEDY: | |
2249 /* zero or one atoms => non-greedy match */ | |
2250 if (nfa_calc_size == TRUE) | |
2251 { | |
4458 | 2252 nstate++; |
4444 | 2253 break; |
2254 } | |
2255 e = POP(); | |
2256 s = new_state(NFA_SPLIT, NULL, e.start); | |
2257 if (s == NULL) | |
4484 | 2258 goto theend; |
4444 | 2259 PUSH(frag(s, append(e.out, list1(&s->out)))); |
2260 break; | |
2261 | |
2262 case NFA_PLUS: | |
2263 /* One or more */ | |
2264 if (nfa_calc_size == TRUE) | |
2265 { | |
4458 | 2266 nstate++; |
4444 | 2267 break; |
2268 } | |
2269 e = POP(); | |
2270 s = new_state(NFA_SPLIT, e.start, NULL); | |
2271 if (s == NULL) | |
4484 | 2272 goto theend; |
4444 | 2273 patch(e.out, s); |
2274 PUSH(frag(e.start, list1(&s->out1))); | |
2275 break; | |
2276 | |
2277 case NFA_SKIP_CHAR: | |
2278 /* Symbol of 0-length, Used in a repetition | |
2279 * with max/min count of 0 */ | |
2280 if (nfa_calc_size == TRUE) | |
2281 { | |
4458 | 2282 nstate++; |
4444 | 2283 break; |
2284 } | |
2285 s = new_state(NFA_SKIP_CHAR, NULL, NULL); | |
2286 if (s == NULL) | |
4484 | 2287 goto theend; |
4444 | 2288 PUSH(frag(s, list1(&s->out))); |
2289 break; | |
2290 | |
2291 case NFA_PREV_ATOM_NO_WIDTH: | |
2292 /* The \@= operator: match the preceding atom with 0 width. | |
2293 * Surrounds the preceding atom with START_INVISIBLE and | |
2294 * END_INVISIBLE, similarly to MOPEN. | |
2295 */ | |
2296 /* TODO: Maybe this drops the speed? */ | |
4484 | 2297 goto theend; |
4444 | 2298 |
2299 if (nfa_calc_size == TRUE) | |
2300 { | |
2301 nstate += 2; | |
2302 break; | |
2303 } | |
2304 e = POP(); | |
2305 s1 = new_state(NFA_END_INVISIBLE, NULL, NULL); | |
2306 if (s1 == NULL) | |
4484 | 2307 goto theend; |
4444 | 2308 patch(e.out, s1); |
2309 | |
2310 s = new_state(NFA_START_INVISIBLE, e.start, s1); | |
2311 if (s == NULL) | |
4484 | 2312 goto theend; |
4444 | 2313 PUSH(frag(s, list1(&s1->out))); |
2314 break; | |
2315 | |
2316 case NFA_MOPEN + 0: /* Submatch */ | |
2317 case NFA_MOPEN + 1: | |
2318 case NFA_MOPEN + 2: | |
2319 case NFA_MOPEN + 3: | |
2320 case NFA_MOPEN + 4: | |
2321 case NFA_MOPEN + 5: | |
2322 case NFA_MOPEN + 6: | |
2323 case NFA_MOPEN + 7: | |
2324 case NFA_MOPEN + 8: | |
2325 case NFA_MOPEN + 9: | |
2326 case NFA_NOPEN: /* \%( "Invisible Submatch" */ | |
2327 case NFA_MULTIBYTE: /* mbyte char */ | |
2328 case NFA_COMPOSING: /* composing char */ | |
2329 if (nfa_calc_size == TRUE) | |
2330 { | |
2331 nstate += 2; | |
2332 break; | |
2333 } | |
2334 | |
2335 mopen = *p; | |
2336 switch (*p) | |
2337 { | |
2338 case NFA_NOPEN: | |
2339 mclose = NFA_NCLOSE; | |
2340 break; | |
2341 case NFA_MULTIBYTE: | |
2342 mclose = NFA_END_MULTIBYTE; | |
2343 break; | |
2344 case NFA_COMPOSING: | |
2345 mclose = NFA_END_COMPOSING; | |
2346 break; | |
2347 default: | |
2348 /* NFA_MOPEN(0) ... NFA_MOPEN(9) */ | |
2349 mclose = *p + NSUBEXP; | |
2350 break; | |
2351 } | |
2352 | |
2353 /* Allow "NFA_MOPEN" as a valid postfix representation for | |
2354 * the empty regexp "". In this case, the NFA will be | |
2355 * NFA_MOPEN -> NFA_MCLOSE. Note that this also allows | |
2356 * empty groups of parenthesis, and empty mbyte chars */ | |
2357 if (stackp == stack) | |
2358 { | |
2359 s = new_state(mopen, NULL, NULL); | |
2360 if (s == NULL) | |
4484 | 2361 goto theend; |
4444 | 2362 s1 = new_state(mclose, NULL, NULL); |
2363 if (s1 == NULL) | |
4484 | 2364 goto theend; |
4444 | 2365 patch(list1(&s->out), s1); |
2366 PUSH(frag(s, list1(&s1->out))); | |
2367 break; | |
2368 } | |
2369 | |
2370 /* At least one node was emitted before NFA_MOPEN, so | |
2371 * at least one node will be between NFA_MOPEN and NFA_MCLOSE */ | |
2372 e = POP(); | |
2373 s = new_state(mopen, e.start, NULL); /* `(' */ | |
2374 if (s == NULL) | |
4484 | 2375 goto theend; |
4444 | 2376 |
2377 s1 = new_state(mclose, NULL, NULL); /* `)' */ | |
2378 if (s1 == NULL) | |
4484 | 2379 goto theend; |
4444 | 2380 patch(e.out, s1); |
2381 | |
2382 if (mopen == NFA_MULTIBYTE || mopen == NFA_COMPOSING) | |
2383 /* MULTIBYTE->out1 = END_MULTIBYTE | |
2384 * COMPOSING->out1 = END_COMPOSING */ | |
2385 patch(list1(&s->out1), s1); | |
2386 | |
2387 PUSH(frag(s, list1(&s1->out))); | |
2388 break; | |
2389 | |
2390 case NFA_ZSTART: | |
2391 case NFA_ZEND: | |
2392 default: | |
2393 /* Operands */ | |
2394 if (nfa_calc_size == TRUE) | |
2395 { | |
4458 | 2396 nstate++; |
4444 | 2397 break; |
2398 } | |
2399 s = new_state(*p, NULL, NULL); | |
2400 if (s == NULL) | |
4484 | 2401 goto theend; |
4444 | 2402 PUSH(frag(s, list1(&s->out))); |
2403 break; | |
2404 | |
2405 } /* switch(*p) */ | |
2406 | |
2407 } /* for(p = postfix; *p; ++p) */ | |
2408 | |
2409 if (nfa_calc_size == TRUE) | |
2410 { | |
4458 | 2411 nstate++; |
4484 | 2412 goto theend; /* Return value when counting size is ignored anyway */ |
4444 | 2413 } |
2414 | |
2415 e = POP(); | |
2416 if (stackp != stack) | |
2417 EMSG_RET_NULL(_("E875: (NFA regexp) (While converting from postfix to NFA), too many states left on stack")); | |
2418 | |
2419 if (istate >= nstate) | |
2420 EMSG_RET_NULL(_("E876: (NFA regexp) Not enough space to store the whole NFA ")); | |
2421 | |
2422 matchstate = &state_ptr[istate++]; /* the match state */ | |
2423 matchstate->c = NFA_MATCH; | |
2424 matchstate->out = matchstate->out1 = NULL; | |
2425 | |
2426 patch(e.out, matchstate); | |
4484 | 2427 ret = e.start; |
2428 | |
2429 theend: | |
2430 vim_free(stack); | |
2431 return ret; | |
4444 | 2432 |
2433 #undef POP1 | |
2434 #undef PUSH1 | |
2435 #undef POP2 | |
2436 #undef PUSH2 | |
2437 #undef POP | |
2438 #undef PUSH | |
2439 } | |
2440 | |
2441 /**************************************************************** | |
2442 * NFA execution code. | |
2443 ****************************************************************/ | |
2444 | |
2445 /* thread_T contains runtime information of a NFA state */ | |
2446 struct thread | |
2447 { | |
2448 nfa_state_T *state; | |
2449 regsub_T sub; /* submatch info */ | |
2450 }; | |
2451 | |
2452 typedef struct | |
2453 { | |
2454 thread_T *t; | |
2455 int n; | |
2456 } List; | |
2457 | |
2458 static void addstate __ARGS((List *l, nfa_state_T *state, regsub_T *m, int off, int lid, int *match)); | |
2459 | |
2460 static void | |
2461 addstate(l, state, m, off, lid, match) | |
2462 List *l; /* runtime state list */ | |
2463 nfa_state_T *state; /* state to update */ | |
2464 regsub_T *m; /* pointers to subexpressions */ | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2465 int off; /* byte offset, when -1 go to next line */ |
4444 | 2466 int lid; |
2467 int *match; /* found match? */ | |
2468 { | |
2469 regsub_T save; | |
2470 int subidx = 0; | |
2471 | |
2472 if (l == NULL || state == NULL) | |
2473 return; | |
2474 | |
2475 switch (state->c) | |
2476 { | |
2477 case NFA_SPLIT: | |
2478 case NFA_NOT: | |
2479 case NFA_NOPEN: | |
2480 case NFA_NCLOSE: | |
2481 case NFA_MCLOSE: | |
2482 case NFA_MCLOSE + 1: | |
2483 case NFA_MCLOSE + 2: | |
2484 case NFA_MCLOSE + 3: | |
2485 case NFA_MCLOSE + 4: | |
2486 case NFA_MCLOSE + 5: | |
2487 case NFA_MCLOSE + 6: | |
2488 case NFA_MCLOSE + 7: | |
2489 case NFA_MCLOSE + 8: | |
2490 case NFA_MCLOSE + 9: | |
2491 /* Do not remember these nodes in list "thislist" or "nextlist" */ | |
2492 break; | |
2493 | |
2494 default: | |
2495 if (state->lastlist == lid) | |
2496 { | |
2497 if (++state->visits > 2) | |
2498 return; | |
2499 } | |
2500 else | |
2501 { | |
2502 /* add the state to the list */ | |
2503 state->lastlist = lid; | |
2504 state->lastthread = &l->t[l->n++]; | |
2505 state->lastthread->state = state; | |
2506 state->lastthread->sub = *m; | |
2507 } | |
2508 } | |
2509 | |
2510 #ifdef ENABLE_LOG | |
2511 nfa_set_code(state->c); | |
2512 fprintf(log_fd, "> Adding state %d to list. Character %s, code %d\n", | |
2513 abs(state->id), code, state->c); | |
2514 #endif | |
2515 switch (state->c) | |
2516 { | |
2517 case NFA_MATCH: | |
2518 *match = TRUE; | |
2519 break; | |
2520 | |
2521 case NFA_SPLIT: | |
2522 addstate(l, state->out, m, off, lid, match); | |
2523 addstate(l, state->out1, m, off, lid, match); | |
2524 break; | |
2525 | |
2526 case NFA_SKIP_CHAR: | |
2527 addstate(l, state->out, m, off, lid, match); | |
2528 break; | |
2529 | |
2530 #if 0 | |
2531 case NFA_END_NEG_RANGE: | |
2532 /* Nothing to handle here. nfa_regmatch() will take care of it */ | |
2533 break; | |
2534 | |
2535 case NFA_NOT: | |
2536 EMSG(_("E999: (NFA regexp internal error) Should not process NOT node !")); | |
2537 #ifdef ENABLE_LOG | |
2538 fprintf(f, "\n\n>>> E999: Added state NFA_NOT to a list ... Something went wrong ! Why wasn't it processed already? \n\n"); | |
2539 #endif | |
2540 break; | |
2541 | |
2542 case NFA_COMPOSING: | |
2543 /* nfa_regmatch() will match all the bytes of this composing char. */ | |
2544 break; | |
2545 | |
2546 case NFA_MULTIBYTE: | |
2547 /* nfa_regmatch() will match all the bytes of this multibyte char. */ | |
2548 break; | |
2549 #endif | |
2550 | |
2551 case NFA_END_MULTIBYTE: | |
2552 /* Successfully matched this mbyte char */ | |
2553 addstate(l, state->out, m, off, lid, match); | |
2554 break; | |
2555 | |
2556 case NFA_NOPEN: | |
2557 case NFA_NCLOSE: | |
2558 addstate(l, state->out, m, off, lid, match); | |
2559 break; | |
2560 | |
2561 /* If this state is reached, then a recursive call of nfa_regmatch() | |
2562 * succeeded. the next call saves the found submatches in the | |
2563 * first state after the "invisible" branch. */ | |
2564 #if 0 | |
2565 case NFA_END_INVISIBLE: | |
2566 break; | |
2567 #endif | |
2568 | |
2569 case NFA_MOPEN + 0: | |
2570 case NFA_MOPEN + 1: | |
2571 case NFA_MOPEN + 2: | |
2572 case NFA_MOPEN + 3: | |
2573 case NFA_MOPEN + 4: | |
2574 case NFA_MOPEN + 5: | |
2575 case NFA_MOPEN + 6: | |
2576 case NFA_MOPEN + 7: | |
2577 case NFA_MOPEN + 8: | |
2578 case NFA_MOPEN + 9: | |
2579 case NFA_ZSTART: | |
2580 subidx = state->c - NFA_MOPEN; | |
2581 if (state->c == NFA_ZSTART) | |
2582 subidx = 0; | |
2583 | |
2584 if (REG_MULTI) | |
2585 { | |
2586 save.startpos[subidx] = m->startpos[subidx]; | |
2587 save.endpos[subidx] = m->endpos[subidx]; | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2588 if (off == -1) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2589 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2590 m->startpos[subidx].lnum = reglnum + 1; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2591 m->startpos[subidx].col = 0; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2592 } |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2593 else |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2594 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2595 m->startpos[subidx].lnum = reglnum; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2596 m->startpos[subidx].col = |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2597 (colnr_T)(reginput - regline + off); |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2598 } |
4444 | 2599 } |
2600 else | |
2601 { | |
2602 save.start[subidx] = m->start[subidx]; | |
2603 save.end[subidx] = m->end[subidx]; | |
2604 m->start[subidx] = reginput + off; | |
2605 } | |
2606 | |
2607 addstate(l, state->out, m, off, lid, match); | |
2608 | |
2609 if (REG_MULTI) | |
2610 { | |
2611 m->startpos[subidx] = save.startpos[subidx]; | |
2612 m->endpos[subidx] = save.endpos[subidx]; | |
2613 } | |
2614 else | |
2615 { | |
2616 m->start[subidx] = save.start[subidx]; | |
2617 m->end[subidx] = save.end[subidx]; | |
2618 } | |
2619 break; | |
2620 | |
2621 case NFA_MCLOSE + 0: | |
2622 if (nfa_has_zend == TRUE) | |
2623 { | |
2624 addstate(l, state->out, m, off, lid, match); | |
2625 break; | |
2626 } | |
2627 case NFA_MCLOSE + 1: | |
2628 case NFA_MCLOSE + 2: | |
2629 case NFA_MCLOSE + 3: | |
2630 case NFA_MCLOSE + 4: | |
2631 case NFA_MCLOSE + 5: | |
2632 case NFA_MCLOSE + 6: | |
2633 case NFA_MCLOSE + 7: | |
2634 case NFA_MCLOSE + 8: | |
2635 case NFA_MCLOSE + 9: | |
2636 case NFA_ZEND: | |
2637 subidx = state->c - NFA_MCLOSE; | |
2638 if (state->c == NFA_ZEND) | |
2639 subidx = 0; | |
2640 | |
2641 if (REG_MULTI) | |
2642 { | |
2643 save.startpos[subidx] = m->startpos[subidx]; | |
2644 save.endpos[subidx] = m->endpos[subidx]; | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2645 if (off == -1) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2646 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2647 m->endpos[subidx].lnum = reglnum + 1; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2648 m->endpos[subidx].col = 0; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2649 } |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2650 else |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2651 { |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2652 m->endpos[subidx].lnum = reglnum; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2653 m->endpos[subidx].col = (colnr_T)(reginput - regline + off); |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2654 } |
4444 | 2655 } |
2656 else | |
2657 { | |
2658 save.start[subidx] = m->start[subidx]; | |
2659 save.end[subidx] = m->end[subidx]; | |
2660 m->end[subidx] = reginput + off; | |
2661 } | |
2662 | |
2663 addstate(l, state->out, m, off, lid, match); | |
2664 | |
2665 if (REG_MULTI) | |
2666 { | |
2667 m->startpos[subidx] = save.startpos[subidx]; | |
2668 m->endpos[subidx] = save.endpos[subidx]; | |
2669 } | |
2670 else | |
2671 { | |
2672 m->start[subidx] = save.start[subidx]; | |
2673 m->end[subidx] = save.end[subidx]; | |
2674 } | |
2675 break; | |
2676 } | |
2677 } | |
2678 | |
2679 /* | |
2680 * Check character class "class" against current character c. | |
2681 */ | |
2682 static int | |
2683 check_char_class(class, c) | |
2684 int class; | |
2685 int c; | |
2686 { | |
2687 switch (class) | |
2688 { | |
2689 case NFA_CLASS_ALNUM: | |
4464 | 2690 if (c >= 1 && c <= 255 && isalnum(c)) |
4444 | 2691 return OK; |
2692 break; | |
2693 case NFA_CLASS_ALPHA: | |
4464 | 2694 if (c >= 1 && c <= 255 && isalpha(c)) |
4444 | 2695 return OK; |
2696 break; | |
2697 case NFA_CLASS_BLANK: | |
2698 if (c == ' ' || c == '\t') | |
2699 return OK; | |
2700 break; | |
2701 case NFA_CLASS_CNTRL: | |
4464 | 2702 if (c >= 1 && c <= 255 && iscntrl(c)) |
4444 | 2703 return OK; |
2704 break; | |
2705 case NFA_CLASS_DIGIT: | |
2706 if (VIM_ISDIGIT(c)) | |
2707 return OK; | |
2708 break; | |
2709 case NFA_CLASS_GRAPH: | |
4464 | 2710 if (c >= 1 && c <= 255 && isgraph(c)) |
4444 | 2711 return OK; |
2712 break; | |
2713 case NFA_CLASS_LOWER: | |
2714 if (MB_ISLOWER(c)) | |
2715 return OK; | |
2716 break; | |
2717 case NFA_CLASS_PRINT: | |
2718 if (vim_isprintc(c)) | |
2719 return OK; | |
2720 break; | |
2721 case NFA_CLASS_PUNCT: | |
4464 | 2722 if (c >= 1 && c <= 255 && ispunct(c)) |
4444 | 2723 return OK; |
2724 break; | |
2725 case NFA_CLASS_SPACE: | |
2726 if ((c >=9 && c <= 13) || (c == ' ')) | |
2727 return OK; | |
2728 break; | |
2729 case NFA_CLASS_UPPER: | |
2730 if (MB_ISUPPER(c)) | |
2731 return OK; | |
2732 break; | |
2733 case NFA_CLASS_XDIGIT: | |
2734 if (vim_isxdigit(c)) | |
2735 return OK; | |
2736 break; | |
2737 case NFA_CLASS_TAB: | |
2738 if (c == '\t') | |
2739 return OK; | |
2740 break; | |
2741 case NFA_CLASS_RETURN: | |
2742 if (c == '\r') | |
2743 return OK; | |
2744 break; | |
2745 case NFA_CLASS_BACKSPACE: | |
2746 if (c == '\b') | |
2747 return OK; | |
2748 break; | |
2749 case NFA_CLASS_ESCAPE: | |
2750 if (c == '\033') | |
2751 return OK; | |
2752 break; | |
2753 | |
2754 default: | |
2755 /* should not be here :P */ | |
2756 EMSG_RET_FAIL(_("E877: (NFA regexp) Invalid character class ")); | |
2757 } | |
2758 return FAIL; | |
2759 } | |
2760 | |
2761 /* | |
2762 * Set all NFA nodes' list ID equal to -1. | |
2763 */ | |
2764 static void | |
2765 nfa_set_neg_listids(start) | |
2766 nfa_state_T *start; | |
2767 { | |
2768 if (start == NULL) | |
2769 return; | |
2770 if (start->lastlist >= 0) | |
2771 { | |
2772 start->lastlist = -1; | |
2773 nfa_set_neg_listids(start->out); | |
2774 nfa_set_neg_listids(start->out1); | |
2775 } | |
2776 } | |
2777 | |
2778 /* | |
2779 * Set all NFA nodes' list ID equal to 0. | |
2780 */ | |
2781 static void | |
2782 nfa_set_null_listids(start) | |
2783 nfa_state_T *start; | |
2784 { | |
2785 if (start == NULL) | |
2786 return; | |
2787 if (start->lastlist == -1) | |
2788 { | |
2789 start->lastlist = 0; | |
2790 nfa_set_null_listids(start->out); | |
2791 nfa_set_null_listids(start->out1); | |
2792 } | |
2793 } | |
2794 | |
2795 /* | |
2796 * Save list IDs for all NFA states in "list". | |
2797 */ | |
2798 static void | |
2799 nfa_save_listids(start, list) | |
2800 nfa_state_T *start; | |
2801 int *list; | |
2802 { | |
2803 if (start == NULL) | |
2804 return; | |
2805 if (start->lastlist != -1) | |
2806 { | |
2807 list[abs(start->id)] = start->lastlist; | |
2808 start->lastlist = -1; | |
2809 nfa_save_listids(start->out, list); | |
2810 nfa_save_listids(start->out1, list); | |
2811 } | |
2812 } | |
2813 | |
2814 /* | |
2815 * Restore list IDs from "list" to all NFA states. | |
2816 */ | |
2817 static void | |
2818 nfa_restore_listids(start, list) | |
2819 nfa_state_T *start; | |
2820 int *list; | |
2821 { | |
2822 if (start == NULL) | |
2823 return; | |
2824 if (start->lastlist == -1) | |
2825 { | |
2826 start->lastlist = list[abs(start->id)]; | |
2827 nfa_restore_listids(start->out, list); | |
2828 nfa_restore_listids(start->out1, list); | |
2829 } | |
2830 } | |
2831 | |
2832 /* | |
2833 * Main matching routine. | |
2834 * | |
2835 * Run NFA to determine whether it matches reginput. | |
2836 * | |
2837 * Return TRUE if there is a match, FALSE otherwise. | |
2838 * Note: Caller must ensure that: start != NULL. | |
2839 */ | |
2840 static int | |
2841 nfa_regmatch(start, submatch, m) | |
2842 nfa_state_T *start; | |
2843 regsub_T *submatch; | |
2844 regsub_T *m; | |
2845 { | |
2846 int c = -1; | |
2847 int n; | |
2848 int i = 0; | |
2849 int result; | |
2850 int size = 0; | |
2851 int match = FALSE; | |
2852 int flag = 0; | |
2853 int old_reglnum = -1; | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2854 int go_to_nextline; |
4444 | 2855 thread_T *t; |
2856 char_u *old_reginput = NULL; | |
2857 char_u *old_regline = NULL; | |
2858 nfa_state_T *sta; | |
2859 nfa_state_T *end; | |
2860 List list[3]; | |
2861 List *listtbl[2][2]; | |
2862 List *ll; | |
2863 int listid = 1; | |
2864 int endnode = 0; | |
2865 List *thislist; | |
2866 List *nextlist; | |
2867 List *neglist; | |
2868 int *listids = NULL; | |
2869 int j = 0; | |
2870 int len = 0; | |
4460 | 2871 #ifdef NFA_REGEXP_DEBUG_LOG |
2872 FILE *debug = fopen(NFA_REGEXP_DEBUG_LOG_NAME, "a"); | |
4444 | 2873 |
2874 if (debug == NULL) | |
2875 { | |
4460 | 2876 EMSG2(_("(NFA) COULD NOT OPEN %s !"), NFA_REGEXP_DEBUG_LOG_NAME); |
4444 | 2877 return FALSE; |
2878 } | |
2879 #endif | |
2880 | |
2881 /* Allocate memory for the lists of nodes */ | |
2882 size = (nstate + 1) * sizeof(thread_T); | |
2883 list[0].t = (thread_T *)lalloc(size, TRUE); | |
2884 list[1].t = (thread_T *)lalloc(size, TRUE); | |
2885 list[2].t = (thread_T *)lalloc(size, TRUE); | |
2886 if (list[0].t == NULL || list[1].t == NULL || list[2].t == NULL) | |
2887 goto theend; | |
2888 vim_memset(list[0].t, 0, size); | |
2889 vim_memset(list[1].t, 0, size); | |
2890 vim_memset(list[2].t, 0, size); | |
2891 | |
2892 #ifdef ENABLE_LOG | |
2893 log_fd = fopen(LOG_NAME, "a"); | |
2894 if (log_fd != NULL) | |
2895 { | |
2896 fprintf(log_fd, "**********************************\n"); | |
2897 nfa_set_code(start->c); | |
2898 fprintf(log_fd, " RUNNING nfa_regmatch() starting with state %d, code %s\n", | |
2899 abs(start->id), code); | |
2900 fprintf(log_fd, "**********************************\n"); | |
2901 } | |
2902 else | |
2903 { | |
2904 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); | |
2905 log_fd = stderr; | |
2906 } | |
2907 #endif | |
2908 | |
2909 thislist = &list[0]; | |
2910 thislist->n = 0; | |
2911 nextlist = &list[1]; | |
2912 nextlist->n = 0; | |
2913 neglist = &list[2]; | |
2914 neglist->n = 0; | |
2915 #ifdef ENABLE_LOG | |
2916 fprintf(log_fd, "(---) STARTSTATE\n"); | |
2917 #endif | |
2918 addstate(thislist, start, m, 0, listid, &match); | |
2919 | |
2920 /* There are two cases when the NFA advances: 1. input char matches the | |
2921 * NFA node and 2. input char does not match the NFA node, but the next | |
2922 * node is NFA_NOT. The following macro calls addstate() according to | |
2923 * these rules. It is used A LOT, so use the "listtbl" table for speed */ | |
2924 listtbl[0][0] = NULL; | |
2925 listtbl[0][1] = neglist; | |
2926 listtbl[1][0] = nextlist; | |
2927 listtbl[1][1] = NULL; | |
2928 #define ADD_POS_NEG_STATE(node) \ | |
2929 ll = listtbl[result ? 1 : 0][node->negated]; \ | |
2930 if (ll != NULL) \ | |
2931 addstate(ll, node->out , &t->sub, n, listid + 1, &match); | |
2932 | |
2933 | |
2934 /* | |
2935 * Run for each character. | |
2936 */ | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2937 for (;;) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2938 { |
4444 | 2939 #ifdef FEAT_MBYTE |
2940 if (has_mbyte) | |
2941 { | |
2942 c = (*mb_ptr2char)(reginput); | |
2943 n = (*mb_ptr2len)(reginput); | |
2944 } | |
2945 else | |
2946 #endif | |
2947 { | |
2948 c = *reginput; | |
2949 n = 1; | |
2950 } | |
2951 if (c == NUL) | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2952 { |
4444 | 2953 n = 0; |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2954 go_to_nextline = FALSE; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
2955 } |
4444 | 2956 |
2957 /* swap lists */ | |
2958 thislist = &list[flag]; | |
2959 nextlist = &list[flag ^= 1]; | |
2960 nextlist->n = 0; /* `clear' nextlist */ | |
2961 listtbl[1][0] = nextlist; | |
2962 ++listid; | |
2963 | |
2964 #ifdef ENABLE_LOG | |
2965 fprintf(log_fd, "------------------------------------------\n"); | |
2966 fprintf(log_fd, ">>> Reginput is \"%s\"\n", reginput); | |
2967 fprintf(log_fd, ">>> Advanced one character ... Current char is %c (code %d) \n", c, (int)c); | |
2968 fprintf(log_fd, ">>> Thislist has %d states available: ", thislist->n); | |
2969 for (i = 0; i< thislist->n; i++) | |
2970 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); | |
2971 fprintf(log_fd, "\n"); | |
2972 #endif | |
2973 | |
4460 | 2974 #ifdef NFA_REGEXP_DEBUG_LOG |
4444 | 2975 fprintf(debug, "\n-------------------\n"); |
2976 #endif | |
4480 | 2977 /* |
2978 * If the state lists are empty we can stop. | |
2979 */ | |
2980 if (thislist->n == 0 && neglist->n == 0) | |
2981 break; | |
4444 | 2982 |
2983 /* compute nextlist */ | |
2984 for (i = 0; i < thislist->n || neglist->n > 0; ++i) | |
2985 { | |
2986 if (neglist->n > 0) | |
2987 { | |
2988 t = &neglist->t[0]; | |
4468 | 2989 neglist->n--; |
4444 | 2990 i--; |
2991 } | |
2992 else | |
2993 t = &thislist->t[i]; | |
2994 | |
4460 | 2995 #ifdef NFA_REGEXP_DEBUG_LOG |
4444 | 2996 nfa_set_code(t->state->c); |
2997 fprintf(debug, "%s, ", code); | |
2998 #endif | |
2999 #ifdef ENABLE_LOG | |
3000 nfa_set_code(t->state->c); | |
3001 fprintf(log_fd, "(%d) %s, code %d ... \n", abs(t->state->id), | |
3002 code, (int)t->state->c); | |
3003 #endif | |
3004 | |
3005 /* | |
3006 * Handle the possible codes of the current state. | |
3007 * The most important is NFA_MATCH. | |
3008 */ | |
3009 switch (t->state->c) | |
3010 { | |
3011 case NFA_MATCH: | |
3012 match = TRUE; | |
3013 *submatch = t->sub; | |
3014 #ifdef ENABLE_LOG | |
3015 for (j = 0; j < 4; j++) | |
3016 if (REG_MULTI) | |
3017 fprintf(log_fd, "\n *** group %d, start: c=%d, l=%d, end: c=%d, l=%d", | |
3018 j, | |
3019 t->sub.startpos[j].col, | |
3020 (int)t->sub.startpos[j].lnum, | |
3021 t->sub.endpos[j].col, | |
3022 (int)t->sub.endpos[j].lnum); | |
3023 else | |
3024 fprintf(log_fd, "\n *** group %d, start: \"%s\", end: \"%s\"", | |
3025 j, | |
3026 (char *)t->sub.start[j], | |
3027 (char *)t->sub.end[j]); | |
3028 fprintf(log_fd, "\n"); | |
3029 #endif | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3030 /* Found the left-most longest match, do not look at any other |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3031 * states at this position. */ |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3032 goto nextchar; |
4444 | 3033 |
3034 case NFA_END_INVISIBLE: | |
3035 /* This is only encountered after a NFA_START_INVISIBLE node. | |
3036 * They surround a zero-width group, used with "\@=" and "\&". | |
3037 * If we got here, it means that the current "invisible" group | |
3038 * finished successfully, so return control to the parent | |
3039 * nfa_regmatch(). Submatches are stored in *m, and used in | |
3040 * the parent call. */ | |
3041 if (start->c == NFA_MOPEN + 0) | |
3042 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3043 &match); | |
3044 else | |
3045 { | |
3046 *m = t->sub; | |
3047 match = TRUE; | |
3048 } | |
3049 break; | |
3050 | |
3051 case NFA_START_INVISIBLE: | |
3052 /* Save global variables, and call nfa_regmatch() to check if | |
3053 * the current concat matches at this position. The concat | |
3054 * ends with the node NFA_END_INVISIBLE */ | |
3055 old_reginput = reginput; | |
3056 old_regline = regline; | |
3057 old_reglnum = reglnum; | |
3058 if (listids == NULL) | |
3059 { | |
3060 listids = (int *) lalloc(sizeof(int) * nstate, TRUE); | |
3061 if (listids == NULL) | |
3062 { | |
3063 EMSG(_("E878: (NFA) Could not allocate memory for branch traversal!")); | |
3064 return 0; | |
3065 } | |
3066 } | |
3067 #ifdef ENABLE_LOG | |
3068 if (log_fd != stderr) | |
3069 fclose(log_fd); | |
3070 log_fd = NULL; | |
3071 #endif | |
3072 /* Have to clear the listid field of the NFA nodes, so that | |
3073 * nfa_regmatch() and addstate() can run properly after | |
3074 * recursion. */ | |
3075 nfa_save_listids(start, listids); | |
3076 nfa_set_null_listids(start); | |
3077 result = nfa_regmatch(t->state->out, submatch, m); | |
3078 nfa_set_neg_listids(start); | |
3079 nfa_restore_listids(start, listids); | |
3080 | |
3081 #ifdef ENABLE_LOG | |
3082 log_fd = fopen(LOG_NAME, "a"); | |
3083 if (log_fd != NULL) | |
3084 { | |
3085 fprintf(log_fd, "****************************\n"); | |
3086 fprintf(log_fd, "FINISHED RUNNING nfa_regmatch() recursively\n"); | |
3087 fprintf(log_fd, "MATCH = %s\n", result == TRUE ? "OK" : "FALSE"); | |
3088 fprintf(log_fd, "****************************\n"); | |
3089 } | |
3090 else | |
3091 { | |
3092 EMSG(_("Could not open temporary log file for writing, displaying on stderr ... ")); | |
3093 log_fd = stderr; | |
3094 } | |
3095 #endif | |
3096 if (result == TRUE) | |
3097 { | |
3098 /* Restore position in input text */ | |
3099 reginput = old_reginput; | |
3100 regline = old_regline; | |
3101 reglnum = old_reglnum; | |
3102 /* Copy submatch info from the recursive call */ | |
3103 if (REG_MULTI) | |
3104 for (j = 1; j < NSUBEXP; j++) | |
3105 { | |
3106 t->sub.startpos[j] = m->startpos[j]; | |
3107 t->sub.endpos[j] = m->endpos[j]; | |
3108 } | |
3109 else | |
3110 for (j = 1; j < NSUBEXP; j++) | |
3111 { | |
3112 t->sub.start[j] = m->start[j]; | |
3113 t->sub.end[j] = m->end[j]; | |
3114 } | |
3115 /* t->state->out1 is the corresponding END_INVISIBLE node */ | |
3116 addstate(thislist, t->state->out1->out, &t->sub, 0, listid, | |
3117 &match); | |
3118 } | |
3119 else | |
3120 { | |
3121 /* continue with next input char */ | |
3122 reginput = old_reginput; | |
3123 } | |
3124 break; | |
3125 | |
3126 case NFA_BOL: | |
3127 if (reginput == regline) | |
3128 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3129 &match); | |
3130 break; | |
3131 | |
3132 case NFA_EOL: | |
3133 if (c == NUL) | |
3134 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3135 &match); | |
3136 break; | |
3137 | |
3138 case NFA_BOW: | |
3139 { | |
3140 int bow = TRUE; | |
3141 | |
3142 if (c == NUL) | |
3143 bow = FALSE; | |
3144 #ifdef FEAT_MBYTE | |
3145 else if (has_mbyte) | |
3146 { | |
3147 int this_class; | |
3148 | |
3149 /* Get class of current and previous char (if it exists). */ | |
4503 | 3150 this_class = mb_get_class_buf(reginput, reg_buf); |
4444 | 3151 if (this_class <= 1) |
3152 bow = FALSE; | |
3153 else if (reg_prev_class() == this_class) | |
3154 bow = FALSE; | |
3155 } | |
3156 #endif | |
4503 | 3157 else if (!vim_iswordc_buf(c, reg_buf) |
3158 || (reginput > regline | |
3159 && vim_iswordc_buf(reginput[-1], reg_buf))) | |
4444 | 3160 bow = FALSE; |
3161 if (bow) | |
3162 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3163 &match); | |
3164 break; | |
3165 } | |
3166 | |
3167 case NFA_EOW: | |
3168 { | |
3169 int eow = TRUE; | |
3170 | |
3171 if (reginput == regline) | |
3172 eow = FALSE; | |
3173 #ifdef FEAT_MBYTE | |
3174 else if (has_mbyte) | |
3175 { | |
3176 int this_class, prev_class; | |
3177 | |
3178 /* Get class of current and previous char (if it exists). */ | |
4503 | 3179 this_class = mb_get_class_buf(reginput, reg_buf); |
4444 | 3180 prev_class = reg_prev_class(); |
3181 if (this_class == prev_class | |
3182 || prev_class == 0 || prev_class == 1) | |
3183 eow = FALSE; | |
3184 } | |
3185 #endif | |
4503 | 3186 else if (!vim_iswordc_buf(reginput[-1], reg_buf) |
3187 || (reginput[0] != NUL && vim_iswordc_buf(c, reg_buf))) | |
4444 | 3188 eow = FALSE; |
3189 if (eow) | |
3190 addstate(thislist, t->state->out, &t->sub, 0, listid, | |
3191 &match); | |
3192 break; | |
3193 } | |
3194 | |
3195 case NFA_MULTIBYTE: | |
3196 case NFA_COMPOSING: | |
3197 switch (t->state->c) | |
3198 { | |
3199 case NFA_MULTIBYTE: endnode = NFA_END_MULTIBYTE; break; | |
3200 case NFA_COMPOSING: endnode = NFA_END_COMPOSING; break; | |
3201 default: endnode = 0; | |
3202 } | |
3203 | |
3204 result = OK; | |
3205 sta = t->state->out; | |
3206 len = 1; | |
3207 while (sta->c != endnode && len <= n) | |
3208 { | |
3209 if (reginput[len-1] != sta->c) | |
3210 { | |
3211 result = OK - 1; | |
3212 break; | |
3213 } | |
3214 len++; | |
3215 sta = sta->out; | |
3216 } | |
3217 | |
3218 /* if input char length doesn't match regexp char length */ | |
3219 if (len -1 < n || sta->c != endnode) | |
3220 result = OK - 1; | |
3221 end = t->state->out1; /* NFA_END_MULTIBYTE or | |
3222 NFA_END_COMPOSING */ | |
3223 /* If \Z was present, then ignore composing characters */ | |
3224 if (regflags & RF_ICOMBINE) | |
3225 result = 1 ^ sta->negated; | |
3226 ADD_POS_NEG_STATE(end); | |
3227 break; | |
3228 | |
3229 case NFA_NEWL: | |
3230 if (!reg_line_lbr && REG_MULTI | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3231 && c == NUL && reglnum <= reg_maxline) |
4444 | 3232 { |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3233 go_to_nextline = TRUE; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3234 /* Pass -1 for the offset, which means taking the position |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3235 * at the start of the next line. */ |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3236 addstate(nextlist, t->state->out, &t->sub, -1, |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3237 listid + 1, &match); |
4444 | 3238 } |
3239 break; | |
3240 | |
3241 case NFA_CLASS_ALNUM: | |
3242 case NFA_CLASS_ALPHA: | |
3243 case NFA_CLASS_BLANK: | |
3244 case NFA_CLASS_CNTRL: | |
3245 case NFA_CLASS_DIGIT: | |
3246 case NFA_CLASS_GRAPH: | |
3247 case NFA_CLASS_LOWER: | |
3248 case NFA_CLASS_PRINT: | |
3249 case NFA_CLASS_PUNCT: | |
3250 case NFA_CLASS_SPACE: | |
3251 case NFA_CLASS_UPPER: | |
3252 case NFA_CLASS_XDIGIT: | |
3253 case NFA_CLASS_TAB: | |
3254 case NFA_CLASS_RETURN: | |
3255 case NFA_CLASS_BACKSPACE: | |
3256 case NFA_CLASS_ESCAPE: | |
3257 result = check_char_class(t->state->c, c); | |
3258 ADD_POS_NEG_STATE(t->state); | |
3259 break; | |
3260 | |
3261 case NFA_END_NEG_RANGE: | |
3262 /* This follows a series of negated nodes, like: | |
3263 * CHAR(x), NFA_NOT, CHAR(y), NFA_NOT etc. */ | |
3264 if (c > 0) | |
3265 addstate(nextlist, t->state->out, &t->sub, n, listid + 1, | |
3266 &match); | |
3267 break; | |
3268 | |
3269 case NFA_ANY: | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3270 /* Any char except '\0', (end of input) does not match. */ |
4444 | 3271 if (c > 0) |
3272 addstate(nextlist, t->state->out, &t->sub, n, listid + 1, | |
3273 &match); | |
3274 break; | |
3275 | |
3276 /* | |
3277 * Character classes like \a for alpha, \d for digit etc. | |
3278 */ | |
3279 case NFA_IDENT: /* \i */ | |
3280 result = vim_isIDc(c); | |
3281 ADD_POS_NEG_STATE(t->state); | |
3282 break; | |
3283 | |
3284 case NFA_SIDENT: /* \I */ | |
3285 result = !VIM_ISDIGIT(c) && vim_isIDc(c); | |
3286 ADD_POS_NEG_STATE(t->state); | |
3287 break; | |
3288 | |
3289 case NFA_KWORD: /* \k */ | |
4503 | 3290 result = vim_iswordp_buf(reginput, reg_buf); |
4444 | 3291 ADD_POS_NEG_STATE(t->state); |
3292 break; | |
3293 | |
3294 case NFA_SKWORD: /* \K */ | |
4503 | 3295 result = !VIM_ISDIGIT(c) && vim_iswordp_buf(reginput, reg_buf); |
4444 | 3296 ADD_POS_NEG_STATE(t->state); |
3297 break; | |
3298 | |
3299 case NFA_FNAME: /* \f */ | |
3300 result = vim_isfilec(c); | |
3301 ADD_POS_NEG_STATE(t->state); | |
3302 break; | |
3303 | |
3304 case NFA_SFNAME: /* \F */ | |
3305 result = !VIM_ISDIGIT(c) && vim_isfilec(c); | |
3306 ADD_POS_NEG_STATE(t->state); | |
3307 break; | |
3308 | |
3309 case NFA_PRINT: /* \p */ | |
4470 | 3310 result = ptr2cells(reginput) == 1; |
4444 | 3311 ADD_POS_NEG_STATE(t->state); |
3312 break; | |
3313 | |
3314 case NFA_SPRINT: /* \P */ | |
4470 | 3315 result = !VIM_ISDIGIT(c) && ptr2cells(reginput) == 1; |
4444 | 3316 ADD_POS_NEG_STATE(t->state); |
3317 break; | |
3318 | |
3319 case NFA_WHITE: /* \s */ | |
3320 result = vim_iswhite(c); | |
3321 ADD_POS_NEG_STATE(t->state); | |
3322 break; | |
3323 | |
3324 case NFA_NWHITE: /* \S */ | |
3325 result = c != NUL && !vim_iswhite(c); | |
3326 ADD_POS_NEG_STATE(t->state); | |
3327 break; | |
3328 | |
3329 case NFA_DIGIT: /* \d */ | |
3330 result = ri_digit(c); | |
3331 ADD_POS_NEG_STATE(t->state); | |
3332 break; | |
3333 | |
3334 case NFA_NDIGIT: /* \D */ | |
3335 result = c != NUL && !ri_digit(c); | |
3336 ADD_POS_NEG_STATE(t->state); | |
3337 break; | |
3338 | |
3339 case NFA_HEX: /* \x */ | |
3340 result = ri_hex(c); | |
3341 ADD_POS_NEG_STATE(t->state); | |
3342 break; | |
3343 | |
3344 case NFA_NHEX: /* \X */ | |
3345 result = c != NUL && !ri_hex(c); | |
3346 ADD_POS_NEG_STATE(t->state); | |
3347 break; | |
3348 | |
3349 case NFA_OCTAL: /* \o */ | |
3350 result = ri_octal(c); | |
3351 ADD_POS_NEG_STATE(t->state); | |
3352 break; | |
3353 | |
3354 case NFA_NOCTAL: /* \O */ | |
3355 result = c != NUL && !ri_octal(c); | |
3356 ADD_POS_NEG_STATE(t->state); | |
3357 break; | |
3358 | |
3359 case NFA_WORD: /* \w */ | |
3360 result = ri_word(c); | |
3361 ADD_POS_NEG_STATE(t->state); | |
3362 break; | |
3363 | |
3364 case NFA_NWORD: /* \W */ | |
3365 result = c != NUL && !ri_word(c); | |
3366 ADD_POS_NEG_STATE(t->state); | |
3367 break; | |
3368 | |
3369 case NFA_HEAD: /* \h */ | |
3370 result = ri_head(c); | |
3371 ADD_POS_NEG_STATE(t->state); | |
3372 break; | |
3373 | |
3374 case NFA_NHEAD: /* \H */ | |
3375 result = c != NUL && !ri_head(c); | |
3376 ADD_POS_NEG_STATE(t->state); | |
3377 break; | |
3378 | |
3379 case NFA_ALPHA: /* \a */ | |
3380 result = ri_alpha(c); | |
3381 ADD_POS_NEG_STATE(t->state); | |
3382 break; | |
3383 | |
3384 case NFA_NALPHA: /* \A */ | |
3385 result = c != NUL && !ri_alpha(c); | |
3386 ADD_POS_NEG_STATE(t->state); | |
3387 break; | |
3388 | |
3389 case NFA_LOWER: /* \l */ | |
3390 result = ri_lower(c); | |
3391 ADD_POS_NEG_STATE(t->state); | |
3392 break; | |
3393 | |
3394 case NFA_NLOWER: /* \L */ | |
3395 result = c != NUL && !ri_lower(c); | |
3396 ADD_POS_NEG_STATE(t->state); | |
3397 break; | |
3398 | |
3399 case NFA_UPPER: /* \u */ | |
3400 result = ri_upper(c); | |
3401 ADD_POS_NEG_STATE(t->state); | |
3402 break; | |
3403 | |
3404 case NFA_NUPPER: /* \U */ | |
3405 result = c != NUL && !ri_upper(c); | |
3406 ADD_POS_NEG_STATE(t->state); | |
3407 break; | |
3408 | |
4482 | 3409 case NFA_MOPEN + 0: |
3410 case NFA_MOPEN + 1: | |
3411 case NFA_MOPEN + 2: | |
3412 case NFA_MOPEN + 3: | |
3413 case NFA_MOPEN + 4: | |
3414 case NFA_MOPEN + 5: | |
3415 case NFA_MOPEN + 6: | |
3416 case NFA_MOPEN + 7: | |
3417 case NFA_MOPEN + 8: | |
3418 case NFA_MOPEN + 9: | |
3419 /* handled below */ | |
3420 break; | |
3421 | |
3422 case NFA_SKIP_CHAR: | |
3423 case NFA_ZSTART: | |
3424 /* TODO: should not happen? */ | |
3425 break; | |
3426 | |
4444 | 3427 default: /* regular character */ |
4482 | 3428 /* TODO: put this in #ifdef later */ |
3429 if (t->state->c < -256) | |
3430 EMSGN("INTERNAL: Negative state char: %ld", t->state->c); | |
4444 | 3431 result = (no_Magic(t->state->c) == c); |
4482 | 3432 |
4444 | 3433 if (!result) |
3434 result = ireg_ic == TRUE | |
3435 && MB_TOLOWER(t->state->c) == MB_TOLOWER(c); | |
3436 ADD_POS_NEG_STATE(t->state); | |
3437 break; | |
3438 } | |
3439 | |
3440 } /* for (thislist = thislist; thislist->state; thislist++) */ | |
3441 | |
3442 /* The first found match is the leftmost one, but there may be a | |
3443 * longer one. Keep running the NFA, but don't start from the | |
3444 * beginning. Also, do not add the start state in recursive calls of | |
3445 * nfa_regmatch(), because recursive calls should only start in the | |
3446 * first position. */ | |
3447 if (match == FALSE && start->c == NFA_MOPEN + 0) | |
3448 { | |
3449 #ifdef ENABLE_LOG | |
3450 fprintf(log_fd, "(---) STARTSTATE\n"); | |
3451 #endif | |
3452 addstate(nextlist, start, m, n, listid + 1, &match); | |
3453 } | |
3454 | |
3455 #ifdef ENABLE_LOG | |
3456 fprintf(log_fd, ">>> Thislist had %d states available: ", thislist->n); | |
3457 for (i = 0; i< thislist->n; i++) | |
3458 fprintf(log_fd, "%d ", abs(thislist->t[i].state->id)); | |
3459 fprintf(log_fd, "\n"); | |
3460 #endif | |
3461 | |
3462 nextchar: | |
4515
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3463 /* Advance to the next character, or advance to the next line, or |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3464 * finish. */ |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3465 if (n != 0) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3466 reginput += n; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3467 else if (go_to_nextline) |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3468 reg_nextline(); |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3469 else |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3470 break; |
90e9917d4114
updated for version 7.3.1005
Bram Moolenaar <bram@vim.org>
parents:
4507
diff
changeset
|
3471 } |
4444 | 3472 |
3473 #ifdef ENABLE_LOG | |
3474 if (log_fd != stderr) | |
3475 fclose(log_fd); | |
3476 log_fd = NULL; | |
3477 #endif | |
3478 | |
3479 theend: | |
3480 /* Free memory */ | |
3481 vim_free(list[0].t); | |
3482 vim_free(list[1].t); | |
3483 vim_free(list[2].t); | |
3484 list[0].t = list[1].t = list[2].t = NULL; | |
3485 if (listids != NULL) | |
3486 vim_free(listids); | |
3487 #undef ADD_POS_NEG_STATE | |
4460 | 3488 #ifdef NFA_REGEXP_DEBUG_LOG |
4444 | 3489 fclose(debug); |
3490 #endif | |
3491 | |
3492 return match; | |
3493 } | |
3494 | |
3495 /* | |
3496 * Try match of "prog" with at regline["col"]. | |
3497 * Returns 0 for failure, number of lines contained in the match otherwise. | |
3498 */ | |
3499 static long | |
3500 nfa_regtry(start, col) | |
3501 nfa_state_T *start; | |
3502 colnr_T col; | |
3503 { | |
3504 int i; | |
3505 regsub_T sub, m; | |
3506 #ifdef ENABLE_LOG | |
3507 FILE *f; | |
3508 #endif | |
3509 | |
3510 reginput = regline + col; | |
3511 need_clear_subexpr = TRUE; | |
3512 | |
3513 #ifdef ENABLE_LOG | |
3514 f = fopen(LOG_NAME, "a"); | |
3515 if (f != NULL) | |
3516 { | |
3517 fprintf(f, "\n\n\n\n\n\n\t\t=======================================================\n"); | |
3518 fprintf(f, " =======================================================\n"); | |
3519 #ifdef DEBUG | |
3520 fprintf(f, "\tRegexp is \"%s\"\n", nfa_regengine.expr); | |
3521 #endif | |
3522 fprintf(f, "\tInput text is \"%s\" \n", reginput); | |
3523 fprintf(f, " =======================================================\n\n\n\n\n\n\n"); | |
3524 nfa_print_state(f, start, 0); | |
3525 fprintf(f, "\n\n"); | |
3526 fclose(f); | |
3527 } | |
3528 else | |
3529 EMSG(_("Could not open temporary log file for writing ")); | |
3530 #endif | |
3531 | |
3532 if (REG_MULTI) | |
3533 { | |
3534 /* Use 0xff to set lnum to -1 */ | |
3535 vim_memset(sub.startpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3536 vim_memset(sub.endpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3537 vim_memset(m.startpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3538 vim_memset(m.endpos, 0xff, sizeof(lpos_T) * NSUBEXP); | |
3539 } | |
3540 else | |
3541 { | |
3542 vim_memset(sub.start, 0, sizeof(char_u *) * NSUBEXP); | |
3543 vim_memset(sub.end, 0, sizeof(char_u *) * NSUBEXP); | |
3544 vim_memset(m.start, 0, sizeof(char_u *) * NSUBEXP); | |
3545 vim_memset(m.end, 0, sizeof(char_u *) * NSUBEXP); | |
3546 } | |
3547 | |
3548 if (nfa_regmatch(start, &sub, &m) == FALSE) | |
3549 return 0; | |
3550 | |
3551 cleanup_subexpr(); | |
3552 if (REG_MULTI) | |
3553 { | |
3554 for (i = 0; i < NSUBEXP; i++) | |
3555 { | |
3556 reg_startpos[i] = sub.startpos[i]; | |
3557 reg_endpos[i] = sub.endpos[i]; | |
3558 } | |
3559 | |
3560 if (reg_startpos[0].lnum < 0) | |
3561 { | |
3562 reg_startpos[0].lnum = 0; | |
3563 reg_startpos[0].col = col; | |
3564 } | |
3565 if (reg_endpos[0].lnum < 0) | |
3566 { | |
3567 reg_endpos[0].lnum = reglnum; | |
3568 reg_endpos[0].col = (int)(reginput - regline); | |
3569 } | |
3570 else | |
3571 /* Use line number of "\ze". */ | |
3572 reglnum = reg_endpos[0].lnum; | |
3573 } | |
3574 else | |
3575 { | |
3576 for (i = 0; i < NSUBEXP; i++) | |
3577 { | |
3578 reg_startp[i] = sub.start[i]; | |
3579 reg_endp[i] = sub.end[i]; | |
3580 } | |
3581 | |
3582 if (reg_startp[0] == NULL) | |
3583 reg_startp[0] = regline + col; | |
3584 if (reg_endp[0] == NULL) | |
3585 reg_endp[0] = reginput; | |
3586 } | |
3587 | |
3588 return 1 + reglnum; | |
3589 } | |
3590 | |
3591 /* | |
3592 * Match a regexp against a string ("line" points to the string) or multiple | |
3593 * lines ("line" is NULL, use reg_getline()). | |
3594 * | |
3595 * Returns 0 for failure, number of lines contained in the match otherwise. | |
3596 */ | |
3597 static long | |
3598 nfa_regexec_both(line, col) | |
3599 char_u *line; | |
3600 colnr_T col; /* column to start looking for match */ | |
3601 { | |
3602 nfa_regprog_T *prog; | |
3603 long retval = 0L; | |
3604 int i; | |
3605 | |
3606 if (REG_MULTI) | |
3607 { | |
3608 prog = (nfa_regprog_T *)reg_mmatch->regprog; | |
3609 line = reg_getline((linenr_T)0); /* relative to the cursor */ | |
3610 reg_startpos = reg_mmatch->startpos; | |
3611 reg_endpos = reg_mmatch->endpos; | |
3612 } | |
3613 else | |
3614 { | |
3615 prog = (nfa_regprog_T *)reg_match->regprog; | |
3616 reg_startp = reg_match->startp; | |
3617 reg_endp = reg_match->endp; | |
3618 } | |
3619 | |
3620 /* Be paranoid... */ | |
3621 if (prog == NULL || line == NULL) | |
3622 { | |
3623 EMSG(_(e_null)); | |
3624 goto theend; | |
3625 } | |
3626 | |
3627 /* If the start column is past the maximum column: no need to try. */ | |
3628 if (ireg_maxcol > 0 && col >= ireg_maxcol) | |
3629 goto theend; | |
3630 | |
3631 /* If pattern contains "\c" or "\C": overrule value of ireg_ic */ | |
3632 if (prog->regflags & RF_ICASE) | |
3633 ireg_ic = TRUE; | |
3634 else if (prog->regflags & RF_NOICASE) | |
3635 ireg_ic = FALSE; | |
3636 | |
3637 #ifdef FEAT_MBYTE | |
3638 /* If pattern contains "\Z" overrule value of ireg_icombine */ | |
3639 if (prog->regflags & RF_ICOMBINE) | |
3640 ireg_icombine = TRUE; | |
3641 #endif | |
3642 | |
3643 regline = line; | |
3644 reglnum = 0; /* relative to line */ | |
3645 | |
3646 nstate = prog->nstate; | |
3647 | |
3648 for (i = 0; i < nstate; ++i) | |
3649 { | |
3650 prog->state[i].id = i; | |
3651 prog->state[i].lastlist = 0; | |
3652 prog->state[i].visits = 0; | |
3653 prog->state[i].lastthread = NULL; | |
3654 } | |
3655 | |
3656 retval = nfa_regtry(prog->start, col); | |
3657 | |
3658 theend: | |
3659 return retval; | |
3660 } | |
3661 | |
3662 /* | |
3663 * Compile a regular expression into internal code for the NFA matcher. | |
3664 * Returns the program in allocated space. Returns NULL for an error. | |
3665 */ | |
3666 static regprog_T * | |
3667 nfa_regcomp(expr, re_flags) | |
3668 char_u *expr; | |
3669 int re_flags; | |
3670 { | |
3671 nfa_regprog_T *prog; | |
4458 | 3672 size_t prog_size; |
4444 | 3673 int *postfix; |
3674 | |
3675 if (expr == NULL) | |
3676 return NULL; | |
3677 | |
3678 #ifdef DEBUG | |
3679 nfa_regengine.expr = expr; | |
3680 #endif | |
3681 | |
3682 init_class_tab(); | |
3683 | |
3684 if (nfa_regcomp_start(expr, re_flags) == FAIL) | |
3685 return NULL; | |
3686 | |
3687 /* Space for compiled regexp */ | |
3688 prog_size = sizeof(nfa_regprog_T) + sizeof(nfa_state_T) * nstate_max; | |
3689 prog = (nfa_regprog_T *)lalloc(prog_size, TRUE); | |
3690 if (prog == NULL) | |
3691 goto fail; | |
3692 vim_memset(prog, 0, prog_size); | |
3693 | |
3694 /* Build postfix form of the regexp. Needed to build the NFA | |
3695 * (and count its size) */ | |
3696 postfix = re2post(); | |
3697 if (postfix == NULL) | |
3698 goto fail; /* Cascaded (syntax?) error */ | |
3699 | |
3700 /* | |
3701 * In order to build the NFA, we parse the input regexp twice: | |
3702 * 1. first pass to count size (so we can allocate space) | |
3703 * 2. second to emit code | |
3704 */ | |
3705 #ifdef ENABLE_LOG | |
3706 { | |
3707 FILE *f = fopen(LOG_NAME, "a"); | |
3708 | |
3709 if (f != NULL) | |
3710 { | |
3711 fprintf(f, "\n*****************************\n\n\n\n\tCompiling regexp \"%s\" ... hold on !\n", expr); | |
3712 fclose(f); | |
3713 } | |
3714 } | |
3715 #endif | |
3716 | |
3717 /* | |
3718 * PASS 1 | |
3719 * Count number of NFA states in "nstate". Do not build the NFA. | |
3720 */ | |
3721 post2nfa(postfix, post_ptr, TRUE); | |
3722 state_ptr = prog->state; | |
3723 | |
3724 /* | |
3725 * PASS 2 | |
3726 * Build the NFA | |
3727 */ | |
3728 prog->start = post2nfa(postfix, post_ptr, FALSE); | |
3729 if (prog->start == NULL) | |
3730 goto fail; | |
3731 | |
3732 prog->regflags = regflags; | |
3733 prog->engine = &nfa_regengine; | |
3734 prog->nstate = nstate; | |
3735 #ifdef ENABLE_LOG | |
3736 nfa_postfix_dump(expr, OK); | |
3737 nfa_dump(prog); | |
3738 #endif | |
3739 | |
3740 out: | |
3741 vim_free(post_start); | |
3742 post_start = post_ptr = post_end = NULL; | |
3743 state_ptr = NULL; | |
3744 return (regprog_T *)prog; | |
3745 | |
3746 fail: | |
3747 vim_free(prog); | |
3748 prog = NULL; | |
3749 #ifdef ENABLE_LOG | |
3750 nfa_postfix_dump(expr, FAIL); | |
3751 #endif | |
3752 #ifdef DEBUG | |
3753 nfa_regengine.expr = NULL; | |
3754 #endif | |
3755 goto out; | |
3756 } | |
3757 | |
3758 | |
3759 /* | |
3760 * Match a regexp against a string. | |
3761 * "rmp->regprog" is a compiled regexp as returned by nfa_regcomp(). | |
3762 * Uses curbuf for line count and 'iskeyword'. | |
3763 * | |
3764 * Return TRUE if there is a match, FALSE if not. | |
3765 */ | |
3766 static int | |
3767 nfa_regexec(rmp, line, col) | |
3768 regmatch_T *rmp; | |
3769 char_u *line; /* string to match against */ | |
3770 colnr_T col; /* column to start looking for match */ | |
3771 { | |
3772 reg_match = rmp; | |
3773 reg_mmatch = NULL; | |
3774 reg_maxline = 0; | |
3775 reg_line_lbr = FALSE; | |
3776 reg_buf = curbuf; | |
3777 reg_win = NULL; | |
3778 ireg_ic = rmp->rm_ic; | |
3779 #ifdef FEAT_MBYTE | |
3780 ireg_icombine = FALSE; | |
3781 #endif | |
3782 ireg_maxcol = 0; | |
3783 return (nfa_regexec_both(line, col) != 0); | |
3784 } | |
3785 | |
3786 #if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) \ | |
3787 || defined(FIND_REPLACE_DIALOG) || defined(PROTO) | |
3788 | |
3789 static int nfa_regexec_nl __ARGS((regmatch_T *rmp, char_u *line, colnr_T col)); | |
3790 | |
3791 /* | |
3792 * Like nfa_regexec(), but consider a "\n" in "line" to be a line break. | |
3793 */ | |
3794 static int | |
3795 nfa_regexec_nl(rmp, line, col) | |
3796 regmatch_T *rmp; | |
3797 char_u *line; /* string to match against */ | |
3798 colnr_T col; /* column to start looking for match */ | |
3799 { | |
3800 reg_match = rmp; | |
3801 reg_mmatch = NULL; | |
3802 reg_maxline = 0; | |
3803 reg_line_lbr = TRUE; | |
3804 reg_buf = curbuf; | |
3805 reg_win = NULL; | |
3806 ireg_ic = rmp->rm_ic; | |
3807 #ifdef FEAT_MBYTE | |
3808 ireg_icombine = FALSE; | |
3809 #endif | |
3810 ireg_maxcol = 0; | |
3811 return (nfa_regexec_both(line, col) != 0); | |
3812 } | |
3813 #endif | |
3814 | |
3815 | |
3816 /* | |
3817 * Match a regexp against multiple lines. | |
3818 * "rmp->regprog" is a compiled regexp as returned by vim_regcomp(). | |
3819 * Uses curbuf for line count and 'iskeyword'. | |
3820 * | |
3821 * Return zero if there is no match. Return number of lines contained in the | |
3822 * match otherwise. | |
3823 * | |
3824 * Note: the body is the same as bt_regexec() except for nfa_regexec_both() | |
3825 * | |
3826 * ! Also NOTE : match may actually be in another line. e.g.: | |
3827 * when r.e. is \nc, cursor is at 'a' and the text buffer looks like | |
3828 * | |
3829 * +-------------------------+ | |
3830 * |a | | |
3831 * |b | | |
3832 * |c | | |
3833 * | | | |
3834 * +-------------------------+ | |
3835 * | |
3836 * then nfa_regexec_multi() returns 3. while the original | |
3837 * vim_regexec_multi() returns 0 and a second call at line 2 will return 2. | |
3838 * | |
3839 * FIXME if this behavior is not compatible. | |
3840 */ | |
3841 static long | |
3842 nfa_regexec_multi(rmp, win, buf, lnum, col, tm) | |
3843 regmmatch_T *rmp; | |
3844 win_T *win; /* window in which to search or NULL */ | |
3845 buf_T *buf; /* buffer in which to search */ | |
3846 linenr_T lnum; /* nr of line to start looking for match */ | |
3847 colnr_T col; /* column to start looking for match */ | |
3848 proftime_T *tm UNUSED; /* timeout limit or NULL */ | |
3849 { | |
3850 reg_match = NULL; | |
3851 reg_mmatch = rmp; | |
3852 reg_buf = buf; | |
3853 reg_win = win; | |
3854 reg_firstlnum = lnum; | |
3855 reg_maxline = reg_buf->b_ml.ml_line_count - lnum; | |
3856 reg_line_lbr = FALSE; | |
3857 ireg_ic = rmp->rmm_ic; | |
3858 #ifdef FEAT_MBYTE | |
3859 ireg_icombine = FALSE; | |
3860 #endif | |
3861 ireg_maxcol = rmp->rmm_maxcol; | |
3862 | |
4503 | 3863 return nfa_regexec_both(NULL, col); |
4444 | 3864 } |
3865 | |
3866 #ifdef DEBUG | |
3867 # undef ENABLE_LOG | |
3868 #endif |