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